Class Picture:

import java.awt.*;

public class Picture {
    protected int x;
    protected int y;
    protected int w;
    protected int h;
    protected Color c;

    public Picture (int x, int y, int w, int h, Color c) {
	this.x = x;
	this.y = y;
	this.w = w;
	this.h = h;
	this.c = c;
    }

    public void draw(Graphics g) {
	// remember the original color
	Color saved = g.getColor();
	// set the color to a new one
	g.setColor(c);

	// draw a filled rectangle 
	g.fillRect(x,y,w,h);

	// restore the color:
	g.setColor(saved);
    }

    public boolean isGreen() {
	return c == Color.green;
    }
}
Test files for class picture:

<HTML>
<APPLET CODE="TestDraw.class" HEIGHT=400 WIDTH=400></APPLET>
</HTML>

import java.awt.*;
import java.applet.*;
import java.util.*;

public class TestDraw extends Applet {

    public void paint(Graphics g) {
	Color [] colors = {Color.green, Color.blue, Color.yellow};
	Color [] mcolors = {Color.magenta, Color.white, Color.green};
	Picture [] pics = new Picture[4];
	Random r = new Random();

	// create 4 pictures:
	for (int i = 0; i < 4; ++i) {
	    int j = 0;
	    if (i > 1) j = 1;
	    int c_index = r.nextInt(3);
	    pics[i] = new Picture((i%2)*200,j*200,200,200,colors[c_index]);
	    if (pics[i].isGreen()) 
	    System.out.println("Happy St. Patricks!");
        }

	// draw the pictures
	for (int i = 0; i < 4; ++i) {
	    pics[i].draw(g);
	}
    }
}
Class OvalPicture inherits from Picture:

import java.awt.*;

public class OvalPicture extends Picture{
    Color middle;

    public OvalPicture(int x, int y, int w, int h, Color c, Color m) {
	super(x,y,w,h,c); // using the constructor of the superclass
	middle = m;
    }

    public void draw(Graphics g) {
	// remember the original color
	Color saved = g.getColor();
	// set the color to a new one
	g.setColor(c);

	// draw a filled rectangle 
	g.fillOval(x,y,w,h);

	g.setColor(middle);
	g.fillOval(x + w/4, y + h/4, w/2, h/2);

	// restore the color:
	g.setColor(saved);
    }

    // adding a new method:
    public boolean hasGreen() {
	return c == Color.green || middle == Color.green;
    }
}
Test file:

import java.awt.*;
import java.applet.*;
import java.util.*;

public class TestDraw extends Applet {

    public void paint(Graphics g) {
	Color [] colors = {Color.green, Color.blue, Color.yellow};
	Color [] mcolors = {Color.magenta, Color.white, Color.green};
	OvalPicture [] pics = new OvalPicture[4];
	Random r = new Random();

	// create 4 pictures:
	for (int i = 0; i < 4; ++i) {
	    int j = 0;
	    if (i > 1) j = 1;
	    int c_index = r.nextInt(3);
	    int mc_index = r.nextInt(3);
	    pics[i] = new OvalPicture((i%2)*200,j*200,200,200,colors[c_index],
	    mcolors[mc_index]);
	    if (pics[i].hasGreen()) 
		System.out.println("Happy St. Patricks!");
        }

	for (int i = 0; i < 4; ++i) {
	    pics[i].draw(g);
	}
    }
}
Another class that extends Picture:

import java.awt.*;

public class Flag extends Picture{
    Color middle;

    public Flag(int x, int y, int w, int h, Color c, Color m) {
	super(x,y,w,h,c); // using the constructor of the superclass
	middle = m;
    }

    public void draw(Graphics g) {
	// remember the original color
	Color saved = g.getColor();
	// set the color to a new one
	g.setColor(c);

	// draw a filled rectangle 
	g.fillRect(x,y,w,h);

	g.setColor(middle);
	g.fillRect(x, y + y/3, w, h/3);

	// restore the color:
	g.setColor(saved);
    }

    // adding a new method:
    public boolean hasGreen() {
	return c == Color.green || middle == Color.green;
    }
}
The test that uses both OvalPicture and Flag. Note that hasGreen() cannot be used since it's not defined in Picture.

import java.awt.*;
import java.applet.*;
import java.util.*;

public class TestDraw extends Applet {

    public void paint(Graphics g) {
	Color [] colors = {Color.green, Color.blue, Color.yellow};
	Color [] mcolors = {Color.magenta, Color.white, Color.green};
	Picture [] pics = new Picture[4];
	Random r = new Random();

	// create 4 pictures:
	for (int i = 0; i < 2; ++i) {
	    int c_index = r.nextInt(3);
	    int mc_index = r.nextInt(3);
	    pics[2*i] = new OvalPicture(0,i*200,200,200,colors[c_index],
					  mcolors[mc_index]);
	    pics[2*i+1] = new Flag(200,i*200,200,200,colors[c_index],
					  mcolors[mc_index]);

	    //if (pics[i].hasGreen()) 
	    //System.out.println("Happy St. Patricks!");
        }

	for (int i = 0; i < 4; ++i) {
	    pics[i].draw(g); // the right method is called for each object
	}
    }
}

This is an example from CSci 1211 course.

The views and opinions expressed in this page are strictly those of the page author. The contents of this page have not been reviewed or approved by the University of Minnesota.