src/tokenclustering/model/ClusteringAreaData.java
/**
     * Puts a token in this area.
     * @param token The token to put.
     * @return The token put or null if it was not possible.
     */
Puts a token in this area.
@param token The token to put.
@return The token put or null if it was not possible.
Puts a token in this area . @param token The token to put . @return The token put or null if it was not possible .
public Token putToken(Token token) {
        if (this.width < token.getWidth() || this.height < token.getHeight()) {
            return null;
        }
        if (this.getTokens().contains(token)) {
            return null;
        }
        int xPossibilities = this.width - token.getWidth() + 1;
        int yPossibilities = this.height - token.getHeight() + 1;
        ArrayList<Slot> slotsTried = new ArrayList<Slot>();
        for (int j = 0; j < yPossibilities; j++) {
            for (int i = 0; i < xPossibilities; i++) {
                boolean thisTry = true;
                for (int k = 0; k < token.getWidth(); k++) {
                    for (int l = 0; l < token.getHeight(); l++) {
                        Slot slotTried = new Slot(i + k, j + l);
                        if (this.freeSlots.contains(slotTried)) {
                            slotsTried.add(slotTried);
                        } else {
                            thisTry = false;
                        }
                    }
                }
                if (thisTry) {
                    token.setOccupiedSlots((ArrayList<Slot>) slotsTried.clone());
                    int minX = Integer.MAX_VALUE;
                    int minY = Integer.MAX_VALUE;
                    for (Slot slot : slotsTried) {
                        if (slot.getX() < minX) {
                            minX = slot.getX();
                        }
                        if (slot.getY() < minY) {
                            minY = slot.getY();
                        }
                    }
                    token.setUpperLeftSlot(new Slot(minX, minY));
                    this.freeSlots.removeAll(slotsTried);
                    this.occupiedSlots.addAll(slotsTried);
                    this.getTokens().add(token);
                    return token;
                } else {
                    slotsTried.clear();
                }
            }
        }
        return null;
    }
public Token putToken ( Token token ) { if ( this . width < token . getWidth ( ) || this . height < token . getHeight ( ) ) { return null ; } if ( this . getTokens ( ) . contains ( token ) ) { return null ; } int xPossibilities = this . width - token . getWidth ( ) + 1 ; int yPossibilities = this . height - token . getHeight ( ) + 1 ; ArrayList < Slot > slotsTried = new ArrayList < Slot > ( ) ; for ( int j = 0 ; j < yPossibilities ; j ++ ) { for ( int i = 0 ; i < xPossibilities ; i ++ ) { boolean thisTry = true ; for ( int k = 0 ; k < token . getWidth ( ) ; k ++ ) { for ( int l = 0 ; l < token . getHeight ( ) ; l ++ ) { Slot slotTried = new Slot ( i + k , j + l ) ; if ( this . freeSlots . contains ( slotTried ) ) { slotsTried . add ( slotTried ) ; } else { thisTry = false ; } } } if ( thisTry ) { token . setOccupiedSlots ( ( ArrayList < Slot > ) slotsTried . clone ( ) ) ; int minX = Integer . MAX_VALUE ; int minY = Integer . MAX_VALUE ; for ( Slot slot : slotsTried ) { if ( slot . getX ( ) < minX ) { minX = slot . getX ( ) ; } if ( slot . getY ( ) < minY ) { minY = slot . getY ( ) ; } } token . setUpperLeftSlot ( new Slot ( minX , minY ) ) ; this . freeSlots . removeAll ( slotsTried ) ; this . occupiedSlots . addAll ( slotsTried ) ; this . getTokens ( ) . add ( token ) ; return token ; } else { slotsTried . clear ( ) ; } } } return null ; }
Puts a token in this area.
Puts a token in this area .