Matt Gerrans
Posts: 1153
Nickname: matt
Registered: Feb, 2002
|
|
Re: Interface Question
|
Posted: Apr 12, 2002 12:23 AM
|
|
Okay, I think this is along the lines of what you are after. I have only implemented a DrawableRectangle and left all the other shapes for you, but I think you'll get the picture. The idea is that you have a collection of Drawables, not Shapes, so you can iterate through it and ask each one to draw itself (since they all must implement draw()).
Some notes: - I changed the creator class name to reflect the fact that it is creating Drawables. - I've commented out all the cases in your switch except the rectangle case in the creator method -- you can uncomment the others as you create their Drawable counterparts. - For ease of posting, I've not made the interface public and put everything in one file, but in real work, you would want each interface and class in its own file (and probably in a package). - I made the DrawableRectangle extend Rectangle, which I think is okay in this simple case, but some people always prefer composition. Using composition, the DrawableRectangle class would contain a Rectangle instance variable along with the Color it has already. If you used composition, you'd probably use a different name, like ColorfulRectangleDrawer, to reflect the fact that it is an object which draws colorful rectangles (and to confuse people who are looking for handy furniture objects!). - I added a main(), so that you can run it stand-alone (I find this makes for quicker test runs).
import java.applet.*;
import java.awt.*;
import java.util.Random;
//=================================================================
// Drawable
//=================================================================
interface Drawable extends Shape
{
public void draw( Graphics page );
}
//=================================================================
// DrawableRectangle
//=================================================================
class DrawableRectangle extends Rectangle implements Drawable
{
Color color;
DrawableRectangle( Color c, int x, int y, int width, int height )
{
super( x, y, width, height );
color = c;
}
public void draw( Graphics page )
{
Color prevColor = page.getColor();
page.setColor(color);
page.drawRect( x, y, width, height);
page.setColor(prevColor);
}
}
//=================================================================
// This applet creates and draws multiple random shapes.
//=================================================================
public class Random_Draw extends Applet
{
final int MAX_SHAPE = 50;
Drawable[] shapes = new Drawable[ MAX_SHAPE ];
Drawable_Creator creator;
public static void main(String[] args)
{
Applet applet = new Random_Draw();
javax.swing.JFrame frame = new javax.swing.JFrame("Random Shapes");
frame.setSize(800, 800);
frame.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(applet);
applet.init();
applet.start();
frame.setVisible(true);
}
//-----------------------------------------------------
// Creates and stores multiple shapes.
//-----------------------------------------------------
public void init()
{
shapes = new Drawable[ MAX_SHAPE ];
creator = new Drawable_Creator();
for(int index = 0;index < MAX_SHAPE;index++)
shapes[ index ] = creator.new_shape();
} // method init
//-----------------------------------------------------
// Draws all of the shapes on the applet.
//-----------------------------------------------------
public void paint(Graphics page)
{
for(int index = 0;index < MAX_SHAPE;index++)
shapes[ index ].draw(page);
} // method paint
} // class Random_Draw
//====================================================
// The Shape_Creator class contains code to create random shapes,
// each with random size, color, and location.
//====================================================
class Drawable_Creator
{
final int COLOR_MAX = 255;
final int DIMENSION_MAX = 400;
final int NUM_SHAPES = 6;
final int POLY_MAX = 9;
final int LINE = 1;
final int RECTANGLE = 2;
final int OVAL = 3;
final int SQUARE = 4;
final int CIRCLE = 5;
final int POLYGON = 6;
Random rand = new Random();
//-----------------------------------------------------
// Returns a random integer between 1 and max.
//-----------------------------------------------------
public int random_int(int max)
{
return Math.abs(rand.nextInt()) % max + 1;
} // method random_int
//-----------------------------------------------------
// Returns a random color created using random RGB
// values.
//-----------------------------------------------------
public Color random_color()
{
return new Color(random_int(COLOR_MAX), random_int(COLOR_MAX), random_int(COLOR_MAX));
} // method random_color
//-----------------------------------------------------
// Returns a random shape with random characteristics.
//-----------------------------------------------------
public Drawable new_shape()
{
Drawable result;
int x1, y1, x2, y2, x, y, width, idth, height, length;
int[] xPolygon = new int[ POLY_MAX ];
int[] yPolygon = new int[ POLY_MAX ];
switch(random_int(NUM_SHAPES))
{
/***************
To do: Create the Drawable version of all these classes, then uncomment
this code!
case LINE:
x1 = random_int(DIMENSION_MAX);
y1 = random_int(DIMENSION_MAX);
x2 = random_int(DIMENSION_MAX);
y2 = random_int(DIMENSION_MAX);
result = new Line(random_color(), x1, y1, x2, y2);
break;
case OVAL:
x = random_int(DIMENSION_MAX);
y = random_int(DIMENSION_MAX);
width = random_int(DIMENSION_MAX);
height = random_int(DIMENSION_MAX);
result = new Oval(random_color(), x, y, width, height);
break;
case SQUARE:
x = random_int(DIMENSION_MAX);
y = random_int(DIMENSION_MAX);
width = random_int(DIMENSION_MAX);
height = width;
result = new Rectangle(random_color(), x, y, width, height);
break;
case CIRCLE:
x = random_int(DIMENSION_MAX);
y = random_int(DIMENSION_MAX);
width = random_int(DIMENSION_MAX);
height = width;
result = new Oval(random_color(), x, y, width, height);
break;
case POLYGON:
length = random_int(POLY_MAX);
for(int i = 0;i < length;i++)
{
xPolygon[ i ] = random_int(DIMENSION_MAX);
yPolygon[ i ] = random_int(DIMENSION_MAX);
}
result = new Polygon(random_color(), xPolygon, yPolygon, length);
break;
***************/
case RECTANGLE:
default:
x = random_int(DIMENSION_MAX);
y = random_int(DIMENSION_MAX);
width = random_int(DIMENSION_MAX);
height = random_int(DIMENSION_MAX);
result = new DrawableRectangle(random_color(), x, y, width, height);
break;
}
return result;
} // method new_shape
} // class Shape_Creator
|
|