Easy to Learn Java: Programming Articles, Examples and Tips

Start with Java in a few days with Java Lessons or Lectures

Home

Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips

Books

Submit News
Search the site here...
Search...
 

Chapter 2. (Introduction) Swing Mechanics. Easy for reading, Click here!

Custom Search
Chapter 2. (Introduction) Swing Mechanics. Easy for reading, Click here!

[ Return to Swing (Book) ]

Page: 10/14 



Previous Page Previous Page (9/14) - Next Page (11/14) Next Page
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 <===



[ Return to Swing (Book) ]


Top 10 read Java Articles
 Get free "1000 Java Tips eBook"

 Java Calendar and Date: good to know facts and code examples

 Array vs ArrayList vs LinkedList vs Vector: an excellent overview and examples

 How can I convert any Java Object into byte array? And byte array to file object

 The Java Lesson 1: What is Java?

 How do I compare two dates and times, date between dates, time between times and

 Maven vs Ant or Ant vs Maven?

 How to open, read, write, close file(s) in Java? Examples on move, rename and de

 Java Array

 Java: JLabel font and color


[ More in News Section ]
Java Lessons

The Java Lesson 1:
What is Java?
The Java Lesson 2:
Anatomy of a simple Java program
The Java Lesson 3:
Identifiers and primitive data types
The Java Lesson 4:
Variables, constants, and literals
The Java Lesson 5:
Arithmetic operations, conversions, and casts
The Java Lesson 6:
Boolean expressions and operations
The Java Lesson 7:
Bitwise operations
The Java Lesson 8:
Flow control with if and else
The Java Lesson 9:
switch statements
The Java Lesson 10:
for, while, and do-while statements
The Java Lesson 11:
Using break and continue
The Java Lesson 12:
Class methods and how they are called
The Java Lesson 13:
Using the Math class
The Java Lesson 14:
Creating and calling custom class methods
The Java Lesson 15:
Overloading class methods
The Java Lesson 16:
An introduction to objects and object references
The Java Lesson 17:
The String class
The Java Lesson 18:
The StringBuffer class
The Java Lesson 19:
Initializing and processing arrays of primitives
The Java Lesson 20:
Initializing and processing arrays of objects
The Java Lesson 23:
Inheritance and overriding inherited methods
The Java Lesson 24:
abstract classes and polymorphism
The Java Lesson 25:
Interfaces, instanceof, and object conversion and casting
The Java Lesson 26:
Introduction to graphical programming and the java.awt packa
The Java Lesson 27:
The Component class
The Java Lesson 28:
Containers and simple layout managers
The Java Lesson 29:
The Color and Font classes
The Java Lesson 30:
Drawing geometric shapes
The Java Lesson 31:
Choice, List, and Checkbox controls
The Java Lesson 32:
Using the Scrollbar graphical control
The Java Lesson 33:
Menus and submenus
The Java Lesson 34:
An introduction to applets and the Applet class
The Java Lesson 35:
Essential HTML to launch an applet and pass it parameters
The Java Lesson 36:
Mouse event processing
Java Lesson 37:
Menus and submenus
Java Lesson 38:
The WindowListener interface and the WindowAdapter class
Java Lesson 39:
An introduction to GridBagLayout
Java Lesson 40:
An introduction to the Java Collections API
Java Lesson 41:
Exception handling with try, catch, and finally blocks
Java Lesson 42:
Claiming and throwing exceptions
Java Lesson 43:
Multithreading, the Thread class, and the Runnable interface
Java Lesson 44:
An introduction to I/O and the File and FileDialog classes
Java Lesson 45:
Low-level and high-level stream classes
Java Lesson 46:
Using the RandomAccessFile class
Java Lessons by
Joh Huhtala: Update

Latest articles
 Java Profiler JProbe to Resolve Performance Problems Faster

 SSL with GlassFish v2, page 5

 SSL with GlassFish v2, page 4

 SSL with GlassFish v2, page 3

 SSL with GlassFish v2, page 2

 The Java Lesson 2: Anatomy of a simple Java program, page 2

 New site about Java for robots and robotics: both software and hardware.

 Exceptions -III: What's an exception and why do I care?

 Exceptions -II: What's an exception and why do I care?

 Exceptions: What's an exception and why do I care?

 Double your Java code quality in 10 minutes, here is receipt

 Murach's Java Servlets and JSP

 How to get ascii code from a char in Java?

 Can we just try without catch? Yes!

 Make Tomcat page load faster

 Make your Tomcat More secure - limit network address for certain IP addresses

 New Java book online starts now here...

 Implementing RESTful Web Services in Java

 Firefox trimming from 1 GB to 40 Mb with many tabs opened

 SSL with GlassFish v2

 My request to replublish Tech Tips

 Search JavaFAQ.nu site here

 New Advanced Installer for Java 6.0 brings XML updates and imports 3rd party MSI

 EJB programming restrictions

 Maven vs Ant or Ant vs Maven?

 Why Java does not use default value which it should?

 How to unsign signed bytes in Java - your guide is here

 The Java Lesson 3: Identifiers and primitive data types. Page 2

 The Java Lesson 7: Bitwise operations with good examples, click here! Page 4

 The Java Lesson 7: Bitwise operations with good examples, click here! Page 3


[ More in News Section ]


Home Code Examples Java Forum All Java Tips Books Submit News, Code... Search... Offshore Software Tech Doodling

RSS feed Java FAQ RSS feed Java FAQ News     

    RSS feed Java Forums RSS feed Java Forums

All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest 1999-2006 by Java FAQs Daily Tips.

Interactive software released under GNU GPL, Code Credits, Privacy Policy