Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/demo/applets/DrawTest/DrawTest.java
38827 views
/*1* Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.2*3* Redistribution and use in source and binary forms, with or without4* modification, are permitted provided that the following conditions5* are met:6*7* - Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9*10* - Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* - Neither the name of Oracle nor the names of its15* contributors may be used to endorse or promote products derived16* from this software without specific prior written permission.17*18* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS19* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,20* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR21* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR22* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,23* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,24* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR25* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF26* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING27* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS28* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.29*/3031/*32* This source code is provided to illustrate the usage of a given feature33* or technique and has been deliberately simplified. Additional steps34* required for a production-quality application, such as security checks,35* input validation and proper error handling, might not be present in36* this sample code.37*/38394041import java.applet.Applet;42import java.awt.BorderLayout;43import java.awt.Checkbox;44import java.awt.CheckboxGroup;45import java.awt.Choice;46import java.awt.Color;47import java.awt.Component;48import java.awt.Dimension;49import java.awt.FlowLayout;50import java.awt.Frame;51import java.awt.Graphics;52import java.awt.Panel;53import java.awt.Point;54import java.awt.Rectangle;55import java.awt.event.ItemEvent;56import java.awt.event.ItemListener;57import java.awt.event.MouseEvent;58import java.awt.event.MouseListener;59import java.awt.event.MouseMotionListener;60import java.util.ArrayList;61import java.util.List;626364@SuppressWarnings("serial")65public class DrawTest extends Applet {6667DrawPanel panel;68DrawControls controls;6970@Override71public void init() {72setLayout(new BorderLayout());73panel = new DrawPanel();74controls = new DrawControls(panel);75add("Center", panel);76add("South", controls);77}7879@Override80public void destroy() {81remove(panel);82remove(controls);83}8485public static void main(String args[]) {86Frame f = new Frame("DrawTest");87DrawTest drawTest = new DrawTest();88drawTest.init();89drawTest.start();9091f.add("Center", drawTest);92f.setSize(300, 300);93f.setVisible(true);94}9596@Override97public String getAppletInfo() {98return "A simple drawing program.";99}100}101102103@SuppressWarnings("serial")104class DrawPanel extends Panel implements MouseListener, MouseMotionListener {105106public static final int LINES = 0;107public static final int POINTS = 1;108int mode = LINES;109List<Rectangle> lines = new ArrayList<Rectangle>();110List<Color> colors = new ArrayList<Color>();111int x1, y1;112int x2, y2;113114@SuppressWarnings("LeakingThisInConstructor")115public DrawPanel() {116setBackground(Color.white);117addMouseMotionListener(this);118addMouseListener(this);119}120121public void setDrawMode(int mode) {122switch (mode) {123case LINES:124case POINTS:125this.mode = mode;126break;127default:128throw new IllegalArgumentException();129}130}131132@Override133public void mouseDragged(MouseEvent e) {134e.consume();135switch (mode) {136case LINES:137x2 = e.getX();138y2 = e.getY();139break;140case POINTS:141default:142colors.add(getForeground());143lines.add(new Rectangle(x1, y1, e.getX(), e.getY()));144x1 = e.getX();145y1 = e.getY();146break;147}148repaint();149}150151@Override152public void mouseMoved(MouseEvent e) {153}154155@Override156public void mousePressed(MouseEvent e) {157e.consume();158switch (mode) {159case LINES:160x1 = e.getX();161y1 = e.getY();162x2 = -1;163break;164case POINTS:165default:166colors.add(getForeground());167lines.add(new Rectangle(e.getX(), e.getY(), -1, -1));168x1 = e.getX();169y1 = e.getY();170repaint();171break;172}173}174175@Override176public void mouseReleased(MouseEvent e) {177e.consume();178switch (mode) {179case LINES:180colors.add(getForeground());181lines.add(new Rectangle(x1, y1, e.getX(), e.getY()));182x2 = -1;183break;184case POINTS:185default:186break;187}188repaint();189}190191@Override192public void mouseEntered(MouseEvent e) {193}194195@Override196public void mouseExited(MouseEvent e) {197}198199@Override200public void mouseClicked(MouseEvent e) {201}202203@Override204public void paint(Graphics g) {205int np = lines.size();206207/* draw the current lines */208g.setColor(getForeground());209for (int i = 0; i < np; i++) {210Rectangle p = lines.get(i);211g.setColor(colors.get(i));212if (p.width != -1) {213g.drawLine(p.x, p.y, p.width, p.height);214} else {215g.drawLine(p.x, p.y, p.x, p.y);216}217}218if (mode == LINES) {219g.setColor(getForeground());220if (x2 != -1) {221g.drawLine(x1, y1, x2, y2);222}223}224}225}226227228@SuppressWarnings("serial")229class DrawControls extends Panel implements ItemListener {230231DrawPanel target;232233@SuppressWarnings("LeakingThisInConstructor")234public DrawControls(DrawPanel target) {235this.target = target;236setLayout(new FlowLayout());237setBackground(Color.lightGray);238target.setForeground(Color.red);239CheckboxGroup group = new CheckboxGroup();240Checkbox b;241add(b = new Checkbox(null, group, false));242b.addItemListener(this);243b.setForeground(Color.red);244add(b = new Checkbox(null, group, false));245b.addItemListener(this);246b.setForeground(Color.green);247add(b = new Checkbox(null, group, false));248b.addItemListener(this);249b.setForeground(Color.blue);250add(b = new Checkbox(null, group, false));251b.addItemListener(this);252b.setForeground(Color.pink);253add(b = new Checkbox(null, group, false));254b.addItemListener(this);255b.setForeground(Color.orange);256add(b = new Checkbox(null, group, true));257b.addItemListener(this);258b.setForeground(Color.black);259target.setForeground(b.getForeground());260Choice shapes = new Choice();261shapes.addItemListener(this);262shapes.addItem("Lines");263shapes.addItem("Points");264shapes.setBackground(Color.lightGray);265add(shapes);266}267268@Override269public void paint(Graphics g) {270Rectangle r = getBounds();271g.setColor(Color.lightGray);272g.draw3DRect(0, 0, r.width, r.height, false);273274int n = getComponentCount();275for (int i = 0; i < n; i++) {276Component comp = getComponent(i);277if (comp instanceof Checkbox) {278Point loc = comp.getLocation();279Dimension d = comp.getSize();280g.setColor(comp.getForeground());281g.drawRect(loc.x - 1, loc.y - 1, d.width + 1, d.height + 1);282}283}284}285286@Override287public void itemStateChanged(ItemEvent e) {288if (e.getSource() instanceof Checkbox) {289target.setForeground(((Component) e.getSource()).getForeground());290} else if (e.getSource() instanceof Choice) {291String choice = (String) e.getItem();292if (choice.equals("Lines")) {293target.setDrawMode(DrawPanel.LINES);294} else if (choice.equals("Points")) {295target.setDrawMode(DrawPanel.POINTS);296}297}298}299}300301302