summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/jrummikub/JRummikub.java89
-rw-r--r--src/jrummikub/model/Stone.java8
-rw-r--r--src/jrummikub/view/IClickable.java26
-rw-r--r--src/jrummikub/view/IHandPanel.java2
-rw-r--r--src/jrummikub/view/IStoneCollectionPanel.java2
-rw-r--r--src/jrummikub/view/IStonePanel.java38
-rw-r--r--src/jrummikub/view/ITablePanel.java2
-rw-r--r--src/jrummikub/view/impl/AbstractStonePanel.java77
-rw-r--r--src/jrummikub/view/impl/HandPanel.java5
-rw-r--r--src/jrummikub/view/impl/StoneCollectionPanel.java27
-rw-r--r--src/jrummikub/view/impl/TablePanel.java14
11 files changed, 207 insertions, 83 deletions
diff --git a/src/jrummikub/JRummikub.java b/src/jrummikub/JRummikub.java
index 6b76af6..54566d5 100644
--- a/src/jrummikub/JRummikub.java
+++ b/src/jrummikub/JRummikub.java
@@ -13,6 +13,7 @@ import jrummikub.model.Stone;
import jrummikub.model.StoneColor;
import jrummikub.model.StoneSet;
import jrummikub.util.IListener;
+import jrummikub.util.IListener1;
import jrummikub.util.IListener2;
import jrummikub.view.IView;
@@ -70,82 +71,98 @@ public class JRummikub {
view.getPlayerPanel().getHandPanel().setStones(stones);
view.getPlayerPanel().getHandPanel().getClickEvent()
- .add(new IListener2<Position, Boolean>() {
+ .add(new IListener1<Position>() {
@Override
- public void fire(Position p, Boolean collect) {
- System.out.println("Board clicked at " + p
+ public void fire(Position p) {
+ System.out.println("Hand clicked at " + p);
+ }
+ });
+ view.getPlayerPanel().getHandPanel().getStoneClickEvent()
+ .add(new IListener2<Stone, Boolean>() {
+ @Override
+ public void fire(Stone s, Boolean collect) {
+ System.out.println("Hand clicked on " + s
+ (collect ? ", collect" : ""));
}
});
view.getPlayerPanel().getHandPanel().getRangeClickEvent()
- .add(new IListener2<Position, Boolean>() {
+ .add(new IListener2<Stone, Boolean>() {
@Override
- public void fire(Position p, Boolean collect) {
- System.out.println("Board range-clicked at " + p
+ public void fire(Stone s, Boolean collect) {
+ System.out.println("Hand range-clicked on " + s
+ (collect ? ", collect" : ""));
}
});
view.getPlayerPanel().getHandPanel().getSetClickEvent()
- .add(new IListener2<Position, Boolean>() {
+ .add(new IListener2<Stone, Boolean>() {
@Override
- public void fire(Position p, Boolean collect) {
- System.out.println("Board set-clicked at " + p
+ public void fire(Stone s, Boolean collect) {
+ System.out.println("Hand set-clicked at " + s
+ (collect ? ", collect" : ""));
}
});
- view.getTablePanel().getClickEvent().add(new IListener2<Position, Boolean>() {
- @Override
- public void fire(Position p, Boolean collect) {
- System.out.println("Table clicked at " + p
- + (collect ? ", collect" : ""));
+ view.getTablePanel().getClickEvent()
+ .add(new IListener1<Position>() {
+ @Override
+ public void fire(Position p) {
+ System.out.println("Table clicked at " + p);
+ }
+ });
+ view.getTablePanel().getStoneClickEvent()
+ .add(new IListener2<Stone, Boolean>() {
+ @Override
+ public void fire(Stone s, Boolean collect) {
+ System.out.println("Table clicked on " + s
+ + (collect ? ", collect" : ""));
- }
- });
+ }
+ });
view.getTablePanel().getRangeClickEvent()
- .add(new IListener2<Position, Boolean>() {
+ .add(new IListener2<Stone, Boolean>() {
@Override
- public void fire(Position p, Boolean collect) {
- System.out.println("Table range-clicked at " + p
+ public void fire(Stone s, Boolean collect) {
+ System.out.println("Table range-clicked on " + s
+ (collect ? ", collect" : ""));
}
});
- view.getTablePanel().getSetClickEvent().add(new IListener2<Position, Boolean>() {
- @Override
- public void fire(Position p, Boolean collect) {
- System.out.println("Table set-clicked at " + p
- + (collect ? ", collect" : ""));
+ view.getTablePanel().getSetClickEvent()
+ .add(new IListener2<Stone, Boolean>() {
+ @Override
+ public void fire(Stone s, Boolean collect) {
+ System.out.println("Table set-clicked at " + s
+ + (collect ? ", collect" : ""));
- }
- });
+ }
+ });
- view.getTablePanel().getStoneCollectionPanel().getClickEvent()
- .add(new IListener2<Position, Boolean>() {
+ view.getTablePanel().getStoneCollectionPanel().getStoneClickEvent()
+ .add(new IListener2<Stone, Boolean>() {
@Override
- public void fire(Position p, Boolean collect) {
- System.out.println("Collection clicked at " + p
+ public void fire(Stone s, Boolean collect) {
+ System.out.println("Collection clicked on " + s
+ (collect ? ", collect" : ""));
}
});
view.getTablePanel().getStoneCollectionPanel().getRangeClickEvent()
- .add(new IListener2<Position, Boolean>() {
+ .add(new IListener2<Stone, Boolean>() {
@Override
- public void fire(Position p, Boolean collect) {
- System.out.println("Collection range-clicked at " + p
+ public void fire(Stone s, Boolean collect) {
+ System.out.println("Collection range-clicked on " + s
+ (collect ? ", collect" : ""));
}
});
view.getTablePanel().getStoneCollectionPanel().getSetClickEvent()
- .add(new IListener2<Position, Boolean>() {
+ .add(new IListener2<Stone, Boolean>() {
@Override
- public void fire(Position p, Boolean collect) {
- System.out.println("Collection set-clicked at " + p
+ public void fire(Stone s, Boolean collect) {
+ System.out.println("Collection set-clicked at " + s
+ (collect ? ", collect" : ""));
}
diff --git a/src/jrummikub/model/Stone.java b/src/jrummikub/model/Stone.java
index da8052b..800425e 100644
--- a/src/jrummikub/model/Stone.java
+++ b/src/jrummikub/model/Stone.java
@@ -71,4 +71,12 @@ public class Stone implements Sizeable {
return 1;
}
+ @Override
+ public String toString() {
+ if (joker) {
+ return "Stone[joker,color=" + color + "]";
+ } else {
+ return "Stone[value=" + value + ",color=" + color + "]";
+ }
+ }
}
diff --git a/src/jrummikub/view/IClickable.java b/src/jrummikub/view/IClickable.java
index c827042..4f44b5e 100644
--- a/src/jrummikub/view/IClickable.java
+++ b/src/jrummikub/view/IClickable.java
@@ -1,38 +1,18 @@
package jrummikub.view;
import jrummikub.model.Position;
-import jrummikub.util.IEvent2;
+import jrummikub.util.IEvent1;
/**
* An interface for view elements that can emit click events
*/
public interface IClickable {
/**
- * the click event is emitted when the player clicks on the table/board/etc.
+ * the click event is emitted when the player clicks on the table/hand/etc.
*
* @return the event; the first parameter is the position of the click in grid
* coordinates, the second is true when the player wants to add stones
* to his selection instead of replacing them
*/
- public IEvent2<Position, Boolean> getClickEvent();
-
- /**
- * the range click event is emitted when the player clicks on the table/board/
- * etc. and wants to select a range instead of a single stone
- *
- * @return the event; the first parameter is the position of the click in grid
- * coordinates, the second is true when the player wants to add stones
- * to his selection instead of replacing them
- */
- public IEvent2<Position, Boolean> getRangeClickEvent();
-
- /**
- * the set click event is emitted when the player clicks on the table/board/
- * etc. and wants to select a whole set instead of a single stone
- *
- * @return the event; the first parameter is the position of the click in grid
- * coordinates, the second is true when the player wants to add stones
- * to his selection instead of replacing them
- */
- public IEvent2<Position, Boolean> getSetClickEvent();
+ public IEvent1<Position> getClickEvent();
}
diff --git a/src/jrummikub/view/IHandPanel.java b/src/jrummikub/view/IHandPanel.java
index 4573b93..23cd0ca 100644
--- a/src/jrummikub/view/IHandPanel.java
+++ b/src/jrummikub/view/IHandPanel.java
@@ -8,7 +8,7 @@ import jrummikub.model.Stone;
/**
* The view for a player's hand that displays his stones
*/
-public interface IHandPanel extends IClickable {
+public interface IHandPanel extends IStonePanel, IClickable {
/**
* Set the player's stones to display on the board
*
diff --git a/src/jrummikub/view/IStoneCollectionPanel.java b/src/jrummikub/view/IStoneCollectionPanel.java
index 4c84508..5fa234c 100644
--- a/src/jrummikub/view/IStoneCollectionPanel.java
+++ b/src/jrummikub/view/IStoneCollectionPanel.java
@@ -3,5 +3,5 @@ package jrummikub.view;
/**
* The view of the collection that shows the stones a player has selected
*/
-public interface IStoneCollectionPanel extends IClickable {
+public interface IStoneCollectionPanel extends IStonePanel {
}
diff --git a/src/jrummikub/view/IStonePanel.java b/src/jrummikub/view/IStonePanel.java
new file mode 100644
index 0000000..6720902
--- /dev/null
+++ b/src/jrummikub/view/IStonePanel.java
@@ -0,0 +1,38 @@
+package jrummikub.view;
+
+import jrummikub.model.Stone;
+import jrummikub.util.IEvent2;
+
+/**
+ * An interface for view elements that can emit click events for clicks on stones
+ */
+public interface IStonePanel {
+ /**
+ * the click event is emitted when the player clicks on the table/hand/etc.
+ *
+ * @return the event; the first parameter is the position of the click in grid
+ * coordinates, the second is true when the player wants to add stones
+ * to his selection instead of replacing them
+ */
+ public IEvent2<Stone, Boolean> getStoneClickEvent();
+
+ /**
+ * the range click event is emitted when the player clicks on the table/hand/
+ * etc. and wants to select a range instead of a single stone
+ *
+ * @return the event; the first parameter is the position of the click in grid
+ * coordinates, the second is true when the player wants to add stones
+ * to his selection instead of replacing them
+ */
+ public IEvent2<Stone, Boolean> getRangeClickEvent();
+
+ /**
+ * the set click event is emitted when the player clicks on the table/hand/
+ * etc. and wants to select a whole set instead of a single stone
+ *
+ * @return the event; the first parameter is the position of the click in grid
+ * coordinates, the second is true when the player wants to add stones
+ * to his selection instead of replacing them
+ */
+ public IEvent2<Stone, Boolean> getSetClickEvent();
+}
diff --git a/src/jrummikub/view/ITablePanel.java b/src/jrummikub/view/ITablePanel.java
index 48eb357..ec7f3ea 100644
--- a/src/jrummikub/view/ITablePanel.java
+++ b/src/jrummikub/view/ITablePanel.java
@@ -8,7 +8,7 @@ import jrummikub.model.StoneSet;
/**
* The view of the table, where the stone sets lie
*/
-public interface ITablePanel extends IClickable {
+public interface ITablePanel extends IStonePanel, IClickable {
/**
* Sets the player name on the left label
*
diff --git a/src/jrummikub/view/impl/AbstractStonePanel.java b/src/jrummikub/view/impl/AbstractStonePanel.java
index 8ab18d8..19e5891 100644
--- a/src/jrummikub/view/impl/AbstractStonePanel.java
+++ b/src/jrummikub/view/impl/AbstractStonePanel.java
@@ -3,23 +3,33 @@ package jrummikub.view.impl;
import java.awt.Insets;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
+import java.awt.geom.Rectangle2D;
+import java.util.Collections;
+import java.util.Map;
import javax.swing.JPanel;
import jrummikub.model.Position;
+import jrummikub.model.Stone;
+import jrummikub.util.Event1;
import jrummikub.util.Event2;
import jrummikub.view.IClickable;
+import jrummikub.view.IStonePanel;
/**
* Base class for panels that draw stones
*/
@SuppressWarnings("serial")
-abstract class AbstractStonePanel extends JPanel implements IClickable {
+abstract class AbstractStonePanel extends JPanel implements IStonePanel,
+ IClickable {
private StonePainter stonePainter;
- private Event2<Position, Boolean> clickEvent = new Event2<Position, Boolean>();
- private Event2<Position, Boolean> rangeClickEvent = new Event2<Position, Boolean>();
- private Event2<Position, Boolean> setClickEvent = new Event2<Position, Boolean>();
+ private Event1<Position> clickEvent = new Event1<Position>();
+ private Event2<Stone, Boolean> stoneClickEvent = new Event2<Stone, Boolean>();
+ private Event2<Stone, Boolean> rangeClickEvent = new Event2<Stone, Boolean>();
+ private Event2<Stone, Boolean> setClickEvent = new Event2<Stone, Boolean>();
+
+ private Map<Stone, Position> stones = Collections.emptyMap();
/**
* @return the stone painter
@@ -50,32 +60,77 @@ abstract class AbstractStonePanel extends JPanel implements IClickable {
@Override
public void mouseClicked(MouseEvent e) {
Insets insets = getInsets();
- Event2<Position, Boolean> event = clickEvent;
+ Position pos = stonePainter.calculatePosition(e.getX() - insets.left,
+ e.getY() - insets.top);
+ Stone stone = getStoneAt(pos);
+
+ if (stone == null) {
+ clickEvent.fire(pos);
+ return;
+ }
+
+ Event2<Stone, Boolean> event = stoneClickEvent;
if (e.isShiftDown())
event = rangeClickEvent;
else if (e.getClickCount() >= 2)
event = setClickEvent;
- event.fire(
- stonePainter.calculatePosition(e.getX() - insets.left, e.getY()
- - insets.top), e.isControlDown());
+ event.fire(stone, e.isControlDown());
}
});
}
+ private Stone getStoneAt(Position pos) {
+ for (Map.Entry<Stone, Position> entry : stones.entrySet()) {
+ Stone stone = entry.getKey();
+ Position p = entry.getValue();
+ Rectangle2D rect = new Rectangle2D.Float(p.getX(), p.getY(),
+ stone.getWidth(), stone.getHeight());
+
+ if (rect.contains(pos.getX(), pos.getY()))
+ return stone;
+ }
+
+ return null;
+ }
+
+ /**
+ * Sets the list of stones that can be clicked on
+ *
+ * @param stones
+ * the stones and positions
+ */
+ protected void setStones(Map<Stone, Position> stones) {
+ this.stones = stones;
+ }
+
+ /**
+ * Returns the list of stones and positions currently set
+ *
+ * @return the stones
+ */
+ protected Map<Stone, Position> getStones() {
+ return stones;
+ }
+
@Override
- public Event2<Position, Boolean> getClickEvent() {
+ public Event1<Position> getClickEvent() {
return clickEvent;
}
@Override
- public Event2<Position, Boolean> getRangeClickEvent() {
+ public Event2<Stone, Boolean> getStoneClickEvent() {
+ return stoneClickEvent;
+ }
+
+ @Override
+ public Event2<Stone, Boolean> getRangeClickEvent() {
return rangeClickEvent;
}
@Override
- public Event2<Position, Boolean> getSetClickEvent() {
+ public Event2<Stone, Boolean> getSetClickEvent() {
return setClickEvent;
}
diff --git a/src/jrummikub/view/impl/HandPanel.java b/src/jrummikub/view/impl/HandPanel.java
index 1610c00..0533181 100644
--- a/src/jrummikub/view/impl/HandPanel.java
+++ b/src/jrummikub/view/impl/HandPanel.java
@@ -39,7 +39,6 @@ class HandPanel extends AbstractStonePanel implements IHandPanel {
}
private BufferedImage scaledBackground = BACKGROUND;
- private Map<Stone, Position> stones = Collections.emptyMap();
private Collection<Stone> selectedStones = Collections.emptyList();
/**
@@ -97,7 +96,7 @@ class HandPanel extends AbstractStonePanel implements IHandPanel {
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
- for (Map.Entry<Stone, Position> entry : stones.entrySet()) {
+ for (Map.Entry<Stone, Position> entry : getStones().entrySet()) {
getStonePainter().paintStone(g, entry.getKey(), entry.getValue(),
selectedStones.contains(entry.getKey()));
}
@@ -105,7 +104,7 @@ class HandPanel extends AbstractStonePanel implements IHandPanel {
@Override
public void setStones(Map<Stone, Position> stones) {
- this.stones = stones;
+ super.setStones(stones);
repaint();
}
diff --git a/src/jrummikub/view/impl/StoneCollectionPanel.java b/src/jrummikub/view/impl/StoneCollectionPanel.java
index e4ca1c6..8729170 100644
--- a/src/jrummikub/view/impl/StoneCollectionPanel.java
+++ b/src/jrummikub/view/impl/StoneCollectionPanel.java
@@ -6,6 +6,8 @@ import java.awt.Graphics2D;
import java.awt.Insets;
import java.util.Collection;
import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
import javax.swing.border.EmptyBorder;
@@ -17,7 +19,8 @@ import jrummikub.view.IStoneCollectionPanel;
* Implementation of the stone collection (selection)
*/
@SuppressWarnings("serial")
-class StoneCollectionPanel extends AbstractStonePanel implements IStoneCollectionPanel {
+class StoneCollectionPanel extends AbstractStonePanel implements
+ IStoneCollectionPanel {
private final static int INSET = 7;
private final static float STONE_SCALE = 1.1f;
@@ -37,17 +40,27 @@ class StoneCollectionPanel extends AbstractStonePanel implements IStoneCollectio
/**
* Sets the stones to be shown in the collection
*
- * @param stones
+ * @param selectedStones
* the selected stones
*/
- void setSelectedStones(Collection<Stone> stones) {
- selectedStones = stones;
+ void setSelectedStones(Collection<Stone> selectedStones) {
+ this.selectedStones = selectedStones;
- if (stones.isEmpty()) {
+ Map<Stone, Position> stones = new HashMap<Stone, Position>();
+ float x = 0;
+
+ for (Stone stone : selectedStones) {
+ stones.put(stone, new Position(x, 0));
+ x += stone.getWidth();
+ }
+
+ setStones(stones);
+
+ if (selectedStones.isEmpty()) {
setVisible(false);
} else {
- setSize(getStonePainter().getStoneWidth() * stones.size() + 2 * INSET,
- getStonePainter().getStoneHeight() + 2 * INSET);
+ setSize(getStonePainter().getStoneWidth() * selectedStones.size() + 2
+ * INSET, getStonePainter().getStoneHeight() + 2 * INSET);
setVisible(true);
repaint();
diff --git a/src/jrummikub/view/impl/TablePanel.java b/src/jrummikub/view/impl/TablePanel.java
index b02a9a9..3eb33d8 100644
--- a/src/jrummikub/view/impl/TablePanel.java
+++ b/src/jrummikub/view/impl/TablePanel.java
@@ -10,6 +10,7 @@ import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.util.Collection;
import java.util.Collections;
+import java.util.HashMap;
import java.util.Map;
import javax.swing.ImageIcon;
@@ -55,7 +56,20 @@ class TablePanel extends AbstractStonePanel implements ITablePanel {
@Override
public void setStoneSets(Map<StoneSet, Position> stoneSets) {
+ Map<Stone, Position> stones = new HashMap<Stone, Position>();
+
+ for (Map.Entry<StoneSet, Position> entry : stoneSets.entrySet()) {
+ float x = entry.getValue().getX(), y = entry.getValue().getY();
+
+ for (Stone stone : entry.getKey()) {
+ stones.put(stone, new Position(x, y));
+ x += stone.getWidth();
+ }
+ }
+
+ setStones(stones);
this.stoneSets = stoneSets;
+
repaint();
}