java icon to image

Solutions on MaxInterview for java icon to image by the best coders in the world

showing results for - "java icon to image"
Itzel
02 Nov 2017
1public static class SafeIcon implements Icon {
2
3    private Icon wrappee;
4    private Icon standIn;
5
6    public SafeIcon(Icon wrappee) {
7        this.wrappee = wrappee;
8    }
9
10    @Override
11    public int getIconHeight() {
12        return wrappee.getIconHeight();
13    }
14
15    @Override
16    public int getIconWidth() {
17        return wrappee.getIconWidth();
18    }
19
20    @Override
21    public void paintIcon(Component c, Graphics g, int x, int y) {
22        if (standIn == this) {
23            paintFallback(c, g, x, y);
24        } else if (standIn != null) {
25            standIn.paintIcon(c, g, x, y);
26        } else {
27            try {
28               wrappee.paintIcon(c, g, x, y); 
29            } catch (ClassCastException e) {
30                createStandIn(e, x, y);
31                standIn.paintIcon(c, g, x, y);
32            }
33        }
34    }
35
36    /**
37     * @param e
38     */
39    private void createStandIn(ClassCastException e, int x, int y) {
40        try {
41            Class<?> clazz = getClass(e);
42            JComponent standInComponent = getSubstitute(clazz);
43            standIn = createImageIcon(standInComponent, x, y);
44        } catch (Exception e1) {
45            // something went wrong - fallback to this painting
46            standIn = this;
47        } 
48    }
49
50    private Icon createImageIcon(JComponent standInComponent, int x, int y) {
51        BufferedImage image = new BufferedImage(getIconWidth(),
52                getIconHeight(), BufferedImage.TYPE_INT_ARGB);
53          Graphics g = image.createGraphics();
54          try {
55              wrappee.paintIcon(standInComponent, g, 0, 0);
56              return new ImageIcon(image);
57          } finally {
58              g.dispose();
59          }
60    }
61
62    /**
63     * @param clazz
64     * @throws IllegalAccessException 
65     */
66    private JComponent getSubstitute(Class<?> clazz) throws IllegalAccessException {
67        JComponent standInComponent;
68        try {
69            standInComponent = (JComponent) clazz.newInstance();
70        } catch (InstantiationException e) {
71            standInComponent = new AbstractButton() {
72
73            };
74            ((AbstractButton) standInComponent).setModel(new DefaultButtonModel());
75        } 
76        return standInComponent;
77    }
78
79    private Class<?> getClass(ClassCastException e) throws ClassNotFoundException {
80        String className = e.getMessage();
81        className = className.substring(className.lastIndexOf(" ") + 1);
82        return Class.forName(className);
83
84    }
85
86    private void paintFallback(Component c, Graphics g, int x, int y) {
87        g.drawRect(x, y, getIconWidth(), getIconHeight());
88        g.drawLine(x, y, x + getIconWidth(), y + getIconHeight());
89        g.drawLine(x + getIconWidth(), y, x, y + getIconHeight());
90    }
91
92}
93////////////////////////////////////////////////////
94To use  : 
95
96icon = new SafeIcon(icon);
97BufferedImage image = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_RGB);
98icon.paintIcon(new JPanel(), image.getGraphics(), 0, 0);
99
100
101
102
103
104
105
106
107
queries leading to this page
java icon to imagejava icon to image