diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/jrummikub/JRummikub.java | 126 | ||||
-rw-r--r-- | src/jrummikub/util/Event.java | 2 | ||||
-rw-r--r-- | src/jrummikub/util/Event1.java | 2 | ||||
-rw-r--r-- | src/jrummikub/util/Event2.java | 4 | ||||
-rw-r--r-- | src/jrummikub/util/IEvent.java | 1 | ||||
-rw-r--r-- | src/jrummikub/util/IEvent1.java | 1 | ||||
-rw-r--r-- | src/jrummikub/util/IEvent2.java | 1 | ||||
-rw-r--r-- | src/jrummikub/util/Pair.java | 29 | ||||
-rw-r--r-- | src/jrummikub/view/IClickable.java | 2 | ||||
-rw-r--r-- | src/jrummikub/view/IPlayerPanel.java | 9 | ||||
-rw-r--r-- | src/jrummikub/view/ITable.java | 6 | ||||
-rw-r--r-- | src/jrummikub/view/IView.java | 2 | ||||
-rw-r--r-- | src/jrummikub/view/impl/Board.java | 52 | ||||
-rw-r--r-- | src/jrummikub/view/impl/CustomBorder.java | 13 | ||||
-rw-r--r-- | src/jrummikub/view/impl/PlayerPanel.java | 136 | ||||
-rw-r--r-- | src/jrummikub/view/impl/StonePainter.java | 179 | ||||
-rw-r--r-- | src/jrummikub/view/impl/StonePanel.java | 21 | ||||
-rw-r--r-- | src/jrummikub/view/impl/Table.java | 28 | ||||
-rw-r--r-- | src/jrummikub/view/impl/View.java | 37 |
19 files changed, 352 insertions, 299 deletions
diff --git a/src/jrummikub/JRummikub.java b/src/jrummikub/JRummikub.java index de270f6..9571446 100644 --- a/src/jrummikub/JRummikub.java +++ b/src/jrummikub/JRummikub.java @@ -28,97 +28,117 @@ public class JRummikub { UIManager.setLookAndFeel(nativeLF); } catch (Exception e) { } - + IView view = new jrummikub.view.impl.View(); - + view.getPlayerPanel().setCurrentPlayerName("Player 1"); view.getPlayerPanel().setTimeLeft(42); view.getTable().setLeftPlayerName("Player 2"); view.getTable().setTopPlayerName("Player 3"); view.getTable().setRightPlayerName("Player 4"); - + view.getPlayerPanel().getSortByNumberEvent().add(new IListener() { @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)); stones.put(new Stone(9, StoneColor.RED, false), new Position(0.5f, 1)); stones.put(new Stone(7, StoneColor.BLACK, false), new Position(1.75f, 1)); - - Stone stoneJoker = new Stone(0, StoneColor.RED, true); + + Stone stoneJoker = new Stone(0, StoneColor.RED, true); stones.put(stoneJoker, new Position(2.5f, 0)); stones.put(new Stone(0, StoneColor.BLACK, true), new Position(3.5f, 0)); - + view.getPlayerPanel().getBoard().setStones(stones); - - 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("Table clicked at "+p+(collect?", collect":"")); - - }}); - view.getTable().getRangeClickEvent().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("Table range-clicked at "+p+(collect?", collect":"")); - - }}); - view.getTable().getSetClickEvent().add(new IListener2<Position, Boolean>(){ + 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 + System.out.println("Table set-clicked at " + p + + (collect ? ", collect" : "")); + + } + }); + + // 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>(); - + stoneList.add(new Stone(7, StoneColor.BLACK, false)); Stone stone8 = new Stone(8, StoneColor.BLACK, false); stoneList.add(stone8); stoneList.add(new Stone(9, StoneColor.BLACK, false)); stoneList.add(new Stone(10, StoneColor.BLACK, false)); - + stoneSets.put(new StoneSet(stoneList), new Position(3.5f, 4)); - + view.getTable().setStoneSets(stoneSets); view.setSelectedStones(Arrays.asList(stoneJoker, stone8)); diff --git a/src/jrummikub/util/Event.java b/src/jrummikub/util/Event.java index 0b2317f..1a68eac 100644 --- a/src/jrummikub/util/Event.java +++ b/src/jrummikub/util/Event.java @@ -4,7 +4,7 @@ import java.util.HashSet; public class Event implements IEvent { private HashSet<IListener> listeners = new HashSet<IListener>(); - + @Override public void add(IListener listener) { listeners.add(listener); diff --git a/src/jrummikub/util/Event1.java b/src/jrummikub/util/Event1.java index e8ccb43..f0595f9 100644 --- a/src/jrummikub/util/Event1.java +++ b/src/jrummikub/util/Event1.java @@ -4,7 +4,7 @@ import java.util.HashSet; public class Event1<T> implements IEvent1<T> { private HashSet<IListener1<T>> listeners = new HashSet<IListener1<T>>(); - + @Override public void add(IListener1<T> listener) { listeners.add(listener); diff --git a/src/jrummikub/util/Event2.java b/src/jrummikub/util/Event2.java index 19d2d58..c7ca8e7 100644 --- a/src/jrummikub/util/Event2.java +++ b/src/jrummikub/util/Event2.java @@ -3,8 +3,8 @@ 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) { listeners.add(listener); diff --git a/src/jrummikub/util/IEvent.java b/src/jrummikub/util/IEvent.java index ce0e13d..2295118 100644 --- a/src/jrummikub/util/IEvent.java +++ b/src/jrummikub/util/IEvent.java @@ -2,5 +2,6 @@ package jrummikub.util; public interface IEvent { public void add(IListener listener); + public void remove(IListener listener); } diff --git a/src/jrummikub/util/IEvent1.java b/src/jrummikub/util/IEvent1.java index 47075db..4108078 100644 --- a/src/jrummikub/util/IEvent1.java +++ b/src/jrummikub/util/IEvent1.java @@ -2,5 +2,6 @@ package jrummikub.util; public interface IEvent1<T> { public void add(IListener1<T> listener); + public void remove(IListener1<T> listener); } diff --git a/src/jrummikub/util/IEvent2.java b/src/jrummikub/util/IEvent2.java index f677a2d..ac68c4f 100644 --- a/src/jrummikub/util/IEvent2.java +++ b/src/jrummikub/util/IEvent2.java @@ -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); } diff --git a/src/jrummikub/util/Pair.java b/src/jrummikub/util/Pair.java index 95af113..5fb8589 100644 --- a/src/jrummikub/util/Pair.java +++ b/src/jrummikub/util/Pair.java @@ -4,21 +4,20 @@ package jrummikub.util; * A pair of objects */ public class Pair<T1, T2> { - final T1 first; - final T2 second; - - public Pair(T1 first, T2 second) { - this.first = first; - this.second = second; - } + final T1 first; + final T2 second; - public T1 getFirst() { - return first; - } + public Pair(T1 first, T2 second) { + this.first = first; + this.second = second; + } + + public T1 getFirst() { + return first; + } + + public T2 getSecond() { + return second; + } - public T2 getSecond() { - return second; - } - - } diff --git a/src/jrummikub/view/IClickable.java b/src/jrummikub/view/IClickable.java index fe381a9..9973d5b 100644 --- a/src/jrummikub/view/IClickable.java +++ b/src/jrummikub/view/IClickable.java @@ -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(); } diff --git a/src/jrummikub/view/IPlayerPanel.java b/src/jrummikub/view/IPlayerPanel.java index 79c15a4..4c15278 100644 --- a/src/jrummikub/view/IPlayerPanel.java +++ b/src/jrummikub/view/IPlayerPanel.java @@ -4,11 +4,14 @@ import jrummikub.util.IEvent; 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(); -}
\ No newline at end of file +} diff --git a/src/jrummikub/view/ITable.java b/src/jrummikub/view/ITable.java index 2630c7f..d0b0fc6 100644 --- a/src/jrummikub/view/ITable.java +++ b/src/jrummikub/view/ITable.java @@ -7,8 +7,10 @@ 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); -}
\ No newline at end of file +} diff --git a/src/jrummikub/view/IView.java b/src/jrummikub/view/IView.java index 1fcc6dc..2b34522 100644 --- a/src/jrummikub/view/IView.java +++ b/src/jrummikub/view/IView.java @@ -16,4 +16,4 @@ public interface IView { public IPlayerPanel getPlayerPanel(); public void setSelectedStones(Collection<Stone> stones); -}
\ No newline at end of file +} diff --git a/src/jrummikub/view/impl/Board.java b/src/jrummikub/view/impl/Board.java index e633233..864b764 100644 --- a/src/jrummikub/view/impl/Board.java +++ b/src/jrummikub/view/impl/Board.java @@ -23,14 +23,14 @@ import jrummikub.view.IBoard; class Board extends StonePanel implements IBoard { private final static int BOARD_HEIGHT = 2; private final static int BOARD_WIDTH = 14; - + private final static BufferedImage BACKGROUND; static { ImageIcon image = new ImageIcon( Board.class.getResource("/jrummikub/resource/wood.png")); BACKGROUND = new BufferedImage(image.getIconWidth(), image.getIconHeight(), BufferedImage.TYPE_INT_RGB); - + image.paintIcon(null, BACKGROUND.createGraphics(), 0, 0); } private BufferedImage scaledBackground = BACKGROUND; @@ -40,52 +40,56 @@ class Board extends StonePanel implements IBoard { Board() { setBorder(new CustomBorder(Color.DARK_GRAY, 0, 1, 0, 1)); - + addComponentListener(new ComponentAdapter() { @Override public void componentResized(ComponentEvent e) { Insets insets = getInsets(); - int size = (getHeight()-insets.top-insets.bottom)/BOARD_HEIGHT; - - getStonePainter().setScale(size*StonePainter.HEIGHT_SCALE); - - setSize(new Dimension(BOARD_WIDTH*getStonePainter().getStoneWidth()+insets.left+insets.right, getHeight())); + int size = (getHeight() - insets.top - insets.bottom) / BOARD_HEIGHT; + + getStonePainter().setScale(size * StonePainter.HEIGHT_SCALE); + + 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); - + return scaled; } - + @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); - + for (int i = 0; i < BOARD_HEIGHT; ++i) { for (int xpos = -size * i / 3; xpos < width; xpos += size) { g.drawImage(scaledBackground, xpos, size * i, null); } } - - getStonePainter().setScale(size*StonePainter.HEIGHT_SCALE); - + + getStonePainter().setScale(size * StonePainter.HEIGHT_SCALE); + g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); - + for (Map.Entry<Stone, Position> entry : stones.entrySet()) { getStonePainter().paintStone(g, entry.getKey(), entry.getValue(), selectedStones.contains(entry.getKey())); @@ -97,7 +101,7 @@ class Board extends StonePanel implements IBoard { this.stones = stones; repaint(); } - + public void setSelectedStones(Collection<Stone> stones) { selectedStones = stones; repaint(); diff --git a/src/jrummikub/view/impl/CustomBorder.java b/src/jrummikub/view/impl/CustomBorder.java index 3e79ce7..eae0589 100644 --- a/src/jrummikub/view/impl/CustomBorder.java +++ b/src/jrummikub/view/impl/CustomBorder.java @@ -10,7 +10,7 @@ import javax.swing.border.Border; class CustomBorder implements Border { private Color color; private int top, left, bottom, right; - + public CustomBorder(Color color, int top, int left, int bottom, int right) { this.color = color; this.top = top; @@ -18,7 +18,7 @@ class CustomBorder implements Border { this.bottom = bottom; this.right = right; } - + @Override public Insets getBorderInsets(Component c) { return new Insets(top, left, bottom, right); @@ -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); } } diff --git a/src/jrummikub/view/impl/PlayerPanel.java b/src/jrummikub/view/impl/PlayerPanel.java index abf7cc9..ef8b4f6 100644 --- a/src/jrummikub/view/impl/PlayerPanel.java +++ b/src/jrummikub/view/impl/PlayerPanel.java @@ -25,24 +25,23 @@ class PlayerPanel extends JPanel implements IPlayerPanel { private final static int SIDE_PANEL_SEPARATOR = 10; private final static float SIDE_PANEL_FIRST_LINE_HEIGHT = 0.375f; private final static int SIDE_PANEL_MAX_WIDTH = 180; - + private final static DecimalFormat secondFormat = new DecimalFormat("00"); - + private Board board; - + JPanel leftPanel, rightPanel; - + private JLabel currentPlayerNameLabel; private JButton sortByNumberButton; private JButton sortByColorButton; private JProgressBar timeBar; private JButton endTurnButton; - + private Event sortByNumberEvent = new Event(); private Event sortByColorEvent = new Event(); private Event endTurnEvent = new Event(); - - + @Override public Board getBoard() { return board; @@ -52,154 +51,167 @@ class PlayerPanel extends JPanel implements IPlayerPanel { public void setCurrentPlayerName(String playerName) { currentPlayerNameLabel.setText(playerName); } - + @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 public IEvent getSortByNumberEvent() { return sortByNumberEvent; } - + @Override public IEvent getSortByColorEvent() { return sortByColorEvent; } - + @Override public IEvent getEndTurnEvent() { 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); currentPlayerNameLabel.setHorizontalTextPosition(JLabel.CENTER); currentPlayerNameLabel.setVerticalAlignment(JLabel.CENTER); currentPlayerNameLabel.setVerticalTextPosition(JLabel.CENTER); leftPanel.add(currentPlayerNameLabel); - + sortByNumberButton = new JButton("<html><center>Sort by<br>number"); sortByNumberButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { sortByNumberEvent.fire(); - }}); + } + }); leftPanel.add(sortByNumberButton); - + sortByColorButton = new JButton("<html><center>Sort by<br>color"); sortByColorButton.addActionListener(new ActionListener() { @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); } }); } - + private void createRightPanel() { 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); rightPanel.add(timeBar); - + endTurnButton = new JButton("End turn"); endTurnButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { endTurnEvent.fire(); - }}); - + } + }); + rightPanel.add(endTurnButton); - + rightPanel.addComponentListener(new ComponentAdapter() { @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); - + setBackground(Color.LIGHT_GRAY); createLeftPanel(); add(leftPanel); - + board = new Board(); add(board); - + createRightPanel(); add(rightPanel); - + ComponentListener rescaleListener = new ComponentAdapter() { @Override public void componentResized(ComponentEvent e) { rescale(); } }; - + addComponentListener(rescaleListener); board.addComponentListener(rescaleListener); } diff --git a/src/jrummikub/view/impl/StonePainter.java b/src/jrummikub/view/impl/StonePainter.java index c9ff4b6..f873f2d 100644 --- a/src/jrummikub/view/impl/StonePainter.java +++ b/src/jrummikub/view/impl/StonePainter.java @@ -23,29 +23,27 @@ class StonePainter { private static final Color BACKGROUND_COLOR = new Color(0.9f, 0.9f, 0.6f); private static final Color SELECTED_COLOR = BACKGROUND_COLOR.darker(); - + 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) { int r = (int) (color.getRed() * BRIGHTER_SCALE); 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: @@ -55,10 +53,10 @@ class StonePainter { case RED: return new Color(0.9f, 0.0f, 0.25f); } - + return null; } - + public void setScale(float scale) { this.scale = scale; } @@ -66,164 +64,171 @@ class StonePainter { public float getScale() { return scale; } - + /** - * @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); - + // Paint bevel border 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) { Stroke oldStroke = g.getStroke(); - + g.setStroke(new BasicStroke(2)); g.drawOval(x, y, width, height); - + 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); - + g.setStroke(oldStroke); } - + 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); - - g.setFont(new Font("SansSerif", Font.BOLD, height/4)); + int pos = y + (int) (TEXT_POS * height); + + g.setFont(new Font("SansSerif", Font.BOLD, height / 4)); FontMetrics fm = g.getFontMetrics(); - String value = Integer.toString(v); + 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) { Color background = selected ? SELECTED_COLOR : BACKGROUND_COLOR; 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); - + Color color = getColor(stone.getColor()); if (selected) color = color.darker(); - + if (stone.isJoker()) { paintJoker(g, x, y, width, height, color); } else { paintStoneNumber(g, x, y, width, height, color, stone.getValue()); } - + paintCircle(g, x, y, width, height, background); } } diff --git a/src/jrummikub/view/impl/StonePanel.java b/src/jrummikub/view/impl/StonePanel.java index b47c354..d5d56a9 100644 --- a/src/jrummikub/view/impl/StonePanel.java +++ b/src/jrummikub/view/impl/StonePanel.java @@ -13,37 +13,38 @@ import jrummikub.view.IClickable; @SuppressWarnings("serial") abstract class StonePanel extends JPanel implements IClickable { private StonePainter stonePainter; - + private Event2<Position, Boolean> clickEvent = new Event2<Position, Boolean>(); private Event2<Position, Boolean> rangeClickEvent = new Event2<Position, Boolean>(); private Event2<Position, Boolean> setClickEvent = new Event2<Position, Boolean>(); - + protected StonePainter getStonePainter() { return stonePainter; } - + public StonePanel() { this(1); } - + 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<Position, Boolean> 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()); + + event.fire( + stonePainter.calculatePosition(e.getX() - insets.left, e.getY() + - insets.top), e.isControlDown()); } }); } diff --git a/src/jrummikub/view/impl/Table.java b/src/jrummikub/view/impl/Table.java index 4a5589b..eb163d1 100644 --- a/src/jrummikub/view/impl/Table.java +++ b/src/jrummikub/view/impl/Table.java @@ -19,20 +19,19 @@ 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; - + private JLabel leftPlayerLabel, topPlayerLabel, rightPlayerLabel; private JPanel innerPanel; - + private StonePainter selectedStonePainter = new StonePainter(1.2f); private Map<StoneSet, Position> stoneSets = Collections.emptyMap(); private Collection<Stone> selectedStones = Collections.emptyList(); - @Override public void setLeftPlayerName(String playerName) { leftPlayerLabel.setText(playerName); @@ -53,7 +52,7 @@ public class Table extends StonePanel implements ITable { this.stoneSets = stoneSets; repaint(); } - + public void setSelectedStones(Collection<Stone> stones) { selectedStones = stones; repaint(); @@ -61,7 +60,7 @@ public class Table extends StonePanel implements ITable { Table() { super(DEFAULT_SCALE); - + setLayout(new BorderLayout()); leftPlayerLabel = new JLabel(); @@ -92,7 +91,7 @@ public class Table extends StonePanel implements ITable { x++; } } - + @Override protected void paintComponent(Graphics g1) { Graphics2D g = (Graphics2D) g1; @@ -109,24 +108,25 @@ public class Table extends StonePanel implements ITable { for (Map.Entry<StoneSet, Position> stoneSet : stoneSets.entrySet()) { 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); - + 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++; } } diff --git a/src/jrummikub/view/impl/View.java b/src/jrummikub/view/impl/View.java index 4cbad44..3508877 100644 --- a/src/jrummikub/view/impl/View.java +++ b/src/jrummikub/view/impl/View.java @@ -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; @@ -18,16 +17,15 @@ import jrummikub.view.IView; public class View extends JFrame implements IView { private Table table; private PlayerPanel playerPanel; - + private final static float PLAYER_PANEL_RATIO = 0.125f; 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() { return table; } @@ -36,39 +34,42 @@ public class View extends JFrame implements IView { return playerPanel; } - public View() { super("JRummikub"); setLayout(null); - + setSize(1000, 700); setDefaultCloseOperation(EXIT_ON_CLOSE); - + table = new Table(); 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 playerPanelHeight = even(Math.pow((double)width*width*height, 1/3.0)*PLAYER_PANEL_RATIO) + PLAYER_PANEL_BORDER_WIDTH; + 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; if (playerPanelHeight > PLAYER_PANEL_MAX_HEIGHT) playerPanelHeight = PLAYER_PANEL_MAX_HEIGHT; - + int tableHeight = height - playerPanelHeight; - + table.setBounds(x, y, width, tableHeight); table.validate(); - playerPanel.setBounds(x, y+tableHeight, width, playerPanelHeight); + playerPanel.setBounds(x, y + tableHeight, width, playerPanelHeight); } }); - + setVisible(true); } |