graphstream/gs-core

mouse clicks not working

sanaz-afzali opened this issue · 16 comments

I implemented the "Retrieving mouse clicks on the viewer" part of the tutorial just the same as it was and it is not working at all... it is not doing anything when I click my mouse...

If you talk about this example :

public class Clicks implements ViewerListener {
	protected boolean loop = true;

	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 do our work and to wait for events.
		// In this loop we will need to call the
		// pump() method before each use of the graph to copy back events
		// that have already occurred in the viewer thread inside
		// our thread.

		while(loop) {
			fromViewer.pump(); // or fromViewer.blockingPump(); in the nightly builds

			// here your simulation code.

			// You do not necessarily need to use a loop, this is only an example.
			// as long as you call pump() before using the graph. pump() is non
			// blocking.  If you only use the loop to look at event, use blockingPump()
			// to avoid 100% CPU usage. The blockingPump() method is only available from
			// the nightly builds.
		}
	}

	public void viewClosed(String id) {
		loop = false;
	}

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

	public void buttonReleased(String id) {
		System.out.println("Button released on node "+id);
	}
}

It only show you an empty graph, you should add some node if you want to see something on click.

@hichbra yes I talk about this, but I added two nodes and the edge. the graph is shown, but nothing happens when I click on its nodes.

Which version do you use and can you show your code ?

@hichbra
I am using the latest version and this is the code:

`

import org.graphstream.graph.Graph;
import org.graphstream.graph.implementations.SingleGraph;
import org.graphstream.ui.view.Viewer;
import org.graphstream.ui.view.ViewerListener;
import org.graphstream.ui.view.ViewerPipe;

public class Clicks implements ViewerListener {
protected boolean loop = true;

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();
    graph.addNode("a");
    graph.addNode("b");
    graph.addEdge("ab","a","b");
    // 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 do our work and to wait for events.
    // In this loop we will need to call the
    // pump() method before each use of the graph to copy back events
    // that have already occurred in the viewer thread inside
    // our thread.

    while(loop) {
        fromViewer.pump(); // or fromViewer.blockingPump(); in the nightly builds

        // here your simulation code.

        // You do not necessarily need to use a loop, this is only an example.
        // as long as you call pump() before using the graph. pump() is non
        // blocking.  If you only use the loop to look at event, use blockingPump()
        // to avoid 100% CPU usage. The blockingPump() method is only available from
        // the nightly builds.
    }
}

public void viewClosed(String id) {
    loop = false;
}

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

public void buttonReleased(String id) {
    System.out.println("Button released on node "+id);
}

}
`

If you were using the latest version, you would have received an error message with this code. You are probably using 1.3, can you verify and show your maven config

@hichbra
I didn't get any error, but it runs and shows the graph.
I am not working on maven, my project is gradle-based (because of other elements in the project) and I have written these two lines in dependencies:
implementation 'org.graphstream:gs-core:1.3' implementation 'org.graphstream:gs-ui:1.3'

I can't reproduce the issue. The code works fine and the version is correct. The problem must come from your environment.

You can try Graphstream 2.0 to see if the issues remains. Change your gradle to add :

repositories {
    mavenCentral()
}

allprojects {
    repositories {
        maven { url 'https://jitpack.io' }
    }
}

dependencies {
    api 'com.github.graphstream:gs-ui-swing:2.0-alpha'
    api 'com.github.graphstream:gs-core:2.0-alpha'
}

And change your program to :

import org.graphstream.graph.Graph;
import org.graphstream.graph.implementations.SingleGraph;
import org.graphstream.ui.view.Viewer;
import org.graphstream.ui.view.ViewerListener;
import org.graphstream.ui.view.ViewerPipe;

public class Clicks implements ViewerListener {
	protected boolean loop = true;

	public static void main(String args[]) {
	    System.setProperty("org.graphstream.ui", "swing"); 
	    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();
	    graph.addNode("a");
	    graph.addNode("b");
	    graph.addEdge("ab","a","b");
	    // 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 do our work and to wait for events.
	    // In this loop we will need to call the
	    // pump() method before each use of the graph to copy back events
	    // that have already occurred in the viewer thread inside
	    // our thread.
	
	    while(loop) {
	        fromViewer.pump(); // or fromViewer.blockingPump(); in the nightly builds
	
	        // here your simulation code.
	
	        // You do not necessarily need to use a loop, this is only an example.
	        // as long as you call pump() before using the graph. pump() is non
	        // blocking.  If you only use the loop to look at event, use blockingPump()
	        // to avoid 100% CPU usage. The blockingPump() method is only available from
	        // the nightly builds.
	    }
	}
	
	public void viewClosed(String id) {
	    loop = false;
	}
	
	public void buttonPushed(String id) {
	    System.out.println("Button pushed on node "+id);
	}
	
	public void buttonReleased(String id) {
	    System.out.println("Button released on node "+id);
	}

	public void mouseLeft(String arg0) {}
	
	public void mouseOver(String arg0) {}
}

@hichbra
I did what you just told, I am getting this error when publishing:

Could not find method api() for arguments [com.github.graphstream:gs-ui-swing:2.0-alpha] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.

Did you properly add jitpack ?

repositories {
    mavenCentral()
}

allprojects {
    repositories {
        maven { url 'https://jitpack.io' }
    }
}

Also, update your version of gradle. If after that it still don't work, you can take the .jar from github and add them to your classpath.

I finally migrated to graphstream-2.0-alpha,
and again same issue. the code runs with no error and no warning, but the graph doesn't react to clicks

On which operating system are you and on which version of Java?

os: windows - JDK: java11
and I use IntelliJ Idea

We tried in the same environment as you. First, you can use implementation keyword instead of api in your gradle build, that should work.
Then, I could not find any issues. That mean this problem is definitely from your config and not from the library. At this point, I can only encourage you to git clone graphstream, and try directly to find your issue.
The main concerning classes are DefaultMouseManager who handle the mouse and DefaultView, the main pane.

Good luck.

I think I've encountered the same problem here. It seems that the coordinates given by Point3 dst = bck.transform(elt.getX(), elt.getY(), 0); in the nodeContains(GraphicElement elt, double x, double y) method are twice as large as the coordinates given by the mouse pointer. g2.getTransform() seems to already contain this factor. I don't know to much about this stuff, but is there may be a difference between virtual and real pixels that needs to be converted somehow? (I'm using an UHD Display)

Found a solution to the same problem: Depending on your screen's resolution windows will scale up or down the applications on your pc. For some reason, this causes the nodes to be another position than actually displayed. To solve this go into Windows settings and search for display settings. Change the "Scale and layout" to 100% (might've been 150% recommended on a laptop). This instantly solved it for me.