Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

java icon to image

public static class SafeIcon implements Icon {

    private Icon wrappee;
    private Icon standIn;

    public SafeIcon(Icon wrappee) {
        this.wrappee = wrappee;
    }

    @Override
    public int getIconHeight() {
        return wrappee.getIconHeight();
    }

    @Override
    public int getIconWidth() {
        return wrappee.getIconWidth();
    }

    @Override
    public void paintIcon(Component c, Graphics g, int x, int y) {
        if (standIn == this) {
            paintFallback(c, g, x, y);
        } else if (standIn != null) {
            standIn.paintIcon(c, g, x, y);
        } else {
            try {
               wrappee.paintIcon(c, g, x, y); 
            } catch (ClassCastException e) {
                createStandIn(e, x, y);
                standIn.paintIcon(c, g, x, y);
            }
        }
    }

    /**
     * @param e
     */
    private void createStandIn(ClassCastException e, int x, int y) {
        try {
            Class<?> clazz = getClass(e);
            JComponent standInComponent = getSubstitute(clazz);
            standIn = createImageIcon(standInComponent, x, y);
        } catch (Exception e1) {
            // something went wrong - fallback to this painting
            standIn = this;
        } 
    }

    private Icon createImageIcon(JComponent standInComponent, int x, int y) {
        BufferedImage image = new BufferedImage(getIconWidth(),
                getIconHeight(), BufferedImage.TYPE_INT_ARGB);
          Graphics g = image.createGraphics();
          try {
              wrappee.paintIcon(standInComponent, g, 0, 0);
              return new ImageIcon(image);
          } finally {
              g.dispose();
          }
    }

    /**
     * @param clazz
     * @throws IllegalAccessException 
     */
    private JComponent getSubstitute(Class<?> clazz) throws IllegalAccessException {
        JComponent standInComponent;
        try {
            standInComponent = (JComponent) clazz.newInstance();
        } catch (InstantiationException e) {
            standInComponent = new AbstractButton() {

            };
            ((AbstractButton) standInComponent).setModel(new DefaultButtonModel());
        } 
        return standInComponent;
    }

    private Class<?> getClass(ClassCastException e) throws ClassNotFoundException {
        String className = e.getMessage();
        className = className.substring(className.lastIndexOf(" ") + 1);
        return Class.forName(className);

    }

    private void paintFallback(Component c, Graphics g, int x, int y) {
        g.drawRect(x, y, getIconWidth(), getIconHeight());
        g.drawLine(x, y, x + getIconWidth(), y + getIconHeight());
        g.drawLine(x + getIconWidth(), y, x, y + getIconHeight());
    }

}
////////////////////////////////////////////////////
To use  : 

icon = new SafeIcon(icon);
BufferedImage image = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_RGB);
icon.paintIcon(new JPanel(), image.getGraphics(), 0, 0);








Comment

PREVIOUS NEXT
Code Example
Java :: java swing tablecellrenderer 
Java :: java font bold italic 
Java :: print outputstream to console java 
Java :: coin flip in java 
Java :: java color light gray 
Java :: ejercicios resueltos de python codeskulptor 
Java :: ticket sales java program 
Java :: mambalam srardham online booking 
Java :: interact with databse java 
Sql :: sql server reseed single table 
Sql :: find sp name by text in sql server 
Sql :: postgresql find biggest table 
Sql :: psql drop column 
Sql :: apex execute batch job 
Sql :: doublon sql 
Sql :: grant revoke privileges to mysql username 
Sql :: commentaire table oracle 
Sql :: sql add column 
Sql :: how to list columns for particular tables in postgresql 
Sql :: oracle kill session by username 
Sql :: .... ERROR! The server quit without updating PID file mysql centos 
Sql :: sql server last executed query 
Sql :: rename table sql 
Sql :: truncate table mysql 
Sql :: mysql check table exists 
Sql :: postgresql print variable 
Sql :: CREATE DATABASE db; SyntaxError: Unexpected identifier 
Sql :: sql drop primary key 
Sql :: Sql query to force the database to be drop 
Sql :: mysql format date 
ADD CONTENT
Topic
Content
Source link
Name
2+9 =