-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCircleView.java
executable file
·45 lines (38 loc) · 1.02 KB
/
CircleView.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
import java.awt.*;
import java.awt.event.*;
public class CircleView extends javax.swing.JPanel {
private CircleModel model;
/** Set a model */
public void setModel(CircleModel newModel) {
model = newModel;
if (model != null)
// Register the view as listener for the model
model.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
repaint();
}
});
}
public CircleModel getModel() {
return model;
}
@Override
protected void paintComponent(Graphics g) {
if (model != null) {
super.paintComponent(g);
g.setColor(model.getColor());
int xCenter = getWidth() / 2;
int yCenter = getHeight() / 2;
int radius = (int)model.getRadius();
if (model.isFilled()) {
g.fillOval(xCenter - radius, yCenter - radius,
2 * radius, 2 * radius);
}
else {
g.drawOval(xCenter - radius, yCenter - radius,
2 * radius, 2 * radius);
}
}
}
}