home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-12-14 | 5.2 KB | 252 lines |
- import java.applet.Applet;
- import java.awt.*;
- import java.awt.event.*;
-
- /**
- * The ColorBar class displays a color bar for color selection.
- */
- class ColorBar {
-
- /*
- * the top-left coordinate of the color bar
- */
- int xpos, ypos;
-
- /*
- * the width and height of the color bar
- */
- int width, height;
-
- /*
- * the current color selection index into the colors array
- */
- int selectedColor = 3;
-
- /*
- * the array of colors available for selection
- */
- static Color colors[] = {
- Color.white, Color.gray, Color.red, Color.pink,
- Color.orange, Color.yellow, Color.green, Color.magenta,
- Color.cyan, Color.blue
- };
-
- /**
- * Create the color bar
- */
- public ColorBar (int x, int y, int w, int h) {
-
- xpos = x;
- ypos = y;
- width = w;
- height = h;
- }
-
- /**
- * Paint the color bar
- * @param g - destination graphics object
- */
- void paint (Graphics g) {
-
- int x, y; // position of each color box
- int w, h; // size of each color box
-
- for (int i=0; i<colors.length; i+=1) {
- w = width;
- h = height/colors.length;
- x = xpos;
- y = ypos + (i * h);
- g.setColor (Color.black);
- g.fillRect (x, y, w, h);
- if (i == selectedColor) {
- x += 5;
- y += 5;
- w -= 10;
- h -= 10;
- } else {
- x += 1;
- y += 1;
- w -= 2;
- h -= 2;
- }
- g.setColor (colors[i]);
- g.fillRect (x, y, w, h);
- }
- }
-
- /**
- * Check to see if the mouse is inside a palette box.
- * If so, set selectedColor and return true,
- * otherwise return false.
- * @param x, y - x and y position of mouse
- */
- boolean inside (int x, int y) {
-
- int i, h;
-
- if (x < xpos || x > xpos+width) return false;
- if (y < ypos || y > ypos+height) return false;
-
- h = height/colors.length;
- for (i=0; i<colors.length; i+=1) {
- if (y < (i+1)*h+ypos) {
- selectedColor = i;
- return true;
- }
- }
- return false;
- }
-
- }
-
- /**
- * The Doodle applet implements a drawable surface
- * with a limited choice of colors to draw with.
- */
- public class Doodle extends Applet
- implements MouseListener, MouseMotionListener
- {
-
- /*
- * the maximum number of points that can be
- * saved in the xpoints, ypoints, and color arrays
- */
- static final int MaxPoints = 1000;
-
- /*
- * arrays to hold the points where the user draws
- */
- int xpoints[] = new int[MaxPoints];
- int ypoints[] = new int[MaxPoints];
-
- /*
- * the color of each point
- */
- int color[] = new int[MaxPoints];
-
- /*
- * used to keep track of the previous mouse
- * click to avoid filling arrays with the
- * same point
- */
- int lastx;
- int lasty;
-
- /*
- * the number of points in the arrays
- */
- int npoints = 0;
- ColorBar colorBar;
- boolean inColorBar;
-
- /**
- * Initialize the drawing space
- */
- public void init () {
-
- setBackground(Color.white);
- colorBar = new ColorBar (10, 10, 30, 200);
- addMouseListener(this);
- addMouseMotionListener(this);
- }
-
- /**
- * Redisplay the drawing space
- * @param g - destination graphics object
- */
- public void update (Graphics g) {
-
- int i;
-
- for (i=0; i<npoints; i+=1) {
- g.setColor (colorBar.colors[color[i]]);
- g.fillOval (xpoints[i]-5, ypoints[i]-5, 10, 10);
- }
- colorBar.paint (g);
- }
-
- /**
- * Repaint the drawing space when required
- * @param g - destination graphics object
- */
- public void paint (Graphics g) {
-
- update (g);
- }
-
-
-
- public void mouseClicked(MouseEvent e){}
- public void mouseEntered(MouseEvent e){}
- public void mouseExited(MouseEvent e){}
- public void mouseReleased(MouseEvent e){}
- public void mouseMoved(MouseEvent e){}
-
- public void mousePressed(MouseEvent e)
- {
- int x =e.getX();
- int y =e.getY();
-
- if (colorBar.inside (x, y)) {
- inColorBar = true;
- repaint ();
- }
- inColorBar = false;
- if (npoints < MaxPoints) {
- lastx = x;
- lasty = y;
- xpoints[npoints] = x;
- ypoints[npoints] = y;
- color[npoints] = colorBar.selectedColor;
- npoints += 1;
- repaint();
- return;
- }
- }
-
- public void mouseDragged(MouseEvent e)
- {
- if (inColorBar) return;
-
- int x =e.getX();
- int y =e.getY();
-
- if ((x != lastx || y != lasty) && npoints < MaxPoints) {
- lastx = x;
- lasty = y;
- xpoints[npoints] = x;
- ypoints[npoints] = y;
- color[npoints] = colorBar.selectedColor;
- npoints += 1;
- repaint ();
- }
-
- }
-
- /**
- * The main method allows this class to be run as an application
- * in addition to being run as an applet.
- * @param args - command-line arguments
- */
-
- public static void main (String args[]) {
-
- Frame f = new Frame ("Doodle");
- Doodle doodle = new Doodle ();
- WindowListener l = new WindowAdapter()
- {
- public void windowClosing(WindowEvent e)
- {
- System.exit(0);
- }//windowClosing
- };//WindowListener
- f.addWindowListener(l);
-
-
- f.setSize (410, 430);
- f.add ("Center", doodle);
- f.show ();
- doodle.init ();
- }
- }
-