Drawing examples
Fall 2007, David Matuszek

 

import java.awt.*;
import javax.swing.*;

/**
 * Drawing examples.
 * 
 * @author Dave Matuszek
 * @version Sept 2, 2007
 */
public class MyDrawing extends JApplet {
 
    public void paint(Graphics g) {
        // Set the desired size of this applet
        this.setSize(400, 300);
        // Erase everything (to white)
        g.clearRect(0, 0, 1000, 1000);
        
        // Black rectangle outline
        g.setColor(Color.BLACK);
        g.drawRect(10, 10, 80, 80); // x, y, width, height
        
        // Blue filled rectangle
        g.setColor(Color.BLUE);
        g.fillRect(110, 10, 80, 80); // x, y, width, height
        
        // Dark gray circle outline
        g.setColor(Color.DARK_GRAY);
        g.drawOval(210, 10, 80, 80); // x, y, width, height
        
        // Cyan filled oval
        g.setColor(Color.CYAN);
        g.fillOval(320, 10, 60, 80); // x, y, width, height
        
        // Light gray portion of an arc
        // startAngle, degrees, x, y, width of oval, height of oval
        g.setColor(Color.LIGHT_GRAY);
        g.drawArc(10, 110, 80, 80, 0, 45);
        
        // Magenta filled segment
        // startAngle, degrees, x, y, width of oval, height of oval
        g.setColor(Color.MAGENTA);
        g.fillArc(110, 110, 80, 80, 45, 90);
        
        // Orange rounded rectangle outline
        // x, y, width, height, arcWidth, arcHeight
        g.setColor(Color.ORANGE);
        g.drawRoundRect(210, 110, 80, 80, 40, 40);
        
        // Pink filled rounded rectangle
        // x, y, width, height, arcWidth, arcHeight
        g.setColor(Color.PINK);
        g.fillRoundRect(310, 110, 80, 80, 20, 60);
        
        // Red line
        g.setColor(Color.RED);
        g.drawLine(10, 210, 90, 290); // startX, startY, endX, endY
        
        // Text (still in red)
        g.drawString("ABC123", 110, 210 + 50); // text, startX, startY
        
        // Black triangle outline
        // X values, Y values, number of points
        g.setColor(Color.BLACK);
        g.drawPolygon(new int[] { 250, 290, 210 },
                      new int[] { 210, 290, 290 }, 3);

        // Yellow filled star
        // X values, Y values, number of points
        g.setColor(Color.YELLOW);
        g.fillPolygon(new int[] { 320, 350, 380, 310, 390 },
                      new int[] { 290, 210, 290, 240, 240 }, 5);
    }
}