1:
37:
38: package ;
39:
40: import ;
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48: import ;
49: import ;
50: import ;
51:
52:
53:
60: public class DebugGraphics extends Graphics
61: {
62:
65: public static final int LOG_OPTION = 1;
66:
67:
70: public static final int FLASH_OPTION = 2;
71:
72:
75: public static final int BUFFERED_OPTION = 4;
76:
77:
80: public static final int NONE_OPTION = -1;
81:
82: static Color debugFlashColor = Color.RED;
83: static int debugFlashCount = 10;
84: static int debugFlashTime = 1000;
85: static PrintStream debugLogStream = System.out;
86:
87:
91: static int counter = 0;
92:
93:
96: Graphics graphics;
97:
98:
101: Image buffer;
102:
103:
106: int debugOptions;
107:
108:
111: int graphicsID;
112:
113:
116: int xOffset;
117:
118:
121: int yOffset;
122:
123:
126: public DebugGraphics()
127: {
128: counter++;
129: }
130:
131:
137: public DebugGraphics(Graphics graphics, JComponent component)
138: {
139: this(graphics);
140:
141: }
142:
143:
148: public DebugGraphics(Graphics graphics)
149: {
150: this();
151: this.graphics = graphics;
152: }
153:
154:
159: public void setColor(Color color)
160: {
161: if ((debugOptions & LOG_OPTION) != 0)
162: logStream().println(prefix() + " Setting color: " + color);
163:
164: graphics.setColor(color);
165: }
166:
167:
173: public Graphics create()
174: {
175: DebugGraphics copy = new DebugGraphics(graphics.create());
176: copy.debugOptions = debugOptions;
177: return copy;
178: }
179:
180:
191: public Graphics create(int x, int y, int width, int height)
192: {
193: DebugGraphics copy = new DebugGraphics(graphics.create(x, y, width,
194: height));
195: copy.debugOptions = debugOptions;
196: return copy;
197: }
198:
199:
204: public static Color flashColor()
205: {
206: return debugFlashColor;
207: }
208:
209:
214: public static void setFlashColor(Color color)
215: {
216: debugFlashColor = color;
217: }
218:
219:
224: public static int flashTime()
225: {
226: return debugFlashTime;
227: }
228:
229:
234: public static void setFlashTime(int time)
235: {
236: debugFlashTime = time;
237: }
238:
239:
244: public static int flashCount()
245: {
246: return debugFlashCount;
247: }
248:
249:
254: public static void setFlashCount(int count)
255: {
256: debugFlashCount = count;
257: }
258:
259:
264: public static PrintStream logStream()
265: {
266: return debugLogStream;
267: }
268:
269:
274: public static void setLogStream(PrintStream stream)
275: {
276: debugLogStream = stream;
277: }
278:
279:
284: public Font getFont()
285: {
286: return graphics.getFont();
287: }
288:
289:
294: public void setFont(Font font)
295: {
296: if ((debugOptions & LOG_OPTION) != 0)
297: logStream().println(prefix() + " Setting font: " + font);
298:
299: graphics.setFont(font);
300: }
301:
302:
307: public Color getColor()
308: {
309: return graphics.getColor();
310: }
311:
312:
317: public FontMetrics getFontMetrics()
318: {
319: return graphics.getFontMetrics();
320: }
321:
322:
329: public FontMetrics getFontMetrics(Font font)
330: {
331: return graphics.getFontMetrics(font);
332: }
333:
334:
340: public void translate(int x, int y)
341: {
342: if ((debugOptions & LOG_OPTION) != 0)
343: logStream().println(prefix() + " Translating by: " + new Point(x, y));
344:
345: graphics.translate(x, y);
346: }
347:
348:
351: public void setPaintMode()
352: {
353: if ((debugOptions & LOG_OPTION) != 0)
354: logStream().println(prefix() + " Setting paint mode");
355:
356: graphics.setPaintMode();
357: }
358:
359:
364: public void setXORMode(Color color)
365: {
366: if ((debugOptions & LOG_OPTION) != 0)
367: logStream().println(prefix() + " Setting XOR mode: " + color);
368:
369: graphics.setXORMode(color);
370: }
371:
372:
377: public Rectangle getClipBounds()
378: {
379: return graphics.getClipBounds();
380: }
381:
382:
390: public void clipRect(int x, int y, int width, int height)
391: {
392: if ((debugOptions & LOG_OPTION) != 0)
393: {
394: logStream().print(prefix() + " Setting clipRect: "
395: + new Rectangle(x, y, width, height));
396: }
397:
398: graphics.clipRect(x, y, width, height);
399:
400: if ((debugOptions & LOG_OPTION) != 0)
401: logStream().println(" Netting clipRect: " + graphics.getClipBounds());
402: }
403:
404:
412: public void setClip(int x, int y, int width, int height)
413: {
414: if ((debugOptions & LOG_OPTION) != 0)
415: {
416: logStream().println(prefix() + " Setting new clipRect: "
417: + new Rectangle(x, y, width, height));
418: }
419:
420: graphics.setClip(x, y, width, height);
421: }
422:
423:
428: public Shape getClip()
429: {
430: return graphics.getClip();
431: }
432:
433:
438: public void setClip(Shape shape)
439: {
440: if ((debugOptions & LOG_OPTION) != 0)
441: logStream().println(prefix() + " Setting new clipRect: " + shape);
442:
443: graphics.setClip(shape);
444: }
445:
446: private void sleep(int milliseconds)
447: {
448: try
449: {
450: Thread.sleep(milliseconds);
451: }
452: catch (InterruptedException e)
453: {
454:
455: }
456: }
457:
458:
466: public void drawRect(int x, int y, int width, int height)
467: {
468: if ((debugOptions & LOG_OPTION) != 0)
469: {
470: logStream().println(prefix() + " Drawing rect: "
471: + new Rectangle(x, y, width, height));
472: }
473:
474: if ((debugOptions & FLASH_OPTION) != 0)
475: {
476: Color color = graphics.getColor();
477: for (int index = 0; index < (debugFlashCount - 1); ++index)
478: {
479: graphics.setColor(color);
480: graphics.drawRect(x, y, width, height);
481: sleep(debugFlashTime);
482: graphics.setColor(debugFlashColor);
483: graphics.drawRect(x, y, width, height);
484: sleep(debugFlashTime);
485: }
486: graphics.setColor(color);
487: }
488:
489: graphics.drawRect(x, y, width, height);
490: }
491:
492:
500: public void fillRect(int x, int y, int width, int height)
501: {
502: if ((debugOptions & LOG_OPTION) != 0)
503: {
504: logStream().println(prefix() + " Filling rect: "
505: + new Rectangle(x, y, width, height));
506: }
507:
508: if ((debugOptions & FLASH_OPTION) != 0)
509: {
510: Color color = graphics.getColor();
511: for (int index = 0; index < (debugFlashCount - 1); ++index)
512: {
513: graphics.setColor(color);
514: graphics.fillRect(x, y, width, height);
515: sleep(debugFlashTime);
516: graphics.setColor(debugFlashColor);
517: graphics.fillRect(x, y, width, height);
518: sleep(debugFlashTime);
519: }
520: graphics.setColor(color);
521: }
522:
523: graphics.fillRect(x, y, width, height);
524: }
525:
526:
534: public void clearRect(int x, int y, int width, int height)
535: {
536: if ((debugOptions & LOG_OPTION) != 0)
537: {
538: logStream().println(prefix() + " Clearing rect: "
539: + new Rectangle(x, y, width, height));
540: }
541:
542: graphics.clearRect(x, y, width, height);
543: }
544:
545:
555: public void drawRoundRect(int x, int y, int width, int height,
556: int arcWidth, int arcHeight)
557: {
558: if ((debugOptions & LOG_OPTION) != 0)
559: {
560: logStream().println(prefix() + " Drawing round rect: "
561: + new Rectangle(x, y, width, height)
562: + " arcWidth: " + arcWidth
563: + " arcHeight: " + arcHeight);
564: }
565:
566: graphics.drawRoundRect(x, y, width, height, arcWidth, arcHeight);
567: }
568:
569:
579: public void fillRoundRect(int x, int y, int width, int height,
580: int arcWidth, int arcHeight)
581: {
582: if ((debugOptions & LOG_OPTION) != 0)
583: {
584: logStream().println(prefix() + " Filling round rect: "
585: + new Rectangle(x, y, width, height)
586: + " arcWidth: " + arcWidth
587: + " arcHeight: " + arcHeight);
588: }
589:
590: graphics.fillRoundRect(x, y, width, height, arcWidth, arcHeight);
591: }
592:
593:
601: public void drawLine(int x1, int y1, int x2, int y2)
602: {
603: if ((debugOptions & LOG_OPTION) != 0)
604: {
605: logStream().println(prefix() + " Drawing line: from (" + x1 + ", "
606: + y1 + ") to (" + x2 + ", " + y2 + ")");
607: }
608:
609: graphics.drawLine(x1, y1, x2, y2);
610: }
611:
612:
621: public void draw3DRect(int x, int y, int width, int height, boolean raised)
622: {
623: if ((debugOptions & LOG_OPTION) != 0)
624: {
625: logStream().println(prefix() + " Drawing 3D rect: "
626: + new Rectangle(x, y, width, height)
627: + "Raised bezel: " + raised);
628: }
629:
630: graphics.draw3DRect(x, y, width, height, raised);
631: }
632:
633:
642: public void fill3DRect(int x, int y, int width, int height, boolean raised)
643: {
644: if ((debugOptions & LOG_OPTION) != 0)
645: {
646: logStream().println(prefix() + " Filling 3D rect: "
647: + new Rectangle(x, y, width, height)
648: + "Raised bezel: " + raised);
649: }
650:
651: graphics.fill3DRect(x, y, width, height, raised);
652: }
653:
654:
662: public void drawOval(int x, int y, int width, int height)
663: {
664: if ((debugOptions & LOG_OPTION) != 0)
665: {
666: logStream().println(prefix() + " Drawing oval: "
667: + new Rectangle(x, y, width, height));
668: }
669:
670: graphics.drawOval(x, y, width, height);
671: }
672:
673:
681: public void fillOval(int x, int y, int width, int height)
682: {
683: if ((debugOptions & LOG_OPTION) != 0)
684: {
685: logStream().println(prefix() + " Filling oval: "
686: + new Rectangle(x, y, width, height));
687: }
688:
689: graphics.fillOval(x, y, width, height);
690: }
691:
692:
702: public void drawArc(int x, int y, int width, int height,
703: int startAngle, int arcAngle)
704: {
705: if ((debugOptions & LOG_OPTION) != 0)
706: {
707: logStream().println(prefix() + " Drawing arc: "
708: + new Rectangle(x, y, width, height)
709: + " startAngle: " + startAngle
710: + " arcAngle: " + arcAngle);
711: }
712:
713: graphics.drawArc(x, y, width, height, startAngle, arcAngle);
714: }
715:
716:
726: public void fillArc(int x, int y, int width, int height,
727: int startAngle, int arcAngle)
728: {
729: if ((debugOptions & LOG_OPTION) != 0)
730: {
731: logStream().println(prefix() + " Filling arc: "
732: + new Rectangle(x, y, width, height)
733: + " startAngle: " + startAngle
734: + " arcAngle: " + arcAngle);
735: }
736:
737: graphics.fillArc(x, y, width, height, startAngle, arcAngle);
738: }
739:
740:
747: public void drawPolyline(int[] xpoints, int[] ypoints, int npoints)
748: {
749: if ((debugOptions & LOG_OPTION) != 0)
750: {
751: logStream().println(prefix() + " Drawing polyline: nPoints: " + npoints
752: + " X's: " + xpoints + " Y's: " + ypoints);
753: }
754:
755: graphics.drawPolyline(xpoints, ypoints, npoints);
756: }
757:
758:
765: public void drawPolygon(int[] xpoints, int[] ypoints, int npoints)
766: {
767: if ((debugOptions & LOG_OPTION) != 0)
768: {
769: logStream().println(prefix() + " Drawing polygon: nPoints: " + npoints
770: + " X's: " + xpoints + " Y's: " + ypoints);
771: }
772:
773: graphics.drawPolygon(xpoints, ypoints, npoints);
774: }
775:
776:
783: public void fillPolygon(int[] xpoints, int[] ypoints, int npoints)
784: {
785: if ((debugOptions & LOG_OPTION) != 0)
786: {
787: logStream().println(prefix() + " Drawing polygon: nPoints: " + npoints
788: + " X's: " + xpoints + " Y's: " + ypoints);
789: }
790:
791: graphics.fillPolygon(xpoints, ypoints, npoints);
792: }
793:
794:
801: public void drawString(String string, int x, int y)
802: {
803: if ((debugOptions & LOG_OPTION) != 0)
804: {
805: logStream().println(prefix() + " Drawing string: \"" + string
806: + "\" at: " + new Point(x, y));
807: }
808:
809: graphics.drawString(string, x, y);
810: }
811:
812:
819: public void drawString(AttributedCharacterIterator iterator,
820: int x, int y)
821: {
822: if ((debugOptions & LOG_OPTION) != 0)
823: {
824: logStream().println(prefix() + " Drawing string: \"" + iterator
825: + "\" at: " + new Point(x, y));
826: }
827:
828: graphics.drawString(iterator, x, y);
829: }
830:
831:
840: public void drawBytes(byte[] data, int offset, int length,
841: int x, int y)
842: {
843: if ((debugOptions & LOG_OPTION) != 0)
844: logStream().println(prefix() + " Drawing bytes at: " + new Point(x, y));
845:
846: graphics.drawBytes(data, offset, length, x, y);
847: }
848:
849:
858: public void drawChars(char[] data, int offset, int length,
859: int x, int y)
860: {
861: if ((debugOptions & LOG_OPTION) != 0)
862: logStream().println(prefix() + " Drawing chars at: " + new Point(x, y));
863:
864: if ((debugOptions & FLASH_OPTION) != 0)
865: {
866: Color color = graphics.getColor();
867: for (int index = 0; index < (debugFlashCount - 1); ++index)
868: {
869: graphics.setColor(color);
870: graphics.drawChars(data, offset, length, x, y);
871: sleep(debugFlashTime);
872: graphics.setColor(debugFlashColor);
873: graphics.drawChars(data, offset, length, x, y);
874: sleep(debugFlashTime);
875: }
876: graphics.setColor(color);
877: }
878:
879: graphics.drawChars(data, offset, length, x, y);
880: }
881:
882:
891: public boolean drawImage(Image image, int x, int y,
892: ImageObserver observer)
893: {
894: if ((debugOptions & LOG_OPTION) != 0)
895: {
896: logStream().println(prefix() + " Drawing image: " + image + " at: "
897: + new Point(x, y));
898: }
899:
900: return graphics.drawImage(image, x, y, observer);
901: }
902:
903:
915: public boolean drawImage(Image image, int x, int y, int width,
916: int height, ImageObserver observer)
917: {
918: if ((debugOptions & LOG_OPTION) != 0)
919: {
920: logStream().println(prefix() + " Drawing image: " + image
921: + " at: " + new Rectangle(x, y, width, height));
922: }
923:
924: return graphics.drawImage(image, x, y, width, height, observer);
925: }
926:
927:
939: public boolean drawImage(Image image, int x, int y,
940: Color background, ImageObserver observer)
941: {
942: if ((debugOptions & LOG_OPTION) != 0)
943: {
944: logStream().println(prefix() + " Drawing image: " + image
945: + " at: " + new Point(x, y)
946: + ", bgcolor: " + background);
947: }
948:
949: return graphics.drawImage(image, x, y, background, observer);
950: }
951:
952:
966: public boolean drawImage(Image image, int x, int y, int width, int height,
967: Color background, ImageObserver observer)
968: {
969: if ((debugOptions & LOG_OPTION) != 0)
970: {
971: logStream().println(prefix() + " Drawing image: " + image
972: + " at: " + new Rectangle(x, y, width, height)
973: + ", bgcolor: " + background);
974: }
975:
976: return graphics.drawImage(image, x, y, width, height, background, observer);
977: }
978:
979:
995: public boolean drawImage(Image image, int dx1, int dy1,
996: int dx2, int dy2, int sx1, int sy1, int sx2, int sy2,
997: ImageObserver observer)
998: {
999: if ((debugOptions & LOG_OPTION) != 0)
1000: {
1001: logStream().println(prefix() + " Drawing image: " + image
1002: + " destination: " + new Rectangle(dx1, dy1, dx2, dy2)
1003: + " source: " + new Rectangle(sx1, sy1, sx2, sy2));
1004: }
1005:
1006: return graphics.drawImage(image, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, observer);
1007: }
1008:
1009:
1027: public boolean drawImage(Image image, int dx1, int dy1,
1028: int dx2, int dy2, int sx1, int sy1, int sx2, int sy2,
1029: Color background, ImageObserver observer)
1030: {
1031: if ((debugOptions & LOG_OPTION) != 0)
1032: {
1033: logStream().println(prefix() + " Drawing image: " + image
1034: + " destination: " + new Rectangle(dx1, dy1, dx2, dy2)
1035: + " source: " + new Rectangle(sx1, sy1, sx2, sy2)
1036: + ", bgcolor: " + background);
1037: }
1038:
1039: return graphics.drawImage(image, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, background, observer);
1040: }
1041:
1042:
1052: public void copyArea(int x, int y, int width, int height,
1053: int destx, int desty)
1054: {
1055: if ((debugOptions & LOG_OPTION) != 0)
1056: {
1057: logStream().println(prefix() + " Copying area from: "
1058: + new Rectangle(x, y, width, height)
1059: + " to: " + new Point(destx, desty));
1060: }
1061:
1062: graphics.copyArea(x, y, width, height, destx, desty);
1063: }
1064:
1065:
1068: public void dispose()
1069: {
1070: graphics.dispose();
1071: graphics = null;
1072: }
1073:
1074:
1079: public boolean isDrawingBuffer()
1080: {
1081: return false;
1082: }
1083:
1084:
1089: public void setDebugOptions(int options)
1090: {
1091: debugOptions = options;
1092: if ((debugOptions & LOG_OPTION) != 0)
1093: if (options == NONE_OPTION)
1094: logStream().println(prefix() + "Disabling debug");
1095: else
1096: logStream().println(prefix() + "Enabling debug");
1097: }
1098:
1099:
1104: public int getDebugOptions()
1105: {
1106: return debugOptions;
1107: }
1108:
1109:
1121: private String prefix()
1122: {
1123: return "Graphics(" + counter + "-1)";
1124: }
1125: }