Count points when date is selected.
This commit is contained in:
@@ -57,11 +57,13 @@ public class CalendarPanel extends JPanel {
|
||||
public void setSelectedDates(Set<LocalDate> dates) {
|
||||
selectedDates.clear();
|
||||
selectedDates.addAll(dates);
|
||||
firePropertyChange("selectedDates", null, selectedDates);
|
||||
repaint();
|
||||
}
|
||||
|
||||
public void clearSelection() {
|
||||
selectedDates.clear();
|
||||
firePropertyChange("selectedDates", null, selectedDates);
|
||||
repaint();
|
||||
}
|
||||
|
||||
@@ -71,9 +73,14 @@ public class CalendarPanel extends JPanel {
|
||||
} else {
|
||||
selectedDates.add(date);
|
||||
}
|
||||
firePropertyChange("selectedDates", null, selectedDates);
|
||||
repaint();
|
||||
}
|
||||
|
||||
public double getTotalPoints() {
|
||||
return selectedDates.size() * 1.0;
|
||||
}
|
||||
|
||||
private LocalDate getDateAtPoint(int x, int y) {
|
||||
int row = y / cellHeight;
|
||||
int col = x / cellWidth;
|
||||
@@ -158,9 +165,17 @@ public class CalendarPanel extends JPanel {
|
||||
|
||||
String dayStr = String.valueOf(day);
|
||||
int textX = x + (cellWidth - fm.stringWidth(dayStr)) / 2;
|
||||
int textY = y + (cellHeight - fm.getHeight()) / 2 + fm.getAscent();
|
||||
int textY = y + (cellHeight - fm.getHeight()) / 2 + fm.getAscent() - 8;
|
||||
g.drawString(dayStr, textX, textY);
|
||||
|
||||
String pointsStr = "(1.0)";
|
||||
Font smallFont = new Font("Arial", Font.PLAIN, 10);
|
||||
FontMetrics smallFm = g.getFontMetrics(smallFont);
|
||||
g.setFont(smallFont);
|
||||
int pointsX = x + (cellWidth - smallFm.stringWidth(pointsStr)) / 2;
|
||||
int pointsY = textY + fm.getHeight();
|
||||
g.drawString(pointsStr, pointsX, pointsY);
|
||||
|
||||
col++;
|
||||
if (col == 7) {
|
||||
col = 0;
|
||||
|
||||
@@ -7,6 +7,7 @@ import java.time.YearMonth;
|
||||
public class VacCalc extends JFrame {
|
||||
private CalendarPanel calendarPanel;
|
||||
private JLabel monthLabel;
|
||||
private JLabel pointsLabel;
|
||||
|
||||
public VacCalc() {
|
||||
setTitle("VacCalc");
|
||||
@@ -17,12 +18,16 @@ public class VacCalc extends JFrame {
|
||||
calendarPanel = new CalendarPanel();
|
||||
|
||||
JPanel headerPanel = createHeaderPanel();
|
||||
JPanel footerPanel = createFooterPanel();
|
||||
JPanel mainPanel = new JPanel(new BorderLayout());
|
||||
mainPanel.add(headerPanel, BorderLayout.NORTH);
|
||||
mainPanel.add(calendarPanel, BorderLayout.CENTER);
|
||||
mainPanel.add(footerPanel, BorderLayout.SOUTH);
|
||||
|
||||
add(mainPanel);
|
||||
pack();
|
||||
|
||||
calendarPanel.addPropertyChangeListener("selectedDates", e -> updatePointsLabel());
|
||||
}
|
||||
|
||||
private JPanel createHeaderPanel() {
|
||||
@@ -65,6 +70,21 @@ public class VacCalc extends JFrame {
|
||||
return headerPanel;
|
||||
}
|
||||
|
||||
private JPanel createFooterPanel() {
|
||||
JPanel footerPanel = new JPanel(new BorderLayout());
|
||||
footerPanel.setBackground(new Color(230, 230, 230));
|
||||
footerPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
|
||||
|
||||
pointsLabel = new JLabel();
|
||||
pointsLabel.setFont(new Font("Arial", Font.BOLD, 16));
|
||||
pointsLabel.setHorizontalAlignment(JLabel.CENTER);
|
||||
|
||||
updatePointsLabel();
|
||||
footerPanel.add(pointsLabel, BorderLayout.CENTER);
|
||||
|
||||
return footerPanel;
|
||||
}
|
||||
|
||||
private void updateMonthLabel() {
|
||||
YearMonth current = calendarPanel.getCurrentMonth();
|
||||
monthLabel.setText(String.format("%s %d",
|
||||
@@ -72,6 +92,11 @@ public class VacCalc extends JFrame {
|
||||
current.getYear()));
|
||||
}
|
||||
|
||||
private void updatePointsLabel() {
|
||||
double points = calendarPanel.getTotalPoints();
|
||||
pointsLabel.setText(String.format("Total Points: %.1f", points));
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
SwingUtilities.invokeLater(() -> {
|
||||
VacCalc app = new VacCalc();
|
||||
|
||||
Reference in New Issue
Block a user