-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClientGUI.java
270 lines (250 loc) · 10 KB
/
ClientGUI.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
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
package Assignment3;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.net.*;
import java.nio.ByteBuffer;
public class ClientGUI extends Client {
JFrame window;
private JPanel panel1;
JLabel maxBid;
JLabel buyerAddress;
JLabel timeLeft;
private JTextField bidAmount;
private JButton bidButton;
private JLabel errorMessage;
boolean active = true;
private class Listener implements ActionListener, KeyListener {
@Override
public void actionPerformed(ActionEvent e) {
if (active) {
errorMessage.setVisible(false);
bid(bidAmount.getText());
bidAmount.setText("");
}
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
}
}
private class MyTask implements Runnable {
public void run() {
for(;;) {
byte[] receiveData = new byte[2000];
try {
DatagramSocket clientSocket = new DatagramSocket(port + 1);
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
clientSocket.receive(receivePacket);
bid = ByteBuffer.wrap(receiveData).getDouble();
//Uncomment this to add receiving of buyer address and time left in auction
//CLIENT TEXT SHOULDN'T DO ANYTHING WITH TIME OTHER THAN RECEIVE TO CLEAR THE PACKET
/*
receivePacket = new DatagramPacket(addressPacket, addressPacket.length);
clientSocket.receive(receivePacket);
buyerAddress = new String(addressPacket);
buyerAddress = buyerAddress.trim();
receivePacket = new DatagramPacket(timePacket, timePacket.length);
socket.receive(receivePacket);
*/
System.out.println("Max bid: " + bid);
maxBid.setText("$" + bid);
clientSocket.close();
} catch (IOException e) {}
}
}
}
public ClientGUI() {
startWindow();
}
private void startWindow() {
window = new JFrame();
window.setTitle("Startup");
window.setSize(300, 80);
window.setLocationRelativeTo(null);
final JPanel startPanel = new JPanel();
startPanel.setSize(300, 80);
// Initialize the components
final JLabel label = new JLabel("Would you like to start the bidding app?");
final JButton startButton = new JButton("Yes");
//format the components
// Setup the action listeners
startButton.addActionListener((ActionEvent event) -> {
startPanel.setVisible(false);
window.getContentPane().remove(startPanel);
bidWindow();
});
// Add all of the labels to the JFrame
startPanel.add(label);
startPanel.add(startButton);
window.getContentPane().add(startPanel);
window.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
private void bidWindow() {
//Panel1
panel1 = new JPanel();
GroupLayout layout1 = new GroupLayout(panel1);
panel1.setLayout(layout1);
layout1.setAutoCreateGaps(true);
layout1.setAutoCreateContainerGaps(true);
//Panel1 -> Panel2
final JPanel panel2 = new JPanel();
GroupLayout layout2 = new GroupLayout(panel2);
panel2.setLayout(layout2);
layout2.setAutoCreateContainerGaps(true);
final JLabel label1 = new JLabel("Current Maximum Bid:");
maxBid = new JLabel("$0.00");
final JLabel label2 = new JLabel("Current Buyer Address:");
buyerAddress = new JLabel("0.0.0.0:0");
layout2.setHorizontalGroup(
layout2.createSequentialGroup()
.addComponent(label1)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(maxBid)
.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(label2)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(buyerAddress)
);
layout2.setVerticalGroup(
layout2.createSequentialGroup()
.addGroup(layout2.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(label1)
.addComponent(maxBid)
.addComponent(label2)
.addComponent(buyerAddress))
);
//Panel1 -> Panel3
final JPanel panel3 = new JPanel();
GroupLayout layout3 = new GroupLayout(panel3);
panel3.setLayout(layout3);
layout3.setAutoCreateContainerGaps(true);
final JLabel label4 = new JLabel("Time Left in Auction:");
timeLeft = new JLabel("0 seconds");
layout3.setHorizontalGroup(
layout3.createSequentialGroup()
.addComponent(label4)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(timeLeft)
);
layout3.setVerticalGroup(
layout3.createSequentialGroup()
.addGroup(layout3.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(label4)
.addComponent(timeLeft))
);
//Panel1 -> Panel4
final JPanel panel4 = new JPanel();
GroupLayout layout4 = new GroupLayout(panel4);
panel4.setLayout(layout4);
layout4.setAutoCreateContainerGaps(true);
final JLabel label3 = new JLabel("Bid Amount: $");
bidAmount = new JTextField("", 8);
errorMessage = new JLabel("*");
bidButton = new JButton("Bid");
layout4.setHorizontalGroup(
layout4.createSequentialGroup()
.addComponent(label3)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout4.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(bidAmount)
.addComponent(errorMessage))
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(bidButton)
);
layout4.setVerticalGroup(
layout4.createSequentialGroup()
.addGroup(layout4.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(label3)
.addComponent(bidAmount)
.addComponent(bidButton))
.addComponent(errorMessage)
);
//Add Panels2-4 to Panel1
layout1.setHorizontalGroup(
layout1.createSequentialGroup()
.addGroup(layout1.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(panel2)
.addComponent(panel3)
.addComponent(panel4))
);
layout1.setVerticalGroup(
layout1.createSequentialGroup()
.addComponent(panel2)
.addComponent(panel3)
.addComponent(panel4)
);
//Final setup stuff
Listener listener = new Listener();
try {
findGoodPort();
socket = new DatagramSocket(port);
address = InetAddress.getByName(HOST_NAME);
contactServer();
} catch (IOException e) {
e.printStackTrace();
}
window.setContentPane(panel1);
window.setResizable(false);
window.setTitle("Bidding");
window.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
socket.close();
System.out.println("Safely closed");
}
});
window.pack();
errorMessage.setForeground(Color.RED);
errorMessage.setFont(new Font(UIManager.getDefaults().getFont("Label.font").getFontName(), Font.PLAIN,
8));
errorMessage.setVisible(false);
bidButton.addActionListener(listener);
bidAmount.addActionListener(listener);
window.setVisible(true);
}
public void bid(String input) {
try {
Thread t = new Thread(new MyTask());
t.start();
double bid;
String[] parts = input.split("\\.");
if (parts.length > 1) {
if (parts[1].length() > 2) {
errorMessage.setText("*Amount can only have up to two decimal places");
errorMessage.setVisible(true);
return;
}
}
try {
bid = Double.parseDouble(input);
} catch (NumberFormatException e) {
errorMessage.setText("*Invalid amount");
errorMessage.setVisible(true);
return;
}
//long startTime = System.nanoTime();
DatagramPacket sendPacket = new DatagramPacket(ByteBuffer.allocate(8).putDouble(bid).array(), 8,
address, SERVER_PORT);
socket.send(sendPacket);
if(!input.equals("-1")) {
System.out.println("Client sent $" + input + " as bid");
}
} catch (IOException e) {}
}
public void contactServer() {
bid("-1");
}
public void main() {
EventQueue.invokeLater(() -> {
ClientGUI gui = new ClientGUI();
gui.window.setVisible(true);
});
}
}