From 3e257e3979f1f58d8b5ceb7a2d4472201bb94c34 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Tue, 3 May 2011 16:03:09 +0200 Subject: Renamed everything in the view git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@75 72836036-5685-4462-b002-a69064685172 --- src/jrummikub/JRummikub.java | 28 ++-- src/jrummikub/view/IBoard.java | 19 --- src/jrummikub/view/IHandPanel.java | 19 +++ src/jrummikub/view/IPlayerPanel.java | 2 +- src/jrummikub/view/IStoneCollection.java | 7 - src/jrummikub/view/IStoneCollectionPanel.java | 7 + src/jrummikub/view/ITable.java | 51 ------- src/jrummikub/view/ITablePanel.java | 51 +++++++ src/jrummikub/view/IView.java | 2 +- src/jrummikub/view/impl/AbstractStonePanel.java | 82 +++++++++++ src/jrummikub/view/impl/Board.java | 122 --------------- src/jrummikub/view/impl/HandPanel.java | 122 +++++++++++++++ src/jrummikub/view/impl/PlayerPanel.java | 16 +- src/jrummikub/view/impl/StoneCollection.java | 76 ---------- src/jrummikub/view/impl/StoneCollectionPanel.java | 76 ++++++++++ src/jrummikub/view/impl/StonePainter.java | 4 +- src/jrummikub/view/impl/StonePanel.java | 82 ----------- src/jrummikub/view/impl/Table.java | 171 ---------------------- src/jrummikub/view/impl/TablePanel.java | 171 ++++++++++++++++++++++ src/jrummikub/view/impl/View.java | 10 +- 20 files changed, 559 insertions(+), 559 deletions(-) delete mode 100644 src/jrummikub/view/IBoard.java create mode 100644 src/jrummikub/view/IHandPanel.java delete mode 100644 src/jrummikub/view/IStoneCollection.java create mode 100644 src/jrummikub/view/IStoneCollectionPanel.java delete mode 100644 src/jrummikub/view/ITable.java create mode 100644 src/jrummikub/view/ITablePanel.java create mode 100644 src/jrummikub/view/impl/AbstractStonePanel.java delete mode 100644 src/jrummikub/view/impl/Board.java create mode 100644 src/jrummikub/view/impl/HandPanel.java delete mode 100644 src/jrummikub/view/impl/StoneCollection.java create mode 100644 src/jrummikub/view/impl/StoneCollectionPanel.java delete mode 100644 src/jrummikub/view/impl/StonePanel.java delete mode 100644 src/jrummikub/view/impl/Table.java create mode 100644 src/jrummikub/view/impl/TablePanel.java (limited to 'src') diff --git a/src/jrummikub/JRummikub.java b/src/jrummikub/JRummikub.java index 7d3ee28..6b76af6 100644 --- a/src/jrummikub/JRummikub.java +++ b/src/jrummikub/JRummikub.java @@ -33,9 +33,9 @@ public class JRummikub { view.getPlayerPanel().setCurrentPlayerName("Player 1"); view.getPlayerPanel().setTimeLeft(42); - view.getTable().setLeftPlayerName("Player 2"); - view.getTable().setTopPlayerName("Player 3"); - view.getTable().setRightPlayerName("Player 4"); + view.getTablePanel().setLeftPlayerName("Player 2"); + view.getTablePanel().setTopPlayerName("Player 3"); + view.getTablePanel().setRightPlayerName("Player 4"); view.getPlayerPanel().getSortByNumberEvent().add(new IListener() { @Override @@ -67,9 +67,9 @@ public class JRummikub { stones.put(stoneJoker, new Position(2.5f, 0)); stones.put(new Stone(StoneColor.BLACK), new Position(3.5f, 0)); - view.getPlayerPanel().getBoard().setStones(stones); + view.getPlayerPanel().getHandPanel().setStones(stones); - view.getPlayerPanel().getBoard().getClickEvent() + view.getPlayerPanel().getHandPanel().getClickEvent() .add(new IListener2() { @Override public void fire(Position p, Boolean collect) { @@ -78,7 +78,7 @@ public class JRummikub { } }); - view.getPlayerPanel().getBoard().getRangeClickEvent() + view.getPlayerPanel().getHandPanel().getRangeClickEvent() .add(new IListener2() { @Override public void fire(Position p, Boolean collect) { @@ -87,7 +87,7 @@ public class JRummikub { } }); - view.getPlayerPanel().getBoard().getSetClickEvent() + view.getPlayerPanel().getHandPanel().getSetClickEvent() .add(new IListener2() { @Override public void fire(Position p, Boolean collect) { @@ -97,7 +97,7 @@ public class JRummikub { } }); - view.getTable().getClickEvent().add(new IListener2() { + view.getTablePanel().getClickEvent().add(new IListener2() { @Override public void fire(Position p, Boolean collect) { System.out.println("Table clicked at " + p @@ -105,7 +105,7 @@ public class JRummikub { } }); - view.getTable().getRangeClickEvent() + view.getTablePanel().getRangeClickEvent() .add(new IListener2() { @Override public void fire(Position p, Boolean collect) { @@ -114,7 +114,7 @@ public class JRummikub { } }); - view.getTable().getSetClickEvent().add(new IListener2() { + view.getTablePanel().getSetClickEvent().add(new IListener2() { @Override public void fire(Position p, Boolean collect) { System.out.println("Table set-clicked at " + p @@ -123,7 +123,7 @@ public class JRummikub { } }); - view.getTable().getStoneCollection().getClickEvent() + view.getTablePanel().getStoneCollectionPanel().getClickEvent() .add(new IListener2() { @Override public void fire(Position p, Boolean collect) { @@ -132,7 +132,7 @@ public class JRummikub { } }); - view.getTable().getStoneCollection().getRangeClickEvent() + view.getTablePanel().getStoneCollectionPanel().getRangeClickEvent() .add(new IListener2() { @Override public void fire(Position p, Boolean collect) { @@ -141,7 +141,7 @@ public class JRummikub { } }); - view.getTable().getStoneCollection().getSetClickEvent() + view.getTablePanel().getStoneCollectionPanel().getSetClickEvent() .add(new IListener2() { @Override public void fire(Position p, Boolean collect) { @@ -167,7 +167,7 @@ public class JRummikub { stoneSets.put(new StoneSet(stoneList), new Position(3.5f, 4)); - view.getTable().setStoneSets(stoneSets); + view.getTablePanel().setStoneSets(stoneSets); view.setSelectedStones(Arrays.asList(stoneJoker, stone8)); } diff --git a/src/jrummikub/view/IBoard.java b/src/jrummikub/view/IBoard.java deleted file mode 100644 index 1424dff..0000000 --- a/src/jrummikub/view/IBoard.java +++ /dev/null @@ -1,19 +0,0 @@ -package jrummikub.view; - -import java.util.Map; - -import jrummikub.model.Position; -import jrummikub.model.Stone; - -/** - * The view for a player's board that displayed his stones - */ -public interface IBoard extends IClickable { - /** - * Set the player's stones to display on the board - * - * @param stones - * the stones - */ - public void setStones(Map stones); -} diff --git a/src/jrummikub/view/IHandPanel.java b/src/jrummikub/view/IHandPanel.java new file mode 100644 index 0000000..4573b93 --- /dev/null +++ b/src/jrummikub/view/IHandPanel.java @@ -0,0 +1,19 @@ +package jrummikub.view; + +import java.util.Map; + +import jrummikub.model.Position; +import jrummikub.model.Stone; + +/** + * The view for a player's hand that displays his stones + */ +public interface IHandPanel extends IClickable { + /** + * Set the player's stones to display on the board + * + * @param stones + * the stones + */ + public void setStones(Map stones); +} diff --git a/src/jrummikub/view/IPlayerPanel.java b/src/jrummikub/view/IPlayerPanel.java index e39ab65..f07ced4 100644 --- a/src/jrummikub/view/IPlayerPanel.java +++ b/src/jrummikub/view/IPlayerPanel.java @@ -9,7 +9,7 @@ public interface IPlayerPanel { /** * @return the board where the players hand stones are displayed */ - public IBoard getBoard(); + public IHandPanel getHandPanel(); /** * Sets the current player's name diff --git a/src/jrummikub/view/IStoneCollection.java b/src/jrummikub/view/IStoneCollection.java deleted file mode 100644 index 8b4148d..0000000 --- a/src/jrummikub/view/IStoneCollection.java +++ /dev/null @@ -1,7 +0,0 @@ -package jrummikub.view; - -/** - * The view of the collection that shows the stones a player has selected - */ -public interface IStoneCollection extends IClickable { -} diff --git a/src/jrummikub/view/IStoneCollectionPanel.java b/src/jrummikub/view/IStoneCollectionPanel.java new file mode 100644 index 0000000..4c84508 --- /dev/null +++ b/src/jrummikub/view/IStoneCollectionPanel.java @@ -0,0 +1,7 @@ +package jrummikub.view; + +/** + * The view of the collection that shows the stones a player has selected + */ +public interface IStoneCollectionPanel extends IClickable { +} diff --git a/src/jrummikub/view/ITable.java b/src/jrummikub/view/ITable.java deleted file mode 100644 index e257d26..0000000 --- a/src/jrummikub/view/ITable.java +++ /dev/null @@ -1,51 +0,0 @@ -package jrummikub.view; - -import java.util.Map; - -import jrummikub.model.Position; -import jrummikub.model.StoneSet; - -/** - * The view of the table, where the stone sets lie - */ -public interface ITable extends IClickable { - /** - * Sets the player name on the left label - * - * @param playerName - * the name to set - */ - public void setLeftPlayerName(String playerName); - - /** - * Sets the player name on the top label - * - * @param playerName - * the name to set - */ - public void setTopPlayerName(String playerName); - - /** - * Sets the player name on the right label - * - * @param playerName - * the name to set - */ - public void setRightPlayerName(String playerName); - - /** - * Sets the stone sets lying on the table - * - * @param stoneSets - * set stone sets on the table - */ - public void setStoneSets(Map stoneSets); - - /** - * Returns the stone collection (the panel showing the stones currently - * selected) - * - * @return the stone collection - */ - IStoneCollection getStoneCollection(); -} diff --git a/src/jrummikub/view/ITablePanel.java b/src/jrummikub/view/ITablePanel.java new file mode 100644 index 0000000..48eb357 --- /dev/null +++ b/src/jrummikub/view/ITablePanel.java @@ -0,0 +1,51 @@ +package jrummikub.view; + +import java.util.Map; + +import jrummikub.model.Position; +import jrummikub.model.StoneSet; + +/** + * The view of the table, where the stone sets lie + */ +public interface ITablePanel extends IClickable { + /** + * Sets the player name on the left label + * + * @param playerName + * the name to set + */ + public void setLeftPlayerName(String playerName); + + /** + * Sets the player name on the top label + * + * @param playerName + * the name to set + */ + public void setTopPlayerName(String playerName); + + /** + * Sets the player name on the right label + * + * @param playerName + * the name to set + */ + public void setRightPlayerName(String playerName); + + /** + * Sets the stone sets lying on the table + * + * @param stoneSets + * set stone sets on the table + */ + public void setStoneSets(Map stoneSets); + + /** + * Returns the stone collection (the panel showing the stones currently + * selected) + * + * @return the stone collection + */ + IStoneCollectionPanel getStoneCollectionPanel(); +} diff --git a/src/jrummikub/view/IView.java b/src/jrummikub/view/IView.java index cb4ab3e..8e59baa 100644 --- a/src/jrummikub/view/IView.java +++ b/src/jrummikub/view/IView.java @@ -13,7 +13,7 @@ public interface IView { * * @return the table */ - public ITable getTable(); + public ITablePanel getTablePanel(); /** * Returns the player panel diff --git a/src/jrummikub/view/impl/AbstractStonePanel.java b/src/jrummikub/view/impl/AbstractStonePanel.java new file mode 100644 index 0000000..8ab18d8 --- /dev/null +++ b/src/jrummikub/view/impl/AbstractStonePanel.java @@ -0,0 +1,82 @@ +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; + +/** + * Base class for panels that draw stones + */ +@SuppressWarnings("serial") +abstract class AbstractStonePanel extends JPanel implements IClickable { + private StonePainter stonePainter; + + private Event2 clickEvent = new Event2(); + private Event2 rangeClickEvent = new Event2(); + private Event2 setClickEvent = new Event2(); + + /** + * @return the stone painter + */ + protected StonePainter getStonePainter() { + return stonePainter; + } + + /** + * Create a new StonePanel with default scale factor + */ + public AbstractStonePanel() { + this(1); + } + + /** + * Create a new StonePanel with a given scale factor + * + * @param scale + * the grid scale + */ + public AbstractStonePanel(float scale) { + super(true); // Set double buffered + + stonePainter = new StonePainter(scale); + + addMouseListener(new MouseAdapter() { + @Override + public void mouseClicked(MouseEvent e) { + Insets insets = getInsets(); + Event2 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 getClickEvent() { + return clickEvent; + } + + @Override + public Event2 getRangeClickEvent() { + return rangeClickEvent; + } + + @Override + public Event2 getSetClickEvent() { + return setClickEvent; + } + +} diff --git a/src/jrummikub/view/impl/Board.java b/src/jrummikub/view/impl/Board.java deleted file mode 100644 index 94571e0..0000000 --- a/src/jrummikub/view/impl/Board.java +++ /dev/null @@ -1,122 +0,0 @@ -package jrummikub.view.impl; - -import java.awt.Color; -import java.awt.Dimension; -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.image.BufferedImage; -import java.util.Collection; -import java.util.Collections; -import java.util.Map; - -import javax.swing.ImageIcon; -import javax.swing.border.MatteBorder; - -import jrummikub.model.Position; -import jrummikub.model.Stone; -import jrummikub.view.IBoard; - -/** - * Implementation of the board - */ -@SuppressWarnings("serial") -class Board extends StonePanel implements IBoard { - private final static int BOARD_HEIGHT = 2; - private final static int BOARD_WIDTH = 14; - - private final static BufferedImage BACKGROUND; - static { - ImageIcon image = new ImageIcon( - Board.class.getResource("/jrummikub/resource/wood.png")); - BACKGROUND = new BufferedImage(image.getIconWidth(), image.getIconHeight(), - BufferedImage.TYPE_INT_RGB); - - image.paintIcon(null, BACKGROUND.createGraphics(), 0, 0); - } - private BufferedImage scaledBackground = BACKGROUND; - - private Map stones = Collections.emptyMap(); - private Collection selectedStones = Collections.emptyList(); - - /** - * Creates a new Board instance - */ - Board() { - setBorder(new MatteBorder(0, 1, 0, 1, Color.DARK_GRAY)); - - addComponentListener(new ComponentAdapter() { - - @Override - public void componentResized(ComponentEvent e) { - Insets insets = getInsets(); - int size = (getHeight() - insets.top - insets.bottom) / BOARD_HEIGHT; - - getStonePainter().setScale(size * StonePainter.HEIGHT_SCALE); - - setSize(new Dimension(BOARD_WIDTH * getStonePainter().getStoneWidth() - + insets.left + insets.right, getHeight())); - } - }); - } - - private BufferedImage getScaledBackground(int size) { - BufferedImage scaled = new BufferedImage(size, size, - BufferedImage.TYPE_INT_RGB); - Graphics2D g = scaled.createGraphics(); - g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, - RenderingHints.VALUE_INTERPOLATION_BICUBIC); - - g.drawImage(BACKGROUND, 0, 0, size, size, null); - - return scaled; - } - - @Override - protected 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); - int size = height / BOARD_HEIGHT; - - if (scaledBackground.getHeight() != size) - scaledBackground = getScaledBackground(size); - - for (int i = 0; i < BOARD_HEIGHT; ++i) { - for (int xpos = -size * i / 3; xpos < width; xpos += size) { - g.drawImage(scaledBackground, xpos, size * i, null); - } - } - - getStonePainter().setScale(size * StonePainter.HEIGHT_SCALE); - - g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, - RenderingHints.VALUE_ANTIALIAS_ON); - - for (Map.Entry entry : stones.entrySet()) { - getStonePainter().paintStone(g, entry.getKey(), entry.getValue(), - selectedStones.contains(entry.getKey())); - } - } - - @Override - public void setStones(Map stones) { - this.stones = stones; - repaint(); - } - - /** - * Sets the stones that are to be painted selected - * - * @param stones - * the selected stones - */ - void setSelectedStones(Collection stones) { - selectedStones = stones; - repaint(); - } -} diff --git a/src/jrummikub/view/impl/HandPanel.java b/src/jrummikub/view/impl/HandPanel.java new file mode 100644 index 0000000..1610c00 --- /dev/null +++ b/src/jrummikub/view/impl/HandPanel.java @@ -0,0 +1,122 @@ +package jrummikub.view.impl; + +import java.awt.Color; +import java.awt.Dimension; +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.image.BufferedImage; +import java.util.Collection; +import java.util.Collections; +import java.util.Map; + +import javax.swing.ImageIcon; +import javax.swing.border.MatteBorder; + +import jrummikub.model.Position; +import jrummikub.model.Stone; +import jrummikub.view.IHandPanel; + +/** + * Implementation of the board + */ +@SuppressWarnings("serial") +class HandPanel extends AbstractStonePanel implements IHandPanel { + private final static int HAND_HEIGHT = 2; + private final static int HAND_WIDTH = 14; + + private final static BufferedImage BACKGROUND; + static { + ImageIcon image = new ImageIcon( + HandPanel.class.getResource("/jrummikub/resource/wood.png")); + BACKGROUND = new BufferedImage(image.getIconWidth(), image.getIconHeight(), + BufferedImage.TYPE_INT_RGB); + + image.paintIcon(null, BACKGROUND.createGraphics(), 0, 0); + } + private BufferedImage scaledBackground = BACKGROUND; + + private Map stones = Collections.emptyMap(); + private Collection selectedStones = Collections.emptyList(); + + /** + * Creates a new Board instance + */ + HandPanel() { + setBorder(new MatteBorder(0, 1, 0, 1, Color.DARK_GRAY)); + + addComponentListener(new ComponentAdapter() { + + @Override + public void componentResized(ComponentEvent e) { + Insets insets = getInsets(); + int size = (getHeight() - insets.top - insets.bottom) / HAND_HEIGHT; + + getStonePainter().setScale(size * StonePainter.HEIGHT_SCALE); + + setSize(new Dimension(HAND_WIDTH * getStonePainter().getStoneWidth() + + insets.left + insets.right, getHeight())); + } + }); + } + + private BufferedImage getScaledBackground(int size) { + BufferedImage scaled = new BufferedImage(size, size, + BufferedImage.TYPE_INT_RGB); + Graphics2D g = scaled.createGraphics(); + g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, + RenderingHints.VALUE_INTERPOLATION_BICUBIC); + + g.drawImage(BACKGROUND, 0, 0, size, size, null); + + return scaled; + } + + @Override + protected 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); + int size = height / HAND_HEIGHT; + + if (scaledBackground.getHeight() != size) + scaledBackground = getScaledBackground(size); + + for (int i = 0; i < HAND_HEIGHT; ++i) { + for (int xpos = -size * i / 3; xpos < width; xpos += size) { + g.drawImage(scaledBackground, xpos, size * i, null); + } + } + + getStonePainter().setScale(size * StonePainter.HEIGHT_SCALE); + + g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, + RenderingHints.VALUE_ANTIALIAS_ON); + + for (Map.Entry entry : stones.entrySet()) { + getStonePainter().paintStone(g, entry.getKey(), entry.getValue(), + selectedStones.contains(entry.getKey())); + } + } + + @Override + public void setStones(Map stones) { + this.stones = stones; + repaint(); + } + + /** + * Sets the stones that are to be painted selected + * + * @param stones + * the selected stones + */ + void setSelectedStones(Collection stones) { + selectedStones = stones; + repaint(); + } +} diff --git a/src/jrummikub/view/impl/PlayerPanel.java b/src/jrummikub/view/impl/PlayerPanel.java index 9136e85..0a7e64a 100644 --- a/src/jrummikub/view/impl/PlayerPanel.java +++ b/src/jrummikub/view/impl/PlayerPanel.java @@ -31,7 +31,7 @@ class PlayerPanel extends JPanel implements IPlayerPanel { private final static DecimalFormat secondFormat = new DecimalFormat("00"); - private Board board; + private HandPanel hand; private JPanel leftPanel, rightPanel; @@ -46,8 +46,8 @@ class PlayerPanel extends JPanel implements IPlayerPanel { private Event endTurnEvent = new Event(); @Override - public Board getBoard() { - return board; + public HandPanel getHandPanel() { + return hand; } @Override @@ -144,11 +144,11 @@ class PlayerPanel extends JPanel implements IPlayerPanel { Insets insets = getInsets(); int x = insets.left, y = insets.top, width = getWidth() - insets.left - insets.right, height = getHeight() - insets.top - insets.bottom; - int boardWidth = board.getWidth(); + int boardWidth = hand.getWidth(); int panelWidth = (width - boardWidth) / 2; leftPanel.setBounds(x, y, panelWidth, height); - board.setBounds(x + panelWidth, y, boardWidth, height); + hand.setBounds(x + panelWidth, y, boardWidth, height); rightPanel.setBounds(x + panelWidth + boardWidth, y, panelWidth, height); leftPanel.validate(); @@ -164,8 +164,8 @@ class PlayerPanel extends JPanel implements IPlayerPanel { createLeftPanel(); add(leftPanel); - board = new Board(); - add(board); + hand = new HandPanel(); + add(hand); createRightPanel(); add(rightPanel); @@ -178,7 +178,7 @@ class PlayerPanel extends JPanel implements IPlayerPanel { }; addComponentListener(rescaleListener); - board.addComponentListener(rescaleListener); + hand.addComponentListener(rescaleListener); } private class LeftPanelResizeListener extends ComponentAdapter { diff --git a/src/jrummikub/view/impl/StoneCollection.java b/src/jrummikub/view/impl/StoneCollection.java deleted file mode 100644 index a374dd2..0000000 --- a/src/jrummikub/view/impl/StoneCollection.java +++ /dev/null @@ -1,76 +0,0 @@ -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; - -/** - * Implementation of the stone collection (selection) - */ -@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(); - - /** - * Creates a new StoneCollection instance - */ - StoneCollection() { - super(STONE_SCALE); - - setOpaque(false); - setVisible(false); - setBorder(new EmptyBorder(INSET, INSET, INSET, INSET)); - } - - /** - * Sets the stones to be shown in the collection - * - * @param stones - * the selected stones - */ - 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/StoneCollectionPanel.java b/src/jrummikub/view/impl/StoneCollectionPanel.java new file mode 100644 index 0000000..e4ca1c6 --- /dev/null +++ b/src/jrummikub/view/impl/StoneCollectionPanel.java @@ -0,0 +1,76 @@ +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.IStoneCollectionPanel; + +/** + * Implementation of the stone collection (selection) + */ +@SuppressWarnings("serial") +class StoneCollectionPanel extends AbstractStonePanel implements IStoneCollectionPanel { + private final static int INSET = 7; + private final static float STONE_SCALE = 1.1f; + + private Collection selectedStones = Collections.emptyList(); + + /** + * Creates a new StoneCollection instance + */ + StoneCollectionPanel() { + super(STONE_SCALE); + + setOpaque(false); + setVisible(false); + setBorder(new EmptyBorder(INSET, INSET, INSET, INSET)); + } + + /** + * Sets the stones to be shown in the collection + * + * @param stones + * the selected stones + */ + 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/StonePainter.java b/src/jrummikub/view/impl/StonePainter.java index 47c0e9b..b901e78 100644 --- a/src/jrummikub/view/impl/StonePainter.java +++ b/src/jrummikub/view/impl/StonePainter.java @@ -207,7 +207,7 @@ class StonePainter { - faceSize / 2, faceSize, faceSize)); } - private void paintStoneNumber(Graphics2D g, Rectangle r, Color color, int v) { + private void paintStoneValue(Graphics2D g, Rectangle r, Color color, int v) { int pos = r.y + (int) (TEXT_POS * r.height); g.setFont(new Font("SansSerif", Font.BOLD, r.height / 4)); @@ -266,7 +266,7 @@ class StonePainter { if (stone.isJoker()) { paintJoker(g, rect, color); } else { - paintStoneNumber(g, rect, color, stone.getValue()); + paintStoneValue(g, rect, color, stone.getValue()); } paintCircle(g, rect, background); diff --git a/src/jrummikub/view/impl/StonePanel.java b/src/jrummikub/view/impl/StonePanel.java deleted file mode 100644 index 9e84349..0000000 --- a/src/jrummikub/view/impl/StonePanel.java +++ /dev/null @@ -1,82 +0,0 @@ -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; - -/** - * Base class for panels that draw stones - */ -@SuppressWarnings("serial") -abstract class StonePanel extends JPanel implements IClickable { - private StonePainter stonePainter; - - private Event2 clickEvent = new Event2(); - private Event2 rangeClickEvent = new Event2(); - private Event2 setClickEvent = new Event2(); - - /** - * @return the stone painter - */ - protected StonePainter getStonePainter() { - return stonePainter; - } - - /** - * Create a new StonePanel with default scale factor - */ - public StonePanel() { - this(1); - } - - /** - * Create a new StonePanel with a given scale factor - * - * @param scale - * the grid scale - */ - 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 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 getClickEvent() { - return clickEvent; - } - - @Override - public Event2 getRangeClickEvent() { - return rangeClickEvent; - } - - @Override - public Event2 getSetClickEvent() { - return setClickEvent; - } - -} diff --git a/src/jrummikub/view/impl/Table.java b/src/jrummikub/view/impl/Table.java deleted file mode 100644 index 1d78ac1..0000000 --- a/src/jrummikub/view/impl/Table.java +++ /dev/null @@ -1,171 +0,0 @@ -package jrummikub.view.impl; - -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 jrummikub.model.Position; -import jrummikub.model.Stone; -import jrummikub.model.StoneSet; -import jrummikub.view.IStoneCollection; -import jrummikub.view.ITable; - -/** - * The implementation of the table - */ -@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 StoneCollection stoneCollection; - - private Map stoneSets = Collections.emptyMap(); - private Collection selectedStones = Collections.emptyList(); - - @Override - public void setLeftPlayerName(String playerName) { - leftPlayerLabel.setText(playerName); - } - - @Override - public void setTopPlayerName(String playerName) { - topPlayerLabel.setText(playerName); - } - - @Override - public void setRightPlayerName(String playerName) { - rightPlayerLabel.setText(playerName); - } - - @Override - public void setStoneSets(Map stoneSets) { - this.stoneSets = stoneSets; - repaint(); - } - - @Override - public IStoneCollection getStoneCollection() { - return stoneCollection; - } - - /** - * Sets the currently selected stones - * - * @param stones - * the selected stones - */ - 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); - } - - /** - * Creates a new Table instance - */ - Table() { - super(DEFAULT_SCALE); - - setLayout(null); - - leftPlayerLabel = new JLabel(); - leftPlayerLabel.setForeground(Color.WHITE); - 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); - - rightPlayerLabel = new JLabel(); - rightPlayerLabel.setForeground(Color.WHITE); - 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(); - } - }; - - addComponentListener(rescaleListener); - stoneCollection.addComponentListener(rescaleListener); - } - - private void paintStoneSet(Graphics2D g, StoneSet stoneSet, Position pos) { - float x = pos.getX(); - 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); - - 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); - } - - @Override - protected void paintComponent(Graphics g1) { - Graphics2D g = (Graphics2D) g1; - - for (int x = 0; x < getWidth(); x += background.getIconWidth()) { - for (int y = 0; y < getHeight(); y += background.getIconHeight()) { - background.paintIcon(this, g, x, y); - } - } - - g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, - RenderingHints.VALUE_ANTIALIAS_ON); - - for (Map.Entry stoneSet : stoneSets.entrySet()) { - paintStoneSet(g, stoneSet.getKey(), stoneSet.getValue()); - } - } -} diff --git a/src/jrummikub/view/impl/TablePanel.java b/src/jrummikub/view/impl/TablePanel.java new file mode 100644 index 0000000..b02a9a9 --- /dev/null +++ b/src/jrummikub/view/impl/TablePanel.java @@ -0,0 +1,171 @@ +package jrummikub.view.impl; + +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 jrummikub.model.Position; +import jrummikub.model.Stone; +import jrummikub.model.StoneSet; +import jrummikub.view.IStoneCollectionPanel; +import jrummikub.view.ITablePanel; + +/** + * The implementation of the table + */ +@SuppressWarnings("serial") +class TablePanel extends AbstractStonePanel implements ITablePanel { + private final static ImageIcon background = new ImageIcon( + HandPanel.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 StoneCollectionPanel stoneCollection; + + private Map stoneSets = Collections.emptyMap(); + private Collection selectedStones = Collections.emptyList(); + + @Override + public void setLeftPlayerName(String playerName) { + leftPlayerLabel.setText(playerName); + } + + @Override + public void setTopPlayerName(String playerName) { + topPlayerLabel.setText(playerName); + } + + @Override + public void setRightPlayerName(String playerName) { + rightPlayerLabel.setText(playerName); + } + + @Override + public void setStoneSets(Map stoneSets) { + this.stoneSets = stoneSets; + repaint(); + } + + @Override + public IStoneCollectionPanel getStoneCollectionPanel() { + return stoneCollection; + } + + /** + * Sets the currently selected stones + * + * @param stones + * the selected stones + */ + 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); + } + + /** + * Creates a new Table instance + */ + TablePanel() { + super(DEFAULT_SCALE); + + setLayout(null); + + leftPlayerLabel = new JLabel(); + leftPlayerLabel.setForeground(Color.WHITE); + 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); + + rightPlayerLabel = new JLabel(); + rightPlayerLabel.setForeground(Color.WHITE); + rightPlayerLabel.setHorizontalAlignment(JLabel.RIGHT); + rightPlayerLabel.setHorizontalTextPosition(JLabel.RIGHT); + add(rightPlayerLabel); + + stoneCollection = new StoneCollectionPanel(); + add(stoneCollection); + + ComponentListener rescaleListener = new ComponentAdapter() { + @Override + public void componentResized(ComponentEvent e) { + rescale(); + } + }; + + addComponentListener(rescaleListener); + stoneCollection.addComponentListener(rescaleListener); + } + + private void paintStoneSet(Graphics2D g, StoneSet stoneSet, Position pos) { + float x = pos.getX(); + 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); + + 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); + } + + @Override + protected void paintComponent(Graphics g1) { + Graphics2D g = (Graphics2D) g1; + + for (int x = 0; x < getWidth(); x += background.getIconWidth()) { + for (int y = 0; y < getHeight(); y += background.getIconHeight()) { + background.paintIcon(this, g, x, y); + } + } + + g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, + RenderingHints.VALUE_ANTIALIAS_ON); + + for (Map.Entry stoneSet : stoneSets.entrySet()) { + paintStoneSet(g, stoneSet.getKey(), stoneSet.getValue()); + } + } +} diff --git a/src/jrummikub/view/impl/View.java b/src/jrummikub/view/impl/View.java index a257034..7ef0e18 100644 --- a/src/jrummikub/view/impl/View.java +++ b/src/jrummikub/view/impl/View.java @@ -11,7 +11,7 @@ import javax.swing.border.MatteBorder; import jrummikub.model.Stone; import jrummikub.view.IPlayerPanel; -import jrummikub.view.ITable; +import jrummikub.view.ITablePanel; import jrummikub.view.IView; /** @@ -19,7 +19,7 @@ import jrummikub.view.IView; */ @SuppressWarnings("serial") public class View extends JFrame implements IView { - private Table table; + private TablePanel table; private PlayerPanel playerPanel; private final static float PLAYER_PANEL_RATIO = 0.14f; @@ -30,7 +30,7 @@ public class View extends JFrame implements IView { return 2 * (int) (d / 2); } - public ITable getTable() { + public ITablePanel getTablePanel() { return table; } @@ -48,7 +48,7 @@ public class View extends JFrame implements IView { setSize(800, 600); setDefaultCloseOperation(EXIT_ON_CLOSE); - table = new Table(); + table = new TablePanel(); add(table); playerPanel = new PlayerPanel(); @@ -83,6 +83,6 @@ public class View extends JFrame implements IView { @Override public void setSelectedStones(Collection stones) { table.setSelectedStones(stones); - playerPanel.getBoard().setSelectedStones(stones); + playerPanel.getHandPanel().setSelectedStones(stones); } } -- cgit v1.2.3