ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java – 部分在JPanel之外的Prefuse示例图

2019-06-23 19:49:36  阅读:271  来源: 互联网

标签:java swing graph-visualization swingx prefuse


我想使用Prefuse来可视化图形.我跟着他们的tutorial并尝试了他们的示例应用程序.它的源代码可以在here找到

但是,即使我只是复制完整代码,结果图也不会显示在教程中.只有一半可见,卡在JPanel的左上角.它的某些部分缺失,因为它们必须在面板外显示.
我尝试了一些自己的图表,但我仍然遇到同样的现象.

我想这不是预期的行为,但我不知道在哪里寻找问题.我不知道这是Swing(x)还是prefuse或者……的问题?

更新:
这是修改后的代码.我没有从示例中做太多改变,只添加了trashgod建议的内容.

package visualise;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import prefuse.Constants;
import prefuse.Display;
import prefuse.Visualization;
import prefuse.action.ActionList;
import prefuse.action.RepaintAction;
import prefuse.action.assignment.ColorAction;
import prefuse.action.assignment.DataColorAction;
import prefuse.action.layout.graph.ForceDirectedLayout;
import prefuse.activity.Activity;
import prefuse.controls.DragControl;
import prefuse.controls.PanControl;
import prefuse.controls.ZoomControl;
import prefuse.data.Graph;
import prefuse.data.io.DataIOException;
import prefuse.data.io.GraphMLReader;
import prefuse.render.DefaultRendererFactory;
import prefuse.render.LabelRenderer;
import prefuse.util.ColorLib;
import prefuse.visual.VisualItem;

public class PrefuseExample {

public static void main(String[] argv) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
             // -- 1. load the data ------------------------------------------------

            // load the socialnet.xml file. it is assumed that the file can be
            // found at the root of the java classpath
            Graph graph = null;
            try {
                graph = new GraphMLReader().readGraph("../../resources/visualisation/prefuse/Prefuse-master/data/socialnet.xml");
            } catch ( DataIOException e ) {
                e.printStackTrace();
                System.err.println("Error loading graph. Exiting...");
                System.exit(1);
            }


            // -- 2. the visualization --------------------------------------------

            // add the graph to the visualization as the data group "graph"
            // nodes and edges are accessible as "graph.nodes" and "graph.edges"
            Visualization vis = new Visualization();
            vis.add("graph", graph);
            vis.setInteractive("graph.edges", null, false);

            // -- 3. the renderers and renderer factory ---------------------------

            // draw the "name" label for NodeItems
            LabelRenderer r = new LabelRenderer("name");
            r.setRoundedCorner(8, 8); // round the corners

            // create a new default renderer factory
            // return our name label renderer as the default for all non-EdgeItems
            // includes straight line edges for EdgeItems by default
            vis.setRendererFactory(new DefaultRendererFactory(r));


            // -- 4. the processing actions ---------------------------------------

            // create our nominal color palette
            // pink for females, baby blue for males
            int[] palette = new int[] {
                ColorLib.rgb(255,180,180), ColorLib.rgb(190,190,255)
            };
            // map nominal data values to colors using our provided palette
            DataColorAction fill = new DataColorAction("graph.nodes", "gender",
                    Constants.NOMINAL, VisualItem.FILLCOLOR, palette);
            // use black for node text
            ColorAction text = new ColorAction("graph.nodes",
                    VisualItem.TEXTCOLOR, ColorLib.gray(0));
            // use light grey for edges
            ColorAction edges = new ColorAction("graph.edges",
                    VisualItem.STROKECOLOR, ColorLib.gray(200));

            // create an action list containing all color assignments
            ActionList color = new ActionList();
            color.add(fill);
            color.add(text);
            color.add(edges);

            // create an action list with an animated layout
            ActionList layout = new ActionList(Activity.INFINITY);
            layout.add(new ForceDirectedLayout("graph"));
            layout.add(new RepaintAction());

            // add the actions to the visualization
            vis.putAction("color", color);
            vis.putAction("layout", layout);


            // -- 5. the display and interactive controls -------------------------

            Display d = new Display(vis);
            d.setSize(720, 500); // set display size
            // drag individual items around
            d.addControlListener(new DragControl());
            // pan with left-click drag on background
            d.addControlListener(new PanControl()); 
            // zoom with right-click drag
            d.addControlListener(new ZoomControl());

            // -- 6. launch the visualization -------------------------------------

            // create a new window to hold the visualization
            JFrame frame = new JFrame("prefuse example");
            // ensure application exits when window is closed
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(d);
            frame.pack();           // layout components in window
            frame.setVisible(true); // show the window

            // assign the colors
            vis.run("color");
            // start up the animated layout
            vis.run("layout");
        }
    });
}

}

解决方法:

我是Prefuse的新手,但是许多常见错误都可能导致观察到的问题.看着example,

>正如here所讨论的,当你真正想要覆盖getPreferredSize()时,不要在Display上使用setSize().
>应仅在event dispatch thread‌​上构造和操作Swing GUI对象.
>初始聚类是图形原点落在显示组件左上角(0,0)处的人工制品.选择了首选尺寸后,可以将()平移到中心.

private static final int W = 640;
private static final int H = 480;
…
Display d = new Display(vis) {

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(W, H);
    }
};
d.pan(W / 2, H / 2);

标签:java,swing,graph-visualization,swingx,prefuse
来源: https://codeday.me/bug/20190623/1274095.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有