home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / JBuilder8.iso / Solaris / resource / jre / demo / plugin / applets / DrawTest / DrawTest.java < prev    next >
Encoding:
Java Source  |  2002-09-06  |  7.2 KB  |  264 lines

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