Tutor 11 Salman
Tutor 11 Salman
/*
MD Salman Khan 202405010323
Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
package com.mycompany.clickgame;
A fun and interactive click game with animations and sounds.
@author ChatGPT
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
public class ClickGame implements ActionListener {
private int clicks = 0;
private JLabel label = new JLabel("Ready to start clicking?",
SwingConstants.CENTER);
private JFrame frame = new JFrame();
private JPanel panel = new JPanel();
private JButton button = new JButton("Click Me!");
private Random random = new Random();
private String[] messages = {
"Keep going!", "Nice one!", "You're on fire!", "Click click click!",
"Almost there!", "Unstoppable!", "Master Clicker", "Insane clicking speed!"
};
private Timer colorTimer;
public ClickGame() {
// Button Setup
button.addActionListener(this);
button.setFont(new Font("Arial", Font.BOLD, 16));
// Label Setup
label.setFont(new Font("Arial", Font.BOLD, 18));
// Panel Setup
panel.setBorder(BorderFactory.createEmptyBorder(40, 50, 40, 50));
panel.setLayout(new GridLayout(2, 1, 10, 10));
panel.add(button);
panel.add(label);
// Frame Setup
frame.add(panel, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Ultimate Clicker Challenge");
frame.setSize(400, 250);
frame.setVisible(true);
// Color Timer for Dynamic Background Changes
colorTimer = new Timer(500, e -> panel.setBackground(getRandomColor()));
colorTimer.start();
}
// Handles button click events
public void actionPerformed(ActionEvent e) {
clicks++;
label.setText("Clicks: " + clicks);
button.setText(getRandomMessage());
panel.setBackground(getRandomColor());
playClickSound();
}
// Returns a random motivational message
private String getRandomMessage() {
return messages[random.nextInt(messages.length)];
}
// Generates a random background color
private Color getRandomColor() {
return new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256));
}
// Simulates a click sound
private void playClickSound() {
Toolkit.getDefaultToolkit().beep(); // Simple beep sound for now
}
public static void main(String[] args) {
new ClickGame();
}
}
Output