/*
Definitive Guide to Swing for Java 2, Second Edition
By John Zukowski
ISBN: 1-893115-78-X
Publisher: APress
*/
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Polygon;
import javax.swing.Icon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.border.BevelBorder;
import javax.swing.border.Border;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.border.LineBorder;
import javax.swing.border.MatteBorder;
import javax.swing.border.SoftBevelBorder;
import javax.swing.border.TitledBorder;
public class BorderSample {
public static void main(String args[]) {
JFrame frame = new JFrame("Sample Borders");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Border bevelBorder = new BevelBorder(BevelBorder.RAISED, Color.red,
Color.red.darker(), Color.pink, Color.pink.brighter());
Border emptyBorder = new EmptyBorder(5, 10, 5, 10);
Border etchedBorder = new EtchedBorder(EtchedBorder.RAISED, Color.red,
Color.pink);
Border lineBorder = new LineBorder(Color.red, 5);
Icon diamondIcon = new DiamondIcon(Color.red);
Border matteBorder = new MatteBorder(5, 10, 5, 10, diamondIcon);
Border softBevelBorder = new SoftBevelBorder(BevelBorder.RAISED,
Color.red, Color.red.darker(), Color.pink, Color.pink
.brighter());
Font font = new Font("Serif", Font.ITALIC, 12);
Border titledBorder = new TitledBorder(lineBorder, "Hello",
TitledBorder.LEFT, TitledBorder.BELOW_BOTTOM, font, Color.red);
Border compoundBorder = new CompoundBorder(lineBorder, matteBorder);
JLabel bevelLabel = new JLabel("Bevel");
bevelLabel.setBorder(bevelBorder);
bevelLabel.setHorizontalAlignment(JLabel.CENTER);
JLabel emptyLabel = new JLabel("Empty");
emptyLabel.setBorder(emptyBorder);
emptyLabel.setHorizontalAlignment(JLabel.CENTER);
JLabel etchedLabel = new JLabel("Etched");
etchedLabel.setBorder(etchedBorder);
etchedLabel.setHorizontalAlignment(JLabel.CENTER);
JLabel lineLabel = new JLabel("Line");
lineLabel.setBorder(lineBorder);
lineLabel.setHorizontalAlignment(JLabel.CENTER);
JLabel matteLabel = new JLabel("Matte");
matteLabel.setBorder(matteBorder);
matteLabel.setHorizontalAlignment(JLabel.CENTER);
JLabel softBevelLabel = new JLabel("Soft Bevel");
softBevelLabel.setBorder(softBevelBorder);
softBevelLabel.setHorizontalAlignment(JLabel.CENTER);
JLabel titledLabel = new JLabel("Titled");
titledLabel.setBorder(titledBorder);
titledLabel.setHorizontalAlignment(JLabel.CENTER);
JLabel compoundLabel = new JLabel("Compound");
compoundLabel.setBorder(compoundBorder);
compoundLabel.setHorizontalAlignment(JLabel.CENTER);
Container contentPane = frame.getContentPane();
contentPane.setLayout(new GridLayout(2, 4, 5, 5));
contentPane.add(bevelLabel);
contentPane.add(emptyLabel);
contentPane.add(etchedLabel);
contentPane.add(lineLabel);
contentPane.add(matteLabel);
contentPane.add(softBevelLabel);
contentPane.add(titledLabel);
contentPane.add(compoundLabel);
frame.setSize(400, 200);
frame.setVisible(true);
}
}
class DiamondIcon implements Icon {
private Color color;
private boolean selected;
private int width;
private int height;
private Polygon poly;
private static final int DEFAULT_WIDTH = 10;
private static final int DEFAULT_HEIGHT = 10;
public DiamondIcon(Color color) {
this(color, true, DEFAULT_WIDTH, DEFAULT_HEIGHT);
}
public DiamondIcon(Color color, boolean selected) {
this(color, selected, DEFAULT_WIDTH, DEFAULT_HEIGHT);
}
public DiamondIcon(Color color, boolean selected, int width, int height) {
this.color = color;
this.selected = selected;
this.width = width;
this.height = height;
initPolygon();
}
private void initPolygon() {
poly = new Polygon();
int halfWidth = width / 2;
int halfHeight = height / 2;
poly.addPoint(0, halfHeight);
poly.addPoint(halfWidth, 0);
poly.addPoint(width, halfHeight);
poly.addPoint(halfWidth, height);
}
public int getIconHeight() {
return height;
}
public int getIconWidth() {
return width;
}
public void paintIcon(Component c, Graphics g, int x, int y) {
g.setColor(color);
g.translate(x, y);
if (selected) {
g.fillPolygon(poly);
} else {
g.drawPolygon(poly);
}
g.translate(-x, -y);
}
}