O'Reilly logo

Swing Hacks by Chris Adamson, Joshua Marinacci

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

20
|
Chapter 1, Basic JComponents
#4 Display Dates in a Custom Calendar
HACK
Create the Images
I created three images in Photoshop: one for the background, one for each day,
and one for the current day. These are shown in Figures 1-16, 1-17, and 1-18.
I could have separated the day names and the title, but since
they don’t change, it was simpler to make them part of the
image.
A Component to Paint
The easiest way to create a custom component with fancy drawing is to start
off with a
JPanel and override the paintComponent( ) method, as shown in
Example 1-9.
Figure 1-16. calendar.png for the general background
Figure 1-17. day.png for the day backgrounds
Figure 1-18. highlight.png for the current day
Example 1-9. A Calendar base component
public class CalendarHack extends JPanel {
protected Image background, highlight, day_img;
protected SimpleDateFormat month = new SimpleDateFormat("MMMM");

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required