-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
114 lines (96 loc) · 3.15 KB
/
Main.java
File metadata and controls
114 lines (96 loc) · 3.15 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
package ca.bcit.comp2526.a2a;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Toolkit;
import javax.swing.JFrame;
/**
* Main class containing method
* instantiate world, gameframe.
* @author Kevin Oane
* @version 1
*/
public final class Main {
private static final Toolkit TOOLKIT;
static {
TOOLKIT = Toolkit.getDefaultToolkit();
}
/**
* Main constructor.
*/
private Main() {
}
/**
* Main method to create World and insert
* it into GameFrame.
* @param argv
* Receives command-line args.
*/
public static void main(final String[] argv) {
final GameFrame frame;
final World world;
final int numRows = 25;
final int numCols = 25;
RandomGenerator.reset();
world = new World(numRows, numCols);
world.init();
frame = new GameFrame(world);
position(frame);
frame.init();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
/**
* Adjusts GameFrame relative to screen size.
* @param frame
* GameFrame object.
*/
private static void position(final GameFrame frame) {
final Dimension size;
size = calculateScreenArea(0.80f, 0.80f);
frame.setSize(size);
frame.setLocation(centreOnScreen(size));
}
/**
* Fine adjustment to GUI.
* @param size
* size of Point.
* @return
* Adjusted GUI.
*/
public static Point centreOnScreen(final Dimension size) {
final Dimension screenSize;
if (size == null) {
throw new IllegalArgumentException("Size cannot be null");
}
screenSize = TOOLKIT.getScreenSize();
return (new Point((screenSize.width - size.width) / 2, (screenSize.height - size.height) / 2));
}
/**
* Adjusts world & cell relative to window size.
* @param widthPercent
* Width.
* @param heightPercent
* Height.
* @return
* Adjusted size.
*/
public static Dimension calculateScreenArea(final float widthPercent, final float heightPercent) {
final Dimension screenSize;
final Dimension area;
final int width;
final int height;
final int size;
if ((widthPercent <= 0.0f) || (widthPercent > 100.0f)) {
throw new IllegalArgumentException("widthPercent cannot be " + "<= 0 or > 100 - got: " + widthPercent);
}
if ((heightPercent <= 0.0f) || (heightPercent > 100.0f)) {
throw new IllegalArgumentException("heightPercent cannot be " + "<= 0 or > 100 - got: " + heightPercent);
}
screenSize = TOOLKIT.getScreenSize();
width = (int) (screenSize.width * widthPercent);
height = (int) (screenSize.height * heightPercent);
size = Math.min(width, height);
area = new Dimension(size, size);
return (area);
}
}