Può la grafica Jung apparire nello stesso posto ogni volta?
Domanda
Sto usando JUNG ( http://jung.sourceforge.net/index.html) per disegnare elementi grafici in Java. Il software è grande ma ho una piccola domanda. Come posso essere sicuro che il grafico visualizzato è ogni volta la stessa (nessun cambiamento è l'architettura o posizione)?
Per essere più precisi: il modello grafico (i dati per essere rappresentati) non cambia, ma la sua rappresentazione cambia ogni volta che mi ha colpito il pulsante "Visualizza grafico" :) [alcuni vertici sono in altri luoghi, ad esempio: a volte in la parte superiore della finestra, talvolta nella parte inferiore]
Grazie,
Iulian
Soluzione
Un StaticLayout permette di specificare un vertice a Point2D trasformatore . Questo vi permetterà di controllare dove i vertici sono collocati e dovrebbero fare quello che vuoi fare. Si consiglia di utilizzare il seguente costruttore :
public StaticLayout(Graph<V,E> graph,
org.apache.commons.collections15.Transformer<V,Point2D> initializer)
È necessario implementare il proprio trasformatore che prende in un vertice e restituisce la posizione in cui dovrebbe apparire il vertice. Un esempio del suo uso in:
package test;
import java.awt.Dimension;
import java.awt.geom.Point2D;
import java.io.IOException;
import javax.swing.JFrame;
import org.apache.commons.collections15.Transformer;
import edu.uci.ics.jung.algorithms.layout.StaticLayout;
import edu.uci.ics.jung.graph.Graph;
import edu.uci.ics.jung.graph.SparseMultigraph;
import edu.uci.ics.jung.visualization.VisualizationViewer;
/**
* Jung example - vertices appearing in same location
*
* @author Kah
*/
public class StaticLocation {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// Setup the example graph.
Graph<Integer, String> basis = new SparseMultigraph<Integer, String>();
basis.addVertex(Integer.valueOf(0));
basis.addVertex(Integer.valueOf(1));
basis.addVertex(Integer.valueOf(2));
basis.addEdge("Edge 1", Integer.valueOf(0), Integer.valueOf(1));
basis.addEdge("Edge 2", Integer.valueOf(0), Integer.valueOf(2));
basis.addEdge("Edge 3", Integer.valueOf(1), Integer.valueOf(2));
Transformer<Integer, Point2D> locationTransformer = new Transformer<Integer, Point2D>() {
@Override
public Point2D transform(Integer vertex) {
int value = (vertex.intValue() * 40) + 20;
return new Point2D.Double((double) value, (double) value);
}
};
StaticLayout<Integer, String> layout = new StaticLayout<Integer, String>(
basis, locationTransformer);
layout.setSize(new Dimension(250, 250));
VisualizationViewer<Integer, String> vv = new VisualizationViewer<Integer, String>(
layout);
vv.setPreferredSize(new Dimension(250, 250));
JFrame frame = new JFrame("Simple Graph View 2");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(vv);
vv.setOpaque(false);
frame.pack();
frame.setVisible(true);
}
}
Aggiunto 20 Feb 2010:
Un'alternativa è quella di utilizzare un PersistentLayoutImpl per salvare le posizioni dei vertici in un file. Tuttavia, è necessario anche in qualche modo persistere il grafico per ottenere che i vertici ed i vertici erano in là (questo deve persistito a parte). Ci sono certo numero di classi per persistere il grafico in edu.uci.ics.jung.io . Questo è un esempio che utilizza solo PersistentLayoutImpl :
package test;
import java.awt.Dimension;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JFrame;
import org.apache.commons.collections15.Transformer;
import edu.uci.ics.jung.algorithms.layout.Layout;
import edu.uci.ics.jung.algorithms.layout.SpringLayout2;
import edu.uci.ics.jung.graph.Graph;
import edu.uci.ics.jung.graph.SparseMultigraph;
import edu.uci.ics.jung.io.GraphMLReader;
import edu.uci.ics.jung.io.GraphMLWriter;
import edu.uci.ics.jung.visualization.VisualizationViewer;
import edu.uci.ics.jung.visualization.decorators.ToStringLabeller;
import edu.uci.ics.jung.visualization.layout.PersistentLayoutImpl;
/**
* Jung example - vertices appearing in same location
*
* @author Kah
*/
public class PersistentVertices
{
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException
{
// Setup the example graph.
try
{
VisualizationViewer<Integer, String> vv = new VisualizationViewer<Integer, String>(
getLayout());
vv.setPreferredSize(new Dimension(250, 250));
JFrame frame = new JFrame("Simple Graph View 2");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(vv);
vv.setOpaque(false);
frame.pack();
frame.setVisible(true);
}
catch (Exception e)
{
e.printStackTrace();
}
}
private static Layout<Integer, String> getLayout() throws IOException,
ClassNotFoundException
{
Graph<Integer, String> graph = new SparseMultigraph<Integer, String>();
File source = new File("C:\\layout.dat");
SpringLayout2<Integer, String> backing = new SpringLayout2<Integer, String>(
graph);
PersistentLayoutImpl<Integer, String> layout = new PersistentLayoutImpl<Integer, String>(
backing);
layout.setSize(new Dimension(250, 250));
// Note that you also need to put the vertices and edges back before
// restoring.
graph.addVertex(Integer.valueOf(0));
graph.addVertex(Integer.valueOf(1));
graph.addVertex(Integer.valueOf(2));
graph.addEdge("Edge 1", Integer.valueOf(0), Integer.valueOf(1));
graph.addEdge("Edge 2", Integer.valueOf(0), Integer.valueOf(2));
graph.addEdge("Edge 3", Integer.valueOf(1), Integer.valueOf(2));
if (source.exists())
{
layout.restore(source.getAbsolutePath());
}
else
{
layout.persist(source.getAbsolutePath());
}
return layout;
}
}
Si noti che l'esempio non persistono i vertici e gli spigoli ancora, come non ho avuto il tempo di capire come utilizzare le classi in edu.uci.ics.jung.io ancora.