66529

How to draw graph inside swing with GraphStream actually?

I am trying to implement drawing of tutorial graph inside swing, but failing.

The code is follows:

package tests.graphstream;

import java.awt.BorderLayout; import javax.swing.JFrame; import javax.swing.SwingUtilities; import org.graphstream.graph.Graph; import org.graphstream.graph.implementations.SingleGraph; import org.graphstream.ui.swingViewer.View; import org.graphstream.ui.swingViewer.Viewer; public class Tutorial1_01 { private static Graph graph = new SingleGraph("Tutorial 1"); public static class MyFrame extends JFrame { private static final long serialVersionUID = 8394236698316485656L; //private Graph graph = new MultiGraph("embedded"); //private Viewer viewer = new Viewer(graph, Viewer.ThreadingModel.GRAPH_IN_ANOTHER_THREAD); private Viewer viewer = new Viewer(graph, Viewer.ThreadingModel.GRAPH_IN_SWING_THREAD); private View view = viewer.addDefaultView(false); public MyFrame() { setLayout(new BorderLayout()); add(view, BorderLayout.CENTER); setDefaultCloseOperation(EXIT_ON_CLOSE); } } public static void main(String args[]) { SwingUtilities.invokeLater(new Runnable() { public void run() { MyFrame frame = new MyFrame(); frame.setSize(320, 240); frame.setLocationRelativeTo(null); frame.setVisible(true); graph.addNode("A"); graph.addNode("B"); graph.addNode("C"); graph.addEdge("AB", "A", "B"); graph.addEdge("BC", "B", "C"); graph.addEdge("CA", "C", "A"); graph.addAttribute("ui.quality"); graph.addAttribute("ui.antialias"); } }); } }

And it draws this:

<img src="https://i.stack.imgur.com/meXrf.png" alt="enter image description here">

If drag nodes, it turns to:

<img src="https://i.stack.imgur.com/mfvt8.png" alt="enter image description here">

How to get results, close to graph.display()?

Answer1:

This is a side effect due to the fact that nodes do not have x and y coordinates by default.

To prevent this you should whether :

    <li>activate the autolayout on your viewer object with viewer.enableAutoLayout();</li> <li>or specify by yourself some xand y attributes for each node.</li> </ul>

    With Autolayout

    // ... public MyFrame() { setLayout(new BorderLayout()); add(view, BorderLayout.CENTER); setDefaultCloseOperation(EXIT_ON_CLOSE); // Activate autolayout here : viewer.enableAutoLayout(); } // ...

    With nodes attributes

    // In main() ... Node a = graph.addNode("A"); a.addAttribute("xy", 0, 0); Node b = graph.addNode("B"); b.addAttribute("xy", 10, 0); Node c = graph.addNode("C"); c.addAttribute("xy", 10, 10); // ...

Recommend

  • Python Tkinter: addressing Label widget created by for loop
  • Python Tkinter: addressing Label widget created by for loop
  • I want to handle a clickbutton event from another page
  • How to layout nodes once in GraphStream?
  • How do I nest layout managers without using swing classes/methods?
  • Git rebase conflicts after successful merge?
  • Mac: How to assign a file extension to be opened with my java .app application?
  • Sequence of random numbers without repeats
  • hibernate entity is not mapped since version 4.3.2
  • Open pdf.js with a data URL in IE11
  • Http 404 resource not found [duplicate]
  • How to use output buffering inside PHPUnit test?
  • In a MEAN stack, how can I do one-time MongoDB indexing?
  • Python PIL remove sections of an image based on its colour
  • Consuming a web service with the Netbeans Platform
  • presentShareDialogWithParams posts to FB wall, but callback handler results say error
  • Content-Length header not returned from Pylons response
  • Play WS (2.2.1): post/put large request
  • Highlight and Bold text in JTextPane
  • how to adjust image in a panel in Java swing?
  • How to access EntityManager inside Entity class in EJB3
  • Xamarin Forms - UWP Fonts
  • how to do an event when i swipe from fragment to the other
  • req.body is undefined - nodejs
  • Possible to stop flickering java tooltip in heavyweight mode?
  • Validaiting emails with Net.Mail MailAddress
  • sending/ receiving email in Java
  • vba code to select only visible cells in specific column except heading
  • Akka Routing: Reply's send to router ends up as dead letters
  • Cannot Parse HTML Data Using Android / JSOUP
  • How to delete a row from a dynamic generate table using jquery?
  • Proper way to use connect-multiparty with express.js?
  • JTable with a ScrollPane misbehaving
  • Java static initializers and reflection
  • unknown Exception android
  • Checking variable from a different class in C#
  • Observable and ngFor in Angular 2
  • failed to connect to specific WiFi in android programmatically
  • Unable to use reactive element in my shiny app
  • How can I use threading to 'tick' a timer to be accessed by other threads?