2

Since I couldn't find any specific place to discuss this, I thought I'd post here... I'm using graphstream 1.1 (http://graphstream-project.org/), a graph visualization library for java, to develop a data visualization tool. I'm needing to retrieve mouseclicks on nodes to display related data, but after following the library tutorial, it's still not clear for me how to do this. Does anyone that used this could help me out here with a more straightfoward answer? The tutorial I'm following is at:

http://graphstream-project.org/doc/Tutorials/Graph-Visualisation_1.0/#retrieving-mouse-clicks-on-the-viewer

public class Clicks implements ViewerListener {
    protected boolean loop;

    public static void main(String args[]) {
        new Clicks();
    }
    public Clicks() {
    // 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 graph = new SingleGraph("Clicks");
        Viewer viewer = graph.display();

    // 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 = viewer.newViewerPipe();
        fromViewer.addViewerListener(this);
        fromViewer.addSink(graph);

    // 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 occured in the viewer thread inside
    // our thread.

        while(loop) {
            fromViewer.pump();
        }
    }

    viewClosed(String id) {
        loop = false;
    }

    buttonPushed(String id) {
        System.out.println("Button pushed on node "+id);
    }

    buttonReleased(String id) {
        System.out.println("Button released on node "+id);
    }
}
lalli
  • 6,083
  • 7
  • 42
  • 55
mcopo
  • 103
  • 1
  • 6
  • Are you getting caught up somewhere? Can you post a more specific question such as errors? Unexpected behaviors? – Grambot May 21 '13 at 13:40
  • My code is preety much the same of the tutorial. Just changed the declarations of the functions at the end to make it possible to compile. The listener seems not to be detecting the clicks. It's supposed to print on console the node's id when I click, but nothing happens... not even a exception. The console remains clean. – mcopo May 21 '13 at 14:15
  • Care to post your code changes? – Grambot May 22 '13 at 12:48
  • I simply re-declared them as "public void", since the original on the tutorial doesn't have any kind of declaration - only the fucntion names are present. The rest is identical to the tutorial – mcopo May 23 '13 at 05:53
  • I apologize that my response is so slow. Without a code example pasted into your question or a link to something like Pastebin this is too difficult to troubleshoot. You may have redeclared a constructor or misread something they posted that should have just been a general statement on an existing function; there is a lot in that tutorial to take in before its even possible to troubleshoot based off what you've provided – Grambot May 24 '13 at 22:18
  • No problem. I thought it'd be unnecessary to post code since its identical. But since you asked, here it is. Just edited it on the question.I tried changing the 3 last funcitons to public void, but the rest is the same and I got the results I described on previous comments. The truth is, I'm a noob when it comes to listeners. When I need one, I always look in swing tutorials and simply copy code changing really minor things. But this one here is specific to the library, so that tutorial I linked is the only I know. I'd like to know the next steps to make this thing work. – mcopo May 25 '13 at 01:57
  • Just got it solved! Thank you for trying to answer. I'm posting the details on my answer. – mcopo May 28 '13 at 19:51

2 Answers2

2

Just got it solved! I sent an e-mail to their mailing group. The tutorial code on the website was lacking some information. Those three functions need to be public void, and other 'imports' must be added:

import org.graphstream.ui.swingViewer.Viewer;
import org.graphstream.ui.swingViewer.ViewerListener;
import org.graphstream.ui.swingViewer.ViewerPipe;
mcopo
  • 103
  • 1
  • 6
0

Here a simple code to show you how to add click event to the nodes of a given graph in graphstream library. This code show how you can change the node's background by clicking on it. The colors are choosen randomly:

public class TutoMouseClicked{

    Graph graph;

    public TutoMouseClicked(){
    }    

    public void run(){

        //Build a simple graph with one node
        graph = new SingleGraph("TutoMouseClicked", false, true);
        graph.setAttribute("ui.quality");
        graph.setAttribute("ui.antialias");

        Node n1 = graph.addNode("n1");
        n1.setAttribute("ui.style", "size: 100px;");

        Viewer viewer = graph.display();
        viewer.getDefaultView().setMouseManager(new TutoMouseManage());
    }

     public static void main(String args[]) {        
        new TutoMouseClicked().run();
    }
}

And the class TutoMouseManage that implements MouseManager interface:

public class TutoMouseManage implements MouseManager{


    /**
     * The view this manager operates upon. 
     */ 
    protected View view; 


    /**
     * The graph to modify according to the view actions. 
     */ 
    protected GraphicGraph graph;


    protected GraphicElement element; 

    @Override
    public void init(GraphicGraph gg, View view) {
        this.graph = gg;
        this.view = view;
        view.addMouseListener(this);
        view.addMouseMotionListener(this);
    }

    @Override
    public void release() {
        view.removeMouseListener(this);
        view.removeMouseMotionListener(this);
    }

    @Override
    public void mouseClicked(MouseEvent me) {
        element = view.findNodeOrSpriteAt(me.getX(), me.getY());
        if(element != null){
            Random r = new Random();
            element.setAttribute("ui.style", "fill-color: rgb("+r.nextInt(256)+","+r.nextInt(256)+","+r.nextInt(256)+");");
        }

    }

    @Override
    public void mousePressed(MouseEvent me) {
    }

    @Override
    public void mouseReleased(MouseEvent me) {
    }

    @Override
    public void mouseEntered(MouseEvent me) {
    }

    @Override
    public void mouseExited(MouseEvent me) {
    }

    @Override
    public void mouseDragged(MouseEvent me) {
    }

    @Override
    public void mouseMoved(MouseEvent me) {
    }

}

you can adapt this code to get what you need, add any other mouse event you want: mouse released, mouse pressed, mouse dragged and all mouse events.