Translate table origin

git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@98 72836036-5685-4462-b002-a69064685172
This commit is contained in:
Matthias Schiffer 2011-05-03 21:34:59 +02:00
parent 2aeedc6241
commit 51f70cafe3
3 changed files with 223 additions and 177 deletions

View file

@ -19,191 +19,190 @@ import jrummikub.view.IView;
public class JRummikub { public class JRummikub {
/** /**
* @param args * @param args
*/ */
public static void main(String[] args) { public static void main(String[] args) {
String nativeLF = UIManager.getSystemLookAndFeelClassName(); String nativeLF = UIManager.getSystemLookAndFeelClassName();
try { try {
UIManager.setLookAndFeel(nativeLF); UIManager.setLookAndFeel(nativeLF);
} catch (Exception e) { } catch (Exception e) {
} }
IView view = new jrummikub.view.impl.View(); IView view = new jrummikub.view.impl.View();
view.getPlayerPanel().setCurrentPlayerName("Player 1"); view.getPlayerPanel().setCurrentPlayerName("Player 1");
view.getPlayerPanel().setTimeLeft(42); view.getPlayerPanel().setTimeLeft(42);
view.getTablePanel().setLeftPlayerName("Player 2"); view.getTablePanel().setLeftPlayerName("Player 2");
view.getTablePanel().setTopPlayerName("Player 3"); view.getTablePanel().setTopPlayerName("Player 3");
view.getTablePanel().setRightPlayerName("Player 4"); view.getTablePanel().setRightPlayerName("Player 4");
view.getPlayerPanel().getSortByGroupsEvent().add(new IListener() { view.getPlayerPanel().getSortByGroupsEvent().add(new IListener() {
@Override @Override
public void handle() { public void handle() {
System.out.println("'Sort by groups' fired"); System.out.println("'Sort by groups' fired");
} }
}); });
view.getPlayerPanel().getSortByRunsEvent().add(new IListener() { view.getPlayerPanel().getSortByRunsEvent().add(new IListener() {
@Override @Override
public void handle() { public void handle() {
System.out.println("'Sort by runs' fired"); System.out.println("'Sort by runs' fired");
} }
}); });
view.getPlayerPanel().getEndTurnEvent().add(new IListener() { view.getPlayerPanel().getEndTurnEvent().add(new IListener() {
@Override @Override
public void handle() { public void handle() {
System.out.println("'End turn' fired"); System.out.println("'End turn' fired");
} }
}); });
// stones on the board // stones on the board
Map<Stone, Position> stones = new HashMap<Stone, Position>(); Map<Stone, Position> stones = new HashMap<Stone, Position>();
stones.put(new Stone(1, StoneColor.ORANGE), new Position(0, 0)); stones.put(new Stone(1, StoneColor.ORANGE), new Position(0, 0));
stones.put(new Stone(10, StoneColor.BLUE), new Position(1, 0)); stones.put(new Stone(10, StoneColor.BLUE), new Position(1, 0));
stones.put(new Stone(9, StoneColor.RED), new Position(0.5f, 1)); stones.put(new Stone(9, StoneColor.RED), new Position(0.5f, 1));
stones.put(new Stone(7, StoneColor.BLACK), new Position(1.75f, 1)); stones.put(new Stone(7, StoneColor.BLACK), new Position(1.75f, 1));
Stone stoneJoker = new Stone(StoneColor.RED); Stone stoneJoker = new Stone(StoneColor.RED);
stones.put(stoneJoker, new Position(2.5f, 0)); stones.put(stoneJoker, new Position(2.5f, 0));
stones.put(new Stone(StoneColor.BLACK), new Position(3.5f, 0)); stones.put(new Stone(StoneColor.BLACK), new Position(3.5f, 0));
view.getPlayerPanel().getHandPanel().setStones(stones); view.getPlayerPanel().getHandPanel().setStones(stones);
view.getPlayerPanel().getHandPanel().getClickEvent() view.getPlayerPanel().getHandPanel().getClickEvent()
.add(new IListener1<Position>() { .add(new IListener1<Position>() {
@Override @Override
public void handle(Position p) { public void handle(Position p) {
System.out.println("Hand clicked at " + p); System.out.println("Hand clicked at " + p);
} }
}); });
view.getPlayerPanel().getHandPanel().getStoneClickEvent() view.getPlayerPanel().getHandPanel().getStoneClickEvent()
.add(new IListener2<Stone, Boolean>() { .add(new IListener2<Stone, Boolean>() {
@Override @Override
public void handle(Stone s, Boolean collect) { public void handle(Stone s, Boolean collect) {
System.out.println("Hand clicked on " + s System.out.println("Hand clicked on " + s
+ (collect ? ", collect" : "")); + (collect ? ", collect" : ""));
} }
}); });
view.getPlayerPanel().getHandPanel().getRangeClickEvent() view.getPlayerPanel().getHandPanel().getRangeClickEvent()
.add(new IListener2<Stone, Boolean>() { .add(new IListener2<Stone, Boolean>() {
@Override @Override
public void handle(Stone s, Boolean collect) { public void handle(Stone s, Boolean collect) {
System.out.println("Hand range-clicked on " + s System.out.println("Hand range-clicked on " + s
+ (collect ? ", collect" : "")); + (collect ? ", collect" : ""));
} }
}); });
view.getPlayerPanel().getHandPanel().getSetClickEvent() view.getPlayerPanel().getHandPanel().getSetClickEvent()
.add(new IListener2<Stone, Boolean>() { .add(new IListener2<Stone, Boolean>() {
@Override @Override
public void handle(Stone s, Boolean collect) { public void handle(Stone s, Boolean collect) {
System.out.println("Hand set-clicked at " + s System.out.println("Hand set-clicked at " + s
+ (collect ? ", collect" : "")); + (collect ? ", collect" : ""));
} }
}); });
view.getTablePanel().getClickEvent() view.getTablePanel().getClickEvent().add(new IListener1<Position>() {
.add(new IListener1<Position>() { @Override
@Override public void handle(Position p) {
public void handle(Position p) { System.out.println("Table clicked at " + p);
System.out.println("Table clicked at " + p); }
} });
}); view.getTablePanel().getStoneClickEvent()
view.getTablePanel().getStoneClickEvent() .add(new IListener2<Stone, Boolean>() {
.add(new IListener2<Stone, Boolean>() { @Override
@Override public void handle(Stone s, Boolean collect) {
public void handle(Stone s, Boolean collect) { System.out.println("Table clicked on " + s
System.out.println("Table clicked on " + s + (collect ? ", collect" : ""));
+ (collect ? ", collect" : ""));
} }
}); });
view.getTablePanel().getRangeClickEvent() view.getTablePanel().getRangeClickEvent()
.add(new IListener2<Stone, Boolean>() { .add(new IListener2<Stone, Boolean>() {
@Override @Override
public void handle(Stone s, Boolean collect) { public void handle(Stone s, Boolean collect) {
System.out.println("Table range-clicked on " + s System.out.println("Table range-clicked on " + s
+ (collect ? ", collect" : "")); + (collect ? ", collect" : ""));
} }
}); });
view.getTablePanel().getSetClickEvent() view.getTablePanel().getSetClickEvent()
.add(new IListener2<Stone, Boolean>() { .add(new IListener2<Stone, Boolean>() {
@Override @Override
public void handle(Stone s, Boolean collect) { public void handle(Stone s, Boolean collect) {
System.out.println("Table set-clicked at " + s System.out.println("Table set-clicked at " + s
+ (collect ? ", collect" : "")); + (collect ? ", collect" : ""));
} }
}); });
view.getTablePanel().getLeftConnectorClickEvent() view.getTablePanel().getLeftConnectorClickEvent()
.add(new IListener1<StoneSet>() { .add(new IListener1<StoneSet>() {
@Override @Override
public void handle(StoneSet s) { public void handle(StoneSet s) {
System.out.println("Left connector clicked on " + s); System.out.println("Left connector clicked on " + s);
} }
}); });
view.getTablePanel().getRightConnectorClickEvent() view.getTablePanel().getRightConnectorClickEvent()
.add(new IListener1<StoneSet>() { .add(new IListener1<StoneSet>() {
@Override @Override
public void handle(StoneSet s) { public void handle(StoneSet s) {
System.out.println("Right connector clicked on " + s); System.out.println("Right connector clicked on " + s);
} }
}); });
view.getTablePanel().getStoneCollectionPanel().getStoneClickEvent() view.getTablePanel().getStoneCollectionPanel().getStoneClickEvent()
.add(new IListener2<Stone, Boolean>() { .add(new IListener2<Stone, Boolean>() {
@Override @Override
public void handle(Stone s, Boolean collect) { public void handle(Stone s, Boolean collect) {
System.out.println("Collection clicked on " + s System.out.println("Collection clicked on " + s
+ (collect ? ", collect" : "")); + (collect ? ", collect" : ""));
} }
}); });
view.getTablePanel().getStoneCollectionPanel().getRangeClickEvent() view.getTablePanel().getStoneCollectionPanel().getRangeClickEvent()
.add(new IListener2<Stone, Boolean>() { .add(new IListener2<Stone, Boolean>() {
@Override @Override
public void handle(Stone s, Boolean collect) { public void handle(Stone s, Boolean collect) {
System.out.println("Collection range-clicked on " + s System.out.println("Collection range-clicked on " + s
+ (collect ? ", collect" : "")); + (collect ? ", collect" : ""));
} }
}); });
view.getTablePanel().getStoneCollectionPanel().getSetClickEvent() view.getTablePanel().getStoneCollectionPanel().getSetClickEvent()
.add(new IListener2<Stone, Boolean>() { .add(new IListener2<Stone, Boolean>() {
@Override @Override
public void handle(Stone s, Boolean collect) { public void handle(Stone s, Boolean collect) {
System.out.println("Collection set-clicked at " + s System.out.println("Collection set-clicked at " + s
+ (collect ? ", collect" : "")); + (collect ? ", collect" : ""));
} }
}); });
// stoneSets on the table // stoneSets on the table
Map<StoneSet, Position> stoneSets = new HashMap<StoneSet, Position>(); Map<StoneSet, Position> stoneSets = new HashMap<StoneSet, Position>();
stoneSets.put(new StoneSet(new Stone(5, StoneColor.ORANGE)), new Position( stoneSets.put(new StoneSet(new Stone(5, StoneColor.ORANGE)), new Position(
0.5f, 1)); 0.5f, 1));
List<Stone> stoneList = new ArrayList<Stone>(); List<Stone> stoneList = new ArrayList<Stone>();
stoneList.add(new Stone(7, StoneColor.BLACK)); stoneList.add(new Stone(7, StoneColor.BLACK));
Stone stone8 = new Stone(8, StoneColor.BLACK); Stone stone8 = new Stone(8, StoneColor.BLACK);
stoneList.add(stone8); stoneList.add(stone8);
stoneList.add(new Stone(9, StoneColor.BLACK)); stoneList.add(new Stone(9, StoneColor.BLACK));
stoneList.add(new Stone(10, StoneColor.BLACK)); stoneList.add(new Stone(10, StoneColor.BLACK));
stoneSets.put(new StoneSet(stoneList), new Position(3.5f, 4)); stoneSets.put(new StoneSet(stoneList), new Position(3.5f, 4));
view.getTablePanel().setStoneSets(stoneSets); view.getTablePanel().setStoneSets(stoneSets);
view.setSelectedStones(Arrays.asList(stoneJoker, stone8)); view.setSelectedStones(Arrays.asList(stoneJoker, stone8));
} }
} }

View file

@ -13,6 +13,7 @@ import jrummikub.model.Position;
import jrummikub.model.Stone; import jrummikub.model.Stone;
import jrummikub.util.Event1; import jrummikub.util.Event1;
import jrummikub.util.Event2; import jrummikub.util.Event2;
import jrummikub.util.Pair;
import jrummikub.view.IClickable; import jrummikub.view.IClickable;
import jrummikub.view.IStonePanel; import jrummikub.view.IStonePanel;
@ -60,8 +61,9 @@ abstract class AbstractStonePanel extends JPanel implements IStonePanel,
@Override @Override
public void mouseClicked(MouseEvent e) { public void mouseClicked(MouseEvent e) {
Insets insets = getInsets(); Insets insets = getInsets();
Position pos = stonePainter.calculatePosition(e.getX() - insets.left, Pair<Integer, Integer> trans = getTranslation();
e.getY() - insets.top); Position pos = stonePainter.calculatePosition(e.getX() - insets.left
- trans.getFirst(), e.getY() - insets.top - trans.getSecond());
Stone stone = getStoneAt(pos); Stone stone = getStoneAt(pos);
if (stone == null) { if (stone == null) {
@ -85,7 +87,9 @@ abstract class AbstractStonePanel extends JPanel implements IStonePanel,
/** /**
* *Overwrite this method* to signal if special zone was clicked * *Overwrite this method* to signal if special zone was clicked
* @param pos the clicked position *
* @param pos
* the clicked position
* *
* @return special zone clicked * @return special zone clicked
*/ */
@ -116,6 +120,15 @@ abstract class AbstractStonePanel extends JPanel implements IStonePanel,
this.stones = stones; this.stones = stones;
} }
/**
* Returns the translation in pixels the stones in this panel are painted with
*
* @return the translation
*/
protected Pair<Integer, Integer> getTranslation() {
return new Pair<Integer, Integer>(0, 0);
}
/** /**
* Returns the list of stones and positions currently set * Returns the list of stones and positions currently set
* *

View file

@ -8,6 +8,7 @@ import java.awt.RenderingHints;
import java.awt.event.ComponentAdapter; import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent; import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener; import java.awt.event.ComponentListener;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D; import java.awt.geom.Rectangle2D;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
@ -21,6 +22,7 @@ import jrummikub.model.Position;
import jrummikub.model.Stone; import jrummikub.model.Stone;
import jrummikub.model.StoneSet; import jrummikub.model.StoneSet;
import jrummikub.util.Event1; import jrummikub.util.Event1;
import jrummikub.util.Pair;
import jrummikub.view.IStoneCollectionPanel; import jrummikub.view.IStoneCollectionPanel;
import jrummikub.view.ITablePanel; import jrummikub.view.ITablePanel;
@ -34,6 +36,8 @@ class TablePanel extends AbstractStonePanel implements ITablePanel {
private final static float MIN_VISIBLE_WIDTH = 15; private final static float MIN_VISIBLE_WIDTH = 15;
private final static float MIN_VISIBLE_HEIGHT = 7.5f; private final static float MIN_VISIBLE_HEIGHT = 7.5f;
private final static float HORIZONTAL_MARGIN = 1.5f;
private final static float VERTICAL_MARGIN = 1;
private final static float CONNECTOR_WIDTH = 0.25f; private final static float CONNECTOR_WIDTH = 0.25f;
private final int COLLECTION_GAP = 5; private final int COLLECTION_GAP = 5;
@ -107,18 +111,7 @@ class TablePanel extends AbstractStonePanel implements ITablePanel {
repaint(); repaint();
} }
private void rescale() { private Rectangle2D calculateTableExtent() {
Insets insets = getInsets();
int x = insets.left, y = insets.top, width = getWidth() - insets.left
- insets.right, height = getHeight() - insets.top - insets.bottom;
leftPlayerLabel.setBounds(x, y, width, height);
topPlayerLabel.setBounds(x, y, width, height);
rightPlayerLabel.setBounds(x, y, width, height);
stoneCollection.setLocation(x + width / 2 - stoneCollection.getWidth() / 2,
y + height - stoneCollection.getHeight() - COLLECTION_GAP);
float minx = -MIN_VISIBLE_WIDTH / 2, maxx = MIN_VISIBLE_WIDTH / 2; float minx = -MIN_VISIBLE_WIDTH / 2, maxx = MIN_VISIBLE_WIDTH / 2;
float miny = -MIN_VISIBLE_HEIGHT / 2, maxy = MIN_VISIBLE_HEIGHT / 2; float miny = -MIN_VISIBLE_HEIGHT / 2, maxy = MIN_VISIBLE_HEIGHT / 2;
@ -139,8 +132,29 @@ class TablePanel extends AbstractStonePanel implements ITablePanel {
maxy = p.getY() + 1; maxy = p.getY() + 1;
} }
float widthScale = width / (maxx - minx) * StonePainter.WIDTH_SCALE; return new Rectangle2D.Float(minx - HORIZONTAL_MARGIN, miny
float heightScale = height / (maxy - miny) * StonePainter.HEIGHT_SCALE; - VERTICAL_MARGIN, maxx - minx + 2 * HORIZONTAL_MARGIN, maxy - miny + 2
* VERTICAL_MARGIN);
}
private void rescale() {
Insets insets = getInsets();
int x = insets.left, y = insets.top, width = getWidth() - insets.left
- insets.right, height = getHeight() - insets.top - insets.bottom;
leftPlayerLabel.setBounds(x, y, width, height);
topPlayerLabel.setBounds(x, y, width, height);
rightPlayerLabel.setBounds(x, y, width, height);
stoneCollection.setLocation(x + width / 2 - stoneCollection.getWidth() / 2,
y + height - stoneCollection.getHeight() - COLLECTION_GAP);
Rectangle2D extent = calculateTableExtent();
float widthScale = width / (float) extent.getWidth()
* StonePainter.WIDTH_SCALE;
float heightScale = height / (float) extent.getHeight()
* StonePainter.HEIGHT_SCALE;
getStonePainter().setScale(Math.min(widthScale, heightScale)); getStonePainter().setScale(Math.min(widthScale, heightScale));
@ -211,6 +225,19 @@ class TablePanel extends AbstractStonePanel implements ITablePanel {
return false; return false;
} }
@Override
protected Pair<Integer, Integer> getTranslation() {
Insets insets = getInsets();
int width = getWidth() - insets.left - insets.right, height = getHeight()
- insets.top - insets.bottom;
int stoneWidth = getStonePainter().getStoneWidth(), stoneHeight = getStonePainter()
.getStoneHeight();
Rectangle2D extent = calculateTableExtent();
return new Pair<Integer, Integer>((int) (width / 2 - extent.getCenterX()
* stoneWidth), (int) (height / 2 - extent.getCenterY() * stoneHeight));
}
private void paintStoneSet(Graphics2D g, StoneSet stoneSet, Position pos) { private void paintStoneSet(Graphics2D g, StoneSet stoneSet, Position pos) {
float x = pos.getX(); float x = pos.getX();
int width = getStonePainter().getStoneWidth(), height = getStonePainter() int width = getStonePainter().getStoneWidth(), height = getStonePainter()
@ -241,11 +268,18 @@ class TablePanel extends AbstractStonePanel implements ITablePanel {
} }
} }
AffineTransform oldTransform = g.getTransform();
Pair<Integer, Integer> translation = getTranslation();
g.translate(translation.getFirst(), translation.getSecond());
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON); RenderingHints.VALUE_ANTIALIAS_ON);
for (Map.Entry<StoneSet, Position> entry : stoneSets.entrySet()) { for (Map.Entry<StoneSet, Position> entry : stoneSets.entrySet()) {
paintStoneSet(g, entry.getKey(), entry.getValue()); paintStoneSet(g, entry.getKey(), entry.getValue());
} }
g.setTransform(oldTransform);
} }
} }