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
This commit is contained in:
Matthias Schiffer 2011-05-01 19:02:21 +02:00
parent 4ef731cd64
commit 5436407515
4 changed files with 155 additions and 88 deletions

View file

@ -1,10 +1,13 @@
package jrummikub.view.impl; package jrummikub.view.impl;
import java.awt.Color; import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics; import java.awt.Graphics;
import java.awt.Graphics2D; import java.awt.Graphics2D;
import java.awt.Insets; import java.awt.Insets;
import java.awt.RenderingHints; import java.awt.RenderingHints;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
@ -17,7 +20,10 @@ import jrummikub.model.Stone;
import jrummikub.view.IBoard; import jrummikub.view.IBoard;
@SuppressWarnings("serial") @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; private final static BufferedImage BACKGROUND;
static { static {
ImageIcon image = new ImageIcon( ImageIcon image = new ImageIcon(
@ -27,12 +33,26 @@ public class Board extends StonePanel implements IBoard {
image.paintIcon(null, BACKGROUND.createGraphics(), 0, 0); image.paintIcon(null, BACKGROUND.createGraphics(), 0, 0);
} }
private BufferedImage scaledBackground = BACKGROUND;
private Map<Stone, Position> stones = Collections.emptyMap(); private Map<Stone, Position> stones = Collections.emptyMap();
private Collection<Stone> selectedStones = Collections.emptyList(); private Collection<Stone> selectedStones = Collections.emptyList();
Board() { Board() {
setBorder(new CustomBorder(Color.DARK_GRAY, 0, 1, 0, 1)); 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) { private BufferedImage getScaledBackground(int size) {
@ -50,21 +70,22 @@ public class Board extends StonePanel implements IBoard {
Insets insets = getInsets(); 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;
Graphics2D g = (Graphics2D)g1.create(x, y, width, height); Graphics2D g = (Graphics2D)g1.create(x, y, width, height);
int size = height/2; int size = height/BOARD_HEIGHT;
BufferedImage scaledBackground = getScaledBackground(size);
for(int xpos = 0; xpos < width; xpos += size) { if (scaledBackground.getHeight() != size)
g.drawImage(scaledBackground, xpos, 0, null); 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);
} }
for(int xpos = -size/3; xpos < width; xpos += size) {
g.drawImage(scaledBackground, xpos, size, null);
} }
getStonePainter().setScale(size*StonePainter.HEIGHT_SCALE);
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON); RenderingHints.VALUE_ANTIALIAS_ON);
getStonePainter().setScale(size*StonePainter.PIXEL_SCALE);
for (Map.Entry<Stone, Position> entry : stones.entrySet()) { for (Map.Entry<Stone, Position> entry : stones.entrySet()) {
getStonePainter().paintStone(g, entry.getKey(), entry.getValue(), getStonePainter().paintStone(g, entry.getKey(), entry.getValue(),
selectedStones.contains(entry.getKey())); selectedStones.contains(entry.getKey()));

View file

@ -1,29 +1,37 @@
package jrummikub.view.impl; package jrummikub.view.impl;
import java.awt.Color; import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets; import java.awt.Insets;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; 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 java.text.DecimalFormat;
import javax.swing.JButton; import javax.swing.JButton;
import javax.swing.JLabel; import javax.swing.JLabel;
import javax.swing.JPanel; import javax.swing.JPanel;
import javax.swing.JProgressBar; import javax.swing.JProgressBar;
import javax.swing.border.EmptyBorder;
import jrummikub.util.Event; import jrummikub.util.Event;
import jrummikub.util.IEvent; import jrummikub.util.IEvent;
import jrummikub.view.IPlayerPanel; import jrummikub.view.IPlayerPanel;
@SuppressWarnings("serial") @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 final static DecimalFormat secondFormat = new DecimalFormat("00");
private Board board; private Board board;
JPanel leftPanel, rightPanel;
private JLabel currentPlayerNameLabel; private JLabel currentPlayerNameLabel;
private JButton sortByNumberButton; private JButton sortByNumberButton;
private JButton sortByColorButton; private JButton sortByColorButton;
@ -68,117 +76,131 @@ public class PlayerPanel extends JPanel implements IPlayerPanel {
} }
JPanel createLeftPanel() { private void createLeftPanel() {
JPanel panel = new JPanel(); leftPanel = new JPanel();
panel.setPreferredSize(new Dimension(0, 0)); leftPanel.setLayout(null);
GridBagLayout layout = new GridBagLayout(); leftPanel.setOpaque(false);
GridBagConstraints c = new GridBagConstraints(); leftPanel.setBorder(new EmptyBorder(SIDE_PANEL_INSET, SIDE_PANEL_INSET, SIDE_PANEL_INSET, SIDE_PANEL_INSET));
panel.setLayout(layout);
panel.setOpaque(false);
currentPlayerNameLabel = new JLabel(); currentPlayerNameLabel = new JLabel();
currentPlayerNameLabel.setHorizontalAlignment(JLabel.CENTER); currentPlayerNameLabel.setHorizontalAlignment(JLabel.CENTER);
currentPlayerNameLabel.setHorizontalTextPosition(JLabel.CENTER); currentPlayerNameLabel.setHorizontalTextPosition(JLabel.CENTER);
currentPlayerNameLabel.setVerticalAlignment(JLabel.CENTER); currentPlayerNameLabel.setVerticalAlignment(JLabel.CENTER);
currentPlayerNameLabel.setVerticalTextPosition(JLabel.CENTER); currentPlayerNameLabel.setVerticalTextPosition(JLabel.CENTER);
currentPlayerNameLabel.setPreferredSize(new Dimension(180, 30)); leftPanel.add(currentPlayerNameLabel);
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);
sortByNumberButton = new JButton("<html><center>Sort by<br>number"); sortByNumberButton = new JButton("<html><center>Sort by<br>number");
sortByNumberButton.setPreferredSize(new Dimension(85, 50));
sortByNumberButton.addActionListener(new ActionListener() { sortByNumberButton.addActionListener(new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent arg0) { public void actionPerformed(ActionEvent arg0) {
sortByNumberEvent.fire(); sortByNumberEvent.fire();
}}); }});
leftPanel.add(sortByNumberButton);
c.gridwidth = GridBagConstraints.RELATIVE;
c.gridheight = GridBagConstraints.REMAINDER;
c.insets = new Insets(15, 0, 20, 5);
layout.setConstraints(sortByNumberButton, c);
panel.add(sortByNumberButton);
sortByColorButton = new JButton("<html><center>Sort by<br>color"); sortByColorButton = new JButton("<html><center>Sort by<br>color");
sortByColorButton.setPreferredSize(new Dimension(85, 50));
sortByColorButton.addActionListener(new ActionListener() { sortByColorButton.addActionListener(new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent arg0) { public void actionPerformed(ActionEvent arg0) {
sortByColorEvent.fire(); sortByColorEvent.fire();
}}); }});
leftPanel.add(sortByColorButton);
c.gridwidth = GridBagConstraints.REMAINDER; leftPanel.addComponentListener(new ComponentAdapter() {
c.insets = new Insets(15, 5, 20, 0); @Override
layout.setConstraints(sortByColorButton, c); public void componentResized(ComponentEvent e) {
panel.add(sortByColorButton); 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;
return panel; if (width > SIDE_PANEL_MAX_WIDTH) {
x += (width-SIDE_PANEL_MAX_WIDTH)/4;
width = width/2+SIDE_PANEL_MAX_WIDTH/2;
} }
JPanel createRightPanel() { int firstLineHeight = (int)((height-SIDE_PANEL_SEPARATOR)*SIDE_PANEL_FIRST_LINE_HEIGHT);
JPanel panel = new JPanel(); int buttonWidth = (width-SIDE_PANEL_SEPARATOR)/2;
panel.setPreferredSize(new Dimension(0, 0));
GridBagLayout layout = new GridBagLayout(); currentPlayerNameLabel.setBounds(x, y, width, firstLineHeight);
GridBagConstraints c = new GridBagConstraints(); sortByNumberButton.setBounds(x, y+firstLineHeight+SIDE_PANEL_SEPARATOR, buttonWidth, height-SIDE_PANEL_SEPARATOR-firstLineHeight);
panel.setLayout(layout); sortByColorButton.setBounds(x+buttonWidth+SIDE_PANEL_SEPARATOR, y+firstLineHeight+SIDE_PANEL_SEPARATOR, buttonWidth, height-SIDE_PANEL_SEPARATOR-firstLineHeight);
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 = new JProgressBar(0, 60);
timeBar.setStringPainted(true); timeBar.setStringPainted(true);
timeBar.setPreferredSize(new Dimension(180, 30)); rightPanel.add(timeBar);
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);
endTurnButton = new JButton("End turn"); endTurnButton = new JButton("End turn");
endTurnButton.setPreferredSize(new Dimension(180, 50));
endTurnButton.addActionListener(new ActionListener() { endTurnButton.addActionListener(new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent arg0) { public void actionPerformed(ActionEvent arg0) {
endTurnEvent.fire(); endTurnEvent.fire();
}}); }});
c.gridheight = GridBagConstraints.REMAINDER; rightPanel.add(endTurnButton);
c.insets = new Insets(15, 0, 20, 0);
layout.setConstraints(endTurnButton, c);
panel.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() { PlayerPanel() {
GridBagLayout layout = new GridBagLayout(); setLayout(null);
GridBagConstraints c = new GridBagConstraints();
setLayout(layout);
setBackground(Color.LIGHT_GRAY); setBackground(Color.LIGHT_GRAY);
JPanel leftPanel = createLeftPanel(); createLeftPanel();
c.fill = GridBagConstraints.BOTH;
c.weightx = 1;
c.weighty = 1;
c.gridheight = GridBagConstraints.REMAINDER;
layout.setConstraints(leftPanel, c);
add(leftPanel); add(leftPanel);
board = new Board(); board = new Board();
c.weightx = 3;
layout.setConstraints(board, c);
add(board); add(board);
JPanel rightPanel = createRightPanel(); createRightPanel();
c.weightx = 1;
c.gridwidth = GridBagConstraints.REMAINDER;
layout.setConstraints(rightPanel, c);
add(rightPanel); add(rightPanel);
ComponentListener rescaleListener = new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
rescale();
}
};
addComponentListener(rescaleListener);
board.addComponentListener(rescaleListener);
} }
} }

