-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathView.java
More file actions
58 lines (49 loc) · 1.8 KB
/
View.java
File metadata and controls
58 lines (49 loc) · 1.8 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
package a2;
import java.awt.BasicStroke;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class View extends JPanel implements IView {
// the model that this view is showing
private Model model;
View(Model model, Controller controller) {
this.setBorder(BorderFactory.createTitledBorder("Canvas"));
this.setPreferredSize(new Dimension(400, 400));
this.model = model;
addMouseListener(controller);
addMouseMotionListener(controller);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g; // cast to get 2D drawing methods
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
ArrayList<Polyline> strokes = model.getStrokes();
for (int j = 0; j < model.getSliderPos(); j++) {
Polyline stroke = strokes.get(j);
ArrayList<Point2D> points = stroke.getPoints();
if (points != null) {
int[] x_points = new int[points.size()];
int[] y_points = new int[points.size()];
for (int i=0; i < points.size(); i++) {
x_points[i] = (int)points.get(i).getX();
y_points[i] = (int)points.get(i).getY();
}
if (x_points != null) {
g2.setColor(stroke.getColour());
g2.setStroke(new BasicStroke(stroke.getStrokeThickness()));
g2.drawPolyline(x_points, y_points, points.size());
}
}
}
}
// IView interface
public void updateView() {
repaint();
}
}