Show Calendar Icon

An icon is a pictorial image used in a graphical user interface to represent a program, a command, a link to a Web page, etc.

Show Calendar Icon

An icon is a pictorial image used in a graphical user interface to represent a program, a command, a link to a Web page, etc.

Show Calendar Icon

Show Calendar Icon

     

This section illustrates you how to draw the calendar icon.

An icon is a pictorial image used in a graphical user interface to represent a program, a command, a link to a Web page, etc. To show calendar icon, we have used the class Calendar. The Calendar class provides methods for converting a specific instant in time and a set of calendar fields, like year, month, day, hour etc, and for manipulating the calendar fields.

 The Font class defines the font for the date, day and month. The object of class FontMetrics encapsulates information to render  a particular font on a particular screen. The Icon class gives the method paintIcon() which describes the structure of icon.

The cal.get(Calendar.DAY_OF_WEEK) returns the day of the week. The Integer.toString (cal.get (Calendar . DAY_OF_MONTH)) converts the integer into string and returns the date of the month. The cal.get(Calendar.MONTH) returns the month of the year.

 The method stringWidth(st) calculates the width of the entire string in pixels. The drawString() method draws the specified text string at the specified location with the specified font.

Here is the code of CalendarIconExample.java

import java.awt.*;
import javax.swing.*;
import java.util.Calendar;
import java.text.DecimalFormat;

public class CalendarIconExample extends JComponent {
  int SIZE = 60;
  Dimension dim = new Dimension(SIZE, SIZE);
  int nx, ny, width = 38, height = 38;
  Calendar cal;
  Font dateFont, dayFont, monthFont;
  FontMetrics date, day, month ;
  boolean showTime = true;
  String[] days ={"SUN""MON""TUE""WED""THU""FRI""SAT"};
  String[] months={"JAN""FEB""MAR""APR""MAY""JUN""JUL",
  "AUG""SEP""OCT""NOV""DEC", };

  public CalendarIconExample(boolean show) {
  this(Calendar.getInstance(), show);
  }
  public CalendarIconExample(Calendar c, boolean show) {
  super();
  cal = c; 
  ny = 5;
  nx = 10
  dateFont = new Font("Serif", Font.BOLD, 18);
  date = getFontMetrics(dateFont);
  dayFont = new Font("Book Antiqua", Font.BOLD, 10);
  day = getFontMetrics(dayFont);
  monthFont = new Font("Book Antiqua", Font.BOLD, 10);
  month = getFontMetrics(monthFont);
  }
  public void paint(Graphics graphics) {
 paintIcon(this, graphics, 00);
  }
  public void paintIcon(Component component, Graphics g,
   
int x, int y) {
    g.drawRect(x, y, dim.width - 2, dim.height - 2);
  g.setColor(Color.gray);
  g.fillRect(x + nx + 3, y + ny + 3, width, height);
  g.setColor(Color.white);
  g.fillRect(x + nx, y + ny, width, height);
  g.setColor(Color.black);
  if (showTime)
 super.paint(g);
  
  String st = days[cal.get(Calendar.DAY_OF_WEEK) - 1];
  g.setFont(dayFont);
  g.setColor(Color.red);
  int w = day.stringWidth(st);
  g.drawString(st, x + nx + ((width - w) / 2), y + ny + 10);

  st = Integer.toString(cal.get(Calendar.DAY_OF_MONTH));
  g.setFont(dateFont);
  g.setColor(Color.black);
  w = date.stringWidth(st);
  g.drawString(st, x + nx + ((width - w) / 2), y + ny + 25);

  st = months[cal.get(Calendar.MONTH)];
  g.setFont(monthFont);
  g.setColor(Color.red);
  w = month.stringWidth(st);
  g.drawString(st, x + nx + ((width - w) / 2), y + ny + 35);
}
 public static void main(String[] args) {
  JFrame frame = new JFrame("Calendar");
  Container container= frame.getContentPane();
  CalendarIconExample iconExample = new CalendarIconExample(true);
  container.add(iconExample);
  frame.setSize(100100);
  frame.show();
  }
}

Output will be displayed as:

Download Source Code