Java Reference
In-Depth Information
g.translate (left, top);
for (int i = 0; i < 8; i++)
g.drawLine
(cellWidth * i, cellHeight,
cellWidth * i, 7 * cellHeight - 1);
for (int i = 1; i < 8; i++)
g.drawLine
(0, cellHeight * i,
totalWidth - 1, cellHeight * i);
FontMetrics font = g.getFontMetrics();
int textHeight = font.getHeight();
int ascent = font.getAscent();
for (int i = 0; i < 7; i++) {
String name = days [i];
int textWidth = font.stringWidth(name);
g.drawString
(name, cellWidth * i + (cellWidth - textWidth) / 2,
cellHeight - 1);
}
int day = calendar.get (Calendar.DAY_OF_MONTH);
calendar.set(Calendar.DAY_OF_MONTH, 1);
firstDay = calendar.get(Calendar.DAY_OF_WEEK) - 1;
calendar.set(Calendar.DAY_OF_MONTH, day);
int dayOfWeek = firstDay;
int days = daysInMonth (calendar);
int weekOfMonth = 1;
for (int i = 1; i <= days; i++) {
String s = String.valueOf(i);
int textWidth = font.stringWidth(s);
int cellLeft = cellWidth * dayOfWeek;
int cellTop = cellHeight * weekOfMonth;
if (i == day) {
g.fillRect (cellLeft, cellTop,
cellWidth, cellHeight);
g.setColor (Color.white);
}
g.drawString (s,
cellLeft + (cellWidth - textWidth) / 2,
cellTop + ascent + (cellHeight - textHeight)
/ 2);
g.setColor (Color.black);
dayOfWeek++;
if (dayOfWeek == 7) {
weekOfMonth++;
dayOfWeek = 0;
}
}
Search WWH ::




Custom Search