• API
• FAQ
• Tools
• Archive
SHARE
TWEET

# Untitled

a guest Nov 20th, 2018 75 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. private DSShape r, oldr;
2.     private int index;
3.     private double dx;
4.     private double dy;
5.     private double distx, disty;
6.     private Point2D c;
7.
8.     /******************************************************************************
9.      * We scale the shape by remembering the initial distance from the centre
10.      * where the mouse was first pressed. We then scale by the ratio of the new
11.      * distance to the centre to the old distance. To keep things simple, we
12.      * keep a clone of the original shape and draw the changes relative to the
13.      * original
14.      *****************************************************************************/
15.     public void mousePressed(MouseEvent e) {
16.         r = canvas.getShapes().findShape(getPoint(e));
17.         if (r == null)
18.             return;
19.         canvas.select(r);
20.         oldr = (DSShape) r.clone();
21.         index = canvas.getShapes().indexOf(r);
22.         canvas.repaint();
23.         Point i = gridPoint(e);
24.         c = r.centre();
25. /******************************************************************************
26.  * Set them to at least 5 to avoid divide by zero errors
27.  *****************************************************************************/
28.         distx = Math.max(5, (i.getX() - c.getX()));
29.         disty = Math.max(5, (i.getY() - c.getY()));
30.     }
31.
32.     public void mouseDragged(MouseEvent e) {
33.         if (r != null) {
34.             Point n = gridPoint(e);
35.             dx = (n.getX() - c.getX()) / (distx);
36.             dy = (n.getY() - c.getY()) / (disty);
37.             DSShape newr = (DSShape) oldr.clone();
38.             newr.scale(c, dx, dy);
39.             canvas.getShapes().set(index, newr);
40.             canvas.select(newr);
41.             canvas.repaint();
42.         }
43.     }
44.
45.     public void mouseReleased(MouseEvent e) {
46.         if (r != null) {
47.             Point n = gridPoint(e);
48.             dx = (n.getX() - c.getX()) / (distx);
49.             dy = (n.getY() - c.getY()) / (disty);
50.             r.scale(c, dx, dy);
51.             canvas.repaint();
52.         }
53.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top