-
Notifications
You must be signed in to change notification settings - Fork 0
/
HomePanel.java
203 lines (176 loc) · 6.64 KB
/
HomePanel.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
/*
* Team: Mary DuBard, Hannah Murphy, Alyssa Rivera
* Writer for this Class: Mary DuBard
*
* File name: HomePanel.java
* Date Created: 12/8/15
* Last Updated: 12/13/15
* Known Bugs: None
*
* Class that contains Panel elements for the Home tab of the Walking Effect GUI
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.event.*;
import java.io.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
public class HomePanel extends JPanel implements ComponentListener{
private JLabel header, origin, destination, footer;
private JPanel navi, map;
private static JComboBox orig, dest;
private JButton submit;
private static JTextArea keyText;
private JTextPane directions;
private String[] locs;
private Map m;
private JCheckBox stairs;
private JCheckBox steep;
public static void setKeyText(String[] locs){
keyText.setText("");
keyText.append("Map Key: ");
//keyText.setRows(1);
for(int i = 0; i < locs.length; i++){
keyText.append("\n");
keyText.append(locs[i]);
}
//keyText.setFont(keyFont);
}
public static void setComboBoxes(String[] locs){
orig.removeAllItems();
for(int i = 0; i < locs.length; i++){
orig.addItem(locs[i]);
}
dest.removeAllItems();
for(int i = 0; i < locs.length; i++){
dest.addItem(locs[i]);
}
//orig.addItem(locs);
//keyText.setFont(keyFont);
}
public HomePanel(Map ma){
//on load reload locs?
m = ma;
setLayout (new BorderLayout());
//set font to default as helvetica
Font headerFont = new Font("Helvetica", Font.PLAIN, 18);
Font customFont = new Font("Helvetica", Font.PLAIN, 15);
Font keyFont = new Font("Helvetica", Font.PLAIN, 13);
try {
//create the font to use. Specify the size!
headerFont = Font.createFont(Font.TRUETYPE_FONT, new File("fontBold.ttf")).deriveFont(25f);
customFont = Font.createFont(Font.TRUETYPE_FONT, new File("font.ttf")).deriveFont(20f);
keyFont = Font.createFont(Font.TRUETYPE_FONT, new File("font.ttf")).deriveFont(17f);
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
//register the font
ge.registerFont(Font.createFont(Font.TRUETYPE_FONT, new File("font.ttf")));
} catch (IOException e) {
e.printStackTrace();
}
catch(FontFormatException e)
{
e.printStackTrace();
}
header = new JLabel("\nPick your origin and destination below to receive the quickest path between them!", SwingConstants.CENTER);
header.setFont(headerFont);
locs = m.getLocations();
//initialize combo boxes, using String array ratings for values
orig = new JComboBox(locs);
orig.setFont(keyFont);
dest = new JComboBox(locs);
dest.setFont(keyFont);
//initializes labels for combo boxes
origin = new JLabel("Origin: ");
origin.setFont(customFont);
destination = new JLabel("Destination: ");
destination.setFont(customFont);
//creates submit button
submit = new JButton("Submit");
submit.addActionListener(new submitListener());
submit.setFont(customFont);
//Creates panel for navigation options
navi = new JPanel();
navi.setLayout(new BoxLayout(navi, BoxLayout.Y_AXIS));
navi.add(Box.createRigidArea(new Dimension(0, 50)));
navi.add(origin);
navi.add(Box.createRigidArea(new Dimension(0, 5)));
navi.add(orig);
navi.add(Box.createRigidArea(new Dimension(0, 50)));
navi.add(destination);
navi.add(dest);
navi.add(Box.createRigidArea(new Dimension(0, 100)));
navi.add(submit);
navi.add(Box.createRigidArea (new Dimension (0, 100)));
//creates panel for map and key
map = new JPanel();
map.setLayout(new BoxLayout(map, BoxLayout.X_AXIS));
//map.add(Box.createRigidArea(new Dimension(100, 0)));
//adds map image
try{
BufferedImage myPicture = ImageIO.read(new File("map.png"));
ImageIcon pic = new ImageIcon(myPicture);
Image img = pic.getImage();
BufferedImage bi = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB);
Graphics g = bi.createGraphics();
g.drawImage(img, 100, 50, 500, 350, null); //locates and sizes image
ImageIcon newIcon = new ImageIcon(bi);
JLabel picLabel = new JLabel(newIcon); //adds image to label
map.add(picLabel); //adds label to panel
}
catch(IOException io){
System.out.println(io);
}
keyText = new JTextArea(12, 20);
keyText.setMaximumSize(keyText.getPreferredSize());
//keyText.setEditable(false);
keyText.append("Map Key: ");
//keyText.setRows(1);
for(int i = 0; i < locs.length; i++){
keyText.append("\n");
keyText.append(locs[i]);
}
keyText.setFont(keyFont);
JScrollPane jp = new JScrollPane(keyText);
jp.setMaximumSize(keyText.getPreferredSize());
map.add(jp);
//Initializes footer
directions = new JTextPane();
directions.setEditable(false);
directions.setText("<Directions here>");
directions.setFont(customFont);
StyledDocument doc = directions.getStyledDocument();
SimpleAttributeSet center = new SimpleAttributeSet();
StyleConstants.setAlignment(center, StyleConstants.ALIGN_CENTER);
doc.setParagraphAttributes(0, doc.getLength(), center, false);
//JScrollPane jp2 = new JScrollPane(directions);
//jp2.setMaximumSize(directions.getPreferredSize());
//adds elements to frame
add(header, BorderLayout.NORTH);
add(navi, BorderLayout.WEST);
add(map, BorderLayout.EAST);
add(directions, BorderLayout.SOUTH);
}
public void componentHidden(ComponentEvent e){}
public void componentMoved(ComponentEvent e){}
public void componentResized(ComponentEvent e){}
//rewrite the key when tab shown again
public void componentShown(ComponentEvent e){
locs = m.getLocations();
keyText.replaceRange("Map Key: ", 0, keyText.getLineCount()-1);
//keyText.setRows(1);
for(int i = 0; i < locs.length; i++){
keyText.append("\n");
keyText.append(locs[i]);
}
}
private class submitListener implements ActionListener{
public void actionPerformed(ActionEvent event){
//save combo box values as a string, if no value was chosen, the default value is 1
String origString = orig.getSelectedItem().toString();
String destString = dest.getSelectedItem().toString();
directions.setText("Directions from " + origString + " to " + destString + ": " + "\n" + m.directionsString(m.findLocation(origString), m.findLocation(destString)));
}
}
}