Last active
December 23, 2015 04:09
-
-
Save gsavin/6577865 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.event.KeyEvent; | |
import java.awt.event.MouseEvent; | |
import javax.swing.JOptionPane; | |
import javax.swing.SwingUtilities; | |
import org.graphstream.algorithm.generator.BarabasiAlbertGenerator; | |
import org.graphstream.graph.Graph; | |
import org.graphstream.graph.implementations.AdjacencyListGraph; | |
import org.graphstream.ui.graphicGraph.GraphicElement; | |
import org.graphstream.ui.swingViewer.View; | |
import org.graphstream.ui.swingViewer.Viewer; | |
import org.graphstream.ui.swingViewer.Viewer.CloseFramePolicy; | |
import org.graphstream.ui.swingViewer.Viewer.ThreadingModel; | |
import org.graphstream.ui.swingViewer.util.DefaultMouseManager; | |
import org.graphstream.ui.swingViewer.util.DefaultShortcutManager; | |
public class GUIEventsExample implements Runnable { | |
public static void main(String... args) { | |
SwingUtilities.invokeLater(new GUIEventsExample()); | |
} | |
Graph g; | |
Viewer viewer; | |
View view; | |
boolean focusOnClick = true; | |
public void run() { | |
if (!SwingUtilities.isEventDispatchThread()) | |
throw new RuntimeException("Not in SWING thread"); | |
init(); | |
} | |
private void init() { | |
g = new AdjacencyListGraph("g"); | |
BarabasiAlbertGenerator gen = new BarabasiAlbertGenerator(); | |
/* | |
* Generate an initial graph. | |
*/ | |
gen.addSink(g); | |
gen.begin(); | |
for (int i = 0; i < 100; i++) | |
gen.nextEvents(); | |
gen.end(); | |
gen.removeSink(g); | |
/* | |
* Initialize the GUI part. | |
*/ | |
viewer = new Viewer(g, ThreadingModel.GRAPH_IN_SWING_THREAD); | |
viewer.enableAutoLayout(); | |
view = viewer.addDefaultView(true); | |
viewer.setCloseFramePolicy(CloseFramePolicy.EXIT); | |
view.setMouseManager(new InternalMouseManager()); | |
view.setShortcutManager(new InternalShortcutManager()); | |
} | |
class InternalMouseManager extends DefaultMouseManager { | |
protected void mouseButtonPressOnElement(GraphicElement element, | |
MouseEvent event) { | |
super.mouseButtonPressOnElement(element, event); | |
if (focusOnClick && element instanceof GraphicNode) { | |
double x = element.getX(); | |
double y = element.getY(); | |
double z = element.getZ(); | |
view.getCamera().setViewCenter(x, y, z); | |
} | |
JOptionPane.showMessageDialog(view, | |
"You click on " + element.getId()); | |
} | |
} | |
class InternalShortcutManager extends DefaultShortcutManager { | |
public void keyTyped(KeyEvent event) { | |
super.keyTyped(event); | |
switch (event.getKeyChar()) { | |
case 'S': | |
case 's': | |
// | |
// Do something amazing. | |
// | |
String c = String.format("#%02X%02X%02X", | |
(int) (Math.random() * 255), | |
(int) (Math.random() * 255), | |
(int) (Math.random() * 255)); | |
g.addAttribute("ui.stylesheet", | |
String.format("node {fill-color: %s;}", c)); | |
break; | |
default: | |
// Nothing | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment