简体   繁体   English

如何在 Java 中制作圆形图像标签?

[英]How to make Circle image Label in Java?

How to make Circle image Label in Java?如何在 Java 中制作圆形图像标签?

I wanna make circle image Label But I can't do this.. Hey guys Help me..TT我想制作圆形图像标签但我不能这样做..嘿伙计们帮帮我..TT

I tried make circle panel for add image icon but that didn't work.我尝试制作圆形面板以添加图像图标,但没有奏效。

help me please...请帮帮我...

I think you should change your tack, instead of trying to modify the output of a component, instead, modify the input...我认为你应该改变你的策略,而不是尝试修改组件的输出,而是修改输入......

在此处输入图像描述

So all this does, is apply a circular (alpha based) mask to another image所以这一切所做的就是将圆形(基于 alpha 的)蒙版应用于另一个图像

    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这样做的好处是您可以获得软剪裁( setClip不提供),并且不会弄乱组件的现有剪裁形状

And applyQualityRenderingHints ...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);

}

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.转到博客 NiceApplication1.BlogSpot.Com,并下载新的 Nice Application 1 Home Stupendous,然后解压后进入 dist 文件夹并打开 Java Jar Nice Application 1,并获取 jar Nice_Application_1_Files,并仅将您的方法转换为一些节省空间的线条。

After jar adds only the below method with your file location and call method. jar 后仅添加以下方法以及您的文件位置和调用方法。

import Nice_Application_1.Icon;导入 Nice_Application_1.Icon;

private void Addicon() throws java.lang.NullPointerException, IOException { private void Addicon() 抛出 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)));上面的例子,如果想在弹出窗口中使用 Jbutton,请键入 JOptionPane.showMessageDialog(null, new JLabel(new ImageIcon(Appli1)));

After downloading the jar, you can also get more advantages.下载jar后,还可以获得更多优势。

You can use the Area class to create a clip region:您可以使用Area类来创建剪辑区域:

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)));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM