Add display for selected stones above board

git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@41 72836036-5685-4462-b002-a69064685172
This commit is contained in:
Matthias Schiffer 2011-04-30 23:17:48 +02:00
parent 28f1ecd987
commit d262d91b07
9 changed files with 59 additions and 26 deletions

View file

@ -1,7 +1,7 @@
package jrummikub; package jrummikub;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -65,7 +65,6 @@ public class JRummikub {
stones.put(new Stone(0, StoneColor.BLACK, true), new Position(3.5f, 0)); stones.put(new Stone(0, StoneColor.BLACK, true), new Position(3.5f, 0));
view.getPlayerPanel().getBoard().setStones(stones); view.getPlayerPanel().getBoard().setStones(stones);
view.getPlayerPanel().getBoard().setHighlightedStones(Collections.singleton(stoneJoker));
view.getPlayerPanel().getBoard().getClickEvent().add(new IListener1<Position>(){ view.getPlayerPanel().getBoard().getClickEvent().add(new IListener1<Position>(){
@ -99,8 +98,8 @@ public class JRummikub {
stoneSets.put(new StoneSet(stoneList), new Position(3.5f, 4)); stoneSets.put(new StoneSet(stoneList), new Position(3.5f, 4));
view.getTable().setStoneSets(stoneSets); view.getTable().setStoneSets(stoneSets);
view.getTable().setHighlightedStones(Collections.singleton(stone8));
view.setSelectedStones(Arrays.asList(stoneJoker, stone8));
} }
} }

View file

@ -1,6 +1,5 @@
package jrummikub.view; package jrummikub.view;
import java.util.Collection;
import java.util.Map; import java.util.Map;
import jrummikub.model.Position; import jrummikub.model.Position;
@ -9,7 +8,6 @@ import jrummikub.util.IEvent1;
public interface IBoard { public interface IBoard {
public void setStones(Map<Stone, Position> stones); public void setStones(Map<Stone, Position> stones);
public void setHighlightedStones(Collection<Stone> stones);
public IEvent1<Position> getClickEvent(); public IEvent1<Position> getClickEvent();
} }

View file

@ -1,10 +1,8 @@
package jrummikub.view; package jrummikub.view;
import java.util.Collection;
import java.util.Map; import java.util.Map;
import jrummikub.model.Position; import jrummikub.model.Position;
import jrummikub.model.Stone;
import jrummikub.model.StoneSet; import jrummikub.model.StoneSet;
import jrummikub.util.IEvent1; import jrummikub.util.IEvent1;
@ -14,7 +12,6 @@ public interface ITable {
public void setRightPlayerName(String playerName); public void setRightPlayerName(String playerName);
public void setStoneSets(Map<StoneSet, Position> stoneSets); public void setStoneSets(Map<StoneSet, Position> stoneSets);
public void setHighlightedStones(Collection<Stone> stones);
public IEvent1<Position> getClickEvent(); public IEvent1<Position> getClickEvent();
} }

View file

@ -1,7 +1,10 @@
package jrummikub.view; package jrummikub.view;
public interface IView { import java.util.Collection;
import jrummikub.model.Stone;
public interface IView {
/** /**
* @return the table * @return the table
*/ */
@ -12,4 +15,5 @@ public interface IView {
*/ */
public IPlayerPanel getPlayerPanel(); public IPlayerPanel getPlayerPanel();
public void setSelectedStones(Collection<Stone> stones);
} }

View file

@ -25,7 +25,7 @@ public class Board extends JPanel implements IBoard {
private final static ImageIcon background = new ImageIcon(Board.class.getResource("/jrummikub/resource/wood.png")); private final static ImageIcon background = new ImageIcon(Board.class.getResource("/jrummikub/resource/wood.png"));
private Map<Stone, Position> stones = Collections.emptyMap(); private Map<Stone, Position> stones = Collections.emptyMap();
private Collection<Stone> highlightedStones = Collections.emptyList(); private Collection<Stone> selectedStones = Collections.emptyList();
private StonePainter stonePainter = new StonePainter(StonePainter.BOARD_SCALE); private StonePainter stonePainter = new StonePainter(StonePainter.BOARD_SCALE);
@ -70,7 +70,7 @@ public class Board extends JPanel implements IBoard {
for (Map.Entry<Stone, Position> entry : stones.entrySet()) { for (Map.Entry<Stone, Position> entry : stones.entrySet()) {
stonePainter.paintStone(g, entry.getKey(), entry.getValue(), stonePainter.paintStone(g, entry.getKey(), entry.getValue(),
highlightedStones.contains(entry.getKey())); selectedStones.contains(entry.getKey()));
} }
} }
@ -80,9 +80,8 @@ public class Board extends JPanel implements IBoard {
repaint(); repaint();
} }
@Override public void setSelectedStones(Collection<Stone> stones) {
public void setHighlightedStones(Collection<Stone> stones) { selectedStones = stones;
highlightedStones = stones;
repaint(); repaint();
} }
} }

