⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 abstractgridlayout.java

📁 OpenJGraph是一个开源的Java库
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
  {
    int       alternatex=0, alternatey=0, newx=0, newy=0;
    int       xstartAt, xinterval;
    int       ystartAt, yinterval;
    boolean   alternatefound = false;
    VisualVertex  preplaced;

    if( preferredx == anchorPoint.x ){
      xstartAt = adjacentPoint.x < anchorPoint.x ? anchorPoint.x - 1 : anchorPoint.x + 1;
      xinterval = adjacentPoint.x < anchorPoint.x ? 2 : -2;
      yinterval = adjacentPoint.y < anchorPoint.y ? -1 : 1;
      //System.out.println( "       Testing alternate points 1 to 4..." );
      for( alternatey = anchorPoint.y; !alternatefound && Math.abs( alternatey - anchorPoint.y )<=1; alternatey+=yinterval ) {
        for( alternatex = xstartAt; !alternatefound && Math.abs( alternatex - anchorPoint.x )==1; alternatex+=xinterval ) {
          if( alternatey>=0 && alternatey < grid.getHeight() &&
              alternatex>=0 && alternatex < grid.getWidth() ) {

            preplaced = grid.getGridPoint( alternatex, alternatey );
            //if( preplaced == null || ( preplaced != null && !finalplaced.contains( preplaced ) && !interimfinalplaced.contains( preplaced ))) {
            if( preplaced == null || ( preplaced != null && !finalplaced.contains( preplaced ) )) {
              alternatefound = true;
              newx = alternatex;
              newy = alternatey;
            }
          }

        }
      }

      if( !alternatefound ) {
        //System.out.println( "       Testing alternate points 5 to 7..." );
        alternatey = anchorPoint.y + (adjacentPoint.y < anchorPoint.y ? 1 : -1 );
        xinterval = adjacentPoint.x < anchorPoint.x ? -1 : 1;
        for( alternatex = anchorPoint.x; !alternatefound && Math.abs( alternatex - anchorPoint.x )<=1; alternatex+=xinterval ) {
          //System.out.println( "alternatex: " + alternatex );
          //System.out.println( "xinterval: " + xinterval );
          if( alternatex>=0 && alternatex < grid.getWidth() &&
              alternatey>=0 && alternatey < grid.getHeight()) {

            preplaced = grid.getGridPoint( alternatex, alternatey );
            //if( preplaced == null || ( preplaced != null && !finalplaced.contains( preplaced ) && !interimfinalplaced.contains( preplaced ))) {
            if( preplaced == null || ( preplaced != null && !finalplaced.contains( preplaced ) )) {
              alternatefound = true;
              newx = alternatex;
              newy = alternatey;
            }
          }

          xinterval =
            (adjacentPoint.x <= anchorPoint.x && alternatex == anchorPoint.x - 1 ? 2 :
            (adjacentPoint.x > anchorPoint.x && alternatex == anchorPoint.x + 1 ? -2 : xinterval ));
        }
      }
    }

    if( preferredy == anchorPoint.y ){
      ystartAt = adjacentPoint.y < anchorPoint.y ? anchorPoint.y - 1 : anchorPoint.y + 1;
      yinterval = adjacentPoint.y < anchorPoint.y ? 2 : -2;
      xinterval = adjacentPoint.x < anchorPoint.x ? -1 : 1;
      //System.out.println( "       Testing alternate points 1 to 4..." );
      for( alternatex = anchorPoint.x; !alternatefound && Math.abs( alternatex - anchorPoint.x )<=1; alternatex+=xinterval ) {
        for( alternatey = ystartAt; !alternatefound && Math.abs( alternatey - anchorPoint.y )==1; alternatey+=yinterval ) {
          if( alternatex>=0 && alternatex < grid.getWidth() &&
              alternatey>=0 && alternatey < grid.getHeight() ) {
            preplaced = grid.getGridPoint( alternatex, alternatey );
            //if( preplaced == null || ( preplaced != null && !finalplaced.contains( preplaced ) && !interimfinalplaced.contains( preplaced ))) {
            if( preplaced == null || ( preplaced != null && !finalplaced.contains( preplaced ) )) {
              alternatefound = true;
              newx = alternatex;
              newy = alternatey;
            }
          }
        }
      }

      if( !alternatefound ) {
        //System.out.println( "       Testing alternate points 5 to 7..." );
        alternatex = anchorPoint.x + (adjacentPoint.x < anchorPoint.x ? 1 : -1 );
        yinterval = adjacentPoint.y < anchorPoint.y ? -1 : 1;
        for( alternatey = anchorPoint.y; !alternatefound && Math.abs( alternatey - anchorPoint.y )<=1; alternatey+=yinterval ) {
          //System.out.println( "alternatey: " + alternatey );
          //System.out.println( "yinterval: " + yinterval );
          if( alternatex>=0 && alternatex < grid.getWidth() &&
              alternatey>=0 && alternatey < grid.getHeight() ) {
            preplaced = grid.getGridPoint( alternatex, alternatey );
            //if( preplaced == null || ( preplaced != null && !finalplaced.contains( preplaced ) && !interimfinalplaced.contains( preplaced ))) {
            if( preplaced == null || ( preplaced != null && !finalplaced.contains( preplaced ) )) {
              alternatefound = true;
              newx = alternatex;
              newy = alternatey;
            }
          }

          yinterval =
            (adjacentPoint.y <= anchorPoint.y && alternatey == anchorPoint.y - 1 ? 2 :
            (adjacentPoint.y > anchorPoint.y && alternatey == anchorPoint.y + 1 ? -2 : yinterval ));
        }
      }

    }

    if( alternatefound )
      return new Point( newx, newy );
    else
      return null;
  }

  private Point alternateMinimalEdgePlacement
  (
    VisualVertex anchorVertex, VisualVertex adjacentVertex,
    Point anchorPoint, Point adjacentPoint, List finalplaced,
    Grid grid, int preferredx, int preferredy, int xtestpoints[], int ytestpoints[] )
  {
    int xarraylength = Array.getLength( xtestpoints );
    int yarraylength = Array.getLength( ytestpoints );
    int arraylength = xarraylength > yarraylength ? yarraylength : xarraylength;
    int swapvalue;
    int multiple = 1, x, y, i;
    int       alternatex=0, alternatey=0, newx=0, newy=0;
    boolean   swap = false;
    boolean   alternatefound = false;
    VisualVertex  preplaced;

    if( preferredx == anchorPoint.x && preferredy > anchorPoint.y )
      multiple = 1;
    if( preferredx == anchorPoint.x && preferredy < anchorPoint.y )
      multiple = -1;
    if( preferredy == anchorPoint.y && preferredx > anchorPoint.x ) {
      multiple = 1;
      swap = true;
    }
    if( preferredy == anchorPoint.y && preferredx < anchorPoint.x ) {
      multiple = -1;
      swap = true;
    }

    for( x = 0; x < arraylength; x++ ){ xtestpoints[x]*=multiple; }
    for( y = 0; y < arraylength; y++ ){ ytestpoints[y]*=multiple; }

    if( swap ) {
      for( i = 0; i < arraylength; i++ ){
        swapvalue = xtestpoints[i];
        xtestpoints[i] = ytestpoints[i];
        ytestpoints[i] = swapvalue;
        }
    }

    for( i = 0; i < arraylength; i++ ){
      alternatex = preferredx + xtestpoints[i];
      alternatey = preferredy + ytestpoints[i];

      if( alternatex>=0 && alternatex < grid.getWidth() &&
        alternatey>=0 && alternatey < grid.getHeight() ) {

        preplaced = grid.getGridPoint( alternatex, alternatey );
        //if( preplaced == null || ( preplaced != null && !finalplaced.contains( preplaced ) && !interimfinalplaced.contains( preplaced ))) {
        if( preplaced == null || ( preplaced != null && !finalplaced.contains( preplaced ) )) {
          alternatefound = true;
          newx = alternatex;
          newy = alternatey;
        }
      }
    }

    if( alternatefound )
      return new Point( newx, newy );
    else
      return null;
  }


  public void run() {
    ThreadQueueObject   threadqueueobject = (ThreadQueueObject) this.threadqueue.remove();
    //System.out.println( "threadqueueobject is " + threadqueueobject );

    if( threadqueueobject != null ) {
      Grid  grid = threadqueueobject.getGrid();

      //System.out.println( " Now strarting work with vertex " + (VisualVertex) threadqueueobject.getConnectedset().get(0) );
      //this.minimalEdgePlacement( threadqueueobject.getConnectedset(), grid );
      //System.out.printlnpr( " Finsihed working with vertex " + (VisualVertex) threadqueueobject.getConnectedset().get(0) );
      //this.appendGrid( grid );
    }
  }

  private synchronized void appendGrid( Grid grid ) {
    this.grids.add( grid );
  }

}

