Calendar
(www.jojoo.net) 2002-1-8 (Please double-click the automatic scroll to watch, click Stop, then hit ..)
The Java program can display the calendar of January after 1582. Users can in one text box
Enter the year, select the month by dropping the box, then click "New Calendar" button, you can display that
Month calendar. This program can be called by adding the following statement in an HTML file.
This program assumes drawing calendars on a 421x481 pixel
Language: Java Platform: UNIX, Windows
*********************************************************** ********************* /
IMPORT JAVA.Applet.Applet;
Import java.awt. *;
Import java.util.date;
Public Class Calendar Extends Applet
{
Static Final Int Ytop = 90; / * Blank on the calendar * /
Static Final Int Yheader = 30; / * The height of the date of the date * /
Static Final Int ncellx = 7; / * Crossing squares * /
Static Final Int Cellsize = 60; / * Checkered Size * /
Static final int mark = 8; / * Digital range from above and the right blank * /
Static final int february = 1; / * Special month in the leap year * /
// The control above the calendar.
Label Yearlabel = New Label ("Year:");
TextField YeartextField = New Textfield ("1996", 5);
Label Monthlabel = New Label ("Month:");
Choice monthchoice = new choice ();
Button newcalbutton = new button ("New Calendar");
// The date object of the current year and the month.
Date now = new date ();
// The control of the control above the calendar.
Font SmallarialFont = New Font ("Arial", Font.Plain, 15);
// Calendar The font of the annual title of the month.
Font LargearialFont = New Font ("Arial", Font.Bold, 30);
String days [] = {"Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "SATURDAY"}
String months [] = {"January", "February", "march", "april",
"May", "June", "July", "August", "September",
"October", "November", "DECEMBER"}; int daysinmonth [] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
// User Enter the year and month.
INT Usermonth;
Int useear;
Public void init ()
/ *
Function: Get the current year and month, initialize the control.
Note: When the program is started, it is automatically called.
* /
{
SetBackground (Color.White);
// Inferten the month and year as the current value.
Usermonth = now.getMonth ();
Useryear = now.getYear () 1900;
// "Year:" tag.
Yearlabel.SetFont (SmallarialFont);
Add (YearLabel);
// Enter the text area of the year.
YeartextField.SetFont (SmallArialFont);
Yeartextfield.Settext (String.Valueof (useerYear);
Add (YeartextField);
// "Month:" tag.
Monthlabel.SetFont (SmallArialFont);
Add (Monthlabel);
// Enter the combo box of the month.
Monthchoice.SETFONT (SmallArialFont);
For (int i = 0; i <12; i )
Monthchoice.Additem (Months [i]);
Monthchoice.select (usermonth);
Add (MONTHCHOICE);
// "New Calendar" press New.
NewcalButton.SetFont (SmallArialFont);
Add (newcalbutton);
} // Initialization
Public void Paint (Graphics G)
/ *
Function: Draw calendars based on global variables UserMonth and Useryear.
Note: When the interface needs to be redrawn, the user can click "New Calendar" to press New, and will also trigger the heavy draw function.
* /
{
FontMetrics FM; / * Get font information * /
Int fontascent; / * Character height * /
INT daypos; / * Date string vertical direction location * /
INT Xsize, Ysize; / * Size of Calendar (Cell) * /
Int NumRows; / * Number of lines (4, 5, 6) * /
INT XNUM, YNUM; / * Number Position At Top Right of Cells * /
INT NUMDAYS; / * The day of the month * /
String daystr; / * Week 1 week string * /
Int marg; / * Margin of Month String Baseline from cell table * /
String capen; / * The month of the center * /
// Get font information.
FM = g.getFontMetrics ();
FONTASCENT = fm.getascent ();
Daypos = YTOP (YHEADER FONTASCENT) / 2;
// Calendar width (cell).
Xsize = ncellx * cellsize;
// HEADER Rectangle Across Top for day names.
g.drawRect (0, YTOP, Xsize, YHEADER);
// Put Days At top of each column, centered.
For (int i = 0; i g.drawstring (days [i], (cellsize-fm.stringwidth (days [i])) / 2 i * Cellsize, Daypos); / / Get the number of rows required for this month Numrows = NumberRowsNeed (UserYear, UserMont); / / The vertical line of the table. Ysize = nuMrows * cellsize; For (int i = 0; i <= xsize; i = cellsize) g.drawline (i, ytop yheader, i, ytop yheader ysize); / / The horizontal line of the table. For (int i = 0, j = ytop yheader; i <= nuMrows; i , J = Cellsize) g.drawline (0, J, Xsize, J); / / The position of the initial number (upper right corner unit). XNum = (Calcfirstofmonth 1) * Cellsize - Margin YNUM = YTOP YHEADER Margin Fontascent // Get the number of days in the month, if it is the second day of the year, add one day. Numdays = daysinmonth [usermonth] ((IsleApyear && (usermonth == february))? 1: 0); // Display the number in the upper right corner of each table. For (int days = 1; day <= numbers; day ) { Daystr = String.Valueof (day); g.drawstring (daystr, xnum - fm.stringwidth (daystr), ynum); XNUM = Cellsize; IF (xnum> xsize) { Xnum = Cellsize - Margin; YNUM = Cellsize; } // if } // for // Set the annual title of the big font. G.SetFont (LargearialFont); // Get font information (current large font). FM = g.getFontMetrics (); // Set the blank header in the vertical direction. Marg = 2 * fm.getdescent (); / / Set the title as the month and in the middle. Caption = months [usermonth] " string.valueof (useerYear); g.drawstring (CAPTION) / 2, YTOP - Marg; } // Draw Public Boolean Action (EVENT E, OBJECT O) / * Function: Brush the global variable of the New Year, when the user tap button to draw. * / { Int UseryEarint; IF (E.TARGET InstanceOf Button) { IF ("New Calendar". Equals (String)))) { // Select the month from the combo box. Usermonth = monthchoice.getSelectedIndex (); // Get the year from the text box. Useryearint = integer.parseint (YeartextField.getText (), 10); IF (Useryearint> 1581) Useryear = userint; // Call Paint () Draw a new calendar. Repaint (); Return True; } } Return False; } INT NumberrowsNeeded (int year, int month) / * Function: Calculate the number of rows required for calendar. Enter: Year = The year given 1582 years later. Month = 0 is in January, 1 is in February, and it is pushed according to the class. Output: Number of lines: 5 or 6, only 28 days in February, simultaneous number 1 is on Sunday, only 4 lines. * / { INT firstday; / * The first day of the month * / INT Numcells; / * Number of tables required for this month * / / * The year should be 1582. * / IF (YEAR <1582) Return (-1); / * Remove invalid month. * / IF ((MONTH <0) || (Month> 11)) Return (-1); / * Get the first day of the month. * / Firstday = Calcfirstofmont (Year, Month); / * If it is the second year of the leap year, the first day is Sunday, returns 4 lines. * / IF ((MONTH == February) && (firstday == 0) &&! isleapyear (year)) Return (4); / * Number of tables required = the first line of empty table the number of days of the month. * / Numcells = firstday daysinmonth [month]; / * The leap year needs to increase one on February 29 * / IF ((Month == February) && (IsleApyear (Year))) Numcells ; / * Less than or equal to 35 square 5 lines, more is 6 lines. * / Return ((Numcells <= 35)? 5: 6); } INT Calcfirstofmonth (int year, int month) / * Function: Calculate the first day of the month is the day. Enter: Year = The year given 1582 years later. Month = 0 is in January, 1 is in February, and it is pushed according to the class. Output: The first day of the month: 0 = Sunday, 1 = Monday, it is pushed. * / { INT firstday; / * On the week of this year, then the first day of the month * / INT i; / * cycle variables of month * / / * The year should be 1582. * / IF (YEAR <1582) Return (-1); / * Remove invalid month. * / IF ((MONTH <0) || (Month> 11)) Return (-1); / * Get the week of January 1, this year * / Firstday = CalcjanuaryFirst (Year); / * The first day of the month is the first few days in the year. * / For (i = 0; i Firstday = daysinmonth [i]; / * Add one day after the month of the year * / IF ((Month> February) && isleApyear (Year)) Firstday ; / * Convert to Week * / Return (Firstday% 7); } Boolean isleApyear (int year) / * Function: Judging whether it is a leap year. Enter: Year = The year given 1582 years later. Output: True is a leap year, False is not. * / { / * Can be unable to be completely removed by 400, not a leap year. * It can be unified by 100, and it is also a year of 400, it is a leap year. * / IF ((Year% 100) == 0) Return ((Year% 400) == 0); / * Can't be divided by 100, it is a year of 4 to complete it. * / Return ((Year% 4) == 0); } Int CalcjanuaryFirst (int year) / * Function: Calculate the week on January 1, this year. Enter: Year = The year given 1582 years later. Output: January 1st Week: 0 = Sunday, 1 = Monday, it is pushed. Note: January 1, 1582 is Friday; 1 year plus 1, leap year plus 2. * / { / * The year should be 1582. * / IF (YEAR <1582) Return (-1); / * Start from 01-01-1582 Friday; 1 year plus 1, leap year plus 2. * / RETURN ((5 (Year - 1582) CalcleApyears (YEAR))% 7); } INT CalcleApyears (int year) / * Function: Calculate the number of leap years since 1582. Enter: Year = The year given 1582 years later. Output: Since 1582, the number of years later. If earlier than 1582, -1 is returned. Note: If the year given is a leap year, the calculation result does not include it. Can be 100 unsteady, the year cannot be tied to 400, not a leap year. It can be more than 100, and it is also a year of 400. It cannot be divided by 100, and the year to be 4 complete is a leap year. * / { INT Leepyears; / * Returns the number of years * / Int hundreds; / * can be tetached than the number of years * / Int FourhundReds; / * can be completely removed from 400 * / / * The year should be 1582. * / IF (YEAR <1582) Return (-1); / * Can be 4 full number * / LeepyEars = (YEAR - 1581) / 4; / * Calculate the number of years that can be completely removed; then remove from the number of leap years * / Hundreds = (YEAR - 1501) / 100; Leepyears - = Hundreds; / * In the number of years, the number of people who can be completely removed * / FourhundReds = (YEAR - 1201) / 400; LeepyEars = Fourhundreds; Return (Leepyears); } }