Issues (88)

src/main/java/projectstems/paint/PaintApp.java (7 issues)

1
package projectstems.paint;
2
3
import org.gannacademy.cdf.graphics.Drawable;
4
import org.gannacademy.cdf.graphics.geom.Rectangle;
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.event.MouseMotionListener;
10
import java.awt.geom.Point2D;
11
12
public class PaintApp extends AppWindow implements MouseListener, MouseMotionListener {
13
  private Rectangle rectButton;
0 ignored issues
show
Bug Best Practice introduced by
Fields like rectButton in a serializable class should either be transient or serializable.
Loading history...
14
  private Tool active;
15
  private Point2D start;
0 ignored issues
show
Bug Best Practice introduced by
Fields like start in a serializable class should either be transient or serializable.
Loading history...
16
  private Drawable current;
0 ignored issues
show
Bug Best Practice introduced by
Fields like current in a serializable class should either be transient or serializable.
Loading history...
17
18
  public static void main(String[] args) {
19
    new PaintApp();
20
  }
21
22
  @Override
23
  protected void setup() {
24
    rectButton = new Rectangle(5, 5, 15, 15, getDrawingPanel());
25
    active = Tool.none;
26
    getDrawingPanel().addMouseListener(this);
27
    getDrawingPanel().addMouseMotionListener(this);
28
  }
29
30
  @Override
31
  public void mouseClicked(MouseEvent e) {
32
    if (rectButton.contains(e.getPoint())) {
33
      active = Tool.Rect;
34
    }
35
  }
36
37
  @Override
38
  public void mousePressed(MouseEvent e) {
39
    switch (active) {
40
      case Rect:
0 ignored issues
show
Comprehensibility introduced by
End this switch case with an unconditional break, return or throw statement.
Loading history...
41
        if (!rectButton.contains(e.getPoint())) {
42
          start = e.getPoint();
43
          current = new Rectangle(e.getX(), e.getY(), 1, 1, getDrawingPanel());
44
        }
45
      case none:
46
        // do nothing
47
    }
48
  }
49
50
  @Override
51
  public void mouseReleased(MouseEvent e) {
52
    current = null;
53
  }
54
55
  @Override
56
  public void mouseEntered(MouseEvent e) {
0 ignored issues
show
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...
57
58
  }
59
60
  @Override
61
  public void mouseExited(MouseEvent e) {
0 ignored issues
show
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...
62
63
  }
64
65
  @Override
66
  public void mouseDragged(MouseEvent e) {
67
    if (active != Tool.none) {
68
      current.setWidth(Math.abs(e.getX() - start.getX()));
69
      current.setHeight(Math.abs(e.getY() - start.getY()));
70
      if (e.getX() < start.getX()) {
71
        current.setX(e.getX());
72
      }
73
      if (e.getY() < start.getY()) {
74
        current.setY(e.getY());
75
      }
76
    }
77
  }
78
79
  @Override
80
  public void mouseMoved(MouseEvent e) {
0 ignored issues
show
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...
81
82
  }
83
84
  private enum Tool {Rect, none}
85
}
86