Java Jung图形每次都会出现在同一个地方吗?

Java Jung图形每次都会出现在同一个地方吗?,java,graphics,jung,Java,Graphics,Jung,我使用JUNG()在java中绘制图形。软件很棒,但我有一个小问题。如何确保每次显示的图形都是相同的(架构或位置没有变化) 更具体地说:图形模型(要表示的数据)不会更改,但每次我点击“查看图形”按钮时,其表示都会更改:)[一些顶点位于其他位置,例如:有时在窗口的上部,有时在下部] 谢谢, Iulian可用于指定要转换的顶点。这将允许您控制顶点的放置位置,并执行您想要执行的操作。您应该使用以下选项: 公共静态布局(图形, org.apache.commons.collections15.Trans

我使用JUNG()在java中绘制图形。软件很棒,但我有一个小问题。如何确保每次显示的图形都是相同的(架构或位置没有变化)

更具体地说:图形模型(要表示的数据)不会更改,但每次我点击“查看图形”按钮时,其表示都会更改:)[一些顶点位于其他位置,例如:有时在窗口的上部,有时在下部]

谢谢,

Iulian可用于指定要转换的顶点。这将允许您控制顶点的放置位置,并执行您想要执行的操作。您应该使用以下选项:

公共静态布局(图形,
org.apache.commons.collections15.Transformer初始值设定项)
您需要实现自己的方法,该方法接收顶点并返回顶点应该出现的位置。其使用中的一个示例:

package test;

import java.awt.Dimension;
import java.awt.geom.Point2D;
import java.io.IOException;

import javax.swing.JFrame;

import org.apache.commons.collections15.Transformer;

import edu.uci.ics.jung.algorithms.layout.StaticLayout;
import edu.uci.ics.jung.graph.Graph;
import edu.uci.ics.jung.graph.SparseMultigraph;
import edu.uci.ics.jung.visualization.VisualizationViewer;

/**
 * Jung example - vertices appearing in same location
 * 
 * @author Kah
 */
public class StaticLocation {

