From 36da8c8a179faab8e52c66130c4031027e06db6b Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Sat, 30 Apr 2011 17:56:10 +0200 Subject: Added clickEvent to Board git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@30 72836036-5685-4462-b002-a69064685172 --- src/jrummikub/view/IBoard.java | 3 ++ src/jrummikub/view/impl/Board.java | 46 +++++++++++++++++++++++++++++++ src/jrummikub/view/impl/StonePainter.java | 12 ++++++++ 3 files changed, 61 insertions(+) (limited to 'src/jrummikub/view') 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 stones); + + public IEvent1 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 stones = Collections.emptyMap(); private StonePainter stonePainter = new StonePainter(StonePainter.BOARD_SCALE); + + private Event1 clickEvent = new Event1(); 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 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; -- cgit v1.2.3