summaryrefslogtreecommitdiffstats
path: root/src/jrummikub/view/impl
diff options
context:
space:
mode:
Diffstat (limited to 'src/jrummikub/view/impl')
-rw-r--r--src/jrummikub/view/impl/Board.java46
-rw-r--r--src/jrummikub/view/impl/StonePainter.java12
2 files changed, 58 insertions, 0 deletions
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;