-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.java
72 lines (66 loc) · 2.23 KB
/
Main.java
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
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.imageio.ImageIO;
import java.io.InputStream;
public class Main extends Frame {
public static Frame menuGui;
public static Main mainWindow;
public static Image img;
Main(){
try {
InputStream imageStream = getClass().getResourceAsStream("\\background_2.png");
img = ImageIO.read(imageStream);
} catch (IOException e) {
System.out.println("Image could not be loaded.");
e.printStackTrace();
}
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public void paint(Graphics g) {
if (img != null) {
g.drawImage(img, 0, 50, 400, 500, null);
} else {
g.drawString("Image not loaded.", 150, 200);
}
}
public static void main(String[] args) {
mainWindow = new Main();
mainWindow.setSize(400, 550);
mainWindow.setLocationRelativeTo(null);
mainWindow.setResizable(false);
Panel panel = new Panel();
Panel panel2 = new Panel();
Button startGameButton = new Button("Start Game");
Button exitButton = new Button("Exit");
Label title = new Label("Apple Catching Game");
Font myFont = new Font("Arial", Font.BOLD, 16);
title.setFont(myFont);
startGameButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
mainWindow.setVisible(false);
new GameWindow();
}
});
exitButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
panel.add(startGameButton);
panel.add(exitButton);
panel2.add(title);
mainWindow.add(panel2, BorderLayout.NORTH);
mainWindow.add(panel, BorderLayout.SOUTH);
mainWindow.setVisible(true);
}
}