Вопрос

I'm using JUNG to create and visualize some graphs. It appears that the nodes' centers are given coordinates by the layout algorithms. Thus, when a node is on the edge of the pane, some of that node will be cut off (and perhaps the node label as well). I'm not sure if this is a JUNG issue or a JFrame/GUI issue (I'm not too familiar with Java GUI development). I did try to make the maximum dimension of the of the graph image (600x600) less than the size of the pane (800x800). I tried to center the graph using setLocation(), but that didn't seem to work. Even if it did center the graph, I'm not sure if it would take care of the problem.

Here's my code.

import java.awt.Dimension;
import javax.swing.*;

import edu.uci.ics.jung.graph.*;
import edu.uci.ics.jung.visualization.VisualizationImageServer;
import edu.uci.ics.jung.algorithms.layout.SpringLayout;
import edu.uci.ics.jung.visualization.decorators.ToStringLabeller;
import edu.uci.ics.jung.algorithms.generators.random.EppsteinPowerLawGenerator;
import edu.uci.ics.jung.visualization.decorators.EdgeShape;
import org.apache.commons.collections15.Factory;

public class Main {

    public static void main(String[] args) {

        // Factories required for the graph generator
        Factory <Integer> vertexFactory = new Factory<Integer>() {
            int vertexCount = 0;
            public Integer create() {
                return vertexCount++;
            }
        };

        Factory <Integer> edgeFactory = new Factory<Integer>() {
            int edgeCount = 0;
            public Integer create() {
                return edgeCount++;
            }
        };

        Factory<Graph<Integer,Integer>> graphFactory = new Factory<Graph<Integer,Integer>>() {
            public Graph<Integer,Integer> create() {
                return new UndirectedSparseGraph<Integer, Integer>();
            }
        };

        int numVertices = 150;
        int numEdges = 150;
        int numIter = 100;       
        // Create a graph using a power law generator
        EppsteinPowerLawGenerator gen = new EppsteinPowerLawGenerator(graphFactory, vertexFactory, edgeFactory, numVertices, numEdges, numIter);      
        Graph<Integer, Integer> graph = gen.create();

        // Visualization of graph
        SpringLayout layout = new SpringLayout(graph);
        layout.setSize(new Dimension(600,600));
        VisualizationImageServer vs = new VisualizationImageServer(layout, new Dimension(800, 800));
        vs.setLocation(100,700); // seems to have no effect
        vs.getRenderContext().setVertexLabelTransformer(new ToStringLabeller());
        vs.getRenderContext().setEdgeShapeTransformer(new EdgeShape.Line<Integer, Number>());

        JFrame frame = new JFrame(); 
        frame.add(vs); 
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
   }
}
Это было полезно?

Решение

Give this a shot:

float factor = 0.5;
ScalingControl scalingControl = new CrossoverScalingControl();
scalingControl.scale(vs, factor, vs.getCenter());

Also, you shouldn't be adding directly to the Swing Frame, rather you should add to its content pane using:

frame.getContentPane().add(...)
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top