0% found this document useful (0 votes)
24 views2 pages

Tutor 11 Salman

The document describes a Java program for an interactive click game called 'Ultimate Clicker Challenge'. It features a graphical user interface with a button that tracks clicks, displays motivational messages, and changes background colors dynamically. The program utilizes Swing components and includes sound effects for a more engaging user experience.

Uploaded by

Salman Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views2 pages

Tutor 11 Salman

The document describes a Java program for an interactive click game called 'Ultimate Clicker Challenge'. It features a graphical user interface with a button that tracks clicks, displays motivational messages, and changes background colors dynamically. The program utilizes Swing components and includes sound effects for a more engaging user experience.

Uploaded by

Salman Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Tutorial 11 OOP

MD Salman Khan 202405010323

/*
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

You might also like