Maria4
March 7th, 2000, 09:23 AM
Write pgm to dispaly the calender.
This is my Home Work assignment. and I can't use gridbag Layout manager.
Q
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.
I must display the calender on Canvas.
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;
}
}
// Canvas,TextField and Button to dispaly the Calender
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();
}
}
Q 2
When user press the show button to display the calender for the month of the year. Each day of the calender is shown on a button and the weekday names are labels.
Place all the buttons for the calender in a panel using gridLayout.
//Code to create Buttons and textField
public class BCalender extends Frame implements ActionListener
{
private TextField tfDate;
private Button btShow;
private Button bt[]=new Button[35];
public static void main(String[] args)
{
BCalender f=new BCalender("Calender");
f.setSize(300,300);
f.setVisible(true);
}
public BCalender(String s)
{
super(s);
Panel p1=new Panel();
p1.setLayout(new GridLayout(5,7));
for(int i=0;i<=34;i++)
p1.add(bt[i]=new Button());
Panel p2=new Panel();
p2.setLayout(new FlowLayout());
p2.add(tfDate=new TextField(15));
p2.add(btShow=new Button("Show Calender"));
setLayout(new BorderLayout());
add("Center",p1);
add("South",p2 );
btShow.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
}
}
In this program even I have to display previos month and next month days
Ex:
Sun Mon Tue Wed Thu Fri Sat
27* 28* 29* 1 so on
5 * * 6* 7* 8
26 * 27* 28* 29* 30* 31* 1
'*' rep blank places
I dont't know where to write g.String method to display the calender Please Help
Please help me to write rest of the code
Thank you
This is my Home Work assignment. and I can't use gridbag Layout manager.
Q
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.
I must display the calender on Canvas.
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;
}
}
// Canvas,TextField and Button to dispaly the Calender
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();
}
}
Q 2
When user press the show button to display the calender for the month of the year. Each day of the calender is shown on a button and the weekday names are labels.
Place all the buttons for the calender in a panel using gridLayout.
//Code to create Buttons and textField
public class BCalender extends Frame implements ActionListener
{
private TextField tfDate;
private Button btShow;
private Button bt[]=new Button[35];
public static void main(String[] args)
{
BCalender f=new BCalender("Calender");
f.setSize(300,300);
f.setVisible(true);
}
public BCalender(String s)
{
super(s);
Panel p1=new Panel();
p1.setLayout(new GridLayout(5,7));
for(int i=0;i<=34;i++)
p1.add(bt[i]=new Button());
Panel p2=new Panel();
p2.setLayout(new FlowLayout());
p2.add(tfDate=new TextField(15));
p2.add(btShow=new Button("Show Calender"));
setLayout(new BorderLayout());
add("Center",p1);
add("South",p2 );
btShow.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
}
}
In this program even I have to display previos month and next month days
Ex:
Sun Mon Tue Wed Thu Fri Sat
27* 28* 29* 1 so on
5 * * 6* 7* 8
26 * 27* 28* 29* 30* 31* 1
'*' rep blank places
I dont't know where to write g.String method to display the calender Please Help
Please help me to write rest of the code
Thank you