jdk/src/share/demo/applets/DrawTest/DrawTest.java
changeset 8962 8833da0e7bc9
parent 5506 202f599c92aa
child 10292 ed7db6a12c2a
equal deleted inserted replaced
8961:6e8562ef340d 8962:8833da0e7bc9
     1 /*
     1 /*
     2  * Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
     3  *
     3  *
     4  * Redistribution and use in source and binary forms, with or without
     4  * Redistribution and use in source and binary forms, with or without
     5  * modification, are permitted provided that the following conditions
     5  * modification, are permitted provided that the following conditions
     6  * are met:
     6  * are met:
     7  *
     7  *
    27  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
    27  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
    28  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    28  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    29  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    29  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    30  */
    30  */
    31 
    31 
    32 /*
    32 
    33  */
    33 import java.applet.Applet;
    34 
    34 import java.awt.BorderLayout;
    35 import java.awt.event.*;
    35 import java.awt.Checkbox;
    36 import java.awt.*;
    36 import java.awt.CheckboxGroup;
    37 import java.applet.*;
    37 import java.awt.Choice;
    38 
    38 import java.awt.Color;
    39 import java.util.Vector;
    39 import java.awt.Component;
    40 
    40 import java.awt.Dimension;
    41 public class DrawTest extends Applet{
    41 import java.awt.FlowLayout;
       
    42 import java.awt.Frame;
       
    43 import java.awt.Graphics;
       
    44 import java.awt.Panel;
       
    45 import java.awt.Point;
       
    46 import java.awt.Rectangle;
       
    47 import java.awt.event.ItemEvent;
       
    48 import java.awt.event.ItemListener;
       
    49 import java.awt.event.MouseEvent;
       
    50 import java.awt.event.MouseListener;
       
    51 import java.awt.event.MouseMotionListener;
       
    52 import java.util.ArrayList;
       
    53 import java.util.List;
       
    54 
       
    55 
       
    56 @SuppressWarnings("serial")
       
    57 public class DrawTest extends Applet {
       
    58 
    42     DrawPanel panel;
    59     DrawPanel panel;
    43     DrawControls controls;
    60     DrawControls controls;
    44 
    61 
       
    62     @Override
    45     public void init() {
    63     public void init() {
    46         setLayout(new BorderLayout());
    64         setLayout(new BorderLayout());
    47         panel = new DrawPanel();
    65         panel = new DrawPanel();
    48         controls = new DrawControls(panel);
    66         controls = new DrawControls(panel);
    49         add("Center", panel);
    67         add("Center", panel);
    50         add("South",controls);
    68         add("South", controls);
    51     }
    69     }
    52 
    70 
       
    71     @Override
    53     public void destroy() {
    72     public void destroy() {
    54         remove(panel);
    73         remove(panel);
    55         remove(controls);
    74         remove(controls);
    56     }
    75     }
    57 
    76 
    61         drawTest.init();
    80         drawTest.init();
    62         drawTest.start();
    81         drawTest.start();
    63 
    82 
    64         f.add("Center", drawTest);
    83         f.add("Center", drawTest);
    65         f.setSize(300, 300);
    84         f.setSize(300, 300);
    66         f.show();
    85         f.setVisible(true);
    67     }
    86     }
       
    87 
       
    88     @Override
    68     public String getAppletInfo() {
    89     public String getAppletInfo() {
    69         return "A simple drawing program.";
    90         return "A simple drawing program.";
    70     }
    91     }
    71 }
    92 }
    72 
    93 
       
    94 
       
    95 @SuppressWarnings("serial")
    73 class DrawPanel extends Panel implements MouseListener, MouseMotionListener {
    96 class DrawPanel extends Panel implements MouseListener, MouseMotionListener {
       
    97 
    74     public static final int LINES = 0;
    98     public static final int LINES = 0;
    75     public static final int POINTS = 1;
    99     public static final int POINTS = 1;
    76     int    mode = LINES;
   100     int mode = LINES;
    77     Vector lines = new Vector();
   101     List<Rectangle> lines = new ArrayList<Rectangle>();
    78     Vector colors = new Vector();
   102     List<Color> colors = new ArrayList<Color>();
    79     int x1,y1;
   103     int x1, y1;
    80     int x2,y2;
   104     int x2, y2;
    81 
   105 
       
   106     @SuppressWarnings("LeakingThisInConstructor")
    82     public DrawPanel() {
   107     public DrawPanel() {
    83         setBackground(Color.white);
   108         setBackground(Color.white);
    84         addMouseMotionListener(this);
   109         addMouseMotionListener(this);
    85         addMouseListener(this);
   110         addMouseListener(this);
    86     }
   111     }
    87 
   112 
    88     public void setDrawMode(int mode) {
   113     public void setDrawMode(int mode) {
    89         switch (mode) {
   114         switch (mode) {
    90           case LINES:
   115             case LINES:
    91           case POINTS:
   116             case POINTS:
    92             this.mode = mode;
   117                 this.mode = mode;
    93             break;
   118                 break;
    94           default:
   119             default:
    95             throw new IllegalArgumentException();
   120                 throw new IllegalArgumentException();
    96         }
   121         }
    97     }
   122     }
    98 
   123 
    99 
   124     @Override
   100     public void mouseDragged(MouseEvent e) {
   125     public void mouseDragged(MouseEvent e) {
   101         e.consume();
   126         e.consume();
   102         switch (mode) {
   127         switch (mode) {
   103             case LINES:
   128             case LINES:
   104                 x2 = e.getX();
   129                 x2 = e.getX();
   105                 y2 = e.getY();
   130                 y2 = e.getY();
   106                 break;
   131                 break;
   107             case POINTS:
   132             case POINTS:
   108             default:
   133             default:
   109                 colors.addElement(getForeground());
   134                 colors.add(getForeground());
   110                 lines.addElement(new Rectangle(x1, y1, e.getX(), e.getY()));
   135                 lines.add(new Rectangle(x1, y1, e.getX(), e.getY()));
   111                 x1 = e.getX();
   136                 x1 = e.getX();
   112                 y1 = e.getY();
   137                 y1 = e.getY();
   113                 break;
   138                 break;
   114         }
   139         }
   115         repaint();
   140         repaint();
   116     }
   141     }
   117 
   142 
       
   143     @Override
   118     public void mouseMoved(MouseEvent e) {
   144     public void mouseMoved(MouseEvent e) {
   119     }
   145     }
   120 
   146 
       
   147     @Override
   121     public void mousePressed(MouseEvent e) {
   148     public void mousePressed(MouseEvent e) {
   122         e.consume();
   149         e.consume();
   123         switch (mode) {
   150         switch (mode) {
   124             case LINES:
   151             case LINES:
   125                 x1 = e.getX();
   152                 x1 = e.getX();
   126                 y1 = e.getY();
   153                 y1 = e.getY();
   127                 x2 = -1;
   154                 x2 = -1;
   128                 break;
   155                 break;
   129             case POINTS:
   156             case POINTS:
   130             default:
   157             default:
   131                 colors.addElement(getForeground());
   158                 colors.add(getForeground());
   132                 lines.addElement(new Rectangle(e.getX(), e.getY(), -1, -1));
   159                 lines.add(new Rectangle(e.getX(), e.getY(), -1, -1));
   133                 x1 = e.getX();
   160                 x1 = e.getX();
   134                 y1 = e.getY();
   161                 y1 = e.getY();
   135                 repaint();
   162                 repaint();
   136                 break;
   163                 break;
   137         }
   164         }
   138     }
   165     }
   139 
   166 
       
   167     @Override
   140     public void mouseReleased(MouseEvent e) {
   168     public void mouseReleased(MouseEvent e) {
   141         e.consume();
   169         e.consume();
   142         switch (mode) {
   170         switch (mode) {
   143             case LINES:
   171             case LINES:
   144                 colors.addElement(getForeground());
   172                 colors.add(getForeground());
   145                 lines.addElement(new Rectangle(x1, y1, e.getX(), e.getY()));
   173                 lines.add(new Rectangle(x1, y1, e.getX(), e.getY()));
   146                 x2 = -1;
   174                 x2 = -1;
   147                 break;
   175                 break;
   148             case POINTS:
   176             case POINTS:
   149             default:
   177             default:
   150                 break;
   178                 break;
   151         }
   179         }
   152         repaint();
   180         repaint();
   153     }
   181     }
   154 
   182 
       
   183     @Override
   155     public void mouseEntered(MouseEvent e) {
   184     public void mouseEntered(MouseEvent e) {
   156     }
   185     }
   157 
   186 
       
   187     @Override
   158     public void mouseExited(MouseEvent e) {
   188     public void mouseExited(MouseEvent e) {
   159     }
   189     }
   160 
   190 
       
   191     @Override
   161     public void mouseClicked(MouseEvent e) {
   192     public void mouseClicked(MouseEvent e) {
   162     }
   193     }
   163 
   194 
       
   195     @Override
   164     public void paint(Graphics g) {
   196     public void paint(Graphics g) {
   165         int np = lines.size();
   197         int np = lines.size();
   166 
   198 
   167         /* draw the current lines */
   199         /* draw the current lines */
   168         g.setColor(getForeground());
   200         g.setColor(getForeground());
   169         for (int i=0; i < np; i++) {
   201         for (int i = 0; i < np; i++) {
   170             Rectangle p = (Rectangle)lines.elementAt(i);
   202             Rectangle p = lines.get(i);
   171             g.setColor((Color)colors.elementAt(i));
   203             g.setColor(colors.get(i));
   172             if (p.width != -1) {
   204             if (p.width != -1) {
   173                 g.drawLine(p.x, p.y, p.width, p.height);
   205                 g.drawLine(p.x, p.y, p.width, p.height);
   174             } else {
   206             } else {
   175                 g.drawLine(p.x, p.y, p.x, p.y);
   207                 g.drawLine(p.x, p.y, p.x, p.y);
   176             }
   208             }
   183         }
   215         }
   184     }
   216     }
   185 }
   217 }
   186 
   218 
   187 
   219 
       
   220 @SuppressWarnings("serial")
   188 class DrawControls extends Panel implements ItemListener {
   221 class DrawControls extends Panel implements ItemListener {
       
   222 
   189     DrawPanel target;
   223     DrawPanel target;
   190 
   224 
       
   225     @SuppressWarnings("LeakingThisInConstructor")
   191     public DrawControls(DrawPanel target) {
   226     public DrawControls(DrawPanel target) {
   192         this.target = target;
   227         this.target = target;
   193         setLayout(new FlowLayout());
   228         setLayout(new FlowLayout());
   194         setBackground(Color.lightGray);
   229         setBackground(Color.lightGray);
   195         target.setForeground(Color.red);
   230         target.setForeground(Color.red);
   220         shapes.addItem("Points");
   255         shapes.addItem("Points");
   221         shapes.setBackground(Color.lightGray);
   256         shapes.setBackground(Color.lightGray);
   222         add(shapes);
   257         add(shapes);
   223     }
   258     }
   224 
   259 
       
   260     @Override
   225     public void paint(Graphics g) {
   261     public void paint(Graphics g) {
   226         Rectangle r = getBounds();
   262         Rectangle r = getBounds();
   227         g.setColor(Color.lightGray);
   263         g.setColor(Color.lightGray);
   228         g.draw3DRect(0, 0, r.width, r.height, false);
   264         g.draw3DRect(0, 0, r.width, r.height, false);
   229 
   265 
   230         int n = getComponentCount();
   266         int n = getComponentCount();
   231         for(int i=0; i<n; i++) {
   267         for (int i = 0; i < n; i++) {
   232             Component comp = getComponent(i);
   268             Component comp = getComponent(i);
   233             if (comp instanceof Checkbox) {
   269             if (comp instanceof Checkbox) {
   234                 Point loc = comp.getLocation();
   270                 Point loc = comp.getLocation();
   235                 Dimension d = comp.getSize();
   271                 Dimension d = comp.getSize();
   236                 g.setColor(comp.getForeground());
   272                 g.setColor(comp.getForeground());
   237                 g.drawRect(loc.x-1, loc.y-1, d.width+1, d.height+1);
   273                 g.drawRect(loc.x - 1, loc.y - 1, d.width + 1, d.height + 1);
   238             }
   274             }
   239         }
   275         }
   240     }
   276     }
   241 
   277 
   242   public void itemStateChanged(ItemEvent e) {
   278     @Override
   243     if (e.getSource() instanceof Checkbox) {
   279     public void itemStateChanged(ItemEvent e) {
   244       target.setForeground(((Component)e.getSource()).getForeground());
   280         if (e.getSource() instanceof Checkbox) {
   245     } else if (e.getSource() instanceof Choice) {
   281             target.setForeground(((Component) e.getSource()).getForeground());
   246       String choice = (String) e.getItem();
   282         } else if (e.getSource() instanceof Choice) {
   247       if (choice.equals("Lines")) {
   283             String choice = (String) e.getItem();
   248         target.setDrawMode(DrawPanel.LINES);
   284             if (choice.equals("Lines")) {
   249       } else if (choice.equals("Points")) {
   285                 target.setDrawMode(DrawPanel.LINES);
   250         target.setDrawMode(DrawPanel.POINTS);
   286             } else if (choice.equals("Points")) {
   251       }
   287                 target.setDrawMode(DrawPanel.POINTS);
   252     }
   288             }
   253   }
   289         }
       
   290     }
   254 }
   291 }