This repository has been archived by the owner on Sep 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
InitGame.java
190 lines (157 loc) · 5.81 KB
/
InitGame.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
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
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.filechooser.FileSystemView;
import net.miginfocom.layout.AC;
import net.miginfocom.layout.CC;
import net.miginfocom.layout.LC;
import net.miginfocom.swing.MigLayout;
public class InitGame {
@SuppressWarnings("serial")
public class LayerController extends JLayeredPane{
public LayerController(){
super();
}
@Override
public boolean isOptimizedDrawingEnabled(){
return false;
}
}
@SuppressWarnings("serial")
public class ImageContainer extends JLabel{
public ImageContainer(String fileDirectory, String description){
this.setIcon(new ImageIcon(fileDirectory, description));
}
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawImage(((ImageIcon) this.getIcon()).getImage(), 0, 0, getWidth(), getHeight(), this);
}
}
public static String gameAction = "none";
public static String[] logs;
public static InitGame window;
private JFrame frame;
private Font defaultFont;
private ImageContainer image = new ImageContainer(FileSystemView.getFileSystemView().getDefaultDirectory().getPath() + "\\My Games\\Farmers of Capitalism\\Images\\splash.jpg", "splash image - hills");
/**
* Launch the application.
*/
public static String[] main(String[] args) throws InterruptedException{
String[] toLog = new String[args.length + 2];
for(int i = 0; i < args.length; i++){
toLog[i] = args[i];
}
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
toLog[args.length] = "Initializing Splash Screen";
window = new InitGame(toLog);
window.frame.setVisible(true);
} catch (Exception e) {
toLog[args.length] = e.getMessage();
}
}
});
return toLog;
}
/**
* Create the application.
*/
public InitGame(String[] args) throws InterruptedException{
initialize(args);
}
/**
* Initialize the contents of the frame.
*/
private void initialize(String[] args) throws InterruptedException{
frame = new JFrame();
setWindowPosition(frame, 0);
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.getContentPane().setLayout(new MigLayout(new LC(), new AC().grow(), new AC().grow()));
frame.setTitle("Farmers Of Capitalism");
frame.setIconImage(((ImageIcon) image.getIcon()).getImage());
frame.addComponentListener(new ComponentListener(){
@Override
public void componentResized(ComponentEvent e) {
frame.getContentPane().removeAll();
JPanel main = new JPanel(new MigLayout(new LC(), new AC().grow(), new AC().grow()));
JPanel buttons = new JPanel(new MigLayout(new LC(), new AC().grow(), new AC().grow()));
buttons.setOpaque(false);
LayerController layer = new LayerController();
layer.setLayout(new MigLayout());
layer.add(main, new CC().width("100%").height("100%"), new Integer(1));
layer.add(buttons, new CC().pos("80%", "80%"), new Integer(0));
frame.add(layer, "grow");
image.setFont(defaultFont);
image.setHorizontalAlignment(SwingConstants.CENTER);
main.add(image, "grow");
JButton newGame = new JButton("New Game");
newGame.setHorizontalAlignment(SwingConstants.CENTER);
newGame.setFont(defaultFont);
newGame.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
args[args.length - 1] = "User selected new game";
logs = args;
gameAction = "new game";
}
});
buttons.add(newGame, "growx, wrap");
JButton cont = new JButton("Continue");
cont.setHorizontalAlignment(SwingConstants.CENTER);
cont.setFont(defaultFont);
cont.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
//TODO user selects game save
args[args.length - 1] = "User selected to continue game at "; //TODO + directoryName;
logs = args;
gameAction = ""; // TODO make it directory
}
});
buttons.add(cont, "growx, wrap");
frame.repaint();
frame.revalidate();
}
@Override
public void componentMoved(ComponentEvent e) {}
@Override
public void componentShown(ComponentEvent e) {}
@Override
public void componentHidden(ComponentEvent e) {}
});
}
private void setWindowPosition(JFrame window, int screen){
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] allDevices = env.getScreenDevices();
int topLeftX, topLeftY, screenX, screenY, windowPosX, windowPosY;
screen = (screen < allDevices.length && screen > -1) ? screen : 0;
topLeftX = allDevices[screen].getDefaultConfiguration().getBounds().x;
topLeftY = allDevices[screen].getDefaultConfiguration().getBounds().y;
screenX = allDevices[screen].getDefaultConfiguration().getBounds().width;
screenY = allDevices[screen].getDefaultConfiguration().getBounds().height;
window.setBounds(0, 0, allDevices[screen].getDefaultConfiguration().getBounds().width / 2, allDevices[screen].getDefaultConfiguration().getBounds().height / 2);
windowPosX = ((screenX - window.getWidth()) / 2) + topLeftX;
windowPosY = ((screenY - window.getHeight()) / 2) + topLeftY;
defaultFont = new Font("Roman Baseline", Font.ROMAN_BASELINE, screenX / 75);
window.setLocation(windowPosX, windowPosY);
}
public void die(){
frame.dispose();
}
}