home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / javafile / ch05 / Doodle.java next >
Encoding:
Java Source  |  1998-12-14  |  5.2 KB  |  252 lines

  1. import java.applet.Applet;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4.  
  5. /**
  6.  * The ColorBar class displays a color bar for color selection.
  7.  */
  8. class ColorBar {
  9.  
  10. /*
  11.  * the top-left coordinate of the color bar
  12.  */
  13. int xpos, ypos;
  14.  
  15. /*
  16.  * the width and height of the color bar
  17.  */
  18. int width, height;
  19.  
  20. /*
  21.  * the current color selection index into the colors array
  22.  */
  23. int selectedColor = 3;
  24.  
  25. /*
  26.  * the array of colors available for selection
  27.  */
  28. static Color colors[] = {
  29.     Color.white, Color.gray, Color.red, Color.pink,
  30.     Color.orange, Color.yellow, Color.green, Color.magenta,
  31.     Color.cyan, Color.blue
  32. };
  33.  
  34. /**
  35.  * Create the color bar
  36.  */
  37. public ColorBar (int x, int y, int w, int h) {
  38.  
  39.     xpos = x;
  40.     ypos = y;
  41.     width = w;
  42.     height = h;
  43. }
  44.  
  45. /**
  46.  * Paint the color bar
  47.  * @param g - destination graphics object
  48.  */
  49. void paint (Graphics g) {
  50.  
  51.     int x, y;    // position of each color box
  52.     int w, h;    // size of each color box
  53.  
  54.     for (int i=0; i<colors.length; i+=1) {
  55.         w = width; 
  56.         h = height/colors.length;
  57.         x = xpos;
  58.         y = ypos + (i * h);
  59.         g.setColor (Color.black);
  60.         g.fillRect (x, y, w, h);
  61.         if (i == selectedColor) {
  62.             x += 5;
  63.             y += 5;
  64.             w -= 10;
  65.             h -= 10;
  66.         } else {
  67.             x += 1;
  68.             y += 1;
  69.             w -= 2;
  70.             h -= 2;
  71.         }
  72.         g.setColor (colors[i]);
  73.         g.fillRect (x, y, w, h);
  74.     }
  75. }
  76.  
  77. /**
  78.  * Check to see if the mouse is inside a palette box.
  79.  * If so, set selectedColor and return true,
  80.  *        otherwise return false.
  81.  * @param x, y - x and y position of mouse
  82.  */
  83. boolean inside (int x, int y) {
  84.  
  85.     int i, h;
  86.  
  87.     if (x < xpos || x > xpos+width) return false;
  88.     if (y < ypos || y > ypos+height) return false;
  89.  
  90.     h = height/colors.length;
  91.     for (i=0; i<colors.length; i+=1) {
  92.         if (y < (i+1)*h+ypos) {
  93.             selectedColor = i;
  94.             return true;
  95.         }
  96.     }
  97.     return false;
  98. }
  99.  
  100. }
  101.  
  102. /**
  103.  * The Doodle applet implements a drawable surface
  104.  * with a limited choice of colors to draw with.
  105.  */
  106. public class Doodle extends Applet
  107.         implements MouseListener, MouseMotionListener
  108. {
  109.  
  110. /*
  111.  * the maximum number of points that can be
  112.  * saved in the xpoints, ypoints, and color arrays
  113.  */
  114. static final int MaxPoints = 1000;
  115.  
  116. /*
  117.  * arrays to hold the points where the user draws
  118.  */
  119. int xpoints[] = new int[MaxPoints];
  120. int ypoints[] = new int[MaxPoints];
  121.  
  122. /*
  123.  * the color of each point
  124.  */
  125. int color[] = new int[MaxPoints];
  126.  
  127. /*
  128.  * used to keep track of the previous mouse
  129.  * click to avoid filling arrays with the
  130.  * same point
  131.  */
  132. int lastx;
  133. int lasty;
  134.  
  135. /*
  136.  * the number of points in the arrays
  137.  */
  138. int npoints = 0;
  139. ColorBar colorBar;
  140. boolean inColorBar;
  141.  
  142. /**
  143.  * Initialize the drawing space
  144.  */
  145. public void init () {
  146.  
  147.     setBackground(Color.white);
  148.     colorBar = new ColorBar (10, 10, 30, 200);
  149.     addMouseListener(this);
  150.     addMouseMotionListener(this);
  151. }
  152.  
  153. /**
  154.  * Redisplay the drawing space
  155.  * @param g - destination graphics object
  156.  */
  157. public void update (Graphics g) {
  158.  
  159.     int i; 
  160.  
  161.     for (i=0; i<npoints; i+=1) {
  162.         g.setColor (colorBar.colors[color[i]]);
  163.         g.fillOval (xpoints[i]-5, ypoints[i]-5, 10, 10);
  164.     }
  165.     colorBar.paint (g);
  166. }
  167.  
  168. /**
  169.  * Repaint the drawing space when required
  170.  * @param g - destination graphics object
  171.  */
  172. public void paint (Graphics g) {
  173.  
  174.     update (g);
  175. }
  176.  
  177.  
  178.  
  179. public void mouseClicked(MouseEvent e){}
  180. public void mouseEntered(MouseEvent e){}
  181. public void mouseExited(MouseEvent e){}
  182. public void mouseReleased(MouseEvent e){}
  183. public void mouseMoved(MouseEvent e){}
  184.  
  185. public void mousePressed(MouseEvent e)
  186. {
  187.     int x =e.getX();
  188.     int y =e.getY();
  189.  
  190.     if (colorBar.inside (x, y)) {
  191.         inColorBar = true;
  192.         repaint ();
  193.     }
  194.     inColorBar = false;
  195.     if (npoints < MaxPoints) {
  196.         lastx = x;
  197.         lasty = y;
  198.         xpoints[npoints] = x;
  199.         ypoints[npoints] = y;
  200.         color[npoints] = colorBar.selectedColor; 
  201.         npoints += 1;
  202.         repaint();
  203.         return;
  204.     }
  205. }
  206.  
  207. public void mouseDragged(MouseEvent e)
  208. {
  209.     if (inColorBar) return;
  210.  
  211.     int x =e.getX();
  212.     int y =e.getY();
  213.  
  214.     if ((x != lastx || y != lasty) && npoints < MaxPoints) {
  215.         lastx = x;
  216.         lasty = y; 
  217.         xpoints[npoints] = x;
  218.         ypoints[npoints] = y;
  219.         color[npoints] = colorBar.selectedColor;
  220.         npoints += 1;
  221.         repaint ();
  222.     }
  223.  
  224. }
  225.  
  226. /**
  227.  * The main method allows this class to be run as an application
  228.  * in addition to being run as an applet.
  229.  * @param args - command-line arguments
  230.  */
  231.  
  232. public static void main (String args[]) {
  233.  
  234.     Frame f = new Frame ("Doodle");
  235.     Doodle doodle = new Doodle ();
  236.        WindowListener l = new WindowAdapter()
  237.        {
  238.             public void windowClosing(WindowEvent e)
  239.             {
  240.                 System.exit(0);
  241.             }//windowClosing
  242.        };//WindowListener
  243.        f.addWindowListener(l);
  244.  
  245.  
  246.     f.setSize (410, 430); 
  247.     f.add ("Center", doodle);
  248.     f.show ();
  249.     doodle.init ();
  250. }
  251. }
  252.