44 import java.awt.event.*;
45 import java.applet.Applet;
48 @SuppressWarnings(
"serial")
49 class GraphicsPanel extends Panel {
53 public GraphicsCards cards;
55 GraphicsPanel(EventListener listener) {
56 al = (ActionListener) listener;
57 il = (ItemListener) listener;
59 setLayout(
new BorderLayout());
61 add(
"Center", cards =
new GraphicsCards());
63 Panel p =
new Panel();
66 Button b =
new Button(
"next");
67 b.addActionListener(al);
70 b =
new Button(
"previous");
71 b.addActionListener(al);
74 p.add(
new Label(
"go to:", Label.RIGHT));
76 Choice c =
new Choice();
77 c.addItemListener(il);
84 c.addItem(
"RoundRect");
92 public Dimension getPreferredSize() {
93 return new Dimension(200, 100);
98 @SuppressWarnings(
"serial")
100 implements ActionListener, ItemListener {
102 GraphicsPanel mainPanel;
106 setLayout(
new BorderLayout());
107 add(
"Center", mainPanel =
new GraphicsPanel(
this));
117 String arg = e.getActionCommand();
119 if (
"next".equals(arg)) {
120 ((CardLayout) mainPanel.cards.getLayout()).next(mainPanel.cards);
121 }
else if (
"previous".equals(arg)) {
122 ((CardLayout) mainPanel.cards.getLayout()).previous(mainPanel.cards);
128 ((CardLayout) mainPanel.cards.getLayout()).show(mainPanel.cards,
129 (String) e.getItem());
132 public static void main(String args[]) {
133 AppletFrame.startApplet(
"GraphicsTest",
"Graphics Test", args);
138 return "An interactive demonstration of some graphics.";
143 @SuppressWarnings(
"serial")
144 class GraphicsCards extends Panel {
146 public GraphicsCards() {
147 setLayout(
new CardLayout());
148 add(
"Arc",
new ArcCard());
149 add(
"Oval",
new ShapeTest(
new OvalShape()));
150 add(
"Polygon",
new ShapeTest(
new PolygonShape()));
151 add(
"Rect",
new ShapeTest(
new RectShape()));
152 add(
"RoundRect",
new ShapeTest(
new RoundRectShape()));
157 @SuppressWarnings(
"serial")
158 class ArcCard extends Panel {
161 setLayout(
new GridLayout(0, 2));
162 add(
new ArcPanel(
true));
163 add(
new ArcPanel(
false));
164 add(
new ArcDegreePanel(
true));
165 add(
new ArcDegreePanel(
false));
170 @SuppressWarnings(
"serial")
171 class ArcDegreePanel extends Panel {
175 public ArcDegreePanel(
boolean filled) {
176 this.filled = filled;
179 void arcSteps(Graphics g,
191 for (; (a1 + a2) <= 360; a1 = a1 + a2, a2 += 1) {
192 if (g.getColor() == c1) {
199 g.fillArc(x, y, w, h, a1, a2);
201 g.drawArc(x, y, w, h, a1, a2);
207 if (progress != 360) {
209 g.fillArc(x, y, w, h, a1, 360 - progress);
211 g.drawArc(x, y, w, h, a1, 360 - progress);
217 public void paint(Graphics g) {
218 Rectangle r = getBounds();
220 arcSteps(g, 3, 0, 0, r.width, r.height, Color.orange, Color.blue);
244 @SuppressWarnings(
"serial")
245 class ArcPanel extends Panel {
249 public ArcPanel(
boolean filled) {
250 this.filled = filled;
254 public void paint(Graphics g) {
255 Rectangle r = getBounds();
257 g.setColor(Color.yellow);
259 g.fillArc(0, 0, r.width, r.height, 0, 45);
261 g.drawArc(0, 0, r.width, r.height, 0, 45);
264 g.setColor(Color.green);
266 g.fillArc(0, 0, r.width, r.height, 90, -45);
268 g.drawArc(0, 0, r.width, r.height, 90, -45);
271 g.setColor(Color.orange);
273 g.fillArc(0, 0, r.width, r.height, 135, -45);
275 g.drawArc(0, 0, r.width, r.height, 135, -45);
278 g.setColor(Color.magenta);
281 g.fillArc(0, 0, r.width, r.height, -225, 45);
283 g.drawArc(0, 0, r.width, r.height, -225, 45);
286 g.setColor(Color.yellow);
288 g.fillArc(0, 0, r.width, r.height, 225, -45);
290 g.drawArc(0, 0, r.width, r.height, 225, -45);
293 g.setColor(Color.green);
295 g.fillArc(0, 0, r.width, r.height, -135, 45);
297 g.drawArc(0, 0, r.width, r.height, -135, 45);
300 g.setColor(Color.orange);
302 g.fillArc(0, 0, r.width, r.height, -45, -45);
304 g.drawArc(0, 0, r.width, r.height, -45, -45);
307 g.setColor(Color.magenta);
309 g.fillArc(0, 0, r.width, r.height, 315, 45);
311 g.drawArc(0, 0, r.width, r.height, 315, 45);
318 abstract class Shape {
320 abstract void draw(Graphics g,
int x,
int y,
int w,
int h);
322 abstract void fill(Graphics g,
int x,
int y,
int w,
int h);
326 class RectShape
extends Shape {
329 void draw(Graphics g,
int x,
int y,
int w,
int h) {
330 g.drawRect(x, y, w, h);
334 void fill(Graphics g,
int x,
int y,
int w,
int h) {
335 g.fillRect(x, y, w, h);
340 class OvalShape
extends Shape {
343 void draw(Graphics g,
int x,
int y,
int w,
int h) {
344 g.drawOval(x, y, w, h);
348 void fill(Graphics g,
int x,
int y,
int w,
int h) {
349 g.fillOval(x, y, w, h);
354 class RoundRectShape
extends Shape {
357 void draw(Graphics g,
int x,
int y,
int w,
int h) {
358 g.drawRoundRect(x, y, w, h, 10, 10);
362 void fill(Graphics g,
int x,
int y,
int w,
int h) {
363 g.fillRoundRect(x, y, w, h, 10, 10);
368 class PolygonShape
extends Shape {
374 public PolygonShape() {
375 pBase =
new Polygon();
376 pBase.addPoint(0, 0);
377 pBase.addPoint(10, 0);
378 pBase.addPoint(5, 15);
379 pBase.addPoint(10, 20);
380 pBase.addPoint(5, 20);
381 pBase.addPoint(0, 10);
382 pBase.addPoint(0, 0);
385 void scalePolygon(
float w,
float h) {
387 for (
int i = 0; i < pBase.npoints; ++i) {
388 p.addPoint((
int) (pBase.xpoints[i] * w),
389 (
int) (pBase.ypoints[i] * h));
395 void draw(Graphics g,
int x,
int y,
int w,
int h) {
396 Graphics ng = g.create();
399 scalePolygon(((
float) w / 10f), ((
float) h / 20f));
407 void fill(Graphics g,
int x,
int y,
int w,
int h) {
408 Graphics ng = g.create();
411 scalePolygon(((
float) w / 10f), ((
float) h / 20f));
420 @SuppressWarnings(
"serial")
421 class ShapeTest extends Panel {
426 public ShapeTest(Shape shape,
int step) {
431 public ShapeTest(Shape shape) {
436 public void paint(Graphics g) {
437 Rectangle bounds = getBounds();
443 for (color = Color.red, cx = bounds.x, cy = bounds.y,
444 cw = bounds.width / 2, ch = bounds.height;
446 cx += step, cy += step, cw -= (step * 2), ch -= (step * 2),
447 color = ColorUtils.darker(color, 0.9)) {
449 shape.draw(g, cx, cy, cw, ch);
452 for (cx = bounds.x + bounds.width / 2, cy = bounds.y,
453 cw = bounds.width / 2, ch = bounds.height;
455 cx += step, cy += step, cw -= (step * 2), ch -= (step * 2)) {
456 if (g.getColor() == Color.red) {
457 g.setColor(Color.blue);
459 g.setColor(Color.red);
462 shape.fill(g, cx, cy, cw, ch);
470 static Color brighter(Color c,
double factor) {
471 return new Color(Math.min((
int) (c.getRed() * (1 / factor)), 255),
472 Math.min((
int) (c.getGreen() * (1 / factor)), 255),
473 Math.min((
int) (c.getBlue() * (1 / factor)), 255));
476 static Color darker(Color c,
double factor) {
477 return new Color(Math.max((
int) (c.getRed() * factor), 0),
478 Math.max((
int) (c.getGreen() * factor), 0),
479 Math.max((
int) (c.getBlue() * factor), 0));