-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCanvasView.java
More file actions
executable file
·135 lines (113 loc) · 3.32 KB
/
CanvasView.java
File metadata and controls
executable file
·135 lines (113 loc) · 3.32 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 java.io.*;
import java.util.*;
import java.awt.*;
import javax.swing.*;
import javax.imageio.*;
import java.awt.event.*;
/*
enum Tool {
select, erase, line, circle, rectangle, bucket;
}
*/
public class CanvasView extends JPanel implements IView {
private Model model;
private double width = 800;
private double height = 700;
private int originX;
private int originY;
public void drawAllShapes(Graphics2D gc) {
for (JShape s: model.shapes) {
if (s.fillColor != null) {
gc.setColor(s.fillColor);
gc.fill(s.shape);
}
gc.setStroke(new BasicStroke(model.convertThicknessToPx(s.thickness)));
gc.setColor(s.strokeColor);
//Reference: https://docs.oracle.com/javase/tutorial/2d/geometry/strokeandfill.html
if (model.selectedShape == s) {
float dash1[] = {10.0f};
gc.setStroke(new BasicStroke(1.0f,
BasicStroke.CAP_BUTT,
BasicStroke.JOIN_MITER,
10.0f, dash1, 0.0f));
}
gc.draw(s.shape);
}
if (model.currentShape != null) {
gc.setStroke(new BasicStroke(model.convertThicknessToPx(model.currentShape.thickness)));
gc.setColor(model.currentColor);
gc.draw(model.currentShape.shape);
}
}
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D gc = (Graphics2D) g;
drawAllShapes(gc);
}
public void updateView() {
this.repaint();
}
public CanvasView(Model model1) {
model = model1;
this.setLayout(new FlowLayout());
this.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
this.setBackground(Color.WHITE);
this.setBorder(BorderFactory.createLineBorder(Color.BLACK));
this.setPreferredSize(new Dimension(700, 400));
Tool currentTool = model.currentTool;
this.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e)
{
originX = (int) e.getX();
originY = (int) e.getY();
System.out.println("Mouse Pressed");
if (currentTool == Tool.select) {
model.selectShape((int)originX, (int)originY);
} else if (model.currentTool == Tool.erase) {
model.eraseShape(originX, originY);
} else if (model.currentTool == Tool.bucket) {
model.fillShape(originX, originY);
} else if (model.currentTool == Tool.rectangle ||
model.currentTool == Tool.circle ||
model.currentTool == Tool.line) {
model.drawShape(0, 0, 0, 0);
}
}
//DEBUGGING TESTING
@Override
public void mouseEntered (MouseEvent evt) {
System.out.println("mouseEntered");
}
@Override
public void mouseExited (MouseEvent evt) {
System.out.println("mouseExited");
}
@Override
public void mouseReleased(MouseEvent e) {
System.out.println("Mouse Released");
if (model.currentTool == Tool.rectangle ||
model.currentTool == Tool.circle ||
model.currentTool == Tool.line) {
model.addShape();
}
}
});
this.addMouseMotionListener(new MouseAdapter() {
@Override
public void mouseDragged(MouseEvent e) {
System.out.println("Mouse Dragged");
int x = (int) e.getX();
int y = (int) e.getY();
if (model.currentTool == Tool.select) {
model.moveShape(x, y);
} else if (model.currentTool == Tool.rectangle ||
model.currentTool == Tool.circle ||
model.currentTool == Tool.line) {
model.drawShape((int)originX, (int)originY, x, y);
}
}
});
}
}