From 25c6383c73de20ce7e6bc45af6faa255e8d94dc6 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Tue, 31 May 2011 03:02:31 +0200 Subject: Allow setting stone colors to use git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@338 72836036-5685-4462-b002-a69064685172 --- src/jrummikub/control/SettingsControl.java | 77 ++++++++------ src/jrummikub/model/StoneColor.java | 4 +- src/jrummikub/view/ISettingsPanel.java | 2 + src/jrummikub/view/impl/SettingsPanel.java | 162 +++++++++++++++++++---------- src/jrummikub/view/impl/StonePainter.java | 14 +-- 5 files changed, 166 insertions(+), 93 deletions(-) (limited to 'src/jrummikub') diff --git a/src/jrummikub/control/SettingsControl.java b/src/jrummikub/control/SettingsControl.java index fd01613..8362d52 100644 --- a/src/jrummikub/control/SettingsControl.java +++ b/src/jrummikub/control/SettingsControl.java @@ -34,9 +34,9 @@ public class SettingsControl { * Create a new settings control * * @param view - * the view to use + * the view to use * @param settings - * initial game settings + * initial game settings */ public SettingsControl(IView view, GameSettings settings) { this.view = view; @@ -46,8 +46,8 @@ public class SettingsControl { } /** - * the start game event is emitted when the user wants to start a game and - * the settings made are valid + * the start game event is emitted when the user wants to start a game and the + * settings made are valid * * @return the event */ @@ -59,9 +59,25 @@ public class SettingsControl { * Start the operation of the settings control */ public void startSettings() { + addPlayerSettingsListeners(); + + addOptionListeners1(); + addOptionListeners2(); + + connections.add(view.getSettingsPanel().getStartGameEvent() + .add(new IListener() { + @Override + public void handle() { + startGame(); + } + })); + + view.showSettingsPanel(true); + } + + private void addOptionListeners1() { connections.add(view.getSettingsPanel() - .getChangeInitialMeldThresholdEvent() - .add(new IListener1() { + .getChangeInitialMeldThresholdEvent().add(new IListener1() { @Override public void handle(Integer value) { settings.setInitialMeldThreshold(value); @@ -94,6 +110,9 @@ public class SettingsControl { update(); } })); + } + + private void addOptionListeners2() { connections.add(view.getSettingsPanel().getChangeHighestValueEvent() .add(new IListener1() { @Override @@ -110,20 +129,9 @@ public class SettingsControl { update(); } })); - - addPlayerSettingsListeners(); - - view.showSettingsPanel(true); } private void addPlayerSettingsListeners() { - connections.add(view.getSettingsPanel().getStartGameEvent() - .add(new IListener() { - @Override - public void handle() { - startGame(); - } - })); connections.add(view.getSettingsPanel().getAddPlayerEvent() .add(new IListener() { @Override @@ -193,8 +201,7 @@ public class SettingsControl { break; } - settings.getPlayerList().add( - new PlayerSettings("Spieler " + num, color)); + settings.getPlayerList().add(new PlayerSettings("Spieler " + num, color)); update(); } @@ -235,9 +242,8 @@ public class SettingsControl { private void update() { view.getSettingsPanel().enableRemovePlayerButtons( settings.getPlayerList().size() > 2); - view.getSettingsPanel() - .enableAddPlayerButton( - settings.getPlayerList().size() < ISettingsPanel.PLAYER_COLORS.length); + view.getSettingsPanel().enableAddPlayerButton( + settings.getPlayerList().size() < ISettingsPanel.PLAYER_COLORS.length); checkSettings(); @@ -258,9 +264,8 @@ public class SettingsControl { for (int j = i + 1; j < settings.getPlayerList().size(); ++j) { if (settings.getPlayerList().get(j).getName().equals(name)) { - view.getSettingsPanel() - .setError( - ISettingsPanel.SettingsError.DUPLICATE_PLAYER_NAME_ERROR); + view.getSettingsPanel().setError( + ISettingsPanel.SettingsError.DUPLICATE_PLAYER_NAME_ERROR); view.getSettingsPanel().enableStartGameButton(false); return false; } @@ -269,8 +274,7 @@ public class SettingsControl { int totalStonesDealt = settings.getNumberOfStonesDealt() * settings.getPlayerList().size(); - int totalStones = settings.getHighestValue() - * settings.getStoneSetNumber() + int totalStones = settings.getHighestValue() * settings.getStoneSetNumber() * settings.getStoneColors().size() + settings.getJokerNumber(); if (totalStones <= totalStonesDealt) { @@ -280,13 +284,26 @@ public class SettingsControl { return false; } + if (settings.getStoneColors().size() < 3) { + view.getSettingsPanel().setError( + ISettingsPanel.SettingsError.NOT_ENOUGH_COLORS_ERROR); + view.getSettingsPanel().enableStartGameButton(false); + return false; + } + view.getSettingsPanel().setError(ISettingsPanel.SettingsError.NO_ERROR); view.getSettingsPanel().enableStartGameButton(true); + checkWarnings(); + + return true; + } + + private void checkWarnings() { if (settings.getInitialMeldThreshold() >= 100) { view.getSettingsPanel().setError( ISettingsPanel.SettingsError.TOO_HIGH_THRESHOLD_WARNING); - return true; + return; } boolean humanPlayerFound = false; @@ -300,10 +317,8 @@ public class SettingsControl { if (!humanPlayerFound) { view.getSettingsPanel().setError( ISettingsPanel.SettingsError.COMPUTER_PLAYERS_ONLY_WARNING); - return true; + return; } - - return true; } private void startGame() { diff --git a/src/jrummikub/model/StoneColor.java b/src/jrummikub/model/StoneColor.java index 47c5227..4a7286a 100644 --- a/src/jrummikub/model/StoneColor.java +++ b/src/jrummikub/model/StoneColor.java @@ -15,7 +15,7 @@ public enum StoneColor { /** */ VIOLET, /** */ - BROWN, + AQUA, /** */ - WHITE + GRAY } diff --git a/src/jrummikub/view/ISettingsPanel.java b/src/jrummikub/view/ISettingsPanel.java index 1d030fe..fbe1932 100644 --- a/src/jrummikub/view/ISettingsPanel.java +++ b/src/jrummikub/view/ISettingsPanel.java @@ -158,6 +158,8 @@ public interface ISettingsPanel { NO_PLAYER_NAME_ERROR, /** More Stones than present would be dealed */ NOT_ENOUGH_STONES_ERROR, + /** Less than 3 colors are selected */ + NOT_ENOUGH_COLORS_ERROR, // warnings /** threshold higher 100 */ diff --git a/src/jrummikub/view/impl/SettingsPanel.java b/src/jrummikub/view/impl/SettingsPanel.java index 95089a3..3f577cc 100644 --- a/src/jrummikub/view/impl/SettingsPanel.java +++ b/src/jrummikub/view/impl/SettingsPanel.java @@ -16,7 +16,10 @@ import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.util.ArrayList; import java.util.Arrays; +import java.util.HashMap; +import java.util.HashSet; import java.util.List; +import java.util.Map; import java.util.Set; import javax.swing.Box; @@ -32,6 +35,7 @@ import javax.swing.JScrollPane; import javax.swing.JSpinner; import javax.swing.JTabbedPane; import javax.swing.JTextField; +import javax.swing.JToggleButton; import javax.swing.SpinnerNumberModel; import javax.swing.border.CompoundBorder; import javax.swing.border.EmptyBorder; @@ -72,6 +76,9 @@ class SettingsPanel extends JPanel implements ISettingsPanel { private JSpinner numberOfStonesDealtSpinner; private JSpinner jokerNumberSpinner; + private JPanel colorSelectionPanel; + private Map colorButtons = new HashMap(); + private Event startGameEvent = new Event(); private Event addPlayerEvent = new Event(); private Event1 removePlayerEvent = new Event1(); @@ -138,32 +145,37 @@ class SettingsPanel extends JPanel implements ISettingsPanel { @Override public void setError(SettingsError error) { switch (error) { - case NO_ERROR: - errorMessageLabel.setText(" "); - break; - case DUPLICATE_PLAYER_NAME_ERROR: - errorMessageLabel - .setText("Jeder Spielername darf nur einmal verwendet werden."); - errorMessageLabel.setForeground(Color.RED); - break; - case NO_PLAYER_NAME_ERROR: - errorMessageLabel.setText("Jeder Spieler muss einen Namen haben."); - errorMessageLabel.setForeground(Color.RED); - break; - case NOT_ENOUGH_STONES_ERROR: - errorMessageLabel - .setText("Es gibt nicht genug Steine f\u00fcr die gew\u00e4hlte Spieleranzahl."); - errorMessageLabel.setForeground(Color.RED); - break; - case COMPUTER_PLAYERS_ONLY_WARNING: - errorMessageLabel.setText("Es gibt keinen menschlichen Spieler."); - errorMessageLabel.setForeground(Color.ORANGE.darker()); - break; - case TOO_HIGH_THRESHOLD_WARNING: - errorMessageLabel - .setText("Die gew\u00e4hlte Schranke ist m\u00f6glicherweise zu hoch"); - errorMessageLabel.setForeground(Color.ORANGE.darker()); - break; + case NO_ERROR: + errorMessageLabel.setText(" "); + break; + case DUPLICATE_PLAYER_NAME_ERROR: + errorMessageLabel + .setText("Jeder Spielername darf nur einmal verwendet werden."); + errorMessageLabel.setForeground(Color.RED); + break; + case NO_PLAYER_NAME_ERROR: + errorMessageLabel.setText("Jeder Spieler muss einen Namen haben."); + errorMessageLabel.setForeground(Color.RED); + break; + case NOT_ENOUGH_STONES_ERROR: + errorMessageLabel + .setText("Es gibt nicht genug Steine f\u00fcr die gew\u00e4hlte Spieleranzahl."); + errorMessageLabel.setForeground(Color.RED); + break; + case NOT_ENOUGH_COLORS_ERROR: + errorMessageLabel + .setText("Es m\u00fcssen mindestens drei Farben ausgew\u00e4hlt werden."); + errorMessageLabel.setForeground(Color.RED); + break; + case COMPUTER_PLAYERS_ONLY_WARNING: + errorMessageLabel.setText("Es gibt keinen menschlichen Spieler."); + errorMessageLabel.setForeground(Color.ORANGE.darker()); + break; + case TOO_HIGH_THRESHOLD_WARNING: + errorMessageLabel + .setText("Die gew\u00e4hlte Schranke ist m\u00f6glicherweise zu hoch"); + errorMessageLabel.setForeground(Color.ORANGE.darker()); + break; } } @@ -188,8 +200,7 @@ class SettingsPanel extends JPanel implements ISettingsPanel { @Override public void setGameSettings(GameSettings gameSettings) { - while (playerSettingsPanels.size() > gameSettings.getPlayerList() - .size()) { + while (playerSettingsPanels.size() > gameSettings.getPlayerList().size()) { removePlayerSettingsPanel(); } @@ -197,13 +208,18 @@ class SettingsPanel extends JPanel implements ISettingsPanel { updatePlayerSettingsPanel(i, gameSettings.getPlayerList().get(i)); } - initialMeldThresholdSpinner.setValue(gameSettings - .getInitialMeldThreshold()); + initialMeldThresholdSpinner + .setValue(gameSettings.getInitialMeldThreshold()); stoneSetNumberSpinner.setValue(gameSettings.getStoneSetNumber()); highestValueSpinner.setValue(gameSettings.getHighestValue()); numberOfStonesDealtSpinner.setValue(gameSettings.getNumberOfStonesDealt()); jokerNumberSpinner.setValue(gameSettings.getJokerNumber()); + for (StoneColor color : StoneColor.values()) { + colorButtons.get(color).getModel() + .setSelected(gameSettings.getStoneColors().contains(color)); + } + playerSettingsViewport.revalidate(); } @@ -223,8 +239,8 @@ class SettingsPanel extends JPanel implements ISettingsPanel { } private void removePlayerSettingsPanel() { - PlayerSettingsPanel p = playerSettingsPanels - .remove(playerSettingsPanels.size() - 1); + PlayerSettingsPanel p = playerSettingsPanels.remove(playerSettingsPanels + .size() - 1); playerSettingsViewport.remove(p); } @@ -246,8 +262,7 @@ class SettingsPanel extends JPanel implements ISettingsPanel { playerSettingsViewport.add(addPlayerPanel); addPlayerButton = new JButton("+"); - addPlayerButton - .setFont(addPlayerButton.getFont().deriveFont(Font.BOLD)); + addPlayerButton.setFont(addPlayerButton.getFont().deriveFont(Font.BOLD)); addPlayerButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { @@ -284,18 +299,62 @@ class SettingsPanel extends JPanel implements ISettingsPanel { changeNumberOfStonesDealtEvent); makeOptionLabel(4, "Jokeranzahl:"); - jokerNumberSpinner = makeOptionSpinner(4, 1, 999, 1, - changeJokerNumberEvent); + jokerNumberSpinner = makeOptionSpinner(4, 1, 999, 1, changeJokerNumberEvent); + + makeOptionLabel(5, "Steinfarben:"); + createColorSelectionPanel(5); GridBagConstraints c = new GridBagConstraints(); c.gridx = 1; - c.gridy = 5; + c.gridy = 6; c.fill = GridBagConstraints.BOTH; c.weightx = 1; c.weighty = 1; optionsPanel.add(makeFiller(), c); } + private void createColorSelectionPanel(int row) { + GridBagConstraints c = new GridBagConstraints(); + c.anchor = GridBagConstraints.WEST; + c.gridx = 1; + c.gridy = row; + + colorSelectionPanel = new JPanel(); + optionsPanel.add(colorSelectionPanel, c); + + for (StoneColor color : StoneColor.values()) { + createColorButton(color); + } + } + + private void createColorButton(final StoneColor color) { + JToggleButton button = new JToggleButton(ImageUtil.createColorIcon( + StonePainter.getColor(color), 12, 1)); + button.addChangeListener(new ChangeListener() { + + @Override + public void stateChanged(ChangeEvent e) { + emitColorChangeEvent(); + } + }); + + colorSelectionPanel.add(button); + + colorButtons.put(color, button); + } + + private void emitColorChangeEvent() { + Set colors = new HashSet(); + + for (StoneColor color : StoneColor.values()) { + if (colorButtons.get(color).getModel().isSelected()) { + colors.add(color); + } + } + + changeStoneColorsEvent.emit(colors); + } + private JSpinner makeOptionSpinner(int row, int min, int max, int step, final Event1 targetEvent) { GridBagConstraints c = new GridBagConstraints(); @@ -305,8 +364,8 @@ class SettingsPanel extends JPanel implements ISettingsPanel { c.insets = new Insets(2, 2, 2, 2); final JSpinner spinner = new JSpinner(); spinner.setModel(new SpinnerNumberModel(min, min, max, step)); - spinner.setPreferredSize(new Dimension(60, - spinner.getMinimumSize().height)); + spinner + .setPreferredSize(new Dimension(60, spinner.getMinimumSize().height)); spinner.addChangeListener(new ChangeListener() { @Override public void stateChanged(ChangeEvent e) { @@ -359,8 +418,8 @@ class SettingsPanel extends JPanel implements ISettingsPanel { }); add(startButton, c); - setBorder(new CompoundBorder(new LineBorder(Color.BLACK), - new EmptyBorder(10, 10, 10, 10))); + setBorder(new CompoundBorder(new LineBorder(Color.BLACK), new EmptyBorder( + 10, 10, 10, 10))); } private class PlayerSettingsPanel extends JPanel { @@ -380,8 +439,8 @@ class SettingsPanel extends JPanel implements ISettingsPanel { if (index != playerType.getSelectedIndex()) { playerType.setSelectedIndex(index); } - colorButton.setIcon(ImageUtil.createColorIcon(settings.getColor(), - 16, 2)); + colorButton + .setIcon(ImageUtil.createColorIcon(settings.getColor(), 16, 2)); } public void enableRemoveButton(boolean enable) { @@ -429,8 +488,7 @@ class SettingsPanel extends JPanel implements ISettingsPanel { c.gridy = 0; add(colorButton, c); - nameField.getDocument().addDocumentListener( - new NameChangeListener()); + nameField.getDocument().addDocumentListener(new NameChangeListener()); c2.gridy = 0; c2.weightx = 1; c2.fill = GridBagConstraints.HORIZONTAL; @@ -463,9 +521,8 @@ class SettingsPanel extends JPanel implements ISettingsPanel { public Dimension getPreferredSize() { Dimension preferredSize = super.getPreferredSize(); for (Component component : this.getComponents()) { - preferredSize.setSize( - preferredSize.width, - Math.max(preferredSize.height, + preferredSize + .setSize(preferredSize.width, Math.max(preferredSize.height, component.getPreferredSize().height)); } return preferredSize; @@ -475,9 +532,8 @@ class SettingsPanel extends JPanel implements ISettingsPanel { public Dimension getMaximumSize() { Dimension preferredSize = super.getPreferredSize(); for (Component component : this.getComponents()) { - preferredSize.setSize( - Integer.MAX_VALUE, - Math.max(preferredSize.height, + preferredSize + .setSize(Integer.MAX_VALUE, Math.max(preferredSize.height, component.getPreferredSize().height)); } return preferredSize; @@ -492,8 +548,8 @@ class SettingsPanel extends JPanel implements ISettingsPanel { } private void changeType() { - changePlayerTypeEvent.emit(playerNumber, TurnControlFactory.Type - .values()[playerType.getSelectedIndex()]); + changePlayerTypeEvent.emit(playerNumber, + TurnControlFactory.Type.values()[playerType.getSelectedIndex()]); } private void remove() { diff --git a/src/jrummikub/view/impl/StonePainter.java b/src/jrummikub/view/impl/StonePainter.java index 572ba76..e2dae24 100644 --- a/src/jrummikub/view/impl/StonePainter.java +++ b/src/jrummikub/view/impl/StonePainter.java @@ -71,24 +71,24 @@ class StonePainter { return new Color(r > 255 ? 255 : r, g > 255 ? 255 : g, b > 255 ? 255 : b); } - private static Color getColor(StoneColor color) { + public static Color getColor(StoneColor color) { switch (color) { case BLACK: - return new Color(0.15f, 0.15f, 0.15f); + return new Color(0.0f, 0.0f, 0.0f); case BLUE: return new Color(0.0f, 0.0f, 1.0f); case ORANGE: return new Color(1.0f, 0.4f, 0.0f); case RED: return new Color(0.9f, 0.0f, 0.25f); - case BROWN: - return new Color(0.5f, 0.25f, 0.0f); + case AQUA: + return new Color(0.0f, 0.85f, 0.75f); case GREEN: - return new Color(0.0f, 0.75f, 0.0f); + return new Color(0.0f, 0.65f, 0.0f); case VIOLET: return new Color(0.75f, 0.325f, 0.75f); - case WHITE: - return new Color(1.0f, 1.0f, 1.0f); + case GRAY: + return new Color(0.5f, 0.5f, 0.5f); } return null; -- cgit v1.2.3