Passed
Push — master ( 3286bc...c7e1c0 )
by Seth
05:56 queued 03:30
created

example.RotationExample.mouseEntered(MouseEvent)   A

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
dl 0
loc 2
rs 10
1
package example;
2
3
import org.gannacademy.cdf.graphics.Text;
4
import org.gannacademy.cdf.graphics.geom.Path;
5
import org.gannacademy.cdf.graphics.ui.AppWindow;
6
7
import java.awt.event.MouseEvent;
8
import java.awt.event.MouseListener;
9
import java.awt.geom.AffineTransform;
10
import java.awt.geom.PathIterator;
11
import java.awt.geom.Point2D;
12
13
public class RotationExample extends AppWindow implements MouseListener {
14
    private static final double
15
            BASE = 50,
16
            HEIGHT = 100,
17
            THETA = Math.PI / 180.0;
18
    private Path path;
19
    private double x, y;
20
    private boolean mouseDown;
21
    private int button;
22
23
    @Override
24
    protected void setup() {
25
        // show instructions to user
26
        new Text("Left, middle, or right-click to rotate the triangle", 10, 40, getDrawingPanel());
27
        x = getDrawingPanel().getWidth() / 2.0;
28
29
        // calculate top point of triangle
30
        y = (getDrawingPanel().getHeight() - HEIGHT) / 2.0;
31
32
        // define the triangle path
33
        path = new Path(getDrawingPanel());
34
        path.moveTo(x, y); // move to top point
35
        path.lineTo(x + BASE / 2.0, y + HEIGHT); // line to bottom-right point
36
        path.lineTo(x - BASE / 2.0, y + HEIGHT); // line to bottom-left point
37
        path.lineTo(x, y); // line back to top point
38
39
        // register to be notified of mouse events (and assume no one is clicking right now)
40
        mouseDown = false;
41
        getDrawingPanel().addMouseListener(this);
42
        getDrawingPanel().requestFocus();
43
    }
44
45
    @Override
46
    protected void loop() {
47
        if (mouseDown) {
48
49
            double theta = THETA; // default, rotate clockwise around mouse
50
            double x = this.x, y = this.y; // default, use whatever (x, y) is currently set for axis of rotation
0 ignored issues
show
Comprehensibility introduced by
The variable xshadows a field with the same name declared in line 19. Consider renaming it.
Loading history...
Comprehensibility introduced by
The variable yshadows a field with the same name declared in line 19. Consider renaming it.
Loading history...
51
52
            if (button == MouseEvent.BUTTON1) theta *= -1; // left button, rotate counter-clockwise around mouse
53
54
            else if (button == MouseEvent.BUTTON2) { // middle button, rotate around center of gravity
55
                // use center of gravity calculation as rotation axis
56
                Point2D center = getCenterOfGravity();
57
                x = center.getX();
58
                y = center.getY();
59
            }
60
61
            // rotate, using any adjustments to theta and axis of rotiation
62
            path.transform(AffineTransform.getRotateInstance(theta, x, y));
63
        }
64
        sleep(25);
65
    }
66
67
    public Point2D getCenterOfGravity() {
68
        PathIterator i = path.getPathIterator(null);
69
        double[] coords = new double[6]; // to hold coordinates
70
        double x = 0, y = 0; // tally x and y-coordinates
0 ignored issues
show
Comprehensibility introduced by
The variable xshadows a field with the same name declared in line 19. Consider renaming it.
Loading history...
Comprehensibility introduced by
The variable yshadows a field with the same name declared in line 19. Consider renaming it.
Loading history...
71
        int points = 0; // haven't visited any points yet
72
        while (!i.isDone()) {
73
            if (points != 0) { // skip the first visit to the top point
74
                i.currentSegment(coords);
75
                x += coords[0];
76
                y += coords[1];
77
            }
78
            i.next();
79
            if (!i.isDone()) points++; // don't double-count last point!
80
        }
81
82
        // package (x, y) coordinates into a single object
83
        return new Point2D.Double(x / points, y / points);
0 ignored issues
show
introduced by
Make sure "points" can't be zero before doing this division.
Loading history...
84
    }
85
86
    @Override
87
    public void mouseClicked(MouseEvent e) {
0 ignored issues
show
Bug introduced by
An empty method may be confusing. Add a nested comment explaining why this method is empty, throw an UnsupportedOperationException or complete the implementation.
Loading history...
88
    }
89
90
    @Override
91
    public void mousePressed(MouseEvent e) {
92
        mouseDown = true;
93
        button = e.getButton();
94
        x = e.getX();
95
        y = e.getY();
96
    }
97
98
    @Override
99
    public void mouseReleased(MouseEvent e) {
100
        mouseDown = false;
101
    }
102
103
    @Override
104
    public void mouseEntered(MouseEvent e) {
0 ignored issues
show
Bug introduced by
An empty method may be confusing. Add a nested comment explaining why this method is empty, throw an UnsupportedOperationException or complete the implementation.
Loading history...
105
    }
106
107
    @Override
108
    public void mouseExited(MouseEvent e) {
0 ignored issues
show
Bug introduced by
An empty method may be confusing. Add a nested comment explaining why this method is empty, throw an UnsupportedOperationException or complete the implementation.
Loading history...
109
    }
110
111
    public static void main(String[] args) {
112
        new RotationExample();
113
    }
114
}
115