jdk/src/demo/share/applets/ArcTest/ArcTest.java
author prr
Thu, 18 Dec 2014 10:45:45 -0800
changeset 29908 83e2c403fefd
parent 25859 3317bb8137f4
permissions -rw-r--r--
8067050: Better font consistency checking Reviewed-by: bae, srl, mschoene
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
8974
765c75b834ef 7027673: /applets/ArcTest demo needs to be improved
mrkam
parents: 5506
diff changeset
     2
 * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 * Redistribution and use in source and binary forms, with or without
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * modification, are permitted provided that the following conditions
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * are met:
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *   - Redistributions of source code must retain the above copyright
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 *     notice, this list of conditions and the following disclaimer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 *   - Redistributions in binary form must reproduce the above copyright
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 *     notice, this list of conditions and the following disclaimer in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 *     documentation and/or other materials provided with the distribution.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    15
 *   - Neither the name of Oracle nor the names of its
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *     contributors may be used to endorse or promote products derived
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 *     from this software without specific prior written permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
10292
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 8974
diff changeset
    32
/*
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 8974
diff changeset
    33
 * This source code is provided to illustrate the usage of a given feature
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 8974
diff changeset
    34
 * or technique and has been deliberately simplified. Additional steps
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 8974
diff changeset
    35
 * required for a production-quality application, such as security checks,
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 8974
diff changeset
    36
 * input validation and proper error handling, might not be present in
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 8974
diff changeset
    37
 * this sample code.
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 8974
diff changeset
    38
 */
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 8974
diff changeset
    39
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 8974
diff changeset
    40
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.applet.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
8974
765c75b834ef 7027673: /applets/ArcTest demo needs to be improved
mrkam
parents: 5506
diff changeset
    46
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * An interactive test of the Graphics.drawArc and Graphics.fillArc
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * routines. Can be run either as a standalone application by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * typing "java ArcTest" or as an applet in the AppletViewer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 */
8974
765c75b834ef 7027673: /applets/ArcTest demo needs to be improved
mrkam
parents: 5506
diff changeset
    52
