DrawTest.java revision 0
0N/A/*
1879N/A * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved.
0N/A *
0N/A * Redistribution and use in source and binary forms, with or without
0N/A * modification, are permitted provided that the following conditions
0N/A * are met:
0N/A *
0N/A * - Redistributions of source code must retain the above copyright
0N/A * notice, this list of conditions and the following disclaimer.
0N/A *
0N/A * - Redistributions in binary form must reproduce the above copyright
0N/A * notice, this list of conditions and the following disclaimer in the
0N/A * documentation and/or other materials provided with the distribution.
0N/A *
0N/A * - Neither the name of Sun Microsystems nor the names of its
0N/A * contributors may be used to endorse or promote products derived
0N/A * from this software without specific prior written permission.
0N/A *
1472N/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
1472N/A * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
1472N/A * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
0N/A * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
0N/A * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
0N/A * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
1879N/A * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
1879N/A * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
1879N/A * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
1879N/A * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
1879N/A * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1879N/A */
1879N/A
1879N/A/*
1879N/A */
1879N/A
1879N/Aimport java.awt.event.*;
1879N/Aimport java.awt.*;
1879N/Aimport java.applet.*;
1879N/A
1879N/Aimport java.util.Vector;
1879N/A
1879N/Apublic class DrawTest extends Applet{
2073N/A DrawPanel panel;
2073N/A DrawControls controls;
2073N/A
2073N/A public void init() {
2073N/A setLayout(new BorderLayout());
2073N/A panel = new DrawPanel();
1879N/A controls = new DrawControls(panel);
1879N/A add("Center", panel);
1879N/A add("South",controls);
1879N/A }
1879N/A
1879N/A public void destroy() {
1879N/A remove(panel);
1879N/A remove(controls);
1879N/A }
1879N/A
0N/A public static void main(String args[]) {
0N/A Frame f = new Frame("DrawTest");
0N/A DrawTest drawTest = new DrawTest();
0N/A drawTest.init();
0N/A drawTest.start();
0N/A
0N/A f.add("Center", drawTest);
0N/A f.setSize(300, 300);
0N/A f.show();
0N/A }
0N/A public String getAppletInfo() {
0N/A return "A simple drawing program.";
0N/A }
0N/A}
0N/A
0N/Aclass DrawPanel extends Panel implements MouseListener, MouseMotionListener {
0N/A public static final int LINES = 0;
0N/A public static final int POINTS = 1;
0N/A int mode = LINES;
0N/A Vector lines = new Vector();
0N/A Vector colors = new Vector();
0N/A int x1,y1;
0N/A int x2,y2;
0N/A
0N/A public DrawPanel() {
0N/A setBackground(Color.white);
0N/A addMouseMotionListener(this);
0N/A addMouseListener(this);
0N/A }
0N/A
0N/A public void setDrawMode(int mode) {
0N/A switch (mode) {
0N/A case LINES:
0N/A case POINTS:
0N/A this.mode = mode;
0N/A break;
0N/A default:
0N/A throw new IllegalArgumentException();
0N/A }
0N/A }
0N/A
0N/A
0N/A public void mouseDragged(MouseEvent e) {
0N/A e.consume();
0N/A switch (mode) {
0N/A case LINES:
0N/A x2 = e.getX();
0N/A y2 = e.getY();
0N/A break;
0N/A case POINTS:
0N/A default:
0N/A colors.addElement(getForeground());
0N/A lines.addElement(new Rectangle(x1, y1, e.getX(), e.getY()));
0N/A x1 = e.getX();
0N/A y1 = e.getY();
0N/A break;
0N/A }
0N/A repaint();
0N/A }
0N/A
0N/A public void mouseMoved(MouseEvent e) {
0N/A }
0N/A
0N/A public void mousePressed(MouseEvent e) {
0N/A e.consume();
0N/A switch (mode) {
0N/A case LINES:
0N/A x1 = e.getX();
0N/A y1 = e.getY();
0N/A x2 = -1;
0N/A break;
0N/A case POINTS:
0N/A default:
0N/A colors.addElement(getForeground());
1426N/A lines.addElement(new Rectangle(e.getX(), e.getY(), -1, -1));
0N/A x1 = e.getX();
0N/A y1 = e.getY();
1426N/A repaint();
0N/A break;
0N/A }
0N/A }
0N/A
0N/A public void mouseReleased(MouseEvent e) {
0N/A e.consume();
0N/A switch (mode) {
0N/A case LINES:
0N/A colors.addElement(getForeground());
0N/A lines.addElement(new Rectangle(x1, y1, e.getX(), e.getY()));
0N/A x2 = -1;
0N/A break;
0N/A case POINTS:
0N/A default:
0N/A break;
0N/A }
0N/A repaint();
0N/A }
0N/A
0N/A public void mouseEntered(MouseEvent e) {
0N/A }
0N/A
0N/A public void mouseExited(MouseEvent e) {
0N/A }
0N/A
0N/A public void mouseClicked(MouseEvent e) {
0N/A }
0N/A
0N/A public void paint(Graphics g) {
0N/A int np = lines.size();
0N/A
0N/A /* draw the current lines */
0N/A g.setColor(getForeground());
0N/A for (int i=0; i < np; i++) {
0N/A Rectangle p = (Rectangle)lines.elementAt(i);
0N/A g.setColor((Color)colors.elementAt(i));
0N/A if (p.width != -1) {
0N/A g.drawLine(p.x, p.y, p.width, p.height);
0N/A } else {
0N/A g.drawLine(p.x, p.y, p.x, p.y);
0N/A }
0N/A }
0N/A if (mode == LINES) {
0N/A g.setColor(getForeground());
0N/A if (x2 != -1) {
0N/A g.drawLine(x1, y1, x2, y2);
465N/A }
0N/A }
0N/A }
0N/A}
0N/A
0N/A
0N/Aclass DrawControls extends Panel implements ItemListener {
0N/A DrawPanel target;
0N/A
0N/A public DrawControls(DrawPanel target) {
0N/A this.target = target;
0N/A setLayout(new FlowLayout());
0N/A setBackground(Color.lightGray);
0N/A target.setForeground(Color.red);
0N/A CheckboxGroup group = new CheckboxGroup();
0N/A Checkbox b;
2062N/A add(b = new Checkbox(null, group, false));
0N/A b.addItemListener(this);
2062N/A b.setForeground(Color.red);
2062N/A add(b = new Checkbox(null, group, false));
2062N/A b.addItemListener(this);
0N/A b.setForeground(Color.green);
0N/A add(b = new Checkbox(null, group, false));
0N/A b.addItemListener(this);
0N/A b.setForeground(Color.blue);
0N/A add(b = new Checkbox(null, group, false));
2062N/A b.addItemListener(this);
0N/A b.setForeground(Color.pink);
2062N/A add(b = new Checkbox(null, group, false));
2062N/A b.addItemListener(this);
2062N/A b.setForeground(Color.orange);
0N/A add(b = new Checkbox(null, group, true));
0N/A b.addItemListener(this);
0N/A b.setForeground(Color.black);
2062N/A target.setForeground(b.getForeground());
0N/A Choice shapes = new Choice();
2062N/A shapes.addItemListener(this);
2062N/A shapes.addItem("Lines");
2062N/A shapes.addItem("Points");
0N/A shapes.setBackground(Color.lightGray);
0N/A add(shapes);
0N/A }
0N/A
1879N/A public void paint(Graphics g) {
1879N/A Rectangle r = getBounds();
g.setColor(Color.lightGray);
g.draw3DRect(0, 0, r.width, r.height, false);
int n = getComponentCount();
for(int i=0; i<n; i++) {
Component comp = getComponent(i);
if (comp instanceof Checkbox) {
Point loc = comp.getLocation();
Dimension d = comp.getSize();
g.setColor(comp.getForeground());
g.drawRect(loc.x-1, loc.y-1, d.width+1, d.height+1);
}
}
}
public void itemStateChanged(ItemEvent e) {
if (e.getSource() instanceof Checkbox) {
target.setForeground(((Component)e.getSource()).getForeground());
} else if (e.getSource() instanceof Choice) {
String choice = (String) e.getItem();
if (choice.equals("Lines")) {
target.setDrawMode(DrawPanel.LINES);
} else if (choice.equals("Points")) {
target.setDrawMode(DrawPanel.POINTS);
}
}
}
}