View file

@ -16,7 +16,6 @@ import javax.swing.JProgressBar;
import jrummikub.util.Event; import jrummikub.util.Event;
import jrummikub.util.IEvent; import jrummikub.util.IEvent;
import jrummikub.view.IBoard;
import jrummikub.view.IPlayerPanel; import jrummikub.view.IPlayerPanel;
@SuppressWarnings("serial") @SuppressWarnings("serial")
@ -37,7 +36,7 @@ public class PlayerPanel extends JPanel implements IPlayerPanel {
@Override @Override
public IBoard getBoard() { public Board getBoard() {
return board; return board;
} }

View file

@ -73,12 +73,20 @@ class StonePainter {
* @return position in grid coordinates * @return position in grid coordinates
*/ */
public Position calculatePosition(int x, int y){ public Position calculatePosition(int x, int y){
float width = even(DEFAULT_WIDTH*scale); float width = getStoneWidth();
float height = (int)(DEFAULT_WIDTH*scale/ASPECT_RATIO); float height = getStoneHeight();
return new Position(x/width, y/height); return new Position(x/width, y/height);
} }
public int getStoneWidth() {
return even(DEFAULT_WIDTH*scale);
}
public int getStoneHeight() {
return (int)(DEFAULT_WIDTH*scale/ASPECT_RATIO);
}
StonePainter(float scale) { StonePainter(float scale) {
this.scale = scale; this.scale = scale;
} }
@ -199,8 +207,8 @@ class StonePainter {
public void paintStone(Graphics2D g, Stone stone, Position p, boolean highlighted) { public void paintStone(Graphics2D g, Stone stone, Position p, boolean highlighted) {
Color background = highlighted ? HIGHLIGHTED_COLOR : BACKGROUND_COLOR; Color background = highlighted ? HIGHLIGHTED_COLOR : BACKGROUND_COLOR;
int width = even(DEFAULT_WIDTH*scale); int width = getStoneWidth();
int height = (int)(DEFAULT_WIDTH*scale/ASPECT_RATIO); int height = getStoneHeight();
int x = (int)(p.getX()*width); int x = (int)(p.getX()*width);
int y = (int)(p.getY()*height); int y = (int)(p.getY()*height);

View file

@ -32,9 +32,10 @@ public class Table extends JPanel implements ITable {
private JPanel innerPanel; private JPanel innerPanel;
private StonePainter stonePainter = new StonePainter(1); private StonePainter stonePainter = new StonePainter(1);
private StonePainter selectedStonePainter = new StonePainter(1.2f);
private Map<StoneSet, Position> stoneSets = Collections.emptyMap(); private Map<StoneSet, Position> stoneSets = Collections.emptyMap();
private Collection<Stone> highlightedStones = Collections.emptyList(); private Collection<Stone> selectedStones = Collections.emptyList();
private Event1<Position> clickEvent = new Event1<Position>(); private Event1<Position> clickEvent = new Event1<Position>();
@ -60,9 +61,8 @@ public class Table extends JPanel implements ITable {
repaint(); repaint();
} }
@Override public void setSelectedStones(Collection<Stone> stones) {
public void setHighlightedStones(Collection<Stone> stones) { selectedStones = stones;
highlightedStones = stones;
repaint(); repaint();
} }
@ -109,7 +109,7 @@ public class Table extends JPanel implements ITable {
for (Stone stone : stoneSet) { for (Stone stone : stoneSet) {
stonePainter.paintStone(g, stone, new Position(x, pos.getY()), stonePainter.paintStone(g, stone, new Position(x, pos.getY()),
highlightedStones.contains(stone)); selectedStones.contains(stone));
x++; x++;
} }
} }
@ -130,5 +130,26 @@ public class Table extends JPanel implements ITable {
for (Map.Entry<StoneSet, Position> stoneSet : stoneSets.entrySet()) { for (Map.Entry<StoneSet, Position> stoneSet : stoneSets.entrySet()) {
paintStoneSet(g, stoneSet.getKey(), stoneSet.getValue()); 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++;
}
}
} }
} }

View file

@ -3,9 +3,11 @@ package jrummikub.view.impl;
import java.awt.BorderLayout; import java.awt.BorderLayout;
import java.awt.Color; import java.awt.Color;
import java.awt.Dimension; import java.awt.Dimension;
import java.util.Collection;
import javax.swing.JFrame; import javax.swing.JFrame;
import jrummikub.model.Stone;
import jrummikub.view.IPlayerPanel; import jrummikub.view.IPlayerPanel;
import jrummikub.view.ITable; import jrummikub.view.ITable;
import jrummikub.view.IView; import jrummikub.view.IView;
@ -45,4 +47,10 @@ public class View extends JFrame implements IView {
setVisible(true); setVisible(true);
} }
@Override
public void setSelectedStones(Collection<Stone> stones) {
table.setSelectedStones(stones);
playerPanel.getBoard().setSelectedStones(stones);
}
} }