-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBirthdayApp.java
159 lines (135 loc) · 5.97 KB
/
BirthdayApp.java
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Happy Birthday, M!
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import javax.imageio.ImageIO;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import java.awt.image.BufferedImage;
public class BirthdayApp extends JFrame {
private JLabel recipeLabel;
private int recipeIndex = 0;
private String[] recipes = {
"C:\\Users\\edenc\\PersonalProjects\\Java\\slide1.jpg",
"C:\\Users\\edenc\\PersonalProjects\\Java\\slide2.jpg",
"C:\\Users\\edenc\\PersonalProjects\\Java\\slide3.jpg",
"C:\\Users\\edenc\\PersonalProjects\\Java\\slide4.jpg",
"C:\\Users\\edenc\\PersonalProjects\\Java\\slide5.jpg",
"C:\\Users\\edenc\\PersonalProjects\\Java\\slide6.jpg",
"C:\\Users\\edenc\\PersonalProjects\\Java\\slide7.jpg",
"C:\\Users\\edenc\\PersonalProjects\\Java\\slide8.jpg",
"C:\\Users\\edenc\\PersonalProjects\\Java\\slide9.jpg",
"C:\\Users\\edenc\\PersonalProjects\\Java\\slide10.jpg",
"C:\\Users\\edenc\\PersonalProjects\\Java\\slide11.jpg",
"C:\\Users\\edenc\\PersonalProjects\\Java\\slide12.jpg",
"C:\\Users\\edenc\\PersonalProjects\\Java\\slide13.jpg"
};
private JLabel countdownLabel;
private LocalDate anniversaryDate = LocalDate.of(2025, 10, 16);
public BirthdayApp() {
// Set up the frame
setTitle("Happy Birthday, M!");
setSize(1800, 900);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new BorderLayout());
// Slideshow area
recipeLabel = new JLabel();
recipeLabel.setHorizontalAlignment(JLabel.CENTER); // Center the image
recipeLabel.setVerticalAlignment(JLabel.CENTER); // Center vertically
updateRecipeImage();
add(recipeLabel, BorderLayout.CENTER);
// Buttons for navigating slideshow
JPanel buttonPanel = new JPanel();
JButton prevButton = new JButton("Previous Slide");
JButton nextButton = new JButton("Next Slide");
buttonPanel.add(prevButton);
buttonPanel.add(nextButton);
add(buttonPanel, BorderLayout.SOUTH);
// Countdown area
countdownLabel = new JLabel("Days until Engagement Anniversary: " + getDaysUntilAnniversary());
countdownLabel.setFont(new Font("Arial", Font.BOLD, 20));
countdownLabel.setHorizontalAlignment(JLabel.CENTER);
add(countdownLabel, BorderLayout.NORTH);
// Music Button
JButton playMusicButton = new JButton("Play Music");
buttonPanel.add(playMusicButton);
// Button actions
prevButton.addActionListener(e -> {
recipeIndex = (recipeIndex - 1 + recipes.length) % recipes.length;
updateRecipeImage();
});
nextButton.addActionListener(e -> {
recipeIndex = (recipeIndex + 1) % recipes.length;
updateRecipeImage();
});
playMusicButton.addActionListener(e -> {
new Thread(() -> playMusic("C:\\Users\\edenc\\PersonalProjects\\Java\\wfil2.wav")).start(); // Run music in a separate thread
});
// Start countdown timer
Timer timer = new Timer(1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
countdownLabel.setText("Days until Engagement Anniversary: " + getDaysUntilAnniversary());
}
});
timer.start();
}
// Method to update the recipe image in the slideshow with high-quality scaling
private void updateRecipeImage() {
try {
// Read the original image
BufferedImage originalImage = ImageIO.read(new File(recipes[recipeIndex]));
// Define the target width and height (90% of window size, so 720x540)
int targetWidth = 1500;
int targetHeight = 700;
// Get scaled dimensions while maintaining aspect ratio
int width = originalImage.getWidth();
int height = originalImage.getHeight();
double aspectRatio = (double) width / height;
if (width > height) {
targetHeight = (int) (targetWidth / aspectRatio);
} else {
targetWidth = (int) (targetHeight * aspectRatio);
}
// Create a high-quality scaled image
BufferedImage scaledImage = new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = scaledImage.createGraphics();
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.drawImage(originalImage, 0, 0, targetWidth, targetHeight, null);
g2d.dispose();
// Set the scaled image to the label
recipeLabel.setIcon(new ImageIcon(scaledImage));
} catch (IOException e) {
e.printStackTrace();
}
}
// Method to calculate days until the anniversary
private long getDaysUntilAnniversary() {
LocalDate today = LocalDate.now();
return ChronoUnit.DAYS.between(today, anniversaryDate);
}
// Method to play music
private void playMusic(String filePath) {
try {
File musicFile = new File(filePath);
Clip clip = AudioSystem.getClip();
clip.open(AudioSystem.getAudioInputStream(musicFile));
clip.start();
} catch (Exception ex) {
ex.printStackTrace();
}
}
// Main method to run the app
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
new BirthdayApp().setVisible(true);
});
}
}