0

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);
}
};
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • can you share your code pls? – soorapadman Oct 10 '15 at 10:34
  • @ Soorapadman,I have posted the code above. –  Oct 10 '15 at 10:41
  • *"I have posted the code above."* 1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). – Andrew Thompson Oct 10 '15 at 10:53
  • 1
    `public void paintComponent(Graphics g) {` Two tips: 1) Always call the `super` method first. 2) Never try to perform long running tasks (such as accessing the file system) in a paint method! That stuff should be set up when the class is instantiated. – Andrew Thompson Oct 10 '15 at 10:55
  • 1) See also [this answer](http://stackoverflow.com/a/6296381/418556) to 'Cut out image in shape of text'. 2) **`private Ellipse2D.Double border = new Ellipse2D.Double();` Hmmm... using a 0 sized ellipse as a clip and not seeing anything. And you think this problem is mysterious?** – Andrew Thompson Oct 10 '15 at 10:56
  • 1
    @AndrewThompson: 1) Points taken, thanks. (2)The size if the ellipseis set in the line border.setFrame(0, 0, width, height); Can you post a revision of my code with suggested modifications, please? –  Oct 10 '15 at 11:21
  • *"Can you post a revision of my code with suggested modifications.."* What's your budget for *me* doing *your* work?!? Either create and post an MCVE or don't. It is up to **you.** Voting to close (based on lack of MCVE). – Andrew Thompson Oct 10 '15 at 11:34

0 Answers0