Subpages: 1. Frames and Panels overview
2. Borders
3. Creating a custom border
3.3 Creating a custom border
To create a custom border we can implement the javax.swing.Border interface and define the following three methods:
void paintBorder(Component c, Graphics g): perform the border rendering--only paint within the Insets region.
Insets getBorderInsets(Component c): return an Insets instance representing the top, bottom, left, and right thicknesses.
boolean isBorderOpaque(): return whether or not the border is opaque or transparent.
The following class is a simple implementation of a custom rounded-rectangle border which we call OvalBorder.

Figure 3.9 A custom rounded-corner Border implementation.
<<file figure3-9.gif>>
The Code: OvalBorder.java
see \Chapter3\2
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
public class OvalBorder implements Border
{
protected int m_w=6;
protected int m_h=6;
protected Color m_topColor = Color.white;
protected Color m_bottomColor = Color.gray;
public OvalBorder() {
m_w=6;
m_h=6;
}
public OvalBorder(int w, int h) {
m_w=w;
m_h=h;
}
public OvalBorder(int w, int h, Color topColor,
Color bottomColor) {
m_w=w;
m_h=h;
m_topColor = topColor;
m_bottomColor = bottomColor;
}
public Insets getBorderInsets(Component c) {
return new Insets(m_h, m_w, m_h, m_w);
}
public boolean isBorderOpaque() { return true; }
public void paintBorder(Component c, Graphics g,
int x, int y, int w, int h) {
w--;
h--;
g.setColor(m_topColor);
g.drawLine(x, y+h-m_h, x, y+m_h);
g.drawArc(x, y, 2*m_w, 2*m_h, 180, -90);
g.drawLine(x+m_w, y, x+w-m_w, y);
g.drawArc(x+w-2*m_w, y, 2*m_w, 2*m_h, 90, -90);
g.setColor(m_bottomColor);
g.drawLine(x+w, y+m_h, x+w, y+h-m_h);
g.drawArc(x+w-2*m_w, y+h-2*m_h, 2*m_w, 2*m_h, 0, -90);
g.drawLine(x+m_w, y+h, x+w-m_w, y+h);
g.drawArc(x, y+h-2*m_h, 2*m_w, 2*m_h, -90, -90);
}
public static void main(String[] args) {
JFrame frame = new JFrame("Custom Border: OvalBorder");
JLabel label = new JLabel("OvalBorder");
((JPanel) frame.getContentPane()).setBorder(new CompoundBorder(
new EmptyBorder(10,10,10,10), new OvalBorder(10,10)));
frame.getContentPane().add(label);
frame.setBounds(0,0,300,150);
frame.setVisible(true);
}
}
Understanding the Code
This border consists of a raised shadowed rectangle with rounded corners. Instance variables:
int m_w: left and right inset value.
int m_h: top and bottom inset value.
Color m_topColor: non-shadow color.
Color m_bottomColor: shadow color.
Three constructors are provided to allow optional specification of the width and height of left/right and top/bottom inset values respectively. We can also specify the shadow color (bottom color) and non-shadow color (top color). The inset values default to 6, the top color defaults to white, and the shadow color defaults to gray.
The isBorderOpaque() method always returns true to signify that this border's region will always be completely filled. getBorderInsests() simpley returns an Insets instance made up of the left/right and top/bottom inset values specified in a constructor.
The paintBorder() method is responsible of rendering our border and simply paints a sequence of four lines and arcs in the approproate colors. By simply reversing the use of bottomColor and topColor we can switch from a raised look to a lowered look (a more flexible implementation might include a raised/lowered flag, and an additional constructor parameter used to specify this).
The main() method creates a JFrame with content pane surrounded by a CompoundBorder. The outer border is an EmptyBorder to provide white space, and the inner border is an instance of our OvalBorder class with inset values of 10.
Running the Code
Figure 3.9 illustrates. Try running this class and resize the parent frame. Note that with a very small width or height the border does not render itself pleasantly. A more professional implementation would take this into account in the paintBorder() routine.


RSS feed Java FAQ News