| 1 7 8 package javax.swing; 9 10 import java.awt.*; 11 import java.awt.image.*; 12 import java.text.AttributedCharacterIterator ; 13 14 32 public class DebugGraphics extends Graphics { 33 Graphics graphics; 34 Image buffer; 35 int debugOptions; 36 int graphicsID = graphicsCount++; 37 int xOffset, yOffset; 38 private static int graphicsCount = 0; 39 40 41 public static final int LOG_OPTION = 1 << 0; 42 43 public static final int FLASH_OPTION = 1 << 1; 44 45 public static final int BUFFERED_OPTION = 1 << 2; 46 47 public static final int NONE_OPTION = -1; 48 49 static { 50 JComponent.DEBUG_GRAPHICS_LOADED = true; 51 } 52 53 57 public DebugGraphics() { 58 super(); 59 buffer = null; 60 xOffset = yOffset = 0; 61 } 62 63 70 public DebugGraphics(Graphics graphics, JComponent component) { 71 this(graphics); 72 setDebugOptions(component.shouldDebugGraphics()); 73 } 74 75 81 public DebugGraphics(Graphics graphics) { 82 this(); 83 this.graphics = graphics; 84 } 85 86 89 public Graphics create() { 90 DebugGraphics debugGraphics; 91 92 debugGraphics = new DebugGraphics (); 93 debugGraphics.graphics = graphics.create(); 94 debugGraphics.debugOptions = debugOptions; 95 debugGraphics.buffer = buffer; 96 97 return debugGraphics; 98 } 99 100 103 public Graphics create(int x, int y, int width, int height) { 104 DebugGraphics debugGraphics; 105 106 debugGraphics = new DebugGraphics (); 107 debugGraphics.graphics = graphics.create(x, y, width, height); 108 debugGraphics.debugOptions = debugOptions; 109 debugGraphics.buffer = buffer; 110 debugGraphics.xOffset = xOffset + x; 111 debugGraphics.yOffset = yOffset + y; 112 113 return debugGraphics; 114 } 115 116 117 121 124 public static void setFlashColor(Color flashColor) { 125 info().flashColor = flashColor; 126 } 127 128 132 public static Color flashColor() { 133 return info().flashColor; 134 } 135 136 139 public static void setFlashTime(int flashTime) { 140 info().flashTime = flashTime; 141 } 142 143 147 public static int flashTime() { 148 return info().flashTime; 149 } 150 151 154 public static void setFlashCount(int flashCount) { 155 info().flashCount = flashCount; 156 } 157 158 161 public static int flashCount() { 162 return info().flashCount; 163 } 164 165 167 public static void setLogStream(java.io.PrintStream stream) { 168 info().stream = stream; 169 } 170 171 174 public static java.io.PrintStream logStream() { 175 return info().stream; 176 } 177 178 180 public void setFont(Font aFont) { 181 if (debugLog()) { 182 info().log(toShortString() + " Setting font: " + aFont); 183 } 184 graphics.setFont(aFont); 185 } 186 187 190 public Font getFont() { 191 return graphics.getFont(); 192 } 193 194 196 public void setColor(Color aColor) { 197 if (debugLog()) { 198 info().log(toShortString() + " Setting color: " + aColor); 199 } 200 graphics.setColor(aColor); 201 } 202 203 206 public Color getColor() { 207 return graphics.getColor(); 208 } 209 210 211 215 218 public FontMetrics getFontMetrics() { 219 return graphics.getFontMetrics(); 220 } 221 222 225 public FontMetrics getFontMetrics(Font f) { 226 return graphics.getFontMetrics(f); 227 } 228 229 232 public void translate(int x, int y) { 233 if (debugLog()) { 234 info().log(toShortString() + 235 " Translating by: " + new Point(x, y)); 236 } 237 xOffset += x; 238 yOffset += y; 239 graphics.translate(x, y); 240 } 241 242 245 public void setPaintMode() { 246 if (debugLog()) { 247 info().log(toShortString() + " Setting paint mode"); 248 } 249 graphics.setPaintMode(); 250 } 251 252 255 public void setXORMode(Color aColor) { 256 if (debugLog()) { 257 info().log(toShortString() + " Setting XOR mode: " + aColor); 258 } 259 graphics.setXORMode(aColor); 260 } 261 262 265 public Rectangle getClipBounds() { 266 return graphics.getClipBounds(); 267 } 268 269 272 public void clipRect(int x, int y, int width, int height) { 273 graphics.clipRect(x, y, width, height); 274 if (debugLog()) { 275 info().log(toShortString() + 276 " Setting clipRect: " + (new Rectangle(x, y, width, height)) + 277 " New clipRect: " + graphics.getClip()); 278 } 279 } 280 281 284 public void setClip(int x, int y, int width, int height) { 285 graphics.setClip(x, y, width, height); 286 if (debugLog()) { 287 info().log(toShortString() + 288 " Setting new clipRect: " + graphics.getClip()); 289 } 290 } 291 292 295 public Shape getClip() { 296 return graphics.getClip(); 297 } 298 299 302 public void setClip(Shape clip) { 303 graphics.setClip(clip); 304 if (debugLog()) { 305 info().log(toShortString() + 306 " Setting new clipRect: " + graphics.getClip()); 307 } 308 } 309 310 313 public void drawRect(int x, int y, int width, int height) { 314 DebugGraphicsInfo info = info(); 315 316 if (debugLog()) { 317 info().log(toShortString() + 318 " Drawing rect: " + 319 new Rectangle(x, y, width, height)); 320 } 321 322 if (isDrawingBuffer()) { 323 if (debugBuffered()) { 324 Graphics debugGraphics = debugGraphics(); 325 326 debugGraphics.drawRect(x, y, width, height); 327 debugGraphics.dispose(); 328 } 329 } else if (debugFlash()) { 330 Color oldColor = getColor(); 331 int i, count = (info.flashCount * 2) - 1; 332 333 for (i = 0; i < count; i++) { 334 graphics.setColor((i % 2) == 0 ? info.flashColor : oldColor); 335 graphics.drawRect(x, y, width, height); 336 Toolkit.getDefaultToolkit().sync(); 337 sleep(info.flashTime); 338 } 339 graphics.setColor(oldColor); 340 } 341 graphics.drawRect(x, y, width, height); 342 } 343 344 347 public void fillRect(int x, int y, int width, int height) { 348 DebugGraphicsInfo info = info(); 349 350 if (debugLog()) { 351 info().log(toShortString() + 352 " Filling rect: " + 353 new Rectangle(x, y, width, height)); 354 } 355 356 if (isDrawingBuffer()) { 357 if (debugBuffered()) { 358 Graphics debugGraphics = debugGraphics(); 359 360 debugGraphics.fillRect(x, y, width, height); 361 debugGraphics.dispose(); 362 } 363 } else if (debugFlash()) { 364 Color oldColor = getColor(); 365 int i, count = (info.flashCount * 2) - 1; 366 367 for (i = 0; i < count; i++) { 368 graphics.setColor((i % 2) == 0 ? info.flashColor : oldColor); 369 graphics.fillRect(x, y, width, height); 370 Toolkit.getDefaultToolkit().sync(); 371 sleep(info.flashTime); 372 } 373 graphics.setColor(oldColor); 374 } 375 graphics.fillRect(x, y, width, height); 376 } 377 378 381 public void clearRect(int x, int y, int width, int height) { 382 DebugGraphicsInfo info = info(); 383 384 if (debugLog()) { 385 info().log(toShortString() + 386 " Clearing rect: " + 387 new Rectangle(x, y, width, height)); 388 } 389 390 if (isDrawingBuffer()) { 391 if (debugBuffered()) { 392 Graphics debugGraphics = debugGraphics(); 393 394 debugGraphics.clearRect(x, y, width, height); 395 debugGraphics.dispose(); 396 } 397 } else if (debugFlash()) { 398 Color oldColor = getColor(); 399 int i, count = (info.flashCount * 2) - 1; 400 401 for (i = 0; i < count; i++) { 402 graphics.setColor((i % 2) == 0 ? info.flashColor : oldColor); 403 graphics.clearRect(x, y, width, height); 404 Toolkit.getDefaultToolkit().sync(); 405 sleep(info.flashTime); 406 } 407 graphics.setColor(oldColor); 408 } 409 graphics.clearRect(x, y, width, height); 410 } 411 412 415 public void drawRoundRect(int x, int y, int width, int height, 416 int arcWidth, int arcHeight) { 417 DebugGraphicsInfo info = info(); 418 419 if (debugLog()) { 420 info().log(toShortString() + 421 " Drawing round rect: " + 422 new Rectangle(x, y, width, height) + 423 " arcWidth: " + arcWidth + 424 " archHeight: " + arcHeight); 425 } 426 if (isDrawingBuffer()) { 427 if (debugBuffered()) { 428 Graphics debugGraphics = debugGraphics(); 429 430 debugGraphics.drawRoundRect(x, y, width, height, 431 arcWidth, arcHeight); 432 debugGraphics.dispose(); 433 } 434 } else if (debugFlash()) { 435 Color oldColor = getColor(); 436 int i, count = (info.flashCount * 2) - 1; 437 438 for (i = 0; i < count; i++) { 439 graphics.setColor((i % 2) == 0 ? info.flashColor : oldColor); 440 graphics.drawRoundRect(x, y, width, height, 441 arcWidth, arcHeight); 442 Toolkit.getDefaultToolkit().sync(); 443 sleep(info.flashTime); 444 } 445 graphics.setColor(oldColor); 446 } 447 graphics.drawRoundRect(x, y, width, height, arcWidth, arcHeight); 448 } 449 450 453 public void fillRoundRect(int x, int y, int width, int height, 454 int arcWidth, int arcHeight) { 455 DebugGraphicsInfo info = info(); 456 457 if (debugLog()) { 458 info().log(toShortString() + 459 " Filling round rect: " + 460 new Rectangle(x, y, width, height) + 461 " arcWidth: " + arcWidth + 462 " archHeight: " + arcHeight); 463 } 464 if (isDrawingBuffer()) { 465 if (debugBuffered()) { 466 Graphics debugGraphics = debugGraphics(); 467 468 debugGraphics.fillRoundRect(x, y, width, height, 469 arcWidth, arcHeight); 470 debugGraphics.dispose(); 471 } 472 } else if (debugFlash()) { 473 Color oldColor = getColor(); 474 int i, count = (info.flashCount * 2) - 1; 475 476 for (i = 0; i < count; i++) { 477 graphics.setColor((i % 2) == 0 ? info.flashColor : oldColor); 478 graphics.fillRoundRect(x, y, width, height, 479 arcWidth, arcHeight); 480 Toolkit.getDefaultToolkit().sync(); 481 sleep(info.flashTime); 482 } 483 graphics.setColor(oldColor); 484 } 485 graphics.fillRoundRect(x, y, width, height, arcWidth, arcHeight); 486 } 487 488 491 public void drawLine(int x1, int y1, int x2, int y2) { 492 DebugGraphicsInfo info = info(); 493 494 if (debugLog()) { 495 info().log(toShortString() + 496 " Drawing line: from " + pointToString(x1, y1) + 497 " to " + pointToString(x2, y2)); 498 } 499 500 if (isDrawingBuffer()) { 501 if (debugBuffered()) { 502 Graphics debugGraphics = debugGraphics(); 503 504 debugGraphics.drawLine(x1, y1, x2, y2); 505 debugGraphics.dispose(); 506 } 507 } else if (debugFlash()) { 508 Color oldColor = getColor(); 509 int i, count = (info.flashCount * 2) - 1; 510 511 for (i = 0; i < count; i++) { 512 graphics.setColor((i % 2) == 0 ? info.flashColor : oldColor); 513 graphics.drawLine(x1, y1, x2, y2); 514 Toolkit.getDefaultToolkit().sync(); 515 sleep(info.flashTime); 516 } 517 graphics.setColor(oldColor); 518 } 519 graphics.drawLine(x1, y1, x2, y2); 520 } 521 522 525 public void draw3DRect(int x, int y, int width, int height, 526 boolean raised) { 527 DebugGraphicsInfo info = info(); 528 529 if (debugLog()) { 530 info().log(toShortString() + 531 " Drawing 3D rect: " + 532 new Rectangle(x, y, width, height) + 533 " Raised bezel: " + raised); 534 } 535 if (isDrawingBuffer()) { 536 if (debugBuffered()) { 537 Graphics debugGraphics = debugGraphics(); 538 539 debugGraphics.draw3DRect(x, y, width, height, raised); 540 debugGraphics.dispose(); 541 } 542 } else if (debugFlash()) { 543 Color oldColor = getColor(); 544 int i, count = (info.flashCount * 2) - 1; 545 546 for (i = 0; i < count; i++) { 547 graphics.setColor((i % 2) == 0 ? info.flashColor : oldColor); 548 graphics.draw3DRect(x, y, width, height, raised); 549 Toolkit.getDefaultToolkit().sync(); 550 sleep(info.flashTime); 551 } 552 graphics.setColor(oldColor); 553 } 554 graphics.draw3DRect(x, y, width, height, raised); 555 } 556 557 560 public void fill3DRect(int x, int y, int width, int height, 561 boolean raised) { 562 DebugGraphicsInfo info = info(); 563 564 if (debugLog()) { 565 info().log(toShortString() + 566 " Filling 3D rect: " + 567 new Rectangle(x, y, width, height) + 568 " Raised bezel: " + raised); 569 } 570 if (isDrawingBuffer()) { 571 if (debugBuffered()) { 572 Graphics debugGraphics = debugGraphics(); 573 574 debugGraphics.fill3DRect(x, y, width, height, raised); 575 debugGraphics.dispose(); 576 } 577 } else if (debugFlash()) { 578 Color oldColor = getColor(); 579 int i, count = (info.flashCount * 2) - 1; 580 581 for (i = 0; i < count; i++) { 582 graphics.setColor((i % 2) == 0 ? info.flashColor : oldColor); 583 graphics.fill3DRect(x, y, width, height, raised); 584 Toolkit.getDefaultToolkit().sync(); 585 sleep(info.flashTime); 586 } 587 graphics.setColor(oldColor); 588 } 589 graphics.fill3DRect(x, y, width, height, raised); 590 } 591 592 595 public void drawOval(int x, int y, int width, int height) { 596 DebugGraphicsInfo info = info(); 597 598 if (debugLog()) { 599 info().log(toShortString() + 600 " Drawing oval: " + 601 new Rectangle(x, y, width, height)); 602 } 603 if (isDrawingBuffer()) { 604 if (debugBuffered()) { 605 Graphics debugGraphics = debugGraphics(); 606 607 debugGraphics.drawOval(x, y, width, height); 608 debugGraphics.dispose(); 609 } 610 } else if (debugFlash()) { 611 Color oldColor = getColor(); 612 int i, count = (info.flashCount * 2) - 1; 613 614 for (i = 0; i < count; i++) { 615 graphics.setColor((i % 2) == 0 ? info.flashColor : oldColor); 616 graphics.drawOval(x, y, width, height); 617 Toolkit.getDefaultToolkit().sync(); 618 sleep(info.flashTime); 619 } 620 graphics.setColor(oldColor); 621 } 622 graphics.drawOval(x, y, width, height); 623 } 624 625 628 public void fillOval(int x, int y, int width, int height) { 629 DebugGraphicsInfo info = info(); 630 631 if (debugLog()) { 632 info().log(toShortString() + 633 " Filling oval: " + 634 new Rectangle(x, y, width, height)); 635 } 636 if (isDrawingBuffer()) { 637 if (debugBuffered()) { 638 Graphics debugGraphics = debugGraphics(); 639 640 debugGraphics.fillOval(x, y, width, height); 641 debugGraphics.dispose(); 642 } 643 } else if (debugFlash()) { 644 Color oldColor = getColor(); 645 int i, count = (info.flashCount * 2) - 1; 646 647 for (i = 0; i < count; i++) { 648 graphics.setColor((i % 2) == 0 ? info.flashColor : oldColor); 649 graphics.fillOval(x, y, width, height); 650 Toolkit.getDefaultToolkit().sync(); 651 sleep(info.flashTime); 652 } 653 graphics.setColor(oldColor); 654 } 655 graphics.fillOval(x, y, width, height); 656 } 657 658 661 public void drawArc(int x, int y, int width, int height, 662 int startAngle, int arcAngle) { 663 DebugGraphicsInfo info = info(); 664 665 if (debugLog()) { 666 info().log(toShortString() + 667 " Drawing arc: " + 668 new Rectangle(x, y, width, height) + 669 " startAngle: " + startAngle + 670 " arcAngle: " + arcAngle); 671 } 672 if (isDrawingBuffer()) { 673 if (debugBuffered()) { 674 Graphics debugGraphics = debugGraphics(); 675 676 debugGraphics.drawArc(x, y, width, height, 677 startAngle, arcAngle); 678 debugGraphics.dispose(); 679 } 680 } else if (debugFlash()) { 681 Color oldColor = getColor(); 682 int i, count = (info.flashCount * 2) - 1; 683 684 for (i = 0; i < count; i++) { 685 graphics.setColor((i % 2) == 0 ? info.flashColor : oldColor); 686 graphics.drawArc(x, y, width, height, startAngle, arcAngle); 687 Toolkit.getDefaultToolkit().sync(); 688 sleep(info.flashTime); 689 } 690 graphics.setColor(oldColor); 691 } 692 graphics.drawArc(x, y, width, height, startAngle, arcAngle); 693 } 694 695 698 public void fillArc(int x, int y, int width, int height, 699 int startAngle, int arcAngle) { 700 DebugGraphicsInfo info = info(); 701 702 if (debugLog()) { 703 info().log(toShortString() + 704 " Filling arc: " + 705 new Rectangle(x, y, width, height) + 706 " startAngle: " + startAngle + 707 " arcAngle: " + arcAngle); 708 } 709 if (isDrawingBuffer()) { 710 if (debugBuffered()) { 711 Graphics debugGraphics = debugGraphics(); 712 713 debugGraphics.fillArc(x, y, width, height, 714 startAngle, arcAngle); 715 debugGraphics.dispose(); 716 } 717 } else if (debugFlash()) { 718 Color oldColor = getColor(); 719 int i, count = (info.flashCount * 2) - 1; 720 721 for (i = 0; i < count; i++) { 722 graphics.setColor((i % 2) == 0 ? info.flashColor : oldColor); 723 graphics.fillArc(x, y, width, height, startAngle, arcAngle); 724 Toolkit.getDefaultToolkit().sync(); 725 sleep(info.flashTime); 726 } 727 graphics.setColor(oldColor); 728 } 729 graphics.fillArc(x, y, width, height, startAngle, arcAngle); 730 } 731 732 735 public void drawPolyline(int xPoints[], int yPoints[], int nPoints) { 736 DebugGraphicsInfo info = info(); 737 738 if (debugLog()) { 739 info().log(toShortString() + 740 " Drawing polyline: " + 741 " nPoints: " + nPoints + 742 " X's: " + xPoints + 743 " Y's: " + yPoints); 744 } 745 if (isDrawingBuffer()) { 746 if (debugBuffered()) { 747 Graphics debugGraphics = debugGraphics(); 748 749 debugGraphics.drawPolyline(xPoints, yPoints, nPoints); 750 debugGraphics.dispose(); 751 } 752 } else if (debugFlash()) { 753 Color oldColor = getColor(); 754 int i, count = (info.flashCount * 2) - 1; 755 756 for (i = 0; i < count; i++) { 757 graphics.setColor((i % 2) == 0 ? info.flashColor : oldColor); 758 graphics.drawPolyline(xPoints, yPoints, nPoints); 759 Toolkit.getDefaultToolkit().sync(); 760 sleep(info.flashTime); 761 } 762 graphics.setColor(oldColor); 763 } 764 graphics.drawPolyline(xPoints, yPoints, nPoints); 765 } 766 767 770 public void drawPolygon(int xPoints[], int yPoints[], int nPoints) { 771 DebugGraphicsInfo info = info(); 772 773 if (debugLog()) { 774 info().log(toShortString() + 775 " Drawing polygon: " + 776 " nPoints: " + nPoints + 777 " X's: " + xPoints + 778 " Y's: " + yPoints); 779 } 780 if (isDrawingBuffer()) { 781 if (debugBuffered()) { 782 Graphics debugGraphics = debugGraphics(); 783 784 debugGraphics.drawPolygon(xPoints, yPoints, nPoints); 785 debugGraphics.dispose(); 786 } 787 } else if (debugFlash()) { 788 Color oldColor = getColor(); 789 int i, count = (info.flashCount * 2) - 1; 790 791 for (i = 0; i < count; i++) { 792 graphics.setColor((i % 2) == 0 ? info.flashColor : oldColor); 793 graphics.drawPolygon(xPoints, yPoints, nPoints); 794 Toolkit.getDefaultToolkit().sync(); 795 sleep(info.flashTime); 796 } 797 graphics.setColor(oldColor); 798 } 799 graphics.drawPolygon(xPoints, yPoints, nPoints); 800 } 801 802 805 public void fillPolygon(int xPoints[], int yPoints[], int nPoints) { 806 DebugGraphicsInfo info = info(); 807 808 if (debugLog()) { 809 info().log(toShortString() + 810 " Filling polygon: " + 811 " nPoints: " + nPoints + 812 " X's: " + xPoints + 813 " Y's: " + yPoints); 814 } 815 if (isDrawingBuffer()) { 816 if (debugBuffered()) { 817 Graphics debugGraphics = debugGraphics(); 818 819 debugGraphics.fillPolygon(xPoints, yPoints, nPoints); 820 debugGraphics.dispose(); 821 } 822 } else if (debugFlash()) { 823 Color oldColor = getColor(); 824 int i, count = (info.flashCount * 2) - 1; 825 826 for (i = 0; i < count; i++) { 827 graphics.setColor((i % 2) == 0 ? info.flashColor : oldColor); 828 graphics.fillPolygon(xPoints, yPoints, nPoints); 829 Toolkit.getDefaultToolkit().sync(); 830 sleep(info.flashTime); 831 } 832 graphics.setColor(oldColor); 833 } 834 graphics.fillPolygon(xPoints, yPoints, nPoints); 835 } 836 837 840 public void drawString(String aString, int x, int y) { 841 DebugGraphicsInfo info = info(); 842 843 if (debugLog()) { 844 info().log(toShortString() + 845 " Drawing string: \"" + aString + 846 "\" at: " + new Point(x, y)); 847 } 848 849 if (isDrawingBuffer()) { 850 if (debugBuffered()) { 851 Graphics debugGraphics = debugGraphics(); 852 853 debugGraphics.drawString(aString, x, y); 854 debugGraphics.dispose(); 855 } 856 } else if (debugFlash()) { 857 Color oldColor = getColor(); 858 int i, count = (info.flashCount * 2) - 1; 859 860 for (i = 0; i < count; i++) { 861 graphics.setColor((i % 2) == 0 ? info.flashColor 862 : oldColor); 863 graphics.drawString(aString, x, y); 864 Toolkit.getDefaultToolkit().sync(); 865 sleep(info.flashTime); 866 } 867 graphics.setColor(oldColor); 868 } 869 graphics.drawString(aString, x, y); 870 } 871 872 875 public void drawString(AttributedCharacterIterator iterator, int x, int y) { 876 DebugGraphicsInfo info = info(); 877 878 if (debugLog()) { 879 info().log(toShortString() + 880 " Drawing text: \"" + iterator + 881 "\" at: " + new Point(x, y)); 882 } 883 884 if (isDrawingBuffer()) { 885
|