Fix formatting

git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@52 72836036-5685-4462-b002-a69064685172
This commit is contained in:
Matthias Schiffer 2011-05-01 19:14:55 +02:00
parent 5436407515
commit b2dbfcc317
22 changed files with 483 additions and 427 deletions

View file

@ -41,19 +41,22 @@ public class JRummikub {
@Override
public void fire() {
System.out.println("'Sort by number' fired");
}});
}
});
view.getPlayerPanel().getSortByColorEvent().add(new IListener() {
@Override
public void fire() {
System.out.println("'Sort by color' fired");
}});
}
});
view.getPlayerPanel().getEndTurnEvent().add(new IListener() {
@Override
public void fire() {
System.out.println("'End turn' fired");
}});
}
});
//stones on the board
// stones on the board
Map<Stone, Position> stones = new HashMap<Stone, Position>();
stones.put(new Stone(1, StoneColor.ORANGE, false), new Position(0, 0));
stones.put(new Stone(10, StoneColor.BLUE, false), new Position(1, 0));
@ -66,48 +69,65 @@ public class JRummikub {
view.getPlayerPanel().getBoard().setStones(stones);
view.getPlayerPanel().getBoard().getClickEvent().add(new IListener2<Position, Boolean>(){
view.getPlayerPanel().getBoard().getClickEvent()
.add(new IListener2<Position, Boolean>() {
@Override
public void fire(Position p, Boolean collect) {
System.out.println("Board clicked at " + p
+ (collect ? ", collect" : ""));
}
});
view.getPlayerPanel().getBoard().getRangeClickEvent()
.add(new IListener2<Position, Boolean>() {
@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<Position, Boolean>() {
@Override
public void fire(Position p, Boolean collect) {
System.out.println("Board set-clicked at " + p
+ (collect ? ", collect" : ""));
}
});
view.getTable().getClickEvent().add(new IListener2<Position, Boolean>() {
@Override
public void fire(Position p, Boolean collect) {
System.out.println("Board clicked at "+p+(collect?", collect":""));
System.out.println("Table clicked at " + p
+ (collect ? ", collect" : ""));
}});
view.getPlayerPanel().getBoard().getRangeClickEvent().add(new IListener2<Position, Boolean>(){
}
});
view.getTable().getRangeClickEvent()
.add(new IListener2<Position, Boolean>() {
@Override
public void fire(Position p, Boolean collect) {
System.out.println("Table range-clicked at " + p
+ (collect ? ", collect" : ""));
}
});
view.getTable().getSetClickEvent().add(new IListener2<Position, Boolean>() {
@Override
public void fire(Position p, Boolean collect) {
System.out.println("Board range-clicked at "+p+(collect?", collect":""));
System.out.println("Table set-clicked at " + p
+ (collect ? ", collect" : ""));
}});
view.getPlayerPanel().getBoard().getSetClickEvent().add(new IListener2<Position, Boolean>(){
@Override
public void fire(Position p, Boolean collect) {
System.out.println("Board set-clicked at "+p+(collect?", collect":""));
}
});
}});
view.getTable().getClickEvent().add(new IListener2<Position, Boolean>(){
@Override
public void fire(Position p, Boolean collect) {
System.out.println("Table clicked at "+p+(collect?", collect":""));
}});
view.getTable().getRangeClickEvent().add(new IListener2<Position, Boolean>(){
@Override
public void fire(Position p, Boolean collect) {
System.out.println("Table range-clicked at "+p+(collect?", collect":""));
}});
view.getTable().getSetClickEvent().add(new IListener2<Position, Boolean>(){
@Override
public void fire(Position p, Boolean collect) {
System.out.println("Table set-clicked at "+p+(collect?", collect":""));
}});
//stoneSets on the table
// stoneSets on the table
Map<StoneSet, Position> stoneSets = new HashMap<StoneSet, Position>();
stoneSets.put(new StoneSet(new Stone(5, StoneColor.ORANGE, false)), new Position(0.5f, 1));
stoneSets.put(new StoneSet(new Stone(5, StoneColor.ORANGE, false)),
new Position(0.5f, 1));
List<Stone> stoneList = new ArrayList<Stone>();

View file

