ALLES was muss und da ist ist kommentiert
git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@561 72836036-5685-4462-b002-a69064685172
This commit is contained in:
parent
b7da5ad631
commit
9861417ac1
2 changed files with 78 additions and 60 deletions
|
@ -11,7 +11,7 @@ import jrummikub.util.Pair;
|
||||||
* or {@link StoneSet}s.
|
* or {@link StoneSet}s.
|
||||||
*
|
*
|
||||||
* @param <E>
|
* @param <E>
|
||||||
* Type of positioned objects (must implement Sizeable)
|
* Type of positioned objects (must implement Sizeable)
|
||||||
*/
|
*/
|
||||||
public class StoneTray<E extends Sizeable> implements IStoneTray<E> {
|
public class StoneTray<E extends Sizeable> implements IStoneTray<E> {
|
||||||
private static final long serialVersionUID = -6329309928640027222L;
|
private static final long serialVersionUID = -6329309928640027222L;
|
||||||
|
@ -36,11 +36,11 @@ public class StoneTray<E extends Sizeable> implements IStoneTray<E> {
|
||||||
* dropped one collides with position-wise evade in
|
* dropped one collides with position-wise evade in
|
||||||
*
|
*
|
||||||
* @param object
|
* @param object
|
||||||
* the object to add to Hand
|
* the object to add to Hand
|
||||||
* @param position
|
* @param position
|
||||||
* {@link Position} to put the object
|
* {@link Position} to put the object
|
||||||
* @param direction
|
* @param direction
|
||||||
* the direction the other stones evade in
|
* the direction the other stones evade in
|
||||||
*/
|
*/
|
||||||
private void drop(E object, Position position, Direction direction) {
|
private void drop(E object, Position position, Direction direction) {
|
||||||
Pair<Position, Direction> update = fixInvalidDrop(object, position,
|
Pair<Position, Direction> update = fixInvalidDrop(object, position,
|
||||||
|
@ -57,11 +57,11 @@ public class StoneTray<E extends Sizeable> implements IStoneTray<E> {
|
||||||
* Subroutine to "drop" to execute the actual drop
|
* Subroutine to "drop" to execute the actual drop
|
||||||
*
|
*
|
||||||
* @param object
|
* @param object
|
||||||
* the object to add to Hand
|
* the object to add to Hand
|
||||||
* @param position
|
* @param position
|
||||||
* {@link Position} to put the object
|
* {@link Position} to put the object
|
||||||
* @param direction
|
* @param direction
|
||||||
* the direction the other stones evade in
|
* the direction the other stones evade in
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
private void dropUnchecked(E object, Position position, Direction direction) {
|
private void dropUnchecked(E object, Position position, Direction direction) {
|
||||||
|
@ -73,7 +73,8 @@ public class StoneTray<E extends Sizeable> implements IStoneTray<E> {
|
||||||
if (currentObject == object)
|
if (currentObject == object)
|
||||||
continue;
|
continue;
|
||||||
Position currentPosition = getPosition(currentObject);
|
Position currentPosition = getPosition(currentObject);
|
||||||
if (!objectsOverlap(object, position, currentObject, currentPosition)) {
|
if (!objectsOverlap(object, position, currentObject,
|
||||||
|
currentPosition)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
// Object would be placed inside the current object
|
// Object would be placed inside the current object
|
||||||
|
@ -82,39 +83,47 @@ public class StoneTray<E extends Sizeable> implements IStoneTray<E> {
|
||||||
}
|
}
|
||||||
Position newPosition = null;
|
Position newPosition = null;
|
||||||
// Move object to avoid overlap
|
// Move object to avoid overlap
|
||||||
switch (newDirection) {
|
newPosition = getNewPosition(object, position, newDirection,
|
||||||
case TOP:
|
currentObject, currentPosition);
|
||||||
newPosition = new Position(currentPosition.getX(), position.getY()
|
|
||||||
- currentObject.getHeight());
|
|
||||||
break;
|
|
||||||
case BOTTOM:
|
|
||||||
newPosition = new Position(currentPosition.getX(), position.getY()
|
|
||||||
+ object.getHeight());
|
|
||||||
break;
|
|
||||||
case LEFT:
|
|
||||||
newPosition = new Position(position.getX() - currentObject.getWidth(),
|
|
||||||
currentPosition.getY());
|
|
||||||
break;
|
|
||||||
case RIGHT:
|
|
||||||
newPosition = new Position(position.getX() + object.getWidth(),
|
|
||||||
currentPosition.getY());
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
objects.remove(currentObject);
|
objects.remove(currentObject);
|
||||||
drop(currentObject, newPosition, newDirection);
|
drop(currentObject, newPosition, newDirection);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Position getNewPosition(E object, Position position,
|
||||||
|
Direction newDirection, E currentObject, Position currentPosition) {
|
||||||
|
Position newPosition = new Position(0, 0);
|
||||||
|
switch (newDirection) {
|
||||||
|
case TOP:
|
||||||
|
newPosition = new Position(currentPosition.getX(), position.getY()
|
||||||
|
- currentObject.getHeight());
|
||||||
|
break;
|
||||||
|
case BOTTOM:
|
||||||
|
newPosition = new Position(currentPosition.getX(), position.getY()
|
||||||
|
+ object.getHeight());
|
||||||
|
break;
|
||||||
|
case LEFT:
|
||||||
|
newPosition = new Position(position.getX()
|
||||||
|
- currentObject.getWidth(), currentPosition.getY());
|
||||||
|
break;
|
||||||
|
case RIGHT:
|
||||||
|
newPosition = new Position(position.getX() + object.getWidth(),
|
||||||
|
currentPosition.getY());
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return newPosition;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks whether the object may be placed on the given position, computes new
|
* Checks whether the object may be placed on the given position, computes
|
||||||
* position if not
|
* new position if not
|
||||||
*
|
*
|
||||||
* @param object
|
* @param object
|
||||||
* to be dropped
|
* to be dropped
|
||||||
* @param dir
|
* @param dir
|
||||||
* @param pos
|
* @param pos
|
||||||
* the object is dropped at
|
* the object is dropped at
|
||||||
* @return null if the drop is valid, new position otherwise
|
* @return null if the drop is valid, new position otherwise
|
||||||
*/
|
*/
|
||||||
protected Pair<Position, Direction> fixInvalidDrop(E object, Position pos,
|
protected Pair<Position, Direction> fixInvalidDrop(E object, Position pos,
|
||||||
|
@ -123,13 +132,13 @@ public class StoneTray<E extends Sizeable> implements IStoneTray<E> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Static method for determining a less or equal relation considering a small
|
* Static method for determining a less or equal relation considering a
|
||||||
* fuzziness
|
* small fuzziness
|
||||||
*
|
*
|
||||||
* @param d
|
* @param d
|
||||||
* the value to be less or equal
|
* the value to be less or equal
|
||||||
* @param e
|
* @param e
|
||||||
* than the other one
|
* than the other one
|
||||||
* @return if d is less or equal e
|
* @return if d is less or equal e
|
||||||
*/
|
*/
|
||||||
private static boolean lessOrEqual(double d, double e) {
|
private static boolean lessOrEqual(double d, double e) {
|
||||||
|
@ -149,13 +158,13 @@ public class StoneTray<E extends Sizeable> implements IStoneTray<E> {
|
||||||
* Tests whether two objects overlap
|
* Tests whether two objects overlap
|
||||||
*
|
*
|
||||||
* @param object1
|
* @param object1
|
||||||
* first object
|
* first object
|
||||||
* @param position1
|
* @param position1
|
||||||
* first object's position
|
* first object's position
|
||||||
* @param object2
|
* @param object2
|
||||||
* second object
|
* second object
|
||||||
* @param position2
|
* @param position2
|
||||||
* second object's position
|
* second object's position
|
||||||
*
|
*
|
||||||
* @return whether they overlap
|
* @return whether they overlap
|
||||||
**/
|
**/
|
||||||
|
@ -165,13 +174,15 @@ public class StoneTray<E extends Sizeable> implements IStoneTray<E> {
|
||||||
if (lessOrEqual(position1.getX() + object1.getWidth(), position2.getX())) {
|
if (lessOrEqual(position1.getX() + object1.getWidth(), position2.getX())) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (lessOrEqual(position1.getY() + object1.getHeight(), position2.getY())) {
|
if (lessOrEqual(position1.getY() + object1.getHeight(),
|
||||||
|
position2.getY())) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (lessOrEqual(position2.getX() + object2.getWidth(), position1.getX())) {
|
if (lessOrEqual(position2.getX() + object2.getWidth(), position1.getX())) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (lessOrEqual(position2.getY() + object2.getHeight(), position1.getY())) {
|
if (lessOrEqual(position2.getY() + object2.getHeight(),
|
||||||
|
position1.getY())) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
@ -181,11 +192,11 @@ public class StoneTray<E extends Sizeable> implements IStoneTray<E> {
|
||||||
* Returns the direction to move the object in
|
* Returns the direction to move the object in
|
||||||
*
|
*
|
||||||
* @param object
|
* @param object
|
||||||
* the object
|
* the object
|
||||||
* @param position
|
* @param position
|
||||||
* the object's position
|
* the object's position
|
||||||
* @param blocking
|
* @param blocking
|
||||||
* the object thats blocking
|
* the object thats blocking
|
||||||
* @return the direction
|
* @return the direction
|
||||||
*/
|
*/
|
||||||
private Direction getMoveDirection(E object, Position position,
|
private Direction getMoveDirection(E object, Position position,
|
||||||
|
@ -216,11 +227,11 @@ public class StoneTray<E extends Sizeable> implements IStoneTray<E> {
|
||||||
* Will the object be moved horizontally or vertically
|
* Will the object be moved horizontally or vertically
|
||||||
*
|
*
|
||||||
* @param object
|
* @param object
|
||||||
* the object
|
* the object
|
||||||
* @param position
|
* @param position
|
||||||
* the objects position
|
* the objects position
|
||||||
* @param blocking
|
* @param blocking
|
||||||
* the object thats blocking
|
* the object thats blocking
|
||||||
*
|
*
|
||||||
* @return boolean vertical movement
|
* @return boolean vertical movement
|
||||||
*/
|
*/
|
||||||
|
@ -230,13 +241,15 @@ public class StoneTray<E extends Sizeable> implements IStoneTray<E> {
|
||||||
double blockingRight = blocking.getSecond().getX()
|
double blockingRight = blocking.getSecond().getX()
|
||||||
+ blocking.getFirst().getWidth();
|
+ blocking.getFirst().getWidth();
|
||||||
double overlapRight = Math.min(objectRight, blockingRight);
|
double overlapRight = Math.min(objectRight, blockingRight);
|
||||||
double overlapLeft = Math.max(position.getX(), blocking.getSecond().getX());
|
double overlapLeft = Math.max(position.getX(), blocking.getSecond()
|
||||||
|
.getX());
|
||||||
double overlapX = overlapRight - overlapLeft;
|
double overlapX = overlapRight - overlapLeft;
|
||||||
double objectBottom = position.getY() + object.getHeight();
|
double objectBottom = position.getY() + object.getHeight();
|
||||||
double blockingBottom = blocking.getSecond().getY()
|
double blockingBottom = blocking.getSecond().getY()
|
||||||
+ blocking.getFirst().getHeight();
|
+ blocking.getFirst().getHeight();
|
||||||
double overlapBottom = Math.min(objectBottom, blockingBottom);
|
double overlapBottom = Math.min(objectBottom, blockingBottom);
|
||||||
double overlapTop = Math.max(position.getY(), blocking.getSecond().getY());
|
double overlapTop = Math.max(position.getY(), blocking.getSecond()
|
||||||
|
.getY());
|
||||||
double overlapY = overlapBottom - overlapTop;
|
double overlapY = overlapBottom - overlapTop;
|
||||||
return overlapX > overlapY;
|
return overlapX > overlapY;
|
||||||
}
|
}
|
||||||
|
|
|
@ -136,18 +136,7 @@ class WinPanel extends JPanel {
|
||||||
fontSize = MAX_BUTTON_FONT_SIZE;
|
fontSize = MAX_BUTTON_FONT_SIZE;
|
||||||
|
|
||||||
if (type == BottomPanelType.WIN_PANEL) {
|
if (type == BottomPanelType.WIN_PANEL) {
|
||||||
waitingLabel.setVisible(false);
|
rescaleWinPanel(x, buttonWidth, buttonHeight, buttonY, fontSize);
|
||||||
newRoundButton.setBounds(x, buttonY, buttonWidth, buttonHeight);
|
|
||||||
newRoundButton.setFont(newRoundButton.getFont().deriveFont(fontSize));
|
|
||||||
newRoundButton.setVisible(true);
|
|
||||||
|
|
||||||
newGameButton.setBounds(x + buttonWidth + PANEL_SEPARATOR, buttonY,
|
|
||||||
buttonWidth, buttonHeight);
|
|
||||||
newGameButton.setFont(newGameButton.getFont().deriveFont(fontSize));
|
|
||||||
|
|
||||||
endProgramButton.setBounds(x + 2 * (buttonWidth + PANEL_SEPARATOR),
|
|
||||||
buttonY, buttonWidth, buttonHeight);
|
|
||||||
endProgramButton.setFont(endProgramButton.getFont().deriveFont(fontSize));
|
|
||||||
} else if (type == BottomPanelType.NETWORK_WIN_PANEL) {
|
} else if (type == BottomPanelType.NETWORK_WIN_PANEL) {
|
||||||
waitingLabel.setBounds(x, y, width, labelHeight);
|
waitingLabel.setBounds(x, y, width, labelHeight);
|
||||||
waitingLabel.setVisible(true);
|
waitingLabel.setVisible(true);
|
||||||
|
@ -163,6 +152,22 @@ class WinPanel extends JPanel {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void rescaleWinPanel(int x, int buttonWidth, int buttonHeight,
|
||||||
|
int buttonY, float fontSize) {
|
||||||
|
waitingLabel.setVisible(false);
|
||||||
|
newRoundButton.setBounds(x, buttonY, buttonWidth, buttonHeight);
|
||||||
|
newRoundButton.setFont(newRoundButton.getFont().deriveFont(fontSize));
|
||||||
|
newRoundButton.setVisible(true);
|
||||||
|
|
||||||
|
newGameButton.setBounds(x + buttonWidth + PANEL_SEPARATOR, buttonY,
|
||||||
|
buttonWidth, buttonHeight);
|
||||||
|
newGameButton.setFont(newGameButton.getFont().deriveFont(fontSize));
|
||||||
|
|
||||||
|
endProgramButton.setBounds(x + 2 * (buttonWidth + PANEL_SEPARATOR),
|
||||||
|
buttonY, buttonWidth, buttonHeight);
|
||||||
|
endProgramButton.setFont(endProgramButton.getFont().deriveFont(fontSize));
|
||||||
|
}
|
||||||
|
|
||||||
void setType(BottomPanelType type) {
|
void setType(BottomPanelType type) {
|
||||||
this.type = type;
|
this.type = type;
|
||||||
rescale();
|
rescale();
|
||||||
|
|
Reference in a new issue