gann-cdf /
graphics
| 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
Loading history...
|
|||
| 14 | private Tool active; |
||
| 15 | private Point2D start; |
||
|
0 ignored issues
–
show
|
|||
| 16 | private Drawable current; |
||
|
0 ignored issues
–
show
|
|||
| 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
|
|||
| 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
|
|||
| 57 | |||
| 58 | } |
||
| 59 | |||
| 60 | @Override |
||
| 61 | public void mouseExited(MouseEvent e) { |
||
|
0 ignored issues
–
show
|
|||
| 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
|
|||
| 81 | |||
| 82 | } |
||
| 83 | |||
| 84 | private enum Tool {Rect, none} |
||
| 85 | } |
||
| 86 |