example.swing.Ball.move()   A
last analyzed

Complexity

Conditions 5

Size

Total Lines 7
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
c 1
b 0
f 0
dl 0
loc 7
rs 9.3333
eloc 6
1
package example.swing;
2
3
import org.gannacademy.cdf.graphics.geom.Ellipse;
4
import org.gannacademy.cdf.graphics.ui.DrawingPanel;
5
6
import java.awt.*;
7
8
public class Ball {
9
    private Ellipse ball;
0 ignored issues
show
Comprehensibility introduced by
A field should not duplicate the name of its containing class. While technically legal, this practice may be confusing.
Loading history...
10
    private double dx, dy;
11
12
    public Ball(DrawingPanel panel) {
13
        double heading = Math.random() * Math.PI * 2;
14
        dx = Math.cos(heading);
15
        dy = Math.sin(heading);
16
17
        ball = new Ellipse(
18
                Math.random() * panel.getWidth(),
19
                Math.random() * panel.getHeight(),
20
                20, 20,
21
                panel
22
        );
23
        ball.setFillColor(new Color((float) Math.random(), (float) Math.random(), (float) Math.random()));
24
        ball.setStrokeColor(ball.getFillColor());
25
    }
26
27
    public void move() {
28
        ball.translate(dx, dy);
29
        if (ball.getX() < 0 || ball.getX() + ball.getWidth() > ball.getDrawingPanel().getWidth()) {
30
            dx = -dx;
31
        }
32
        if (ball.getY() < 0 || ball.getY() + ball.getWidth() > ball.getDrawingPanel().getHeight()) {
33
            dy = -dy;
34
        }
35
    }
36
}
37