diff options
Diffstat (limited to 'src/jrummikub/view/impl')
-rw-r--r-- | src/jrummikub/view/impl/StoneCollection.java | 65 | ||||
-rw-r--r-- | src/jrummikub/view/impl/Table.java | 98 |
2 files changed, 123 insertions, 40 deletions
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<Stone> selectedStones = Collections.emptyList(); + + StoneCollection() { + super(STONE_SCALE); + + setOpaque(false); + setVisible(false); + setBorder(new EmptyBorder(INSET, INSET, INSET, INSET)); + } + + void setSelectedStones(Collection<Stone> 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<StoneSet, Position> stoneSets = Collections.emptyMap(); private Collection<Stone> selectedStones = Collections.emptyList(); @@ -53,50 +56,87 @@ class Table extends StonePanel implements ITable { repaint(); } + @Override + public IStoneCollection getStoneCollection() { + return stoneCollection; + } + public void setSelectedStones(Collection<Stone> 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, Position> 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++; - } - } } } |