How to draw an rounded corner border?

Is there a Border class can draw a rounded cornder around a component?
[77 byte] By [youhaodiyia] at [2007-11-27 6:34:16]
# 1

No, but it's pretty easy to do your own:

import java.awt.*;

import javax.swing.border.AbstractBorder;

import javax.swing.*;

public class RoundedBorder extends AbstractBorder {

private final static int MARGIN = 10;

public void paintBorder( Component c, Graphics g, int x, int y, int width, int height ) {

g.drawRoundRect( x, y, width - 1, height - 1, MARGIN, MARGIN );

}

public Insets getBorderInsets( Component c ) {

return new Insets( MARGIN, MARGIN, MARGIN, MARGIN );

}

public Insets getBorderInsets( Component c, Insets insets ) {

insets.left = insets.top = insets.right = insets.bottom = MARGIN;

return insets;

}

private static void createUI() {

JPanel p = new JPanel( new BorderLayout() );

p.setBorder( BorderFactory.createEmptyBorder( MARGIN, MARGIN, MARGIN, MARGIN ) );

JLabel l = new JLabel( "Label" );

l.setBorder( new RoundedBorder() );

p.add( l, BorderLayout.CENTER );

JFrame f = new JFrame( "RoundedBorder" );

f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

f.getContentPane().add( p );

f.pack();

f.setVisible( true );

}

public static void main( String[] args ) {

Runnable doRun = new Runnable() {

public void run() { createUI(); }

};

SwingUtilities.invokeLater( doRun );

}

}

JayDSa at 2007-7-12 18:00:35 > top of Java-index,Desktop,Core GUI APIs...
# 2

If the color of the frame and then panel is different, draw a round rect will not work as needed.

For example:

JPanel p = new JPanel( new BorderLayout() );

p.setBorder( BorderFactory.createEmptyBorder( MARGIN, MARGIN, MARGIN, MARGIN ) );

p.setBackground(new Color.red);

JLabel l = new JLabel( "Label" );

l.setBorder( new RoundedBorder() );

p.add( l, BorderLayout.CENTER );

JFrame f = new JFrame( "RoundedBorder" );

f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

f.getContentPane().add( p );

f.setBackground(new Color.yellow);

f.pack();

f.setVisible( true );

youhaodiyia at 2007-7-12 18:00:35 > top of Java-index,Desktop,Core GUI APIs...
# 3
I added those lines and it still draws just fine for me: http://img358.imageshack.us/img358/1641/roundedbordertd8.pngPerhaps you could clarify what you mean?
JayDSa at 2007-7-12 18:00:35 > top of Java-index,Desktop,Core GUI APIs...