-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameOver.java
More file actions
92 lines (76 loc) · 2.56 KB
/
Copy pathGameOver.java
File metadata and controls
92 lines (76 loc) · 2.56 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
//Graphics &GUI imports
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class GameOver extends JFrame {
// class variable (non-static)
static double x, y;
static GOPanel gOPanel;
JFrame thisFrame;
// Constructor - this runs first
GameOver(int player) {
super("Game Over");
this.thisFrame = this;
// Set the frame to full screen
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(Toolkit.getDefaultToolkit().getScreenSize());
this.setUndecorated(true); //Set to true to remove title barcc
//setBackground(new Color(0,0,0,0));
// Set up the game panel (where we put our graphics)
gOPanel = new GOPanel(player);
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
JButton backButton = new JButton("< Back");
backButton.addActionListener(new BackButtonListener());
gOPanel.add(backButton);
mainPanel.add(gOPanel);
this.add(mainPanel);
this.requestFocusInWindow(); // make sure the frame has focus
this.setVisible(true);
} // End of Constructor
class GOPanel extends JPanel {
private BufferedImage img;
public GOPanel(int player) {
try {
img = ImageIO.read(new File("player" + player + ".png"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//Scales to screen size
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
int w = getWidth();
int h = getHeight();
int iw = img.getWidth();
int ih = img.getHeight();
double xScale = (double) w / iw;
double yScale = (double) h / ih;
double scale = Math.min(xScale, yScale); // scale to fit
// Math.max(xScale, yScale); // scale to fill
int width = (int) (scale * iw);
int height = (int) (scale * ih);
int x = (w - width) / 2;
int y = (h - height) / 2;
g2.drawImage(img, x, y, width, height, this);
}
}
// if user wants to go back a new starting frame is generated
class BackButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
thisFrame.dispose();
new StartingFrame();
}
}
}