summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/jrummikub/JRummikub.java8
-rw-r--r--src/jrummikub/model/Position.java5
-rw-r--r--src/jrummikub/view/IBoard.java3
-rw-r--r--src/jrummikub/view/impl/Board.java46
-rw-r--r--src/jrummikub/view/impl/StonePainter.java12
5 files changed, 74 insertions, 0 deletions
diff --git a/src/jrummikub/JRummikub.java b/src/jrummikub/JRummikub.java
index 552ca8f..1990fda 100644
--- a/src/jrummikub/JRummikub.java
+++ b/src/jrummikub/JRummikub.java
@@ -9,6 +9,7 @@ import jrummikub.model.Position;
import jrummikub.model.Stone;
import jrummikub.model.StoneColor;
import jrummikub.util.IListener;
+import jrummikub.util.IListener1;
import jrummikub.view.IView;
public class JRummikub {
@@ -58,6 +59,13 @@ public class JRummikub {
stones.put(new Stone(0, StoneColor.BLACK, true), new Position(3.5f, 0));
view.getPlayerPanel().getBoard().setStones(stones);
+ view.getPlayerPanel().getBoard().getClickEvent().add(new IListener1<Position>(){
+
+ @Override
+ public void fire(Position value) {
+ System.out.println("Board clicked at "+value);
+
+ }});
}
diff --git a/src/jrummikub/model/Position.java b/src/jrummikub/model/Position.java
index 595015f..f7e53e6 100644
--- a/src/jrummikub/model/Position.java
+++ b/src/jrummikub/model/Position.java
@@ -21,5 +21,10 @@ public class Position {
public float getY() {
return y;
}
+
+ @Override
+ public String toString(){
+ return "Position[x="+x+",y="+y+"]";
+ }
}
diff --git a/src/jrummikub/view/IBoard.java b/src/jrummikub/view/IBoard.java
index f363285..237518f 100644
--- a/src/jrummikub/view/IBoard.java
+++ b/src/jrummikub/view/IBoard.java
@@ -4,7 +4,10 @@ import java.util.Map;
import jrummikub.model.Position;
import jrummikub.model.Stone;
+import jrummikub.util.IEvent1;
public interface IBoard {
public void setStones(Map<Stone, Position> stones);
+
+ public IEvent1<Position> getClickEvent();
}
diff --git a/src/jrummikub/view/impl/Board.java b/src/jrummikub/view/impl/Board.java
index 2e02f75..b227760 100644
--- a/src/jrummikub/view/impl/Board.java
+++ b/src/jrummikub/view/impl/Board.java
@@ -5,6 +5,8 @@ import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.RenderingHints;
+import java.awt.event.MouseEvent;
+import java.awt.event.MouseListener;
import java.util.Collections;
import java.util.Map;
@@ -13,6 +15,9 @@ import javax.swing.JPanel;
import jrummikub.model.Position;
import jrummikub.model.Stone;
+import jrummikub.util.Event1;
+import jrummikub.util.IEvent;
+import jrummikub.util.IEvent1;
import jrummikub.view.IBoard;
@SuppressWarnings("serial")
@@ -21,11 +26,52 @@ public class Board extends JPanel implements IBoard {
private Map<Stone, Position> stones = Collections.emptyMap();
private StonePainter stonePainter = new StonePainter(StonePainter.BOARD_SCALE);
+
+ private Event1<Position> clickEvent = new Event1<Position>();
Board() {
super(true);
setBorder(new CustomBorder(Color.DARK_GRAY, 0, 1, 0, 1));
+
+ addMouseListener(new MouseListener(){
+
+ @Override
+ public void mouseClicked(MouseEvent e) {
+ Insets insets = getInsets();
+
+ clickEvent.fire(stonePainter.calculatePosition(e.getX()-insets.left, e.getY()-insets.top));
+
+ }
+
+ @Override
+ public void mouseEntered(MouseEvent e) {
+ // TODO Auto-generated method stub
+
+ }
+
+ @Override
+ public void mouseExited(MouseEvent e) {
+ // TODO Auto-generated method stub
+
+ }
+
+ @Override
+ public void mousePressed(MouseEvent e) {
+ // TODO Auto-generated method stub
+
+ }
+
+ @Override
+ public void mouseReleased(MouseEvent e) {
+ // TODO Auto-generated method stub
+
+ }});
+ }
+
+ @Override
+ public IEvent1<Position> getClickEvent() {
+ return clickEvent;
}
@Override
diff --git a/src/jrummikub/view/impl/StonePainter.java b/src/jrummikub/view/impl/StonePainter.java
index 9e836e5..6fa5f38 100644
--- a/src/jrummikub/view/impl/StonePainter.java
+++ b/src/jrummikub/view/impl/StonePainter.java
@@ -56,6 +56,18 @@ class StonePainter {
public float getScale() {
return scale;
}
+
+ /**
+ * @param x x position in screen coordinates
+ * @param y y position in screen coordinates
+ * @return position in grid coordinates
+ */
+ public Position calculatePosition(int x, int y){
+ float width = even(DEFAULT_WIDTH*scale);
+ float height = (int)(DEFAULT_WIDTH*scale/ASPECT_RATIO);
+
+ return new Position(x/width, y/height);
+ }
StonePainter(float scale) {
this.scale = scale;