From 9ad7f0822eb66972f740fb3da58c85d7a555d549 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Mon, 16 May 2011 20:49:11 +0200 Subject: Add hand row controls to view git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@241 72836036-5685-4462-b002-a69064685172 --- src/jrummikub/view/IHandPanel.java | 15 +---- src/jrummikub/view/impl/HandPanel.java | 99 ++++++++++++++++++++++---------- src/jrummikub/view/impl/PlayerPanel.java | 63 +++++++++++++++++--- 3 files changed, 126 insertions(+), 51 deletions(-) (limited to 'src/jrummikub/view') diff --git a/src/jrummikub/view/IHandPanel.java b/src/jrummikub/view/IHandPanel.java index 3a76780..ea08f86 100644 --- a/src/jrummikub/view/IHandPanel.java +++ b/src/jrummikub/view/IHandPanel.java @@ -17,18 +17,7 @@ public interface IHandPanel extends IStonePanel, IClickable { public void setStones(Iterable> stones); /** - * Set the number of stones that fit on the hand horizontally - * - * @param width - * number of stones - */ - public void setHandWidth(int width); - - /** - * Set the number of stones that fit on the hand vertically - * - * @param height - * number of stones + * Resets the rows currently displayed */ - public void setHandHeight(int height); + void resetCurrentRow(); } diff --git a/src/jrummikub/view/impl/HandPanel.java b/src/jrummikub/view/impl/HandPanel.java index 6ad6270..bede319 100644 --- a/src/jrummikub/view/impl/HandPanel.java +++ b/src/jrummikub/view/impl/HandPanel.java @@ -15,6 +15,7 @@ import java.util.Collections; import javax.swing.ImageIcon; import javax.swing.border.MatteBorder; +import jrummikub.model.Hand; import jrummikub.model.Position; import jrummikub.model.Stone; import jrummikub.util.Pair; @@ -25,42 +26,34 @@ import jrummikub.view.IHandPanel; */ @SuppressWarnings("serial") class HandPanel extends AbstractStonePanel implements IHandPanel { - private int handWidth = 1; - private int handHeight = 1; + private final static int HEIGHT = 2; 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); + BACKGROUND = new BufferedImage(image.getIconWidth(), image.getIconHeight(), + BufferedImage.TYPE_INT_RGB); image.paintIcon(null, BACKGROUND.createGraphics(), 0, 0); } + private BufferedImage scaledBackground = BACKGROUND; - private boolean repaintAll = true; + private PlayerPanel playerPanel; - private Collection selectedStones = Collections.emptyList(); - - @Override - public void setHandWidth(int width) { - handWidth = width; - rescale(); - repaint(); - } + private int currentRow = 0; + private int maxRow = 0; - @Override - public void setHandHeight(int height) { - handHeight = height; - rescale(); - repaint(); - } + private boolean repaintAll = true; + private Collection selectedStones = Collections.emptyList(); /** * Creates a new Board instance */ - HandPanel() { + HandPanel(PlayerPanel playerPanel) { + this.playerPanel = playerPanel; + setBorder(new MatteBorder(0, 1, 0, 1, Color.DARK_GRAY)); addComponentListener(new ComponentAdapter() { @@ -88,26 +81,27 @@ class HandPanel extends AbstractStonePanel implements IHandPanel { 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; + - insets.right, height = getHeight() - insets.top - insets.bottom; Graphics2D g = (Graphics2D) g1.create(x, y, width, height); - int size = height / handHeight; + int size = height / HEIGHT; if (repaintAll) { if (scaledBackground.getHeight() != size) scaledBackground = getScaledBackground(size); - for (int i = 0; i < handHeight; ++i) { + for (int i = 0; i < HEIGHT; ++i) { for (int xpos = -size * i / 3; xpos < width; xpos += size) { g.drawImage(scaledBackground, xpos, size * i, null); } } } + Pair trans = getTranslation(); + g.translate(trans.getFirst(), trans.getSecond()); + for (Pair entry : getStones()) { - getStonePainter().paintStone(g, entry.getFirst(), - entry.getSecond(), + getStonePainter().paintStone(g, entry.getFirst(), entry.getSecond(), selectedStones.contains(entry.getFirst()), entry.getFirst() == getHoveredStone()); } @@ -116,15 +110,62 @@ class HandPanel extends AbstractStonePanel implements IHandPanel { @Override public void setStones(Iterable> stones) { super.setStones(stones); + + maxRow = 0; + for (Pair entry : stones) { + if (entry.getSecond().getY() > maxRow) { + maxRow = (int) entry.getSecond().getY(); + } + } + + repaintAll = true; + repaint(); + + playerPanel.updateButtons(); + } + + void rowUp() { + currentRow--; + repaintAll = true; + repaint(); + + playerPanel.updateButtons(); + } + + void rowDown() { + currentRow++; repaintAll = true; repaint(); + + playerPanel.updateButtons(); + } + + @Override + public void resetCurrentRow() { + currentRow = Math.max(0, maxRow - 1); + + playerPanel.updateButtons(); + } + + boolean canRowUp() { + return (currentRow > 0); + } + + boolean canRowDown() { + return (currentRow < maxRow); + } + + @Override + public Pair getTranslation() { + return new Pair(0, -getStonePainter().getStoneHeight() + * currentRow); } /** * Sets the stones that are to be painted selected * * @param stones - * the selected stones + * the selected stones */ void setSelectedStones(Collection stones) { selectedStones = stones; @@ -133,11 +174,11 @@ class HandPanel extends AbstractStonePanel implements IHandPanel { private void rescale() { Insets insets = getInsets(); - int size = (getHeight() - insets.top - insets.bottom) / handHeight; + int size = (getHeight() - insets.top - insets.bottom) / HEIGHT; getStonePainter().setScale(size * StonePainter.HEIGHT_SCALE); - setSize(new Dimension(handWidth * getStonePainter().getStoneWidth() + setSize(new Dimension(Hand.WIDTH * getStonePainter().getStoneWidth() + insets.left + insets.right, getHeight())); repaintAll = true; diff --git a/src/jrummikub/view/impl/PlayerPanel.java b/src/jrummikub/view/impl/PlayerPanel.java index ce8b8ec..20fb05a 100644 --- a/src/jrummikub/view/impl/PlayerPanel.java +++ b/src/jrummikub/view/impl/PlayerPanel.java @@ -28,6 +28,7 @@ class PlayerPanel extends JPanel implements IPlayerPanel { private final static int SIDE_PANEL_SEPARATOR = 10; private final static float SIDE_PANEL_FIRST_LINE_HEIGHT = 0.375f; private final static int SIDE_PANEL_MAX_WIDTH = 180; + private final static float HAND_ROW_BUTTON_RATIO = 0.03f; private final static float MAX_BUTTON_FONT_SIZE = 12; private final static DecimalFormat secondFormat = new DecimalFormat("00"); @@ -39,6 +40,8 @@ class PlayerPanel extends JPanel implements IPlayerPanel { private JLabel currentPlayerNameLabel; private JButton sortByGroupsButton; private JButton sortByRunsButton; + private JButton handRowUpButton; + private JButton handRowDownButton; private JProgressBar timeBar; private JButton endTurnButton; @@ -132,6 +135,26 @@ class PlayerPanel extends JPanel implements IPlayerPanel { rightPanel.setBorder(new EmptyBorder(SIDE_PANEL_INSET, SIDE_PANEL_INSET, SIDE_PANEL_INSET, SIDE_PANEL_INSET)); + handRowUpButton = new JButton("
\u25B2"); + handRowUpButton.setMargin(new Insets(0, 0, 0, 0)); + handRowUpButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + hand.rowUp(); + } + }); + rightPanel.add(handRowUpButton); + + handRowDownButton = new JButton("
\u25BC"); + handRowDownButton.setMargin(new Insets(0, 0, 0, 0)); + handRowDownButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + hand.rowDown(); + } + }); + rightPanel.add(handRowDownButton); + timeBar = new JProgressBar(0, 60); timeBar.setStringPainted(true); rightPanel.add(timeBar); @@ -141,7 +164,7 @@ class PlayerPanel extends JPanel implements IPlayerPanel { endTurnButton.setMargin(new Insets(0, 0, 0, 0)); endTurnButton.addActionListener(new ActionListener() { @Override - public void actionPerformed(ActionEvent arg0) { + public void actionPerformed(ActionEvent e) { endTurnEvent.emit(); } }); @@ -156,16 +179,28 @@ class PlayerPanel extends JPanel implements IPlayerPanel { int x = insets.left, y = insets.top, width = getWidth() - insets.left - insets.right, height = getHeight() - insets.top - insets.bottom; int boardWidth = hand.getWidth(); - int panelWidth = (width - boardWidth) / 2; + int handButtonWidth = (int) (width * HAND_ROW_BUTTON_RATIO); + int meanPanelWidth = (width - boardWidth) / 2; + int leftPanelWidth = meanPanelWidth - handButtonWidth / 2; + int rightPanelWidth = meanPanelWidth + handButtonWidth / 2; - leftPanel.setBounds(x, y, panelWidth, height); - hand.setBounds(x + panelWidth, y, boardWidth, height); - rightPanel.setBounds(x + panelWidth + boardWidth, y, panelWidth, height); + leftPanel.setBounds(x, y, leftPanelWidth, height); + hand.setBounds(x + leftPanelWidth, y, boardWidth, height); + rightPanel.setBounds(x + leftPanelWidth + boardWidth, y, rightPanelWidth, + height); leftPanel.validate(); rightPanel.validate(); } + void updateButtons() { + handRowUpButton.setEnabled(hand.canRowUp()); + handRowUpButton.setForeground(hand.canRowUp() ? Color.BLACK : Color.GRAY); + handRowDownButton.setEnabled(hand.canRowDown()); + handRowDownButton.setForeground(hand.canRowDown() ? Color.BLACK + : Color.GRAY); + } + /** * Creates a new PlayerPanel instance */ @@ -175,7 +210,7 @@ class PlayerPanel extends JPanel implements IPlayerPanel { createLeftPanel(); add(leftPanel); - hand = new HandPanel(); + hand = new HandPanel(this); add(hand); createRightPanel(); @@ -227,10 +262,12 @@ class PlayerPanel extends JPanel implements IPlayerPanel { private class RightPanelResizeListener extends ComponentAdapter { @Override public void componentResized(ComponentEvent e) { + int handButtonWidth = (int) (getWidth() * HAND_ROW_BUTTON_RATIO); + Insets insets = rightPanel.getInsets(); - int x = insets.left, y = insets.top, width = rightPanel.getWidth() - - insets.left - insets.right, height = rightPanel.getHeight() - - insets.top - insets.bottom; + int x = insets.left + handButtonWidth, y = insets.top, width = rightPanel + .getWidth() - insets.left - insets.right - handButtonWidth, height = rightPanel + .getHeight() - insets.top - insets.bottom; if (width > SIDE_PANEL_MAX_WIDTH) { x += (width - SIDE_PANEL_MAX_WIDTH) / 4; @@ -244,6 +281,14 @@ class PlayerPanel extends JPanel implements IPlayerPanel { if (fontSize > MAX_BUTTON_FONT_SIZE) fontSize = MAX_BUTTON_FONT_SIZE; + handRowUpButton.setBounds(0, 0, handButtonWidth, getHeight() / 2); + handRowUpButton.setFont(handRowUpButton.getFont().deriveFont( + fontSize * 1.5f)); + handRowDownButton.setBounds(0, getHeight() / 2, handButtonWidth, + getHeight() / 2); + handRowDownButton.setFont(handRowDownButton.getFont().deriveFont( + fontSize * 1.5f)); + timeBar.setBounds(x, y, width, firstLineHeight); endTurnButton.setBounds(x, y + firstLineHeight + SIDE_PANEL_SEPARATOR, buttonWidth, buttonHeight); -- cgit v1.2.3