@ -3,7 +3,7 @@ package jrummikub.util;
import java.util.HashSet;
public class Event2<T1, T2> implements IEvent2<T1, T2> {
private HashSet<IListener2<T1, T2>> listeners = new HashSet<IListener2<T1, T2>>();
private HashSet<IListener2<T1, T2>> listeners = new HashSet<IListener2<T1, T2>>();
@Override
public void add(IListener2<T1, T2> listener) {

View file

@ -2,5 +2,6 @@ package jrummikub.util;
public interface IEvent {
public void add(IListener listener);
public void remove(IListener listener);
}

View file

@ -2,5 +2,6 @@ package jrummikub.util;
public interface IEvent1<T> {
public void add(IListener1<T> listener);
public void remove(IListener1<T> listener);
}

View file

@ -2,5 +2,6 @@ package jrummikub.util;
public interface IEvent2<T1, T2> {
public void add(IListener2<T1, T2> listener);
public void remove(IListener2<T1, T2> listener);
}

View file

@ -4,21 +4,20 @@ package jrummikub.util;
* A pair of objects
*/
public class Pair<T1, T2> {
final T1 first;
final T2 second;
final T1 first;
final T2 second;
public Pair(T1 first, T2 second) {
this.first = first;
this.second = second;
}
public Pair(T1 first, T2 second) {
this.first = first;
this.second = second;
}
public T1 getFirst() {
return first;
}
public T2 getSecond() {
return second;
}
public T1 getFirst() {
return first;
}
public T2 getSecond() {
return second;
}
}

View file

@ -5,6 +5,8 @@ import jrummikub.util.IEvent2;
public interface IClickable {
public IEvent2<Position, Boolean> getClickEvent();
public IEvent2<Position, Boolean> getRangeClickEvent();
public IEvent2<Position, Boolean> getSetClickEvent();
}

View file

@ -6,9 +6,12 @@ public interface IPlayerPanel {
public IBoard getBoard();
public void setCurrentPlayerName(String playerName);
public void setTimeLeft(int time);
public IEvent getSortByNumberEvent();
public IEvent getSortByColorEvent();
public IEvent getEndTurnEvent();
}

View file

@ -7,7 +7,9 @@ import jrummikub.model.StoneSet;
public interface ITable extends IClickable {
public void setLeftPlayerName(String playerName);
public void setTopPlayerName(String playerName);
public void setRightPlayerName(String playerName);
public void setStoneSets(Map<StoneSet, Position> stoneSets);

View file

@ -46,19 +46,22 @@ class Board extends StonePanel implements IBoard {
@Override
public void componentResized(ComponentEvent e) {
Insets insets = getInsets();
int size = (getHeight()-insets.top-insets.bottom)/BOARD_HEIGHT;
int size = (getHeight() - insets.top - insets.bottom) / BOARD_HEIGHT;
getStonePainter().setScale(size*StonePainter.HEIGHT_SCALE);
getStonePainter().setScale(size * StonePainter.HEIGHT_SCALE);
setSize(new Dimension(BOARD_WIDTH*getStonePainter().getStoneWidth()+insets.left+insets.right, getHeight()));
setSize(new Dimension(BOARD_WIDTH * getStonePainter().getStoneWidth()
+ insets.left + insets.right, getHeight()));
}
});
}
private BufferedImage getScaledBackground(int size) {
BufferedImage scaled = new BufferedImage(size, size, BufferedImage.TYPE_INT_RGB);
BufferedImage scaled = new BufferedImage(size, size,
BufferedImage.TYPE_INT_RGB);
Graphics2D g = scaled.createGraphics();
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BICUBIC);
g.drawImage(BACKGROUND, 0, 0, size, size, null);
@ -68,9 +71,10 @@ class Board extends StonePanel implements IBoard {
@Override
protected void paintComponent(Graphics g1) {
Insets insets = getInsets();
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);
int size = height/BOARD_HEIGHT;
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);
int size = height / BOARD_HEIGHT;
if (scaledBackground.getHeight() != size)
scaledBackground = getScaledBackground(size);
@ -81,7 +85,7 @@ class Board extends StonePanel implements IBoard {
}
}
getStonePainter().setScale(size*StonePainter.HEIGHT_SCALE);
getStonePainter().setScale(size * StonePainter.HEIGHT_SCALE);
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);

View file

@ -30,13 +30,14 @@ class CustomBorder implements Border {
}
@Override
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
public void paintBorder(Component c, Graphics g, int x, int y, int width,
int height) {
g.setColor(color);
g.fillRect(x, y, width, top);
g.fillRect(x, y+height-bottom, width, bottom);
g.fillRect(x, y + height - bottom, width, bottom);
g.fillRect(x, y, left, height);
g.fillRect(x+width-right, y, right, height);
g.fillRect(x + width - right, y, right, height);
}
}

