src/demo/share/applets/DrawTest/DrawTest.java
author stuefe
Thu, 10 May 2018 07:26:18 +0200
changeset 50074 26ac622a4cab
parent 47216 71c04702a3d5
permissions -rw-r--r--
8202822: Add .git to .hgignore Reviewed-by: erikj
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
8962
8833da0e7bc9 7027680: /applets/DrawTest 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: 8962
diff changeset
    32
/*
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 8962
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: 8962
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: 8962
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: 8962
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: 8962
diff changeset
    37
 * this sample code.
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 8962
diff changeset
    38
 */
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 8962
diff changeset
    39
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 8962
diff changeset
    40
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
8962
8833da0e7bc9 7027680: /applets/DrawTest demo needs to be improved
mrkam
parents: 5506
diff changeset
    42
import java.applet.Applet;
8833da0e7bc9 7027680: /applets/DrawTest demo needs to be improved
mrkam
parents: 5506
diff changeset
    43
import java.awt.BorderLayout;
8833da0e7bc9 7027680: /applets/DrawTest demo needs to be improved
mrkam
parents: 5506
diff changeset
    44
import java.awt.Checkbox;
8833da0e7bc9 7027680: /applets/DrawTest demo needs to be improved
mrkam
parents: 5506
diff changeset
    45
import java.awt.CheckboxGroup;
8833da0e7bc9 7027680: /applets/DrawTest demo needs to be improved
mrkam
parents: 5506
diff changeset
    46
import java.awt.Choice;
8833da0e7bc9 7027680: /applets/DrawTest demo needs to be improved
mrkam
parents: 5506
diff changeset
    47
import java.awt.Color;
8833da0e7bc9 7027680: /applets/DrawTest demo needs to be improved
mrkam
parents: 5506
diff changeset
    48
import java.awt.Component;
8833da0e7bc9 7027680: /applets/DrawTest demo needs to be improved
mrkam
parents: 5506
diff changeset
    49
import java.awt.Dimension;
8833da0e7bc9 7027680: /applets/DrawTest demo needs to be improved
mrkam
parents: 5506
diff changeset
    50
import java.awt.FlowLayout;
8833da0e7bc9 7027680: /applets/DrawTest demo needs to be improved
mrkam
parents: 5506
diff changeset
    51
import java.awt.Frame;
8833da0e7bc9 7027680: /applets/DrawTest demo needs to be improved
mrkam
parents: 5506
diff changeset
    52
import java.awt.Graphics;
8833da0e7bc9 7027680: /applets/DrawTest demo needs to be improved
mrkam
parents: 5506
diff changeset
    53
import java.awt.Panel;
8833da0e7bc9 7027680: /applets/DrawTest demo needs to be improved
mrkam
parents: 5506
diff changeset
    54
import java.awt.Point;
8833da0e7bc9 7027680: /applets/DrawTest demo needs to be improved
mrkam
parents: 5506
diff changeset
    55
import java.awt.Rectangle;
8833da0e7bc9 7027680: /applets/DrawTest demo needs to be improved
mrkam
parents: 5506
diff changeset
    56
import java.awt.event.ItemEvent;
8833da0e7bc9 7027680: /applets/DrawTest demo needs to be improved
mrkam
parents: 5506
diff changeset
    57
import java.awt.event.ItemListener;
8833da0e7bc9 7027680: /applets/DrawTest demo needs to be improved
mrkam
parents: 5506
diff changeset
    58
import java.awt.event.MouseEvent;
8833da0e7bc9 7027680: /applets/DrawTest demo needs to be improved
mrkam
parents: 5506
diff changeset
    59
import java.awt.event.MouseListener;
8833da0e7bc9 7027680: /applets/DrawTest demo needs to be improved
mrkam
parents: 5506
diff changeset
    60
import java.awt.event.MouseMotionListener;
8833da0e7bc9 7027680: /applets/DrawTest demo needs to be improved
mrkam
parents: 5506
diff changeset
    61
import java.util.ArrayList;
8833da0e7bc9 7027680: /applets/DrawTest demo needs to be improved
mrkam
parents: 5506
diff changeset
    62
import java.util.List;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
8962
8833da0e7bc9 7027680: /applets/DrawTest demo needs to be improved
mrkam
parents: 5506
diff changeset
    65
@SuppressWarnings("serial")
8833da0e7bc9 7027680: /applets/DrawTest demo needs to be improved
mrkam
parents: 5506
diff changeset
    66
