-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandomGame
More file actions
129 lines (129 loc) · 5.5 KB
/
Copy pathRandomGame
File metadata and controls
129 lines (129 loc) · 5.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
package Oasis;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
public class RandomGame extends JFrame {
private JLabel backgroundLabel;
private int numberToGuess;
private int numberOfTries;
private JTextField guessField;
private JButton guessButton;
private JLabel titleLabel;
private JLabel messageLabel; // Label for displaying the message
private JButton tryagainButton;
public RandomGame() {
setTitle("Number Guessing Game");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(800, 600);
setLayout(null); // Use null layout for precise component placement
// Initialize the game
numberOfTries = 0;
Random random = new Random();
numberToGuess = random.nextInt(100) + 1;
initializeComponents();
// Create and set the initial background image
ImageIcon backgroundImage = new ImageIcon("C:\\Users\\zone\\Downloads\\image.jpg");
backgroundLabel = new JLabel(backgroundImage);
backgroundLabel.setBounds(0, 0, 800, 600); // Set bounds to cover the entire frame
// Add components to the frame
add(backgroundLabel);
// Center the frame on the screen
setLocationRelativeTo(null);
}
private void initializeComponents() {
titleLabel = new JLabel("Welcome To Number Guessing Game");
titleLabel.setHorizontalAlignment(JLabel.CENTER);
titleLabel.setVerticalAlignment(JLabel.CENTER);
titleLabel.setForeground(Color.YELLOW);
titleLabel.setFont(new Font("Poopins", Font.BOLD, 36));
titleLabel.setBounds(0, 250, 800, 30);
guessField = new JTextField(10);
guessField.setBounds(280, 350, 100, 30);
// Adjusted position
guessButton = new JButton("Guess");
guessButton.setBounds(400, 350, 100, 30); // Adjusted position
// Create the message label with transparent background
messageLabel = new JLabel();
messageLabel.setBounds(0, 0, 800, 600);
messageLabel.setHorizontalAlignment(JLabel.CENTER);
messageLabel.setVerticalAlignment(JLabel.CENTER);
messageLabel.setForeground(Color.YELLOW); // Set the text color
messageLabel.setFont(new Font("SansSerif", Font.BOLD, 32)); // Set the font
messageLabel.setVisible(false);
tryagainButton=new JButton("Try Again");
tryagainButton.setVisible(false);
tryagainButton.setBounds(350, 350, 120, 30);// Initially, the message is hidden;
tryagainButton.setBackground(Color.ORANGE);
guessButton.setBackground(Color.ORANGE);
guessButton.setForeground(Color.WHITE);
tryagainButton.setForeground(Color.WHITE);
guessButton.setFont(new Font("SansSerif", Font.BOLD, 18));
tryagainButton.setFont(new Font("SansSerif", Font.BOLD, 18));
add(titleLabel);
add(guessField);
add(guessButton);
add(messageLabel);
add(tryagainButton);
tryagainButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Display a message on the image
numberOfTries = 0;
Random random = new Random();
numberToGuess = random.nextInt(100) + 1;
// Reset components
guessField.setVisible(true);
guessButton.setVisible(true);
titleLabel.setVisible(true);
messageLabel.setVisible(false);
tryagainButton.setVisible(false);
// Reset background image
ImageIcon backgroundImage = new ImageIcon("C:\\Users\\zone\\Downloads\\image.jpg");
backgroundLabel.setIcon(backgroundImage);
}
});
guessButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Display a message on the image
messageLabel.setVisible(true);
setSize(1000, 1000);
ImageIcon newBackground = new ImageIcon("C:\\Users\\zone\\Pictures\\image1 (1).jpeg");
backgroundLabel.setIcon(newBackground);
guessField.setVisible(false);
guessButton.setVisible(false);
titleLabel.setVisible(false);
try {
int userGuess = Integer.parseInt(guessField.getText());
numberOfTries++;
if (userGuess == numberToGuess) {
messageLabel.setText("Congratulations! \n You guessed it in " + numberOfTries + " tries.");
} else if (userGuess < numberToGuess) {
messageLabel.setText("Try Higher!");
tryagainButton.setVisible(true);
}
else{
messageLabel.setText("Try Lower!");
tryagainButton.setVisible(true);
}
}
catch (NumberFormatException ex) {
messageLabel.setText("Invalid input. Enter a number.");
tryagainButton.setVisible(true);
}
guessField.setText("");
}
});
}
// Method to generate a message to be displayed
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
RandomGame game = new RandomGame();
game.setVisible(true);
}
});
}
}