-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDemoPanel.java
More file actions
181 lines (159 loc) · 5.99 KB
/
Copy pathDemoPanel.java
File metadata and controls
181 lines (159 loc) · 5.99 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
package lumberlots;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.EtchedBorder;
/**
*
* @author nick
*/
public class DemoPanel extends JPanel{
private SchoolTile[] schoolTiles = new SchoolTile[6];
private int[] carCount = new int[8];
private JLabel[] lotName = new JLabel[6];
//public JLabel[] carsInLot = new JLabel[8];
Font myBigFont = (new Font("Bookman Old Style", Font.BOLD, 30));
Font mySmallFont = (new Font("Bookman Old Style", Font.BOLD, 20));
//constructor
public DemoPanel(){
this.setLayout(new GridLayout(3,2));
nameLots();
for(int i = 0; i < 6; i++){
SchoolTile tile = new SchoolTile(i);
schoolTiles[i] = tile;
this.add(tile);
carCount[i+2] = 0; //set 0 cars into each lot
//carsInLot[i+2] = new JLabel();
//carsInLot[i+2].setText(Integer.toString(carCount[i+2]));
}
}
//contains name, add and remove button, and car count
private class SchoolTile extends JPanel{
JLabel carsInLot;
//JLabel carsInLot = new JLabel();
SchoolTile(int id){
this.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED));
this.setLayout(new BorderLayout());
AddButton add = new AddButton(id);
RemoveButton remove = new RemoveButton(id);
this.add(add, BorderLayout.WEST);
this.add(remove, BorderLayout.EAST);
carsInLot = new JLabel(Integer.toString(carCount[id]));
//carsInLot[id+2].setText("asdf");
carsInLot.setHorizontalAlignment(SwingConstants.CENTER);
carsInLot.setFont(mySmallFont);
this.add(carsInLot, BorderLayout.SOUTH);
this.add(lotName[id], BorderLayout.NORTH);
this.setPreferredSize(new Dimension(300, 300));
}
}
private void nameLots(){
//lotName[0] = new JLabel("Creek View"); //NUC only
lotName[0] = new JLabel("Jolly Giant");
lotName[1] = new JLabel("The one behind the gym");
lotName[2] = new JLabel("College Creek");
lotName[3] = new JLabel("BSS");
lotName[4] = new JLabel("CR A");
lotName[5] = new JLabel("CR B");
for(int i = 0; i <=5; i++){
lotName[i].setHorizontalAlignment(SwingConstants.CENTER);
lotName[i].setFont(mySmallFont);
}
}
private void showCarsInLots(){
JLabel test = new JLabel("Test");
schoolTiles[1].add(test, BorderLayout.SOUTH);
}
private class AddButton extends JButton{
int Id;
AddButton(int id){
this.setText("Add Car");
this.addActionListener(new AddCarListener());
Id = id+2;
}
public int getId(){
return Id;
}
}
private class RemoveButton extends JButton{
int Id;
RemoveButton(int id){
this.setText("Remove Car");
this.addActionListener(new RemoveCarListener());
Id = id+2;
}
public int getId(){
return Id;
}
}
private class AddCarListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent ae) {
try {
AddButton source = (AddButton)ae.getSource();
int Id = source.getId();
increaseCarCount(Id);
schoolTiles[Id-2].carsInLot.setText(Integer.toString(carCount[Id]));
//carsInLot[Id-1].setText(Integer.toString(carCount[Id-1]));
} catch (Exception ex) {
Logger.getLogger(DemoPanel.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
private class RemoveCarListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent ae) {
try {
RemoveButton source = (RemoveButton)ae.getSource();
int Id = source.getId();
decreaseCarCount(Id);
} catch (Exception ex) {
Logger.getLogger(DemoPanel.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
public static void getHTML(String urlToRead) throws Exception {
URL url = new URL(urlToRead);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
reader.close();
}
private String generateUrl(int lotNum){
String url = "https://wizards13467653673838292827.herokuapp.com/lot_update/"
+ String.valueOf(lotNum) + "/qty/" + String.valueOf(carCount[lotNum]);
return url;
}
private void increaseCarCount(int lotNum) throws Exception{
carCount[lotNum]++;
try{
String url = generateUrl(lotNum);
getHTML(url);
} catch(java.io.IOException e){
System.out.println("Car added to lot " + lotNum);
}
}
private void decreaseCarCount(int lotNum) throws Exception{
carCount[lotNum]--;
try{
String url = generateUrl(lotNum);
getHTML(url);
} catch(java.io.IOException e){
System.out.println("Removed car from lot " + lotNum);
}
}
}