-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCongrats1.java
More file actions
200 lines (175 loc) · 10.5 KB
/
Copy pathCongrats1.java
File metadata and controls
200 lines (175 loc) · 10.5 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import javax.swing.*; // Import Swing components for GUI
import java.awt.*; // Import AWT for layout and graphics
import java.awt.event.ActionEvent; // Import for handling action events
import java.awt.event.ActionListener; // Import for action listener interface
import java.util.ArrayList; // Import for using ArrayList
import java.util.List; // Import for using List interface
import java.util.Random; // Import for generating random numbers
public class congrats1 extends JFrame {
private String username; // Variable to store the username
private HomePage homePage; // Reference to the HomePage object
private int score; // Variable to store the user's score
// Constructor for the congrats1 class
public congrats1(String username, HomePage homePage, int score) {
this.username = username; // Initialize username
this.homePage = homePage; // Initialize homePage reference
setTitle("Chapter Complete"); // Set window title
setSize(800, 600); // Set window size
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Close application on exit
setLayout(null); // Use absolute positioning for custom placements
setVisible(true); // Make the frame visible
// Add confetti panel as the background
ConfettiPanel confettiPanel = new ConfettiPanel();
confettiPanel.setBounds(0, 0, 800, 600); // Set bounds for the confetti panel
add(confettiPanel); // Add confetti panel to the frame
// Load and display stars image
ImageIcon starsIcon = new ImageIcon("D:\\Screenshot 2024-11-03 214615.png"); // Path to stars image
Image starsImage = starsIcon.getImage().getScaledInstance(240, 88, Image.SCALE_SMOOTH); // Resize image
JLabel starsLabel = new JLabel(new ImageIcon(starsImage)); // Create a JLabel with the stars image
starsLabel.setHorizontalAlignment(SwingConstants.CENTER); // Center the label
starsLabel.setBounds(280, 25, 240, 88); // Set position and size
confettiPanel.add(starsLabel); // Add stars label to the confetti panel
// Create and set up the congratulations banner
JLabel congratulationsLabel = new JLabel("CONGRATULATIONS");
congratulationsLabel.setForeground(Color.WHITE); // Set text color
congratulationsLabel.setFont(new Font("Arial Rounded MT Bold", Font.PLAIN, 32)); // Set font style and size
congratulationsLabel.setHorizontalAlignment(SwingConstants.CENTER); // Center the label
congratulationsLabel.setBounds(200, 110, 400, 60); // Set position and size
confettiPanel.add(congratulationsLabel); // Add to the confetti panel
// Create and set up the completion message
JLabel messageLabel = new JLabel("You have successfully completed");
messageLabel.setForeground(Color.WHITE); // Set text color
messageLabel.setFont(new Font("Arial Rounded MT Bold", Font.PLAIN, 18)); // Set font style and size
messageLabel.setHorizontalAlignment(SwingConstants.CENTER); // Center the label
messageLabel.setBounds(200, 170, 400, 30); // Set position and size
confettiPanel.add(messageLabel); // Add to the confetti panel
// Create and set up the chapter label
JLabel chapterLabel = new JLabel("CHAPTER 1");
chapterLabel.setFont(new Font("Arial Rounded MT Bold", Font.BOLD, 20)); // Set font style
chapterLabel.setForeground(Color.WHITE); // Set text color
chapterLabel.setHorizontalAlignment(SwingConstants.CENTER); // Center the label
chapterLabel.setBounds(300, 220, 200, 30); // Set position and size
confettiPanel.add(chapterLabel); // Add to the confetti panel
// Load and display the pet image
ImageIcon dogIcon = new ImageIcon("C:\\Users\\USER\\IdeaProjects\\Quizz\\src\\images\\transparent-cute-dog-cartoon-corgi-puppy-with-bone-on-black-1710989585356.png"); // Path to dog image
Image dogImage = dogIcon.getImage().getScaledInstance(340, 348, Image.SCALE_SMOOTH); // Resize image
JLabel petLabel = new JLabel(new ImageIcon(dogImage)); // Create a JLabel with the dog image
petLabel.setHorizontalAlignment(SwingConstants.CENTER); // Center the label
petLabel.setBounds(240, 220, 320, 250); // Set position and size
confettiPanel.add(petLabel); // Add to the confetti panel
// Create and set up the Home Page button
RoundButton homeButton = new RoundButton("Home Page");
homeButton.setFont(new Font("Arial Rounded MT Bold", Font.PLAIN, 18)); // Set font style and size
homeButton.setBackground(new Color(245, 174, 82)); // Set button background color
homeButton.setForeground(Color.WHITE); // Set button text color
homeButton.setFocusPainted(false); // Disable focus painting
homeButton.setBounds(325, 450, 150, 50); // Set position and size
confettiPanel.add(homeButton); // Add button to the confetti panel
// Add action listener to the home button
homeButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new HomePage(username); // Open the HomePage window
dispose(); // Close the current window
}
});
// Start confetti animation
confettiPanel.startAnimation(); // Begin the confetti animation
}
// Main method to launch the congrats1 frame
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
congrats1 screen = new congrats1("testUser", new HomePage(), 10); // Create an instance with test values
screen.setVisible(true); // Set the screen visible
});
}
// Custom round button class for aesthetic button design
private class RoundButton extends JButton {
public RoundButton(String label) {
super(label); // Call superclass constructor with button label
setOpaque(false); // Set to non-opaque for rounded effect
setFocusPainted(false); // Remove focus ring
}
@Override
protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g.create(); // Create a copy of the graphics context
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // Enable anti-aliasing
g2.setColor(getBackground()); // Set the background color
g2.fillRoundRect(0, 0, getWidth(), getHeight(), 50, 50); // Draw rounded rectangle
super.paintComponent(g); // Call superclass paint method
}
@Override
protected void paintBorder(Graphics g) {
Graphics2D g2 = (Graphics2D) g.create(); // Create a copy of the graphics context
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // Enable anti-aliasing
g2.setColor(getForeground()); // Set the border color
g2.drawRoundRect(0, 0, getWidth() - 1, getHeight() - 1, 50, 50); // Draw rounded border
g2.dispose(); // Dispose of the graphics context
}
@Override
public void setContentAreaFilled(boolean b) {
// Override to do nothing to keep the rounded effect
}
}
// Confetti Panel Class for animated confetti effect
private class ConfettiPanel extends JPanel {
private final List<Confetti> confettiList = new ArrayList<>(); // List to hold confetti particles
public ConfettiPanel() {
setLayout(null); // Set layout to null for absolute positioning
setBackground(new Color(34, 45, 90)); // Set background color
for (int i = 0; i < 100; i++) { // Add 100 confetti particles
Random random = new Random(); // Create a new random object
confettiList.add(new Confetti(random)); // Add new Confetti particle to the list
}
}
// Method to start the confetti animation
public void startAnimation() {
Timer timer = new Timer(30, e -> { // Create a timer that fires every 30ms
for (Confetti confetti : confettiList) {
confetti.update(); // Update each confetti particle's position
}
repaint(); // Repaint the panel to update the display
});
timer.start(); // Start the timer
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g); // Call superclass paint method
Graphics2D g2d = (Graphics2D) g; // Cast to Graphics2D for better rendering
for (Confetti confetti : confettiList) {
confetti.draw(g2d); // Draw each confetti particle
}
}
}
// Confetti Particle Class to manage individual confetti behavior
private class Confetti {
private int x, y, size; // Position and size of the confetti
private final Color color; // Color of the confetti
private int speed; // Falling speed of the confetti
// Constructor to initialize confetti particles
public Confetti(Random random) {
reset(random); // Reset to a random starting position
color = new Color(random.nextInt(255), random.nextInt(255), random.nextInt(255)); // Random color
}
// Update method to change position of the confetti
public void update() {
y += speed; // Move the confetti down by its speed
if (y > getHeight()) { // If it goes off screen
Random random = new Random(); // Create a new random object
reset(random); // Reset its position
}
}
// Method to draw the confetti particle
public void draw(Graphics2D g) {
g.setColor(color); // Set the color for drawing
g.fillOval(x, y, size, size); // Draw the confetti as an oval
}
// Reset method to initialize confetti properties
private void reset(Random random) {
x = random.nextInt(800); // Random x position within the frame width
y = random.nextInt(600) - 600; // Start above the screen
size = random.nextInt(10) + 5; // Random size between 5 and 15
speed = random.nextInt(5) + 1; // Random speed between 1 and 5
}
}
}