public class DrawTest extends Applet {
8833da0e7bc9 7027680: /applets/DrawTest demo needs to be improved
mrkam
parents: 5506
diff changeset
    67
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    DrawPanel panel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    DrawControls controls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
8962
8833da0e7bc9 7027680: /applets/DrawTest demo needs to be improved
mrkam
parents: 5506
diff changeset
    71
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    public void init() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        setLayout(new BorderLayout());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        panel = new DrawPanel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        controls = new DrawControls(panel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        add("Center", panel);
8962
8833da0e7bc9 7027680: /applets/DrawTest demo needs to be improved
mrkam
parents: 5506
diff changeset
    77
        add("South", controls);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
8962
8833da0e7bc9 7027680: /applets/DrawTest demo needs to be improved
mrkam
parents: 5506
diff changeset
    80
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    public void destroy() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        remove(panel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        remove(controls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    public static void main(String args[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        Frame f = new Frame("DrawTest");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        DrawTest drawTest = new DrawTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        drawTest.init();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        drawTest.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        f.add("Center", drawTest);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        f.setSize(300, 300);
8962
8833da0e7bc9 7027680: /applets/DrawTest demo needs to be improved
mrkam
parents: 5506
diff changeset
    94
        f.setVisible(true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
8962
8833da0e7bc9 7027680: /applets/DrawTest demo needs to be improved
mrkam
parents: 5506
diff changeset
    96
8833da0e7bc9 7027680: /applets/DrawTest demo needs to be improved
mrkam
parents: 5506
diff changeset
    97
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    public String getAppletInfo() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        return "A simple drawing program.";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
8962
8833da0e7bc9 7027680: /applets/DrawTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   103
8833da0e7bc9 7027680: /applets/DrawTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   104
@SuppressWarnings("serial")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
class DrawPanel extends Panel implements MouseListener, MouseMotionListener {
8962
8833da0e7bc9 7027680: /applets/DrawTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   106
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    public static final int LINES = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    public static final int POINTS = 1;
8962
8833da0e7bc9 7027680: /applets/DrawTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   109
    int mode = LINES;
8833da0e7bc9 7027680: /applets/DrawTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   110
    List<Rectangle> lines = new ArrayList<Rectangle>();
8833da0e7bc9 7027680: /applets/DrawTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   111
    List<Color> colors = new ArrayList<Color>();
8833da0e7bc9 7027680: /applets/DrawTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   112
    int x1, y1;
8833da0e7bc9 7027680: /applets/DrawTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   113
    int x2, y2;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
8962
8833da0e7bc9 7027680: /applets/DrawTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   115
    @SuppressWarnings("LeakingThisInConstructor")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    public DrawPanel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        setBackground(Color.white);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        addMouseMotionListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        addMouseListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    public void setDrawMode(int mode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        switch (mode) {
8962
8833da0e7bc9 7027680: /applets/DrawTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   124
            case LINES:
8833da0e7bc9 7027680: /applets/DrawTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   125
            case POINTS:
8833da0e7bc9 7027680: /applets/DrawTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   126
                this.mode = mode;
8833da0e7bc9 7027680: /applets/DrawTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   127
                break;
8833da0e7bc9 7027680: /applets/DrawTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   128
            default:
8833da0e7bc9 7027680: /applets/DrawTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   129
                throw new IllegalArgumentException();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
8962
8833da0e7bc9 7027680: /applets/DrawTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   133
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    public void mouseDragged(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        e.consume();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        switch (mode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            case LINES:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                x2 = e.getX();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                y2 = e.getY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            case POINTS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            default:
8962
8833da0e7bc9 7027680: /applets/DrawTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   143
                colors.add(getForeground());
8833da0e7bc9 7027680: /applets/DrawTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   144
                lines.add(new Rectangle(x1, y1, e.getX(), e.getY()));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                x1 = e.getX();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                y1 = e.getY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
8962
8833da0e7bc9 7027680: /applets/DrawTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   152
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    public void mouseMoved(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
8962
8833da0e7bc9 7027680: /applets/DrawTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   156
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    public void mousePressed(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        e.consume();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        switch (mode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            case LINES:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                x1 = e.getX();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                y1 = e.getY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                x2 = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            case POINTS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            default:
8962
8833da0e7bc9 7027680: /applets/DrawTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   167
                colors.add(getForeground());
8833da0e7bc9 7027680: /applets/DrawTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   168
                lines.add(new Rectangle(e.getX(), e.getY(), -1, -1));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                x1 = e.getX();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                y1 = e.getY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
8962
8833da0e7bc9 7027680: /applets/DrawTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   176
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    public void mouseReleased(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        e.consume();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        switch (mode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            case LINES:
8962
8833da0e7bc9 7027680: /applets/DrawTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   181
                colors.add(getForeground());
8833da0e7bc9 7027680: /applets/DrawTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   182
                lines.add(new Rectangle(x1, y1, e.getX(), e.getY()));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                x2 = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            case POINTS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
8962
8833da0e7bc9 7027680: /applets/DrawTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   192
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    public void mouseEntered(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
8962
8833da0e7bc9 7027680: /applets/DrawTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   196
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    public void mouseExited(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
8962
8833da0e7bc9 7027680: /applets/DrawTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   200
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    public void mouseClicked(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
8962
8833da0e7bc9 7027680: /applets/DrawTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   204
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    public void paint(Graphics g) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        int np = lines.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        /* draw the current lines */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        g.setColor(getForeground());
8962
8833da0e7bc9 7027680: /applets/DrawTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   210
        for (int i = 0; i < np; i++) {
8833da0e7bc9 7027680: /applets/DrawTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   211
            Rectangle p = lines.get(i);
8833da0e7bc9 7027680: /applets/DrawTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   212
            g.setColor(colors.get(i));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            if (p.width != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                g.drawLine(p.x, p.y, p.width, p.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                g.drawLine(p.x, p.y, p.x, p.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        if (mode == LINES) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            g.setColor(getForeground());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            if (x2 != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                g.drawLine(x1, y1, x2, y2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
8962
8833da0e7bc9 7027680: /applets/DrawTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   229
@SuppressWarnings("serial")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
class DrawControls extends Panel implements ItemListener {
8962
8833da0e7bc9 7027680: /applets/DrawTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   231
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    DrawPanel target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
8962
8833da0e7bc9 7027680: /applets/DrawTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   234
    @SuppressWarnings("LeakingThisInConstructor")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    public DrawControls(DrawPanel target) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        this.target = target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        setLayout(new FlowLayout());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        setBackground(Color.lightGray);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        target.setForeground(Color.red);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        CheckboxGroup group = new CheckboxGroup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        Checkbox b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        add(b = new Checkbox(null, group, false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        b.addItemListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        b.setForeground(Color.red);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        add(b = new Checkbox(null, group, false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        b.addItemListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        b.setForeground(Color.green);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        add(b = new Checkbox(null, group, false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        b.addItemListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        b.setForeground(Color.blue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        add(b = new Checkbox(null, group, false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        b.addItemListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        b.setForeground(Color.pink);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        add(b = new Checkbox(null, group, false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        b.addItemListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        b.setForeground(Color.orange);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        add(b = new Checkbox(null, group, true));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        b.addItemListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        b.setForeground(Color.black);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        target.setForeground(b.getForeground());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        Choice shapes = new Choice();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        shapes.addItemListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        shapes.addItem("Lines");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        shapes.addItem("Points");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        shapes.setBackground(Color.lightGray);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        add(shapes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
8962
8833da0e7bc9 7027680: /applets/DrawTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   269
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    public void paint(Graphics g) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        Rectangle r = getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        g.setColor(Color.lightGray);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        g.draw3DRect(0, 0, r.width, r.height, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        int n = getComponentCount();
8962
8833da0e7bc9 7027680: /applets/DrawTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   276
        for (int i = 0; i < n; i++) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            Component comp = getComponent(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            if (comp instanceof Checkbox) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                Point loc = comp.getLocation();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                Dimension d = comp.getSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                g.setColor(comp.getForeground());
8962
8833da0e7bc9 7027680: /applets/DrawTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   282
                g.drawRect(loc.x - 1, loc.y - 1, d.width + 1, d.height + 1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
8962
8833da0e7bc9 7027680: /applets/DrawTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   287
    @Override
8833da0e7bc9 7027680: /applets/DrawTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   288
    public void itemStateChanged(ItemEvent e) {
8833da0e7bc9 7027680: /applets/DrawTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   289
        if (e.getSource() instanceof Checkbox) {
8833da0e7bc9 7027680: /applets/DrawTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   290
            target.setForeground(((Component) e.getSource()).getForeground());
8833da0e7bc9 7027680: /applets/DrawTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   291
        } else if (e.getSource() instanceof Choice) {
8833da0e7bc9 7027680: /applets/DrawTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   292
            String choice = (String) e.getItem();
8833da0e7bc9 7027680: /applets/DrawTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   293
            if (choice.equals("Lines")) {
8833da0e7bc9 7027680: /applets/DrawTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   294
                target.setDrawMode(DrawPanel.LINES);
8833da0e7bc9 7027680: /applets/DrawTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   295
            } else if (choice.equals("Points")) {
8833da0e7bc9 7027680: /applets/DrawTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   296
                target.setDrawMode(DrawPanel.POINTS);
8833da0e7bc9 7027680: /applets/DrawTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   297
            }
8833da0e7bc9 7027680: /applets/DrawTest demo needs to be improved
mrkam
parents: 5506
diff changeset
   298
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
}