// 頂点が選択された場合,その色を変化させる
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.graph.decorators.PickableVertexPaintFunction;
import java.awt.Color;
public class Sample02 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();
// 頂点が選択された場合の塗りつぶし色を設定する.
//
// void setVertexPaintFunction(VertexPaintFunction vpf) は
// ノードの周囲,テキストおよび内部の色を指定するための
// VertexPaintFunction を指定する.
// なお VertexPaintFunction はインタフェースであり,その実装クラスと
// して ConstantVertexPaintFunction, PickableVertexPaintFunction,
// ShortestPathDemo.MyVertexPaintFunction がある.
//
// PickableVertexPaintFunction() の第1引数は PickedInfo だが,
// これはインタフェースであり,そ実装クラスのひとつが PluggableRenderer
// である.
PickableVertexPaintFunction pvpf =
new PickableVertexPaintFunction(renderer, // 頂点が選択され\
ているかどうかを判別するための PickedInfo
Color.DARK_GRAY, // 輪郭の色
Color.MAGENTA, // 非選択時の色
Color.GREEN); // 選択時の色
renderer.setVertexPaintFunction(pvpf);
// VisualizationViewer(グラフを表示するパネル)
// JPanel を継承しているため,JFrame に貼り付けることができる.
VisualizationViewer viewer = new VisualizationViewer(layout, renderer);
// VisualizationViewer を JFrame へ貼り付ける
window.add(viewer);
// JFrame の各種設定
window.setSize(600, 600);
window.setLocationRelativeTo(null);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setVisible(true);
}
}