From adcf9fdfb673658b8c6677c23ed06aee3000104b Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Mon, 2 May 2011 00:14:09 +0200 Subject: Add StoneCollection to handle clicks on collected stones git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@55 72836036-5685-4462-b002-a69064685172 --- src/jrummikub/JRummikub.java | 28 ++++++++ src/jrummikub/view/IStoneCollection.java | 4 ++ src/jrummikub/view/ITable.java | 2 + src/jrummikub/view/impl/StoneCollection.java | 65 ++++++++++++++++++ src/jrummikub/view/impl/Table.java | 98 ++++++++++++++++------------ 5 files changed, 157 insertions(+), 40 deletions(-) create mode 100644 src/jrummikub/view/IStoneCollection.java create mode 100644 src/jrummikub/view/impl/StoneCollection.java diff --git a/src/jrummikub/JRummikub.java b/src/jrummikub/JRummikub.java index 9571446..cf0c73f 100644 --- a/src/jrummikub/JRummikub.java +++ b/src/jrummikub/JRummikub.java @@ -123,6 +123,34 @@ public class JRummikub { } }); + view.getTable().getStoneCollection().getClickEvent() + .add(new IListener2() { + @Override + public void fire(Position p, Boolean collect) { + System.out.println("Collection clicked at " + p + + (collect ? ", collect" : "")); + + } + }); + view.getTable().getStoneCollection().getRangeClickEvent() + .add(new IListener2() { + @Override + public void fire(Position p, Boolean collect) { + System.out.println("Collection range-clicked at " + p + + (collect ? ", collect" : "")); + + } + }); + view.getTable().getStoneCollection().getSetClickEvent() + .add(new IListener2() { + @Override + public void fire(Position p, Boolean collect) { + System.out.println("Collection set-clicked at " + p + + (collect ? ", collect" : "")); + + } + }); + // stoneSets on the table Map stoneSets = new HashMap(); diff --git a/src/jrummikub/view/IStoneCollection.java b/src/jrummikub/view/IStoneCollection.java new file mode 100644 index 0000000..2ef9c79 --- /dev/null +++ b/src/jrummikub/view/IStoneCollection.java @@ -0,0 +1,4 @@ +package jrummikub.view; + +public interface IStoneCollection extends IClickable { +} diff --git a/src/jrummikub/view/ITable.java b/src/jrummikub/view/ITable.java index d0b0fc6..b5412cb 100644 --- a/src/jrummikub/view/ITable.java +++ b/src/jrummikub/view/ITable.java @@ -13,4 +13,6 @@ public interface ITable extends IClickable { public void setRightPlayerName(String playerName); public void setStoneSets(Map stoneSets); + + IStoneCollection getStoneCollection(); } diff --git a/src/jrummikub/view/impl/StoneCollection.java b/src/jrummikub/view/impl/StoneCollection.java new file mode 100644 index 0000000..95813da --- /dev/null +++ b/src/jrummikub/view/impl/StoneCollection.java @@ -0,0 +1,65 @@ +package jrummikub.view.impl; + +import java.awt.Color; +import java.awt.Graphics; +import java.awt.Graphics2D; +import java.awt.Insets; +import java.util.Collection; +import java.util.Collections; + +import javax.swing.border.EmptyBorder; + +import jrummikub.model.Position; +import jrummikub.model.Stone; +import jrummikub.view.IStoneCollection; + +@SuppressWarnings("serial") +class StoneCollection extends StonePanel implements IStoneCollection { + private final static int INSET = 7; + private final static float STONE_SCALE = 1.1f; + + private Collection selectedStones = Collections.emptyList(); + + StoneCollection() { + super(STONE_SCALE); + + setOpaque(false); + setVisible(false); + setBorder(new EmptyBorder(INSET, INSET, INSET, INSET)); + } + + void setSelectedStones(Collection stones) { + selectedStones = stones; + + if (stones.isEmpty()) { + setVisible(false); + } else { + setSize(getStonePainter().getStoneWidth() * stones.size() + 2 * INSET, + getStonePainter().getStoneHeight() + 2 * INSET); + setVisible(true); + + repaint(); + } + } + + @Override + public void paintComponent(Graphics g1) { + Insets insets = getInsets(); + 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); + + if (!selectedStones.isEmpty()) { + g1.setColor(new Color(0, 0, 0, 0.25f)); + g1.fillRoundRect(0, 0, getWidth(), getHeight(), INSET, INSET); + + float xpos = 0; + + for (Stone stone : selectedStones) { + getStonePainter().paintStone(g, stone, new Position(xpos, 0), + false); + xpos++; + } + } + } +} diff --git a/src/jrummikub/view/impl/Table.java b/src/jrummikub/view/impl/Table.java index 1d61749..67bf124 100644 --- a/src/jrummikub/view/impl/Table.java +++ b/src/jrummikub/view/impl/Table.java @@ -1,33 +1,36 @@ package jrummikub.view.impl; -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.ComponentAdapter; +import java.awt.event.ComponentEvent; +import java.awt.event.ComponentListener; import java.util.Collection; import java.util.Collections; import java.util.Map; import javax.swing.ImageIcon; import javax.swing.JLabel; -import javax.swing.JPanel; import jrummikub.model.Position; import jrummikub.model.Stone; import jrummikub.model.StoneSet; +import jrummikub.view.IStoneCollection; import jrummikub.view.ITable; @SuppressWarnings("serial") 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 final int COLLECTION_GAP = 5; private JLabel leftPlayerLabel, topPlayerLabel, rightPlayerLabel; - private JPanel innerPanel; - - private StonePainter selectedStonePainter = new StonePainter(1.2f); + private StoneCollection stoneCollection; private Map stoneSets = Collections.emptyMap(); private Collection selectedStones = Collections.emptyList(); @@ -53,50 +56,87 @@ class Table extends StonePanel implements ITable { repaint(); } + @Override + public IStoneCollection getStoneCollection() { + return stoneCollection; + } + public void setSelectedStones(Collection stones) { selectedStones = stones; + stoneCollection.setSelectedStones(stones); repaint(); } + private void rescale() { + Insets insets = getInsets(); + int x = insets.left, y = insets.top, width = getWidth() - insets.left + - insets.right, height = getHeight() - insets.top - insets.bottom; + + leftPlayerLabel.setBounds(x, y, width, height); + topPlayerLabel.setBounds(x, y, width, height); + rightPlayerLabel.setBounds(x, y, width, height); + + stoneCollection.setLocation(x + width / 2 - stoneCollection.getWidth() / 2, + y + height - stoneCollection.getHeight() - COLLECTION_GAP); + } + Table() { super(DEFAULT_SCALE); - setLayout(new BorderLayout()); + setLayout(null); leftPlayerLabel = new JLabel(); leftPlayerLabel.setForeground(Color.WHITE); - add(leftPlayerLabel, BorderLayout.WEST); + leftPlayerLabel.setHorizontalAlignment(JLabel.LEFT); + leftPlayerLabel.setHorizontalTextPosition(JLabel.LEFT); + add(leftPlayerLabel); topPlayerLabel = new JLabel(); topPlayerLabel.setHorizontalAlignment(JLabel.CENTER); topPlayerLabel.setHorizontalTextPosition(JLabel.CENTER); + topPlayerLabel.setVerticalAlignment(JLabel.TOP); + topPlayerLabel.setVerticalTextPosition(JLabel.TOP); topPlayerLabel.setForeground(Color.WHITE); - add(topPlayerLabel, BorderLayout.NORTH); + add(topPlayerLabel); rightPlayerLabel = new JLabel(); rightPlayerLabel.setForeground(Color.WHITE); - add(rightPlayerLabel, BorderLayout.EAST); + rightPlayerLabel.setHorizontalAlignment(JLabel.RIGHT); + rightPlayerLabel.setHorizontalTextPosition(JLabel.RIGHT); + add(rightPlayerLabel); + + stoneCollection = new StoneCollection(); + add(stoneCollection); + + ComponentListener rescaleListener = new ComponentAdapter() { + @Override + public void componentResized(ComponentEvent e) { + rescale(); + } + }; - innerPanel = new JPanel(); - innerPanel.setOpaque(false); - add(innerPanel, BorderLayout.CENTER); + addComponentListener(rescaleListener); + stoneCollection.addComponentListener(rescaleListener); } public void paintStoneSet(Graphics2D g, StoneSet stoneSet, Position pos) { float x = pos.getX(); - int width = getStonePainter().getStoneWidth(), height = getStonePainter().getStoneHeight(); - + int width = getStonePainter().getStoneWidth(), height = getStonePainter() + .getStoneHeight(); + g.setColor(new Color(0, 0, 0, 0.25f)); - g.fillRect((int)(x*width)-width/4, (int)(pos.getY()*height), width/4, height); - + g.fillRect((int) (x * width) - width / 4, (int) (pos.getY() * height), + width / 4, height); + for (Stone stone : stoneSet) { getStonePainter().paintStone(g, stone, new Position(x, pos.getY()), selectedStones.contains(stone)); x++; } - + g.setColor(new Color(0, 0, 0, 0.25f)); - g.fillRect((int)(x*width), (int)(pos.getY()*height), width/4, height); + g.fillRect((int) (x * width), (int) (pos.getY() * height), width / 4, + height); } @Override @@ -115,27 +155,5 @@ class Table extends StonePanel implements ITable { for (Map.Entry stoneSet : stoneSets.entrySet()) { paintStoneSet(g, stoneSet.getKey(), stoneSet.getValue()); } - - int selectedStonesWidth = getWidth() * 3 / 5 - 14; - int selectedStonesHeight = selectedStonePainter.getStoneHeight(); - int selectedStonesX = getWidth() / 2 - selectedStonesWidth / 2; - int selectedStonesY = getHeight() - selectedStonesHeight - 12; - - if (!selectedStones.isEmpty()) { - g.setColor(new Color(0, 0, 0, 0.3f)); - g.fillRect(selectedStonesX - 7, selectedStonesY - 7, - selectedStonesWidth + 14, selectedStonesHeight + 14); - - Graphics2D translatedG = (Graphics2D) g.create(selectedStonesX, - selectedStonesY, selectedStonesWidth, selectedStonesHeight); - - float x = 0; - - for (Stone stone : selectedStones) { - selectedStonePainter.paintStone(translatedG, stone, new Position(x, 0), - false); - x++; - } - } } } -- cgit v1.2.3