-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDifficultyScreen.java
More file actions
93 lines (77 loc) · 2.63 KB
/
DifficultyScreen.java
File metadata and controls
93 lines (77 loc) · 2.63 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
import java.awt.Graphics;
import java.awt.HeadlessException;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
class BackgroundPanel extends JPanel {
private Image backgroundImage;
public BackgroundPanel(String fileName) {
try {
backgroundImage = ImageIO.read(new File(fileName)); // Upload image
} catch (IOException e) {
e.printStackTrace();
System.out.println("The image could not be uploaded! Check the file path.");
}
setLayout(null); // We set the layout to null so that we can manually position the components.
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (backgroundImage != null) {
g.drawImage(backgroundImage, 0, 0, getWidth(), getHeight(), this);
}
}
}
public class DifficultyScreen extends JFrame {
public DifficultyScreen() {
setTitle("Difficulty Screen");
setSize(800, 800);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
// Create a background panel and add it to a JFrame.
BackgroundPanel panel = new BackgroundPanel("spacebck.jpg");
setContentPane(panel);
// Add buttons
JButton btnEasy = new JButton("EASY");
JButton btnMedium = new JButton("MEDIUM");
JButton btnHard = new JButton("HARD");
JButton btnSpecial = new JButton("SPECIAL");
// Adjust the button positions (because the layout is null)
btnEasy.setBounds(50, 50, 120, 50);
btnMedium.setBounds(200, 50, 120, 50);
btnHard.setBounds(350, 50, 120, 50);
btnSpecial.setBounds(500, 50, 120, 50);
// Add buttons to the background panel
panel.add(btnEasy);
panel.add(btnMedium);
panel.add(btnHard);
panel.add(btnSpecial);
setVisible(true);
btnEasy.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
StartGame startGame = null;
try {
startGame = new StartGame("Space Game");
} catch (HeadlessException ex) {
Logger.getLogger(DifficultyScreen.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(DifficultyScreen.class.getName()).log(Level.SEVERE, null, ex);
}
startGame.setVisible(true);
DifficultyScreen.this.setVisible(false);
}
});
}
public static void main(String[] args) {
new DifficultyScreen();
}
}