I have been banging my head since morning on a a Jlabel
issue; I would like to have an round image instead of the normal rectangular image for a label as shown in the image below.
I have tried overriding paintComponent
in the label but the image would not display at all. I have not found a very concrete answer on previous SO posts. Your help is welcome. Thanks in advance.
My code is as below:
jLabelPic = new javax.swing.JLabel(){
BufferedImage image= null;
private Ellipse2D.Double border = new Ellipse2D.Double();
private int width=140, height=140;
@Override
public void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
try {
image= ImageIO.read(getClass().getResource("/resources/image.jpg"));
} catch (IOException ex) {
ex.printStackTrace();
}
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setPaint(Color.RED);
g2d.fillRect(0, 0, width, height);
border.setFrame(0, 0, width, height);
g2d.setClip(border);
g2d.drawImage(image, 0, 0, width, height, this);
}
};