0

How to make Circle image Label in Java?

I wanna make circle image Label But I can't do this.. Hey guys Help me..T.T

I tried make circle panel for add image icon but that didn't work.

help me please...

9oose
  • 33
  • 1
  • 1
  • 3
  • Set the clip before you call `super.paintComponent` just beware, playing with the clip is suck dangrous – MadProgrammer Jul 15 '15 at 06:41
  • 1) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). 2) Use a logical and consistent form of indenting code lines and blocks. The indentation is intended to make the flow of the code easier to follow! 3) That label does not override the preferred size so it is likely given 0x0 by the layout. 4) That code looks as though it would not even compile, by trying to return a value from a `void` method. – Andrew Thompson Jul 15 '15 at 06:41
  • In which class is paintComponent defined? why it returns JLabel in void method? – maskacovnik Jul 15 '15 at 06:42
  • sorry there is not full code and i'm starter... so tell me how can make label shape like circle... – 9oose Jul 15 '15 at 06:50
  • Could you provide an image of what you want using paint or something and put it somewhere to show us what you want? Is the circle label should show a string in a circular way, or it should show a circular Icon? – STaefi Jul 15 '15 at 06:58
  • i can't up load image because i need more reputation T.T – 9oose Jul 15 '15 at 07:05
  • *"..sorry there is not full code .."* Nobody asked for 'full code'. Read the links offered. If you cannot provide an MCVE, that means I cannot offer further help. – Andrew Thompson Jul 15 '15 at 07:05
  • *"because i need more reputation.."* (Glances towards the upper left.) Well it looks like you are going the wrong way about getting ***more*** reputation. Personally I think an MCVE would be more useful than images. – Andrew Thompson Jul 15 '15 at 07:07

3 Answers3

10

I think you should change your tack, instead of trying to modify the output of a component, instead, modify the input...

enter image description here

So all this does, is apply a circular (alpha based) mask to another image

    BufferedImage master = ImageIO.read(new File("/Volumes/Disk02/Dropbox/MegaTokyo/thumnails/megatokyo_omnibus_1_3_cover_by_fredrin-d4oupef.jpg"));

    int diameter = Math.min(master.getWidth(), master.getHeight());
    BufferedImage mask = new BufferedImage(master.getWidth(), master.getHeight(), BufferedImage.TYPE_INT_ARGB);

    Graphics2D g2d = mask.createGraphics();
    applyQualityRenderingHints(g2d);
    g2d.fillOval(0, 0, diameter - 1, diameter - 1);
    g2d.dispose();

    BufferedImage masked = new BufferedImage(diameter, diameter, BufferedImage.TYPE_INT_ARGB);
    g2d = masked.createGraphics();
    applyQualityRenderingHints(g2d);
    int x = (diameter - master.getWidth()) / 2;
    int y = (diameter - master.getHeight()) / 2;
    g2d.drawImage(master, x, y, null);
    g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.DST_IN));
    g2d.drawImage(mask, 0, 0, null);
    g2d.dispose();

    JOptionPane.showMessageDialog(null, new JLabel(new ImageIcon(masked)));

The benefit of this is you can get soft clipping (which setClip doesn't provide) as well as not messing with the existing clipping shape of a component

And applyQualityRenderingHints...

public static void applyQualityRenderingHints(Graphics2D g2d) {

    g2d.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2d.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
    g2d.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE);
    g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
    g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);

}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
-1

Go to the blog NiceApplication1.BlogSpot.Com, and Download the new Nice Application 1 Home Stupendous, then after extract goes to the dist folder and open the Java Jar Nice Application 1, and take the jar Nice_Application_1_Files, and convert your method only in some lines for saving space.

After jar adds only the below method with your file location and call method.

import Nice_Application_1.Icon;

private void Addicon() throws java.lang.NullPointerException, IOException {

    Icon test = new Icon("C:\\Image.jpg");

    String nice = test.icon();

    BufferedImage Appli1 = test.Application1;

    jToggleButton0.setIcon(new ImageIcon(Appli1));

}

//Circle shape icon you can use with the above method.

Above example, with Jbutton if want in a popup so type JOptionPane.showMessageDialog(null, new JLabel(new ImageIcon(Appli1)));

After downloading the jar, you can also get more advantages.

Alan
  • 9
  • 4
-2

You can use the Area class to create a clip region:

BufferedImage image = ImageIO.read(new File("..."));
Area clip = new Area( new Rectangle(0, 0, image.getWidth(), image.getHeight()) );
Area oval = new Area( new Ellipse2D.Double(0, 0, image.getWidth() - 1, image.getHeight() - 1) );
clip.subtract( oval );
Graphics g2d = image.createGraphics();
g2d.setClip( clip );
g2d.setColor( Color.BLACK );
g2d.fillRect(0, 0, image.getWidth(), image.getHeight());
JOptionPane.showMessageDialog(null, new JLabel(new ImageIcon(image)));
camickr
  • 321,443
  • 19
  • 166
  • 288