Repository navigation
Expand file tree
/
Copy pathGUI.java
More file actions
135 lines (105 loc) · 4.6 KB
/
Copy pathGUI.java
File metadata and controls
135 lines (105 loc) · 4.6 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
import javax.swing.*;
import javax.swing.border.Border;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
public class GUI extends JFrame
{
private int mouseX, mouseY;
//private static ArrayList<Tree> trees = new ArrayList<Tree>();
public static int getScreenHeight()
{
return Toolkit.getDefaultToolkit().getScreenSize().height;
}
public static int getScreenWidth()
{
return Toolkit.getDefaultToolkit().getScreenSize().width;
}
public static int[] getScreenSize()
{
int[] screenSize = new int[]{getScreenWidth(), getScreenHeight()};
return screenSize;
}
public int getMouseX()
{
return mouseX;
}
public int getMouseY()
{
return mouseY;
}
public void setMouseX(int mouseX)
{
this.mouseX = mouseX;
}
public void setMouseY(int mouseY)
{
this.mouseY = mouseY;
}
public GUI(String fileName, String csvFileName) //takes the name of the image file and the name of the csv file as parameters, and displays the image file with the amount of trees in the area of the image that the user clicks on
{
FileReader read = new FileReader();
read.lines(csvFileName);
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
JFrame frame = new JFrame("Philly Tree Map");
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BorderLayout());
JLabel imgLabel = new JLabel("");
Border border = BorderFactory.createLineBorder(Color.BLACK, 1);
imgLabel.setBorder(border);
ImageIcon imgIcon = new ImageIcon(fileName);
Image img = imgIcon.getImage();
imgLabel.setIcon(imgIcon);
JLabel textLabel = new JLabel("Click on the map to display the amount of trees in that area.");
textLabel.setHorizontalAlignment(JLabel.CENTER);
textLabel.setVerticalAlignment(JLabel.CENTER);
textLabel.setMinimumSize(new Dimension(621, 50));
textLabel.setMaximumSize(new Dimension(621, 50));
textLabel.setPreferredSize(new Dimension(621, 50));
JPanel textPanel = new JPanel();
textPanel.add(textLabel, BorderLayout.NORTH);
textPanel.setPreferredSize(new Dimension(621, 50));
textPanel.setMinimumSize(new Dimension(621, 50));
textPanel.setMaximumSize(new Dimension(621, 50));
JPanel imgPanel = new JPanel();
imgPanel.add(imgLabel, BorderLayout.CENTER);
imgPanel.setPreferredSize(new Dimension(img.getWidth(null), img.getHeight(null)));
imgPanel.setMinimumSize(new Dimension(img.getWidth(null), img.getHeight(null)));
imgPanel.setMaximumSize(new Dimension(img.getWidth(null), img.getHeight(null)));
textLabel.setPreferredSize(new Dimension(500, 50));
textLabel.setMinimumSize(new Dimension(500, 50));
textLabel.setMaximumSize(new Dimension(500, 50));
mainPanel.add(imgPanel, BorderLayout.CENTER);
mainPanel.add(textPanel, BorderLayout.NORTH);
frame.setVisible(true);
frame.add(textLabel, BorderLayout.NORTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(getScreenWidth(), getScreenHeight());
frame.getContentPane().add(imgPanel);
MouseAdapter mouseAdapter = new MouseAdapter() {
@Override
public void mouseClicked(java.awt.event.MouseEvent e)
{
setMouseX(e.getX());
setMouseY(e.getY());
int near = Selection.getNearbyTrees(getMouseX(), getMouseY(), Selection.trees);
if(near > 0)
textLabel.setText("<html>Click on the map to display the amount of trees in that area.<br>" + "<center>There are " + near + " trees in a square mile from this point.</center>");
else
textLabel.setText("<html>Click on the map to display the amount of trees in that area.<br>" + "<center>There are currently no recorded trees in this area.</center>");
}
};
imgLabel.addMouseListener(mouseAdapter);
pack();
frame.setVisible(true);
}
public static void main(String[] args)
{
GUI gui = new GUI("PhilaCnty1854.png", "tree2022.csv");
}
}