Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
JGraphX Swing Component - Java Graph Visualization Library
This is a binary & source redistribution of the original, unmodified JGraphX library originating from:
"https://github.com/jgraph/jgraphx/archive/v3.4.1.3.zip".
The purpose of this redistribution is to make the library available to other Maven projects.
/**
* $Id: mxGraphGenerator.java,v 1.23 2012/11/21 13:59:52 mate Exp $
* Copyright (c) 2012, JGraph Ltd
*/
package com.mxgraph.analysis;
import java.util.ArrayList;
import com.mxgraph.costfunction.mxCostFunction;
import com.mxgraph.costfunction.mxDoubleValCostFunction;
import com.mxgraph.generatorfunction.mxGeneratorFunction;
import com.mxgraph.generatorfunction.mxGeneratorRandomFunction;
import com.mxgraph.model.mxGeometry;
import com.mxgraph.model.mxIGraphModel;
import com.mxgraph.view.mxCellState;
import com.mxgraph.view.mxGraph;
import com.mxgraph.view.mxGraphView;
/**
* @author Mate
*
*/
public class mxGraphGenerator
{
// cost function class that implements mxICostFunction
// private mxGeneratorFunction generatorFunction = new mxGeneratorRandomFunction(0,1,2);
// private mxGeneratorFunction generatorFunction = new mxGeneratorConstFunction(1.5);
// private mxGeneratorFunction generatorFunction = new mxGeneratorRandomIntFunction(0, 20);
private mxGeneratorFunction generatorFunction = null;
private mxCostFunction costFunction = null;
public mxGraphGenerator(mxGeneratorFunction generatorFunction, mxCostFunction costFunction)
{
if (generatorFunction != null)
{
this.generatorFunction = generatorFunction;
}
if (costFunction != null)
{
this.costFunction = costFunction;
}
else
{
this.costFunction = new mxDoubleValCostFunction();
}
};
/**
* @param aGraph
* @param numVertexes
* @return a null graph
*/
public void getNullGraph(mxAnalysisGraph aGraph, int numVertices)
{
if (numVertices < 0)
{
throw new IllegalArgumentException();
}
mxGraph graph = aGraph.getGraph();
Object parent = graph.getDefaultParent();
for (int i = 0; i < numVertices; i++)
{
graph.insertVertex(parent, null, new Integer(i).toString(), i * 50, 0, 25, 25);
}
};
/**
* @param aGraph
* @param numVertices number of vertices
* @return A complete graph that has numVertices number of vertices
*/
public void getCompleteGraph(mxAnalysisGraph aGraph, int numVertices)
{
if (numVertices < 0)
{
throw new IllegalArgumentException();
}
mxGraph graph = aGraph.getGraph();
Object parent = graph.getDefaultParent();
Object[] vertices = new Object[numVertices];
for (int i = 0; i < numVertices; i++)
{
vertices[i] = graph.insertVertex(parent, null, new Integer(i).toString(), i * 50, 0, 25, 25);
}
for (int i = 0; i < numVertices; i++)
{
Object vertex1 = vertices[i];
for (int j = 0; j < numVertices; j++)
{
Object vertex2 = vertices[j];
if (vertex1 != vertex2 && !mxGraphStructure.areConnected(aGraph, vertex1, vertex2))
{
graph.insertEdge(parent, null, getNewEdgeValue(aGraph), vertex1, vertex2);
}
}
}
};
/**
* @param aGraph
* @param numRows - number of rows in the grid graph
* @param numColumns - number of columns in the grid graph
* @return Returns a numColumns x numRows grid graph
*/
public void getGridGraph(mxAnalysisGraph aGraph, int numColumns, int numRows)
{
if (numColumns < 0 || numRows < 0)
{
throw new IllegalArgumentException();
}
mxGraph graph = aGraph.getGraph();
Object parent = graph.getDefaultParent();
int numVertices = numColumns * numRows;
Object[] vertices = new Object[numVertices];
for (int i = 0; i < numVertices; i++)
{
vertices[i] = graph.insertVertex(parent, null, new Integer(i).toString(), 0, 0, 25, 25);
}
int vertexCount = 0;
for (int j = 0; j < numRows; j++)
{
for (int i = 0; i < numColumns; i++)
{
Object currVertex = vertices[vertexCount];
if (i > 0)
{
// connect with previous x
graph.insertEdge(parent, null, getNewEdgeValue(aGraph), vertices[vertexCount - 1], currVertex);
}
if (j > 0)
{
//connect with previous y
graph.insertEdge(parent, null, getNewEdgeValue(aGraph), vertices[vertexCount - numColumns], currVertex);
}
vertexCount++;
}
}
};
/**
* Sets the physical spacing between vertices in a grid graph. This works for now only for a graph generated with mxGraphCreator.getGridGraph() only after creating the graph
* @param aGraph
* @param xSpacing - horizontal spacing between vertices
* @param ySpacing - vertical spacing between vertices
* @param numRows - number of rows in the grid graph
* @param numColumns - number of columns in the grid graph
*/
public void setGridGraphSpacing(mxAnalysisGraph aGraph, double xSpacing, double ySpacing, int numColumns, int numRows)
{
mxGraph graph = aGraph.getGraph();
if (xSpacing < 0 || ySpacing < 0 || numColumns < 1 || numRows < 1)
{
throw new IllegalArgumentException();
}
Object parent = graph.getDefaultParent();
Object[] vertices = aGraph.getChildVertices(parent);
mxIGraphModel model = graph.getModel();
for (int i = 0; i < numRows; i++)
{
for (int j = 0; j < numColumns; j++)
{
Object currVertex = vertices[i * numColumns + j];
mxGeometry geometry = model.getGeometry(currVertex);
geometry.setX(j * xSpacing);
geometry.setY(i * ySpacing);
}
}
};
/**
* @param aGraph
* @param numVerticesGroup1 number of vertices in group 1
* @param numVerticesGroup2 number of vertices in group 2
* @return a bipartite graph with group 1 containing numVerticesGroup1 vertices and group 2 containing numVerticesGroup2
*/
public void getBipartiteGraph(mxAnalysisGraph aGraph, int numVerticesGroup1, int numVerticesGroup2)
{
if (numVerticesGroup1 < 0 || numVerticesGroup2 < 0)
{
throw new IllegalArgumentException();
}
int numVertices = numVerticesGroup1 + numVerticesGroup2;
mxGraph graph = aGraph.getGraph();
Object parent = graph.getDefaultParent();
Object[] vertices = new Object[numVertices];
for (int i = 0; i < numVertices; i++)
{
vertices[i] = graph.insertVertex(parent, null, new Integer(i).toString(), 0, 0, 25, 25);
}
for (int i = 0; i < numVerticesGroup1; i++)
{
Object currVertex = vertices[i];
Object destVertex = vertices[getRandomInt(numVerticesGroup1, numVertices - 1)];
graph.insertEdge(parent, null, getNewEdgeValue(aGraph), currVertex, destVertex);
}
for (int j = 0; j < numVerticesGroup2; j++)
{
Object currVertex = vertices[numVerticesGroup1 + j];
int edgeNum = aGraph.getOpposites(aGraph.getEdges(currVertex, null, true, true, false, true), currVertex, true, true).length;
if (edgeNum == 0)
{
Object destVertex = vertices[getRandomInt(0, numVerticesGroup1 - 1)];
graph.insertEdge(parent, null, getNewEdgeValue(aGraph), currVertex, destVertex);
}
}
};
/**
* Sets the physical spacing between vertices in a bipartite graph. This works for now only for a graph generated with mxGraphCreator.getBipartiteGraph()
* only after creating the graph
* @param aGraph
* @param numVerticesGroup1 - number of vertices in group 1
* @param numVerticesGroup2 - number of vertices in group 2
* @param vertexSpacing - vertical spacing between vertices in the same group
* @param groupSpacing - spacing between groups
*/
public void setBipartiteGraphSpacing(mxAnalysisGraph aGraph, int numVerticesGroup1, int numVerticesGroup2, double vertexSpacing,
double groupSpacing)
{
if (numVerticesGroup1 < 0 || numVerticesGroup2 < 0)
{
throw new IllegalArgumentException();
}
mxGraph graph = aGraph.getGraph();
double group1StartY = 0;
double group2StartY = 0;
Object parent = graph.getDefaultParent();
mxIGraphModel model = graph.getModel();
if (numVerticesGroup1 < numVerticesGroup2)
{
double centerYtimes2 = (numVerticesGroup2 * vertexSpacing);
group1StartY = (centerYtimes2 - (numVerticesGroup1 * vertexSpacing)) / 2;
}
else
{
double centerYtimes2 = (numVerticesGroup1 * vertexSpacing);
group2StartY = (centerYtimes2 - (numVerticesGroup2 * vertexSpacing)) / 2;
}
Object[] vertices = aGraph.getChildVertices(parent);
// position vertexes for group 1
for (int i = 0; i < numVerticesGroup1; i++)
{
Object currVertex = vertices[i];
mxGeometry geometry = model.getGeometry(currVertex);
geometry.setX(0);
geometry.setY(group1StartY + i * vertexSpacing);
}
// position vertexes for group 2
for (int i = numVerticesGroup1; i < numVerticesGroup1 + numVerticesGroup2; i++)
{
Object currVertex = vertices[i];
mxGeometry geometry = model.getGeometry(currVertex);
geometry.setX(groupSpacing);
geometry.setY(group2StartY + (i - numVerticesGroup1) * vertexSpacing);
}
};
/**
* @param aGraph
* @param numVerticesGroup1 number of vertices in group 1
* @param numVerticesGroup2 number of vertices in group 2
* @return a bipartite graph with group 1 containing numVerticesGroup1 vertices and group 2 containing numVerticesGroup2
*/
public void getCompleteBipartiteGraph(mxAnalysisGraph aGraph, int numVerticesGroup1, int numVerticesGroup2)
{
if (numVerticesGroup1 < 0 || numVerticesGroup2 < 0)
{
throw new IllegalArgumentException();
}
int numVertices = numVerticesGroup1 + numVerticesGroup2;
mxGraph graph = aGraph.getGraph();
Object parent = graph.getDefaultParent();
Object[] vertices = new Object[numVertices];
for (int i = 0; i < numVertices; i++)
{
vertices[i] = graph.insertVertex(parent, null, new Integer(i).toString(), 0, 0, 25, 25);
}
for (int i = 0; i < numVerticesGroup1; i++)
{
for (int j = numVerticesGroup1; j < numVertices; j++)
{
Object currVertex = vertices[i];
Object destVertex = vertices[j];
graph.insertEdge(parent, null, getNewEdgeValue(aGraph), currVertex, destVertex);
}
}
};
/**
* @param aGraph
* @param xDim
* @param yDim
* @return a knight graph of size xDim x yDim
* Note that the minimum size is 3x3
*/
public void getKnightGraph(mxAnalysisGraph aGraph, int xDim, int yDim)
{
if (xDim < 3 || yDim < 3)
{
throw new IllegalArgumentException();
}
int numVertices = xDim * yDim;
mxGraph graph = aGraph.getGraph();
Object parent = graph.getDefaultParent();
Object[] vertices = new Object[numVertices];
for (int i = 0; i < numVertices; i++)
{
vertices[i] = graph.insertVertex(parent, null, new Integer(i).toString(), 0, 0, 25, 25);
}
//now we set up the starting conditions
int[] currCoords = new int[2];
//the main loop
for (int i = 0; i < (xDim * yDim); i++)
{
currCoords = getVertexGridCoords(xDim, yDim, i);
Object[] neighborMoves = getKnightMoveVertexes(aGraph, xDim, yDim, currCoords[0], currCoords[1]);
for (int j = 0; j < neighborMoves.length; j++)
{
// connect current with the possible move that has minimum number of its (possible moves)
if (!mxGraphStructure.areConnected(aGraph, vertices[i], neighborMoves[j]))
{
graph.insertEdge(parent, null, getNewEdgeValue(aGraph), vertices[i], neighborMoves[j]);
}
// that vertex becomes the current vertex and we repeat until no possible moves
}
}
};
/**
* @param aGraph
* @param xDim x dimension of chess-board, size starts from 1
* @param yDim y dimension of chess-board, size starts from 1
* @param xCoord x coordinate on the chess-board, coordinate starts from 1
* @param yCoord y coordinate on the chess-board, coordinate starts from 1
* @return a list of ALL vertexes which would be valid moves from the current position, regardless if they were visited or not
* Note that both dimensions and both coordinates must be positive
*/
public Object[] getKnightMoveVertexes(mxAnalysisGraph aGraph, int xDim, int yDim, int xCoord, int yCoord)
{
if (xCoord > xDim || yCoord > yDim || xDim < 1 || yDim < 1 || xCoord < 1 || yCoord < 1)
{
throw new IllegalArgumentException();
}
mxGraph graph = aGraph.getGraph();
Object[] vertices = aGraph.getChildVertices(graph.getDefaultParent());
//check all possible 8 locations
//location 1
int currX = xCoord + 1;
int currY = yCoord - 2;
ArrayList