@SuppressWarnings("serial")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
public class ArcTest extends Applet {
8974
765c75b834ef 7027673: /applets/ArcTest demo needs to be improved
mrkam
parents: 5506
diff changeset
    54
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    ArcControls controls;   // The controls for marking and filling arcs
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    ArcCanvas canvas;       // The drawing area to display arcs
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
8974
765c75b834ef 7027673: /applets/ArcTest demo needs to be improved
mrkam
parents: 5506
diff changeset
    58
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    public void init() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        setLayout(new BorderLayout());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        canvas = new ArcCanvas();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        add("Center", canvas);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        add("South", controls = new ArcControls(canvas));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
8974
765c75b834ef 7027673: /applets/ArcTest demo needs to be improved
mrkam
parents: 5506
diff changeset
    66
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    public void destroy() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        remove(controls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        remove(canvas);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
8974
765c75b834ef 7027673: /applets/ArcTest demo needs to be improved
mrkam
parents: 5506
diff changeset
    72
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    public void start() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        controls.setEnabled(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
8974
765c75b834ef 7027673: /applets/ArcTest demo needs to be improved
mrkam
parents: 5506
diff changeset
    77
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    public void stop() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        controls.setEnabled(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
8974
765c75b834ef 7027673: /applets/ArcTest demo needs to be improved
mrkam
parents: 5506
diff changeset
    82
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    public void processEvent(AWTEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        if (e.getID() == Event.WINDOW_DESTROY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            System.exit(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    public static void main(String args[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        Frame f = new Frame("ArcTest");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        ArcTest arcTest = new ArcTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        arcTest.init();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        arcTest.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        f.add("Center", arcTest);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        f.setSize(300, 300);
8974
765c75b834ef 7027673: /applets/ArcTest demo needs to be improved
mrkam
parents: 5506
diff changeset
    98
        f.setVisible(true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
8974
765c75b834ef 7027673: /applets/ArcTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   101
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    public String getAppletInfo() {
8974
765c75b834ef 7027673: /applets/ArcTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   103
        return "An interactive test of the Graphics.drawArc and \nGraphics."
765c75b834ef 7027673: /applets/ArcTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   104
                + "fillArc routines. Can be run \neither as a standalone "
765c75b834ef 7027673: /applets/ArcTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   105
                + "application by typing 'java ArcTest' \nor as an applet in "
765c75b834ef 7027673: /applets/ArcTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   106
                + "the AppletViewer.";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
8974
765c75b834ef 7027673: /applets/ArcTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   110
765c75b834ef 7027673: /applets/ArcTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   111
@SuppressWarnings("serial")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
class ArcCanvas extends Canvas {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
8974
765c75b834ef 7027673: /applets/ArcTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   114
    int startAngle = 0;
765c75b834ef 7027673: /applets/ArcTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   115
    int extent = 45;
765c75b834ef 7027673: /applets/ArcTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   116
    boolean filled = false;
765c75b834ef 7027673: /applets/ArcTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   117
    Font font = new java.awt.Font("SansSerif", Font.PLAIN, 12);
765c75b834ef 7027673: /applets/ArcTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   118
765c75b834ef 7027673: /applets/ArcTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   119
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    public void paint(Graphics g) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        Rectangle r = getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        int hlines = r.height / 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        int vlines = r.width / 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        g.setColor(Color.pink);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        for (int i = 1; i <= hlines; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            g.drawLine(0, i * 10, r.width, i * 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        for (int i = 1; i <= vlines; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            g.drawLine(i * 10, 0, i * 10, r.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        g.setColor(Color.red);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        if (filled) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            g.fillArc(0, 0, r.width - 1, r.height - 1, startAngle, extent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            g.drawArc(0, 0, r.width - 1, r.height - 1, startAngle, extent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        g.setColor(Color.black);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        g.setFont(font);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        g.drawLine(0, r.height / 2, r.width, r.height / 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        g.drawLine(r.width / 2, 0, r.width / 2, r.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        g.drawLine(0, 0, r.width, r.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        g.drawLine(r.width, 0, 0, r.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        int sx = 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        int sy = r.height - 28;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        g.drawString("Start = " + startAngle, sx, sy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        g.drawString("Extent = " + extent, sx, sy + 14);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    public void redraw(boolean filled, int start, int extent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        this.filled = filled;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        this.startAngle = start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        this.extent = extent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
8974
765c75b834ef 7027673: /applets/ArcTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   160
765c75b834ef 7027673: /applets/ArcTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   161
@SuppressWarnings("serial")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
class ArcControls extends Panel
8974
765c75b834ef 7027673: /applets/ArcTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   163
        implements ActionListener {
765c75b834ef 7027673: /applets/ArcTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   164
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    TextField startTF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    TextField extentTF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    ArcCanvas canvas;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
8974
765c75b834ef 7027673: /applets/ArcTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   169
    @SuppressWarnings("LeakingThisInConstructor")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    public ArcControls(ArcCanvas canvas) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        Button b = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        this.canvas = canvas;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        add(startTF = new IntegerTextField("0", 4));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        add(extentTF = new IntegerTextField("45", 4));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        b = new Button("Fill");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        b.addActionListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        add(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        b = new Button("Draw");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        b.addActionListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        add(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
8974
765c75b834ef 7027673: /applets/ArcTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   184
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    public void actionPerformed(ActionEvent ev) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        String label = ev.getActionCommand();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        int start, extent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            start = Integer.parseInt(startTF.getText().trim());
8974
765c75b834ef 7027673: /applets/ArcTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   191
        } catch (NumberFormatException ignored) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            start = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            extent = Integer.parseInt(extentTF.getText().trim());
8974
765c75b834ef 7027673: /applets/ArcTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   196
        } catch (NumberFormatException ignored) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            extent = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        canvas.redraw(label.equals("Fill"), start, extent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
8974
765c75b834ef 7027673: /applets/ArcTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   204
765c75b834ef 7027673: /applets/ArcTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   205
@SuppressWarnings("serial")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
class IntegerTextField extends TextField {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    String oldText = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    public IntegerTextField(String text, int columns) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        super(text, columns);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        enableEvents(AWTEvent.KEY_EVENT_MASK | AWTEvent.TEXT_EVENT_MASK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        oldText = getText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    // Consume non-digit KeyTyped events
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    // Note that processTextEvent kind of eliminates the need for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    // function, but this is neater, since ideally, it would prevent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    // the text from appearing at all.  Sigh.  See bugid 4100317/4114565.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    //
8974
765c75b834ef 7027673: /applets/ArcTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   221
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    protected void processEvent(AWTEvent evt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        int id = evt.getID();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        if (id != KeyEvent.KEY_TYPED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            super.processEvent(evt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        KeyEvent kevt = (KeyEvent) evt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        char c = kevt.getKeyChar();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        // Digits, backspace, and delete are okay
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        // Note that the minus sign is allowed, but not the decimal
8974
765c75b834ef 7027673: /applets/ArcTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   234
        if (Character.isDigit(c) || (c == '\b') || (c == '\u007f') || (c
765c75b834ef 7027673: /applets/ArcTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   235
                == '\u002d')) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            super.processEvent(evt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        Toolkit.getDefaultToolkit().beep();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        kevt.consume();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    // Should consume TextEvents for non-integer Strings
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    // Store away the text in the tf for every TextEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    // so we can revert to it on a TextEvent (paste, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    // legal key in the wrong location) with bad text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    //
8974
765c75b834ef 7027673: /applets/ArcTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   249
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    protected void processTextEvent(TextEvent te) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        // The empty string is okay, too
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        String newText = getText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        if (newText.equals("") || textIsInteger(newText)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            oldText = newText;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            super.processTextEvent(te);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        Toolkit.getDefaultToolkit().beep();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        setText(oldText);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    // Returns true for Integers (zero and negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    // values are allowed).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    // Note that the empty string is not allowed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    private boolean textIsInteger(String textToCheck) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        try {
8974
765c75b834ef 7027673: /applets/ArcTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   270
            Integer.parseInt(textToCheck, 10);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            return true;
8974
765c75b834ef 7027673: /applets/ArcTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   272
        } catch (NumberFormatException ignored) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
}