/*********************************************** * CS2510 Fall 2011 * Lecture #18_2 * VoidWorld fun, Statics ***********************************************/ import tester.*; import image.*; import world.*; import java.util.*; /** Crazy Color Painting */ class PaintWorld extends VoidWorld{ int WIDTH = 500; int HEIGHT = 500; // Random number generator Random randy = new Random(); // List of possible Colors ArrayList colors; Scene canvas = new EmptyScene(this.WIDTH,this.HEIGHT); /** Construct and EyeColorWorld. Create the list of possible Colors */ PaintWorld(){ this.colors = new ArrayList(); this.colors.add("indigo"); this.colors.add("chartreuse"); this.colors.add("red"); this.colors.add("goldenrod"); this.colors.add("blue"); this.colors.add("turquoise"); this.colors.add("green"); this.colors.add("purple"); } /** Return my canvas object to be drawn */ public Scene onDraw(){ return this.canvas; } /** Creates a new spot every so often */ public void onTick(){ if(this.randy.nextInt(100) == 1) { this.canvas = this.canvas.placeImage( new Circle(4,"solid",Helper.randColor(colors)), new Posn( this.randy.nextInt(this.WIDTH), this.randy.nextInt(this.HEIGHT))); } } /** Add a Circle when the mouse is dragged */ public void onMouse(int x, int y, String me){ try{ if(me.equals("drag")){ this.canvas = this.canvas.placeImage(new Circle(8, "solid", Helper.randColor(colors)), new Posn(x,y)); } }catch(ConcurrentModificationException e){ // If the exception is thrown we can just ignore // it, and not do anything } } /* Set my tick rate to something fairly fast */ public double tickRate() { return .01; } } // A set of static helper methods for our class class Helper { // Random number generator static Random randy = new Random(); // Get a random color from a given array of colors static String randColor(ArrayList colors) { return colors.get(randy.nextInt(colors.size())); } } /** Examples class to run our Masterpiece! */ class PaintExamples{ /** Make a new one... */ VoidWorld start = new PaintWorld(); /** Start it up */ VoidWorld end = start.bigBang(); }