📄 boardlayout.java
字号:
package ergo.ui;
// $Id: BoardLayout.java,v 1.2 1999/08/13 01:20:07 sigue Exp $
/*
* Copyright (C) 1999 Carl L. Gay and Antranig M. Basman.
* See the file copyright.txt, distributed with this software,
* for further information.
*/
import java.awt.*;
/*
* BoardLayout implements a layout manager much like the BorderLayout
* layout manager but it guarantees that the panel in the "Center"
* position will always be square, and each side will be an integral
* multiple of the number of cells in the board. The edge panels may be
* stretched to take up any extra room.
*/
public class BoardLayout implements LayoutManager {
private Component board;
private Component north;
private Component south;
private Component east;
private Component west;
private Component status;
private Component controls;
private int boardSize;
public BoardLayout(int boardsize) {
boardSize = boardsize;
}
// This must be implemented by all LayoutManager classes.
public void addLayoutComponent(String name, Component comp) {
if (name.equalsIgnoreCase("North"))
north = comp;
else if (name.equalsIgnoreCase("South"))
south = comp;
else if (name.equalsIgnoreCase("East"))
east = comp;
else if (name.equalsIgnoreCase("West"))
west = comp;
else if (name.equalsIgnoreCase("Board")
|| name.equalsIgnoreCase("Center"))
board = comp;
else if (name.equalsIgnoreCase("Status")
|| name.equalsIgnoreCase("Bottom"))
status = comp;
else if (name.equalsIgnoreCase("Controls"))
controls = comp;
}
// This is probably never called. Not sure what the right thing to
// do here is...
public void removeLayoutComponent(Component comp) {
if (comp == north)
north = null;
else if (comp == south)
south = null;
else if (comp == east)
east = null;
else if (comp == west)
west = null;
else if (comp == board)
board = null;
else if (comp == status)
status = null;
else if (comp == controls)
controls = null;
}
public Dimension preferredLayoutSize(Container target) {
int width = 0;
int height = 0;
if ((east != null) && east.isVisible()) {
Dimension d = east.getPreferredSize();
width += d.width;
height = Math.max(d.height, height);
}
if ((west != null) && west.isVisible()) {
Dimension d = west.getPreferredSize();
width += d.width;
height = Math.max(d.height, height);
}
if ((board != null) && board.isVisible()) {
Dimension d = board.getPreferredSize();
width += d.width;
height = Math.max(d.height, height);
}
if ((north != null) && north.isVisible()) {
Dimension d = north.getPreferredSize();
width = Math.max(d.width, width);
height += d.height;
}
if ((south != null) && south.isVisible()) {
Dimension d = south.getPreferredSize();
width = Math.max(d.width, width);
height += d.height;
}
if ((status != null) && status.isVisible()) {
Dimension d = status.getPreferredSize();
width = Math.max(d.width, width);
height += d.height;
}
if ((controls != null) && controls.isVisible()) {
Dimension d = controls.getPreferredSize();
width = Math.max(d.width, width);
height += d.height;
}
Insets insets = target.getInsets();
width += insets.left + insets.right;
height += insets.top + insets.bottom;
return new Dimension(width, height);
}
public Dimension minimumLayoutSize(Container cont) {
int width = 0;
int height = 0;
if ((east != null) && east.isVisible()) {
Dimension d = east.getMinimumSize();
width += d.width;
height = Math.max(d.height, height);
}
if ((west != null) && west.isVisible()) {
Dimension d = west.getMinimumSize();
width += d.width;
height = Math.max(d.height, height);
}
if ((board != null) && board.isVisible()) {
Dimension d = board.getMinimumSize();
width += d.width;
height = Math.max(d.height, height);
}
if ((north != null) && north.isVisible()) {
Dimension d = north.getMinimumSize();
width = Math.max(d.width, width);
height += d.height;
}
if ((south != null) && south.isVisible()) {
Dimension d = south.getMinimumSize();
width = Math.max(d.width, width);
height += d.height;
}
if ((status != null) && status.isVisible()) {
Dimension d = status.getMinimumSize();
width = Math.max(d.width, width);
height += d.height;
}
if ((controls != null) && controls.isVisible()) {
Dimension d = controls.getMinimumSize();
width = Math.max(d.width, width);
height += d.height;
}
Insets insets = cont.getInsets();
width += insets.left + insets.right;
height += insets.top + insets.bottom;
return new Dimension(width, height);
}
// This is called automatically when a container is resized.
public void layoutContainer(Container target) {
Dimension d = new Dimension(0, 0);
Dimension deast = (east == null) ? d : east.getPreferredSize();
Dimension dwest = (west == null) ? d : west.getPreferredSize();
Dimension dnorth = (north == null) ? d : north.getPreferredSize();
Dimension dsouth = (south == null) ? d : south.getPreferredSize();
Dimension dstatus = (status == null) ? d : status.getPreferredSize();
Dimension dcontrols = (controls == null) ? d : controls.getPreferredSize();
Insets insets = target.getInsets();
int twidth = target.getSize().width;
int theight = target.getSize().height;
int tInnerWidth = twidth - insets.right - insets.left;
// Get the initial edges of the center panel.
int btop = insets.top + dnorth.height;
int bbottom = (theight - insets.bottom - dstatus.height
- dsouth.height - dcontrols.height);
int bleft = insets.left + dwest.width;
int bright = twidth - insets.right - deast.width;
int size = 0;
int maxsize = 0;
// Center the center panel within the initial edge space.
if (board != null && board.isVisible()) {
d = board.getMinimumSize();
maxsize = Math.max(Math.min(bright - bleft, bbottom - btop),
Math.min(d.width, d.height));
// Make board be an integral multiple of boardSize, plus room for border.
// EVIL, EVIL, EVIL! I *MUST* use an interface somehow!! AMB.
// This was a pristine class before I messed with it.
BoardCanvas bc = (BoardCanvas) board;
size = ( (maxsize - 2 * bc.getBevelWidth()) / boardSize) * boardSize
+ 2 * bc.getBevelWidth();
int innerWidth = bright - bleft;
int innerHeight = bbottom - btop;
bleft += ((innerWidth - size) / 2);
// No need to change btop. Always put the top of the board
// directly underneath north. Don't resize north.
//btop += ((innerHeight - size) / 2);
bright = bleft + size;
bbottom = btop + size;
board.setBounds(bleft, btop, size, size);
}
int bottom = bbottom;
// The rest of the panels take up what space is left over.
if (south != null && south.isVisible()) {
south.setBounds(bleft, bottom, size, dsouth.height);
bottom += dsouth.height;
}
if (controls != null && controls.isVisible()) {
controls.setBounds(insets.left, bottom, tInnerWidth, dcontrols.height);
bottom += dcontrols.height;
}
if (status != null && status.isVisible()) {
// Add leftover space from rounding size to an integral
// multiple of boardSize to the status panel.
dstatus.height += (maxsize - size);
status.setBounds(insets.left, bottom, tInnerWidth, dstatus.height);
bottom += dstatus.height;
}
if (west != null && west.isVisible()) {
west.setBounds(insets.left, btop, bleft - insets.left, size);
}
if (east != null && east.isVisible()) {
east.setBounds(bright, btop, twidth - insets.right - bright, size);
}
if (north != null && north.isVisible()) {
north.setBounds(bleft, insets.top, size, btop - insets.top);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -