From 1b15b71a54932a28343917aeeade66d9fc17a550 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Sat, 30 Apr 2011 21:50:25 +0200 Subject: Handle clicks on table git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@34 72836036-5685-4462-b002-a69064685172 --- src/jrummikub/JRummikub.java | 8 ++++++ src/jrummikub/view/ITable.java | 4 ++- src/jrummikub/view/impl/Board.java | 39 +++++--------------------- src/jrummikub/view/impl/Table.java | 57 ++++++++++++++++++++++++++------------ 4 files changed, 58 insertions(+), 50 deletions(-) diff --git a/src/jrummikub/JRummikub.java b/src/jrummikub/JRummikub.java index e4820bc..93cef06 100644 --- a/src/jrummikub/JRummikub.java +++ b/src/jrummikub/JRummikub.java @@ -71,6 +71,14 @@ public class JRummikub { }}); + view.getTable().getClickEvent().add(new IListener1(){ + + @Override + public void fire(Position value) { + System.out.println("Table clicked at "+value); + + }}); + //stoneSets on the table Map stoneSets = new HashMap(); diff --git a/src/jrummikub/view/ITable.java b/src/jrummikub/view/ITable.java index ce508bd..9781f78 100644 --- a/src/jrummikub/view/ITable.java +++ b/src/jrummikub/view/ITable.java @@ -4,10 +4,12 @@ import java.util.Map; import jrummikub.model.Position; import jrummikub.model.StoneSet; +import jrummikub.util.IEvent1; public interface ITable { public void setLeftPlayerName(String playerName); public void setTopPlayerName(String playerName); public void setRightPlayerName(String playerName); - void setStoneSets(Map stoneSets); + public void setStoneSets(Map stoneSets); + public IEvent1 getClickEvent(); } \ No newline at end of file diff --git a/src/jrummikub/view/impl/Board.java b/src/jrummikub/view/impl/Board.java index b227760..b8bd206 100644 --- a/src/jrummikub/view/impl/Board.java +++ b/src/jrummikub/view/impl/Board.java @@ -5,8 +5,8 @@ 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.awt.event.MouseListener; import java.util.Collections; import java.util.Map; @@ -16,7 +16,6 @@ import javax.swing.JPanel; import jrummikub.model.Position; import jrummikub.model.Stone; import jrummikub.util.Event1; -import jrummikub.util.IEvent; import jrummikub.util.IEvent1; import jrummikub.view.IBoard; @@ -30,45 +29,21 @@ public class Board extends JPanel implements IBoard { private Event1 clickEvent = new Event1(); Board() { - super(true); + super(true); // set double buffered setBorder(new CustomBorder(Color.DARK_GRAY, 0, 1, 0, 1)); - - addMouseListener(new MouseListener(){ + 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 - public void mouseEntered(MouseEvent e) { - // TODO Auto-generated method stub - + clickEvent.fire(stonePainter.calculatePosition(e.getX() - insets.left, + e.getY() - insets.top)); } - - @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 public IEvent1 getClickEvent() { return clickEvent; diff --git a/src/jrummikub/view/impl/Table.java b/src/jrummikub/view/impl/Table.java index f7460e2..23ce19d 100644 --- a/src/jrummikub/view/impl/Table.java +++ b/src/jrummikub/view/impl/Table.java @@ -4,7 +4,10 @@ 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; @@ -13,21 +16,26 @@ 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 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 stoneSets = Collections.emptyMap(); - + + private Event1 clickEvent = new Event1(); + + @Override public void setLeftPlayerName(String playerName) { leftPlayerLabel.setText(playerName); @@ -42,45 +50,60 @@ public class Table extends JPanel implements ITable { public void setRightPlayerName(String playerName) { rightPlayerLabel.setText(playerName); } - + @Override public void setStoneSets(Map stoneSets) { this.stoneSets = stoneSets; } + @Override + public IEvent1 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)); + } + }); } - + @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()) { + 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); -- cgit v1.2.3