-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDayGrid.java
More file actions
144 lines (131 loc) · 3.55 KB
/
Copy pathDayGrid.java
File metadata and controls
144 lines (131 loc) · 3.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package Calendar;
import java.awt.Color;
import java.awt.Graphics;
import java.util.*;
public class DayGrid {
//method prints everything together to draw out the grid
public DayGrid(Graphics g, int month, int year) {
//Draw Table
drawTable(g);
//Calculate Date
int dayInmonth = daysInMonth(month, year);
calculateDate(g, month, year, dayInmonth);
}
public static void drawTable(Graphics g) {
int x = 10;
int y = 40;
int width = 40;
int height = 20;
g.setColor(Color.BLACK);
//insert the String "SUN MON TUES WED THU FRI SAT SUN"
g.drawString("SUN MON TUE WED THU FRI SAT",x,y-5);
//draw table
for(int i = 1; i <= 6; i++) {
x = 0;
for(int j = 1; j <= 7; j++) {
g.drawRect(x, y, width, height);
x += width;
}
y += height;
}
}
//method returns amount of days in each month
public static int daysInMonth(int month, int year) {
if((month == 2) && (year % 4 == 0)) {
return 29;
}
else if((month == 2) && (year % 4 != 0)) {
return 28;
}
else if(month == 4 || month == 6 || month == 9 || month == 11) {
return 30;
}
else {
return 31;
}
}
//method draws out the the calandar using known variable
public static void calculateDate(Graphics g, int month, int year, int dayInmonth) {
int x = 17;
int y = 48;
int width = 40;
int height = 20;
int numberOfyear = (year-1) - 1899;
int numberOfleapyear = numberOfyear / 4;
int numberbymonth = calculateDaybymonth(month, year);
int numberOfday = calculateNumberofday(year, numberOfyear, numberOfleapyear, numberbymonth);
int dayofWeek = numberOfday%7;
int spaces = dayofWeek -1;
if(spaces > 0) {
if(spaces > 7) {
spaces = spaces - 7;
for (int i = 0; i <= spaces-1; i++) {
x = 20;
y = 64;
g.drawString(" ", x, y);
x += width;
}
}
}
else if (spaces < 0) {
spaces = 7 + spaces;
for (int i = 0; i <= spaces-1; i++) {
x = 20;
y = 48;
g.drawString(" ", x, y);
x += width;
}
}
x = width * spaces;
for (int i = 1; i <= 7 - spaces; i++) {
String a = Integer.toString(i);
g.drawString(a, x + 18, y + 8);
x += width;
}
x = 17;
y += height;
int count = 1;
for (int i = 8 - spaces; i <= dayInmonth; i++) {
if(count == 1) {
String a = Integer.toString(i);
g.drawString(a, x, y + 8);
}
if(count <= 7 && count != 1) {
String a = Integer.toString(i);
g.drawString(a, x + 40 , y + 8);
x += width;
}
if(count > 7) {
x = 17;
y += height;
String a = Integer.toString(i);
g.drawString(a, x, y + 8);
count = 1;
}
count++;
}
}
//method calculates the amount of days in the year including any leap year
public static int calculateNumberofday(int year, int numberOfyear, int numberOfleapyear, int numberbymonth) {
int numberOfday;
//if statement checks if the given year is a leap year or not
if(year%4!=0) {
return numberOfday = (numberOfyear - numberOfleapyear)*365 + (numberOfleapyear+1)*366 + numberbymonth;
}
else {
return numberOfday = (numberOfyear - numberOfleapyear)*365 + (numberOfleapyear+1)*366 + numberbymonth -1;
}
}
//method calculates amount of days throughout the month
//for example month 1 to month 2 would be 31 days and so forth
public static int calculateDaybymonth (int month, int year) {
if(year % 4 == 0) {
int[] dayByMonth = {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 355};
return dayByMonth[month-1];
}
else {
int[] dayByMonth = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 354};
return dayByMonth[month-1];
}
}
}