2

アプリケーションで頂点とエッジを含むいくつかのグラフを描画したいと思います。JGraphは、グラフをプロットするための優れたライブラリであることがわかりました。私はそれについていくつかのオンラインソースを調べましたが、SwingアプリケーションにJGraphを埋め込む方法に関する関連記事を見つけることができませんでした。(JGraphをJFrameなどで表示する)。誰かがそれを手伝ってくれる?

4

3 に答える 3

3

このコードは私のために働いた:

// Insert the cells via the cache, so they get selected
graph.getGraphLayoutCache().insert(cells);

// Show in Frame
JFrame frame = new JFrame();
frame.getContentPane().add(new JScrollPane(graph));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);

サンプル全体が必要な場合は、アップロードすることもできます。

コードはサイトからの変更されたサンプルでなければならないと思います。どうぞ:

import java.awt.Color;
import java.awt.geom.Rectangle2D;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import org.jgraph.JGraph;
import org.jgraph.graph.DefaultEdge;
import org.jgraph.graph.DefaultGraphCell;
import org.jgraph.graph.DefaultGraphModel;
import org.jgraph.graph.GraphConstants;
import org.jgraph.graph.GraphModel;

public class Foo {

    public static void main(String[] args) {

        // Construct Model and Graph
        GraphModel model = new DefaultGraphModel();
        JGraph graph = new JGraph(model);
        // Control-drag should clone selection
        graph.setCloneable(true);

        // Enable edit without final RETURN keystroke
        graph.setInvokesStopCellEditing(true);

        // When over a cell, jump to its default port (we only have one, anyway)
        graph.setJumpToDefaultPort(true);

        // Insert all three cells in one call, so we need an array to store them
        DefaultGraphCell[] cells = new DefaultGraphCell[3];

        // Create Hello Vertex
        cells[0] = createVertex("Hello", 20, 20, 40, 20, null, false );

        // Create World Vertex
        cells[1] = createVertex("World", 140, 140, 40, 20,
                Color.ORANGE, true);

        // Create Edge
        DefaultEdge edge = new DefaultEdge("foo");
        // Fetch the ports from the new vertices, and connect them with the edge
        edge.setSource(cells[0].getChildAt(0));
        edge.setTarget(cells[0].getChildAt(0));
        cells[2] = edge;

        // Create Edge
        DefaultEdge edge1 = new DefaultEdge();
        // Fetch the ports from the new vertices, and connect them with the edge
//        cells[0].addPort();
//        cells[1].addPort();
//        edge1.setSource(cells[1]);
//        edge1.setTarget(cells[0]);
//        cells[3] = edge1;

        // Set Arrow Style for edge
        int arrow = GraphConstants.ARROW_CLASSIC;
        GraphConstants.setLineEnd(edge.getAttributes(), arrow);
        GraphConstants.setEndFill(edge.getAttributes(), true);

        // Insert the cells via the cache, so they get selected
        graph.getGraphLayoutCache().insert(cells);

        // Show in Frame
        JFrame frame = new JFrame();
        frame.getContentPane().add(new JScrollPane(graph));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

    public static DefaultGraphCell createVertex(String name, double x,
            double y, double w, double h, Color bg, boolean raised) {

        // Create vertex with the given name
        DefaultGraphCell cell = new DefaultGraphCell(name);

        // Set bounds
        GraphConstants.setBounds(cell.getAttributes(),
                new Rectangle2D.Double(x, y, w, h));

        // Set fill color
        if (bg != null) {
            GraphConstants.setGradientColor(cell.getAttributes(), bg);
            GraphConstants.setOpaque(cell.getAttributes(), true);
        }

        // Set raised border
        if (raised) {
            GraphConstants.setBorder(cell.getAttributes(),
                    BorderFactory.createRaisedBevelBorder());
        } else // Set black border
        {
            GraphConstants.setBorderColor(cell.getAttributes(),
                    Color.black);
        }
        // Add a Floating Port
        cell.addPort();

        return cell;
    }
}

これは非常に単純な例です。私はデータベースデータのいくつかの複雑なグラフを作成しましたが、私のニーズにより適したJung2を使用することになりました。

于 2011-07-17T08:45:29.660 に答える
2

JGraphパッケージcom.mxgraph.examples.swingcom.mxgraph.examples.swing.editorexamplesフォルダーが含まれています。

$ ls examples / com / mxgraph / examples / swing
ClickHandler.javaGraphEditor.javaSchemaEditor.javaエディター
CustomCanvas.javaHelloWorld.javaUserObject.javaイメージ
FixedPoints.javaPort.javaValidation.javaリソース

$ ls examples / com / mxgraph / examples / swing / editor
BasicGraphEditor.java EditorMenuBar.java JTableRenderer.java
DefaultFileFilter.java EditorPalette.java SchemaEditorMenuBar.java
EditorAboutFrame.java EditorPopupMenu.java SchemaEditorToolBar.java
EditorActions.java EditorRuler.java SchemaGraphComponent.java
EditorKeyboardHandler.java EditorToolBar.java ShadowBorder.java
于 2011-07-17T13:12:40.580 に答える
1

たぶんあなたが探しているのはGです。

于 2011-07-17T05:41:00.940 に答える