-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDisplayAddress.java
More file actions
170 lines (136 loc) · 5.61 KB
/
DisplayAddress.java
File metadata and controls
170 lines (136 loc) · 5.61 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
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
//behavior for the JFrame to display if they choose to generate a map for the incident
public class DisplayAddress implements DisplayBehavior {
//variable incident to be able to generate and set a
// new windows behavior after the address is selected
private static String incident;
DisplayAddress(String incident){
this.incident = incident;
}
/*
Creates/display the behavior of window(how it looks/fields of user input)
Contains all the user input which we will use to create the email to be sent out
*/
public void display() throws IOException {
JFrame frame = new JFrame("Campus Security");
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
frame.setLocation(430, 100);
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
frame.add(panel);
ArrayList<String> Buildings2 = read_addresses();
String[] Buildings = Buildings2.toArray(new String[0]);
final JComboBox<String> cb = new JComboBox<String>(Buildings);
cb.setMaximumSize(cb.getPreferredSize());
cb.setAlignmentX(Component.CENTER_ALIGNMENT);
panel.add(cb);
JLabel lbl2 = new JLabel("select the radius( in miles)");
lbl2.setAlignmentX(Component.CENTER_ALIGNMENT);
panel.add(lbl2);
JSlider sld = new JSlider(1, 10, 5);
sld.setMaximumSize(sld.getPreferredSize());
sld.setAlignmentX(Component.CENTER_ALIGNMENT);
panel.add(sld);
sld.setPaintTicks(true);
sld.setMajorTickSpacing(5);
JLabel lbl3 = new JLabel("5");
panel.add(lbl3);
sld.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent changeEvent) {
lbl3.setText(String.valueOf(sld.getValue()));
}
});
JButton btn = new JButton("OK");
btn.setAlignmentX(Component.CENTER_ALIGNMENT);
panel.add(btn);
frame.setVisible(true);
btn.addActionListener(new ActionListener(){
private String incident;
/*
adding action listener for button "ok"
to generate and save the map based off the address selected
Then it creates a new WindowTemplate depending on the incident
*/
@Override
public void actionPerformed(ActionEvent e) {
String mapAddress = (String)cb.getSelectedItem();
String radius = (String)lbl3.getText();
if(!mapAddress.equals("select") && !radius.equals("select")) {
saveMap(mapAddress, radius);
}
try {
frame.dispose();
Window incident = new WindowTemplates(DisplayAddress.incident);
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
});
}
//method that reads in all the schools properties
//used for the dropdown for addresses to generate a map from
private static ArrayList<String> read_addresses() throws IOException {
/*Function to read the file with current or of interest Moravian College Properties*/
File file = new File("/Users/rsballek/Sprint01/CollegeProperties");
ArrayList<String> buildings = new ArrayList<>();
BufferedReader br = new BufferedReader(new FileReader(file));
String st;
//add each property to the Array
while ((st = br.readLine()) != null) {
buildings.add(st);
}
return buildings;
}
//saves the image if they choose to have a map generated
private static void saveMap(String mapAddress, String radius){
BufferedImage image;
String CITY = "Behlehem";
String STATE = "PA";
String KEY ="AIzaSyB-rmfGuWb9kl3C3wobelo-V-fRaEsVETQ";
String newStr = mapAddress.replaceAll(" ", "+");
System.out.println(newStr);
try{
String address = "https://maps.googleapis.com/maps/api/staticmap?center="+newStr+","+CITY+","+STATE+"&zoom=16&size=400x400&key="+KEY;
URL url =new URL(address);
// // read the url
image = ImageIO.read(url);
System.out.println(image);
// for png
ImageIO.write(image, "png",new File("/Users/rsballek/Sprint01/map.png"));
// for jpg
ImageIO.write(image, "jpg",new File("/Users/rsballek/Sprint01/map.jpg"));
}catch(IOException e){
e.printStackTrace();
}
}
private static void mapOnline(String address) throws URISyntaxException, IOException {
/*
* General function to display a map online
*
* TODO
*Generalize the function to display any of Moravian College properties with the GUI
*
* */
if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) {
Desktop.getDesktop().browse(new URI("https://www.google.com/maps/search/?api=1&query="+address));
}
}
}