Subpages: 1. JComponent properties, size, and positioning
2. Event handling and dispatching
3. Multithreading
4. Timers
5. AppContext service
6. Inside Timers & the TimerQueue
7. JavaBeans architecture
8. Fonts, Colors, Graphics and text
9. Using the Graphics clipping area
10. Graphics debugging
11. Painting and validation
12. Focus Management
13. Keyboard input, KeyStrokes, and Actions
14. SwingUtilities
2.10 Graphics debugging
Graphics debugging provides the ability to observe each painting operation that occurs during the rendering of a component and all of its children. This is done in slow-motion, using distinct flashes to indicate the region being painted. It is intended to help find problems with rendering, layouts, and container hierarchies -- just about anything display related. If graphics debugging is enabled, the Graphics object used in painting is actually an instance of DebugGraphics (a subclass of Graphics). JComponent, and thus all Swing components, support graphics debugging and it can be turned on/off with JComponent's setDebugGraphicsOptions() method. This method takes an int parameter which is normally one of (or a bitmask combination -- using the bitwise | operator) four static values defined in DebugGraphics.
2.10.1 Graphics debugging options
1. DebugGraphics.FLASH_OPTION: Each paint operation flashes a specified number of times, in a specified flash color, with a specified flash interval. The default values are: 250ms flash interval, 4 flashes, and red flash color. These values can be set with the following DebugGraphics static methods:
setFlashTime(int flashTime)
setFlashCount(int flashCount)
setFlashColor(Color flashColor)
If we don't disable double-buffering in the RepaintManager (discussed in the next section) we will not see the painting as it occurs:
RepaintManager.currentManager(null).
setDoubleBufferingEnabled(false);
Note: Turning off buffering in the RepaintManager has the effect of ignoring every component's doubleBuffered property.
2. DebugGraphics.LOG_OPTION: This sends messages describing each paint operation as they occur. By default these messages are directed to standard output (the console -- System.out). However, we can change the log destination with DebugGraphics' static setLogStream() method. This method takes a PrintStream parameter. To send output to a file we would do something like the following:
PrintStream debugStream = null;
try {
debugStream = new PrintStream(
new FileOutputStream("JCDebug.txt"));
}
catch (Exception e) {
System.out.println("can't open JCDebug.txt..");
}
DebugGraphics.setLogStream(debugStream);
If at some point we need to change the log stream back to standard output:
DebugGraphics.setLogStream(System.out);
We can insert any string into the log by retreiving it with DebugGraphics' static logStream() method, and then printing into it:
PrintStream ps = DebugGraphics.logStream();
ps.println("\n===> paintComponent ENTERED <===");
Warning: Writing a log to a file will overwrite that file each time we reset the stream.
Each operation is printed with the following syntax:
"Graphics" + (isDrawingBuffer() ? "<B>" : "") +
"(" + graphicsID + "-" + debugOptions + ")"
Each line starts with "Graphics." The isDrawingBuffer() method tells us whether buffering is enabled. If it is, a "<B>" is appended. The graphicsID and debugOptions values are then placed in parenthesis, and separated by a "-". The graphicsID value represents the number of DebugGraphics instances that have been created during the application's lifetime (i.e. it's a static int counter). The debugOptions value represents the current debugging mode:
LOG_OPTION = 1
LOG_OPTION and FLASH_OPTION = 3
LOG_OPTION and BUFFERED_OPTION = 5
LOG_OPTION, FLASH_OPTION, and BUFFERED_OPTION = 7
For example, with logging and flashing enabled, we see output similar to this for each operation:
Graphics(1-3) Setting color: java.awt.Color[r=0,g=255,b=0]
Calls to each Graphics method will get logged when this option is enabled. The above line was generated when a call to setColor()was made.
3. DebugGraphics.BUFFERED_OPTION: This is supposed to pop up a frame showing rendering as it occurs in the offscreen buffer if double-buffereing is enabled. As of the Java 2 FCS this option is not functional.
4. DebugGraphics.NONE_OPTION: This nullifies graphics debugging settings and basically shuts it off.
2.10.2 Graphics debugging caveats
There are several issues to be aware of when using graphics debugging:
1. Graphics debugging will not work for any component whose UI is null. Thus, if you have created a direct JComponent subclass without a UI delegate, as we did with JCanvas above, graphics debugging will simply do nothing. The simplest way to work around this is to define a trivial (empty) UI delegate. We'll show how to do this in the example below.
2. DebugGraphics does not properly clean up after itself. By default, a solid red flash color is used. When a region is flashed, that region is filled in with this red flash color and it does not get erased (it just gets painted over). This presents a problem because transparent rendering will not show up transparent. Instead, it will be alpha-blended with the red below (or whatever the flash color happens to be set to). This is not necessarily a design flaw because there is nothing stopping us from using a completely transparent flash color. With an alpha value of 0 the flash color will never be seen. The only downside is that we don't see any flashing. However, in most cases it is easy to follow what is being drawn if we set the flashTime and flashCount to wait long enough between operations.
2.10.3 Using graphics debugging
We now enable graphics debugging in our JCanvas example from the last two sections. Because we must have a non-null UI delegate, we define a trivial extension of ComponentUI and implement its createUI() method to return a static instance of itself:
class EmptyUI extends ComponentUI
{
private static final EmptyUI sharedInstance = new EmptyUI();
public static ComponentUI createUI(JComponent c) {
return sharedInstance;
}
}
In order to properly associate this UI delegate with JCanvas we simply call super.setUI(EmptyUI.createUI(this)) from the JCanvas constructor. We also set up a PrintStream variable in JCanvas and use it to add a few of our own lines to the log stream during the paintComponent method (to log when the method starts and finishes). Other than this, no changes have been made to the JCanvas's paintComponent() code.
In our test application, TestFrame, we create an instance of JCanvas and enable graphics debugging with the LOG_OPTION and FLASH_OPTION options. We disable buffering in the RepaintManager, set the flash time to 100ms, set the flash count to 2, and use a completely transparent flash color.
The Code: TestFrame.java
see \Chapter1\4
import java.awt.*;
import javax.swing.*;
import javax.swing.plaf.*;
import java.io.*;
class TestFrame extends JFrame
{
public TestFrame() {
super( "Graphics demo" );
JCanvas jc = new JCanvas();
RepaintManager.currentManager(jc).
setDoubleBufferingEnabled(false);
jc.setDebugGraphicsOptions(DebugGraphics.LOG_OPTION |
DebugGraphics.FLASH_OPTION);
DebugGraphics.setFlashTime( 100 );
DebugGraphics.setFlashCount( 2 );
DebugGraphics.setFlashColor(new Color(0,0,0,0));
getContentPane().add(jc);
}
public static void main( String args[] ) {
TestFrame mainFrame = new TestFrame();
mainFrame.pack();
mainFrame.setVisible( true );
}
}
class JCanvas extends JComponent
{
// Unchanged code from section 2.9
private PrintStream ps;
public JCanvas() {
super.setUI(EmptyUI.createUI(this));
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
ps = DebugGraphics.logStream();
ps.println("\n===> paintComponent ENTERED <===");
// All painting code unchanged
ps.println("\n# items repainted = " + c + "/10");
ps.println("===> paintComponent FINISHED <===\n");
}
// Unchanged code from section 2.9
}
class EmptyUI extends ComponentUI
{
private static final EmptyUI sharedInstance = new EmptyUI();
public static ComponentUI createUI(JComponent c) {
return sharedInstance;
}
}
By setting the LOG_OPTION, graphics debugging provides us with a more informative way of checking how well our clipping area optimization (from the last section) works. When this example is run the following output should be seen in your console (assuming you don't obscure JCanvas's visible region as it is painted for the first time):
Graphics(0-3) Enabling debug
Graphics(0-3) Setting color:
javax.swing.plaf.ColorUIResource[r=0,g=0,b=0]
Graphics(0-3) Setting font:
javax.swing.plaf.FontUIResource[family=dialog,name=Dialog,
style=plain,size=12]
===> paintComponent ENTERED <===
Graphics(1-3) Setting color: java.awt.Color[r=255,g=255,b=255]
Graphics(1-3) Filling rect: java.awt.Rectangle[x=0,y=0,
width=400,height=400]
Graphics(1-3) Setting color: java.awt.Color[r=255,g=255,b=0]
Graphics(1-3) Filling oval: java.awt.Rectangle[x=0,y=0,
width=240,height=240]
Graphics(1-3) Setting color: java.awt.Color[r=255,g=0,b=255]
Graphics(1-3) Filling oval:
java.awt.Rectangle[x=160,y=160,width=240,height=240]
Graphics(1-3) Drawing image: sun.awt.windows.WImage@32a5625a at:
java.awt.Point[x=258,y=97]
Graphics(1-3) Drawing image: sun.awt.windows.WImage@32a5625a at:
java.awt.Point[x=98,y=257]
Graphics(1-3) Setting color: java.awt.Color[r=255,g=0,b=0]
Graphics(1-3) Filling rect:
java.awt.Rectangle[x=60,y=220,width=120,height=120]
Graphics(1-3) Setting color: java.awt.Color[r=0,g=255,b=0]
Graphics(1-3) Filling oval:
java.awt.Rectangle[x=140,y=140,width=120,height=120]
Graphics(1-3) Setting color: java.awt.Color[r=0,g=0,b=255]
Graphics(1-3) Filling rect:
java.awt.Rectangle[x=220,y=60,width=120,height=120]
Graphics(1-3) Setting color: java.awt.Color[r=0,g=0,b=0]
Graphics(1-3) Setting font:
java.awt.Font[family=monospaced.bolditalic,name=Mono
spaced,style=bolditalic,size=36]
Graphics(1-3) Drawing string: "Swing" at:
java.awt.Point[x=65,y=129]
Graphics(1-3) Setting font:
java.awt.Font[family=Arial,name=SanSerif,style=plain,size=12]
Graphics(1-3) Drawing string: "is" at:
java.awt.Point[x=195,y=203]
Graphics(1-3) Setting font:
java.awt.Font[family=serif.bold,name=Serif,style=bold,size=24]
Graphics(1-3) Drawing string: "powerful!!" at:
java.awt.Point[x=228,y=286]
# items repainted = 10/10
===> paintComponent FINISHED <===



RSS feed Java FAQ News