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
This commit is contained in:
Matthias Schiffer 2011-05-02 00:14:09 +02:00
parent 600cc25ab4
commit adcf9fdfb6
5 changed files with 157 additions and 40 deletions

View file

@ -123,6 +123,34 @@ public class JRummikub {
}
});
view.getTable().getStoneCollection().getClickEvent()
.add(new IListener2<Position, Boolean>() {
@Override
public void fire(Position p, Boolean collect) {
System.out.println("Collection clicked at " + p
+ (collect ? ", collect" : ""));
}
});
view.getTable().getStoneCollection().getRangeClickEvent()
.add(new IListener2<Position, Boolean>() {
@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<Position, Boolean>() {
@Override
public void fire(Position p, Boolean collect) {
System.out.println("Collection set-clicked at " + p
+ (collect ? ", collect" : ""));
}
});
// stoneSets on the table
Map<StoneSet, Position> stoneSets = new HashMap<StoneSet, Position>();

View file

@ -0,0 +1,4 @@
package jrummikub.view;
public interface IStoneCollection extends IClickable {
}

View file

@ -13,4 +13,6 @@ public interface ITable extends IClickable {
public void setRightPlayerName(String playerName);
public void setStoneSets(Map<StoneSet, Position> stoneSets);
IStoneCollection getStoneCollection();
}

View file

@ -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++;
}
}
}
}

View file

@ -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);
innerPanel = new JPanel();
innerPanel.setOpaque(false);
add(innerPanel, BorderLayout.CENTER);
stoneCollection = new StoneCollection();
add(stoneCollection);
ComponentListener rescaleListener = new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
rescale();
}
};
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++;
}
}
}
}