-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLauncher.java
More file actions
271 lines (233 loc) · 10.7 KB
/
Launcher.java
File metadata and controls
271 lines (233 loc) · 10.7 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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Random;
import java.net.*;
import java.io.*;
/**
* Launch Menu for starting the ShootEmUp game, contains options for game setup and config.
*
* @author Joeseph Agnelli, Mackenzie Neaton, Ken Rissew, Andrew Curry
* @version 1.0
*/
public class Launcher extends JFrame implements PortNum{
//----Declarations of GUI components and Objects
private static final long serialVersionUID = 4L;
private JTextField jtfAddress = null;
protected ObjectOutputStream oos = null;
protected ObjectInputStream ois = null;
private String[] stringArray = new String[0];
private Launcher thisObject = null;
public Window wind = null; //the window to display
protected static Object lock = new Object();
/**
* Main method constructs GUI, launching from GUI calls Window.java Constructor
*
* @param args Unused
*/
public static void main(String [] args){
new Launcher(); // New Launcher GUI
}// end main()
/**
* Creates the GUI and all the components within it
* <p>
* Starts the ServerThread for the ShootEmServer
* </p>
*/
public Launcher(){
thisObject = this; // Declare this as an Object
//---- Top Panel, just the header text
JPanel jpNorth = new JPanel();
JLabel jlTitle = new JLabel("Shoot 'em Up");
jpNorth.add(jlTitle);
//---- Center Panel - contains radio Buttons and action listeners
JPanel jpCenter = new JPanel(new GridLayout(0,1));
//----Flow layout for radio buttons
JPanel jpButtons = new JPanel();
//----Button group for choosing between client or server
ButtonGroup jbgServerOrClient = new ButtonGroup();
//----If jrbServer is selected, the IP TextArea will be uneditable and set to localhost
JRadioButton jrbServer = new JRadioButton("Server");
//----If jrbClient is selected, the IP TextArea will be editable and cleared of text
JRadioButton jrbClient = new JRadioButton("Client");
jrbClient.setSelected(true);
//----Adding Buttons to ButtonGroup + buttons JPanel
jbgServerOrClient.add(jrbServer);
jpButtons.add(jrbServer);
jbgServerOrClient.add(jrbClient);
jpButtons.add(jrbClient);
//---- Add listeners to change editable status of IP Field
//----Locks IP Field to localhost
jrbServer.addActionListener(
new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jtfAddress.setEditable(false);
jtfAddress.setText("localhost");
}
});
//----Allows edit of IP Field
jrbClient.addActionListener(
new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jtfAddress.setEditable(true);
jtfAddress.setText("");
}
});
//----Adding buttons JPanel to center JPanel
jpCenter.add(jpButtons);
//----Inner gridLayout for JLabels + TextFields
JPanel jpOptionsPane = new JPanel(new GridLayout(0,2));
//----IP Label + Field
jpOptionsPane.add(new JLabel("IP Address: " , JLabel.RIGHT));
jtfAddress = new JTextField(10);
jpOptionsPane.add(jtfAddress);
//----Nickname Label + Field
jpOptionsPane.add(new JLabel("Nickname: " , JLabel.RIGHT));
JTextField jtfNickname = new JTextField(10);
/*
*Creates random number generator to randomize default nickname selection
*/
Random rand = new Random();
int randValue = rand.nextInt(5);
if( randValue == 0 ){
jtfNickname.setText("Billy");
}
else if( randValue == 1 ){
jtfNickname.setText("Jesse");
}
else if( randValue == 2 ){
jtfNickname.setText("Doc");
}
else if( randValue == 3 ){
jtfNickname.setText("Butch");
}
else if( randValue == 4 ){
jtfNickname.setText("Wyatt");
}
jpOptionsPane.add(jtfNickname);
//----Color Label + ComboBox
jpOptionsPane.add(new JLabel("Team: " , JLabel.RIGHT));
String[] teamStrings = { "Red", "Blue", "Yellow", "Green" };
JComboBox<String> jcbTeamOptions = new JComboBox<String>(teamStrings);
jcbTeamOptions.setEditable(false);
jpOptionsPane.add(jcbTeamOptions);
jpCenter.add(jpOptionsPane);
//----Sout Panel for launch button
JPanel jpSouth = new JPanel();
//----Create launch button and add action listener to submit user input for selections
JButton jbLaunch = new JButton("Launch");
JButton jbHelp = new JButton(" Help ");
jbLaunch.addActionListener(
new ActionListener(){
@Override
public void actionPerformed(ActionEvent ae){
if(jrbServer.isSelected()){
ServerThread shootEmServer = new ServerThread();
shootEmServer.start();
}
try{
String serverAddress = jtfAddress.getText();
//System.out.println(serverAddress);
if(serverAddress.equals("")){
String inputValue = JOptionPane.showInputDialog("Please enter an IP");
if(inputValue.equals("")){
JOptionPane.showMessageDialog(null, "Invalid IP. Shutting down...", "Invalid IP", JOptionPane.ERROR_MESSAGE);
}
else{
serverAddress = inputValue;
}
}
try{
Thread.sleep(1000);
}
catch(InterruptedException e){
}
//----Create server socket and IO objects streams
Socket s = new Socket(serverAddress,PORT_NUMBER);
oos = new ObjectOutputStream(s.getOutputStream());
ois = new ObjectInputStream(s.getInputStream());
/*
**Set color of user and create a new window object that takes in the launcher
** the IO object streams, user name, and color input
*/
String colorString = jcbTeamOptions.getSelectedItem().toString();
Color teamColor = null;
Random colorGen = new Random();
float colorFloat = colorGen.nextFloat();
float zeroFloat = 0;
System.out.println(colorFloat);
if(colorString.equals("Red")){
teamColor = new Color(colorFloat,zeroFloat,zeroFloat);
}
else if(colorString.equals("Blue")){
teamColor = new Color(zeroFloat,zeroFloat,colorFloat);
}
else if(colorString.equals("Yellow")){
float floatFacade = 1;
teamColor = new Color(colorFloat,floatFacade,zeroFloat);;
}
else if(colorString.equals("Green")){
teamColor = new Color(zeroFloat,colorFloat,zeroFloat);
}
thisObject.setState(Frame.ICONIFIED);
wind = new Window(thisObject,jtfNickname.getText(), teamColor);
wind.requestFocus();
}
catch(SocketException se){
JOptionPane.showMessageDialog(null, "Failed to Connect to Server", "Server Unreachable", JOptionPane.ERROR_MESSAGE);
}
catch(UnknownHostException uhe){
JOptionPane.showMessageDialog(null, "Unable to connect to server", "Server Timeout", JOptionPane.ERROR_MESSAGE);
//uhe.printStackTrace();
}
catch(IOException ioe){
ioe.printStackTrace();
}
jbLaunch.setEnabled(false);
}
});
jbHelp.addActionListener(
new ActionListener(){
@Override
public void actionPerformed(ActionEvent ae){
JOptionPane.showMessageDialog(null, "<html><h1>Gameplay</h1><p>Shoot the other players and try to be the last one standing.</p><p>10 hits and you're out!</p><h2>Controls</h2><p>WASD to move</p><p>Mouse 1 to Shoot</p><p>ESC to pull up Exit Dialog</p><h2>Chatting</h2><p>Enter to send a message</p><p>/w [username] to send a message to that user</p><p>/r to reply to the last user to message you</p></html>", "Help Menu", JOptionPane.INFORMATION_MESSAGE);
}
});
//----Add launch button
jpSouth.add(jbLaunch);
jpSouth.add(jbHelp);
//----Blank JPanel, affects spacing so do not remove. It just makes it look a little less funky.
JPanel jpEast = new JPanel();
//----Add all panels to the frame
this.add(jpNorth, BorderLayout.NORTH);
this.add(jpCenter, BorderLayout.CENTER);
this.add(jpSouth, BorderLayout.SOUTH);
this.add(jpEast, BorderLayout.EAST);
//----Set JFrame values for the launch menu
this.pack();
this.setTitle("Shoot 'em Up Launcher");
this.setSize(200,220);
this.setVisible(true);
this.setLocationRelativeTo(null);
this.setResizable(false);
this.setDefaultCloseOperation( EXIT_ON_CLOSE );
}// end Launcher()
/**
*Inner class that creates a Thread of ServerThread
*run() method creates a new Server object of ShootEmServer
*/
class ServerThread extends Thread{
public void run(){
//ShootEmServer.main(stringArray);
new ShootEmServer();
try{
Process p = Runtime.getRuntime().exec("java ShootEmServer");
}
catch(IOException e){
System.out.println("Unable to open server");
}
}//end run()
}//end ServerThread Class
}//end Launcher Class