CSE142—Computer Programming I

Sample Graphics Programs

Program File Draw1.java

// This is a simple drawing program that draws three versions of a particular

// subfigure on the screen.  In this unstructured version of the program

// all of the drawing commands appear in the main method.

 

import java.awt.*;

 

public class Draw1 {

    public static void main(String[] args) {

        DrawingPanel p = new DrawingPanel(500, 400);

        p.setBackground(Color.CYAN);

        Graphics g = p.getGraphics();

 

        // upper-left is (50, 50)

        // lower-right is (150, 150)

        g.setColor(Color.RED);

        g.fillRect(50, 50, 100, 100);

        g.setColor(Color.GREEN);

        g.fillOval(50, 50, 100, 100);

        g.setColor(Color.BLACK);

        g.drawRect(50, 50, 100, 100);

        g.drawOval(50, 50, 100, 100);

        g.drawLine(50, 50, 150, 150);

        g.drawLine(50, 150, 150, 50);

 

        // upper-left is (100, 200)

        // lower-right is (200, 300)

        g.setColor(Color.RED);

        g.fillRect(100, 200, 100, 100);

        g.setColor(Color.GREEN);

        g.fillOval(100, 200, 100, 100);

        g.setColor(Color.BLACK);

        g.drawRect(100, 200, 100, 100);

        g.drawOval(100, 200, 100, 100);

        g.drawLine(100, 200, 200, 300);

        g.drawLine(100, 300, 200, 200);

 

        // upper-left is (250, 150)

        // lower-right is (325, 225)

        g.setColor(Color.RED);

        g.fillRect(250, 150, 75, 75);

        g.setColor(Color.GREEN);

        g.fillOval(250, 150, 75, 75);

        g.setColor(Color.BLACK);

        g.drawRect(250, 150, 75, 75);

        g.drawOval(250, 150, 75, 75);

        g.drawLine(250, 150, 325, 225);

        g.drawLine(250, 225, 325, 150);

    }

}

Program File Draw2.java

// This is a simple drawing program that draws three versions of a particular

// subfigure on the screen.  This version is structured so that main calls

// three methods to draw the subfigures.

 

import java.awt.*;

 

public class Draw2 {

    public static void main(String[] args) {

        DrawingPanel p = new DrawingPanel(500, 400);

        p.setBackground(Color.CYAN);

        Graphics g = p.getGraphics();

        draw1(g);

        draw2(g);

        draw3(g);

    }

 

    // Draws a subfigure with  upper-left of (50, 50) and lower-right of

    // (150, 150)

    public static void draw1(Graphics g) {

        g.setColor(Color.RED);

        g.fillRect(50, 50, 100, 100);

        g.setColor(Color.GREEN);

        g.fillOval(50, 50, 100, 100);

        g.setColor(Color.BLACK);

        g.drawRect(50, 50, 100, 100);

        g.drawOval(50, 50, 100, 100);

        g.drawLine(50, 50, 150, 150);

        g.drawLine(50, 150, 150, 50);

    }

 

    // Draws a subfigure with  upper-left of (100, 200) and lower-right of

    // (200, 300)

    public static void draw2(Graphics g) {

        g.setColor(Color.RED);

        g.fillRect(100, 200, 100, 100);

        g.setColor(Color.GREEN);

        g.fillOval(100, 200, 100, 100);

        g.setColor(Color.BLACK);

        g.drawRect(100, 200, 100, 100);

        g.drawOval(100, 200, 100, 100);

        g.drawLine(100, 200, 200, 300);

        g.drawLine(100, 300, 200, 200);

    }

 

    // Draws a subfigure with  upper-left of (250, 150) and lower-right of

    // (325, 225)

    public static void draw3(Graphics g) {

        g.setColor(Color.RED);

        g.fillRect(250, 150, 75, 75);

        g.setColor(Color.GREEN);

        g.fillOval(250, 150, 75, 75);

        g.setColor(Color.BLACK);

        g.drawRect(250, 150, 75, 75);

        g.drawOval(250, 150, 75, 75);

        g.drawLine(250, 150, 325, 225);

        g.drawLine(250, 225, 325, 150);

    }

}

Program File Draw3.java

// This is a simple drawing program that draws three versions of a particular

// subfigure on the screen.  This version has a generalized method for

// drawing the subfigures that is called three different times.

 

import java.awt.*;

 

public class Draw3 {

    public static void main(String[] args) {

        DrawingPanel p = new DrawingPanel(500, 400);

        p.setBackground(Color.CYAN);

        Graphics g = p.getGraphics();

        draw(g, 50, 50, 100, 100);

        draw(g, 100, 200, 100, 100);

        draw(g, 250, 150, 75, 75);

    }

 

    // Draws a subfigure with  upper-left of (x, y) and given width and height

    public static void draw(Graphics g, int x, int y, int width, int height) {

        g.setColor(Color.RED);

        g.fillRect(x, y, width, height);

        g.setColor(Color.GREEN);

        g.fillOval(x, y, width, height);

        g.setColor(Color.BLACK);

        g.drawRect(x, y, width, height);

        g.drawOval(x, y, width, height);

        g.drawLine(x, y, x + width, y + height);

        g.drawLine(x, y + height, x + width, y);

    }

}

Graphical output