From 5436407515a14ed6a53276c26f0b8403ec27020f Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Sun, 1 May 2011 19:02:21 +0200 Subject: 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 --- src/jrummikub/view/impl/PlayerPanel.java | 160 ++++++++++++++++++------------- 1 file changed, 91 insertions(+), 69 deletions(-) (limited to 'src/jrummikub/view/impl/PlayerPanel.java') 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("
Sort by
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("
Sort by
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); } } -- cgit v1.2.3