package awt;
import java.awt.*;
public class BevelBox extends Container {
private String title;
public BevelBox() {
this(null,null);
}
public BevelBox(String t) {
this (t,null);
}
public BevelBox(Component comp) {
this(null,comp);
}
public BevelBox(String t,Component comp) {
title = t;
setLayout(new BorderLayout());
BevelBorder border;
add("North",border = new BevelBorder(BevelBorder.NORTH));
border.title = t;
add("South",new BevelBorder(BevelBorder.SOUTH));
add("East",new BevelBorder(BevelBorder.EAST));
add("West",new BevelBorder(BevelBorder.WEST));
if (comp != null) add("Center",comp);
}
Warning: a BevelBox can have only *one* child. |
public Component add(Component comp) {
return add("Center",comp);
}
}
class BevelBorder extends Component {
public final static byte NORTH=0;
public final static byte WEST=1;
public final static byte EAST=2;
public final static byte SOUTH=3;
byte kind;
String title;
public BevelBorder(byte direction) {
kind = direction;
}
public Dimension getPreferredSize() {
return getMinimumSize();
}
public Dimension getMinimumSize() {
switch (kind) {
case NORTH:
if (title == null) return new Dimension(0,2);
else {
// Graphics g = getGraphics();
// Interesting (cursus java): getGraphics()==null als de component nog niet zichtbaar is
// maar getFont()!=null
FontMetrics fm = getFontMetrics(getFont());
if (fm == null) return new Dimension(0,2);
else {
return new Dimension(fm.stringWidth(title)+6,fm.getHeight());
}
}
case SOUTH:
return new Dimension(0,2);
case WEST:
case EAST:
return new Dimension(2,0);
default:
return new Dimension(0,0);
}
}
public void paint(Graphics g) {
Dimension siz = getSize();
switch (kind) {
case NORTH:
if (title == null) {
g.setColor(Color.black);
g.drawLine(1,0,siz.width-1,0);
g.setColor(Color.white);
g.drawLine(0,1,siz.width-1,1);
} else {
FontMetrics fm = g.getFontMetrics();
int titlewidth = fm.stringWidth(title)+2;
int ascent = fm.getAscent();
g.drawString(title,(siz.width-titlewidth)/2+1,ascent);
ascent /= 2; // center bar
// Left horizontal bar
g.setColor(Color.black);
g.drawLine(1,ascent,(siz.width-titlewidth)/2,ascent);
g.setColor(Color.white);
g.drawLine(0,ascent+1,(siz.width-titlewidth)/2,ascent+1);
// Left vertical bar
g.setColor(Color.white);
g.drawLine(0,ascent+2,0,siz.height-1);
g.setColor(Color.black);
g.drawLine(1,ascent+2,1,siz.height-1);
// Right horizontal bar
g.setColor(Color.black);
g.drawLine(siz.width-1,ascent,(siz.width+titlewidth)/2,ascent);
g.setColor(Color.white);
g.drawLine(siz.width-1,ascent+1,(siz.width+titlewidth)/2,ascent+1);
// Right vertical bar
g.setColor(Color.white);
g.drawLine(siz.width-1,ascent+2,siz.width-1,siz.height-1);
g.setColor(Color.black);
g.drawLine(siz.width-2,ascent+2,siz.width-2,siz.height-1);
}
break;
case SOUTH:
g.setColor(Color.white);
g.drawLine(0,0,siz.width-1,0);
g.setColor(Color.black);
g.drawLine(0,1,siz.width-1,1);
break;
case WEST:
g.setColor(Color.white);
g.drawLine(0,0,0,siz.height-1);
g.setColor(Color.black);
g.drawLine(1,0,1,siz.height-1);
break;
case EAST:
g.setColor(Color.black);
g.drawLine(0,0,0,siz.height-1);
g.setColor(Color.white);
g.drawLine(1,0,1,siz.height-1);
break;
}
}
}