diff options
author | Matthias Schiffer <mschiffer@universe-factory.net> | 2011-04-30 17:56:10 +0200 |
---|---|---|
committer | Matthias Schiffer <mschiffer@universe-factory.net> | 2011-04-30 17:56:10 +0200 |
commit | 36da8c8a179faab8e52c66130c4031027e06db6b (patch) | |
tree | 60d8ec3e1f4633fda09d255b68ae9cb9a8b18634 /src/jrummikub/view | |
parent | 5440eaba77a77de9ff7ff502e2281b6ad6a5b92e (diff) | |
download | JRummikub-36da8c8a179faab8e52c66130c4031027e06db6b.tar JRummikub-36da8c8a179faab8e52c66130c4031027e06db6b.zip |
Added clickEvent to Board
git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@30 72836036-5685-4462-b002-a69064685172
Diffstat (limited to 'src/jrummikub/view')
-rw-r--r-- | src/jrummikub/view/IBoard.java | 3 | ||||
-rw-r--r-- | src/jrummikub/view/impl/Board.java | 46 | ||||
-rw-r--r-- | src/jrummikub/view/impl/StonePainter.java | 12 |
3 files changed, 61 insertions, 0 deletions
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; |