Results 1 to 3 of 3
Thread: Creating Calendar in Applet
- 04-08-2008, 10:21 PM #1
Member
- Join Date
- Mar 2008
- Posts
- 14
- Rep Power
- 0
Creating Calendar in Applet
Hey,
I'm writing a program as follows:
Suppose you are writing a program that tells what day of the week someone’s birthday falls on this year. Write a method named getDayOfWeek that takes an int parameter, representing what day of the year it is, and returns the numeric value for “Monday”. For example, for 1999, the first day of the year was on Friday. The thirty-second day of the year (February 1, 1999) was a Monday, so getDayOfWeek(1) should return the numeric value for “Friday” and getDayOfWeek(32) should return the numeric value for “Monday”. (Hint: if you divide the day of the year by 7, the remainder will always be a numeric value between 0 and 6, which can be made to correspond to the days of the week).
As part of this program, you’ll also want a method that takes the month and day as parameters and re-turns what day of the year it is. For example, getDay(1,1) should return 1; getDay (2,1) should return 32; and getDay (12,31) should return 366. (Hint: If the month is 3, and the day is 5, you have to add the number of days in January plus the number of days in February to 5 to get the result: 31 + 28 + 5 = 64.)
Design and develop a Java Applet, named BirthdayApplet, that lets the user input their birth date (month, day and 4-digit year) and displays which day of the week it falls on for the year 2007 along with an indication of whether the year they were born was a leap year or not. Use the getDayOfWeek, getDay, and isLeapYear methods described above.
The applet should contain three separate TextField components for inputting the month, day and year along with Labels indicating their purpose and expected format (i.e. year = xxxx). Once the user has entered values into the text fields, they may select the “Display Birthday” Button to display their birthday for THIS year (2008). Your program should also let the user know if the year they were born was a leap year or not. The format for displaying the birthday follows (i.e. User entered November 24, 2000):
The month name and current year should be displayed in the first row, the weekdays should be labeled in the second row, and the respective calendar for that month displayed beneath. The actual birthday must be “highlighted” in some fashion (i.e. bold, colored, circled, etc.). You may use any text-related GUI component(s) to display the birthday and associated month calendar. You may select the style and size of the text you would like to use for displaying the month calendar, just ensure that the requested birthday is “highlighted“ (identifiable from the other printed values) when complete.Java Code:November 2008 S M T W T F S 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 [B]24[/B] 25 26 27 28 29 30
I'm getting a little lost and confused...here's what I have so far, and I hope someone can give me a few pointers to get this rolling and get on the right path. Thanks.
Java Code:import java.applet.*; import java.awt.*; import java.io.*; import javax.swing.*; import java.awt.event.*; public class BirthdayApplet extends Applet implements ActionListener { public static int WIDTH=500, HEIGHT=500; private Button displayButton; private TextField monthField, dayField, yearField; private AudioClip myAudioClip; private int inputMonth, inputDay, inputYear; private int firstDayofYear = 2, firstDayofMonth = -1; // Creates arrays to store the days in the month and their names. int[] daysINmonth = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; String[] monthNames = {" ", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; int[] daysINweek = {0, 1, 2, 3, 4, 5, 6}; String[] dayNames = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Satruday"}; public void init() { setSize (WIDTH, HEIGHT); // Plays 'Happy Birthday' Song myAudioClip = getAudioClip(getCodeBase(), "../../audio.mid"); myAudioClip.loop(); // Creates the textfields, labels, and buttons. displayButton = new Button ("Display Birthday"); displayButton.addActionListener(this); monthField = new TextField (2); dayField = new TextField (2); yearField = new TextField (4); Label monthLabel = new Label ("Month"); Label dayLabel = new Label (" Day"); Label yearLabel = new Label (" Year"); // Adds the buttons, labels, and textfields to applet. add(monthLabel); add(monthField); add(dayLabel); add(dayField); add(yearLabel); add(yearField); add(displayButton); } public void paint (Graphics g) { g.drawString(monthNames[inputMonth] + " 2008", 200, 100); g.drawString("S" + " " + "M" + " " + "T" + " " + "W" + " " + "R" + " " + "F" + " " + "S", 165, 125); } public void actionPerformed(ActionEvent e) { if (e.getSource() == displayButton) { // Converts inputs from strings to integer values. inputMonth = Integer.parseInt(monthField.getText()); inputDay = Integer.parseInt(dayField.getText()); inputYear = Integer.parseInt(yearField.getText()); repaint(); } } }Last edited by wco5002; 04-08-2008 at 10:54 PM.
- 04-09-2008, 03:40 AM #2
Member
- Join Date
- Mar 2008
- Posts
- 14
- Rep Power
- 0
Alright, I've been working on the prog a little, but I'm still unsure about how to draw the actual calendar days itself. Here's the code:
Java Code:import java.applet.*; import java.awt.*; import java.io.*; import javax.swing.*; import java.awt.event.*; public class BirthdayApplet extends Applet implements ActionListener { public static int WIDTH=500, HEIGHT=500; private Button displayButton; private TextField monthField, dayField, yearField; private AudioClip myAudioClip; private int inputMonth, inputDay, inputYear = 1, dayPrinter = 1; private int firstDayofYear = 2, firstDayofMonth = -1, firstDayofWeek = 2; private String year = "", days = ""; // Creates arrays to store the days in the month and their names. int[] daysINmonth = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; String[] monthNames = {" ", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; int[] daysINweek = {0, 1, 2, 3, 4, 5, 6}; String[] daysOFweek = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; // Initializes the applet public void init() { setSize (WIDTH, HEIGHT); // Plays 'Happy Birthday' Song myAudioClip = getAudioClip(getCodeBase(), "../../audio.mid"); myAudioClip.loop(); // Creates the textfields, labels, and buttons. displayButton = new Button ("Display Birthday"); displayButton.addActionListener(this); monthField = new TextField (2); dayField = new TextField (2); yearField = new TextField (4); Label monthLabel = new Label ("Month"); Label dayLabel = new Label (" Day"); Label yearLabel = new Label (" Year"); // Adds the buttons, labels, and textfields to applet. add(monthLabel); add(monthField); add(dayLabel); add(dayField); add(yearLabel); add(yearField); add(displayButton); } // Determines if the input year is a leap year. private static boolean isLeapYear(int year) { if(((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) { return true; } else { return false; } } // Finds the Julian Day equivalency of inputs. private int getDay(int month, int year) { int jday = 0; for (int i = 1; i < inputMonth; i++) { jday = jday + daysINmonth[i]; } jday = jday + inputDay; return jday; } // Finds which day of the week birthday falls. private int getDayOfWeek(int jday) { return (jday + firstDayofWeek - 1)%7; } // Paints calendar and leap year (if applicable). public void paint (Graphics g) { if (isLeapYear(inputYear) == true) { g.drawString("The year you were born was a leap year!", 140, 400); } if (inputMonth > 3 && inputMonth < 8) { g.drawString(" " + monthNames[inputMonth] + " " + year, 205, 200); } else g.drawString(monthNames[inputMonth] + " " + year, 205, 200); g.drawString(days, 170, 225); } public void actionPerformed(ActionEvent e) { if (e.getSource() == displayButton) { // Converts inputs from strings to integer values. inputMonth = Integer.parseInt(monthField.getText()); inputDay = Integer.parseInt(dayField.getText()); inputYear = Integer.parseInt(yearField.getText()); year = "2008"; days = "S " + "M " + "T " + "W " + "T " + "F " + "S"; repaint(); } } }
- 04-09-2008, 04:21 AM #3
Member
- Join Date
- Mar 2008
- Posts
- 14
- Rep Power
- 0
Alright I've gotten everything working except for the actual print out of the dates in the month. I'll post the full code again, as I have it completed thus far. Any pointers are GREATLY appreciated.
Java Code:import java.applet.*; import java.awt.*; import java.io.*; import javax.swing.*; import java.awt.event.*; public class BirthdayApplet extends Applet implements ActionListener { public static int WIDTH=500, HEIGHT=500; private Button displayButton; private TextField monthField, dayField, yearField; private AudioClip myAudioClip; private int inputMonth, inputDay, inputYear = 1, dayPrinter = 1; private int firstDayofYear = 2, firstDayOfMonth = -1, firstDayofWeek = 2; private String year = "", days = ""; // Creates arrays to store the days in the month and their names. int[] daysINmonth = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; String[] monthNames = {" ", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; int[] daysINweek = {0, 1, 2, 3, 4, 5, 6}; String[] daysOFweek = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; // Initializes the applet public void init() { setSize (WIDTH, HEIGHT); // Plays 'Happy Birthday' Song myAudioClip = getAudioClip(getCodeBase(), "../../audio.mid"); myAudioClip.loop(); // Creates the textfields, labels, and buttons. displayButton = new Button ("Display Birthday"); displayButton.addActionListener(this); monthField = new TextField (2); dayField = new TextField (2); yearField = new TextField (4); Label monthLabel = new Label ("Month"); Label dayLabel = new Label (" Day"); Label yearLabel = new Label (" Year"); // Adds the buttons, labels, and textfields to applet. add(monthLabel); add(monthField); add(dayLabel); add(dayField); add(yearLabel); add(yearField); add(displayButton); } // Determines if the input year is a leap year. private static boolean isLeapYear(int year) { if(((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) { return true; } else { return false; } } // Finds the Julian Day equivalency of inputs. private int getDay(int month, int year) { int jday = 0; for (int i = 1; i < inputMonth; i++) { jday = jday + daysINmonth[i]; } jday = jday + inputDay; return jday; } // Finds which day of the week birthday falls. private int getDayOfWeek(int jday) { return (jday + firstDayofWeek - 1)%7; } // Paints calendar and leap year (if applicable). public void paint (Graphics g) { if (isLeapYear(inputYear) == true) { g.drawString("The year you were born was a leap year!", 140, 300); } if (inputMonth > 3 && inputMonth < 8) { g.drawString(" " + monthNames[inputMonth] + " " + year, 205, 100); } else g.drawString(monthNames[inputMonth] + " " + year, 205, 100); g.drawString(days, 170, 125); for (int weeks = 1; weeks < 7; weeks++) { for (int day = 0; day < 7; day++) { if (weeks == 1 && day < firstDayOfMonth) { g.drawString(" ", 170, 150); } else { if (dayPrinter < 31) { String dayPrinter2 = String.valueOf(dayPrinter); g.drawString(" " + dayPrinter2 + " ", 170, 150); dayPrinter = Integer.parseInt(dayPrinter2.trim()); dayPrinter++; } } } } } public void actionPerformed(ActionEvent e) { if (e.getSource() == displayButton) { // Converts inputs from strings to integer values. inputMonth = Integer.parseInt(monthField.getText()); inputDay = Integer.parseInt(dayField.getText()); inputYear = Integer.parseInt(yearField.getText()); year = "2008"; days = "S " + "M " + "T " + "W " + "T " + "F " + "S"; repaint(); } } }
Similar Threads
-
calendar
By John in forum SWT / JFaceReplies: 12Last Post: 08-07-2008, 10:54 PM -
Calendar.DATE
By mew in forum New To JavaReplies: 1Last Post: 01-04-2008, 07:51 PM -
Problem with calendar
By Felissa in forum Advanced JavaReplies: 2Last Post: 07-01-2007, 08:39 PM -
Web calendar
By Daniel in forum Enterprise JavaBeans (EJB)Replies: 1Last Post: 06-27-2007, 05:36 PM -
Applet, To center text and To open I engage in a dialog in an Applet
By Marcus in forum Java AppletsReplies: 4Last Post: 06-08-2007, 06:15 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks