PluggableGraphMouse を用いた,マウスによる Zoom と Pan の実現

// PluggableGraphMouse を用いて,マウスによる Zoom と Pan の機能を追加           
import javax.swing.JFrame;
import edu.uci.ics.jung.graph.Graph;
import edu.uci.ics.jung.graph.UndirectedGraph;
import edu.uci.ics.jung.graph.Vertex;
import edu.uci.ics.jung.graph.impl.UndirectedSparseEdge;
import edu.uci.ics.jung.graph.impl.UndirectedSparseGraph;
import edu.uci.ics.jung.graph.impl.UndirectedSparseVertex;
import edu.uci.ics.jung.visualization.Layout;
import edu.uci.ics.jung.visualization.PluggableRenderer;
import edu.uci.ics.jung.visualization.VisualizationViewer;
import edu.uci.ics.jung.visualization.FRLayout;
import edu.uci.ics.jung.visualization.control.PluggableGraphMouse;
import edu.uci.ics.jung.visualization.control.TranslatingGraphMousePlugin;
import edu.uci.ics.jung.visualization.control.ScalingGraphMousePlugin;
import edu.uci.ics.jung.visualization.control.ViewScalingControl;
import edu.uci.ics.jung.visualization.control.LayoutScalingControl;
import java.awt.Color;

public class Sample07 extends JFrame {

    public static void main(String[] args) {
        JFrame window = new JFrame("Sample01");

        // 疎な無向グラフの作成                                                  
        Graph graph = new UndirectedSparseGraph();

        // 頂点を作成し,グラフに追加                                            
        Vertex vertex1 = graph.addVertex(new UndirectedSparseVertex());
        Vertex vertex2 = graph.addVertex(new UndirectedSparseVertex());
        Vertex vertex3 = graph.addVertex(new UndirectedSparseVertex());
        Vertex vertex4 = graph.addVertex(new UndirectedSparseVertex());

        // エッジを作成し,グラフに追加                                          
        graph.addEdge(new UndirectedSparseEdge(vertex1, vertex2));
        graph.addEdge(new UndirectedSparseEdge(vertex2, vertex3));
        graph.addEdge(new UndirectedSparseEdge(vertex3, vertex1));
        graph.addEdge(new UndirectedSparseEdge(vertex1, vertex4));

        // グラフの配置を FR レイアウト(Fruchterman-Reingold algorithm)に        
        // 従う                                                                  
        Layout layout = new FRLayout(graph);

        // Rendererインタフェース(頂点やエッジの描画を担当)                    
        PluggableRenderer renderer = new PluggableRenderer();

        // VisualizationViewer(グラフを表示するパネル)                         
        // JPanel を継承しているため,JFrame に貼り付けることができる.          
        VisualizationViewer viewer = new VisualizationViewer(layout, renderer);

        // PluggableGraphMouse を用いて,マウスによる Zoom と Pan の機能を追加   
        //                                                                       
        // (メモ)                                                                
        // ScalingGraphMousePlugin() の第1引数について:                        
        //   LayoutScalingControl : 頂点の大きさを変えず,グラフを拡大縮小       
        //   ViewScalingControl : 頂点の大きさも含めて,グラフを拡大縮小         
        // viewer.setGraphMouse(new ZoomPanGraphMouse());と同じ機能を            
        // 提供したいならば LayoutScalingControl を用いる.                      
        PluggableGraphMouse graphMouse = new PluggableGraphMouse();
        graphMouse.add(new TranslatingGraphMousePlugin());      // 視点移動      
        graphMouse.add(new ScalingGraphMousePlugin(new LayoutScalingControl(), 0));     // 拡大・縮小                                                            
        // graphMouse.add(new ScalingGraphMousePlugin(new ViewScalingControl(), 0));                                                                             
        viewer.setGraphMouse(graphMouse);

        // VisualizationViewer を JFrame へ貼り付ける                            
        window.add(viewer);

        // JFrame の各種設定                                                     
        window.setSize(600, 600);
        window.setLocationRelativeTo(null);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setVisible(true);
    }
}