java - How can I convert an Icon to an Image -


i trying convert icon (javax.swing.icon) image (java.awt.image) using code :

private image icontoimage(icon icon) {     if(icon instanceof imageicon)     {         return ((imageicon) icon).getimage();     }     else     {         bufferedimage image = new bufferedimage(icon.geticonwidth(), icon.geticonheight(), bufferedimage.type_int_rgb);         icon.painticon(null, image.getgraphics(), 0, 0);         return image;     } } 

the thing is, painticon function throws me nullpointerexception on image.getgraphics().

for record, icon value default checkbox icon (obtained via uimanager.geticon("checkbox.icon"))

here's details of exception thrown :

exception in thread "awt-eventqueue-0" java.lang.nullpointerexception     @ com.sun.java.swing.plaf.windows.windowsiconfactory$checkboxicon.painticon(windowsiconfactory.java:306)     @ utils.warningrenderer.icontoimage(warningrenderer.java:50)     @ utils.warningrenderer.<init>(warningrenderer.java:38)     @ deliveryexpress.deliveryexpressview.setwarnings(deliveryexpressview.java:278)     @ deliveryexpress.deliveryexpressview.updatelists(deliveryexpressview.java:218)     @ deliveryexpress.deliveryexpressview.access$1100(deliveryexpressview.java:47)     @ deliveryexpress.deliveryexpressview$5.addcheck(deliveryexpressview.java:183)     @ org.japura.gui.model.defaultlistcheckmodel.firechecklistmodellisteners(unknown source)     @ org.japura.gui.model.defaultlistcheckmodel.fireaddchecklistmodellisteners(unknown source)     @ org.japura.gui.model.defaultlistcheckmodel.addcheck(unknown source)     @ org.japura.gui.checklist$1.mouseclicked(unknown source)     @ java.awt.awteventmulticaster.mouseclicked(awteventmulticaster.java:253)     @ java.awt.component.processmouseevent(component.java:6292)     @ javax.swing.jcomponent.processmouseevent(jcomponent.java:3267)     @ java.awt.component.processevent(component.java:6054)     @ java.awt.container.processevent(container.java:2041)     @ java.awt.component.dispatcheventimpl(component.java:4652)     @ java.awt.container.dispatcheventimpl(container.java:2099)     @ java.awt.component.dispatchevent(component.java:4482)     @ java.awt.lightweightdispatcher.retargetmouseevent(container.java:4577)     @ java.awt.lightweightdispatcher.processmouseevent(container.java:4247)     @ java.awt.lightweightdispatcher.dispatchevent(container.java:4168)     @ java.awt.container.dispatcheventimpl(container.java:2085)     @ java.awt.window.dispatcheventimpl(window.java:2478)     @ java.awt.component.dispatchevent(component.java:4482)     @ java.awt.eventqueue.dispatcheventimpl(eventqueue.java:644)     @ java.awt.eventqueue.access$000(eventqueue.java:85)     @ java.awt.eventqueue$1.run(eventqueue.java:603)     @ java.awt.eventqueue$1.run(eventqueue.java:601)     @ java.security.accesscontroller.doprivileged(native method)     @ java.security.accesscontrolcontext$1.dointersectionprivilege(accesscontrolcontext.java:87)     @ java.security.accesscontrolcontext$1.dointersectionprivilege(accesscontrolcontext.java:98)     @ java.awt.eventqueue$2.run(eventqueue.java:617)     @ java.awt.eventqueue$2.run(eventqueue.java:615)     @ java.security.accesscontroller.doprivileged(native method)     @ java.security.accesscontrolcontext$1.dointersectionprivilege(accesscontrolcontext.java:87)     @ java.awt.eventqueue.dispatchevent(eventqueue.java:614)     @ java.awt.eventdispatchthread.pumponeeventforfilters(eventdispatchthread.java:269)     @ java.awt.eventdispatchthread.pumpeventsforfilter(eventdispatchthread.java:184)     @ java.awt.eventdispatchthread.pumpeventsforhierarchy(eventdispatchthread.java:174)     @ java.awt.eventdispatchthread.pumpevents(eventdispatchthread.java:169)     @ java.awt.eventdispatchthread.pumpevents(eventdispatchthread.java:161)     @ java.awt.eventdispatchthread.run(eventdispatchthread.java:122) 

if need more details, tell me, i'll edit post add them.

thanks !

just found code snippet might if want wrap misbehaving laf provided icons more often:

/**  * ui-icons misbehave in unconditionally class-cast   * component type painted on. consequently blow if   * trying paint them anywhere else (f.i. in renderer).    *   * icon adaption of cool trick darryl burke/rob camick found @  * http://tips4java.wordpress.com/2008/12/18/icon-table-cell-renderer/#comment-120  *   * base idea instantiate component of type expected icon,   * let paint graphics of bufferedimage , create imageicon it.  * in subsequent calls imageicon used.   *   */ 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) {             // went wrong - fallback 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);           } {               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 in snippet, pass in arbitrary component:

    icon = new safeicon(icon);     bufferedimage image = new bufferedimage(icon.geticonwidth(), icon.geticonheight(), bufferedimage.type_int_rgb);     icon.painticon(new jpanel(), image.getgraphics(), 0, 0); 

Comments