-
Notifications
You must be signed in to change notification settings - Fork 1
/
Plotter.java
157 lines (128 loc) · 3.52 KB
/
Plotter.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
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
/*
* Main class. Acts as the controller and the main view.
* Extends JFrame -- it is a frame
* Implements ActionListener and MouseListener..i.e. it handles all button and mouse clicks
*/
public class Plotter extends JFrame implements ActionListener, MouseListener {
/*
* Attributes -- this class has access to the data,
* the point explorer panel and the plot component
*/
private PointExplorer pointExplorer;
private DataList plotData;
private BasicPlot scatter;
public Plotter() {
// Standard JFrame things...
this.setSize(1000,800);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
/*
* Create the main panel. This will have two columns
* The left will be used for the plot, the right
* for the pointExplorer
*/
JPanel mainPanel = new JPanel(new GridLayout(0,2));
/*
* Create the DataList and add some initial points.
*/
plotData = new DataList();
plotData.addPoint(10, 10);
plotData.addPoint(50, 5);
plotData.addPoint(200, 110);
/*
* Create the scatter plot component and add it to
* mainPanel
*/
scatter = new BasicPlot(plotData,this);
mainPanel.add(scatter);
/*
* Create the pointExplorer and add it to mainPanel
*/
pointExplorer = new PointExplorer(plotData,this);
mainPanel.add(pointExplorer);
/*
* Add mainPanel to the frame
*/
this.add(mainPanel);
/*
* Make the frame visible
*/
this.setVisible(true);
}
/*
* Button click controller for the pointExplorer object
* We will reach this when a button is clicked
*/
@Override
public void actionPerformed(ActionEvent e) {
/*
* Check that the action came from the expected button
* we don't strictly need to do this, but its good practice
* as if you add more buttons you'll need it
*/
if(e.getSource() == pointExplorer.getAddButton()) {
// Get the data that has been entered as a Point
Point p = pointExplorer.getNewPoint();
// Add the point to the dataset
plotData.addPoint(p);
// Redraw the plot
scatter.repaint();
// Update the pointExplorer
pointExplorer.updateArea();
}
}
/*
* Mouse click controller for the scatter component
* Only the plot component has this class as a Mouse Listener
* So we will only enter this method if the mouse is clicked
* on the scatter component
*/
@Override
public void mouseClicked(MouseEvent e) {
// Clicked inside the scatter object
// Step1: get the position of the click...
int clickX = e.getX();
int clickY = e.getY();
// Make a new point
Point p = new Point(clickX,clickY);
// Add it to the data
plotData.addPoint(p);
// Redraw the plot
scatter.repaint();
// Update the pointExplorer
pointExplorer.updateArea();
}
/*
* Following methods are essential to have
* if a class implements mouseListener
* They are left blank
*/
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
/*
* Main method -- creates the Plotter object
*/
public static void main(String[] args) {
new Plotter();
}
}