荣格图形能每次出现在同一个地方吗? [英] Can Jung graphics appear in the same place every time?

查看:91
本文介绍了荣格图形能每次出现在同一个地方吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JUNG( http://jung.sourceforge.net/index.html)在java中绘制图形。该软件是伟大的,但我有一个小问题。我怎样才能确保显示的图形每次都是相同的(架构或位置没有变化)?

更具体地说:图模型代表)不会改变,但每次我点击查看图形按钮时,它的表示都会改变:) [某些顶点位于其他位置,例如:有时位于窗口的上半部分,有时位于下半部分] p>

谢谢,



iulian

解决方案

StaticLayout 可让您指定一个顶点至 Point2D 变压器。这将允许您控制顶点的放置位置,并且应该执行您想要的操作。你应该使用下面的构造函数

  public StaticLayout(图< V,E>图,
org.apache.commons.collections15.Transformer< V,Point2D>初始化程序)

您需要实现自己的变换器,它接受顶点并返回顶点应该在的位置出现。它的使用示例:

  package test; 

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

import javax.swing.JFrame;

导入org.apache.commons.collections15.Transformer;

导入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例子 - 出现在相同位置的顶点
*
* @author Kah
* /
public class StaticLocation {
$ b $ / **
* @param args
* @throws IOException
* /
public static void main(String [] args)throws IOException {
//设置示例图。
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));

变换器<整数,点2D> locationTransformer = new Transformer< Integer,Point2D>(){

@Override
public Point2D transform(Integer vertex){
int value =(vertex.intValue()* 40) + 20;
返回新的Point2D.Double((double)值,(double)值);
}
};

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);
}
}

2010年2月20日新增:



另一种方法是使用 PersistentLayoutImpl 将顶点的位置保存到文件中。但是,您还需要以某种方式持久保存图以获取哪些顶点和顶点(需要单独持续保存)。有许多类用于保存图表 edu.uci.ics.jung.io 。这是一个只使用 PersistentLayoutImpl

  package test; 

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

import javax.swing.JFrame;

导入org.apache.commons.collections15.Transformer;

导入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;
导入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示例 - 出现在相同位置的顶点
*
* @author Kah
* /
public class PersistentVertices
{

/ **
* @param args
* @throws IOException
* /
public static void main(String []] args)抛出IOException
{
//设置示例图。
尝试
{
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()抛出IOException,
ClassNotFoundException
{
Graph< Integer,String> graph = new SparseMultigraph< Integer,String>();
文件源=新文件(C:\\layout.dat);
SpringLayout2<整数,字符串> backing = new SpringLayout2< Integer,String>(
graph);
PersistentLayoutImpl<整数,字符串> layout = new PersistentLayoutImpl< Integer,String>(
backing);
layout.setSize(new Dimension(250,250));

//请注意,您还需要在
//恢复之前放置顶点和边。
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());
}
返回布局;
}
}

请注意,该示例不会保留顶点和边但是,因为我没有时间弄清楚如何使用 edu.uci.ics.jung.io


I'm using JUNG ( http://jung.sourceforge.net/index.html ) to draw graphics in java. The software is great but I have a small question. How can I be sure that the displayed graph is each time the same (no changes is architecture or position)?

To be more specific: the graph model (data to be represented) doesn't change but its representation changes each time I hit the "View graph" button :) [some vertices are in other places, for example: sometimes in the upper part of the window, sometimes in the lower part ]

Thank you,

Iulian

解决方案

A StaticLayout lets you specify a vertex to Point2D transformer. This will allow you to control where the vertices are placed and should do what you want to do. You should use the following constructor:

public StaticLayout(Graph<V,E> graph,
                org.apache.commons.collections15.Transformer<V,Point2D> initializer)

You'll need to implement your own transformer that takes in a vertex and returns the location where the vertex should appear. An example of its in use:

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);
    }
}

Added 20 Feb 2010:

An alternative is to use a PersistentLayoutImpl to save the locations of the vertices to a file. However, you need to also somehow persist the graph to get which vertices and vertices were on there (this needs to persisted separately). There are number of classes for persisting the graph in edu.uci.ics.jung.io. This is an example that uses just PersistentLayoutImpl:

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;
    }
}

Note that the example does not persist the vertices and edges yet, as I haven't had the time to figure out how use the classes in edu.uci.ics.jung.io yet.

这篇关于荣格图形能每次出现在同一个地方吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