import java.awt.*; /* This class represents a bounding box for objects of type Shape */ class Box { int x,y; //coordinate of upper left corner; int width,height; //width and height; Box() {} Box(int x, int y, int width, int height) { this.x = x; this.y=y; this.width = width; this.height = height; } static Box randomBox(int frame_width, int frame_height) { /* returns a newly allocated box of random size that will fit in a frame of given width and height. The maximum size of the returned box is 1/4 the frame size */ Assertions.assert(frame_width>0 && frame_height>0); Box b = new Box(); b.width = RandomInts.next(5,frame_width/4); b.height = RandomInts.next(5,frame_width/4); b.x = RandomInts.next(0,frame_width-b.width-1); b.y = RandomInts.next(0,frame_height-b.height-1); return b; } public String toString() { return "x="+x+", y="+y+", width="+width+", height="+height; } } class Shape /* Superclass for all shapes. Use by subclassing and overriding the draw method to display the shape */ { Box box; //bounding box for Shape Color color; //color Shape(Box box, Color color) { this.box = box; this.color = color; } void draw(Graphics g) { //OVERRIDE THIS METHOD TO DISPLAY SHAPE } public String toString() {return box.toString() + ", color=" + color.toString(); } } class Oval extends Shape /*Displays the largest oval that will fit in the bounding box */ { Oval(Box box, Color color) { super(box,color); } void draw(Graphics g) {g.setColor(color); g.fillOval(box.x,box.y,box.width,box.height); } } class Triangle extends Shape { /* The vertices of the triangle are (x_vertices[0],y_vertices[0]), (x_vertices[1],y_vertices[1]), and (x_vertices[2],y_vertices[2]) Use the Graphics.fillPolygon method to draw */ int[] x_vertices; int[] y_vertices; Triangle(Box box, Color color) /* constructs Triangle to fit bounding box. Vertices are lower left and right corners of bounding box and the point midway between the upper left and right corners of bounding box. */ { super(box,color); int[] xs = {box.x, box.x+box.width, box.x+box.width/2}; x_vertices = xs; int[] ys = {box.y+box.height,box.y+box.height,box.y}; y_vertices = ys; } void draw(Graphics g) { g.setColor(color); g.fillPolygon(x_vertices,y_vertices,3); } } class IsItArt extends CIS3020CloseableFrame { static final int MIN_NUMBER_SHAPES = 10; static final int MAX_NUMBER_SHAPES = 40; static final Color[] colors = {Color.red,Color.blue,Color.green,Color.yellow,Color.cyan,Color.magenta}; Shape[] shapes; void init() { //Allocate array to hold random number of shapes shapes = new Shape[RandomInts.next(MIN_NUMBER_SHAPES,MAX_NUMBER_SHAPES)]; //get width and height of drawable area of frame Insets insets = getInsets(); int frame_width = getSize().width-insets.left - insets.right; int frame_height = getSize().height-insets.top - insets.bottom; //fill in entries for shapes queue //loop invariant: entries in array shape in [0,i) have been allocated //loop variant shapes.length-i for (int i = 0; i != shapes.length; ++i) { //select kind of shape int select = RandomInts.next(0,1); //allocate shape with random box and color according to select if (select == 0) shapes[i] = new Oval(Box.randomBox(frame_width,frame_height), colors[RandomInts.next(0,colors.length-1)] ); else if (select == 1) shapes[i] = new Triangle(Box.randomBox(frame_width,frame_height), colors[RandomInts.next(0,colors.length-1)] ); } } public void paint(Graphics g) { //translate coordinates of graphics frame to origin of //drawable area (area without titlebar and borders) g.translate(getInsets().left,getInsets().top); //display all figures in shapes array for(int i = 0; i != shapes.length && shapes[i] != null; ++i) shapes[i].draw(g); } public static void main(String[] args) { IsItArt f = new IsItArt(); f.init(); //initialize shapes f.setVisible(true); //display frame (to be done after shapes // created) while(true) {Sleep.sleep(1500); //pause for 1500 milliseconds f.init(); //calculate a new set of shapes f.repaint(); //request window system to redisplay the frame // (i.e. call paint) } } } class Sleep //method sleep causes program to pause for millis milliseconds { static void sleep(int milis) { try { Thread.currentThread().sleep(milis); } catch(InterruptedException e){} } }