CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2000
    Posts
    20

    Print Calender using AWT

    Write pgm to dispaly the calender.
    The user specifies the month/year in the text field .Press the show calender button to display the calender for the month of the year in a canvas,
    using drawing method.
    o/p looks like this

    Calender for March 2000

    Sun Mon Tue Wed Thu Fri Sat


    and displays date just like a calender.

    I have written a pgm to display the Calender on
    console. But I dont't know how to diplay on canvas
    and where I can write g.drawString method to display it please help me.

    code to display calender on console

    class PrintCalendar
    {
    public static void main(String[] args)
    {
    //the user enters year and month
    System.out.println("Enter full year");
    int year = MyInput.readInt();
    System.out.println("Enter month in number between 1 and 12");
    int month = MyInput.readInt();

    //print calendar for the month of the year
    printMonth(year, month);
    }

    static void printMonth(int year, int month)
    {
    //get start day of the week for the first date in the month
    int startDay = getStartDay(year, month);

    //get number of days in the month
    int numOfDaysInMonth = getNumOfDaysInMonth(year, month);

    //print headings
    printMonthTitle(year, month);

    //print body
    printMonthBody(startDay, numOfDaysInMonth);
    }

    static int getStartDay(int year, int month)
    {
    //get total number of days since 1/1/1800
    int startDay1800 = 3;
    long totalNumOfDays = getTotalNumOfDays(year, month);

    //return the start day
    return (int)((totalNumOfDays + startDay1800) % 7);
    }

    static long getTotalNumOfDays(int year, int month)
    {
    long total = 0;

    //get the total days from 1800 to year -1
    for (int i = 1800; i < year; i++)
    if (leapYear(i))
    total = total + 366;
    else
    total = total + 365;

    //add days from Jan to the month prior to the calendar month
    for (int i = 1; i < month; i++)
    total = total + getNumOfDaysInMonth(year, i);

    return total;
    }

    static int getNumOfDaysInMonth(int year, int month)
    {
    if (month == 1 || month==3 || month == 5 || month == 7 ||
    month == 8 || month == 10 || month == 12)
    return 31;
    if (month == 4 || month == 6 || month == 9 || month == 11)
    return 30;
    if (month == 2)
    if (leapYear(year))
    return 29;
    else
    return 28;
    return 0; //if month is incorrect.
    }

    static boolean leapYear(int year)
    {
    if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)))
    return true;
    return false;
    }

    static void printMonthBody(int startDay, int numOfDaysInMonth)
    {
    //print padding space before the first day of the month
    int i = 0;

    for (i = 0; i < startDay; i++)
    System.out.print(" ");

    for (i = 1; i <= numOfDaysInMonth; i++)
    {
    if (i < 10)
    System.out.print(" "+i);
    else
    System.out.print(" "+i);

    if ((i + startDay) % 7 == 0)
    System.out.println();
    }

    System.out.println();
    }

    static void printMonthTitle(int year, int month)
    {
    System.out.println(" "+getMonthName(month)+", "+year);
    System.out.println("-----------------------------");
    System.out.println(" Sun Mon Tue Wed Thu Fri Sat");
    }

    static String getMonthName(int month)
    {
    String monthName = null;

    switch (month)
    {
    case 1: monthName = "January"; break;
    case 2: monthName = "February"; break;
    case 3: monthName = "March"; break;
    case 4: monthName = "April"; break;
    case 5: monthName = "May"; break;
    case 6: monthName = "June"; break;
    case 7: monthName = "July"; break;
    case 8: monthName = "August"; break;
    case 9: monthName = "September"; break;
    case 10: monthName = "October"; break;
    case 11: monthName = "November"; break;
    case 12: monthName = "December";
    }

    return monthName;
    }
    }

    // I craeted Frame, textField and button
    import java.awt.*;
    import java.awt.event.*;

    public class CalenderTest extends Frame implements ActionListener
    {
    private TextField tfDate;
    private Button btShow;
    private DisplayMessage c;

    public static void main(String[] args)
    {
    CalenderTest f=new CalenderTest("Calender");
    f.setSize(400,400);
    f.setVisible(true);
    }

    public CalenderTest(String s)
    {
    super(s);
    Panel p=new Panel();
    p.setLayout(new FlowLayout());
    p.add(tfDate=new TextField(15));
    p.add(btShow=new Button("Show Calender"));

    setLayout(new BorderLayout());
    c=new DisplayMessage();

    add("Center",c);
    add("South",p );


    btShow.addActionListener(this);
    }

    public void actionPerformed(ActionEvent e)
    {

    }


    }

    class DisplayMessage extends Canvas
    {
    private String message=" ";

    public DisplayMessage()
    {
    super();
    }
    }

    Please help me to write rest of the code

    Thank you







  2. #2
    Join Date
    Feb 2000
    Posts
    20

    Re: Print Calender using AWT Gurus please help

    You can also use GregorianCalendar()


  3. #3
    Join Date
    Sep 1999
    Location
    Madurai , TamilNadu , INDIA
    Posts
    1,024

    Re: Print Calender using AWT


  4. #4
    Join Date
    Feb 2000
    Posts
    20

    Re: Print Calender using AWT

    Thanks for your help poochi.

    This is my Home work assignment.
    So I must display the calender on canvas
    and I can't use grid bag Layout manager.

    Thanks in advance



  5. #5
    Join Date
    Sep 1999
    Location
    Madurai , TamilNadu , INDIA
    Posts
    1,024

    Re: Print Calender using AWT


    > So I must display the calender on canvas

    Still you can use the sourcecode over there. Instead of extends the CalenderPanel from Panel ,
    extend it from Canvas. If you shouldnt use GridBagLayout, try with Grid Layout.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured