Count points when date is selected.

This commit is contained in:
2026-02-27 13:07:34 +01:00
parent 3ab7b5175f
commit 9175f6d6ca
2 changed files with 41 additions and 1 deletions

View File

@@ -57,11 +57,13 @@ public class CalendarPanel extends JPanel {
public void setSelectedDates(Set<LocalDate> dates) { public void setSelectedDates(Set<LocalDate> dates) {
selectedDates.clear(); selectedDates.clear();
selectedDates.addAll(dates); selectedDates.addAll(dates);
firePropertyChange("selectedDates", null, selectedDates);
repaint(); repaint();
} }
public void clearSelection() { public void clearSelection() {
selectedDates.clear(); selectedDates.clear();
firePropertyChange("selectedDates", null, selectedDates);
repaint(); repaint();
} }
@@ -71,9 +73,14 @@ public class CalendarPanel extends JPanel {
} else { } else {
selectedDates.add(date); selectedDates.add(date);
} }
firePropertyChange("selectedDates", null, selectedDates);
repaint(); repaint();
} }
public double getTotalPoints() {
return selectedDates.size() * 1.0;
}
private LocalDate getDateAtPoint(int x, int y) { private LocalDate getDateAtPoint(int x, int y) {
int row = y / cellHeight; int row = y / cellHeight;
int col = x / cellWidth; int col = x / cellWidth;
@@ -158,9 +165,17 @@ public class CalendarPanel extends JPanel {
String dayStr = String.valueOf(day); String dayStr = String.valueOf(day);
int textX = x + (cellWidth - fm.stringWidth(dayStr)) / 2; 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); 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++; col++;
if (col == 7) { if (col == 7) {
col = 0; col = 0;

View File

@@ -7,6 +7,7 @@ import java.time.YearMonth;
public class VacCalc extends JFrame { public class VacCalc extends JFrame {
private CalendarPanel calendarPanel; private CalendarPanel calendarPanel;
private JLabel monthLabel; private JLabel monthLabel;
private JLabel pointsLabel;
public VacCalc() { public VacCalc() {
setTitle("VacCalc"); setTitle("VacCalc");
@@ -17,12 +18,16 @@ public class VacCalc extends JFrame {
calendarPanel = new CalendarPanel(); calendarPanel = new CalendarPanel();
JPanel headerPanel = createHeaderPanel(); JPanel headerPanel = createHeaderPanel();
JPanel footerPanel = createFooterPanel();
JPanel mainPanel = new JPanel(new BorderLayout()); JPanel mainPanel = new JPanel(new BorderLayout());
mainPanel.add(headerPanel, BorderLayout.NORTH); mainPanel.add(headerPanel, BorderLayout.NORTH);
mainPanel.add(calendarPanel, BorderLayout.CENTER); mainPanel.add(calendarPanel, BorderLayout.CENTER);
mainPanel.add(footerPanel, BorderLayout.SOUTH);
add(mainPanel); add(mainPanel);
pack(); pack();
calendarPanel.addPropertyChangeListener("selectedDates", e -> updatePointsLabel());
} }
private JPanel createHeaderPanel() { private JPanel createHeaderPanel() {
@@ -65,6 +70,21 @@ public class VacCalc extends JFrame {
return headerPanel; 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() { private void updateMonthLabel() {
YearMonth current = calendarPanel.getCurrentMonth(); YearMonth current = calendarPanel.getCurrentMonth();
monthLabel.setText(String.format("%s %d", monthLabel.setText(String.format("%s %d",
@@ -72,6 +92,11 @@ public class VacCalc extends JFrame {
current.getYear())); current.getYear()));
} }
private void updatePointsLabel() {
double points = calendarPanel.getTotalPoints();
pointsLabel.setText(String.format("Total Points: %.1f", points));
}
public static void main(String[] args) { public static void main(String[] args) {
SwingUtilities.invokeLater(() -> { SwingUtilities.invokeLater(() -> {
VacCalc app = new VacCalc(); VacCalc app = new VacCalc();