View file

@ -26,7 +26,7 @@ class StonePainter {
private static final float BRIGHTER_SCALE = 1.15f; 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; private float scale;

View file

@ -1,8 +1,10 @@
package jrummikub.view.impl; package jrummikub.view.impl;
import java.awt.BorderLayout;
import java.awt.Color; import java.awt.Color;
import java.awt.Dimension; import java.awt.Dimension;
import java.awt.Insets;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.util.Collection; import java.util.Collection;
import javax.swing.JFrame; import javax.swing.JFrame;
@ -17,9 +19,15 @@ public class View extends JFrame implements IView {
private Table table; private Table table;
private PlayerPanel playerPanel; 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() { public ITable getTable() {
return table; return table;
} }
@ -31,19 +39,35 @@ public class View extends JFrame implements IView {
public View() { public View() {
super("JRummikub"); super("JRummikub");
setLayout(null);
setSize(1000, 700); setSize(1000, 700);
setDefaultCloseOperation(EXIT_ON_CLOSE); setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new BorderLayout());
table = new Table(); table = new Table();
add(table, BorderLayout.CENTER); add(table);
playerPanel = new PlayerPanel(); playerPanel = new PlayerPanel();
playerPanel.setBorder(new CustomBorder(Color.BLACK, 1, 0, 0, 0)); playerPanel.setBorder(new CustomBorder(Color.BLACK, PLAYER_PANEL_BORDER_WIDTH, 0, 0, 0));
playerPanel.setPreferredSize(new Dimension(0, PLAYER_PANEL_HEIGHT+1)); add(playerPanel);
add(playerPanel, BorderLayout.SOUTH);
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); setVisible(true);
} }