summaryrefslogtreecommitdiffstats
path: root/src/jrummikub/view/impl/AbstractStonePanel.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/jrummikub/view/impl/AbstractStonePanel.java')
-rw-r--r--src/jrummikub/view/impl/AbstractStonePanel.java38
1 files changed, 37 insertions, 1 deletions
diff --git a/src/jrummikub/view/impl/AbstractStonePanel.java b/src/jrummikub/view/impl/AbstractStonePanel.java
index 7210d1c..fd3862b 100644
--- a/src/jrummikub/view/impl/AbstractStonePanel.java
+++ b/src/jrummikub/view/impl/AbstractStonePanel.java
@@ -3,6 +3,7 @@ package jrummikub.view.impl;
import java.awt.Insets;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
+import java.awt.event.MouseMotionAdapter;
import java.awt.geom.Rectangle2D;
import java.util.Collections;
@@ -30,6 +31,7 @@ abstract class AbstractStonePanel extends JPanel implements IStonePanel,
private Event2<Stone, Boolean> setClickEvent = new Event2<Stone, Boolean>();
private Iterable<Pair<Stone, Position>> stones = Collections.emptySet();
+ private Stone hoveredStone = null;
/**
* @return the stone painter
@@ -52,7 +54,7 @@ abstract class AbstractStonePanel extends JPanel implements IStonePanel,
* the grid scale
*/
public AbstractStonePanel(float scale) {
- super(false); // Unset double buffered
+ super(true); // Set double buffered
stonePainter = new StonePainter(scale);
@@ -81,9 +83,43 @@ abstract class AbstractStonePanel extends JPanel implements IStonePanel,
event.emit(stone, e.isControlDown());
}
+
+ @Override
+ public void mouseExited(MouseEvent e) {
+ setHoveredStone(null);
+ }
+ });
+
+ addMouseMotionListener(new MouseMotionAdapter() {
+ @Override
+ public void mouseMoved(MouseEvent e) {
+ Insets insets = getInsets();
+ Pair<Integer, Integer> trans = getTranslation();
+ Position pos = stonePainter.calculatePosition(e.getX() - insets.left
+ - trans.getFirst(), e.getY() - insets.top - trans.getSecond());
+
+ setHoveredStone(getStoneAt(pos));
+ }
});
}
+ private void setHoveredStone(Stone stone) {
+ Stone oldStone = hoveredStone;
+ hoveredStone = stone;
+
+ if (oldStone != hoveredStone)
+ repaint();
+ }
+
+ /**
+ * Returns the stone the mouse pointer is hovering over
+ *
+ * @return the hovered stone
+ */
+ protected Stone getHoveredStone() {
+ return hoveredStone;
+ }
+
/**
* *Overwrite this method* to signal if special zone was clicked
*