You cannot round corners on the actual JLabel
area; they are always rectangular. However, a simple alternative is to set the ImageIcon
of the JLabel
to an image with rounded edges and not use a border. To set an ImageIcon
:
yourLabel.setIcon(new ImageIcon(getClass().getResource("/path/to/your/image.png"));
// Note: Relative path, starts from root of project
Your image should have the dimensions of your JLabel
.
Note that this will throw a NullPointerException
if the image is not found. Make sure you get the right path!
To create an ImageIcon
that resizes to the size of the JLabel
:
ImageIcon ico = new ImageIcon("/path/to/your/image.png");
Image img = ico.getImage();
BufferedImage bi = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB);
Graphics g = bi.createGraphics();
g.drawImage(img, 0, 0, yourLabel.getWidth(), yourLabel.getHeight(), null);
IconImage newIco = new IconImage(bi);
yourLabel.setIcon(newIco);
EDIT:
Here is the best way to make a border with rounded corners, using Graphics2D
.
First, make a new class called RoundedBorder. Paste this code into it:
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.geom.Line2D;
import javax.swing.border.AbstractBorder;
public class RoundedBorder extends AbstractBorder {
public RoundedBorder(Color c, int g) {
color = c;
gap = g;
}
@Override
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
super.paintBorder(c, g, x, y, width, height);
Graphics2D g2d;
if (g instanceof Graphics2D) {
g2d = (Graphics2D) g;
g2d.setColor(color);
System.out.println(x + y);
g2d.draw(new Line2D.Double((double)x, (double)y + 10, (double)x + 3, (double)y + 3));
g2d.draw(new Line2D.Double((double)x + 3, (double)y + 3, (double)x + 10, (double)y));
g2d.draw(new Line2D.Double((double)x + 10, (double)y, (double)x + 30, (double)y));
g2d.draw(new Line2D.Double((double)x + 30, (double)y, (double)x + 33, (double)y + 2));
g2d.draw(new Line2D.Double((double)x + 33, (double)y + 2, (double)x + 36, (double)y + 8));
g2d.draw(new Line2D.Double((double)x + 36, (double)y + 8, (double)x + 36, (double)y + 28));
g2d.draw(new Line2D.Double((double)x + 36, (double)y + 28, (double)x + 34, (double)y + 31));
g2d.draw(new Line2D.Double((double)x + 34, (double)y + 31, (double)x + 32, (double)y + 33));
g2d.draw(new Line2D.Double((double)x + 32, (double)y + 33, (double)x + 6, (double)y + 33));
g2d.draw(new Line2D.Double((double)x + 6, (double)y + 33, (double)x + 3, (double)y + 31));
g2d.draw(new Line2D.Double((double)x + 3, (double)y + 31, (double)x, (double)y + 27));
g2d.draw(new Line2D.Double((double)x, (double)y + 27, (double)x, (double)y + 10));
}
}
@Override
public Insets getBorderInsets(Component c) {
return (getBorderInsets(c, new Insets(gap, gap, gap, gap)));
}
@Override
public Insets getBorderInsets(Component c, Insets insets) {
insets.left = insets.top = insets.right = insets.bottom = gap;
return insets;
}
@Override
public boolean isBorderOpaque() {
return true;
}
// Variable declarations
private final Color color;
private final int gap;
}
Then, in your JFrame class, to set this as the border of a JLabel
, do:
yourLabel.setBorder(new RoundedBorder(Color.black, 10));
As MadProgrammer mentioned, a more efficient way than drawing lines is to use a RoundRectangle2D
. To use this, replace all of the draw
lines with
g2d.draw(new RoundRectangle2D.Double(x, y, width - 1, height - 1, gap, gap));
Feel free to modify the border as you wish. Here is the syntax for using Graphics2D
:
g2d.draw(new Line2D.Double((double)x1, (double)y1, (double)x2, (double)y2));
OR
g2d.draw(new Line2D.Double(Point2D p1, Point2D p2));
I hope this helped!