-
Notifications
You must be signed in to change notification settings - Fork 0
/
GamesFrame.java
162 lines (144 loc) · 6.89 KB
/
GamesFrame.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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import javax.swing.*;
import javax.swing.border.AbstractBorder;
import java.awt.*;
import java.io.IOException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class GamesFrame extends JFrame {
private JLabel coinsLabel; // JLabel variable declaration
CoinCount coinCount = new CoinCount("coin.txt"); // CoinCount object creation
GamesFrame(Color backgroundColor, JFrame mainFrame) { // GamesFrame constructor
Font montserrat20 = FontLoader.loadFont("fonts/ElectronicHighwaySign.TTF", 22f); // Font variable declaration
Font montserrat10 = FontLoader.loadFont("fonts/ElectronicHighwaySign.TTF", 18f); // Font variable declaration
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.setSize(700, 700);
this.getContentPane().setBackground(backgroundColor);
this.setLayout(null);
CustomButton button = new CustomButton("5 Minute Mayhem (-10)", 350, 100, 175, 70); // CustomButton object creation
button.addActionListener(new ActionListener() {
@Override
//ovveride overrides a superclass method essentially changing it
public void actionPerformed(ActionEvent e) {
try {
if (coinCount.getCurrentCount() - 10 >= 0) {
try {
String unityGamePath = "myfinal\\5MinuteMayham.exe";
String width = "1280";
String height = "720";
ProcessBuilder pb = new ProcessBuilder(unityGamePath, "-screen-width", width, "-screen-height", height);
Process unityGameProcess = pb.start();
GamesFrame.this.setVisible(false);
unityGameProcess.waitFor();
GamesFrame.this.setVisible(true);
coinCount.changeCount(-10);
//exception is smth that occurs during the execution of a program that disrupts the normal flow of instructions
//try catch is used to handle the exception
//catch prints to the error thingie
} catch (IOException | InterruptedException ex) {
ex.printStackTrace();
}
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
});
this.getContentPane().add(button);
CustomButton pong = new CustomButton("Pong (free cus it's bad)", 350, 100, 175, 175); // CustomButton object creation
pong.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//action event is a parameter passed to the action listener "e" contains info about the action
Point currentLocation = GamesFrame.this.getLocation();
Color backgroundColor = getContentPane().getBackground();
BallFrame ballFrame = new BallFrame(backgroundColor, GamesFrame.this); // BallFrame object creation
ballFrame.setLocation(currentLocation);
ballFrame.setVisible(true);
GamesFrame.this.setVisible(false);
}
});
this.getContentPane().add(pong);
BackButton backButton = new BackButton(this, mainFrame); // BackButton object creation
this.getContentPane().add(backButton);
JLabel gamesLabel = new JLabel("Games"); // JLabel object creation
gamesLabel.setFont(montserrat20);
gamesLabel.setBounds(0, 20, 700, 30);
gamesLabel.setHorizontalAlignment(JLabel.CENTER);
gamesLabel.setForeground(Color.WHITE);
this.getContentPane().add(gamesLabel);
JPanel statsPanel = new JPanel(new GridBagLayout()) {
@Override
protected void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(getBackground());
g2d.fillRoundRect(0, 0, getWidth() - 1, getHeight() - 1, 15, 15);
}
};
statsPanel.setBounds(255, 450, 175, 100);
statsPanel.setBackground(new Color(50, 50, 50));
AbstractBorder roundedBorder = new AbstractBorder() {
@Override
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(new Color(25, 25, 25));
g2d.setStroke(new BasicStroke(3));
g2d.drawRoundRect(x, y, width - 1, height - 1, 15, 15);
}
};
statsPanel.setBorder(roundedBorder);
int currentCount = 0;
try {
currentCount = coinCount.getCurrentCount();
} catch (IOException e) {
e.printStackTrace();
System.out.println("Error reading from file");
}
JLabel coinsLabel = new JLabel("Coins: " + currentCount); // JLabel object creation
coinsLabel.setFont(montserrat10);
coinsLabel.setForeground(Color.WHITE);
GradeCount gradeCount = new GradeCount("grade.txt"); // GradeCount object creation
int currentGrade = 0;
try {
currentGrade = gradeCount.getCurrentGrade();
} catch (IOException e) {
e.printStackTrace();
System.out.println("Error reading from file");
}
JLabel ecLabel = new JLabel("Grade: " + currentGrade + "%"); // JLabel object creation
ecLabel.setFont(montserrat10);
ecLabel.setForeground(Color.WHITE);
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weighty = 1;
gbc.insets = new Insets(0, 0, -20, 0);
statsPanel.add(coinsLabel, gbc);
gbc.insets = new Insets(-10, 0, 0, 0);
statsPanel.add(ecLabel, gbc);
this.getContentPane().add(statsPanel);
this.addWindowListener(new WindowAdapter() {
@Override
public void windowActivated(WindowEvent e) {
try {
coinsLabel.setText("Coins: " + coinCount.getCurrentCount());
} catch (IOException ex) {
ex.printStackTrace();
System.out.println("Error reading from file");
}
}
});
}
public void updateCoinsLabel() { // updateCoinsLabel method
int currentCount = 0;
try {
currentCount = coinCount.getCurrentCount();
} catch (IOException e) {
e.printStackTrace();
System.out.println("Error reading from file");
}
coinsLabel.setText("Coins: " + currentCount);
}
}