Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.BorderLayout;
- import java.awt.Dimension;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JPanel;
- import javax.swing.JScrollPane;
- import org.graphstream.graph.Graph;
- import org.graphstream.graph.Node;
- import org.graphstream.graph.implementations.SingleGraph;
- import org.graphstream.ui.swingViewer.GraphRenderer;
- import org.graphstream.ui.swingViewer.View;
- import org.graphstream.ui.swingViewer.Viewer;
- import org.graphstream.ui.swingViewer.ViewerListener;
- import org.graphstream.ui.swingViewer.ViewerPipe;
- import org.graphstream.ui.swingViewer.util.Camera;
- import org.graphstream.ui.swingViewer.util.DefaultCamera;
- public class Clicks extends JFrame implements ViewerListener {
- protected boolean loop = true;
- private JLabel label = new JLabel("bla");
- private Graph graph;
- public static void main(String args[]) {
- new Clicks();
- }
- public Clicks() {
- setSize(new Dimension(800, 600));
- // We do as usual to display a graph. This
- // connect the graph outputs to the viewer.
- // The viewer is a sink of the graph.
- graph = new SingleGraph("Clicks");
- graph.setStrict(false);
- Node n2 = graph.addNode("gs");
- Node n3 = graph.addNode("gs2");
- graph.addEdge("t", n2, n3);
- Viewer viewer = new Viewer(graph, Viewer.ThreadingModel.GRAPH_IN_ANOTHER_THREAD);
- //Viewer viewer = graph.display();
- View view = viewer.addDefaultView(false);
- viewer.enableAutoLayout();
- view.setMinimumSize(new Dimension(600, 600));
- view.setPreferredSize(new Dimension(600, 600));
- Camera cam = view.getCamera();
- this.getContentPane().setLayout(new BorderLayout());
- JScrollPane jsp = new JScrollPane(view);
- this.getContentPane().add(jsp, BorderLayout.CENTER);
- /*
- this.getContentPane().add(viewer.addView(
- String.format("defaultView_%d", (long) (Math.random() * 10000)),
- renderer, false), BorderLayout.CENTER);
- */
- this.getContentPane().add(label, BorderLayout.SOUTH);
- // The default action when closing the view is to quit
- // the program.
- viewer.setCloseFramePolicy(Viewer.CloseFramePolicy.HIDE_ONLY);
- // We connect back the viewer to the graph,
- // the graph becomes a sink for the viewer.
- // We also install us as a viewer listener to
- // intercept the graphic events.
- ViewerPipe fromViewer = new BlockingViewerPipe(viewer);
- fromViewer.addViewerListener(this);
- fromViewer.addSink(graph);
- this.setVisible(true);
- // Then we need a loop to wait for events.
- // In this loop we will need to call the
- // pump() method to copy back events that have
- // already occurred in the viewer thread inside
- // our thread.
- while(loop) {
- fromViewer.pump();
- System.out.println("Pumping....."+Math.random());
- }
- }
- public void viewClosed(String id) {
- loop = false;
- }
- public void buttonPushed(String id) {
- label.setText(id);
- Node n = graph.addNode("Node: "+(int)(Math.random()*100));
- graph.addEdge(n.getId(), n, graph.getNode(0));
- }
- public void buttonReleased(String id) {
- System.out.println("Button released on node "+id);
- }
- }
Add Comment
Please, Sign In to add comment