1 36 37 40 41 import java.awt.*; 42 import java.util.*; 43 import java.awt.event.*; 44 import java.applet.Applet ; 45 46 class GraphicsPanel extends Panel { 47 ActionListener al; 48 ItemListener il; 49 public GraphicsCards cards; 50 51 GraphicsPanel(EventListener listener) { 52 al = (ActionListener)listener; 53 il = (ItemListener)listener; 54 55 setLayout(new BorderLayout()); 56 57 add("Center", cards = new GraphicsCards()); 58 59 Panel p = new Panel(); 60 62 Button b = new Button("next"); 63 b.addActionListener(al); 64 p.add(b); 65 66 b = new Button("previous"); 67 b.addActionListener(al); 68 p.add(b); 69 70 p.add(new Label("go to:", Label.RIGHT)); 71 72 Choice c = new Choice(); 73 c.addItemListener(il); 74 p.add(c); 75 76 c.addItem("Arc"); 77 c.addItem("Oval"); 78 c.addItem("Polygon"); 79 c.addItem("Rect"); 80 c.addItem("RoundRect"); 81 82 add("North", p); 83 84 setSize(400, 400); 85 } 86 87 public Dimension getPreferredSize() { 88 return new Dimension(200, 100); 89 } 90 } 91 92 public class GraphicsTest extends Applet 93 implements ActionListener, ItemListener { 94 GraphicsPanel mainPanel; 95 96 public void init() { 97 setLayout(new BorderLayout()); 98 add("Center", mainPanel = new GraphicsPanel(this)); 99 } 100 101 public void destroy() { 102 remove(mainPanel); 103 } 104 105 public void actionPerformed(ActionEvent e) { 106 String arg = e.getActionCommand(); 107 108 if ("next".equals(arg)) { 109 ((CardLayout)mainPanel.cards.getLayout()).next(mainPanel.cards); 110 } 111 else if ("previous".equals(arg)) { 112 ((CardLayout)mainPanel.cards.getLayout()).previous(mainPanel.cards); 113 } 114 } 115 116 public void itemStateChanged(ItemEvent e) { 117 ((CardLayout)mainPanel.cards.getLayout()).show(mainPanel.cards,(String )e.getItem()); 118 } 119 120 public static void main(String args[]) { 121 AppletFrame.startApplet("GraphicsTest", "Graphics Test", args); 122 } 123 124 public String getAppletInfo() { 125 return "An interactive demonstration of some graphics."; 126 } 127 } 129 130 class GraphicsCards extends Panel { 131 public GraphicsCards() { 132 setLayout(new CardLayout()); 133 add("Arc", new ArcCard()); 134 add("Oval", new ShapeTest( new OvalShape() ) ); 135 add("Polygon", new ShapeTest( new PolygonShape() ) ); 136 add("Rect", new ShapeTest( new RectShape() ) ); 137 add("RoundRect", new ShapeTest( new RoundRectShape() ) ); 138 } 139 } 141 142 class ArcCard extends Panel { 143 public ArcCard() { 144 setLayout(new GridLayout(0, 2)); 145 add(new ArcPanel(true)); 146 add(new ArcPanel(false)); 147 add(new ArcDegreePanel(true)); 148 add(new ArcDegreePanel(false)); 149 } 150 } 152 153 class ArcDegreePanel extends Panel { 154 boolean filled; 155 156 public ArcDegreePanel(boolean filled) { 157 this.filled = filled; 158 } 159 160 void arcSteps(Graphics g, 161 int step, 162 int x, 163 int y, 164 int w, 165 int h, 166 Color c1, 167 Color c2) { 168 int a1 = 0; 169 int a2 = step; 170 int progress = 0; 171 g.setColor(c1); 172 for (; (a1+a2) <= 360; a1 = a1+a2, a2 += 1) { 173 if (g.getColor() == c1) { 174 g.setColor(c2); 175 } 176 else { 177 g.setColor(c1); 178 } 179 180 if (filled) { 181 g.fillArc(x, y, w, h, a1, a2); 182 } 183 else { 184 g.drawArc(x, y, w, h, a1, a2); 185 } 186 187 progress = a1+a2; 188 } 190 if (progress != 360) { 191 if (filled) { 192 g.fillArc(x, y, w, h, a1, 360 - progress); 193 } 194 else { 195 g.drawArc(x, y, w, h, a1, 360 - progress); 196 } 197 } } 200 public void paint(Graphics g) { 201 Rectangle r = getBounds(); 202 203 arcSteps(g, 3, 0, 0, r.width, r.height, Color.orange, Color.blue); 204 205 arcSteps(g, 206 2, 207 r.width / 4, 208 r.height / 4, 209 r.width / 2, 210 r.height / 2, 211 Color.yellow, 212 Color.green); 213 214 arcSteps(g, 215 1, 216 (r.width * 3) / 8, 217 (r.height * 3) / 8, 218 r.width / 4, 219 r.height / 4, 220 Color.magenta, 221 Color.white); 222 223 } } 226 227 class ArcPanel extends Panel { 228 boolean filled; 229 230 public ArcPanel(boolean filled) { 231 this.filled = filled; 232 } 233 234 public void paint(Graphics g) 235 { 236 Rectangle r = getBounds(); 237 238 g.setColor(Color.yellow); 239 if (filled) 240 { 241 g.fillArc(0, 0, r.width, r.height, 0, 45); 242 } 243 else 244 { 245 g.drawArc(0, 0, r.width, r.height, 0, 45); 246 } 247 248 g.setColor(Color.green); 249 if (filled) 250 { 251 g.fillArc(0, 0, r.width, r.height, 90, -45); 252 } 253 else 254 { 255 g.drawArc(0, 0, r.width, r.height, 90, -45); 256 } 257 258 g.setColor(Color.orange); 259 if (filled) 260 { 261 g.fillArc(0, 0, r.width, r.height, 135, -45); 262 } 263 else 264 { 265 g.drawArc(0, 0, r.width, r.height, 135, -45); 266 } 267 268 g.setColor(Color.magenta); 269 270 if (filled) 271 { 272 g.fillArc(0, 0, r.width, r.height, -225, 45); 273 } 274 else 275 { 276 g.drawArc(0, 0, r.width, r.height, -225, 45); 277 } 278 279 g.setColor(Color.yellow); 280 if (filled) 281 { 282 g.fillArc(0, 0, r.width, r.height, 225, -45); 283 } 284 else 285 { 286 g.drawArc(0, 0, r.width, r.height, 225, -45); 287 } 288 289 g.setColor(Color.green); 290 if (filled) 291 { 292 g.fillArc(0, 0, r.width, r.height, -135, 45); 293 } 294 else 295 { 296 g.drawArc(0, 0, r.width, r.height, -135, 45); 297 } 298 299 g.setColor(Color.orange); 300 if (filled) 301 { 302 g.fillArc(0, 0, r.width, r.height, -45, -45); 303 } 304 else 305 { 306 g.drawArc(0, 0, r.width, r.height, -45, -45); 307 } 308 309 g.setColor(Color.magenta); 310 if (filled) 311 { 312 g.fillArc(0, 0, r.width, r.height, 315, 45); 313 } 314 else 315 { 316 g.drawArc(0, 0, r.width, r.height, 315, 45); 317 } 318 319 } 321 } 323 324 abstract class Shape 325 { 326 abstract void draw(Graphics g, int x, int y, int w, int h); 327 abstract void fill(Graphics g, int x, int y, int w, int h); 328 } 329 330 331 class RectShape extends Shape 332 { 333 void draw(Graphics g, int x, int y, int w, int h) 334 { 335 g.drawRect(x, y, w, h); 336 } 337 338 void fill(Graphics g, int x, int y, int w, int h) 339 { 340 g.fillRect(x, y, w, h); 341 } 342 } 343 344 345 class OvalShape extends Shape 346 { 347 void draw(Graphics g, int x, int y, int w, int h) 348 { 349 g.drawOval(x, y, w, h); 350 } 351 352 void fill(Graphics g, int x, int y, int w, int h) 353 { 354 g.fillOval(x, y, w, h); 355 } 356 } 357 358 359 class RoundRectShape extends Shape 360 { 361 void draw(Graphics g, int x, int y, int w, int h) 362 { 363 g.drawRoundRect(x, y, w, h, 10, 10); 364 } 365 366 void fill(Graphics g, int x, int y, int w, int h) 367 { 368 g.fillRoundRect(x, y, w, h, 10, 10); 369 } 370 } 371 372 class PolygonShape extends Shape 373 { 374 Polygon p; 376 Polygon pBase; 377 378 public PolygonShape() 379 { 380 pBase = new Polygon(); 381 pBase.addPoint(0, 0); 382 pBase.addPoint(10, 0); 383 pBase.addPoint(5, 15); 384 pBase.addPoint(10, 20); 385 pBase.addPoint(5, 20); 386 pBase.addPoint(0, 10); 387 pBase.addPoint(0, 0); 388 } 389 390 void scalePolygon(float w, float h) 391 { 392 p = new Polygon(); 393 for (int i = 0; i < pBase.npoints; ++i) 394 { 395 p.addPoint( (int) (pBase.xpoints[i] * w), 396 (int) (pBase.ypoints[i] * h) ); 397 } 398 399 } 400 401 void draw(Graphics g, int x, int y, int w, int h) 402 { 403 Graphics ng = g.create(); 404 try { 405 ng.translate(x, y); 406 scalePolygon( (float) ( (float) w / (float) 10 ), 407 (float) ( (float) h / (float) 20 ) ); 408 ng.drawPolygon(p); 409 } finally { 410 ng.dispose(); 411 } 412 } 413 414 void fill(Graphics g, int x, int y, int w, int h) 415 { 416 Graphics ng = g.create(); 417 try { 418 ng.translate(x, y); 419 scalePolygon( (float) ( (float) w / (float) 10 ), 420 (float) ( (float) h / (float) 20 ) ); 421 ng.fillPolygon(p); 422 } finally { 423 ng.dispose(); 424 } 425 } 426 } 427 428 429 class ShapeTest extends Panel 430 { 431 Shape shape; 432 int step; 433 434 public ShapeTest(Shape shape, int step) 435 { 436 this.shape = shape; 437 this.step = step; 438 } 439 440 public ShapeTest(Shape shape) 441 { 442 this(shape, 10); 443 } 444 445 public void paint(Graphics g) { 446 Rectangle bounds = getBounds(); 447 448 int cx, cy, cw, ch; 449 450 Color color; 451 452 for (color=Color.red, 453 cx=bounds.x, 454 cy=bounds.y, 455 cw=bounds.width / 2, 456 ch=bounds.height; 457 cw > 0 && ch > 0; 458 459 cx+=step, 460 cy += step, 461 cw -= (step * 2), 462 ch -= (step * 2), 463 color=ColorUtils.darker(color, 0.9) ) { 464 g.setColor(color); 465 shape.draw(g, cx, cy, cw, ch); 466 } 467 468 for (cx=bounds.x + bounds.width / 2, 469 cy=bounds.y, 470 cw=bounds.width / 2, ch=bounds.height; 471 cw > 0 && ch > 0; 472 473 cx+=step, 474 cy += step, 475 cw -= (step * 2), 476 ch -= (step * 2) ) { 477 if (g.getColor() == Color.red) { 478 g.setColor(Color.blue); 479 } 480 else { 481 g.setColor(Color.red); 482 } 483 484 shape.fill(g, cx, cy, cw, ch); 485 } } } 489 class ColorUtils { 490 static Color brighter(Color c, double factor) { 491 return new Color( Math.min((int)(c.getRed() *(1/factor)), 255), 492 Math.min((int)(c.getGreen()*(1/factor)), 255), 493 Math.min((int)(c.getBlue() *(1/factor)), 255) ); 494 } 495 496 static Color darker(Color c, double factor) { 497 return new Color( Math.max((int)(c.getRed() *factor), 0), 498 Math.max((int)(c.getGreen()*factor), 0), 499 Math.max((int)(c.getBlue() *factor), 0) ); 500 } 501 } 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 | Popular Tags |