0N/A/*
3836N/A * Copyright (c) 1997, 2011, Oracle and/or its affiliates. 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 *
2362N/A * - Neither the name of Oracle 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 *
0N/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
0N/A * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
0N/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,
0N/A * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
0N/A * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
0N/A * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
0N/A * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
0N/A * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0N/A */
0N/A
4378N/A/*
4378N/A * This source code is provided to illustrate the usage of a given feature
4378N/A * or technique and has been deliberately simplified. Additional steps
4378N/A * required for a production-quality application, such as security checks,
4378N/A * input validation and proper error handling, might not be present in
4378N/A * this sample code.
4378N/A */
4378N/A
4378N/A
0N/A
3836N/Aimport java.applet.Applet;
3836N/Aimport java.awt.BorderLayout;
3836N/Aimport java.awt.Checkbox;
3836N/Aimport java.awt.CheckboxGroup;
3836N/Aimport java.awt.Choice;
3836N/Aimport java.awt.Color;
3836N/Aimport java.awt.Component;
3836N/Aimport java.awt.Dimension;
3836N/Aimport java.awt.FlowLayout;
3836N/Aimport java.awt.Frame;
3836N/Aimport java.awt.Graphics;
3836N/Aimport java.awt.Panel;
3836N/Aimport java.awt.Point;
3836N/Aimport java.awt.Rectangle;
3836N/Aimport java.awt.event.ItemEvent;
3836N/Aimport java.awt.event.ItemListener;
3836N/Aimport java.awt.event.MouseEvent;
3836N/Aimport java.awt.event.MouseListener;
3836N/Aimport java.awt.event.MouseMotionListener;
3836N/Aimport java.util.ArrayList;
3836N/Aimport java.util.List;
0N/A
0N/A
3836N/A@SuppressWarnings("serial")
3836N/Apublic class DrawTest extends Applet {
3836N/A
0N/A DrawPanel panel;
0N/A DrawControls controls;
0N/A
3836N/A @Override
0N/A public void init() {
0N/A setLayout(new BorderLayout());
0N/A panel = new DrawPanel();
0N/A controls = new DrawControls(panel);
0N/A add("Center", panel);
3836N/A add("South", controls);
0N/A }
0N/A
3836N/A @Override
0N/A public void destroy() {
0N/A remove(panel);
0N/A remove(controls);
0N/A }
0N/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);
3836N/A f.setVisible(true);
0N/A }
3836N/A
3836N/A @Override
0N/A public String getAppletInfo() {
0N/A return "A simple drawing program.";
0N/A }
0N/A}
0N/A
3836N/A
3836N/A@SuppressWarnings("serial")
0N/Aclass DrawPanel extends Panel implements MouseListener, MouseMotionListener {
3836N/A
0N/A public static final int LINES = 0;
0N/A public static final int POINTS = 1;
3836N/A int mode = LINES;
3836N/A List<Rectangle> lines = new ArrayList<Rectangle>();
3836N/A List<Color> colors = new ArrayList<Color>();
3836N/A int x1, y1;
3836N/A int x2, y2;
0N/A
3836N/A @SuppressWarnings("LeakingThisInConstructor")
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) {
3836N/A case LINES:
3836N/A case POINTS:
3836N/A this.mode = mode;
3836N/A break;
3836N/A default:
3836N/A throw new IllegalArgumentException();
0N/A }
0N/A }
0N/A
3836N/A @Override
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:
3836N/A colors.add(getForeground());
3836N/A lines.add(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
3836N/A @Override
0N/A public void mouseMoved(MouseEvent e) {
0N/A }
0N/A
3836N/A @Override
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:
3836N/A colors.add(getForeground());
3836N/A lines.add(new Rectangle(e.getX(), e.getY(), -1, -1));
0N/A x1 = e.getX();
0N/A y1 = e.getY();
0N/A repaint();
0N/A break;
0N/A }
0N/A }
0N/A
3836N/A @Override
0N/A public void mouseReleased(MouseEvent e) {
0N/A e.consume();
0N/A switch (mode) {
0N/A case LINES:
3836N/A colors.add(getForeground());
3836N/A lines.add(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
3836N/A @Override
0N/A public void mouseEntered(MouseEvent e) {
0N/A }
0N/A
3836N/A @Override
0N/A public void mouseExited(MouseEvent e) {
0N/A }
0N/A
3836N/A @Override
0N/A public void mouseClicked(MouseEvent e) {
0N/A }
0N/A
3836N/A @Override
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());
3836N/A for (int i = 0; i < np; i++) {
3836N/A Rectangle p = lines.get(i);
3836N/A g.setColor(colors.get(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);
0N/A }
0N/A }
0N/A }
0N/A}
0N/A
0N/A
3836N/A@SuppressWarnings("serial")
0N/Aclass DrawControls extends Panel implements ItemListener {
3836N/A
0N/A DrawPanel target;
0N/A
3836N/A @SuppressWarnings("LeakingThisInConstructor")
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;
0N/A add(b = new Checkbox(null, group, false));
0N/A b.addItemListener(this);
0N/A b.setForeground(Color.red);
0N/A add(b = new Checkbox(null, group, false));
0N/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));
0N/A b.addItemListener(this);
0N/A b.setForeground(Color.pink);
0N/A add(b = new Checkbox(null, group, false));
0N/A b.addItemListener(this);
0N/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);
0N/A target.setForeground(b.getForeground());
0N/A Choice shapes = new Choice();
0N/A shapes.addItemListener(this);
0N/A shapes.addItem("Lines");
0N/A shapes.addItem("Points");
0N/A shapes.setBackground(Color.lightGray);
0N/A add(shapes);
0N/A }
0N/A
3836N/A @Override
0N/A public void paint(Graphics g) {
0N/A Rectangle r = getBounds();
0N/A g.setColor(Color.lightGray);
0N/A g.draw3DRect(0, 0, r.width, r.height, false);
0N/A
0N/A int n = getComponentCount();
3836N/A for (int i = 0; i < n; i++) {
0N/A Component comp = getComponent(i);
0N/A if (comp instanceof Checkbox) {
0N/A Point loc = comp.getLocation();
0N/A Dimension d = comp.getSize();
0N/A g.setColor(comp.getForeground());
3836N/A g.drawRect(loc.x - 1, loc.y - 1, d.width + 1, d.height + 1);
0N/A }
0N/A }
0N/A }
0N/A
3836N/A @Override
3836N/A public void itemStateChanged(ItemEvent e) {
3836N/A if (e.getSource() instanceof Checkbox) {
3836N/A target.setForeground(((Component) e.getSource()).getForeground());
3836N/A } else if (e.getSource() instanceof Choice) {
3836N/A String choice = (String) e.getItem();
3836N/A if (choice.equals("Lines")) {
3836N/A target.setDrawMode(DrawPanel.LINES);
3836N/A } else if (choice.equals("Points")) {
3836N/A target.setDrawMode(DrawPanel.POINTS);
3836N/A }
3836N/A }
0N/A }
0N/A}