/* * Serena Fenton inls 150; spring 2001 * JavaDoodle.java * A simple color draw program that is exploring the available color constants * and mapping these to subtler rgb colors * (see: http://www.oreilly.com/info/java/qref11/java.awt.Color.html): * public static final Color black; * public static final Color blue; * public static final Color cyan; * public static final Color darkGray; * public static final Color gray; * public static final Color green; * public static final Color lightGray; * public static final Color magenta; * public static final Color orange; * public static final Color pink; * public static final Color red; * public static final Color white; * public static final Color yellow; */ import java.awt.*; import java.awt.event.*; import java.applet.*; import java.awt.Color; import java.lang.*; public class JavaDoodle extends Applet implements MouseListener, MouseMotionListener { //Color constants for the different drawing colors. private final static int YELLOW = 0, // yellow RED = 1, // red GREEN = 2, // green BLUE = 3, // blue ORANGE = 4, // orange MAGENTA = 5, // magenta (purplish) PINK= 6, // pink (this is purplish) BLACK= 7, // black DARKGRAY= 8, // brown, actually - not gray LIGHTGRAY= 9; //gray // Set black to be the default color to begin private int currentColor = BLACK; //mouse variables private int prevX, prevY; // The previous location of the mouse. private boolean dragging; // This is set to true while the user is drawing. private Graphics graphicsForDrawing; // A graphics context for the applet // that is used to draw the user's curve. public void init() { // sets up mouse to respond to input addMouseListener(this); addMouseMotionListener(this); } public void update(Graphics g) { // Redefine update so it does not fill the applet with the // background color before calling paint(). paint(g); } public void paint(Graphics g) { // Set up the drawing space. (Size is drawn from html callouts) int width = getSize().width; // Width of the applet. int height = getSize().height; // Height of the applet. //setting the size of the color squares int colorSpacing = (height - 56) / 10; /* First, fill in the white drawing area - except the area for the border and the color squares - if you don't do this, flickering results */ g.setColor(Color.white); g.fillRect(3, 3, width - 59, height - 6); // Draw a 3-pixel border around the applet. /*COMENT OUT the border g.setColor(Color.gray); g.drawRect(0, 0, width-1, height-1); g.drawRect(1, 1, width-3, height-3); g.drawRect(2, 2, width-5, height-5); */ /* Draw a 56-pixel wide gray rectangle along the right edge of the applet. for the color boxes */ g.fillRect(width - 56, 0, 56, height); /* Draw a "erase button" as a 50-by-30 white rectangle in the lower right corner of the applet, allowing for a 3-pixel border. */ g.setColor(Color.white); g.fillRect(width-53, height-53, 50, 30); g.setColor(Color.black); g.drawRect(width-53, height-53, 49, 49); g.drawString("eraser", width-48, height-23); /* Draw the ten color rectangles. */ g.setColor(new Color( 255, 204, 051)); //new rgb yellow g.fillRect(width-53, 3 + 0*colorSpacing, 50, colorSpacing-3); g.setColor(new Color( 204, 000, 051 )); //new rgb red g.fillRect(width-53, 3 + 1*colorSpacing, 50, colorSpacing-3); g.setColor(new Color( 102, 204, 000 )); //new rgb green g.fillRect(width-53, 3 + 2*colorSpacing, 50, colorSpacing-3); g.setColor(new Color( 051, 051, 204 )); //new rgb blue g.fillRect(width-53, 3 + 3*colorSpacing, 50, colorSpacing-3); g.setColor(new Color( 255, 153, 000)); //new rgb orange g.fillRect(width-53, 3 + 4*colorSpacing, 50, colorSpacing-3); g.setColor(new Color( 204, 051, 153)); //new rgb magenta g.fillRect(width-53, 3 + 5*colorSpacing, 50, colorSpacing-3); g.setColor(new Color( 153, 000, 204)); //new rgb pink-ish purple g.fillRect(width-53, 3 + 6*colorSpacing, 50, colorSpacing-3); g.setColor(Color.black); g.fillRect(width-53, 3 + 7*colorSpacing, 50, colorSpacing-3); g.setColor( new Color(153, 102, 000)); //new rgb brown g.fillRect(width-53, 3 + 8*colorSpacing, 50, colorSpacing-3); g.setColor(Color.lightGray); //light gray g.fillRect(width-53, 3 + 9*colorSpacing, 50, colorSpacing-3); } // end paint() private void changeColor(int y) { // Change the drawing color after the user has clicked the // mouse on the color palette at a point with y-coordinate y. int width = getSize().width; // Width of applet. int height = getSize().height; // Height of applet. int colorSpacing = (height - 56) / 10; // Space for one color rectangle. int newColor = y / colorSpacing; // Which color number was clicked? if (newColor < 0 || newColor > 9) // Make sure the color number is valid. return; /* Remove the highlight from the current color, by drawing over it in white. Then change the current drawing color and draw a highlight around the new drawing color in black. */ Graphics g = getGraphics(); g.setColor(Color.white); g.drawRect(width-55, 1 + currentColor*colorSpacing, 53, colorSpacing); g.drawRect(width-54, 2 + currentColor*colorSpacing, 51, colorSpacing-2); currentColor = newColor; g.setColor(Color.black); g.drawRect(width-55, 1 + currentColor*colorSpacing, 53, colorSpacing); g.drawRect(width-54, 2 + currentColor*colorSpacing, 51, colorSpacing-2); g.dispose(); } // end changeColor() private void setUpDrawingGraphics() { // This routine is called in mousePressed when the // user clicks on the drawing area. It sets up the // graphics context, graphicsForDrawing, to be used // to draw the user's sketch in the current color. graphicsForDrawing = getGraphics(); switch (currentColor) { case YELLOW: //yellow graphicsForDrawing.setColor(new Color( 255, 204, 051)); break; case RED: //red graphicsForDrawing.setColor(new Color( 204, 000, 051 )); break; case GREEN: //green graphicsForDrawing.setColor(new Color( 102, 204, 000 )); break; case BLUE: //blue graphicsForDrawing.setColor(new Color( 051, 051, 204 )); break; case ORANGE: //orange graphicsForDrawing.setColor(new Color( 255, 153, 000)); break; case MAGENTA: //magenta graphicsForDrawing.setColor(new Color( 204, 051, 153)); break; case PINK: //pink graphicsForDrawing.setColor(new Color( 153, 000, 204)); break; case BLACK: //black graphicsForDrawing.setColor(Color.black); break; case DARKGRAY: //brown, actually graphicsForDrawing.setColor(new Color( 153, 102, 000)); break; case LIGHTGRAY: //lightgray graphicsForDrawing.setColor(Color.lightGray); break; } } // end setUpDrawingGraphics() public void mousePressed(MouseEvent evt) { int x = evt.getX(); // x-coordinate where the user clicked. int y = evt.getY(); // y-coordinate where the user clicked. int width = getSize().width; // Width of the applet. int height = getSize().height; // Height of the applet. if (dragging == true) // Ignore mouse presses that occur return; // when user is already drawing a curve. // (This can happen if the user presses // two mouse buttons at the same time.) if (x > width - 53) { // User clicked to the right of the drawing area. // This click is either on the clear button or // on the color palette. if (y > height - 53) repaint(); // Clicked on "CLEAR button". else changeColor(y); // Clicked on the color palette. } else if (x > 3 && x < width - 56 && y > 3 && y < height - 3) { // The user has clicked on the white drawing area. // Start drawing a curve from the point (x,y). prevX = x; prevY = y; dragging = true; setUpDrawingGraphics(); } } // end mousePressed() public void mouseReleased(MouseEvent evt) { if (dragging == false) return; // Nothing to do because the user isn't drawing. dragging = false; graphicsForDrawing.dispose(); graphicsForDrawing = null; } public void mouseDragged(MouseEvent evt) { if (dragging == false) return; // Nothing to do because the user isn't drawing. int x = evt.getX(); // x-coordinate of mouse. int y = evt.getY(); // y=coordinate of mouse. if (x < 3) // Adjust the value of x, x = 3; // to make sure it's in if (x > getSize().width - 57) // the drawing area. x = getSize().width - 57; if (y < 3) // Adjust the value of y, y = 3; // to make sure it's in if (y > getSize().height - 4) // the drawing area. y = getSize().height - 4; graphicsForDrawing.drawLine(prevX, prevY, x, y); // Draw the line. prevX = x; // Get ready for the next line segment in the curve. prevY = y; } // end mouseDragged. public void mouseEntered(MouseEvent evt) { } // Some empty routines. public void mouseExited(MouseEvent evt) { } // (Required by the MouseListener public void mouseClicked(MouseEvent evt) { } // and MouseMotionListener public void mouseMoved(MouseEvent evt) { } // interfaces). } // end class SimplePaint