Swing: resize RadioButton

I need to implement font resizing in my application. But when I increase the font size, RadioButtons remains the same size, and on a small screen with high resolution, my client simply cannot easily hit it. Is there a way to resize RadioButton software programmatically without digging into L & F and redrawing the icons manually (this is difficult because the application is designed for several platforms with different user interfaces, and each of them must have 7 icons).

An ideal solution might look like this:

  • Extract user interface icons.
  • Change of size
  • Set a size icon as a component icon.

How to implement step 1? Is it possible?

EDIT : this is what i have tried so far

public class IconImageSaver extends JFrame{

    public IconImageSaver() {

        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setBounds(0,0,100,100);
        setVisible(true);

        JRadioButton rad1 = new JRadioButton();
        rad1.setBounds(10,10,40,40);
        add(rad1);

        Icon icon = UIManager.getIcon("RadioButton.icon");//(1) trying to get icon

        int w = icon.getIconWidth(),h = icon.getIconHeight();
        Image i = rad1.createImage(w, h);
        Image i2 = rad1.createImage(w,h);
        Graphics g = i.getGraphics();
        Graphics g2 = i2.getGraphics();


        g.setColor(Color.CYAN);
        g.fillRect(0, 0, w, h);
        rad1.setIcon(new ImageIcon(i));//setting icons
        g2.setColor(Color.RED);
        g2.fillRect(0, 0, w, h);
        rad1.setPressedIcon(new ImageIcon(i2));//setting icons
    }

    public static void main(String[] args) {
        new IconImageSaver();
    }

}

At position (1), I'm trying to get an icon image, but it only returns the background color. I can’t understand why. The settings icons for various states work as intended.

+1
source share
3 answers

Some L & Fs (for example, Nimbus, Aqua) support a large one JComponent.sizeVariant, as described in Resizing a component and Using client properties .

Addendum: I have to use clean my own L & F.

a JRadioButton ButtonUI. , L & F, API- -. , L & F, . , .

: L & F, , , JToggleButton. ButtonGroup, , , .

+3

L&F sensitive,

  • L & F ( Nimbus auto_whatever), Custom L & F

  • UIManager, ( ) L & F

  • (J) ,

    a), UIManger ( def JVM instace)

    b) , (J) , e.i..... GUI

  • (I need to implement font size switching in my app) , ( ) Font FontUIResources, (J), FontUIResources, , .

+1

: "". , . , , , , .

-.

, UIManager , , .

, , UIManager, , , .

:

public static void scaleRadioButtonIcon(JRadioButton rb){
    boolean previousState = rb.isSelected();
    rb.setSelected(false);
    FontMetrics boxFontMetrics =  rb.getFontMetrics(rb.getFont());
    Icon radioIcon = UIManager.getIcon("RadioButton.icon");
    BufferedImage radioImage = new BufferedImage(
        radioIcon.getIconWidth(), radioIcon.getIconHeight(),BufferedImage.TYPE_INT_ARGB
    );
    Graphics graphics = radioImage.createGraphics();
    try{
        radioIcon.paintIcon(rb, graphics, 0, 0);
    }finally{
        graphics.dispose();
    }
    ImageIcon newRadioImage = new ImageIcon(radioImage);
    Image finalRadioImage = newRadioImage.getImage().getScaledInstance(
        boxFontMetrics.getHeight(), boxFontMetrics.getHeight(), Image.SCALE_SMOOTH
    );

    rb.setSelected(true);
    Icon selectedRadioIcon = UIManager.getIcon("RadioButton.icon");
    BufferedImage selectedRadioImage = new BufferedImage(
            selectedRadioIcon.getIconWidth(), selectedRadioIcon.getIconHeight(),BufferedImage.TYPE_INT_ARGB
    );
    Graphics selectedGraphics = selectedRadioImage.createGraphics();
    try{
        selectedRadioIcon.paintIcon(rb, selectedGraphics, 0, 0);
    }finally{
        selectedGraphics.dispose();
    }
    ImageIcon newSelectedRadioImage = new ImageIcon(selectedRadioImage);
    Image selectedFinalRadioImage = newSelectedRadioImage.getImage().getScaledInstance(
        boxFontMetrics.getHeight(), boxFontMetrics.getHeight(), Image.SCALE_SMOOTH
    );
    rb.setSelected(previousState);
    rb.setIcon(new ImageIcon(finalRadioImage));
    rb.setSelectedIcon(new ImageIcon(selectedFinalRadioImage));
}

, . , , "Look and Feel", .

, , - , , UIManager, "selected", , .

, . , , UIManager, , , "" '.

, , UIManager , , "" , , , .

If you do not want to use the font for the size of the controls, you can simply simply pass the height and width as parameters and use them instead of the font height when setting the size of the buffered image.

I could mention that the same methodology works with checkboxes

+1
source

Source: https://habr.com/ru/post/1787183/


All Articles