Coverage details for edu.uci.ics.jung.visualization.control.RotatingGraphMousePlugin

LineHitsSource
1 /*
2  * Copyright (c) 2005, the JUNG Project and the Regents of the University
3  * of California
4  * All rights reserved.
5  *
6  * This software is open-source under the BSD license; see either
7  * "license.txt" or
8  * http://jung.sourceforge.net/license.txt for a description.
9  * Created on Mar 8, 2005
10  *
11  */
12 package edu.uci.ics.jung.visualization.control;
13  
14 import java.awt.BasicStroke;
15 import java.awt.Color;
16 import java.awt.Cursor;
17 import java.awt.Dimension;
18 import java.awt.Graphics2D;
19 import java.awt.Point;
20 import java.awt.RenderingHints;
21 import java.awt.Toolkit;
22 import java.awt.event.MouseEvent;
23 import java.awt.event.MouseListener;
24 import java.awt.event.MouseMotionListener;
25 import java.awt.geom.Point2D;
26 import java.awt.image.BufferedImage;
27 import java.util.Collections;
28  
29 import edu.uci.ics.jung.visualization.VisualizationViewer;
30 import edu.uci.ics.jung.visualization.transform.MutableTransformer;
31  
32 /**
33  * RotatingGraphMouse provides the abiity to rotate the graph using
34  * the mouse. By default, it is activated by mouse button one drag
35  * with the shift key pressed. The modifiers can be overridden so that
36  * a different mouse/key combination activates the rotation
37  *
38  * @author Tom Nelson
39  */
40 public class RotatingGraphMousePlugin extends AbstractGraphMousePlugin
41     implements MouseListener, MouseMotionListener {
42  
43     /**
44      * create an instance with default modifier values
45      */
46     public RotatingGraphMousePlugin() {
470        this(MouseEvent.BUTTON1_MASK | MouseEvent.SHIFT_MASK);
480    }
49  
50     /**
51      * create an instance with passed zoom in/out values
52      * @param modifiers the event modifiers to trigger rotation
53      */
54     public RotatingGraphMousePlugin(int modifiers) {
550        super(modifiers);
560        Dimension cd = Toolkit.getDefaultToolkit().getBestCursorSize(16,16);
570        BufferedImage cursorImage =
58                 new BufferedImage(cd.width,cd.height,BufferedImage.TYPE_INT_ARGB);
590        Graphics2D g = cursorImage.createGraphics();
600        g.addRenderingHints(Collections.singletonMap(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON));
610        g.setColor(new Color(0,0,0,0));
620        g.fillRect(0,0,16,16);
63  
640        g.setColor(Color.white);
650        g.setStroke(new BasicStroke(3));
660        int left = 0;
670        int top = 0;
680        int right = 15;
690        int bottom = 15;
70         
710        g.setColor(Color.black);
720        g.setStroke(new BasicStroke(1));
73         // top bent line
740        g.drawLine(left+2,top+6,right/2+1,top);
750        g.drawLine(right/2+1,top,right-2,top+5);
76         // bottom bent line
770        g.drawLine(left+2,bottom-6,right/2,bottom);
780        g.drawLine(right/2,bottom,right-2,bottom-6);
79         // top arrow
800        g.drawLine(left+2,top+6,left+5,top+6);
810        g.drawLine(left+2,top+6,left+2,top+3);
82         // bottom arrow
830        g.drawLine(right-2,bottom-6,right-6,bottom-6);
840        g.drawLine(right-2, bottom-6,right-2,bottom-3);
85  
860        g.dispose();
87         
880        cursor = Toolkit.getDefaultToolkit().createCustomCursor(cursorImage, new Point(), "RotateCursor");
890    }
90  
91     /**
92      * save the 'down' point and check the modifiers. If the
93      * modifiers are accepted, set the cursor to the 'hand' cursor
94      * @param e the event
95      */
96     public void mousePressed(MouseEvent e) {
970        VisualizationViewer vv = (VisualizationViewer)e.getSource();
980           boolean accepted = checkModifiers(e);
990           down = e.getPoint();
1000          if(accepted) {
1010               vv.setCursor(cursor);
102            }
1030    }
104     
105     /**
106      * unset the down point and change the cursor back to the default
107      */
108     public void mouseReleased(MouseEvent e) {
1090        VisualizationViewer vv = (VisualizationViewer)e.getSource();
1100        down = null;
1110        vv.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
1120    }
113     
114     /**
115      * check the modifiers. If accepted, use the mouse drag motion
116      * to rotate the graph
117      */
118     public void mouseDragged(MouseEvent e) {
1190        if(down == null) return;
1200        VisualizationViewer vv = (VisualizationViewer)e.getSource();
1210        boolean accepted = checkModifiers(e);
1220        if(accepted) {
1230            MutableTransformer modelTransformer =
124                 vv.getLayoutTransformer();
125             // rotate
1260            vv.setCursor(cursor);
127             
1280            Point2D center = vv.getCenter();
1290            Point2D q = down;
1300            Point2D p = e.getPoint();
1310            Point2D v1 = new Point2D.Double(center.getX()-p.getX(), center.getY()-p.getY());
1320            Point2D v2 = new Point2D.Double(center.getX()-q.getX(), center.getY()-q.getY());
1330            double theta = angleBetween(v1, v2);
1340            modelTransformer.rotate(theta, vv.inverseViewTransform(center));
1350            down.x = e.getX();
1360            down.y = e.getY();
137         
1380            e.consume();
139         }
1400    }
141     
142     /**
143      * Returns the angle between two vectors from the origin
144      * to points v1 and v2.
145      * @param v1
146      * @param v2
147      * @return
148      */
149     protected double angleBetween(Point2D v1, Point2D v2) {
1500        double x1 = v1.getX();
1510        double y1 = v1.getY();
1520        double x2 = v2.getX();
1530        double y2 = v2.getY();
154         // cross product for direction
1550        double cross = x1*y2 - x2*y1;
1560        int cw = 1;
1570        if(cross > 0) {
1580            cw = -1;
159         }
160         // dot product for angle
1610        double angle =
162             cw*Math.acos( ( x1*x2 + y1*y2 ) /
163                 ( Math.sqrt( x1*x1 + y1*y1 ) *
164                         Math.sqrt( x2*x2 + y2*y2 ) ) );
1650        if(Double.isNaN(angle)) {
1660            angle = 0;
167         }
1680        return angle;
169     }
170  
171     public void mouseClicked(MouseEvent e) {
1720    }
173  
174     public void mouseEntered(MouseEvent e) {
1750    }
176  
177     public void mouseExited(MouseEvent e) {
1780    }
179  
180     public void mouseMoved(MouseEvent e) {
1810    }
182 }

this report was generated by version 1.0.5 of jcoverage.
visit www.jcoverage.com for updates.

copyright © 2003, jcoverage ltd. all rights reserved.
Java is a trademark of Sun Microsystems, Inc. in the United States and other countries.