home *** CD-ROM | disk | FTP | other *** search
/ BUG 15 / BUGCD1998_06.ISO / aplic / jbuilder / jsamples.z / DrawTest.java < prev    next >
Encoding:
Java Source  |  1997-07-30  |  6.2 KB  |  237 lines

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