JFrame上的荣格图显示
Jung graph display on JFrame
我正在尝试重用 Jung 网页中的代码 (treecolapse.java; http://jung.sourceforge.net/applet/treecollapse.html),但我需要图表的显示占据整个框架,而不是其中的一部分.. .或者至少默认情况下显示不是那么小...我该怎么做?
谢谢! :D
[1]: http://i.stack.imgur.com/zy2XK.png
public class Visualizacion extends JApplet {
/**
* the graph
*/
static org.graphstream.graph.Graph grafoDAA;
Forest<String,String> graph;Funciones f=new Funciones();
Factory<DirectedGraph<String,String>> graphFactory = new Factory<DirectedGraph<String,String>>() {
public DirectedGraph<String, String> create() {
return new DirectedSparseMultigraph<String,String>();
}
};
Factory<Tree<String,String>> treeFactory =new Factory<Tree<String,String>> () {
public Tree<String, String> create() {
return new DelegateTree<String,String>(graphFactory);
}
};
Factory<String> edgeFactory = new Factory<String>() {
int i=0;
public String create() {
return "E"+i++;
}};
Factory<String> vertexFactory = new Factory<String>() {
int i=0;
public String create() {
return "V"+i++;
}};
// 受保护的 VertexStrokeHighlight vsh;
/**
*图形的可视化组件和渲染器
*/
可视化查看器 vv;
字符串根;
TreeLayout布局;
FRLayout布局1;
TreeCollapser 折叠器;
public Visualizacion(org.graphstream.graph.Graph grafito) {
grafoDAA=grafito;
// create a simple graph for the demo
graph = new DelegateForest<String,String>();
createTree();
layout = new TreeLayout<String,String>(graph);
collapser = new TreeCollapser();
Transformer<String,Paint>vertexPaint= new Transformer<String,Paint>(){
public Paint transform(String i){
Node node=grafoDAA.getNode(i);
if(node.hasAttribute("ExcedeCaudal")){
return Color.RED;}
else{
return Color.lightGray;}
}
};
vv = new VisualizationViewer<String,String>(layout, new Dimension(800,600));
vv.addGraphMouseListener(new TestGraphMouseListener<String>());
vv.setBackground(Color.white);
vv.getRenderContext().setEdgeShapeTransformer(new EdgeShape.Line());
vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller());
vv.getRenderContext().setEdgeLabelTransformer(new ToStringLabeller());
vv.getRenderContext().setVertexShapeTransformer(new ClusterVertexShapeFunction());
vv.getRenderContext().setVertexFillPaintTransformer(vertexPaint);
// add a listener for ToolTips
vv.setVertexToolTipTransformer(new ToStringLabeller());
vv.getRenderContext().setArrowFillPaintTransformer(new ConstantTransformer(Color.lightGray));
Container content = getContentPane();
final GraphZoomScrollPane panel = new GraphZoomScrollPane(vv);
content.add(panel);
final DefaultModalGraphMouse graphMouse = new DefaultModalGraphMouse();
vv.setGraphMouse(graphMouse);
//JComboBox modeBox = graphMouse.getModeComboBox();
//modeBox.addItemListener(graphMouse.getModeListener());
// graphMouse.setMode(ModalGraphMouse.Mode.PICKING);
final ScalingControl scaler = new CrossoverScalingControl();
//ver cual nodo se selecciona
final PickedState<String> pickedState=vv.getPickedVertexState();
pickedState.addItemListener(new ItemListener(){
@Override
public void itemStateChanged(ItemEvent e){
Object subject=e.getItem();
if(subject instanceof String){
String vertice=(String)subject;
if(pickedState.isPicked(vertice)){
System.out.println("Vertice "+vertice+" está seleccionado");
}
else{
System.out.println("Vertice "+vertice+"no está seleccionado");
}
}
}
});
JButton plus = new JButton("+");
plus.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
scaler.scale(vv, 1.1f, vv.getCenter());
}
});
JButton minus = new JButton("-");
minus.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
scaler.scale(vv, 1/1.1f, vv.getCenter());
}
});
JButton collapse = new JButton("Collapse");
collapse.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Collection picked =new HashSet(vv.getPickedVertexState().getPicked());
if(picked.size() == 1) {
Object root = picked.iterator().next();
Forest inGraph = (Forest)layout.getGraph();
try {
collapser.collapse(vv.getGraphLayout(), inGraph, root);
} catch (InstantiationException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IllegalAccessException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
vv.getPickedVertexState().clear();
vv.repaint();
}
}});
JButton expand = new JButton("Expand");
expand.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Collection picked = vv.getPickedVertexState().getPicked();
for(Object v : picked) {
if(v instanceof Forest) {
Forest inGraph = (Forest)layout.getGraph();
collapser.expand(inGraph, (Forest)v);
}
vv.getPickedVertexState().clear();
vv.repaint();
}
}});
JPanel scaleGrid = new JPanel(new GridLayout(1,0));
scaleGrid.setBorder(BorderFactory.createTitledBorder("Zoom"));
JPanel controls = new JPanel();
scaleGrid.add(plus);
scaleGrid.add(minus);
controls.add(scaleGrid);
controls.add(collapse);
controls.add(expand);
content.add(controls, BorderLayout.SOUTH);
}
private void createTree() {
for(Node node:grafoDAA){
graph.addVertex(node.getId());
// System.out.println("node id "+node.getId());
}
for (Edge edge: grafoDAA.getEachEdge()){
String padre=edge.getNode0().getId();
String hijo=edge.getNode1().getId();
String caudal=(edge.getAttribute("Caudal"));
graph.addEdge(caudal, padre,hijo);
}
}
/**
* a demo class that will create a vertex shape that is either a polygon or star.
* The number of sides corresponds to the number
* of vertices that were collapsed into the vertex represented by
* this shape.
*
* @author Tom Nelson
*
* @param <V>
*/
class ClusterVertexShapeFunction<V> extends EllipseVertexShapeTransformer<V> {
ClusterVertexShapeFunction() {
setSizeTransformer(new ClusterVertexSizeFunction<V>(20));
}
@SuppressWarnings("unchecked")
@Override
public Shape transform(V v) {
if(v instanceof Graph) {
int size = ((Graph)v).getVertexCount();
if (size < 8) {
int sides = Math.max(size, 3);
return factory.getRegularPolygon(v, sides);
}
else {
return factory.getRegularStar(v, size);
}
}
return super.transform(v);
}
}
/**
* A demo class that will make vertices larger if they represent
* a collapsed collection of original vertices
* @author Tom Nelson
*
* @param <V>
*/
class ClusterVertexSizeFunction<V> implements Transformer<V,Integer> {
int size;
public ClusterVertexSizeFunction(Integer size) {
this.size = size;
}
public Integer transform(V v) {
if(v instanceof Graph) {
return 30;
}
return size;
}
}
static class TestGraphMouseListener<V> implements GraphMouseListener<V> {
public void graphClicked(V v, MouseEvent me) {
if(me.getClickCount()==2){
System.err.println("Vertex "+v+" fui doble click");
}
System.err.println("Vertex "+v+" was clicked at ("+me.getX()+","+me.getY()+")");
}
public void graphPressed(V v, MouseEvent me) {
System.err.println("Vertex "+v+" was pressed at ("+me.getX()+","+me.getY()+")");
}
public void graphReleased(V v, MouseEvent me) {
System.err.println("Vertex "+v+" was released at ("+me.getX()+","+me.getY()+")");
}
}
public void execute(){
JFrame frame = new JFrame();
Container content = frame.getContentPane();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
content.add(new Visualizacion(grafoDAA));
frame.pack();
frame.setVisible(true);
}
}
您正在创建具有默认 x 和 y 间距的 TreeLayout。尝试增加间距:
我正在尝试重用 Jung 网页中的代码 (treecolapse.java; http://jung.sourceforge.net/applet/treecollapse.html),但我需要图表的显示占据整个框架,而不是其中的一部分.. .或者至少默认情况下显示不是那么小...我该怎么做?
谢谢! :D
[1]: http://i.stack.imgur.com/zy2XK.png
public class Visualizacion extends JApplet {
/**
* the graph
*/
static org.graphstream.graph.Graph grafoDAA;
Forest<String,String> graph;Funciones f=new Funciones();
Factory<DirectedGraph<String,String>> graphFactory = new Factory<DirectedGraph<String,String>>() {
public DirectedGraph<String, String> create() {
return new DirectedSparseMultigraph<String,String>();
}
};
Factory<Tree<String,String>> treeFactory =new Factory<Tree<String,String>> () {
public Tree<String, String> create() {
return new DelegateTree<String,String>(graphFactory);
}
};
Factory<String> edgeFactory = new Factory<String>() {
int i=0;
public String create() {
return "E"+i++;
}};
Factory<String> vertexFactory = new Factory<String>() {
int i=0;
public String create() {
return "V"+i++;
}};
// 受保护的 VertexStrokeHighlight vsh; /** *图形的可视化组件和渲染器 */ 可视化查看器 vv; 字符串根; TreeLayout布局; FRLayout布局1; TreeCollapser 折叠器;
public Visualizacion(org.graphstream.graph.Graph grafito) {
grafoDAA=grafito;
// create a simple graph for the demo
graph = new DelegateForest<String,String>();
createTree();
layout = new TreeLayout<String,String>(graph);
collapser = new TreeCollapser();
Transformer<String,Paint>vertexPaint= new Transformer<String,Paint>(){
public Paint transform(String i){
Node node=grafoDAA.getNode(i);
if(node.hasAttribute("ExcedeCaudal")){
return Color.RED;}
else{
return Color.lightGray;}
}
};
vv = new VisualizationViewer<String,String>(layout, new Dimension(800,600));
vv.addGraphMouseListener(new TestGraphMouseListener<String>());
vv.setBackground(Color.white);
vv.getRenderContext().setEdgeShapeTransformer(new EdgeShape.Line());
vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller());
vv.getRenderContext().setEdgeLabelTransformer(new ToStringLabeller());
vv.getRenderContext().setVertexShapeTransformer(new ClusterVertexShapeFunction());
vv.getRenderContext().setVertexFillPaintTransformer(vertexPaint);
// add a listener for ToolTips
vv.setVertexToolTipTransformer(new ToStringLabeller());
vv.getRenderContext().setArrowFillPaintTransformer(new ConstantTransformer(Color.lightGray));
Container content = getContentPane();
final GraphZoomScrollPane panel = new GraphZoomScrollPane(vv);
content.add(panel);
final DefaultModalGraphMouse graphMouse = new DefaultModalGraphMouse();
vv.setGraphMouse(graphMouse);
//JComboBox modeBox = graphMouse.getModeComboBox();
//modeBox.addItemListener(graphMouse.getModeListener());
// graphMouse.setMode(ModalGraphMouse.Mode.PICKING);
final ScalingControl scaler = new CrossoverScalingControl();
//ver cual nodo se selecciona
final PickedState<String> pickedState=vv.getPickedVertexState();
pickedState.addItemListener(new ItemListener(){
@Override
public void itemStateChanged(ItemEvent e){
Object subject=e.getItem();
if(subject instanceof String){
String vertice=(String)subject;
if(pickedState.isPicked(vertice)){
System.out.println("Vertice "+vertice+" está seleccionado");
}
else{
System.out.println("Vertice "+vertice+"no está seleccionado");
}
}
}
});
JButton plus = new JButton("+");
plus.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
scaler.scale(vv, 1.1f, vv.getCenter());
}
});
JButton minus = new JButton("-");
minus.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
scaler.scale(vv, 1/1.1f, vv.getCenter());
}
});
JButton collapse = new JButton("Collapse");
collapse.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Collection picked =new HashSet(vv.getPickedVertexState().getPicked());
if(picked.size() == 1) {
Object root = picked.iterator().next();
Forest inGraph = (Forest)layout.getGraph();
try {
collapser.collapse(vv.getGraphLayout(), inGraph, root);
} catch (InstantiationException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IllegalAccessException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
vv.getPickedVertexState().clear();
vv.repaint();
}
}});
JButton expand = new JButton("Expand");
expand.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Collection picked = vv.getPickedVertexState().getPicked();
for(Object v : picked) {
if(v instanceof Forest) {
Forest inGraph = (Forest)layout.getGraph();
collapser.expand(inGraph, (Forest)v);
}
vv.getPickedVertexState().clear();
vv.repaint();
}
}});
JPanel scaleGrid = new JPanel(new GridLayout(1,0));
scaleGrid.setBorder(BorderFactory.createTitledBorder("Zoom"));
JPanel controls = new JPanel();
scaleGrid.add(plus);
scaleGrid.add(minus);
controls.add(scaleGrid);
controls.add(collapse);
controls.add(expand);
content.add(controls, BorderLayout.SOUTH);
}
private void createTree() {
for(Node node:grafoDAA){
graph.addVertex(node.getId());
// System.out.println("node id "+node.getId());
}
for (Edge edge: grafoDAA.getEachEdge()){
String padre=edge.getNode0().getId();
String hijo=edge.getNode1().getId();
String caudal=(edge.getAttribute("Caudal"));
graph.addEdge(caudal, padre,hijo);
}
}
/**
* a demo class that will create a vertex shape that is either a polygon or star.
* The number of sides corresponds to the number
* of vertices that were collapsed into the vertex represented by
* this shape.
*
* @author Tom Nelson
*
* @param <V>
*/
class ClusterVertexShapeFunction<V> extends EllipseVertexShapeTransformer<V> {
ClusterVertexShapeFunction() {
setSizeTransformer(new ClusterVertexSizeFunction<V>(20));
}
@SuppressWarnings("unchecked")
@Override
public Shape transform(V v) {
if(v instanceof Graph) {
int size = ((Graph)v).getVertexCount();
if (size < 8) {
int sides = Math.max(size, 3);
return factory.getRegularPolygon(v, sides);
}
else {
return factory.getRegularStar(v, size);
}
}
return super.transform(v);
}
}
/**
* A demo class that will make vertices larger if they represent
* a collapsed collection of original vertices
* @author Tom Nelson
*
* @param <V>
*/
class ClusterVertexSizeFunction<V> implements Transformer<V,Integer> {
int size;
public ClusterVertexSizeFunction(Integer size) {
this.size = size;
}
public Integer transform(V v) {
if(v instanceof Graph) {
return 30;
}
return size;
}
}
static class TestGraphMouseListener<V> implements GraphMouseListener<V> {
public void graphClicked(V v, MouseEvent me) {
if(me.getClickCount()==2){
System.err.println("Vertex "+v+" fui doble click");
}
System.err.println("Vertex "+v+" was clicked at ("+me.getX()+","+me.getY()+")");
}
public void graphPressed(V v, MouseEvent me) {
System.err.println("Vertex "+v+" was pressed at ("+me.getX()+","+me.getY()+")");
}
public void graphReleased(V v, MouseEvent me) {
System.err.println("Vertex "+v+" was released at ("+me.getX()+","+me.getY()+")");
}
}
public void execute(){
JFrame frame = new JFrame();
Container content = frame.getContentPane();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
content.add(new Visualizacion(grafoDAA));
frame.pack();
frame.setVisible(true);
}
}
您正在创建具有默认 x 和 y 间距的 TreeLayout。尝试增加间距: