[java] createImage + MemoryImageSource - getGraphics() ?

Started by
3 comments, last by CodeMachine 17 years, 6 months ago
Hi I have a class, A. This class doesn't inherit another class. When I create instances of this class, I want to create an image within the constructor. I then use this method: Image img = Toolkit.getDefaultToolkit().createImage(new MemoryImageSource(w, h, pixels, 0, w)); Is there a way to get a Graphics object from the image returned from createImage above? If I call "img.getGraphics()" I get the error: "getGraphics() not valid for images created with createImage(producer)" Kind Regards
Advertisement
GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration().createCompatableImage(width, height);
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][LWJGL Tutorials for NeHe][LWJGL Wiki][jMonkey Engine]
Hi Captain
That method is abstract, it doesn't work. :|

Is there an option where I still can use my raw-pixel array to fill the bitmap?
And have the ability to "getGraphics()"?

Kind Regards
You are using it wrong. Here is code that works.

import java.awt.GraphicsEnvironment;import java.awt.image.BufferedImage;public class Test {    public static void main(String[] args) {        BufferedImage im = GraphicsEnvironment.getLocalGraphicsEnvironment().                getDefaultScreenDevice().getDefaultConfiguration().createCompatibleImage(400, 400);        System.out.println(im);    }}
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][LWJGL Tutorials for NeHe][LWJGL Wiki][jMonkey Engine]
Thanks!

This topic is closed to new replies.

Advertisement