0

I am new to programming and I've been trying to get and set the icon to circle but it's not being updated. I want to set every icon that was set to JLabel will become circular. enter image description here

class CircleLabel extends JLabel {

    private Icon icon;
    private int borderSize;

    @Override
    protected void paintComponent(Graphics g) {
        if (getIcon() != null) {
            icon = getIcon();
            int width = getWidth();
            int height = getHeight();
            int diameter = Math.min(width, height);
            int x = width / 2 - diameter / 2;
            int y = height / 2 - diameter / 2;
            int border = borderSize * 2;
            diameter -= border;
            Dimension size = getAutoSize(icon, diameter);
            BufferedImage img = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_ARGB);
            Graphics2D g2_img = img.createGraphics();
            g2_img.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2_img.fillOval(0, 0, diameter, diameter);
            Composite composite = g2_img.getComposite();
            g2_img.setComposite(AlphaComposite.SrcIn);
            g2_img.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
            g2_img.drawImage(toImage(icon), 0, 0, size.width, size.height, null);
            g2_img.setComposite(composite);
            g2_img.dispose();
            Graphics2D g2 = (Graphics2D) g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

            g2.setColor(new Color(255, 255, 255));
            diameter += border;
            g2.fillOval(borderSize, borderSize, diameter, diameter);
            g2.drawImage(img, x - borderSize, y + borderSize, null);
        } else {
            System.out.println("No Icon!");
        }

        super.paintComponent(g);
    }

    private Dimension getAutoSize(Icon image, int size) {
        int w = size;
        int h = size;
        int iw = image.getIconWidth();
        int ih = image.getIconHeight();
        double xScale = (double) w / iw;
        double yScale = (double) h / iw;
        System.out.println(xScale);
        System.out.println(yScale);
        double scale = Math.max(xScale, yScale);
        int width = (int) (scale * iw);
        int height = (int) (scale * ih);
        if (width < 1) {
            width = 1;
        }
        if (height < 1) {
            height = 1;
        }
        return new Dimension(width, height);
    }

    private Image toImage(Icon icon) {
        return ((ImageIcon) icon).getImage();

    }
}

If I set the icon manually and removed the icon from the label, it will work but when I add the icon to the label through properties, it will not work.

icon = new ImageIcon(getClass().getResource("/Attachments/user.png"));

I found this on YouTube and wanted to apply it to the application I'm trying to build.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
sjqwer1
  • 81
  • 1
  • 1
  • 6
  • It appears the circular image logic is working, but the layouts are not. 1) [Edit] to add a [mre]. 2) To be an MRE for image-based code, the code must hotlink to an image from the internet. This [Q&A](https://stackoverflow.com/q/19209650/418556) provides many. – Andrew Thompson Oct 31 '21 at 10:41
  • I appreciate your answer @AndrewThompson but I didn't get what you've said. – sjqwer1 Oct 31 '21 at 10:46
  • If you're new to programming, you should try and make your application functional and worry about "prettiness" much later in your coding career. – Gilbert Le Blanc Oct 31 '21 at 11:03
  • Yes, I'm working on the function and adding the design at the same time. Do you know this is not working working? @GilbertLeBlanc – sjqwer1 Oct 31 '21 at 11:09
  • *"I didn't get what you've said."* What didn't you get? OK, let's start with 'It'. *used as the subject of a verb, or the object of a verb or preposition, to refer to a thing, animal, situation, or idea that has already been mentioned.* Do you now see why you have to be more specific about what you don't 'get'? I'm not about to try and explain every word. – Andrew Thompson Oct 31 '21 at 13:07
  • 1
    Don't create your circular image in the paintComponent() method. This logic will be invoked every time the component needs to be repainted. Instead, create a circular image that you can use to create an ImageIcon which can be added to a JLabel. See: https://stackoverflow.com/a/31424601/131872 for an example of this approach. – camickr Oct 31 '21 at 15:59

0 Answers0