1 /* 2 * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 8 * - Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 11 * - Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * - Neither the name of Oracle nor the names of its 16 * contributors may be used to endorse or promote products derived 17 * from this software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 20 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 26 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 27 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /* 33 * This source code is provided to illustrate the usage of a given feature 34 * or technique and has been deliberately simplified. Additional steps 35 * required for a production-quality application, such as security checks, 36 * input validation and proper error handling, might not be present in 37 * this sample code. 38 */ 39 40 41 42 import java.awt.*; 43 import java.util.*; 44 import java.awt.event.*; 45 import java.applet.Applet; 46 47 48 @SuppressWarnings("serial") 49 class GraphicsPanel extends Panel { 50 51 ActionListener al; 52 ItemListener il; 53 public GraphicsCards cards; 54 GraphicsPanel(EventListener listener)55 GraphicsPanel(EventListener listener) { 56 al = (ActionListener) listener; 57 il = (ItemListener) listener; 58 59 setLayout(new BorderLayout()); 60 61 add("Center", cards = new GraphicsCards()); 62 63 Panel p = new Panel(); 64 //p.setLayout(new BorderLayout()); 65 66 Button b = new Button("next"); 67 b.addActionListener(al); 68 p.add(b); 69 70 b = new Button("previous"); 71 b.addActionListener(al); 72 p.add(b); 73 74 p.add(new Label("go to:", Label.RIGHT)); 75 76 Choice c = new Choice(); 77 c.addItemListener(il); 78 p.add(c); 79 80 c.addItem("Arc"); 81 c.addItem("Oval"); 82 c.addItem("Polygon"); 83 c.addItem("Rect"); 84 c.addItem("RoundRect"); 85 86 add("North", p); 87 88 setSize(400, 400); 89 } 90 91 @Override getPreferredSize()92 public Dimension getPreferredSize() { 93 return new Dimension(200, 100); 94 } 95 } 96 97 98 @SuppressWarnings("serial") 99 public class GraphicsTest extends Applet 100 implements ActionListener, ItemListener { 101 102 GraphicsPanel mainPanel; 103 104 @Override init()105 public void init() { 106 setLayout(new BorderLayout()); 107 add("Center", mainPanel = new GraphicsPanel(this)); 108 } 109 110 @Override destroy()111 public void destroy() { 112 remove(mainPanel); 113 } 114 115 @Override actionPerformed(ActionEvent e)116 public void actionPerformed(ActionEvent e) { 117 String arg = e.getActionCommand(); 118 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); 123 } 124 } 125 126 @Override itemStateChanged(ItemEvent e)127 public void itemStateChanged(ItemEvent e) { 128 ((CardLayout) mainPanel.cards.getLayout()).show(mainPanel.cards, 129 (String) e.getItem()); 130 } 131 main(String args[])132 public static void main(String args[]) { 133 AppletFrame.startApplet("GraphicsTest", "Graphics Test", args); 134 } 135 136 @Override getAppletInfo()137 public String getAppletInfo() { 138 return "An interactive demonstration of some graphics."; 139 } 140 } // end class GraphicsTest 141 142 143 @SuppressWarnings("serial") 144 class GraphicsCards extends Panel { 145 GraphicsCards()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())); 153 } 154 } // end class GraphicsCards 155 156 157 @SuppressWarnings("serial") 158 class ArcCard extends Panel { 159 ArcCard()160 public ArcCard() { 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)); 166 } 167 } // end class ArcCard 168 169 170 @SuppressWarnings("serial") 171 class ArcDegreePanel extends Panel { 172 173 boolean filled; 174 ArcDegreePanel(boolean filled)175 public ArcDegreePanel(boolean filled) { 176 this.filled = filled; 177 } 178 arcSteps(Graphics g, int step, int x, int y, int w, int h, Color c1, Color c2)179 void arcSteps(Graphics g, 180 int step, 181 int x, 182 int y, 183 int w, 184 int h, 185 Color c1, 186 Color c2) { 187 int a1 = 0; 188 int a2 = step; 189 int progress = 0; 190 g.setColor(c1); 191 for (; (a1 + a2) <= 360; a1 = a1 + a2, a2 += 1) { 192 if (g.getColor() == c1) { 193 g.setColor(c2); 194 } else { 195 g.setColor(c1); 196 } 197 198 if (filled) { 199 g.fillArc(x, y, w, h, a1, a2); 200 } else { 201 g.drawArc(x, y, w, h, a1, a2); 202 } 203 204 progress = a1 + a2; 205 } // end for 206 207 if (progress != 360) { 208 if (filled) { 209 g.fillArc(x, y, w, h, a1, 360 - progress); 210 } else { 211 g.drawArc(x, y, w, h, a1, 360 - progress); 212 } 213 } // end if 214 } // end arcSteps() 215 216 @Override paint(Graphics g)217 public void paint(Graphics g) { 218 Rectangle r = getBounds(); 219 220 arcSteps(g, 3, 0, 0, r.width, r.height, Color.orange, Color.blue); 221 222 arcSteps(g, 223 2, 224 r.width / 4, 225 r.height / 4, 226 r.width / 2, 227 r.height / 2, 228 Color.yellow, 229 Color.green); 230 231 arcSteps(g, 232 1, 233 (r.width * 3) / 8, 234 (r.height * 3) / 8, 235 r.width / 4, 236 r.height / 4, 237 Color.magenta, 238 Color.white); 239 240 } // end paint() 241 } // end class ArcDegreePanel 242 243 244 @SuppressWarnings("serial") 245 class ArcPanel extends Panel { 246 247 boolean filled; 248 ArcPanel(boolean filled)249 public ArcPanel(boolean filled) { 250 this.filled = filled; 251 } 252 253 @Override paint(Graphics g)254 public void paint(Graphics g) { 255 Rectangle r = getBounds(); 256 257 g.setColor(Color.yellow); 258 if (filled) { 259 g.fillArc(0, 0, r.width, r.height, 0, 45); 260 } else { 261 g.drawArc(0, 0, r.width, r.height, 0, 45); 262 } 263 264 g.setColor(Color.green); 265 if (filled) { 266 g.fillArc(0, 0, r.width, r.height, 90, -45); 267 } else { 268 g.drawArc(0, 0, r.width, r.height, 90, -45); 269 } 270 271 g.setColor(Color.orange); 272 if (filled) { 273 g.fillArc(0, 0, r.width, r.height, 135, -45); 274 } else { 275 g.drawArc(0, 0, r.width, r.height, 135, -45); 276 } 277 278 g.setColor(Color.magenta); 279 280 if (filled) { 281 g.fillArc(0, 0, r.width, r.height, -225, 45); 282 } else { 283 g.drawArc(0, 0, r.width, r.height, -225, 45); 284 } 285 286 g.setColor(Color.yellow); 287 if (filled) { 288 g.fillArc(0, 0, r.width, r.height, 225, -45); 289 } else { 290 g.drawArc(0, 0, r.width, r.height, 225, -45); 291 } 292 293 g.setColor(Color.green); 294 if (filled) { 295 g.fillArc(0, 0, r.width, r.height, -135, 45); 296 } else { 297 g.drawArc(0, 0, r.width, r.height, -135, 45); 298 } 299 300 g.setColor(Color.orange); 301 if (filled) { 302 g.fillArc(0, 0, r.width, r.height, -45, -45); 303 } else { 304 g.drawArc(0, 0, r.width, r.height, -45, -45); 305 } 306 307 g.setColor(Color.magenta); 308 if (filled) { 309 g.fillArc(0, 0, r.width, r.height, 315, 45); 310 } else { 311 g.drawArc(0, 0, r.width, r.height, 315, 45); 312 } 313 314 } // end paint() 315 } // end class ArcPanel 316 317 318 abstract class Shape { 319 draw(Graphics g, int x, int y, int w, int h)320 abstract void draw(Graphics g, int x, int y, int w, int h); 321 fill(Graphics g, int x, int y, int w, int h)322 abstract void fill(Graphics g, int x, int y, int w, int h); 323 } 324 325 326 class RectShape extends Shape { 327 328 @Override draw(Graphics g, int x, int y, int w, int h)329 void draw(Graphics g, int x, int y, int w, int h) { 330 g.drawRect(x, y, w, h); 331 } 332 333 @Override fill(Graphics g, int x, int y, int w, int h)334 void fill(Graphics g, int x, int y, int w, int h) { 335 g.fillRect(x, y, w, h); 336 } 337 } 338 339 340 class OvalShape extends Shape { 341 342 @Override draw(Graphics g, int x, int y, int w, int h)343 void draw(Graphics g, int x, int y, int w, int h) { 344 g.drawOval(x, y, w, h); 345 } 346 347 @Override fill(Graphics g, int x, int y, int w, int h)348 void fill(Graphics g, int x, int y, int w, int h) { 349 g.fillOval(x, y, w, h); 350 } 351 } 352 353 354 class RoundRectShape extends Shape { 355 356 @Override draw(Graphics g, int x, int y, int w, int h)357 void draw(Graphics g, int x, int y, int w, int h) { 358 g.drawRoundRect(x, y, w, h, 10, 10); 359 } 360 361 @Override fill(Graphics g, int x, int y, int w, int h)362 void fill(Graphics g, int x, int y, int w, int h) { 363 g.fillRoundRect(x, y, w, h, 10, 10); 364 } 365 } 366 367 368 class PolygonShape extends Shape { 369 // class variables 370 371 Polygon p; 372 Polygon pBase; 373 PolygonShape()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); 383 } 384 scalePolygon(float w, float h)385 void scalePolygon(float w, float h) { 386 p = new Polygon(); 387 for (int i = 0; i < pBase.npoints; ++i) { 388 p.addPoint((int) (pBase.xpoints[i] * w), 389 (int) (pBase.ypoints[i] * h)); 390 } 391 392 } 393 394 @Override draw(Graphics g, int x, int y, int w, int h)395 void draw(Graphics g, int x, int y, int w, int h) { 396 Graphics ng = g.create(); 397 try { 398 ng.translate(x, y); 399 scalePolygon(((float) w / 10f), ((float) h / 20f)); 400 ng.drawPolygon(p); 401 } finally { 402 ng.dispose(); 403 } 404 } 405 406 @Override fill(Graphics g, int x, int y, int w, int h)407 void fill(Graphics g, int x, int y, int w, int h) { 408 Graphics ng = g.create(); 409 try { 410 ng.translate(x, y); 411 scalePolygon(((float) w / 10f), ((float) h / 20f)); 412 ng.fillPolygon(p); 413 } finally { 414 ng.dispose(); 415 } 416 } 417 } 418 419 420 @SuppressWarnings("serial") 421 class ShapeTest extends Panel { 422 423 Shape shape; 424 int step; 425 ShapeTest(Shape shape, int step)426 public ShapeTest(Shape shape, int step) { 427 this.shape = shape; 428 this.step = step; 429 } 430 ShapeTest(Shape shape)431 public ShapeTest(Shape shape) { 432 this(shape, 10); 433 } 434 435 @Override paint(Graphics g)436 public void paint(Graphics g) { 437 Rectangle bounds = getBounds(); 438 439 int cx, cy, cw, ch; 440 441 Color color; 442 443 for (color = Color.red, cx = bounds.x, cy = bounds.y, 444 cw = bounds.width / 2, ch = bounds.height; 445 cw > 0 && ch > 0; 446 cx += step, cy += step, cw -= (step * 2), ch -= (step * 2), 447 color = ColorUtils.darker(color, 0.9)) { 448 g.setColor(color); 449 shape.draw(g, cx, cy, cw, ch); 450 } 451 452 for (cx = bounds.x + bounds.width / 2, cy = bounds.y, 453 cw = bounds.width / 2, ch = bounds.height; 454 cw > 0 && ch > 0; 455 cx += step, cy += step, cw -= (step * 2), ch -= (step * 2)) { 456 if (g.getColor() == Color.red) { 457 g.setColor(Color.blue); 458 } else { 459 g.setColor(Color.red); 460 } 461 462 shape.fill(g, cx, cy, cw, ch); 463 } // end for 464 } // end paint() 465 } // end class ShapeTest 466 467 468 class ColorUtils { 469 brighter(Color c, double factor)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)); 474 } 475 darker(Color c, double factor)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)); 480 } 481 } 482