jdk/src/share/demo/applets/GraphicsTest/GraphicsTest.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1997-2006 Sun Microsystems, Inc.  All Rights Reserved.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 *   - Neither the name of Sun Microsystems nor the names of its
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.applet.Applet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
class GraphicsPanel extends Panel {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    ActionListener al;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    ItemListener il;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    public GraphicsCards cards;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
     GraphicsPanel(EventListener listener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
         al = (ActionListener)listener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
         il = (ItemListener)listener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        setLayout(new BorderLayout());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        add("Center", cards = new GraphicsCards());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        Panel p = new Panel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        //p.setLayout(new BorderLayout());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        Button b = new Button("next");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        b.addActionListener(al);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        p.add(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        b = new Button("previous");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        b.addActionListener(al);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        p.add(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        p.add(new Label("go to:", Label.RIGHT));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        Choice c = new Choice();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        c.addItemListener(il);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        p.add(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        c.addItem("Arc");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        c.addItem("Oval");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        c.addItem("Polygon");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        c.addItem("Rect");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        c.addItem("RoundRect");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        add("North", p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        setSize(400, 400);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    public Dimension getPreferredSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        return new Dimension(200, 100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    }
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 class GraphicsTest extends Applet
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
implements ActionListener, ItemListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    GraphicsPanel mainPanel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    public void init() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        setLayout(new BorderLayout());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        add("Center", mainPanel = new GraphicsPanel(this));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    public void destroy() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        remove(mainPanel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        String arg = e.getActionCommand();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        if ("next".equals(arg)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            ((CardLayout)mainPanel.cards.getLayout()).next(mainPanel.cards);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        else if ("previous".equals(arg)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            ((CardLayout)mainPanel.cards.getLayout()).previous(mainPanel.cards);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    public void itemStateChanged(ItemEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        ((CardLayout)mainPanel.cards.getLayout()).show(mainPanel.cards,(String)e.getItem());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    public static void main(String args[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        AppletFrame.startApplet("GraphicsTest", "Graphics Test", args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    public String getAppletInfo() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        return "An interactive demonstration of some graphics.";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
}   // end class GraphicsTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
class GraphicsCards extends Panel {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    public GraphicsCards() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        setLayout(new CardLayout());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        add("Arc", new ArcCard());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        add("Oval", new ShapeTest( new OvalShape() ) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        add("Polygon", new ShapeTest( new PolygonShape() ) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        add("Rect", new ShapeTest( new RectShape() ) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        add("RoundRect", new ShapeTest( new RoundRectShape() ) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
}   // end class GraphicsCards
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
class ArcCard extends Panel {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    public ArcCard() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        setLayout(new GridLayout(0, 2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        add(new ArcPanel(true));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        add(new ArcPanel(false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        add(new ArcDegreePanel(true));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        add(new ArcDegreePanel(false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
}   // end class ArcCard
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
class ArcDegreePanel extends Panel {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    boolean filled;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    public ArcDegreePanel(boolean filled) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        this.filled = filled;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    void arcSteps(Graphics g,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                  int step,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                  int x,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                  int y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                  int w,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                  int h,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                  Color c1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                  Color c2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    int a1 = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    int a2 = step;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    int progress = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    g.setColor(c1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    for (; (a1+a2) <= 360; a1 = a1+a2, a2 += 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        if (g.getColor() == c1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            g.setColor(c2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            g.setColor(c1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        if (filled) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            g.fillArc(x, y, w, h, a1, a2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            g.drawArc(x, y, w, h, a1, a2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        progress = a1+a2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
      }  // end for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    if (progress != 360) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
          if (filled) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            g.fillArc(x, y, w, h, a1, 360 - progress);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            g.drawArc(x, y, w, h, a1, 360 - progress);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
      }  // end if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
  }  // end arcSteps()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    public void paint(Graphics g) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        Rectangle r = getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        arcSteps(g, 3, 0, 0, r.width, r.height, Color.orange, Color.blue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        arcSteps(g,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                 2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                 r.width / 4,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                 r.height / 4,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                 r.width / 2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                 r.height / 2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                 Color.yellow,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                 Color.green);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        arcSteps(g,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                 (r.width  * 3) / 8,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                 (r.height * 3) / 8,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                 r.width / 4,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                 r.height / 4,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                 Color.magenta,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                 Color.white);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
  }  // end paint()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
}   // end class ArcDegreePanel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
class ArcPanel extends Panel {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    boolean filled;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    public ArcPanel(boolean filled) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    this.filled = filled;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
  public void paint(Graphics g)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    Rectangle r = getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    g.setColor(Color.yellow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    if (filled)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        g.fillArc(0, 0, r.width, r.height, 0, 45);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        g.drawArc(0, 0, r.width, r.height, 0, 45);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    g.setColor(Color.green);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    if (filled)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        g.fillArc(0, 0, r.width, r.height, 90, -45);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        g.drawArc(0, 0, r.width, r.height, 90, -45);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    g.setColor(Color.orange);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    if (filled)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        g.fillArc(0, 0, r.width, r.height, 135, -45);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        g.drawArc(0, 0, r.width, r.height, 135, -45);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    g.setColor(Color.magenta);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    if (filled)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        g.fillArc(0, 0, r.width, r.height, -225, 45);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        g.drawArc(0, 0, r.width, r.height, -225, 45);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    g.setColor(Color.yellow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    if (filled)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        g.fillArc(0, 0, r.width, r.height, 225, -45);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        g.drawArc(0, 0, r.width, r.height, 225, -45);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    g.setColor(Color.green);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    if (filled)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        g.fillArc(0, 0, r.width, r.height, -135, 45);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        g.drawArc(0, 0, r.width, r.height, -135, 45);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    g.setColor(Color.orange);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    if (filled)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        g.fillArc(0, 0, r.width, r.height, -45, -45);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        g.drawArc(0, 0, r.width, r.height, -45, -45);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    g.setColor(Color.magenta);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    if (filled)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        g.fillArc(0, 0, r.width, r.height, 315, 45);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        g.drawArc(0, 0, r.width, r.height, 315, 45);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
  }  // end paint()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
}   // end class ArcPanel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
abstract class Shape
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
  abstract void draw(Graphics g, int x, int y, int w, int h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
  abstract void fill(Graphics g, int x, int y, int w, int h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
class RectShape extends Shape
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
  void draw(Graphics g, int x, int y, int w, int h)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    g.drawRect(x, y, w, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
  void fill(Graphics g, int x, int y, int w, int h)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    g.fillRect(x, y, w, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
class OvalShape extends Shape
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
  void draw(Graphics g, int x, int y, int w, int h)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    g.drawOval(x, y, w, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
  void fill(Graphics g, int x, int y, int w, int h)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    g.fillOval(x, y, w, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
class RoundRectShape extends Shape
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
  void draw(Graphics g, int x, int y, int w, int h)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    g.drawRoundRect(x, y, w, h, 10, 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
  void fill(Graphics g, int x, int y, int w, int h)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    g.fillRoundRect(x, y, w, h, 10, 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
class PolygonShape extends Shape
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
  // class variables
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
  Polygon p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
  Polygon pBase;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
  public PolygonShape()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    pBase = new Polygon();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    pBase.addPoint(0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    pBase.addPoint(10, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    pBase.addPoint(5, 15);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    pBase.addPoint(10, 20);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    pBase.addPoint(5, 20);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    pBase.addPoint(0, 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    pBase.addPoint(0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
  void scalePolygon(float w, float h)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    p = new Polygon();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    for (int i = 0; i < pBase.npoints; ++i)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        p.addPoint( (int) (pBase.xpoints[i] * w),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                    (int) (pBase.ypoints[i] * h) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
  void draw(Graphics g, int x, int y, int w, int h)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    Graphics ng = g.create();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        ng.translate(x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        scalePolygon( (float) ( (float) w / (float) 10 ),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                      (float) ( (float) h / (float) 20 ) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        ng.drawPolygon(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        ng.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
  void fill(Graphics g, int x, int y, int w, int h)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    Graphics ng = g.create();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        ng.translate(x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        scalePolygon( (float) ( (float) w / (float) 10 ),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                      (float) ( (float) h / (float) 20 ) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        ng.fillPolygon(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        ng.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
class ShapeTest extends Panel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
  Shape   shape;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
  int       step;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
  public ShapeTest(Shape shape, int step)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    this.shape = shape;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    this.step = step;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
  public ShapeTest(Shape shape)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
    this(shape, 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    public void paint(Graphics g) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        Rectangle bounds = getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        int cx, cy, cw, ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        Color color;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        for (color=Color.red,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                 cx=bounds.x,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                 cy=bounds.y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                 cw=bounds.width / 2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                 ch=bounds.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
             cw > 0 && ch > 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
             cx+=step,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                 cy += step,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                 cw -= (step * 2),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                 ch -= (step * 2),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                 color=ColorUtils.darker(color, 0.9) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            g.setColor(color);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            shape.draw(g, cx, cy, cw, ch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        for (cx=bounds.x + bounds.width / 2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                 cy=bounds.y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                 cw=bounds.width / 2, ch=bounds.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
             cw > 0 && ch > 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
             cx+=step,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                 cy += step,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                 cw -= (step * 2),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                 ch -= (step * 2) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            if (g.getColor() == Color.red) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                g.setColor(Color.blue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                g.setColor(Color.red);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            shape.fill(g, cx, cy, cw, ch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        }  // end for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    }  // end paint()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
}   // end class ShapeTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
class ColorUtils {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    static Color brighter(Color c, double factor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        return new Color( Math.min((int)(c.getRed()  *(1/factor)), 255),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                          Math.min((int)(c.getGreen()*(1/factor)), 255),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                          Math.min((int)(c.getBlue() *(1/factor)), 255) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    static Color darker(Color c, double factor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        return new Color( Math.max((int)(c.getRed()  *factor), 0),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                          Math.max((int)(c.getGreen()*factor), 0),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                          Math.max((int)(c.getBlue() *factor), 0) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
}