-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBasicPlot.java
68 lines (60 loc) · 1.71 KB
/
BasicPlot.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
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.MouseListener;
import java.awt.geom.Ellipse2D;
import javax.swing.JComponent;
public class BasicPlot extends JComponent {
private MouseListener mouseListener;
private DataList dataList;
private final int radius = 6;
public BasicPlot(DataList dataList, MouseListener mouseListener) {
this.dataList = dataList;
this.mouseListener = mouseListener;
/*
* Tell Java which class will be responsible for handling mouse clicks
* on this component. In this case it will be the Plotter class, a reference
* to which is stored in mouseListener
*/
this.addMouseListener(this.mouseListener);
}
/*
* paintComponent method that is called by Java whenever someone calls repaint
* on the component, or when it gets moved / resized etc
*/
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
/*
* Make a big white rectangle that covers the whole of this component
*/
int width = this.getWidth();
int height = this.getHeight();
/*
* (0,0) is the top left, (width,height) is the bottom right
*/
Rectangle r = new Rectangle(0,0,width,height);
g2.draw(r);
g2.setPaint(Color.WHITE);
g2.fill(r);
/*
* Set the color to red
*/
g2.setPaint(Color.RED);
/*
* Plot the points
*/
for(int i=0;i<this.dataList.getN();i++) {
// Get the ith point
Point p = this.dataList.getPoint(i);
// Get its x and y values
int x = p.getX();
int y = p.getY();
// make the ellipse object
Ellipse2D.Double e = new Ellipse2D.Double(x-radius, y-radius, radius*2, radius*2);
// draw and fill
g2.draw(e);
g2.fill(e);
}
}
}