Repository navigation
Expand file tree
/
Copy pathSwingApplication.java
More file actions
216 lines (183 loc) · 5.57 KB
/
Copy pathSwingApplication.java
File metadata and controls
216 lines (183 loc) · 5.57 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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import java.awt.Component;
import java.awt.LayoutManager;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
/**
* This is essentially a wrapper for a Swing JFrame and a Thread starter, so the
* user doesn't have to mess with it.
*/
public abstract class SwingApplication {
/* Number of milliseconds per second. */
private static final int SECONDS_TO_MS = 1000;
/* JFrame to add content to. */
private final JFrame FRAME;
/* Timer for updating the JFrame. */
private Timer timer;
/* Frames per second to run the timer at. */
private int fps;
/* Milliseconds to specify the speed of our timer. */
private int ms;
/* Boolean to set the timer to run or not. */
private boolean isRunning = false;
public SwingApplication(int width, int height, int fps, String title) {
System.setProperty("sun.java2d.opengl", "true");
/* Create the JFrame. */
this.FRAME = new JFrame(title);
this.FRAME.setSize(width, height);
this.FRAME.setResizable(false);
this.FRAME.setLocationRelativeTo(null);
this.FRAME.setVisible(true);
this.FRAME.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
this.FRAME.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent windowEvent) {
stop();
}
});
/*
* Sets the current FPS and the millisecond update time for the Swing
* timer.
*/
this.fps = fps;
this.ms = SECONDS_TO_MS / fps;
/* Starts the timer. */
SwingUtilities.invokeLater(() -> {
this.start();
});
}
/**
* Sets the layout manager for the current JFrame.
*
* @param layout
* - LayoutManager type.
*/
public void setFrameLayout(LayoutManager layout) {
this.FRAME.setLayout(layout);
}
/**
* Adds a Component object to the content pane of this JFrame.
*
* @param component
* - component to add.
*/
public void addComponent(Component component) {
this.FRAME.getContentPane().add(component);
}
/**
* Adds a Component object at a specific index to the content pane of this
* JFrame.
*
* @param component
* - component to add.
* @param index
* - index to add the new component to.
*/
public void addComponent(Component component, int index) {
this.FRAME.getContentPane().add(component, index);
}
/**
* Adds a Component object with predefined constraints to the end of the
* content pane component list.
*
* @param component
* - component to add.
* @param constraints
* - Component constraints.
*/
public void addComponent(Component component, Object constraints) {
this.FRAME.getContentPane().add(component, constraints);
}
/**
* Adds a Component object with predefined constraints at a specific index
* to the content pane of this JFrame.
*
* @param component
* - component to add.
* @param constraints
* - Component constraints.
* @param index
* - index to add the new component to.
*/
public void addComponent(Component component, Object constraints,
int index) {
this.FRAME.getContentPane().add(component, constraints, index);
}
/**
* Sets the location of the frame to the center of the screen, and packs all
* components together, updating their dimensions.
*/
public void packComponents() {
this.FRAME.pack();
this.FRAME.setLocationRelativeTo(null);
}
/**
* Invokes the thread loop for this swing application. Any code that needs
* to be repeated should be placed inside this overridden method.
*/
public abstract void run();
/**
* Starts the program if it is not already running.
*/
private synchronized void start() {
if (this.isRunning) {
return;
}
this.isRunning = true;
this.update();
}
/**
* Stops and destroys the timer and frame if it is not already stopped.
*/
private synchronized void stop() {
if (!this.isRunning) {
return;
}
this.isRunning = false;
this.timer.stop();
this.FRAME.dispose();
System.exit(0);
}
/**
* Initializes the update loop timer.
*/
private void update() {
this.setupAppTimer();
this.timer.start();
}
/**
* Sets up the application and render timer.
*/
private void setupAppTimer() {
this.timer = new Timer(this.ms, timerListener -> {
this.run();
this.FRAME.repaint();
});
}
// =================== GETTERS AND SETTERS ====================== //
public void setFPS(int fps) {
this.fps = fps;
this.ms = SECONDS_TO_MS / fps;
this.timer.setDelay(this.ms);
}
public int getFPS() {
return this.fps;
}
public boolean isRunning() {
return this.isRunning;
}
public void setRunning(boolean running) {
this.isRunning = running;
}
public void isVisible() {
this.FRAME.isVisible();
}
public void setVisible(boolean visible) {
this.FRAME.setVisible(true);
}
public JFrame getFrame() {
return this.FRAME;
}
}