Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Image conversion between AWT and FX

darrylburkeJun 11 2011 — edited Dec 3 2011
It isn't difficult to write a utility method to convert a non-animated <tt>java.awt.Image</tt> to <tt>javafx.scene.image.Image</tt>. But short of using <tt>Robot</tt> to obtain a screenshot (and hope that no part of the fx Image is obscured) is there a way to do the reverse? And is there a way to convert an animated <tt>java.awt.Image</tt> to <tt>javafx.scene.image.Image</tt>?
  public static javafx.scene.image.Image createImage(java.awt.Image image) throws IOException {
    if (!(image instanceof RenderedImage)) {
      BufferedImage bufferedImage = new BufferedImage(image.getWidth(null),
              image.getHeight(null), BufferedImage.TYPE_INT_ARGB);
      Graphics g = bufferedImage.createGraphics();
      g.drawImage(image, 0, 0, null);
      g.dispose();

      image = bufferedImage;
    }
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ImageIO.write((RenderedImage) image, "png", out);
    out.flush();
    ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
    return new javafx.scene.image.Image(in);
  }
Any inputs are appreciated.

db

Comments

Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Dec 31 2011
Added on Jun 11 2011
17 comments
13,878 views