📄 gemlayoutalgorithm.java
字号:
int childCount = getRelatives(view).size();
double massIndex = (double)(childCount + 1) / 2.0;
view.getAttributes().put(KEY_MASSINDEX,new Double(massIndex));
return massIndex;
}
/******************************************************************************/
/**
* Applies the changes to the Cells. This means, that all temporary stored
* positions are applied to all cells in {@link #applyCellList}
*/
private boolean setNewCoordinates(JGraph jgraph){
Map viewMap = new Hashtable();
// dlgProgress.setMessage(phaseName[PHASE_END]);
for( int i = 0; i < cellList.size(); i++ ){
Point2D.Double pos = getPosition(i,cellList);
Rectangle r = ((CellView)cellList.get(i)).getBounds();
r.x = (int) (pos.getX() - ((double)r.width /2.0));
r.y = (int) (pos.getY() - ((double)r.height/2.0));
Object cell = ((CellView) cellList.get(i)).getCell();
Map attributes = GraphConstants.createMap();
GraphConstants.setBounds(attributes, r);
viewMap.put(cell, attributes);
// if( updateProgressDialog(PHASE_END,i,applyCellList.size()) )
// return true;
}
jgraph.getGraphLayoutCache().edit(viewMap,null,null,null);
return false;
}
/******************************************************************************/
/**
* Clears the temporary data from the cells in {@link #cellList} (all cells).
*/
private void removeTemporaryLayoutDataFromCells(){
for( int i = 0; i < cellList.size(); i++ )
((CellView)cellList.get(i)).getAttributes().clear();
}
/******************************************************************************/
/**
* Checks, if the algorithm could break it's calculation earlier, than
* performing until {@link #countRounds} is {@link #maxRounds}. This depends on
* {@link #shouldEndPerAverage}.
*
* @param <b><code>true</code></b> either when the average the temperature of
* all cells is below {@link #minTemperature} or the temperature itself of all
* cells is below.
*/
private boolean isFrozen(){
double sumOfTemp = 0.0; //sum of temperatures to get the average value
double globalTemp = 0.0; //average value of all temperatures
boolean isFrozen = true;//true while all temperatures <= minTemperature
for( int i = 0; i < applyCellList.size(); i++ ){
double temperature = getTemperature(i,applyCellList);
sumOfTemp += temperature;
isFrozen = isFrozen && (temperature <= minTemperature);
if( !isFrozen && !shouldEndPerAverage )//speeds up a little
break;
}
if( shouldEndPerAverage ){
globalTemp = sumOfTemp / (double)applyCellList.size();
return globalTemp < minTemperature;
}
else return isFrozen;
}
/******************************************************************************/
/**
* Erzeugt eine Permutation der Zahlen von 0 bis length
*
* @param length Count and highest value of the generated sequence.
* @return sequence of numbers, contains every number a single time. The
* sequence consists of numbers between 0 and length.
*/
private int[] createPermutation(int length){
int[] permutation = new int[length];
for( int i = 0; i < permutation.length; i++ ){
int newValue = (int)(Math.random()*(double)length);
for( int j = 0; j < i; j++ )
if( newValue == permutation[j] ){
newValue = (int)(Math.random()*(double)length);
j = -1; // wird auf 0 zur點kgesetzt
}
permutation[i] = newValue;
}
return permutation;
}
/******************************************************************************/
/**
* Creates a random Vector, with a given length and a random direction.
*
* @param length Length of the Vector created by this method
* @return Vector represented by a Point2D.Double
*/
private Point2D.Double getRandomVector(double length){
double alpha = Math.random()*Math.PI*2;
// double length = Math.random()*maxLength;
return new Point2D.Double(length*Math.cos(alpha),
length*Math.sin(alpha));
}
/******************************************************************************/
/**
* Calculates the barycenter of a graph, given by a list. This calculation is
* done by summing the coordinates and dividing them with the number of
* coordinates.
*
* @param list List of CellView's
* @return Position of the barycenter
*/
private Point2D.Double computeBarycenter(ArrayList list){
double sumX = 0.0;
double sumY = 0.0;
for( int i = 0; i < list.size(); i++ ){
CellView view = (CellView) list.get(i);
initPosition(view);
Point2D.Double pos = getPosition(view);
sumX += pos.x;
sumY += pos.y;
}
return new Point2D.Double(sumX/((double)list.size()),
sumY/((double)list.size()));
}
/******************************************************************************/
/**
* Initialilzes the position of a CellView to the center point of the bounds
* of the cell. This initialization is only be done, when the cell isn't
* initialised before.
*
* @param view Cell, the position should be initialized.
*/
private void initPosition(CellView view){
if( !view.getAttributes().containsKey(KEY_POSITION) )
view.getAttributes().put(KEY_POSITION,new Point2D.Double(
view.getBounds().getCenterX(),
view.getBounds().getCenterY()));
}
/******************************************************************************/
/**
* Moves the graph to the upper left corner of the drawing space. This is done,
* after a successfull run of the algorithm, to correct it's output.
*/
private void correctCoordinates(){
Rectangle boundingBox = getBoundingBox();
if( boundingBox != null ){
for( int i = 0; i < cellList.size(); i++ ){
CellView view = (CellView) cellList.get(i);
Point2D.Double pos = getPosition(view);
Point2D.Double newPos = new Point2D.Double(
pos.x-boundingBox.getX(),
pos.y-boundingBox.getY());
view.getAttributes().put(KEY_POSITION,newPos);
}
}
}
/******************************************************************************/
/**
* Computes the bounding box of the whole graph. The result is a Rectangle,
* parallel to the X- and Y-axises of the drawing system, closing about the
* whole graph.
* @return Rectangle, that contains the whole graph.
* @see #getBoundingBox(ArrayList)
*/
private Rectangle getBoundingBox(){
return getBoundingBox(cellList);
}
/******************************************************************************/
/**
* Computes the bounding box of the graph in the given list of CellViews.
* The result is a Rectangle, parallel to the X- and Y-axises of the drawing
* system, closing about the graph in the given list.
*
* @param verticeList List containing the CellViews, the bounding box is of
* interest.
* @return Rectangle, that contains the whole graph, linked in the given list.
*/
private Rectangle getBoundingBox(ArrayList verticeList){
if( verticeList.size() > 0 ){
Point2D.Double vertexPos = getPosition(0,verticeList);
Dimension vertexSize = ((CellView)verticeList.get(0)).getBounds().getSize();
double minX = vertexPos.getX();
double minY = vertexPos.getX();
double maxX = vertexPos.getX()+vertexSize.getWidth();
double maxY = vertexPos.getX()+vertexSize.getHeight();
for( int i = 1; i < verticeList.size(); i++ ){
vertexPos = getPosition(i,verticeList);
vertexSize =((CellView)verticeList.get(i)).getBounds().getSize();
if( minX > vertexPos.getX() )
minX = vertexPos.getX();
if( minY > vertexPos.getY() )
minY = vertexPos.getY();
if( maxX < vertexPos.getX()+vertexSize.getWidth() )
maxX = vertexPos.getX()+vertexSize.getWidth();
if( maxY < vertexPos.getY()+vertexSize.getHeight() )
maxY = vertexPos.getY()+vertexSize.getHeight();
}
Rectangle boundingBox = new Rectangle((int)minX,
(int)minY,
(int)(maxX-minX),
(int)(maxY-minY));
return boundingBox;
}
return null;
}
/******************************************************************************/
/**
* Returns the Position of a Cell contained in {@link #applyCellList}.
*
* @param index Identifies the cell. This is the index of the cell in
* the given list of CellViews
* @param list List containing only CellViews
* @see #getAttribute(int,String,ArrayList)
*/
private Point2D.Double getPosition(int index, ArrayList list){
return (Point2D.Double) getAttribute(index,KEY_POSITION,list);
}
/******************************************************************************/
/**
* Returns the temperature of a cell contained in a given list.
*
* @param index Identifies the cell. This is the index of the cell in
* a given list of CellViews
* @param list List containing only CellViews
* @see #getAttribute(int,String,ArrayList)
*/
private double getTemperature(int index, ArrayList list){
Double temperature = (Double) getAttribute(index,KEY_TEMPERATURE,list);
return temperature.doubleValue();
}
/******************************************************************************/
/**
* Returns the Position of a Cell.
*
* @param cell The cell, that holds the position of interest.
*/
private Point2D.Double getPosition(CellView cell){
return (Point2D.Double) cell.getAttributes().get(KEY_POSITION);
}
/******************************************************************************/
/**
* Returns an attribute from a cell contained in a given list of CellViews.
*
* @param index Identifies the cell. This is the index of the cell in
* the given list of CellViews
* @param key Identifies the Attribute, that should be retrieved.
* @param list List containing only CellViews
*/
private Object getAttribute(int index, String key, ArrayList list){
CellView view = (CellView) list.get(index);
return view.getAttributes().get(key);
}
/******************************************************************************/
/**
* Arranges the initial cositions for the inserted cells. This is done, by
* placing the inserted cells in the barycenter of their relatives.
* (possible with errors ... might be fixed soon)
*/
private void arrangePlacement(CellView[] views){
for( int i = 0; i < cellList.size(); i++ )
initPosition((CellView)cellList.get(i));
if( views != null ){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -