首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基于jframe创建的预融合图像

基于jframe创建的预融合图像
EN

Stack Overflow用户
提问于 2014-04-04 19:12:11
回答 1查看 228关注 0票数 0

我正试图绘制两张图像,并在它们之间与普雷费斯线。但是,我的图像并没有加载到create上。当我单击标签时,单击的标签图像将出现。但我希望那些图片出现在创建jframe上。我该怎么做呢。

这是我的密码

代码语言:javascript
复制
    import java.awt.Font;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFrame;

import prefuse.Constants;
import prefuse.Display;
import prefuse.Visualization;
import prefuse.action.ActionList;
import prefuse.action.assignment.ColorAction;
import prefuse.action.assignment.DataSizeAction;
import prefuse.action.assignment.FontAction;
import prefuse.action.layout.graph.BalloonTreeLayout;
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.ImageFactory;
import prefuse.render.LabelRenderer;
import prefuse.util.ColorLib;
import prefuse.util.FontLib;
import prefuse.visual.VisualItem;

public class Example {

public static void main(String[] argv) throws Exception {

    Graph graph = null;
    try {

        graph = new GraphMLReader().readGraph("/socialnet.xml");
    } catch ( DataIOException e ) {
        e.printStackTrace();
        System.err.println("Error loading graph. Exiting...");
        System.exit(1);
    }

ImageFactory imageFactory = new ImageFactory(100,100);

try
{
    //load images and construct imageFactory.
    String images[] = new String[3];
    images[0] = "data/images/Switch.jpg";
    images[1] = "data/images/soup122.jpg";
    images[2] = "data/images/soup1.png";

    String[] names = new String[] {"Switch","soup1","Router"};
    BufferedImage img = null;


    for(int i=0; i < images.length ; i++)
    {
        try {
            img = ImageIO.read(new File(images[i]));
            imageFactory.addImage(names[i],img);

        }
        catch (IOException e){
        }
    }


}
catch(Exception exp)
{

}


    Visualization vis = new Visualization();
    vis.add("graph", graph);

LabelRenderer nodeRenderer = new LabelRenderer("name", "type");
    nodeRenderer.setVerticalAlignment(Constants.BOTTOM);
    nodeRenderer.setHorizontalPadding(0);
    nodeRenderer.setVerticalPadding(0);
    nodeRenderer.setImagePosition(Constants.TOP);
    nodeRenderer.setMaxImageDimensions(100,100); 


    DefaultRendererFactory drf = new DefaultRendererFactory();
    drf.setDefaultRenderer(nodeRenderer);
    vis.setRendererFactory(drf); 


    ColorAction nText = new ColorAction("graph.nodes", VisualItem.TEXTCOLOR);
    nText.setDefaultColor(ColorLib.gray(100));


    ColorAction nEdges = new ColorAction("graph.edges", VisualItem.STROKECOLOR);
    nEdges.setDefaultColor(ColorLib.gray(100));

    // bundle the color actions
    ActionList draw = new ActionList();

     //MAD - changing the size of the nodes dependent on the weight of the people
    final DataSizeAction dsa = new DataSizeAction("graph.nodes","size");        
    draw.add(dsa);

    draw.add(nText);
    draw.add(new FontAction("graph.nodes", FontLib.getFont("Tahoma",Font.BOLD, 12)));
    draw.add(nEdges);

    vis.putAction("draw", draw);



  ActionList layout = new ActionList(Activity.DEFAULT_STEP_TIME);


  BalloonTreeLayout balloonlayout = new BalloonTreeLayout("graph",50);
  layout.add(balloonlayout); 


    Display d = new Display(vis);

    vis.putAction("layout", layout);


// start up the animated layout
    vis.run("draw");
vis.run("layout");


    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

}
}`

这是我的Xml文件

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<!--  An excerpt of an egocentric social network  -->
<graphml xmlns="http://graphml.graphdrawing.org/xmlns">
<graph edgedefault="undirected">

<!-- data schema -->
<key id="name" for="node" attr.name="name" attr.type="string"/>
<key id="type" for="node" attr.name="type" attr.type="string"/>

<!-- nodes -->  
<node id="1">
 <data key="name">Switch1</data>
 <data key="type">data/images/Switch.jpg</data>
 </node>
 <node id="2">
 <data key="name">Switch2</data>
 <data key="type">data/images/soup1.png</data>
 </node>
<edge source="1" target="2"></edge>


</graph>
</graphml>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-04-07 01:54:49

尝试在创建jframe之后运行绘图操作,即

代码语言:javascript
复制
// 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


vis.run("draw");
vis.run("layout");

或者,如果不起作用,调用frame.repaint()。我在想,你需要以某种方式强行绘制视觉效果。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22871216

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档