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.java37
-rw-r--r--src/jrummikub/view/impl/StonePanel.java62
-rw-r--r--src/jrummikub/view/impl/Table.java35
3 files changed, 81 insertions, 53 deletions
diff --git a/src/jrummikub/view/impl/Board.java b/src/jrummikub/view/impl/Board.java
index 555819e..a566312 100644
--- a/src/jrummikub/view/impl/Board.java
+++ b/src/jrummikub/view/impl/Board.java
@@ -5,38 +5,30 @@ import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.RenderingHints;
-import java.awt.event.MouseAdapter;
-import java.awt.event.MouseEvent;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import javax.swing.ImageIcon;
-import javax.swing.JPanel;
import jrummikub.model.Position;
import jrummikub.model.Stone;
-import jrummikub.util.Event1;
-import jrummikub.util.IEvent1;
import jrummikub.view.IBoard;
@SuppressWarnings("serial")
-public class Board extends JPanel implements IBoard {
- private final static ImageIcon background = new ImageIcon(Board.class.getResource("/jrummikub/resource/wood.png"));
-
+public class Board extends StonePanel implements IBoard {
+ private final static ImageIcon BACKGROUND = new ImageIcon(Board.class.getResource("/jrummikub/resource/wood.png"));
+ private final static float DEFAULT_SCALE = StonePainter.BOARD_SCALE;
+
private Map<Stone, Position> stones = Collections.emptyMap();
private Collection<Stone> selectedStones = Collections.emptyList();
-
- private StonePainter stonePainter = new StonePainter(StonePainter.BOARD_SCALE);
-
- private Event1<Position> clickEvent = new Event1<Position>();
Board() {
- super(true); // set double buffered
+ super(DEFAULT_SCALE);
setBorder(new CustomBorder(Color.DARK_GRAY, 0, 1, 0, 1));
- addMouseListener(new MouseAdapter() {
+ /*addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
Insets insets = getInsets();
@@ -44,12 +36,7 @@ public class Board extends JPanel implements IBoard {
clickEvent.fire(stonePainter.calculatePosition(e.getX() - insets.left,
e.getY() - insets.top));
}
- });
- }
-
- @Override
- public IEvent1<Position> getClickEvent() {
- return clickEvent;
+ });*/
}
@Override
@@ -58,18 +45,18 @@ public class Board extends JPanel implements IBoard {
int x = insets.left, y = insets.top, width = getWidth()-insets.left-insets.right, height = getHeight()-insets.top-insets.bottom;
Graphics2D g = (Graphics2D)g1.create(x, y, width, height);
- for(int xpos = 0; xpos < width; xpos += background.getIconWidth()) {
- background.paintIcon(this, g, xpos, 0);
+ for(int xpos = 0; xpos < width; xpos += BACKGROUND.getIconWidth()) {
+ BACKGROUND.paintIcon(this, g, xpos, 0);
}
- for(int xpos = -32; xpos < width; xpos += background.getIconWidth()) {
- background.paintIcon(this, g, xpos, 75);
+ for(int xpos = -32; xpos < width; xpos += BACKGROUND.getIconWidth()) {
+ BACKGROUND.paintIcon(this, g, xpos, 75);
}
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
for (Map.Entry<Stone, Position> entry : stones.entrySet()) {
- stonePainter.paintStone(g, entry.getKey(), entry.getValue(),
+ getStonePainter().paintStone(g, entry.getKey(), entry.getValue(),
selectedStones.contains(entry.getKey()));
}
}
diff --git a/src/jrummikub/view/impl/StonePanel.java b/src/jrummikub/view/impl/StonePanel.java
new file mode 100644
index 0000000..9bcffae
--- /dev/null
+++ b/src/jrummikub/view/impl/StonePanel.java
@@ -0,0 +1,62 @@
+package jrummikub.view.impl;
+
+import java.awt.Insets;
+import java.awt.event.MouseAdapter;
+import java.awt.event.MouseEvent;
+
+import javax.swing.JPanel;
+
+import jrummikub.model.Position;
+import jrummikub.util.Event2;
+import jrummikub.view.IClickable;
+
+@SuppressWarnings("serial")
+abstract class StonePanel extends JPanel implements IClickable {
+ private StonePainter stonePainter;
+
+ private Event2<Position, Boolean> clickEvent = new Event2<Position, Boolean>();
+ private Event2<Position, Boolean> rangeClickEvent = new Event2<Position, Boolean>();
+ private Event2<Position, Boolean> setClickEvent = new Event2<Position, Boolean>();
+
+ protected StonePainter getStonePainter() {
+ return stonePainter;
+ }
+
+ public StonePanel(float scale) {
+ super(true); // Set double buffered
+
+ stonePainter = new StonePainter(scale);
+
+ addMouseListener(new MouseAdapter() {
+ @Override
+ public void mouseClicked(MouseEvent e) {
+ Insets insets = getInsets();
+ Event2<Position, Boolean> event = clickEvent;
+
+ if (e.isShiftDown())
+ event = rangeClickEvent;
+ else if (e.getClickCount() >= 2)
+ event = setClickEvent;
+
+ event.fire(stonePainter.calculatePosition(e.getX() - insets.left,
+ e.getY() - insets.top), e.isControlDown());
+ }
+ });
+ }
+
+ @Override
+ public Event2<Position, Boolean> getClickEvent() {
+ return clickEvent;
+ }
+
+ @Override
+ public Event2<Position, Boolean> getRangeClickEvent() {
+ return rangeClickEvent;
+ }
+
+ @Override
+ public Event2<Position, Boolean> getSetClickEvent() {
+ return setClickEvent;
+ }
+
+}
diff --git a/src/jrummikub/view/impl/Table.java b/src/jrummikub/view/impl/Table.java
index a42a2a9..4a5589b 100644
--- a/src/jrummikub/view/impl/Table.java
+++ b/src/jrummikub/view/impl/Table.java
@@ -4,10 +4,7 @@ import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
-import java.awt.Insets;
import java.awt.RenderingHints;
-import java.awt.event.MouseAdapter;
-import java.awt.event.MouseEvent;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
@@ -19,26 +16,22 @@ import javax.swing.JPanel;
import jrummikub.model.Position;
import jrummikub.model.Stone;
import jrummikub.model.StoneSet;
-import jrummikub.util.Event1;
-import jrummikub.util.IEvent1;
import jrummikub.view.ITable;
@SuppressWarnings("serial")
-public class Table extends JPanel implements ITable {
+public class Table extends StonePanel implements ITable {
private final static ImageIcon background = new ImageIcon(
Board.class.getResource("/jrummikub/resource/felt.png"));
-
+ private final static float DEFAULT_SCALE = 1;
+
private JLabel leftPlayerLabel, topPlayerLabel, rightPlayerLabel;
private JPanel innerPanel;
-
- private StonePainter stonePainter = new StonePainter(1);
+
private StonePainter selectedStonePainter = new StonePainter(1.2f);
private Map<StoneSet, Position> stoneSets = Collections.emptyMap();
private Collection<Stone> selectedStones = Collections.emptyList();
- private Event1<Position> clickEvent = new Event1<Position>();
-
@Override
public void setLeftPlayerName(String playerName) {
@@ -65,14 +58,10 @@ public class Table extends JPanel implements ITable {
selectedStones = stones;
repaint();
}
-
- @Override
- public IEvent1<Position> getClickEvent() {
- return clickEvent;
- }
Table() {
- super(true); // set double buffered
+ super(DEFAULT_SCALE);
+
setLayout(new BorderLayout());
leftPlayerLabel = new JLabel();
@@ -92,23 +81,13 @@ public class Table extends JPanel implements ITable {
innerPanel = new JPanel();
innerPanel.setOpaque(false);
add(innerPanel, BorderLayout.CENTER);
-
- addMouseListener(new MouseAdapter() {
- @Override
- public void mouseClicked(MouseEvent e) {
- Insets insets = getInsets();
-
- clickEvent.fire(stonePainter.calculatePosition(e.getX() - insets.left,
- e.getY() - insets.top));
- }
- });
}
public void paintStoneSet(Graphics2D g, StoneSet stoneSet, Position pos) {
float x = pos.getX();
for (Stone stone : stoneSet) {
- stonePainter.paintStone(g, stone, new Position(x, pos.getY()),
+ getStonePainter().paintStone(g, stone, new Position(x, pos.getY()),
selectedStones.contains(stone));
x++;
}