JGRAPH analysis

xiaoxiao2021-03-06  68

JGRAPH analysis johnny.deng

JGRAPH is an open source, compatible with SWING-based MVC architectural graphics components, with the following features:

1) Complete SWING compatibility;

2) Simple, efficient design;

3) Time efficiency;

4) 100% pure Java;

Generated illustration

Second, JGRAPH design

1) MVC

Swing is one of the UI standard implementations provided by Java (Sun), SWING is based on AWT (Abstract Windowing Toolkit). JGRAPH is fully compatible with Swing, which is still based on the MVC architecture.

JGRAPH MVC

View:

JGRAPH does not contain actual data, which provides data on the data; the mechanism of JGRAPH object is:

Defining the elements as a Cell, each Cell can be one of the vertices (EDGE) or Node (Port). The vertex can have an adjacent vertex, and they are connected by side, and the two end points of the side join are called targets and sources, each target or source is a node. The node is the child of the vertex. Each Cell can have its own child.

Each Cell's appearance is defined by the corresponding attribute, the attribute sequence refers to a series of key-value pairs, and they organize in Map, for example:

Map cellattrib = new hashtable ();

// set bounds

Rectangle2D Hellobounds = New Rectangle2d.double (20, 20, 40, 20);

GraphConstants.setBounds (CELLATTRIB, Hellobounds);

// set Black Border

Graphconstants.setBorderColor (CELLATTRIB, Color.Black);

A Cell has MAP like this to define its appearance.

The appearance can specify attributes such as arrow styles such as one side.

Model:

Data objects can be seen as link points of two independent structures in JGRAPH: Grahp Structure and Group Structure. The GRAPH structure is based on the vertices in the chart, side definitions. The GROUP structure is a CELL's Composition structure. GRAPH structure GetSource () and getTARGET () methods are obtained from the source and target nodes. In Group, getChild (), getParent () to get the constituting structure of Cells.

2) The low layer is based on the diagram logic, that is, a diagram G contains a non-empty element set V (g) and an E (G), where e (g) is two unordered elements in V (g). Tuple. V (g) is called a set of Figure G vertices, and if the vertices X / Y, (X, Y) in any set V (g) are in E (g), the edges (x, y) may be connected to the vertices x and The edge (arc) of Y, X and Y are called adjacent, otherwise X and Y are not adjacent. Third, JGRAPH Application The following is a JGRAPH-based HelloWorld analysis: Import omitted public class helloworld {

Public static void main (String [] args) {

// construct model and graph

//

Graphmodel model = new defaultgraphmodel ();

JGRAPH graph = new jgraph (model);

Graph.setSelectNewcells (TRUE);

// CREATE NESTED MAP (from cells to attributes)

// Record all properties in this map, the key - value pair is Cell-Cellattribute

// Each Cellattribute is another MAP, whose key-value is the specific Cell properties - value

Map attributes = new hashtable ();

// The following two vertices (CELL) Hello and World, and set their properties map, respectively

// Create Hello Vertex

//

DEFAULTGRAPHCELL Hello = New Defaultgraphcell ("Hello");

// Create Hello Vertex Attributes

//

Map helloattrib = new hashtable ();

Attributes.put (Hello, HelloAttrib);

// set bounds

Rectangle2D Hellobounds = New Rectangle2d.double (20, 20, 40, 20);

GraphConstants.setBounds (HelloAttrib, Hellobounds);

// set Black Border

GraphConstants.setBorderColor (HelloAttrib, Color.Black);

// Add a port

// Each vertex must be adjacent to other vertices, you must add nodes (CELL)

DEFAULTPORT HP = New DefaultPort ();

Hello.add (HP);

// CREATE World Vertex

//

DEFAULTGRAPHCELL World = New DefaultGraphcell ("world");

// Create World Vertex Attributes

//

Map worldttrib = new hashtable ();

Attributes.put (world, worldattrib);

// set bounds

Rectangle2D Worldbounds = New Rectangle2d.double (140, 140, 40, 20); GraphConstants.setBounds (WorldAttrib, Worldbounds);

// SET FILL Color

GraphConstants.setBackground (Worldattrib, Color.Orange);

GraphConstants.Setopaque (WorldAttrib, True);

// set raised border

GraphConstants.setBorder (WorldAttrib,

Borderfactory.createraisedbevelborder ());

// Add a port

//

DEFAULTPORT WP = New DefaultPort ();

World.Add (WP);

// Establish the side of the two vertices

// CREATE EDGE

//

DEFAULTEDGE EDGE = New DefaultE ();

// Create Edge Attributes

//

Map edgeattrib = new hashtable ();

Attributes.put (Edge, EdgeAttrib);

// set arrow

Int arrow = graphconstants.arrow_classic;

GraphConstants.SetLinend (EdgeAttrib, Arrow);

Graphconstants.sendfill (EdgeAttrib, True);

// Connect EDGE

The two endpoints of the side are the two vertices (port)

ConnectionSet CS = New ConnectionSet (EDGE, HP, WP);

Object [] cells = new object [] {edge, hello, world};

// Insert Into Model

// MODEL component is completed

Model.Insert (Cells, Attributes, CS, NULL, NULL);

// show in frame

//

JFrame frame = new jframe ();

Frame.getContentPane (). Add (new jscrollpane (graph));

//frame.setdefaultcloseoperation(JFrame.exit_on_close);

Frame.PACK ();

Frame.setVisible (TRUE);

}

}

转载请注明原文地址:https://www.9cbs.com/read-92789.html

New Post(0)