Draw random shapes in random colors and positions

The Problem

Create an applet that draws various geometric figures in random colors scattered around in a window. Use the random number generator to determine the type of figure, its color and its location. Limit the number of different types of figures and the number of different colors to three. There should be 15 figures in total.

Discussion

There are many approaches to generating random colors. We can use for example three random numbers for red, green and blue components and create a new Color object, based on the RGB representation of the color. Similarily we can use HSB or other numeric representation. We will use another way. We will create an array with colors that we can choose from. When we want to get a random color, we will pick random element from that array.

For generating random numbers we will use the nextInt() method of the java.util.Random class.

The problem with random shapes we will solve by creating an abstract class Shape(). The client code will be based on that class and will use polymorfic effect to draw actual shapes that inherit from that class.

To select random shape, we can generate the shape's class name and use reflection to create an instance of that shape. Instead of reflection, we can use a factory class. Because we have a limited number of shapes (3), we will use switch statement to select and create shapes.

Here is the class diagram for the problem:

The solution

/**
 * @(#)RandomShapes.java
 *
 * RandomShapes Applet application
 * 
 * This applet solves the following problem:
 * Create an applet that draws various geometric figures in
 * random colors scattered around in a window. Use the random
 * number generator to determine the type of figure, its color
 * and its location. Limit the number of different types of
 * figures and the number of different colors to three. There
 * should be 15 figures in total. 
 *
 * @author Ivan Georgiev
 * @version 1.00 2008/9/3
 */
 
import java.awt.*;
import javax.swing.*;
import java.util.Random;
 
public class RandomShapes extends JApplet {
 
	public void init() {
		Container content = getContentPane();
		Drawing drawing = new Drawing();
		content.add(drawing);
	}
}
 
class Drawing extends JPanel {
	private final int numOfShapes = 15;
	private static final int shapeWidth = 100;
	private static final int shapeHeight = 100;
	private static final Color[] availableColors =
			{ Color.red, Color.blue, Color.green };
 
	public void paintComponent(Graphics window) {
		super.paintComponent(window);
 
		Shape shape;
		Random rand = new Random();
		int x,y;
		int windowWidth = getWidth();
		int windowHeight = getHeight();
 
		for (int shapeNo = 0; shapeNo < numOfShapes; shapeNo++) {
			x = (int) ((windowWidth - shapeWidth) * Math.random() + 1);
			y = (int) ((windowHeight - shapeHeight) * Math.random() + 1);
 
			switch (rand.nextInt(3)) {
				case 0:
					shape = new Oval(x,y);
					break;
				case 1:
					shape = new Rectangle(x,y);
					break;
				case 2:
					shape = new Arc(x,y);
					break;
				default:
					System.out.println("Unknown shape type index.");
					continue;
			}
			int colorIndex = rand.nextInt(availableColors.length);
			shape.setColor(availableColors[colorIndex]);
			shape.paintShape(window);
		}
	}
}
 
 
abstract class Shape {
	private final int width = 100;
	private final int height = 100;
	private int x;
	private int y;
	private Color color;
 
	public Shape(int x, int y) {
		this.x = x;
		this.y = y;
	}
 
	public void setColor(Color newColor) {
		color = newColor;
	}
 
	public Color getColor() {
		return color;
	}
 
	public int getX() {
		return x;
	}
 
	public int getY() {
		return y;
	}
 
	public int getWidth() {
		return width;
	}
 
	public int getHeight() {
		return height;
	}
 
	abstract public void paintShape(Graphics g);
}
 
class Oval extends Shape {
	public Oval(int x, int y) {
		super(x,y);
	}
 
	public void paintShape(Graphics g) {
		g.setColor(getColor());
		g.drawOval(getX(),getY(),getWidth(),getHeight());
	}
}
 
class Rectangle extends Shape {
	public Rectangle(int x, int y) {
		super(x,y);
	}
 
	public void paintShape(Graphics g) {
		g.setColor(getColor());
		g.drawRect(getX(),getY(),getWidth(),getHeight());
	}
}
 
class Arc extends Shape {
	private final int startAngle = 90;
	private final int arcAngle = 270;
 
	public Arc(int x, int y) {
		super(x,y);
	}
 
	public void paintShape(Graphics g) {
		g.setColor(getColor());
		g.drawArc(getX(),getY(),getWidth(),getHeight(),startAngle,arcAngle);
	}
}
 
java/excercises/randomshapes.txt · Last modified: 2009/10/31 23:39 (external edit)
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki