Christian O.
Christian O.

Reputation: 498

Java Swing: Button only displays Icon upon Mouse-Over

I have Swing Application where I want to display an Icon on a JButton. I had some problems with Windows scaling the Icon and making it very ugly. I was able to solve that problem by using an Icon-Wrapper Class as described here.

Using the wrapper class solves the scaling problem I had but causes a new problem: The icon only shows once I mouse over the button, as can be seen here:

enter image description here

Here is a minimal example:

import javax.swing.*;
import java.awt.*;
import java.net.MalformedURLException;
import java.net.URL;

public class Ui {
    private static JFrame frame;
    private static JButton button1;
    private static JButton button2;
    private static JMenuBar jMenuBar;


    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            try {
                createGui();
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
        });
    }

    private static void createGui() throws MalformedURLException {
        frame = new JFrame("Test");

        button1 = new JButton();
        button2 = new JButton();
        jMenuBar = new JMenuBar();


        Icon icon = new NoScalingIcon(new ImageIcon(new URL("https://i.sstatic.net/wgKeq.png")));
        button1.setIcon(icon);
        button2.setIcon(icon);

        jMenuBar.add(button1);
        jMenuBar.add(button2);
        frame.setJMenuBar(jMenuBar);
        frame.pack();
        frame.setVisible(true);
        frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
    }
}

The icon wrapper class:

import javax.swing.*;
import java.awt.*;
import java.awt.geom.AffineTransform;

public class NoScalingIcon implements Icon {
    private Icon icon;

    public NoScalingIcon(Icon icon) {
        this.icon = icon;
    }

    public int getIconWidth() {
        return icon.getIconWidth();
    }

    public int getIconHeight() {
        return icon.getIconHeight();
    }

    public void paintIcon(Component c, Graphics g, int x, int y) {
        Graphics2D g2d = (Graphics2D) g.create();

        AffineTransform at = g2d.getTransform();
        int scaleX = (int) (x * at.getScaleX());
        int scaleY = (int) (y * at.getScaleY());

        int offsetX = (int) (icon.getIconWidth() * (at.getScaleX() - 1) / 2);
        int offsetY = (int) (icon.getIconHeight() * (at.getScaleY() - 1) / 2);

        g2d.setTransform(new AffineTransform());

        icon.paintIcon(c, g2d, scaleX + offsetX, scaleY + offsetY);

        g2d.dispose();
    }
}

What could be the cause for this problem?

Upvotes: 0

Views: 109

Answers (1)

camickr
camickr

Reputation: 324108

I think I found a solution.

Instead of creating a completely new AffineTransform, the code below merges a new scaled transform with the inverse of the existing scale so the scale factor is set back to 1:

import java.awt.*;
import java.awt.geom.AffineTransform;
import javax.swing.*;


public class NoScalingIcon implements Icon
{
    private Icon icon;

    public NoScalingIcon(Icon icon)
    {
        this.icon = icon;
    }

    public int getIconWidth()
    {
        return icon.getIconWidth();
    }

    public int getIconHeight()
    {
        return icon.getIconHeight();
    }

    public void paintIcon(Component c, Graphics g, int x, int y)
    {
        Graphics2D g2d = (Graphics2D)g.create();

        AffineTransform at = g2d.getTransform();

        int scaleX = (int)(x * at.getScaleX());
        int scaleY = (int)(y * at.getScaleY());

        int offsetX = (int)(icon.getIconWidth() * (at.getScaleX() - 1) / 2);
        int offsetY = (int)(icon.getIconHeight() * (at.getScaleY() - 1) / 2);

        int locationX = scaleX + offsetX;
        int locationY = scaleY + offsetY;

        AffineTransform scaled = AffineTransform.getScaleInstance(1.0 / at.getScaleX(), 1.0 / at.getScaleY());
        at.concatenate( scaled );
        g2d.setTransform( at );

        icon.paintIcon(c, g2d, locationX, locationY);

        g2d.dispose();
    }
}

Upvotes: 1

Related Questions