package one; import javax.swing.JFrame; import javax.swing.JPanel; import prefuse.Display; import prefuse.Visualization; import prefuse.action.ActionList; import prefuse.action.RepaintAction; import prefuse.action.assignment.ColorAction; 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.GraphMLReader; import prefuse.render.DefaultRendererFactory; import prefuse.render.LabelRenderer; import prefuse.util.ColorLib; import prefuse.visual.VisualItem; public class DataViz extends JPanel{ /* Visualization viz; Graph graph; Display display; String prefix = "http://cs.marlboro.edu/~sheller/datafiles/"; URL url; */ public static Visualization BuildIt(){ //First try/catch to build the URL /* try{ url = new URL("http://cs.marlboro.edu/~sheller/datafiles/test1.txt"); //url = new URL(prefix + "test1.txt"); }catch(Exception e){ System.out.println("Couldn't build URL"); e.printStackTrace(); System.exit(1); } */ //Second try/catch to retrieve the graphml file Graph graph = null; try { //Build the graph graph = new GraphMLReader().readGraph("/test1.xml"); //graph = new GraphMLReader().readGraph("http://cs.marlboro.edu/~sheller/datafiles/test1.txt"); //GraphMLReader graphReader = new GraphMLReader(); //graphReader.readGraph(url); }catch(Exception e){ System.out.println("Couldn't get the graph"); e.printStackTrace(); System.exit(1); } //Build the visualization Visualization viz = new Visualization(); viz.add("graph", graph); viz.setInteractive("graph.edges", null, false); viz.setInteractive("graph.nodes", null, true); //Build the Label renderer LabelRenderer tr = new LabelRenderer("name"); tr.setRoundedCorner(8, 8); viz.setRendererFactory(new DefaultRendererFactory(tr)); //Drawing + Coloring action list ActionList draw = new ActionList(); draw.add(new ColorAction("graph.nodes", VisualItem.FILLCOLOR, ColorLib.rgb(255, 0, 0))); draw.add(new ColorAction("graph.nodes", VisualItem.TEXTCOLOR, ColorLib.rgb(0, 0, 0))); draw.add(new ColorAction("graph.nodes", VisualItem.STROKECOLOR, ColorLib.rgb(200, 200, 200))); //draw.add(new ShapeAction("graph.nodes", Constants.SHAPE_DIAMOND)); //Main Layout Routine ActionList layout = new ActionList(Activity.INFINITY); layout.add(new ForceDirectedLayout("graph", true)); layout.add(new RepaintAction()); //Set up the Display //Display display = new Display(viz); //display.addControlListener(new ZoomControl()); //display.addControlListener(new PanControl()); //display.addControlListener(new DragControl()); return(viz); } public static void main(String[] args){ //Set up the JFrame JFrame myFrame = new JFrame("GraphML"); myFrame.setSize(500, 500); myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Get our content pane for packing //Container content = myFrame.getContentPane(); //Set up the layout manager //content.setLayout(new BorderLayout()); //Build Visualization and load it into the JFrame //DataViz myViz = new DataViz(); //content.add(new JScrollPane(myViz.display), BorderLayout.CENTER); Visualization vizs = BuildIt(); //Run the Visualization //myFrame.add(new JScrollPane(myViz.display), BorderLayout.CENTER); Display display = new Display(vizs); display.addControlListener(new ZoomControl()); display.addControlListener(new PanControl()); display.addControlListener(new DragControl()); myFrame.add(display); myFrame.pack(); myFrame.setVisible(true); //Make it all visible //viz.graph.addNode(); vizs.run("draw"); vizs.run("layout"); } }