You might not need to read this lesson at all. Many programs get by with no custom painting. If they display images, they do so using icons in standard Swing components such as labels and buttons. To display styled text, perhaps with embedded images and components, they use text components. To customize the edges of components, they use borders. To change the look of all components, they use a customizable look and feel, such as the GTK+ look and feel.If you can't find a way to make a component look the way you want it to, then read on. This lesson introduces painting concepts, as they apply to Swing components, and then refers you elsewhere so you can write painting code that makes your custom components look great.
Describes how painting happens,
with special attention
to the framework provided by JComponent
.
Gives an overview of the coordinate system, including how borders affect it, and discusses theGraphics
andGraphics2D
classes.
Tells how to implement painting code
within a Swing component
using the paintComponent
method.
Summarizes this lesson and points to where you can find out more.
Describes some common problems related to painting, along with possible solutions to these problems.
Try these questions and exercises to test what you've learned in this lesson.
Note: You can get the old version of this lesson by downloading the Swing trail as of May, 2003.The old version of this lesson featured API introduced in 1.0 and 1.1, including the
Graphics
andMediaTracker
classes. We recommend that you use newer API such as the Java 2D Graphics and Image I/O APIs. The old version of this lesson also had a discussion of animating images. If you think the animation discussion should be updated and made available for easy browsing, please tell us.