import objectdraw.*; import java.awt.*; import java.util.Random; // This program allows the user to draw lines of randomly chosen colors on the screen // using the mouse as if it were a crayon public class Crayon extends FrameWindowController { // number of crayon colors to choose from private static final int NUM_CRAYONS = 4; // random int generated for color selection private int colorNumber; // a random number generator that generates random numbers // between 1 and 4 (inclusive) private Random colorPicker = new Random(); private Location lastPoint; // location where mouse was last pressed private Color currentColor; // current color for scribbling // remember point of press // generate a random color with which lines will be drawn public void onMousePress(Location point) { lastPoint = point; colorNumber = colorPicker.nextInt(NUM_CRAYONS); if (colorNumber == 1) { currentColor = Color.RED; } else if (colorNumber == 2) { currentColor = Color.BLUE; } else if (colorNumber == 3 ) { currentColor = Color.GREEN; } else { currentColor = Color.MAGENTA; } } // draws a line with the random color selected at the time of mouse press public void onMouseDrag(Location point) { new Line(lastPoint, point, canvas).setColor(currentColor); lastPoint = point; } }