/**
 * NumberofEdgesComparator implements the Comparator interface to compare
 * the number of edges between two VisualVertices.
 *
 * @author		Jesus M. Salvo Jr.
 */
class NumberofEdgesComparator implements Comparator, Serializable {
  /**
   * The Graph object containing the VisualVertices to be compared.
   */
  Graph   graph;

  /**
   * Creates a NumberofEdgesComparator object, specifying the graph containing
   * the VisualVertices to be compared.
   *
   * @param   graph   The Graph object containing the VisualVertices to be compared.
   */
  NumberofEdgesComparator( Graph graph ){
    this.graph = graph;
  }

  /**
   * Compares the number of incident edges between two VisualVertices.
   *
   * @param   o1  VisualVertex or Vertex object whose number of edges will be compared
   * to o2.
   * @param   o2  VisualVertex or Vertex object whose number of edges will be compared
   * to o1.
   *
   * @return  Returns -1 if the number of incident edges of o1 is less than o2.
   * Returns 1 if the number of incident edges of 02 is is less than 01.
   * If the number of incident edges are the same, returns 0.
   */
  public int compare( Object o1, Object o2 ) {
    Vertex    v1 = null, v2 = null;
    int       adjacentv1, adjacentv2;

    if( o1 instanceof Vertex ) v1 = (Vertex) o1;
    if( o1 instanceof VisualVertex ) v1 = ((VisualVertex) o1).getVertex();
    if( o2 instanceof Vertex ) v2 = (Vertex) o2;
    if( o2 instanceof VisualVertex ) v2 = ((VisualVertex) o2).getVertex();

    adjacentv1 = this.graph.getAdjacentVertices( v1 ).size();
    adjacentv2 = this.graph.getAdjacentVertices( v2 ).size();

    if( adjacentv1 > adjacentv2 ) return -1;
    if( adjacentv1 < adjacentv2 ) return 1;
    return 0;
  }

