home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / unsupported / JDK1.2beta3 / SOURCE / DEMO / APPLETS / DRAWTEST / DrawTest.java < prev    next >
Encoding:
Java Source  |  1998-03-20  |  6.3 KB  |  249 lines

  1. /*
  2.  * @(#)DrawTest.java    1.2 97/08/01
  3.  *
  4.  * Copyright (c) 1994-1996 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
  7.  * modify and redistribute this software in source and binary code form,
  8.  * provided that i) this copyright notice and license appear on all copies of
  9.  * the software; and ii) Licensee does not utilize the software in a manner
  10.  * which is disparaging to Sun.
  11.  *
  12.  * This software is provided "AS IS," without a warranty of any kind. ALL
  13.  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
  14.  * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
  15.  * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
  16.  * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
  17.  * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
  18.  * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
  19.  * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
  20.  * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
  21.  * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
  22.  * POSSIBILITY OF SUCH DAMAGES.
  23.  *
  24.  * This software is not designed or intended for use in on-line control of
  25.  * aircraft, air traffic, aircraft navigation or aircraft communications; or in
  26.  * the design, construction, operation or maintenance of any nuclear
  27.  * facility. Licensee represents and warrants that it will not use or
  28.  * redistribute the Software for such purposes.
  29.  */
  30.  
  31. import java.awt.event.*;
  32. import java.awt.*;
  33. import java.applet.*;
  34.  
  35. import java.util.Vector;
  36.  
  37. public class DrawTest extends Applet{
  38.     public void init() {
  39.     setLayout(new BorderLayout());
  40.     DrawPanel dp = new DrawPanel();
  41.     add("Center", dp);
  42.     add("South",new DrawControls(dp));
  43.     }
  44.  
  45.     public static void main(String args[]) {
  46.     Frame f = new Frame("DrawTest");
  47.     DrawTest drawTest = new DrawTest();
  48.     drawTest.init();
  49.     drawTest.start();
  50.  
  51.     f.add("Center", drawTest);
  52.     f.setSize(300, 300);
  53.     f.show();
  54.     }
  55.     public String getAppletInfo() {
  56.         return "A simple drawing program.";
  57.     }
  58. }
  59.  
  60. class DrawPanel extends Panel implements MouseListener, MouseMotionListener {
  61.     public static final int LINES = 0;
  62.     public static final int POINTS = 1;
  63.     int       mode = LINES;
  64.     Vector lines = new Vector();
  65.     Vector colors = new Vector();
  66.     int x1,y1;
  67.     int x2,y2;
  68.     int xl, yl;
  69.  
  70.     public DrawPanel() {
  71.     setBackground(Color.white);
  72.     addMouseMotionListener(this);
  73.     addMouseListener(this);
  74.     }
  75.  
  76.     public void setDrawMode(int mode) {
  77.     switch (mode) {
  78.       case LINES:
  79.       case POINTS:
  80.         this.mode = mode;
  81.         break;
  82.       default:
  83.         throw new IllegalArgumentException();
  84.     }
  85.     }
  86.  
  87.  
  88.   public void mouseDragged(MouseEvent e) {
  89.     e.consume();
  90.     switch (mode) {
  91.     case LINES:
  92.       xl = x2;
  93.       yl = y2;
  94.       x2 = e.getX();
  95.       y2 = e.getY();
  96.       break;
  97.     case POINTS:
  98.     default:
  99.       colors.addElement(getForeground());
  100.       lines.addElement(new Rectangle(x1, y1, e.getX(), e.getY()));
  101.       x1 = e.getX();
  102.       y1 = e.getY();
  103.       break;
  104.     }
  105.     repaint();
  106.   }
  107.  
  108.   public void mouseMoved(MouseEvent e) {
  109.   }
  110.  
  111.   public void mousePressed(MouseEvent e) {
  112.     e.consume();
  113.     switch (mode) {
  114.     case LINES:
  115.       x1 = e.getX();
  116.       y1 = e.getY();
  117.       x2 = -1;
  118.       break;
  119.     case POINTS:
  120.     default:
  121.       colors.addElement(getForeground());
  122.       lines.addElement(new Rectangle(e.getX(), e.getY(), -1, -1));
  123.       x1 = e.getX();
  124.       y1 = e.getY();
  125.       repaint();
  126.       break;
  127.     }
  128.   }
  129.  
  130.   public void mouseReleased(MouseEvent e) {
  131.     e.consume();
  132.     switch (mode) {
  133.     case LINES:
  134.       colors.addElement(getForeground());
  135.       lines.addElement(new Rectangle(x1, y1, e.getX(), e.getY()));
  136.       x2 = xl = -1;
  137.       break;
  138.     case POINTS:
  139.     default:
  140.       break;
  141.     }
  142.     repaint();  
  143.   }
  144.  
  145.   public void mouseEntered(MouseEvent e) {
  146.   }
  147.  
  148.   public void mouseExited(MouseEvent e) {
  149.   }
  150.  
  151.   public void mouseClicked(MouseEvent e) {
  152.   }
  153.  
  154.  
  155.  
  156.  
  157.     public void paint(Graphics g) {
  158.     int np = lines.size();
  159.  
  160.     /* draw the current lines */
  161.     g.setColor(getForeground());
  162.     g.setPaintMode();
  163.     for (int i=0; i < np; i++) {
  164.         Rectangle p = (Rectangle)lines.elementAt(i);
  165.         g.setColor((Color)colors.elementAt(i));
  166.         if (p.width != -1) {
  167.         g.drawLine(p.x, p.y, p.width, p.height);
  168.         } else {
  169.         g.drawLine(p.x, p.y, p.x, p.y);
  170.         }
  171.     }
  172.     if (mode == LINES) {
  173.         g.setXORMode(getBackground());
  174.         if (xl != -1) {
  175.         /* erase the last line. */
  176.         g.drawLine(x1, y1, xl, yl);
  177.         }
  178.         g.setColor(getForeground());
  179.         g.setPaintMode();
  180.         if (x2 != -1) {
  181.         g.drawLine(x1, y1, x2, y2);
  182.         }
  183.     }
  184.     }
  185. }
  186.  
  187.  
  188. class DrawControls extends Panel implements ItemListener {
  189.     DrawPanel target;
  190.  
  191.     public DrawControls(DrawPanel target) {
  192.     this.target = target;
  193.     setLayout(new FlowLayout());
  194.     setBackground(Color.lightGray);
  195.     target.setForeground(Color.red);
  196.     CheckboxGroup group = new CheckboxGroup();
  197.     Checkbox b;
  198.     add(b = new Checkbox(null, group, false));
  199.     b.addItemListener(this);
  200.     b.setBackground(Color.red);
  201.     add(b = new Checkbox(null, group, false));
  202.     b.addItemListener(this);
  203.     b.setBackground(Color.green);
  204.     add(b = new Checkbox(null, group, false));
  205.     b.addItemListener(this);
  206.     b.setBackground(Color.blue);
  207.     add(b = new Checkbox(null, group, false));
  208.     b.addItemListener(this);
  209.     b.setBackground(Color.pink);
  210.     add(b = new Checkbox(null, group, false));
  211.     b.addItemListener(this);
  212.     b.setBackground(Color.orange);
  213.     add(b = new Checkbox(null, group, true));
  214.     b.addItemListener(this);
  215.     b.setBackground(Color.black);
  216.     target.setForeground(b.getForeground());
  217.     Choice shapes = new Choice();
  218.     shapes.addItemListener(this);
  219.     shapes.addItem("Lines");
  220.     shapes.addItem("Points");
  221.     shapes.setBackground(Color.lightGray);
  222.     add(shapes);
  223.     }
  224.  
  225.     public void paint(Graphics g) {
  226.     Rectangle r = getBounds();
  227.  
  228.     g.setColor(Color.lightGray);
  229.     g.draw3DRect(0, 0, r.width, r.height, false);
  230.     }
  231.  
  232.   public void itemStateChanged(ItemEvent e) {
  233.     if (e.getSource() instanceof Checkbox) {
  234.       target.setForeground(((Component)e.getSource()).getBackground());
  235.     } else if (e.getSource() instanceof Choice) {
  236.       String choice = (String) e.getItem();
  237.       if (choice.equals("Lines")) {
  238.     target.setDrawMode(DrawPanel.LINES);
  239.       } else if (choice.equals("Points")) {
  240.     target.setDrawMode(DrawPanel.POINTS);
  241.       }
  242.     }
  243.   }
  244. }
  245.     
  246.  
  247.  
  248.     
  249.