    /**
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {
        // Setup the example graph.
        Graph<Integer, String> basis = new SparseMultigraph<Integer, String>();
        basis.addVertex(Integer.valueOf(0));
        basis.addVertex(Integer.valueOf(1));
        basis.addVertex(Integer.valueOf(2));
        basis.addEdge("Edge 1", Integer.valueOf(0), Integer.valueOf(1));
        basis.addEdge("Edge 2", Integer.valueOf(0), Integer.valueOf(2));
        basis.addEdge("Edge 3", Integer.valueOf(1), Integer.valueOf(2));

        Transformer<Integer, Point2D> locationTransformer = new Transformer<Integer, Point2D>() {

            @Override
            public Point2D transform(Integer vertex) {
                int value = (vertex.intValue() * 40) + 20;
                return new Point2D.Double((double) value, (double) value);
            }
        };

        StaticLayout<Integer, String> layout = new StaticLayout<Integer, String>(
                basis, locationTransformer);
        layout.setSize(new Dimension(250, 250));
        VisualizationViewer<Integer, String> vv = new VisualizationViewer<Integer, String>(
                layout);

        vv.setPreferredSize(new Dimension(250, 250));

        JFrame frame = new JFrame("Simple Graph View 2");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(vv);
        vv.setOpaque(false);
        frame.pack();
        frame.setVisible(true);
    }
}
封装测试;
导入java.awt.Dimension;
导入java.awt.geom.Point2D;
导入java.io.IOException;
导入javax.swing.JFrame;
导入org.apache.commons.collections15.Transformer;
导入edu.uci.ics.jung.algorithms.layout.StaticLayout;
导入edu.uci.ics.jung.graph.graph;
导入edu.uci.ics.jung.graph.sparsemultraph;
导入edu.uci.ics.jung.visualization.VisualizationViewer;
/**
*Jung示例-出现在相同位置的顶点
* 
*@作者Kah
*/
公共类静态定位{
/**
*@param args
*@抛出异常
*/
公共静态void main(字符串[]args)引发IOException{
//设置示例图。
图形基础=新的SparseMultraph();
basis.addVertex(整数.valueOf(0));
basis.addVertex(整数.valueOf(1));
基.addVertex(整数.valueOf(2));
基.加法(“边1”,Integer.valueOf(0),Integer.valueOf(1));
基.加法(“边2”,Integer.valueOf(0),Integer.valueOf(2));
基.加数(“边3”,整数值(1),整数值(2));
变压器位置变压器=新变压器(){
@凌驾
公共点2D变换(整数顶点){
int值=(vertex.intValue()*40)+20;
返回新的点2d.Double((Double)值,(Double)值);
}
};
StaticLayout=新的StaticLayout(
基础、位置和变压器);
布局。设置尺寸(新尺寸(250250));
VisualizationViewer vv=新建VisualizationViewer(
布局);
vv.setPreferredSize(新尺寸(250250));
JFrame=新JFrame(“简单图形视图2”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(vv);
vv.setOpaque(假);
frame.pack();
frame.setVisible(true);
}
}
于2010年2月20日添加:

另一种方法是使用将顶点的位置保存到文件中。但是,您还需要以某种方式持久化图形,以获取其中的顶点和顶点(这需要单独持久化)。有许多类用于在中持久化图形。这是一个仅使用以下内容的示例:

封装测试;
导入java.awt.Dimension;
导入java.io.File;
导入java.io.FileWriter;
导入java.io.IOException;
导入javax.swing.JFrame;
导入org.apache.commons.collections15.Transformer;
导入edu.uci.ics.jung.algorithms.layout.layout;
导入edu.uci.ics.jung.algorithms.layout.SpringLayout2;
导入edu.uci.ics.jung.graph.graph;
导入edu.uci.ics.jung.graph.sparsemultraph;
导入edu.uci.ics.jung.io.GraphMLReader;
进口edu.uci.ics.jung.io.GraphMLWriter;
导入edu.uci.ics.jung.visualization.VisualizationViewer;
导入edu.uci.ics.jung.visualization.decorators.ToStringLabeller;
导入edu.uci.ics.jung.visualization.layout.PersistentLayoutImpl;
/**
*Jung示例-出现在相同位置的顶点
* 
*@作者Kah
*/
公共类persistentVertex
{
/**
*@param args
*@抛出异常
*/
公共静态void main(字符串[]args)引发IOException
{
//设置示例图。
尝试
{
VisualizationViewer vv=新建VisualizationViewer(
getLayout());
vv.setPreferredSize(新尺寸(250250));
JFrame=新JFrame(“简单图形视图2”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(vv);
vv.setOpaque(假);
frame.pack();
frame.setVisible(true);
}
捕获(例外e)
{
e、 printStackTrace();
}
}
私有静态布局getLayout()引发IOException,
ClassNotFoundException
{
Graph Graph=新的SparseMultraph();
文件源=新文件(“C:\\layout.dat”);
SpringLayout2 backing=新的SpringLayout2(
图表);
PersistentLayoutImpl布局=新建PersistentLayoutImpl(
支持);
布局。设置尺寸(新尺寸(250250));
//请注意,您还需要将顶点和边放回之前
//恢复。
graph.addVertex(Integer.valueOf(0));
graph.addVertex(Integer.valueOf(1));
graph.addVertex(Integer.valueOf(2));
图.加法(“边1”,Integer.valueOf(0),Integer.valueOf(1));
图.加法(“边2”,Integer.valueOf(0),Integer.valueOf(2));
图.加法(“边3”,Integer.valueOf(1),Integer.valueOf(2));
if(source.exists())
{
layout.restore(source.getAbsolutePath());
}
其他的
{
layout.persist(source.getAbsolutePath());
}
返回布局;
}
}
请注意,该示例还没有持久化顶点和边,因为我还没有时间了解如何在ye中使用这些类
package test;

import java.awt.Dimension;
import java.awt.geom.Point2D;
import java.io.IOException;

import javax.swing.JFrame;

import org.apache.commons.collections15.Transformer;

import edu.uci.ics.jung.algorithms.layout.StaticLayout;
import edu.uci.ics.jung.graph.Graph;
import edu.uci.ics.jung.graph.SparseMultigraph;
import edu.uci.ics.jung.visualization.VisualizationViewer;

/**
 * Jung example - vertices appearing in same location
 * 
 * @author Kah
 */
public class StaticLocation {

    /**
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {
        // Setup the example graph.
        Graph<Integer, String> basis = new SparseMultigraph<Integer, String>();
        basis.addVertex(Integer.valueOf(0));
        basis.addVertex(Integer.valueOf(1));
        basis.addVertex(Integer.valueOf(2));
        basis.addEdge("Edge 1", Integer.valueOf(0), Integer.valueOf(1));
        basis.addEdge("Edge 2", Integer.valueOf(0), Integer.valueOf(2));
        basis.addEdge("Edge 3", Integer.valueOf(1), Integer.valueOf(2));

        Transformer<Integer, Point2D> locationTransformer = new Transformer<Integer, Point2D>() {

            @Override
            public Point2D transform(Integer vertex) {
                int value = (vertex.intValue() * 40) + 20;
                return new Point2D.Double((double) value, (double) value);
            }
        };

        StaticLayout<Integer, String> layout = new StaticLayout<Integer, String>(
                basis, locationTransformer);
        layout.setSize(new Dimension(250, 250));
        VisualizationViewer<Integer, String> vv = new VisualizationViewer<Integer, String>(
                layout);

        vv.setPreferredSize(new Dimension(250, 250));

        JFrame frame = new JFrame("Simple Graph View 2");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(vv);
        vv.setOpaque(false);
        frame.pack();
        frame.setVisible(true);
    }
}
package test;

import java.awt.Dimension;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.JFrame;

import org.apache.commons.collections15.Transformer;

import edu.uci.ics.jung.algorithms.layout.Layout;
import edu.uci.ics.jung.algorithms.layout.SpringLayout2;
import edu.uci.ics.jung.graph.Graph;
import edu.uci.ics.jung.graph.SparseMultigraph;
import edu.uci.ics.jung.io.GraphMLReader;
import edu.uci.ics.jung.io.GraphMLWriter;
import edu.uci.ics.jung.visualization.VisualizationViewer;
import edu.uci.ics.jung.visualization.decorators.ToStringLabeller;
import edu.uci.ics.jung.visualization.layout.PersistentLayoutImpl;

/**
 * Jung example - vertices appearing in same location
 * 
 * @author Kah
 */
public class PersistentVertices
{

    /**
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException
    {
        // Setup the example graph.
        try
        {
            VisualizationViewer<Integer, String> vv = new VisualizationViewer<Integer, String>(
                    getLayout());

            vv.setPreferredSize(new Dimension(250, 250));

            JFrame frame = new JFrame("Simple Graph View 2");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.getContentPane().add(vv);
            vv.setOpaque(false);
            frame.pack();
            frame.setVisible(true);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    private static Layout<Integer, String> getLayout() throws IOException,
            ClassNotFoundException
    {
        Graph<Integer, String> graph = new SparseMultigraph<Integer, String>();
        File source = new File("C:\\layout.dat");
        SpringLayout2<Integer, String> backing = new SpringLayout2<Integer, String>(
                graph);
        PersistentLayoutImpl<Integer, String> layout = new PersistentLayoutImpl<Integer, String>(
                backing);
        layout.setSize(new Dimension(250, 250));

        // Note that you also need to put the vertices and edges back before
        // restoring.
        graph.addVertex(Integer.valueOf(0));
        graph.addVertex(Integer.valueOf(1));
        graph.addVertex(Integer.valueOf(2));
        graph.addEdge("Edge 1", Integer.valueOf(0), Integer.valueOf(1));
        graph.addEdge("Edge 2", Integer.valueOf(0), Integer.valueOf(2));
        graph.addEdge("Edge 3", Integer.valueOf(1), Integer.valueOf(2));

        if (source.exists())
        {
            layout.restore(source.getAbsolutePath());
        }
        else
        {
            layout.persist(source.getAbsolutePath());
        }
        return layout;
    }
}