drawString issue
Why is it that you can use Graphics.drawString on a Graphics Object but when the Graphics object was obtained from an image created from an array of pixels, drawstring cannot be applied? I am assuming internally both types of graphics devices (ie ones created from system devices and ones created manually from array of ints) would store the image data as an array of pixels? So what is the problem?
Thanks
Sriram
[440 byte] By [
spany] at [2007-9-26 1:14:22]

In your code, how are you getting the reference to your Graphics context? Note that if you are rendering the image on a Component and then calling getGraphics(), this method will return null if the Component is not currently displayable...
Hi
Actually the Error is with getGraphics itself. I am doing this:
int pixels[] = new int[100 * 100];
MemoryImageSource mis = new MemoryImageSource(pixels, 100, 100, myColorModel, pixels, 0, 100);
Image im = createImage(mis);
Graphisc ig = im.getGraphics();
At this line I get this exception::
Exception occurred during event dispatching:
java.lang.IllegalAccessError: getGraphics() only valid for images created with createImage
(w, h)
at sun.awt.windows.WImage.getGraphics(Unknown Source)
at t.paint(t.java:37)
at sun.awt.RepaintArea.paint(Unknown Source)
at sun.awt.windows.WComponentPeer.handleEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Any Ideas?
Regards
spany at 2007-6-29 0:31:30 >

This thread at DejaNews is addressing the same problem: http://groups.google.com/groups?hl=en&safe=off&th=9b7650c11440a3f1,3&seekm=gtalFAI60B.K6v%40netcom.com#pKurta
Hi
I went to Deja but it doesnt fully address the problem. Their solution is to use createImage(int, int) to get an image. However, This has a few drawbacks:
The image cannot be "transparent". For example, if an image is created using a MemoryImageSource, the array of pixels (which are ints) can be used to specify the transparency of the pixels.
Secondly, It would be faster to use an array of pixels for many applications and basically call "newPixels()" when a change has been made. eg, if I need to set only say pixel A, B and C to a certain value, i can do it as:
pixelData[index] = colorValue;
However, by using a Graphics object of an Image, I would have to call image.getGraphics().drawLine(x, y, x, y);
It seems inefficient to call this function simply to set the value of a pixel. Whats worse, is that as you get more pixels to fill in, this function needs to be called each time.
Or is it actually possible, to manipulate the image (that is created using "createImage") by actually modifying the data rather than having to manipulate its graphics object?
Any Suggestions.
Sriram
spany at 2007-6-29 0:31:30 >

> Or is it actually possible, to manipulate the image
> (that is created using "createImage") by actually
> modifying the data rather than having to manipulate
> its graphics object?
This might work. There's a PixelGrabber class that can be used to retrieve a subset of the pixels in an image. Then, after modifying the pixels, you can create an image as you did (MemoryImageSource) and draw that image onto the graphics context of the image created by the component.
Weird, but should work.
Kurta
Hi
I was thinking about using the Pixel Grabber class. However, the pixel array that is extracted from the image needs to created into a new image. SO each time we do this we are creating new objects and wasting enormous amounts of space. This is a very expensive option.
Regards
Sriram
spany at 2007-6-29 0:31:30 >

See the API docs of java.awt.BufferedImage! Maybe it suits you. (It is said that its getGraphics works as well.)Kurta
Also, there's a bug in at least some Micro$oft VMs and PixelGrabbers: something weird happens to the color components when you try to obtain the pixels from an image created with createImage. I'm not sure but it looks like they forget one left shift: the red component is always 0.
HiI know I had this problem some times. But when I changed the color depth of my monitor it worked fine.I tried the BufferedImage. But this is only for swing. I am looking for a 1.1 AWT solution. ThanksSriram
spany at 2007-6-29 0:31:30 >
