Solving Common Painting Problems

 <  Day Day Up  >  

Problem: I don't know where to put my painting code.

Solution: Custom painting code usually belongs in the paintComponent method of any component descended from JComponent . See How Swing Components Are Displayed (page 130) in Chapter 6 for details.

Problem: The stuff I paint doesn't show up.

Solution 1: Check whether your component is showing up at all. Solving Common Component Problems (page 735) should help you with this.

Solution 2: Check whether repaint is invoked on your component whenever its appearance needs to be updated.

Problem: The background of my applet shows up, but the foreground stuff doesn't show up.

Solution: Did you make the mistake of performing painting directly in a JApplet subclass? If so, then your contents will be covered by the content pane that is automatically created for every JApplet instance. Instead, create another class that performs the painting and then add that class to the JApplet 's content pane. See Chapter 6, Performing Custom Painting (page 129), for more information on how painting in Swing works.

Problem: My component's foreground shows up, but its background is invisible. The result is that one or more components directly behind my component are unexpectedly visible.

Solution 1: Make sure your component is opaque . JPanel s, for example, are opaque by default in many but not all look and feels. To make components such as JLabel s and GTK+ JPanel s opaque, you must invoke setOpaque(true) on them.

Solution 2: If your custom component extends JPanel or a more specialized JComponent descendant, then you can paint the background by invoking super.paintComponent before painting the contents of your component.

Solution 3: You can paint the background yourself using this code at the top of a custom component's paintComponent method:

 g.setColor(getBackground()); g.fillRect(0, 0, getWidth(), getHeight()); g.setColor(getForeground()); 

Problem: I used setBackground to set my component's background color , but it seemed to have no effect.

Solution: Most likely, your component isn't painting its background, either because it's not opaque or because your custom painting code doesn't paint the background. If you set the background color for a JLabel , for example, you must also invoke set-Opaque(true) on the label to make the label's background be painted . For more help, see the preceding problem.

Problem: I'm using the exact same code as in a tutorial example, but it doesn't work. Why?

Solution: Is the code executed in the exact same method as in the tutorial example? For example, if the tutorial example has the code in the its paintComponent method, then this method might be the only place where the code is guaranteed to work.

Problem: How do I paint thick lines or patterns?

Solution: The Java 2D API provides extensive support for implementing line widths and styles, as well as patterns for use in filling and stroking shapes . For more information on using the Java 2D API, see The Java Tutorial's "2D Graphics" trail on the CD at: JavaTutorial/2d/index.html .

Problem: The edges of a particular component look odd.

Solution 1: Did you set the border on a standard component? Because components often update their borders to reflect component state, you generally should avoid invoking setBorder except on JPanel s and custom subclasses of JComponent .

Solution 2: Is the component painted by a look and feel such as GTK+ or Windows XP that uses UI-painted borders instead of Border objects? If so, don't invoke setBorder on the component.

Solution 3: Does the component have custom painting code? If so, does the painting code take the component's insets into account?

Problem: Visual artifacts appear in my GUI.

Solution 1: If you set the background color of a component, be sure the color has no transparency if the component is supposed to be opaque.

Solution 2: Use the setOpaque method to set component opacity if necessary. For example, the content pane must be opaque, but components with transparent backgrounds must not be opaque.

Solution 3: Make sure your custom component fills its painting area completely if it's opaque.

Problem: The performance of my custom painting code is poor.

Solution 1: If you can paint part of your component, use the getClip or getClipBounds method of Graphics to determine which area you need to paint. The less you paint, the faster it will be.

Solution 2: graphics/cd_icon.gif If only part of your component needs to be updated, make paint requests using a version of repaint that specifies the painting region. An example of doing this is in the updateSize method in SelectionDemo . [2]

[2] To run SelectionDemo using Java Web Start, click the SelectionDemo link on the RunExamples/14painting.html page on the CD. You can find the source files here: JavaTutorial/uiswing/14painting/example-1dot4/index.html#SelectionDemo .

Solution 3: For help on choosing efficient painting techniques, look for the word "performance" in the Java 2D API home page online at: http://java.sun.com/products/java-media/2D/index.html.

Problem: The same transforms applied to seemingly identical Graphics objects sometimes have slightly different effects.

Solution: Because the Swing painting code sets the transform (using the Graphics method translate ) before invoking paintComponent , any transforms that you apply have a cumulative effect. This doesn't matter when doing a simple translation, but a more complex AffineTransform , for example, might have unexpected results.

If you don't see your problem in this list, refer to Solving Common Component Problems (page 735) and Solving Common Layout Problems (page 738).

 <  Day Day Up  >  


JFC Swing Tutorial, The. A Guide to Constructing GUIs
The JFC Swing Tutorial: A Guide to Constructing GUIs (2nd Edition)
ISBN: 0201914670
EAN: 2147483647
Year: 2004
Pages: 171

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net