From 54271eb86d67d1c12f38e1b63cb6c455612f252b Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Sun, 1 May 2011 00:41:27 +0200 Subject: Discriminate between different click events git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@44 72836036-5685-4462-b002-a69064685172 --- src/jrummikub/JRummikub.java | 40 ++++++++++++++++----- src/jrummikub/view/IBoard.java | 5 +-- src/jrummikub/view/IClickable.java | 10 ++++++ src/jrummikub/view/ITable.java | 5 +-- src/jrummikub/view/impl/Board.java | 37 +++++++------------- src/jrummikub/view/impl/StonePanel.java | 62 +++++++++++++++++++++++++++++++++ src/jrummikub/view/impl/Table.java | 35 ++++--------------- 7 files changed, 124 insertions(+), 70 deletions(-) create mode 100644 src/jrummikub/view/IClickable.java create mode 100644 src/jrummikub/view/impl/StonePanel.java diff --git a/src/jrummikub/JRummikub.java b/src/jrummikub/JRummikub.java index c9047a1..de270f6 100644 --- a/src/jrummikub/JRummikub.java +++ b/src/jrummikub/JRummikub.java @@ -13,7 +13,7 @@ import jrummikub.model.Stone; import jrummikub.model.StoneColor; import jrummikub.model.StoneSet; import jrummikub.util.IListener; -import jrummikub.util.IListener1; +import jrummikub.util.IListener2; import jrummikub.view.IView; public class JRummikub { @@ -66,19 +66,41 @@ public class JRummikub { view.getPlayerPanel().getBoard().setStones(stones); - view.getPlayerPanel().getBoard().getClickEvent().add(new IListener1(){ - + view.getPlayerPanel().getBoard().getClickEvent().add(new IListener2(){ + @Override + public void fire(Position p, Boolean collect) { + System.out.println("Board clicked at "+p+(collect?", collect":"")); + + }}); + view.getPlayerPanel().getBoard().getRangeClickEvent().add(new IListener2(){ + @Override + public void fire(Position p, Boolean collect) { + System.out.println("Board range-clicked at "+p+(collect?", collect":"")); + + }}); + view.getPlayerPanel().getBoard().getSetClickEvent().add(new IListener2(){ @Override - public void fire(Position value) { - System.out.println("Board clicked at "+value); + public void fire(Position p, Boolean collect) { + System.out.println("Board set-clicked at "+p+(collect?", collect":"")); }}); - view.getTable().getClickEvent().add(new IListener1(){ - + view.getTable().getClickEvent().add(new IListener2(){ + @Override + public void fire(Position p, Boolean collect) { + System.out.println("Table clicked at "+p+(collect?", collect":"")); + + }}); + view.getTable().getRangeClickEvent().add(new IListener2(){ + @Override + public void fire(Position p, Boolean collect) { + System.out.println("Table range-clicked at "+p+(collect?", collect":"")); + + }}); + view.getTable().getSetClickEvent().add(new IListener2(){ @Override - public void fire(Position value) { - System.out.println("Table clicked at "+value); + public void fire(Position p, Boolean collect) { + System.out.println("Table set-clicked at "+p+(collect?", collect":"")); }}); diff --git a/src/jrummikub/view/IBoard.java b/src/jrummikub/view/IBoard.java index 237518f..cf048f1 100644 --- a/src/jrummikub/view/IBoard.java +++ b/src/jrummikub/view/IBoard.java @@ -4,10 +4,7 @@ import java.util.Map; import jrummikub.model.Position; import jrummikub.model.Stone; -import jrummikub.util.IEvent1; -public interface IBoard { +public interface IBoard extends IClickable { public void setStones(Map stones); - - public IEvent1 getClickEvent(); } diff --git a/src/jrummikub/view/IClickable.java b/src/jrummikub/view/IClickable.java new file mode 100644 index 0000000..fe381a9 --- /dev/null +++ b/src/jrummikub/view/IClickable.java @@ -0,0 +1,10 @@ +package jrummikub.view; + +import jrummikub.model.Position; +import jrummikub.util.IEvent2; + +public interface IClickable { + public IEvent2 getClickEvent(); + public IEvent2 getRangeClickEvent(); + public IEvent2 getSetClickEvent(); +} diff --git a/src/jrummikub/view/ITable.java b/src/jrummikub/view/ITable.java index e10f6f9..2630c7f 100644 --- a/src/jrummikub/view/ITable.java +++ b/src/jrummikub/view/ITable.java @@ -4,14 +4,11 @@ import java.util.Map; import jrummikub.model.Position; import jrummikub.model.StoneSet; -import jrummikub.util.IEvent1; -public interface ITable { +public interface ITable extends IClickable { public void setLeftPlayerName(String playerName); public void setTopPlayerName(String playerName); public void setRightPlayerName(String playerName); 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 555819e..a566312 100644 --- a/src/jrummikub/view/impl/Board.java +++ b/src/jrummikub/view/impl/Board.java @@ -5,38 +5,30 @@ 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.Collection; import java.util.Collections; import java.util.Map; import javax.swing.ImageIcon; -import javax.swing.JPanel; import jrummikub.model.Position; import jrummikub.model.Stone; -import jrummikub.util.Event1; -import jrummikub.util.IEvent1; import jrummikub.view.IBoard; @SuppressWarnings("serial") -public class Board extends JPanel implements IBoard { - private final static ImageIcon background = new ImageIcon(Board.class.getResource("/jrummikub/resource/wood.png")); - +public class Board extends StonePanel implements IBoard { + private final static ImageIcon BACKGROUND = new ImageIcon(Board.class.getResource("/jrummikub/resource/wood.png")); + private final static float DEFAULT_SCALE = StonePainter.BOARD_SCALE; + private Map stones = Collections.emptyMap(); private Collection selectedStones = Collections.emptyList(); - - private StonePainter stonePainter = new StonePainter(StonePainter.BOARD_SCALE); - - private Event1 clickEvent = new Event1(); Board() { - super(true); // set double buffered + super(DEFAULT_SCALE); setBorder(new CustomBorder(Color.DARK_GRAY, 0, 1, 0, 1)); - addMouseListener(new MouseAdapter() { + /*addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { Insets insets = getInsets(); @@ -44,12 +36,7 @@ public class Board extends JPanel implements IBoard { clickEvent.fire(stonePainter.calculatePosition(e.getX() - insets.left, e.getY() - insets.top)); } - }); - } - - @Override - public IEvent1 getClickEvent() { - return clickEvent; + });*/ } @Override @@ -58,18 +45,18 @@ public class Board extends JPanel implements IBoard { 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); - for(int xpos = 0; xpos < width; xpos += background.getIconWidth()) { - background.paintIcon(this, g, xpos, 0); + for(int xpos = 0; xpos < width; xpos += BACKGROUND.getIconWidth()) { + BACKGROUND.paintIcon(this, g, xpos, 0); } - for(int xpos = -32; xpos < width; xpos += background.getIconWidth()) { - background.paintIcon(this, g, xpos, 75); + for(int xpos = -32; xpos < width; xpos += BACKGROUND.getIconWidth()) { + BACKGROUND.paintIcon(this, g, xpos, 75); } g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); for (Map.Entry entry : stones.entrySet()) { - stonePainter.paintStone(g, entry.getKey(), entry.getValue(), + getStonePainter().paintStone(g, entry.getKey(), entry.getValue(), selectedStones.contains(entry.getKey())); } } diff --git a/src/jrummikub/view/impl/StonePanel.java b/src/jrummikub/view/impl/StonePanel.java new file mode 100644 index 0000000..9bcffae --- /dev/null +++ b/src/jrummikub/view/impl/StonePanel.java @@ -0,0 +1,62 @@ +package jrummikub.view.impl; + +import java.awt.Insets; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; + +import javax.swing.JPanel; + +import jrummikub.model.Position; +import jrummikub.util.Event2; +import jrummikub.view.IClickable; + +@SuppressWarnings("serial") +abstract class StonePanel extends JPanel implements IClickable { + private StonePainter stonePainter; + + private Event2 clickEvent = new Event2(); + private Event2 rangeClickEvent = new Event2(); + private Event2 setClickEvent = new Event2(); + + protected StonePainter getStonePainter() { + return stonePainter; + } + + public StonePanel(float scale) { + super(true); // Set double buffered + + stonePainter = new StonePainter(scale); + + addMouseListener(new MouseAdapter() { + @Override + public void mouseClicked(MouseEvent e) { + Insets insets = getInsets(); + Event2 event = clickEvent; + + if (e.isShiftDown()) + event = rangeClickEvent; + else if (e.getClickCount() >= 2) + event = setClickEvent; + + event.fire(stonePainter.calculatePosition(e.getX() - insets.left, + e.getY() - insets.top), e.isControlDown()); + } + }); + } + + @Override + public Event2 getClickEvent() { + return clickEvent; + } + + @Override + public Event2 getRangeClickEvent() { + return rangeClickEvent; + } + + @Override + public Event2 getSetClickEvent() { + return setClickEvent; + } + +} diff --git a/src/jrummikub/view/impl/Table.java b/src/jrummikub/view/impl/Table.java index a42a2a9..4a5589b 100644 --- a/src/jrummikub/view/impl/Table.java +++ b/src/jrummikub/view/impl/Table.java @@ -4,10 +4,7 @@ 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.Collection; import java.util.Collections; import java.util.Map; @@ -19,26 +16,22 @@ 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 { +public 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 JLabel leftPlayerLabel, topPlayerLabel, rightPlayerLabel; private JPanel innerPanel; - - private StonePainter stonePainter = new StonePainter(1); + private StonePainter selectedStonePainter = new StonePainter(1.2f); private Map stoneSets = Collections.emptyMap(); private Collection selectedStones = Collections.emptyList(); - private Event1 clickEvent = new Event1(); - @Override public void setLeftPlayerName(String playerName) { @@ -65,14 +58,10 @@ public class Table extends JPanel implements ITable { selectedStones = stones; repaint(); } - - @Override - public IEvent1 getClickEvent() { - return clickEvent; - } Table() { - super(true); // set double buffered + super(DEFAULT_SCALE); + setLayout(new BorderLayout()); leftPlayerLabel = new JLabel(); @@ -92,23 +81,13 @@ public class Table extends JPanel implements ITable { 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()), + getStonePainter().paintStone(g, stone, new Position(x, pos.getY()), selectedStones.contains(stone)); x++; } -- cgit v1.2.3