Handle clicks on table
git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@34 72836036-5685-4462-b002-a69064685172
This commit is contained in:
parent
7b80311556
commit
1b15b71a54
4 changed files with 58 additions and 50 deletions
|
@ -71,6 +71,14 @@ public class JRummikub {
|
||||||
|
|
||||||
}});
|
}});
|
||||||
|
|
||||||
|
view.getTable().getClickEvent().add(new IListener1<Position>(){
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void fire(Position value) {
|
||||||
|
System.out.println("Table clicked at "+value);
|
||||||
|
|
||||||
|
}});
|
||||||
|
|
||||||
//stoneSets on the table
|
//stoneSets on the table
|
||||||
Map<StoneSet, Position> stoneSets = new HashMap<StoneSet, Position>();
|
Map<StoneSet, Position> stoneSets = new HashMap<StoneSet, Position>();
|
||||||
|
|
||||||
|
|
|
@ -4,10 +4,12 @@ import java.util.Map;
|
||||||
|
|
||||||
import jrummikub.model.Position;
|
import jrummikub.model.Position;
|
||||||
import jrummikub.model.StoneSet;
|
import jrummikub.model.StoneSet;
|
||||||
|
import jrummikub.util.IEvent1;
|
||||||
|
|
||||||
public interface ITable {
|
public interface ITable {
|
||||||
public void setLeftPlayerName(String playerName);
|
public void setLeftPlayerName(String playerName);
|
||||||
public void setTopPlayerName(String playerName);
|
public void setTopPlayerName(String playerName);
|
||||||
public void setRightPlayerName(String playerName);
|
public void setRightPlayerName(String playerName);
|
||||||
void setStoneSets(Map<StoneSet, Position> stoneSets);
|
public void setStoneSets(Map<StoneSet, Position> stoneSets);
|
||||||
|
public IEvent1<Position> getClickEvent();
|
||||||
}
|
}
|
|
@ -5,8 +5,8 @@ import java.awt.Graphics;
|
||||||
import java.awt.Graphics2D;
|
import java.awt.Graphics2D;
|
||||||
import java.awt.Insets;
|
import java.awt.Insets;
|
||||||
import java.awt.RenderingHints;
|
import java.awt.RenderingHints;
|
||||||
|
import java.awt.event.MouseAdapter;
|
||||||
import java.awt.event.MouseEvent;
|
import java.awt.event.MouseEvent;
|
||||||
import java.awt.event.MouseListener;
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
@ -16,7 +16,6 @@ import javax.swing.JPanel;
|
||||||
import jrummikub.model.Position;
|
import jrummikub.model.Position;
|
||||||
import jrummikub.model.Stone;
|
import jrummikub.model.Stone;
|
||||||
import jrummikub.util.Event1;
|
import jrummikub.util.Event1;
|
||||||
import jrummikub.util.IEvent;
|
|
||||||
import jrummikub.util.IEvent1;
|
import jrummikub.util.IEvent1;
|
||||||
import jrummikub.view.IBoard;
|
import jrummikub.view.IBoard;
|
||||||
|
|
||||||
|
@ -30,45 +29,21 @@ public class Board extends JPanel implements IBoard {
|
||||||
private Event1<Position> clickEvent = new Event1<Position>();
|
private Event1<Position> clickEvent = new Event1<Position>();
|
||||||
|
|
||||||
Board() {
|
Board() {
|
||||||
super(true);
|
super(true); // set double buffered
|
||||||
|
|
||||||
setBorder(new CustomBorder(Color.DARK_GRAY, 0, 1, 0, 1));
|
setBorder(new CustomBorder(Color.DARK_GRAY, 0, 1, 0, 1));
|
||||||
|
|
||||||
addMouseListener(new MouseListener(){
|
|
||||||
|
|
||||||
|
addMouseListener(new MouseAdapter() {
|
||||||
@Override
|
@Override
|
||||||
public void mouseClicked(MouseEvent e) {
|
public void mouseClicked(MouseEvent e) {
|
||||||
Insets insets = getInsets();
|
Insets insets = getInsets();
|
||||||
|
|
||||||
clickEvent.fire(stonePainter.calculatePosition(e.getX()-insets.left, e.getY()-insets.top));
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
clickEvent.fire(stonePainter.calculatePosition(e.getX() - insets.left,
|
||||||
public void mouseEntered(MouseEvent e) {
|
e.getY() - insets.top));
|
||||||
// TODO Auto-generated method stub
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
});
|
||||||
@Override
|
|
||||||
public void mouseExited(MouseEvent e) {
|
|
||||||
// TODO Auto-generated method stub
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void mousePressed(MouseEvent e) {
|
|
||||||
// TODO Auto-generated method stub
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void mouseReleased(MouseEvent e) {
|
|
||||||
// TODO Auto-generated method stub
|
|
||||||
|
|
||||||
}});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public IEvent1<Position> getClickEvent() {
|
public IEvent1<Position> getClickEvent() {
|
||||||
return clickEvent;
|
return clickEvent;
|
||||||
|
|
|
@ -4,7 +4,10 @@ import java.awt.BorderLayout;
|
||||||
import java.awt.Color;
|
import java.awt.Color;
|
||||||
import java.awt.Graphics;
|
import java.awt.Graphics;
|
||||||
import java.awt.Graphics2D;
|
import java.awt.Graphics2D;
|
||||||
|
import java.awt.Insets;
|
||||||
import java.awt.RenderingHints;
|
import java.awt.RenderingHints;
|
||||||
|
import java.awt.event.MouseAdapter;
|
||||||
|
import java.awt.event.MouseEvent;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
@ -13,21 +16,26 @@ import javax.swing.JLabel;
|
||||||
import javax.swing.JPanel;
|
import javax.swing.JPanel;
|
||||||
|
|
||||||
import jrummikub.model.Position;
|
import jrummikub.model.Position;
|
||||||
import jrummikub.model.Stone;
|
|
||||||
import jrummikub.model.StoneSet;
|
import jrummikub.model.StoneSet;
|
||||||
|
import jrummikub.util.Event1;
|
||||||
|
import jrummikub.util.IEvent1;
|
||||||
import jrummikub.view.ITable;
|
import jrummikub.view.ITable;
|
||||||
|
|
||||||
@SuppressWarnings("serial")
|
@SuppressWarnings("serial")
|
||||||
public class Table extends JPanel implements ITable {
|
public class Table extends JPanel implements ITable {
|
||||||
private final static ImageIcon background = new ImageIcon(Board.class.getResource("/jrummikub/resource/felt.png"));
|
private final static ImageIcon background = new ImageIcon(
|
||||||
|
Board.class.getResource("/jrummikub/resource/felt.png"));
|
||||||
|
|
||||||
private JLabel leftPlayerLabel, topPlayerLabel, rightPlayerLabel;
|
private JLabel leftPlayerLabel, topPlayerLabel, rightPlayerLabel;
|
||||||
private JPanel innerPanel;
|
private JPanel innerPanel;
|
||||||
|
|
||||||
private StonePainter stonePainter = new StonePainter(1);
|
private StonePainter stonePainter = new StonePainter(1);
|
||||||
|
|
||||||
private Map<StoneSet, Position> stoneSets = Collections.emptyMap();
|
private Map<StoneSet, Position> stoneSets = Collections.emptyMap();
|
||||||
|
|
||||||
|
private Event1<Position> clickEvent = new Event1<Position>();
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setLeftPlayerName(String playerName) {
|
public void setLeftPlayerName(String playerName) {
|
||||||
leftPlayerLabel.setText(playerName);
|
leftPlayerLabel.setText(playerName);
|
||||||
|
@ -42,45 +50,60 @@ public class Table extends JPanel implements ITable {
|
||||||
public void setRightPlayerName(String playerName) {
|
public void setRightPlayerName(String playerName) {
|
||||||
rightPlayerLabel.setText(playerName);
|
rightPlayerLabel.setText(playerName);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setStoneSets(Map<StoneSet, Position> stoneSets) {
|
public void setStoneSets(Map<StoneSet, Position> stoneSets) {
|
||||||
this.stoneSets = stoneSets;
|
this.stoneSets = stoneSets;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public IEvent1<Position> getClickEvent() {
|
||||||
|
return clickEvent;
|
||||||
|
}
|
||||||
|
|
||||||
Table() {
|
Table() {
|
||||||
super(true); // set double buffered
|
super(true); // set double buffered
|
||||||
setLayout(new BorderLayout());
|
setLayout(new BorderLayout());
|
||||||
|
|
||||||
leftPlayerLabel = new JLabel();
|
leftPlayerLabel = new JLabel();
|
||||||
leftPlayerLabel.setForeground(Color.WHITE);
|
leftPlayerLabel.setForeground(Color.WHITE);
|
||||||
add(leftPlayerLabel, BorderLayout.WEST);
|
add(leftPlayerLabel, BorderLayout.WEST);
|
||||||
|
|
||||||
topPlayerLabel = new JLabel();
|
topPlayerLabel = new JLabel();
|
||||||
topPlayerLabel.setHorizontalAlignment(JLabel.CENTER);
|
topPlayerLabel.setHorizontalAlignment(JLabel.CENTER);
|
||||||
topPlayerLabel.setHorizontalTextPosition(JLabel.CENTER);
|
topPlayerLabel.setHorizontalTextPosition(JLabel.CENTER);
|
||||||
topPlayerLabel.setForeground(Color.WHITE);
|
topPlayerLabel.setForeground(Color.WHITE);
|
||||||
add(topPlayerLabel, BorderLayout.NORTH);
|
add(topPlayerLabel, BorderLayout.NORTH);
|
||||||
|
|
||||||
rightPlayerLabel = new JLabel();
|
rightPlayerLabel = new JLabel();
|
||||||
rightPlayerLabel.setForeground(Color.WHITE);
|
rightPlayerLabel.setForeground(Color.WHITE);
|
||||||
add(rightPlayerLabel, BorderLayout.EAST);
|
add(rightPlayerLabel, BorderLayout.EAST);
|
||||||
|
|
||||||
innerPanel = new JPanel();
|
innerPanel = new JPanel();
|
||||||
innerPanel.setOpaque(false);
|
innerPanel.setOpaque(false);
|
||||||
add(innerPanel, BorderLayout.CENTER);
|
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));
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void paintComponent(Graphics g1) {
|
protected void paintComponent(Graphics g1) {
|
||||||
Graphics2D g = (Graphics2D)g1;
|
Graphics2D g = (Graphics2D) g1;
|
||||||
|
|
||||||
for(int x = 0; x < getWidth(); x += background.getIconWidth()) {
|
for (int x = 0; x < getWidth(); x += background.getIconWidth()) {
|
||||||
for(int y = 0; y < getHeight(); y += background.getIconHeight()) {
|
for (int y = 0; y < getHeight(); y += background.getIconHeight()) {
|
||||||
background.paintIcon(this, g, x, y);
|
background.paintIcon(this, g, x, y);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
|
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
|
||||||
RenderingHints.VALUE_ANTIALIAS_ON);
|
RenderingHints.VALUE_ANTIALIAS_ON);
|
||||||
|
|
||||||
|
|
Reference in a new issue