View file

@ -42,7 +42,6 @@ class PlayerPanel extends JPanel implements IPlayerPanel {
private Event sortByColorEvent = new Event();
private Event endTurnEvent = new Event();
@Override
public Board getBoard() {
return board;
@ -54,10 +53,10 @@ class PlayerPanel extends JPanel implements IPlayerPanel {
}
@Override
public
void setTimeLeft(int time) {
public void setTimeLeft(int time) {
timeBar.setValue(time);
timeBar.setString(Integer.toString(time/60) + ":" + secondFormat.format(time%60));
timeBar.setString(Integer.toString(time / 60) + ":"
+ secondFormat.format(time % 60));
}
@Override
@ -75,12 +74,12 @@ class PlayerPanel extends JPanel implements IPlayerPanel {
return endTurnEvent;
}
private void createLeftPanel() {
leftPanel = new JPanel();
leftPanel.setLayout(null);
leftPanel.setOpaque(false);
leftPanel.setBorder(new EmptyBorder(SIDE_PANEL_INSET, SIDE_PANEL_INSET, SIDE_PANEL_INSET, SIDE_PANEL_INSET));
leftPanel.setBorder(new EmptyBorder(SIDE_PANEL_INSET, SIDE_PANEL_INSET,
SIDE_PANEL_INSET, SIDE_PANEL_INSET));
currentPlayerNameLabel = new JLabel();
currentPlayerNameLabel.setHorizontalAlignment(JLabel.CENTER);
@ -94,7 +93,8 @@ class PlayerPanel extends JPanel implements IPlayerPanel {
@Override
public void actionPerformed(ActionEvent arg0) {
sortByNumberEvent.fire();
}});
}
});
leftPanel.add(sortByNumberButton);
sortByColorButton = new JButton("<html><center>Sort by<br>color");
@ -102,26 +102,33 @@ class PlayerPanel extends JPanel implements IPlayerPanel {
@Override
public void actionPerformed(ActionEvent arg0) {
sortByColorEvent.fire();
}});
}
});
leftPanel.add(sortByColorButton);
leftPanel.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
Insets insets = leftPanel.getInsets();
int x = insets.left, y = insets.top, width = leftPanel.getWidth()-insets.left-insets.right, height = leftPanel.getHeight()-insets.top-insets.bottom;
int x = insets.left, y = insets.top, width = leftPanel.getWidth()
- insets.left - insets.right, height = leftPanel.getHeight()
- insets.top - insets.bottom;
if (width > SIDE_PANEL_MAX_WIDTH) {
x += (width-SIDE_PANEL_MAX_WIDTH)/4;
width = width/2+SIDE_PANEL_MAX_WIDTH/2;
x += (width - SIDE_PANEL_MAX_WIDTH) / 4;
width = width / 2 + SIDE_PANEL_MAX_WIDTH / 2;
}
int firstLineHeight = (int)((height-SIDE_PANEL_SEPARATOR)*SIDE_PANEL_FIRST_LINE_HEIGHT);
int buttonWidth = (width-SIDE_PANEL_SEPARATOR)/2;
int firstLineHeight = (int) ((height - SIDE_PANEL_SEPARATOR) * SIDE_PANEL_FIRST_LINE_HEIGHT);
int buttonWidth = (width - SIDE_PANEL_SEPARATOR) / 2;
currentPlayerNameLabel.setBounds(x, y, width, firstLineHeight);
sortByNumberButton.setBounds(x, y+firstLineHeight+SIDE_PANEL_SEPARATOR, buttonWidth, height-SIDE_PANEL_SEPARATOR-firstLineHeight);
sortByColorButton.setBounds(x+buttonWidth+SIDE_PANEL_SEPARATOR, y+firstLineHeight+SIDE_PANEL_SEPARATOR, buttonWidth, height-SIDE_PANEL_SEPARATOR-firstLineHeight);
sortByNumberButton.setBounds(x, y + firstLineHeight
+ SIDE_PANEL_SEPARATOR, buttonWidth, height - SIDE_PANEL_SEPARATOR
- firstLineHeight);
sortByColorButton.setBounds(x + buttonWidth + SIDE_PANEL_SEPARATOR, y
+ firstLineHeight + SIDE_PANEL_SEPARATOR, buttonWidth, height
- SIDE_PANEL_SEPARATOR - firstLineHeight);
}
});
}
@ -130,7 +137,8 @@ class PlayerPanel extends JPanel implements IPlayerPanel {
rightPanel = new JPanel();
rightPanel.setLayout(null);
rightPanel.setOpaque(false);
rightPanel.setBorder(new EmptyBorder(SIDE_PANEL_INSET, SIDE_PANEL_INSET, SIDE_PANEL_INSET, SIDE_PANEL_INSET));
rightPanel.setBorder(new EmptyBorder(SIDE_PANEL_INSET, SIDE_PANEL_INSET,
SIDE_PANEL_INSET, SIDE_PANEL_INSET));
timeBar = new JProgressBar(0, 60);
timeBar.setStringPainted(true);
@ -141,7 +149,8 @@ class PlayerPanel extends JPanel implements IPlayerPanel {
@Override
public void actionPerformed(ActionEvent arg0) {
endTurnEvent.fire();
}});
}
});
rightPanel.add(endTurnButton);
@ -149,36 +158,39 @@ class PlayerPanel extends JPanel implements IPlayerPanel {
@Override
public void componentResized(ComponentEvent e) {
Insets insets = rightPanel.getInsets();
int x = insets.left, y = insets.top, width = rightPanel.getWidth()-insets.left-insets.right, height = rightPanel.getHeight()-insets.top-insets.bottom;
int x = insets.left, y = insets.top, width = rightPanel.getWidth()
- insets.left - insets.right, height = rightPanel.getHeight()
- insets.top - insets.bottom;
if (width > SIDE_PANEL_MAX_WIDTH) {
x += (width-SIDE_PANEL_MAX_WIDTH)/4;
width = width/2+SIDE_PANEL_MAX_WIDTH/2;
x += (width - SIDE_PANEL_MAX_WIDTH) / 4;
width = width / 2 + SIDE_PANEL_MAX_WIDTH / 2;
}
int firstLineHeight = (int)((height-SIDE_PANEL_SEPARATOR)*SIDE_PANEL_FIRST_LINE_HEIGHT);
int firstLineHeight = (int) ((height - SIDE_PANEL_SEPARATOR) * SIDE_PANEL_FIRST_LINE_HEIGHT);
timeBar.setBounds(x, y, width, firstLineHeight);
endTurnButton.setBounds(x, y+firstLineHeight+SIDE_PANEL_SEPARATOR, width, height-SIDE_PANEL_SEPARATOR-firstLineHeight);
endTurnButton.setBounds(x, y + firstLineHeight + SIDE_PANEL_SEPARATOR,
width, height - SIDE_PANEL_SEPARATOR - firstLineHeight);
}
});
}
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;
int x = insets.left, y = insets.top, width = getWidth() - insets.left
- insets.right, height = getHeight() - insets.top - insets.bottom;
int boardWidth = board.getWidth();
int panelWidth = (width-boardWidth)/2;
int panelWidth = (width - boardWidth) / 2;
leftPanel.setBounds(x, y, panelWidth, height);
board.setBounds(x+panelWidth, y, boardWidth, height);
rightPanel.setBounds(x+panelWidth+boardWidth, y, panelWidth, height);
board.setBounds(x + panelWidth, y, boardWidth, height);
rightPanel.setBounds(x + panelWidth + boardWidth, y, panelWidth, height);
leftPanel.validate();
rightPanel.validate();
}
PlayerPanel() {
setLayout(null);

View file

@ -26,14 +26,12 @@ class StonePainter {
private static final float BRIGHTER_SCALE = 1.15f;
public static final float HEIGHT_SCALE = ASPECT_RATIO/DEFAULT_WIDTH;
public static final float HEIGHT_SCALE = ASPECT_RATIO / DEFAULT_WIDTH;
private float scale;
private static int even(float f) {
return 2*(int)(f/2);
return 2 * (int) (f / 2);
}
private static Color brighter(Color color) {
@ -41,11 +39,11 @@ class StonePainter {
int g = (int) (color.getRed() * BRIGHTER_SCALE);
int b = (int) (color.getRed() * BRIGHTER_SCALE);
return new Color(r>255?255:r, g>255?255:g, b>255?255:b);
return new Color(r > 255 ? 255 : r, g > 255 ? 255 : g, b > 255 ? 255 : b);
}
private static Color getColor(StoneColor color) {
switch(color) {
switch (color) {
case BLACK:
return new Color(0.15f, 0.15f, 0.15f);
case BLUE:
@ -68,31 +66,33 @@ class StonePainter {
}
/**
* @param x x position in screen coordinates
* @param y y position in screen coordinates
* @param x
* x position in screen coordinates
* @param y
* y position in screen coordinates
* @return position in grid coordinates
*/
public Position calculatePosition(int x, int y){
public Position calculatePosition(int x, int y) {
float width = getStoneWidth();
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);
return even(DEFAULT_WIDTH * scale);
}
public int getStoneHeight() {
return (int)(DEFAULT_WIDTH*scale/ASPECT_RATIO);
return (int) (DEFAULT_WIDTH * scale / ASPECT_RATIO);
}
StonePainter(float scale) {
this.scale = scale;
}
private void paintStoneBackground(Graphics2D g, int x, int y,
int width, int height, Color background) {
private void paintStoneBackground(Graphics2D g, int x, int y, int width,
int height, Color background) {
// Paint background
g.setColor(background);
g.fillRect(x, y, width, height);
@ -101,22 +101,22 @@ class StonePainter {
g.setColor(brighter(brighter(background)));
g.fillRect(x, y, 1, height);
g.setColor(brighter(background));
g.fillRect(x+1, y+1, 1, height-2);
g.fillRect(x + 1, y + 1, 1, height - 2);
g.setColor(brighter(brighter(background)));
g.fillRect(x, y, width, 1);
g.setColor(brighter(background));
g.fillRect(x+1, y+1, width-2, 1);
g.fillRect(x + 1, y + 1, width - 2, 1);
g.setColor(background.darker().darker());
g.fillRect(x+width-1, y, 1, height);
g.fillRect(x + width - 1, y, 1, height);
g.setColor(background.darker());
g.fillRect(x+width-2, y+1, 1, height-2);
g.fillRect(x + width - 2, y + 1, 1, height - 2);
g.setColor(background.darker().darker());
g.fillRect(x, y+height-1, width, 1);
g.fillRect(x, y + height - 1, width, 1);
g.setColor(background.darker());
g.fillRect(x+1, y+height-2, width-2, 1);
g.fillRect(x + 1, y + height - 2, width - 2, 1);
}
private void paintJokerFace(Graphics2D g, int x, int y, int width, int height) {
@ -128,40 +128,40 @@ class StonePainter {
g.setStroke(new BasicStroke(1));
GeneralPath path = new GeneralPath();
// nose
path.moveTo(x+0.5f*width, y+0.45f*height);
path.lineTo(x+0.53f*width, y+0.6f*height);
path.lineTo(x+0.47f*width, y+0.6f*height);
path.moveTo(x + 0.5f * width, y + 0.45f * height);
path.lineTo(x + 0.53f * width, y + 0.6f * height);
path.lineTo(x + 0.47f * width, y + 0.6f * height);
path.closePath();
g.fill(path);
path.reset();
// mouth, left
path.moveTo(x+0.23f*width, y+0.75f*width);
path.lineTo(x+0.27f*width, y+0.65f*width);
path.moveTo(x + 0.23f * width, y + 0.75f * width);
path.lineTo(x + 0.27f * width, y + 0.65f * width);
// mouth, middle
path.moveTo(x+0.25f*width, y+0.7f*width);
path.lineTo(x+0.5f*width, y+0.8f*width);
path.lineTo(x+0.75f*width, y+0.7f*width);
path.moveTo(x + 0.25f * width, y + 0.7f * width);
path.lineTo(x + 0.5f * width, y + 0.8f * width);
path.lineTo(x + 0.75f * width, y + 0.7f * width);
// mouth, right
path.moveTo(x+0.77f*width, y+0.75f*width);
path.lineTo(x+0.73f*width, y+0.65f*width);
path.moveTo(x + 0.77f * width, y + 0.75f * width);
path.lineTo(x + 0.73f * width, y + 0.65f * width);
g.draw(path);
path.reset();
// left eye
path.moveTo(x+0.3f*width, y+0.41f*height);
path.lineTo(x+0.375f*width, y+0.375f*height);
path.lineTo(x+0.3f*width, y+0.34f*height);
path.lineTo(x+0.225f*width, y+0.375f*height);
path.moveTo(x + 0.3f * width, y + 0.41f * height);
path.lineTo(x + 0.375f * width, y + 0.375f * height);
path.lineTo(x + 0.3f * width, y + 0.34f * height);
path.lineTo(x + 0.225f * width, y + 0.375f * height);
path.closePath();
g.draw(path);
path.reset();
// right eye
path.moveTo(x+0.7f*width, y+0.41f*height);
path.lineTo(x+0.625f*width, y+0.375f*height);
path.lineTo(x+0.7f*width, y+0.34f*height);
path.lineTo(x+0.775f*width, y+0.375f*height);
path.moveTo(x + 0.7f * width, y + 0.41f * height);
path.lineTo(x + 0.625f * width, y + 0.375f * height);
path.lineTo(x + 0.7f * width, y + 0.34f * height);
path.lineTo(x + 0.775f * width, y + 0.375f * height);
path.closePath();
g.draw(path);
@ -170,38 +170,43 @@ class StonePainter {
private void paintJoker(Graphics2D g, int x, int y, int width, int height,
Color color) {
int faceSize = even(FACE_WIDTH*width);
int pos = y + (int)(TEXT_POS*height);
int faceSize = even(FACE_WIDTH * width);
int pos = y + (int) (TEXT_POS * height);
g.setColor(color);
paintJokerFace(g, x+width/2-faceSize/2, pos-faceSize/2, faceSize, faceSize);
paintJokerFace(g, x + width / 2 - faceSize / 2, pos - faceSize / 2,
faceSize, faceSize);
}
private void paintStoneNumber(Graphics2D g, int x, int y, int width,
int height, Color color, int v) {
int pos = y + (int)(TEXT_POS*height);
int pos = y + (int) (TEXT_POS * height);
g.setFont(new Font("SansSerif", Font.BOLD, height/4));
g.setFont(new Font("SansSerif", Font.BOLD, height / 4));
FontMetrics fm = g.getFontMetrics();
String value = Integer.toString(v);
Rectangle2D stringRect = fm.getStringBounds(value, g);
g.setColor(color.darker());
g.drawString(value, (int)(x+width/2-stringRect.getWidth()/2)+1, pos+(fm.getAscent()-fm.getDescent())/2+1);
g.drawString(value, (int) (x + width / 2 - stringRect.getWidth() / 2) + 1,
pos + (fm.getAscent() - fm.getDescent()) / 2 + 1);
g.setColor(color);
g.drawString(value, (int)(x+width/2-stringRect.getWidth()/2), pos+(fm.getAscent()-fm.getDescent())/2);
g.drawString(value, (int) (x + width / 2 - stringRect.getWidth() / 2), pos
+ (fm.getAscent() - fm.getDescent()) / 2);
}
private void paintCircle(Graphics2D g, int x, int y, int width, int height, Color background) {
int size = even(width*CIRCLE_WIDTH);
int pos = y + (int)(CIRCLE_POS*height);
private void paintCircle(Graphics2D g, int x, int y, int width, int height,
Color background) {
int size = even(width * CIRCLE_WIDTH);
int pos = y + (int) (CIRCLE_POS * height);
// Paint circle
g.setColor(background.darker());
g.drawArc(x+width/2-size/2, pos-size/2, size, size, 50, 170);
g.drawArc(x + width / 2 - size / 2, pos - size / 2, size, size, 50, 170);
g.setColor(brighter(background));
g.drawArc((int)(x+width/2-size/2), pos-size/2, size, size, -130, 170);
g.drawArc((int) (x + width / 2 - size / 2), pos - size / 2, size, size,
-130, 170);
}
public void paintStone(Graphics2D g, Stone stone, Position p, boolean selected) {
@ -209,8 +214,8 @@ class StonePainter {
int width = getStoneWidth();
int height = getStoneHeight();
int x = (int)(p.getX()*width);
int y = (int)(p.getY()*height);
int x = (int) (p.getX() * width);
int y = (int) (p.getY() * height);
paintStoneBackground(g, x, y, width, height, background);

View file

@ -42,8 +42,9 @@ abstract class StonePanel extends JPanel implements IClickable {
else if (e.getClickCount() >= 2)
event = setClickEvent;
event.fire(stonePainter.calculatePosition(e.getX() - insets.left,
e.getY() - insets.top), e.isControlDown());
event.fire(
stonePainter.calculatePosition(e.getX() - insets.left, e.getY()
- insets.top), e.isControlDown());
}
});
}

View file

@ -19,7 +19,7 @@ import jrummikub.model.StoneSet;
import jrummikub.view.ITable;
@SuppressWarnings("serial")
public class Table extends StonePanel implements ITable {
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;
@ -32,7 +32,6 @@ public class Table extends StonePanel implements ITable {
private Map<StoneSet, Position> stoneSets = Collections.emptyMap();
private Collection<Stone> selectedStones = Collections.emptyList();
@Override
public void setLeftPlayerName(String playerName) {
leftPlayerLabel.setText(playerName);
@ -110,15 +109,15 @@ public class Table extends StonePanel implements ITable {
paintStoneSet(g, stoneSet.getKey(), stoneSet.getValue());
}
int selectedStonesWidth = getWidth()*3/5-14;
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);
g.fillRect(selectedStonesX - 7, selectedStonesY - 7,
selectedStonesWidth + 14, selectedStonesHeight + 14);
Graphics2D translatedG = (Graphics2D) g.create(selectedStonesX,
selectedStonesY, selectedStonesWidth, selectedStonesHeight);
@ -126,7 +125,8 @@ public class Table extends StonePanel implements ITable {
float x = 0;
for (Stone stone : selectedStones) {
selectedStonePainter.paintStone(translatedG, stone, new Position(x, 0), false);
selectedStonePainter.paintStone(translatedG, stone, new Position(x, 0),
false);
x++;
}
}

View file

@ -1,7 +1,6 @@
package jrummikub.view.impl;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
@ -23,9 +22,8 @@ public class View extends JFrame implements IView {
private final static int PLAYER_PANEL_BORDER_WIDTH = 1;
private final static int PLAYER_PANEL_MAX_HEIGHT = 180 + PLAYER_PANEL_BORDER_WIDTH;
private static int even(double d) {
return 2*(int)(d/2);
return 2 * (int) (d / 2);
}
public ITable getTable() {
@ -36,7 +34,6 @@ public class View extends JFrame implements IView {
return playerPanel;
}
public View() {
super("JRummikub");
setLayout(null);
@ -48,16 +45,20 @@ public class View extends JFrame implements IView {
add(table);
playerPanel = new PlayerPanel();
playerPanel.setBorder(new CustomBorder(Color.BLACK, PLAYER_PANEL_BORDER_WIDTH, 0, 0, 0));
playerPanel.setBorder(new CustomBorder(Color.BLACK,
PLAYER_PANEL_BORDER_WIDTH, 0, 0, 0));
add(playerPanel);
addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
Insets insets = getInsets();
int x = insets.left, y = insets.top, width = getWidth()-insets.left-insets.right, height = getHeight()-insets.top-insets.bottom;
int x = insets.left, y = insets.top, width = getWidth() - insets.left
- insets.right, height = getHeight() - insets.top - insets.bottom;
int playerPanelHeight = even(Math.pow((double)width*width*height, 1/3.0)*PLAYER_PANEL_RATIO) + PLAYER_PANEL_BORDER_WIDTH;
int playerPanelHeight = even(Math.pow((double) width * width * height,
1 / 3.0) * PLAYER_PANEL_RATIO)
+ PLAYER_PANEL_BORDER_WIDTH;
if (playerPanelHeight > PLAYER_PANEL_MAX_HEIGHT)
playerPanelHeight = PLAYER_PANEL_MAX_HEIGHT;
@ -65,7 +66,7 @@ public class View extends JFrame implements IView {
table.setBounds(x, y, width, tableHeight);
table.validate();
playerPanel.setBounds(x, y+tableHeight, width, playerPanelHeight);
playerPanel.setBounds(x, y + tableHeight, width, playerPanelHeight);
}
});

View file

@ -6,77 +6,78 @@ import org.junit.Test;
public class Event1Test {
int fired, fired2;
int fired, fired2;
@Test
public void singleListener() {
fired = 0;
Event1<Integer> testEvent = new Event1<Integer>();
testEvent.add(new IListener1<Integer>() {
@Test
public void singleListener() {
fired = 0;
Event1<Integer> testEvent = new Event1<Integer>();
testEvent.add(new IListener1<Integer>() {
@Override
public void fire(Integer n) {
fired += n;
@Override
public void fire(Integer n) {
fired += n;
}
});
assertEquals(fired, 0);
testEvent.fire(10);
assertEquals(fired, 10);
testEvent.fire(20);
assertEquals(fired, 30);
}
}
});
assertEquals(fired, 0);
testEvent.fire(10);
assertEquals(fired, 10);
testEvent.fire(20);
assertEquals(fired, 30);
}
@Test
public void twoListeners() {
fired = 0;
fired2 = 0;
Event1<Integer> testEvent = new Event1<Integer>();
testEvent.add(new IListener1<Integer>() {
@Test
public void twoListeners() {
fired = 0;
fired2 = 0;
Event1<Integer> testEvent = new Event1<Integer>();
testEvent.add(new IListener1<Integer>() {
@Override
public void fire(Integer n) {
fired += n;
@Override
public void fire(Integer n) {
fired += n;
}
});
testEvent.add(new IListener1<Integer>() {
}
});
testEvent.add(new IListener1<Integer>() {
@Override
public void fire(Integer n) {
fired2 -= n;
@Override
public void fire(Integer n) {
fired2 -= n;
}
});
assertEquals(fired, 0);
assertEquals(fired2, 0);
testEvent.fire(5);
assertEquals(fired, 5);
assertEquals(fired2, -5);
}
});
assertEquals(fired, 0);
assertEquals(fired2, 0);
testEvent.fire(5);
assertEquals(fired, 5);
assertEquals(fired2, -5);
}
}
@Test public void removeListener() {
fired = 0;
Event1<Integer> testEvent = new Event1<Integer>();
testEvent.add(new IListener1<Integer>() {
@Test
public void removeListener() {
fired = 0;
Event1<Integer> testEvent = new Event1<Integer>();
testEvent.add(new IListener1<Integer>() {
@Override
public void fire(Integer n) {
fired += n;
@Override
public void fire(Integer n) {
fired += n;
}
});
IListener1<Integer> rem = new IListener1<Integer>() {
}
});
IListener1<Integer> rem = new IListener1<Integer>() {
@Override
public void fire(Integer n) {
fail();
}
};
testEvent.add(rem);
testEvent.remove(rem);
testEvent.fire(10);
assertEquals(fired, 10);
}
@Override
public void fire(Integer n) {
fail();
}
};
testEvent.add(rem);
testEvent.remove(rem);
testEvent.fire(10);
assertEquals(fired, 10);
}
}

View file

@ -65,7 +65,8 @@ public class Event2Test {
assertEquals(fired4, -10);
}
@Test public void removeListener() {
@Test
public void removeListener() {
fired = 0;
fired2 = 0;
Event2<Integer, Integer> testEvent = new Event2<Integer, Integer>();

View file

@ -4,79 +4,80 @@ import org.junit.Test;
import static org.junit.Assert.*;
public class EventTest {
boolean fired, fired2;
boolean fired, fired2;
@Test
public void singleListener() {
fired = false;
Event testEvent = new Event();
testEvent.add(new IListener() {
@Test
public void singleListener() {
fired = false;
Event testEvent = new Event();
testEvent.add(new IListener() {
@Override
public void fire() {
fired = true;
@Override
public void fire() {
fired = true;
}
});
assertFalse(fired);
testEvent.fire();
assertTrue(fired);
fired = false;
testEvent.fire();
assertTrue(fired);
}
}
});
assertFalse(fired);
testEvent.fire();
assertTrue(fired);
fired = false;
testEvent.fire();
assertTrue(fired);
}
@Test
public void twoListeners() {
fired = false;
fired2 = false;
Event testEvent = new Event();
testEvent.add(new IListener() {
@Test
public void twoListeners() {
fired = false;
fired2 = false;
Event testEvent = new Event();
testEvent.add(new IListener() {
@Override
public void fire() {
fired = true;
@Override
public void fire() {
fired = true;
}
});
testEvent.add(new IListener() {
}
});
testEvent.add(new IListener() {
@Override
public void fire() {
fired2 = true;
@Override
public void fire() {
fired2 = true;
}
});
assertFalse(fired);
assertFalse(fired2);
testEvent.fire();
assertTrue(fired);
assertTrue(fired2);
}
});
assertFalse(fired);
assertFalse(fired2);
testEvent.fire();
assertTrue(fired);
assertTrue(fired2);
}
}
@Test public void removeListener() {
fired = false;
Event testEvent = new Event();
testEvent.add(new IListener() {
@Test
public void removeListener() {
fired = false;
Event testEvent = new Event();
testEvent.add(new IListener() {
@Override
public void fire() {
fired = true;
@Override
public void fire() {
fired = true;
}
});
IListener rem = new IListener() {
}
});
IListener rem = new IListener() {
@Override
public void fire() {
fail();
}
};
testEvent.add(rem);
testEvent.remove(rem);
testEvent.fire();
assertTrue(fired);
}
@Override
public void fire() {
fail();
}
};
testEvent.add(rem);
testEvent.remove(rem);
testEvent.fire();
assertTrue(fired);
}
}