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");

Get Swing Hacks now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.