FileDocCategorySizeDatePackage
CalIcon.javaAPI DocExample4532Thu May 29 14:06:32 BST 2003com.darwinsys.swingui

CalIcon

public class CalIcon extends JComponent implements Icon
Display one of those standard Calendar Page icons with Weekday, Day and Month. Can be used as the Icon in a JButton. Can include or exclude an updating Clock at the top (invoke constructor with value of true to include). However, it should be excluded when using as an Icon, and true when using as a Component.
author
Ian Darwin, http://www.darwinsys.com
version
$Id: CalIcon.java,v 1.3 2003/05/29 18:06:31 ian Exp $

Fields Summary
protected final int
SIZE
The size shalle be 64x64.
protected final Dimension
d
protected final int
RBW
The size of the inner white box
protected final int
RBH
protected final int
RBX
The x location of the inner box
protected final int
RBY
The y location of the inner box
protected Calendar
myCal
Our Calendar
protected boolean
showTime
True if user wants the time shown
protected Clock
clock
The Clock to show the time, if showTime
protected Font
dayNumbFont
Font for displaying the time
protected FontMetrics
dayNumbFM
FontMetrics for displaying the time
protected Font
dayNameFont
Font for displaying the time
protected FontMetrics
dayNameFM
FontMetrics for displaying the time
protected Font
monNameFont
Font for displaying the time
protected FontMetrics
monNameFM
FontMetrics for displaying the time
public String[]
days
Days of the week
public String[]
mons
Constructors Summary
public CalIcon(boolean showT)
Construct the object with default arguments


	       
	   
		this(Calendar.getInstance(), showT);
	
public CalIcon(Calendar c, boolean showT)
Construct the object with a Calendar object

		super();
		showTime = showT;
		myCal = c;

		setLayout(null);			// we don't need another layout, ...

		if (showTime) {
			// System.err.println("Constructing and adding Clock");
			clock = new Clock();
			add(clock);
			clock.setBounds(0, 2, SIZE, 10);
			// clock.setBackground(Color.black);
			// clock.setForeground(Color.green);
			RBY = d.height - (RBH+(showTime?12:0)/2);
		} else {
			RBY = 6;
		}
		RBX = 12;	// raised box x offset
		// System.err.println("RBX, RBY = " + RBX + "," + RBY);

		dayNumbFont = new Font("Serif",     Font.BOLD, 20);
		dayNumbFM = getFontMetrics(dayNumbFont);
		dayNameFont = new Font("SansSerif", Font.PLAIN, 10);
		dayNameFM = getFontMetrics(dayNameFont);
		monNameFont = new Font("SansSerif", Font.ITALIC, 10);
		monNameFM = getFontMetrics(monNameFont);
	
Methods Summary
public intgetIconHeight()

 return SIZE; 
public intgetIconWidth()

 return SIZE; 
public java.awt.DimensiongetMinimumSize()

		return d;
	
public java.awt.DimensiongetPreferredSize()

		return d;
	
public static voidmain(java.lang.String[] args)

		JFrame jf = new JFrame("Calendar");
		Container cp = jf.getContentPane();
		cp.setLayout(new GridLayout(0,1,5,5));
		CalIcon c = new CalIcon(true);
		cp.add(c);
		JButton j = new JButton("As Icon", new CalIcon(false));
		cp.add(j);
		jf.pack();
		jf.setVisible(true);
	
public voidpaint(java.awt.Graphics g)
Paint: draw the calendar page in the JComponent. Delegates most work to paintIcon().


	             	 
	    

		paintIcon(this, g, 0, 0);
	
public voidpaintIcon(java.awt.Component c, java.awt.Graphics g, int x, int y)
paintIcon: draw the calendar page.


		// Allow clock to get painted (voodoo magic)
		if (showTime)
			super.paint(g);

		// Outline it.
		g.setColor(Color.black);
		g.draw3DRect(x, y, d.width-2, d.height-2, true);

		// Show the date: First, a white page with a drop shadow.
		g.setColor(Color.gray);
		g.fillRect(x + RBX+3, y + RBY+3, RBW, RBH);
		g.setColor(Color.white);
		g.fillRect(x + RBX, y + RBY, RBW, RBH);

		// g.setColor(getForeground());
		g.setColor(Color.black);

		String s = days[myCal.get(Calendar.DAY_OF_WEEK)-1];
		g.setFont(dayNameFont);
		int w = dayNameFM.stringWidth(s);
		g.drawString(s, x + RBX+((RBW-w)/2), y + RBY+10);

		s = Integer.toString(myCal.get(Calendar.DAY_OF_MONTH));
		g.setFont(dayNumbFont);
		w = dayNumbFM.stringWidth(s);
		g.drawString(s, x + RBX+((RBW-w)/2), y + RBY+25);

		s = mons[myCal.get(Calendar.MONTH)];
		g.setFont(monNameFont);
		w = monNameFM.stringWidth(s);
		g.drawString(s, x + RBX+((RBW-w)/2), y + RBY+35);