The Java TM Tutorial

| Start of Tutorial > Start of Trail > Start of Lesson | Search

Trail: Creating a GUI with JFC/Swing
Lesson: Working with Graphics

## Solving Common Graphics Problems

> Problem: I don't know where to put my painting code.
> - Painting code belongs in the `paintComponent` method of any component descended from `JComponent`. See Overview of Custom Painting for details.
> Problem: The stuff I paint doesn't show up.
> - Check whether your component is showing up at all. Solving Common Component Problems should help you with this.
> Problem: The background of my applet shows up, but the foreground stuff doesn't show up.
> - 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 Painting 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.
> - Make sure your component is opaque. `JPanel`s, for example, are opaque by default. To make other components such as `JLabel`s opaque, you must invoke `setOpaque(true)` on them.
> - 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.
> - 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.

> - Most likely, your component isn't painting its background, either because it's not opaque or your custom painting code doesn't paint the background. If you set the background color for a `JLabel`, for example, you must also invoke `setOpaque(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 a tutorial example, but it doesn't work. Why?

> - Is the code executed in the exact same method as the tutorial example? For example, if the tutorial example has the code in the example's `paintComponent` method, then this method might be the only place where the code is guaranteed to work.
> Problem: How do I paint thick lines? patterns?
> - The JavaTM 2D API provides extensive support for implementing line widths and styles, as well as patterns for use in filling and stroking shapes. See the 2D Graphics trail for more information on using the Java 2D API.
> If you don't see your problem in this list, see Solving Common Component Problems and Solving Common Layout Problems.

| Start of Tutorial > Start of Trail > Start of Lesson | Search