Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/demo/applets/ArcTest/ArcTest.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.awt.*;42import java.awt.event.*;43import java.applet.*;444546/**47* An interactive test of the Graphics.drawArc and Graphics.fillArc48* routines. Can be run either as a standalone application by49* typing "java ArcTest" or as an applet in the AppletViewer.50*/51@SuppressWarnings("serial")52public class ArcTest extends Applet {5354ArcControls controls; // The controls for marking and filling arcs55ArcCanvas canvas; // The drawing area to display arcs5657@Override58public void init() {59setLayout(new BorderLayout());60canvas = new ArcCanvas();61add("Center", canvas);62add("South", controls = new ArcControls(canvas));63}6465@Override66public void destroy() {67remove(controls);68remove(canvas);69}7071@Override72public void start() {73controls.setEnabled(true);74}7576@Override77public void stop() {78controls.setEnabled(false);79}8081@Override82public void processEvent(AWTEvent e) {83if (e.getID() == Event.WINDOW_DESTROY) {84System.exit(0);85}86}8788public static void main(String args[]) {89Frame f = new Frame("ArcTest");90ArcTest arcTest = new ArcTest();9192arcTest.init();93arcTest.start();9495f.add("Center", arcTest);96f.setSize(300, 300);97f.setVisible(true);98}99100@Override101public String getAppletInfo() {102return "An interactive test of the Graphics.drawArc and \nGraphics."103+ "fillArc routines. Can be run \neither as a standalone "104+ "application by typing 'java ArcTest' \nor as an applet in "105+ "the AppletViewer.";106}107}108109110@SuppressWarnings("serial")111class ArcCanvas extends Canvas {112113int startAngle = 0;114int extent = 45;115boolean filled = false;116Font font = new java.awt.Font("SansSerif", Font.PLAIN, 12);117118@Override119public void paint(Graphics g) {120Rectangle r = getBounds();121int hlines = r.height / 10;122int vlines = r.width / 10;123124g.setColor(Color.pink);125for (int i = 1; i <= hlines; i++) {126g.drawLine(0, i * 10, r.width, i * 10);127}128for (int i = 1; i <= vlines; i++) {129g.drawLine(i * 10, 0, i * 10, r.height);130}131132g.setColor(Color.red);133if (filled) {134g.fillArc(0, 0, r.width - 1, r.height - 1, startAngle, extent);135} else {136g.drawArc(0, 0, r.width - 1, r.height - 1, startAngle, extent);137}138139g.setColor(Color.black);140g.setFont(font);141g.drawLine(0, r.height / 2, r.width, r.height / 2);142g.drawLine(r.width / 2, 0, r.width / 2, r.height);143g.drawLine(0, 0, r.width, r.height);144g.drawLine(r.width, 0, 0, r.height);145int sx = 10;146int sy = r.height - 28;147g.drawString("Start = " + startAngle, sx, sy);148g.drawString("Extent = " + extent, sx, sy + 14);149}150151public void redraw(boolean filled, int start, int extent) {152this.filled = filled;153this.startAngle = start;154this.extent = extent;155repaint();156}157}158159160@SuppressWarnings("serial")161class ArcControls extends Panel162implements ActionListener {163164TextField startTF;165TextField extentTF;166ArcCanvas canvas;167168@SuppressWarnings("LeakingThisInConstructor")169public ArcControls(ArcCanvas canvas) {170Button b = null;171172this.canvas = canvas;173add(startTF = new IntegerTextField("0", 4));174add(extentTF = new IntegerTextField("45", 4));175b = new Button("Fill");176b.addActionListener(this);177add(b);178b = new Button("Draw");179b.addActionListener(this);180add(b);181}182183@Override184public void actionPerformed(ActionEvent ev) {185String label = ev.getActionCommand();186187int start, extent;188try {189start = Integer.parseInt(startTF.getText().trim());190} catch (NumberFormatException ignored) {191start = 0;192}193try {194extent = Integer.parseInt(extentTF.getText().trim());195} catch (NumberFormatException ignored) {196extent = 0;197}198199canvas.redraw(label.equals("Fill"), start, extent);200}201}202203204@SuppressWarnings("serial")205class IntegerTextField extends TextField {206207String oldText = null;208209public IntegerTextField(String text, int columns) {210super(text, columns);211enableEvents(AWTEvent.KEY_EVENT_MASK | AWTEvent.TEXT_EVENT_MASK);212oldText = getText();213}214215// Consume non-digit KeyTyped events216// Note that processTextEvent kind of eliminates the need for this217// function, but this is neater, since ideally, it would prevent218// the text from appearing at all. Sigh. See bugid 4100317/4114565.219//220@Override221protected void processEvent(AWTEvent evt) {222int id = evt.getID();223if (id != KeyEvent.KEY_TYPED) {224super.processEvent(evt);225return;226}227228KeyEvent kevt = (KeyEvent) evt;229char c = kevt.getKeyChar();230231// Digits, backspace, and delete are okay232// Note that the minus sign is allowed, but not the decimal233if (Character.isDigit(c) || (c == '\b') || (c == '\u007f') || (c234== '\u002d')) {235super.processEvent(evt);236return;237}238239Toolkit.getDefaultToolkit().beep();240kevt.consume();241}242243// Should consume TextEvents for non-integer Strings244// Store away the text in the tf for every TextEvent245// so we can revert to it on a TextEvent (paste, or246// legal key in the wrong location) with bad text247//248@Override249protected void processTextEvent(TextEvent te) {250// The empty string is okay, too251String newText = getText();252if (newText.equals("") || textIsInteger(newText)) {253oldText = newText;254super.processTextEvent(te);255return;256}257258Toolkit.getDefaultToolkit().beep();259setText(oldText);260}261262// Returns true for Integers (zero and negative263// values are allowed).264// Note that the empty string is not allowed.265//266private boolean textIsInteger(String textToCheck) {267268try {269Integer.parseInt(textToCheck, 10);270return true;271} catch (NumberFormatException ignored) {272return false;273}274}275}276277278