package jrummikub.view.impl; import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Insets; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.List; import javax.swing.border.EmptyBorder; import jrummikub.model.Position; import jrummikub.model.Stone; import jrummikub.util.Pair; 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 Collection selectedStones = Collections.emptyList(); /** * Creates a new StoneCollection instance */ StoneCollectionPanel() { setOpaque(false); setVisible(false); setBorder(new EmptyBorder(INSET, INSET, INSET, INSET)); } private void rescale() { setSize(getStonePainter().getStoneWidth() * selectedStones.size() + 2 * INSET, getStonePainter().getStoneHeight() + 2 * INSET); } /** * Sets the height to paint the collected stones in * * @param height * the height in pixels */ void setStoneHeight(int height) { getStonePainter().setScale(height * StonePainter.HEIGHT_SCALE); rescale(); repaint(); } /** * Sets the stones to be shown in the collection * * @param selectedStones * the selected stones */ void setSelectedStones(Collection selectedStones) { this.selectedStones = selectedStones; List> stones = new ArrayList>(); float x = 0; for (Stone stone : selectedStones) { stones.add(new Pair(stone, new Position(x, 0))); x++; } setStones(stones); if (selectedStones.isEmpty()) { setVisible(false); } else { rescale(); 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++; } } } }