Skip to main content
misplaced a bracket
Source Link
import org.gephi.graph.api.Graph; import org.gephi.graph.api.GraphModel; import org.gephi.graph.api.Node; import org.gephi.graph.api.NodeData; import org.gephi.graph.api.NodeIterable; import org.gephi.graph.api.NodeIterator; import org.gephi.preview.api.PreviewModel; import org.gephi.preview.api.PreviewMouseEvent; import org.gephi.preview.api.PreviewProperties; import org.gephi.preview.spi.PreviewMouseListener; import org.gephi.project.api.Workspace; import org.openide.util.Lookup; import org.openide.util.lookup.ServiceProvider; /** * * @author ksexton */ @ServiceProvider(service = PreviewMouseListener.class) public class MyMouseListener implements PreviewMouseListener { public void mouseClicked(PreviewMouseEvent event, PreviewProperties properties, Workspace workspace) { System.out.println("(" + event.x + "," + event.y + ")"); Lookup lookup = workspace.getLookup(); GraphModel lookup1 = lookup.lookup(GraphModel.class); Graph graph = lookup1.getGraph(); NodeIterable nodes = graph.getNodes(); NodeIterator iterator = nodes.iterator(); Node selectedNode = null; while (iterator.hasNext()) { Node next = iterator.next(); NodeData nodeData = next.getNodeData(); float x = nodeData.x(); float y = -nodeData.y(); // event y is always wrong. float z = nodeData.z(); float size = nodeData.getSize(); final int minX = Math.round(x - size); final int maxX = Math.round(x + size); final int minY = Math.round(y - size); final int maxY = Math.round(y + size); System.out.println("(" + minX + "," + minY+")" + ", (" + maxX + "," + maxY+")"); if (minX <= event.x && event.x <= maxX && minY <= event.y && event.y <= maxY) { selectedNode = next; } } if (selectedNode != null) { System.out.println("Found a Node!: (" + selectedNode.getNodeData().x() + "," + selectedNode.getNodeData().y() + ")"); } } public void mousePressed(PreviewMouseEvent event, PreviewProperties properties, Workspace workspace) { } public void mouseDragged(PreviewMouseEvent event, PreviewProperties properties, Workspace workspace) { } public void mouseReleased(PreviewMouseEvent event, PreviewProperties properties, Workspace workspace) { } } 
} import org.gephi.preview.plugin.renderers.NodeRenderer; import org.gephi.preview.spi.MouseResponsiveRenderer; import org.gephi.preview.spi.PreviewMouseListener; import org.gephi.preview.spi.Renderer; import org.openide.util.lookup.ServiceProvider; /** * * @author ksexton */ @ServiceProvider(service = Renderer.class) public class NodeRenderer2 extends NodeRenderer implements MouseResponsiveRenderer { public boolean needsPreviewMouseListener(PreviewMouseListener previewMouseListener) { return true; } } 
import org.gephi.graph.api.Graph; import org.gephi.graph.api.GraphModel; import org.gephi.graph.api.Node; import org.gephi.graph.api.NodeData; import org.gephi.graph.api.NodeIterable; import org.gephi.graph.api.NodeIterator; import org.gephi.preview.api.PreviewModel; import org.gephi.preview.api.PreviewMouseEvent; import org.gephi.preview.api.PreviewProperties; import org.gephi.preview.spi.PreviewMouseListener; import org.gephi.project.api.Workspace; import org.openide.util.Lookup; import org.openide.util.lookup.ServiceProvider; /** * * @author ksexton */ @ServiceProvider(service = PreviewMouseListener.class) public class MyMouseListener implements PreviewMouseListener { public void mouseClicked(PreviewMouseEvent event, PreviewProperties properties, Workspace workspace) { System.out.println("(" + event.x + "," + event.y + ")"); Lookup lookup = workspace.getLookup(); GraphModel lookup1 = lookup.lookup(GraphModel.class); Graph graph = lookup1.getGraph(); NodeIterable nodes = graph.getNodes(); NodeIterator iterator = nodes.iterator(); Node selectedNode = null; while (iterator.hasNext()) { Node next = iterator.next(); NodeData nodeData = next.getNodeData(); float x = nodeData.x(); float y = -nodeData.y(); // event y is always wrong. float z = nodeData.z(); float size = nodeData.getSize(); final int minX = Math.round(x - size); final int maxX = Math.round(x + size); final int minY = Math.round(y - size); final int maxY = Math.round(y + size); System.out.println("(" + minX + "," + minY+")" + ", (" + maxX + "," + maxY+")"); if (minX <= event.x && event.x <= maxX && minY <= event.y && event.y <= maxY) { selectedNode = next; } } if (selectedNode != null) { System.out.println("Found a Node!: (" + selectedNode.getNodeData().x() + "," + selectedNode.getNodeData().y() + ")"); } } public void mousePressed(PreviewMouseEvent event, PreviewProperties properties, Workspace workspace) { } public void mouseDragged(PreviewMouseEvent event, PreviewProperties properties, Workspace workspace) { } public void mouseReleased(PreviewMouseEvent event, PreviewProperties properties, Workspace workspace) { } 
} import org.gephi.preview.plugin.renderers.NodeRenderer; import org.gephi.preview.spi.MouseResponsiveRenderer; import org.gephi.preview.spi.PreviewMouseListener; import org.gephi.preview.spi.Renderer; import org.openide.util.lookup.ServiceProvider; /** * * @author ksexton */ @ServiceProvider(service = Renderer.class) public class NodeRenderer2 extends NodeRenderer implements MouseResponsiveRenderer { public boolean needsPreviewMouseListener(PreviewMouseListener previewMouseListener) { return true; } } 
import org.gephi.graph.api.Graph; import org.gephi.graph.api.GraphModel; import org.gephi.graph.api.Node; import org.gephi.graph.api.NodeData; import org.gephi.graph.api.NodeIterable; import org.gephi.graph.api.NodeIterator; import org.gephi.preview.api.PreviewModel; import org.gephi.preview.api.PreviewMouseEvent; import org.gephi.preview.api.PreviewProperties; import org.gephi.preview.spi.PreviewMouseListener; import org.gephi.project.api.Workspace; import org.openide.util.Lookup; import org.openide.util.lookup.ServiceProvider; /** * * @author ksexton */ @ServiceProvider(service = PreviewMouseListener.class) public class MyMouseListener implements PreviewMouseListener { public void mouseClicked(PreviewMouseEvent event, PreviewProperties properties, Workspace workspace) { System.out.println("(" + event.x + "," + event.y + ")"); Lookup lookup = workspace.getLookup(); GraphModel lookup1 = lookup.lookup(GraphModel.class); Graph graph = lookup1.getGraph(); NodeIterable nodes = graph.getNodes(); NodeIterator iterator = nodes.iterator(); Node selectedNode = null; while (iterator.hasNext()) { Node next = iterator.next(); NodeData nodeData = next.getNodeData(); float x = nodeData.x(); float y = -nodeData.y(); // event y is always wrong. float z = nodeData.z(); float size = nodeData.getSize(); final int minX = Math.round(x - size); final int maxX = Math.round(x + size); final int minY = Math.round(y - size); final int maxY = Math.round(y + size); System.out.println("(" + minX + "," + minY+")" + ", (" + maxX + "," + maxY+")"); if (minX <= event.x && event.x <= maxX && minY <= event.y && event.y <= maxY) { selectedNode = next; } } if (selectedNode != null) { System.out.println("Found a Node!: (" + selectedNode.getNodeData().x() + "," + selectedNode.getNodeData().y() + ")"); } } public void mousePressed(PreviewMouseEvent event, PreviewProperties properties, Workspace workspace) { } public void mouseDragged(PreviewMouseEvent event, PreviewProperties properties, Workspace workspace) { } public void mouseReleased(PreviewMouseEvent event, PreviewProperties properties, Workspace workspace) { } } 
import org.gephi.preview.plugin.renderers.NodeRenderer; import org.gephi.preview.spi.MouseResponsiveRenderer; import org.gephi.preview.spi.PreviewMouseListener; import org.gephi.preview.spi.Renderer; import org.openide.util.lookup.ServiceProvider; /** * * @author ksexton */ @ServiceProvider(service = Renderer.class) public class NodeRenderer2 extends NodeRenderer implements MouseResponsiveRenderer { public boolean needsPreviewMouseListener(PreviewMouseListener previewMouseListener) { return true; } } 
Source Link

I played with gephi for a few days and I'm pretty sure there's no way to get the exact node clicked. It really doesn't seem that their API was built with interactivity in mind. However I've found that this works relatively well for a 2D graph.

Main Class:

import java.awt.BorderLayout; import java.awt.Color; import javax.swing.JFrame; import org.gephi.graph.api.*; import org.gephi.io.processor.plugin.DefaultProcessor; import org.gephi.preview.api.PreviewController; import org.gephi.preview.api.PreviewModel; import org.gephi.preview.api.PreviewProperty; import org.gephi.preview.api.ProcessingTarget; import org.gephi.preview.api.RenderTarget; import org.gephi.preview.types.DependantOriginalColor; import org.gephi.project.api.ProjectController; import org.gephi.project.api.Workspace; import org.openide.util.Lookup; import processing.core.PApplet; /** * This demo shows basic features from GraphAPI, how to create and query a graph * programmatically. * * @author Mathieu Bastian */ public class TestGraph { public void script() { //Init a project - and therefore a workspace ProjectController pc = Lookup.getDefault().lookup(ProjectController.class); pc.newProject(); Workspace workspace = pc.getCurrentWorkspace(); //Get a graph model - it exists because we have a workspace GraphModel graphModel = Lookup.getDefault().lookup(GraphController.class).getModel(); //Create three nodes Node n0 = graphModel.factory().newNode("n0"); n0.getNodeData().setLabel("Node 0"); Node n1 = graphModel.factory().newNode("n1"); n1.getNodeData().setLabel("Node 1"); Node n2 = graphModel.factory().newNode("n2"); n2.getNodeData().setLabel("Node 2"); //Create three edges Edge e1 = graphModel.factory().newEdge(n1, n2, 1f, true); Edge e2 = graphModel.factory().newEdge(n0, n2, 2f, true); Edge e3 = graphModel.factory().newEdge(n2, n0, 2f, true); //This is e2's mutual edge //Append as a Directed Graph DirectedGraph directedGraph = graphModel.getDirectedGraph(); directedGraph.addNode(n0); directedGraph.addNode(n1); directedGraph.addNode(n2); directedGraph.addEdge(e1); directedGraph.addEdge(e2); directedGraph.addEdge(e3); //Count nodes and edges System.out.println("Nodes: "+directedGraph.getNodeCount()+" Edges: "+directedGraph.getEdgeCount()); //Get a UndirectedGraph now and count edges UndirectedGraph undirectedGraph = graphModel.getUndirectedGraph(); GraphView view = undirectedGraph.getView(); System.out.println("Edges: "+undirectedGraph.getEdgeCount()); //The mutual edge is automatically merged //Iterate over nodes for(Node n : directedGraph.getNodes()) { Node[] neighbors = directedGraph.getNeighbors(n).toArray(); System.out.println(n.getNodeData().getLabel()+" has "+neighbors.length+" neighbors"); } //Iterate over edges for(Edge e : directedGraph.getEdges()) { System.out.println(e.getSource().getNodeData().getId()+" -> "+e.getTarget().getNodeData().getId()); } //Find node by id Node node2 = directedGraph.getNode("n2"); //Get degree System.out.println("Node2 degree: "+directedGraph.getDegree(node2)); //Preview configuration PreviewController previewController = Lookup.getDefault().lookup(PreviewController.class); PreviewModel previewModel = previewController.getModel(); previewModel.getProperties().putValue(PreviewProperty.SHOW_NODE_LABELS, Boolean.FALSE); previewModel.getProperties().putValue(PreviewProperty.NODE_LABEL_COLOR, new DependantOriginalColor(Color.WHITE)); previewModel.getProperties().putValue(PreviewProperty.EDGE_CURVED, Boolean.FALSE); previewModel.getProperties().putValue(PreviewProperty.EDGE_OPACITY, 50); previewModel.getProperties().putValue(PreviewProperty.EDGE_RADIUS, 10f); previewModel.getProperties().putValue(PreviewProperty.BACKGROUND_COLOR, Color.BLACK); previewController.refreshPreview(); //New Processing target, get the PApplet ProcessingTarget target = (ProcessingTarget) previewController.getRenderTarget(RenderTarget.PROCESSING_TARGET); PApplet applet = target.getApplet(); applet.init(); //Refresh the preview and reset the zoom previewController.render(target); target.refresh(); target.resetZoom(); //Add the applet to a JFrame and display JFrame frame = new JFrame("Test Preview"); frame.setLayout(new BorderLayout()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(applet, BorderLayout.CENTER); frame.pack(); frame.setVisible(true); } public static void main(String[] args) { TestGraph previewJFrame = new TestGraph(); previewJFrame.script(); } } 

Mouse Listener substitute:

import org.gephi.graph.api.Graph; import org.gephi.graph.api.GraphModel; import org.gephi.graph.api.Node; import org.gephi.graph.api.NodeData; import org.gephi.graph.api.NodeIterable; import org.gephi.graph.api.NodeIterator; import org.gephi.preview.api.PreviewModel; import org.gephi.preview.api.PreviewMouseEvent; import org.gephi.preview.api.PreviewProperties; import org.gephi.preview.spi.PreviewMouseListener; import org.gephi.project.api.Workspace; import org.openide.util.Lookup; import org.openide.util.lookup.ServiceProvider; /** * * @author ksexton */ @ServiceProvider(service = PreviewMouseListener.class) public class MyMouseListener implements PreviewMouseListener { public void mouseClicked(PreviewMouseEvent event, PreviewProperties properties, Workspace workspace) { System.out.println("(" + event.x + "," + event.y + ")"); Lookup lookup = workspace.getLookup(); GraphModel lookup1 = lookup.lookup(GraphModel.class); Graph graph = lookup1.getGraph(); NodeIterable nodes = graph.getNodes(); NodeIterator iterator = nodes.iterator(); Node selectedNode = null; while (iterator.hasNext()) { Node next = iterator.next(); NodeData nodeData = next.getNodeData(); float x = nodeData.x(); float y = -nodeData.y(); // event y is always wrong. float z = nodeData.z(); float size = nodeData.getSize(); final int minX = Math.round(x - size); final int maxX = Math.round(x + size); final int minY = Math.round(y - size); final int maxY = Math.round(y + size); System.out.println("(" + minX + "," + minY+")" + ", (" + maxX + "," + maxY+")"); if (minX <= event.x && event.x <= maxX && minY <= event.y && event.y <= maxY) { selectedNode = next; } } if (selectedNode != null) { System.out.println("Found a Node!: (" + selectedNode.getNodeData().x() + "," + selectedNode.getNodeData().y() + ")"); } } public void mousePressed(PreviewMouseEvent event, PreviewProperties properties, Workspace workspace) { } public void mouseDragged(PreviewMouseEvent event, PreviewProperties properties, Workspace workspace) { } public void mouseReleased(PreviewMouseEvent event, PreviewProperties properties, Workspace workspace) { } 

Renderer:

} import org.gephi.preview.plugin.renderers.NodeRenderer; import org.gephi.preview.spi.MouseResponsiveRenderer; import org.gephi.preview.spi.PreviewMouseListener; import org.gephi.preview.spi.Renderer; import org.openide.util.lookup.ServiceProvider; /** * * @author ksexton */ @ServiceProvider(service = Renderer.class) public class NodeRenderer2 extends NodeRenderer implements MouseResponsiveRenderer { public boolean needsPreviewMouseListener(PreviewMouseListener previewMouseListener) { return true; } } 

If anyone has a better, less error prone way I'd like to know.