  /**
   * Indicates wether some other object is "equal to" this comapator.
   * This method simply calls the equals() method of the argument obj.
   *
   * @param   obj   The object reference with which to compare.
   * @return  True if the specified object is also a comparator.
   */
  public boolean equals( Object obj ) {
    return obj.equals( this );
  }
}

class FinalPlacedComparator implements Comparator, Serializable {
  List finalplaced;

  public FinalPlacedComparator( List finalplaced ) {
    this.finalplaced = finalplaced;
  }

  public int compare( Object obj1, Object obj2 ) {
    boolean obj1final = finalplaced.contains( obj1 );
    boolean obj2final = finalplaced.contains( obj2 );

    if( !obj1final && obj2final ) return 1;
    if( obj1final && !obj2final ) return -1;
    return 0;
  }

  public boolean equals( Object comparatorobject ) {
    return comparatorobject.equals( this );
  }
}

class ThreadQueueObject implements Serializable {
  List    connectedset;
  Grid    grid;

  public ThreadQueueObject ( List connectedset, Grid grid ) {
    this.connectedset = connectedset;
    this.grid = grid;
  }

  public List getConnectedset() {
    return this.connectedset;
  }

  public Grid getGrid() {
    return this.grid;
  }
}

class ThreadQueue implements Serializable {
  List  threadqueueobjects;

  public ThreadQueue(){
    threadqueueobjects = new ArrayList( 10 );
  }

  public synchronized void add( Object object ) {
    threadqueueobjects.add( object );
  }

  public synchronized Object remove() {
    if( !threadqueueobjects.isEmpty())
      return threadqueueobjects.remove( 0 );
    else
      return null;
  }

}


class VisualVertexPositionComparator implements Comparator, Serializable {
  Grid    grid;
  int     compareXY = 1;

  public VisualVertexPositionComparator( Grid grid ) {
    this.grid = grid;
  }

  public void setCompareXY( int setter ) {
    this.compareXY = setter;
  }

  public int compare( Object o1, Object o2 ) {
    VisualVertex v1 = (VisualVertex) o1;
    VisualVertex v2 = (VisualVertex) o2;

    if( compareXY >= 0 )
      return grid.findVisualVertex( v1 ).y - grid.findVisualVertex( v2 ).y;
    else
      return grid.findVisualVertex( v1 ).x - grid.findVisualVertex( v2 ).x;
  }

  public boolean equals( Object obj ) {
    return obj.equals( this );
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -