Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private DSShape r, oldr;
- private int index;
- private double dx;
- private double dy;
- private double distx, disty;
- private Point2D c;
- /******************************************************************************
- * We scale the shape by remembering the initial distance from the centre
- * where the mouse was first pressed. We then scale by the ratio of the new
- * distance to the centre to the old distance. To keep things simple, we
- * keep a clone of the original shape and draw the changes relative to the
- * original
- *****************************************************************************/
- public void mousePressed(MouseEvent e) {
- r = canvas.getShapes().findShape(getPoint(e));
- if (r == null)
- return;
- canvas.select(r);
- oldr = (DSShape) r.clone();
- index = canvas.getShapes().indexOf(r);
- canvas.repaint();
- Point i = gridPoint(e);
- c = r.centre();
- /******************************************************************************
- * Set them to at least 5 to avoid divide by zero errors
- *****************************************************************************/
- distx = Math.max(5, (i.getX() - c.getX()));
- disty = Math.max(5, (i.getY() - c.getY()));
- }
- public void mouseDragged(MouseEvent e) {
- if (r != null) {
- Point n = gridPoint(e);
- dx = (n.getX() - c.getX()) / (distx);
- dy = (n.getY() - c.getY()) / (disty);
- DSShape newr = (DSShape) oldr.clone();
- newr.scale(c, dx, dy);
- canvas.getShapes().set(index, newr);
- canvas.select(newr);
- canvas.repaint();
- }
- }
- public void mouseReleased(MouseEvent e) {
- if (r != null) {
- Point n = gridPoint(e);
- dx = (n.getX() - c.getX()) / (distx);
- dy = (n.getY() - c.getY()) / (disty);
- r.scale(c, dx, dy);
- canvas.repaint();
- }
- }
Add Comment
Please, Sign In to add comment