home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 July & August / Pcwk78a98.iso / Internet / Javadraw / DATA.Z / DrawTest.java < prev    next >
Text File  |  1997-03-20  |  6KB  |  246 lines

  1. /*
  2.  * @(#)DrawTest.java    1.1 97/03/20
  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. }
  56.  
  57. class DrawPanel extends Panel implements MouseListener, MouseMotionListener {
  58.     public static final int LINES = 0;
  59.     public static final int POINTS = 1;
  60.     int       mode = LINES;
  61.     Vector lines = new Vector();
  62.     Vector colors = new Vector();
  63.     int x1,y1;
  64.     int x2,y2;
  65.     int xl, yl;
  66.  
  67.     public DrawPanel() {
  68.     setBackground(Color.white);
  69.     addMouseMotionListener(this);
  70.     addMouseListener(this);
  71.     }
  72.  
  73.     public void setDrawMode(int mode) {
  74.     switch (mode) {
  75.       case LINES:
  76.       case POINTS:
  77.         this.mode = mode;
  78.         break;
  79.       default:
  80.         throw new IllegalArgumentException();
  81.     }
  82.     }
  83.  
  84.  
  85.   public void mouseDragged(MouseEvent e) {
  86.     e.consume();
  87.     switch (mode) {
  88.     case LINES:
  89.       xl = x2;
  90.       yl = y2;
  91.       x2 = e.getX();
  92.       y2 = e.getY();
  93.       break;
  94.     case POINTS:
  95.     default:
  96.       colors.addElement(getForeground());
  97.       lines.addElement(new Rectangle(x1, y1, e.getX(), e.getY()));
  98.       x1 = e.getX();
  99.       y1 = e.getY();
  100.       break;
  101.     }
  102.     repaint();
  103.   }
  104.  
  105.   public void mouseMoved(MouseEvent e) {
  106.   }
  107.  
  108.   public void mousePressed(MouseEvent e) {
  109.     e.consume();
  110.     switch (mode) {
  111.     case LINES:
  112.       x1 = e.getX();
  113.       y1 = e.getY();
  114.       x2 = -1;
  115.       break;
  116.     case POINTS:
  117.     default:
  118.       colors.addElement(getForeground());
  119.       lines.addElement(new Rectangle(e.getX(), e.getY(), -1, -1));
  120.       x1 = e.getX();
  121.       y1 = e.getY();
  122.       repaint();
  123.       break;
  124.     }
  125.   }
  126.  
  127.   public void mouseReleased(MouseEvent e) {
  128.     e.consume();
  129.     switch (mode) {
  130.     case LINES:
  131.       colors.addElement(getForeground());
  132.       lines.addElement(new Rectangle(x1, y1, e.getX(), e.getY()));
  133.       x2 = xl = -1;
  134.       break;
  135.     case POINTS:
  136.     default:
  137.       break;
  138.     }
  139.     repaint();  
  140.   }
  141.  
  142.   public void mouseEntered(MouseEvent e) {
  143.   }
  144.  
  145.   public void mouseExited(MouseEvent e) {
  146.   }
  147.  
  148.   public void mouseClicked(MouseEvent e) {
  149.   }
  150.  
  151.  
  152.  
  153.  
  154.     public void paint(Graphics g) {
  155.     int np = lines.size();
  156.  
  157.     /* draw the current lines */
  158.     g.setColor(getForeground());
  159.     g.setPaintMode();
  160.     for (int i=0; i < np; i++) {
  161.         Rectangle p = (Rectangle)lines.elementAt(i);
  162.         g.setColor((Color)colors.elementAt(i));
  163.         if (p.width != -1) {
  164.         g.drawLine(p.x, p.y, p.width, p.height);
  165.         } else {
  166.         g.drawLine(p.x, p.y, p.x, p.y);
  167.         }
  168.     }
  169.     if (mode == LINES) {
  170.         g.setXORMode(getBackground());
  171.         if (xl != -1) {
  172.         /* erase the last line. */
  173.         g.drawLine(x1, y1, xl, yl);
  174.         }
  175.         g.setColor(getForeground());
  176.         g.setPaintMode();
  177.         if (x2 != -1) {
  178.         g.drawLine(x1, y1, x2, y2);
  179.         }
  180.     }
  181.     }
  182. }
  183.  
  184.  
  185. class DrawControls extends Panel implements ItemListener {
  186.     DrawPanel target;
  187.  
  188.     public DrawControls(DrawPanel target) {
  189.     this.target = target;
  190.     setLayout(new FlowLayout());
  191.     setBackground(Color.lightGray);
  192.     target.setForeground(Color.red);
  193.     CheckboxGroup group = new CheckboxGroup();
  194.     Checkbox b;
  195.     add(b = new Checkbox(null, group, false));
  196.     b.addItemListener(this);
  197.     b.setBackground(Color.red);
  198.     add(b = new Checkbox(null, group, false));
  199.     b.addItemListener(this);
  200.     b.setBackground(Color.green);
  201.     add(b = new Checkbox(null, group, false));
  202.     b.addItemListener(this);
  203.     b.setBackground(Color.blue);
  204.     add(b = new Checkbox(null, group, false));
  205.     b.addItemListener(this);
  206.     b.setBackground(Color.pink);
  207.     add(b = new Checkbox(null, group, false));
  208.     b.addItemListener(this);
  209.     b.setBackground(Color.orange);
  210.     add(b = new Checkbox(null, group, true));
  211.     b.addItemListener(this);
  212.     b.setBackground(Color.black);
  213.     target.setForeground(b.getForeground());
  214.     Choice shapes = new Choice();
  215.     shapes.addItemListener(this);
  216.     shapes.addItem("Lines");
  217.     shapes.addItem("Points");
  218.     shapes.setBackground(Color.lightGray);
  219.     add(shapes);
  220.     }
  221.  
  222.     public void paint(Graphics g) {
  223.     Rectangle r = getBounds();
  224.  
  225.     g.setColor(Color.lightGray);
  226.     g.draw3DRect(0, 0, r.width, r.height, false);
  227.     }
  228.  
  229.   public void itemStateChanged(ItemEvent e) {
  230.     if (e.getSource() instanceof Checkbox) {
  231.       target.setForeground(((Component)e.getSource()).getBackground());
  232.     } else if (e.getSource() instanceof Choice) {
  233.       String choice = (String) e.getItem();
  234.       if (choice.equals("Lines")) {
  235.     target.setDrawMode(DrawPanel.LINES);
  236.       } else if (choice.equals("Points")) {
  237.     target.setDrawMode(DrawPanel.POINTS);
  238.       }
  239.     }
  240.   }
  241. }
  242.     
  243.  
  244.  
  245.     
  246.