diff options
author | Matthias Schiffer <mschiffer@universe-factory.net> | 2011-05-01 19:02:21 +0200 |
---|---|---|
committer | Matthias Schiffer <mschiffer@universe-factory.net> | 2011-05-01 19:02:21 +0200 |
commit | 5436407515a14ed6a53276c26f0b8403ec27020f (patch) | |
tree | b180da1f7fbaa2a5e0a5f018c1ad968cf62dc03f /src/jrummikub | |
parent | 4ef731cd6448a66c18abddf1d99c01145897f53b (diff) | |
download | JRummikub-5436407515a14ed6a53276c26f0b8403ec27020f.tar JRummikub-5436407515a14ed6a53276c26f0b8403ec27020f.zip |
Make player panel behave better with different window sizes
git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@51 72836036-5685-4462-b002-a69064685172
Diffstat (limited to 'src/jrummikub')
-rw-r--r-- | src/jrummikub/view/impl/Board.java | 41 | ||||
-rw-r--r-- | src/jrummikub/view/impl/PlayerPanel.java | 160 | ||||
-rw-r--r-- | src/jrummikub/view/impl/StonePainter.java | 2 | ||||
-rw-r--r-- | src/jrummikub/view/impl/View.java | 40 |
4 files changed, 155 insertions, 88 deletions
diff --git a/src/jrummikub/view/impl/Board.java b/src/jrummikub/view/impl/Board.java index 2d51cb2..e633233 100644 --- a/src/jrummikub/view/impl/Board.java +++ b/src/jrummikub/view/impl/Board.java @@ -1,10 +1,13 @@ package jrummikub.view.impl; import java.awt.Color; +import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Insets; import java.awt.RenderingHints; +import java.awt.event.ComponentAdapter; +import java.awt.event.ComponentEvent; import java.awt.image.BufferedImage; import java.util.Collection; import java.util.Collections; @@ -17,7 +20,10 @@ import jrummikub.model.Stone; import jrummikub.view.IBoard; @SuppressWarnings("serial") -public class Board extends StonePanel implements 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( @@ -27,12 +33,26 @@ public class Board extends StonePanel implements IBoard { image.paintIcon(null, BACKGROUND.createGraphics(), 0, 0); } + private BufferedImage scaledBackground = BACKGROUND; private Map<Stone, Position> stones = Collections.emptyMap(); private Collection<Stone> selectedStones = Collections.emptyList(); 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())); + } + }); } private BufferedImage getScaledBackground(int size) { @@ -50,21 +70,22 @@ public class Board extends StonePanel implements IBoard { 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/2; - BufferedImage scaledBackground = getScaledBackground(size); + int size = height/BOARD_HEIGHT; - for(int xpos = 0; xpos < width; xpos += size) { - g.drawImage(scaledBackground, xpos, 0, null); - } - for(int xpos = -size/3; xpos < width; xpos += size) { - g.drawImage(scaledBackground, xpos, size, null); + 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); + g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); - getStonePainter().setScale(size*StonePainter.PIXEL_SCALE); - for (Map.Entry<Stone, Position> entry : stones.entrySet()) { getStonePainter().paintStone(g, entry.getKey(), entry.getValue(), selectedStones.contains(entry.getKey())); diff --git a/src/jrummikub/view/impl/PlayerPanel.java b/src/jrummikub/view/impl/PlayerPanel.java index 55a06c3..abf7cc9 100644 --- a/src/jrummikub/view/impl/PlayerPanel.java +++ b/src/jrummikub/view/impl/PlayerPanel.java @@ -1,29 +1,37 @@ package jrummikub.view.impl; import java.awt.Color; -import java.awt.Dimension; -import java.awt.GridBagConstraints; -import java.awt.GridBagLayout; import java.awt.Insets; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import java.awt.event.ComponentAdapter; +import java.awt.event.ComponentEvent; +import java.awt.event.ComponentListener; import java.text.DecimalFormat; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JProgressBar; +import javax.swing.border.EmptyBorder; import jrummikub.util.Event; import jrummikub.util.IEvent; import jrummikub.view.IPlayerPanel; @SuppressWarnings("serial") -public class PlayerPanel extends JPanel implements IPlayerPanel { +class PlayerPanel extends JPanel implements IPlayerPanel { + private final static int SIDE_PANEL_INSET = 15; + 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; @@ -68,117 +76,131 @@ public class PlayerPanel extends JPanel implements IPlayerPanel { } - JPanel createLeftPanel() { - JPanel panel = new JPanel(); - panel.setPreferredSize(new Dimension(0, 0)); - GridBagLayout layout = new GridBagLayout(); - GridBagConstraints c = new GridBagConstraints(); - panel.setLayout(layout); - panel.setOpaque(false); - + 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)); currentPlayerNameLabel = new JLabel(); currentPlayerNameLabel.setHorizontalAlignment(JLabel.CENTER); currentPlayerNameLabel.setHorizontalTextPosition(JLabel.CENTER); currentPlayerNameLabel.setVerticalAlignment(JLabel.CENTER); currentPlayerNameLabel.setVerticalTextPosition(JLabel.CENTER); - currentPlayerNameLabel.setPreferredSize(new Dimension(180, 30)); - c.weightx = 0; - c.weighty = 1; - c.gridwidth = GridBagConstraints.REMAINDER; - c.insets = new Insets(25, 0, 0, 0); - layout.setConstraints(currentPlayerNameLabel, c); - panel.add(currentPlayerNameLabel); + leftPanel.add(currentPlayerNameLabel); sortByNumberButton = new JButton("<html><center>Sort by<br>number"); - sortByNumberButton.setPreferredSize(new Dimension(85, 50)); sortByNumberButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { sortByNumberEvent.fire(); }}); - - c.gridwidth = GridBagConstraints.RELATIVE; - c.gridheight = GridBagConstraints.REMAINDER; - c.insets = new Insets(15, 0, 20, 5); - layout.setConstraints(sortByNumberButton, c); - panel.add(sortByNumberButton); + leftPanel.add(sortByNumberButton); sortByColorButton = new JButton("<html><center>Sort by<br>color"); - sortByColorButton.setPreferredSize(new Dimension(85, 50)); sortByColorButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { sortByColorEvent.fire(); }}); + leftPanel.add(sortByColorButton); - c.gridwidth = GridBagConstraints.REMAINDER; - c.insets = new Insets(15, 5, 20, 0); - layout.setConstraints(sortByColorButton, c); - panel.add(sortByColorButton); - - return panel; + 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; + + if (width > SIDE_PANEL_MAX_WIDTH) { + 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; + + 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); + } + }); } - JPanel createRightPanel() { - JPanel panel = new JPanel(); - panel.setPreferredSize(new Dimension(0, 0)); - GridBagLayout layout = new GridBagLayout(); - GridBagConstraints c = new GridBagConstraints(); - panel.setLayout(layout); - panel.setOpaque(false); + 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)); timeBar = new JProgressBar(0, 60); timeBar.setStringPainted(true); - timeBar.setPreferredSize(new Dimension(180, 30)); - c.weightx = 0; - c.weighty = 1; - c.gridwidth = GridBagConstraints.REMAINDER; - c.insets = new Insets(25, 0, 0, 0); - layout.setConstraints(timeBar, c); - panel.add(timeBar); + rightPanel.add(timeBar); endTurnButton = new JButton("End turn"); - endTurnButton.setPreferredSize(new Dimension(180, 50)); endTurnButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { endTurnEvent.fire(); }}); - c.gridheight = GridBagConstraints.REMAINDER; - c.insets = new Insets(15, 0, 20, 0); - layout.setConstraints(endTurnButton, c); - panel.add(endTurnButton); + rightPanel.add(endTurnButton); - return panel; + 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; + + if (width > SIDE_PANEL_MAX_WIDTH) { + 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); + + timeBar.setBounds(x, y, width, 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 boardWidth = board.getWidth(); + 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); + + leftPanel.validate(); + rightPanel.validate(); } PlayerPanel() { - GridBagLayout layout = new GridBagLayout(); - GridBagConstraints c = new GridBagConstraints(); - setLayout(layout); + setLayout(null); setBackground(Color.LIGHT_GRAY); - JPanel leftPanel = createLeftPanel(); - c.fill = GridBagConstraints.BOTH; - c.weightx = 1; - c.weighty = 1; - c.gridheight = GridBagConstraints.REMAINDER; - layout.setConstraints(leftPanel, c); + createLeftPanel(); add(leftPanel); board = new Board(); - c.weightx = 3; - layout.setConstraints(board, c); add(board); - JPanel rightPanel = createRightPanel(); - c.weightx = 1; - c.gridwidth = GridBagConstraints.REMAINDER; - layout.setConstraints(rightPanel, c); + 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 9cda8f6..c9ff4b6 100644 --- a/src/jrummikub/view/impl/StonePainter.java +++ b/src/jrummikub/view/impl/StonePainter.java @@ -26,7 +26,7 @@ class StonePainter { private static final float BRIGHTER_SCALE = 1.15f; - public static final float PIXEL_SCALE = ASPECT_RATIO/DEFAULT_WIDTH; + public static final float HEIGHT_SCALE = ASPECT_RATIO/DEFAULT_WIDTH; private float scale; diff --git a/src/jrummikub/view/impl/View.java b/src/jrummikub/view/impl/View.java index 31235c6..4cbad44 100644 --- a/src/jrummikub/view/impl/View.java +++ b/src/jrummikub/view/impl/View.java @@ -1,8 +1,10 @@ package jrummikub.view.impl; -import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; +import java.awt.Insets; +import java.awt.event.ComponentAdapter; +import java.awt.event.ComponentEvent; import java.util.Collection; import javax.swing.JFrame; @@ -17,9 +19,15 @@ public class View extends JFrame implements IView { private Table table; private PlayerPanel playerPanel; - private final static int PLAYER_PANEL_HEIGHT = 140; + 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); + } + public ITable getTable() { return table; } @@ -31,19 +39,35 @@ public class View extends JFrame implements IView { public View() { super("JRummikub"); + setLayout(null); setSize(1000, 700); setDefaultCloseOperation(EXIT_ON_CLOSE); - setLayout(new BorderLayout()); - table = new Table(); - add(table, BorderLayout.CENTER); + add(table); playerPanel = new PlayerPanel(); - playerPanel.setBorder(new CustomBorder(Color.BLACK, 1, 0, 0, 0)); - playerPanel.setPreferredSize(new Dimension(0, PLAYER_PANEL_HEIGHT+1)); - add(playerPanel, BorderLayout.SOUTH); + 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; + 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); + } + }); setVisible(true); } |