Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
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

                         

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

                         

» View all related tutorials
Related Tags: c memory image class images interface io colors sed color get implementation display icon height value source width int id

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.