git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@36 72836036-5685-4462-b002-a69064685172
124 lines
3.3 KiB
Java
124 lines
3.3 KiB
Java
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.MouseAdapter;
|
|
import java.awt.event.MouseEvent;
|
|
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.util.Event1;
|
|
import jrummikub.util.IEvent1;
|
|
import jrummikub.view.ITable;
|
|
|
|
@SuppressWarnings("serial")
|
|
public class Table extends JPanel implements ITable {
|
|
private final static ImageIcon background = new ImageIcon(
|
|
Board.class.getResource("/jrummikub/resource/felt.png"));
|
|
|
|
private JLabel leftPlayerLabel, topPlayerLabel, rightPlayerLabel;
|
|
private JPanel innerPanel;
|
|
|
|
private StonePainter stonePainter = new StonePainter(1);
|
|
|
|
private Map<StoneSet, Position> stoneSets = Collections.emptyMap();
|
|
|
|
private Event1<Position> clickEvent = new Event1<Position>();
|
|
|
|
|
|
@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<StoneSet, Position> stoneSets) {
|
|
this.stoneSets = stoneSets;
|
|
}
|
|
|
|
@Override
|
|
public IEvent1<Position> getClickEvent() {
|
|
return clickEvent;
|
|
}
|
|
|
|
Table() {
|
|
super(true); // set double buffered
|
|
setLayout(new BorderLayout());
|
|
|
|
leftPlayerLabel = new JLabel();
|
|
leftPlayerLabel.setForeground(Color.WHITE);
|
|
add(leftPlayerLabel, BorderLayout.WEST);
|
|
|
|
topPlayerLabel = new JLabel();
|
|
topPlayerLabel.setHorizontalAlignment(JLabel.CENTER);
|
|
topPlayerLabel.setHorizontalTextPosition(JLabel.CENTER);
|
|
topPlayerLabel.setForeground(Color.WHITE);
|
|
add(topPlayerLabel, BorderLayout.NORTH);
|
|
|
|
rightPlayerLabel = new JLabel();
|
|
rightPlayerLabel.setForeground(Color.WHITE);
|
|
add(rightPlayerLabel, BorderLayout.EAST);
|
|
|
|
innerPanel = new JPanel();
|
|
innerPanel.setOpaque(false);
|
|
add(innerPanel, BorderLayout.CENTER);
|
|
|
|
addMouseListener(new MouseAdapter() {
|
|
@Override
|
|
public void mouseClicked(MouseEvent e) {
|
|
Insets insets = getInsets();
|
|
|
|
clickEvent.fire(stonePainter.calculatePosition(e.getX() - insets.left,
|
|
e.getY() - insets.top));
|
|
}
|
|
});
|
|
}
|
|
|
|
public void paintStoneSet(Graphics2D g, StoneSet stoneSet, Position pos) {
|
|
float x = pos.getX();
|
|
|
|
for(Stone stone : stoneSet){
|
|
stonePainter.paintStone(g, stone, new Position(x, pos.getY()), false);
|
|
x++;
|
|
}
|
|
}
|
|
|
|
@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, Position> stoneSet : stoneSets.entrySet()) {
|
|
paintStoneSet(g, stoneSet.getKey(), stoneSet.getValue());
|
|
}
|
|
}
|
|
}
|