September 1998
Intermediate to advanced
1258 pages
42h 57m
English
Creating your own border is simple
when you extend the AbstractBorder class. You need
to define three things: whether the border is opaque, what its insets
are, and how to draw the border. To accomplish this, you must
implement paintBorder(), both
isBorderOpaque() methods, and
getBorderInsets(). The hard part of coming up with
your own border is doing something creative with the
Graphics primitives in the
paintBorder() method. A reminder: make sure that
you do not paint inside the insets region that you define for
yourself. Otherwise, you could be painting over the component you
intend to border.
Let’s take a look at a simple border:
// CurvedBorder.java // import java.awt.*; import javax.swing.border.*; public class CurvedBorder extends AbstractBorder { private Color wallColor = Color.gray; private int sinkLevel = 10; public CurvedBorder() { } public CurvedBorder(int sinkLevel) { this.sinkLevel = sinkLevel; } public CurvedBorder(Color wall) { this.wallColor = wall; } public CurvedBorder(int sinkLevel, Color wall) { this.sinkLevel = sinkLevel; this.wallColor = wall; } public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) { g.setColor(getWallColor()); // Paint a tall wall around the component for (int i = 0; i < sinkLevel; i++) { g.drawRoundRect(x+i, y+i, w-i-1, h-i-1, sinkLevel-i, sinkLevel); g.drawRoundRect(x+i, y+i, w-i-1, h-i-1, sinkLevel, sinkLevel-i); g.drawRoundRect(x+i, y, w-i-1, h-1, sinkLevel-i, sinkLevel); g.drawRoundRect(x, ...Read now
Unlock full access