How can I convert an Icon to an Image

前端 未结 3 987
隐瞒了意图╮
隐瞒了意图╮ 2020-12-10 10:30

I\'m trying to convert an Icon (javax.swing.Icon) to an Image (java.awt.Image) using this code:

private Image iconToImage(Icon icon         


        
3条回答
  •  北海茫月
    2020-12-10 11:10

    Try this:

    icon.paintIcon(new JCheckBox(), image.getGraphics(), 0, 0);
    

    I can't exactly explain why it needs a JCheckBox though. Perhaps it varies for the icon? The NullPointerException was from this line in MetalIconFactory for "CheckBox.icon":

    ButtonModel model = ((JCheckBox)c).getModel();
    

提交回复
热议问题