public Object coerceToUi(Object beanProp, org.zkoss.zul.Image imgComp, BindContext ctx) { if (beanProp == null) { return null; } else if (beanProp instanceof org.zkoss.image.Image) { return beanProp; } else if (beanProp instanceof RenderedImage) { RenderedImage awtImg = (RenderedImage) beanProp; try { return Images.encode("a.png", awtImg); } catch (java.io.IOException ex) { throw UiException.Aide.wrap(ex); } } return beanProp; }
/** Encodes an AWT image into a ZK image. * * @param name The name of the image. The extension of the name must * be the format. For example, foo.png and foo.jpeg. * @param image The AWT image to be encoded. * @exception IllegalArgumentException if name doesn't contain an * extension with supported format. */ public static Image encode(String name, RenderedImage image) throws IOException { ImageEncoder encoder = ImageEncoders.newInstance(getFormat(name)); return new AImage(name, encoder.encode(image)); } private static String getFormat(String name) {
/** Encodes an AWT image into a ZK image (in PNG, JPEG or other format). * * @param name The name of the image. The extension of the name must * be the format. Supported format: png and jpeg. * For example, foo.png and foo.jpeg. * @param image The AWT image to be encoded. * @param quality The quality to use for the image encoding. * It is a number between 0 and 1. The higher the value, * the better the output quality. * Not applicable to "png". * @param encodeAlpha Whether to encode alpha transparency. * Not applicable to "png". * @exception IllegalArgumentException if name doesn't contain an * extension with supported format. */ public static Image encode(String name, RenderedImage image, float quality, boolean encodeAlpha) throws IOException { ImageEncoder encoder = ImageEncoders.newInstance(getFormat(name)); encoder.setQuality(quality); encoder.setEncodingAlpha(encodeAlpha); return new AImage(name, encoder.encode(image)); } /** Encodes an AWT image into a ZK image.
/** Sets the content directly with the rendered image. * It actually encodes the rendered image to an PNG image * ({@link org.zkoss.image.Image}) with {@link Images#encode}, * and then invoke {@link #setImageContent(org.zkoss.image.Image)}. * * <p>If you want more control such as different format, quality, * and naming, you can use {@link Images} directly. * * @since 3.0.7 */ public void setImageContent(RenderedImage image) { try { setImageContent(Images.encode("a.png", image)); } catch (java.io.IOException ex) { throw new UiException(ex); } }
/** Sets the content of the hover image directly with the rendered image. * The hover image is used when the mouse is moving over this component. * * <p>It actually encodes the rendered image to an PNG image * ({@link org.zkoss.image.Image}) with {@link Images#encode}, * and then invoke {@link #setHoverContent(org.zkoss.image.Image)}. * * <p>If you want more control such as different format, quality, * and naming, you can use {@link Images} directly. * @since 3.5.0 */ public void setHoverContent(RenderedImage image) { try { setHoverContent(Images.encode("hover.png", image)); } catch (java.io.IOException ex) { throw new UiException(ex); } }
/** Sets the content directly with the rendered image. * It actually encodes the rendered image to an PNG image * ({@link org.zkoss.image.Image}) with {@link Images#encode}, * and then invoke {@link #setContent(org.zkoss.image.Image)}. * * <p>If you want more control such as different format, quality, * and naming, you can use {@link Images} directly. * * @since 3.0.7 */ public void setContent(RenderedImage image) { try { setContent(image == null ? (org.zkoss.image.Image) null : Images.encode("a.png", image)); } catch (java.io.IOException ex) { throw new UiException(ex); } }
/** Sets the content of the hover image directly with the rendered image. * The hover image is used when the mouse is moving over this component. * * <p>It actually encodes the rendered image to an PNG image * ({@link org.zkoss.image.Image}) with {@link Images#encode}, * and then invoke {@link #setHoverImageContent(org.zkoss.image.Image)}. * * <p>If you want more control such as different format, quality, * and naming, you can use {@link Images} directly. * @since 3.5.0 */ public void setHoverImageContent(RenderedImage image) { try { setHoverImageContent(Images.encode("hover.png", image)); } catch (java.io.IOException ex) { throw new UiException(ex); } }