View Single Post
  #2 (permalink)  
Old 04-09-2008, 04:40 AM
wco5002 wco5002 is offline
Member
 
Join Date: Mar 2008
Posts: 14
wco5002 is on a distinguished road
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:

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(); } } }
Reply With Quote