[java] Stuck: Rendering to an Image...
I''m totally stuck. I''m sure there must be a way to do this, but
I just can''t figure it out. Any suggestions more than welcome!
What I need is to render text to an image.
I have the image, built using MemoryImageSource.
I have the Font.
I have the string I want to render.
I can''t fathom how to get hold of a Graphics object for the image.
There are two additional problems: I can''t use the Graphics2D class, and the image is an Image, not a BufferedImage. =P For
both the original interfaces must be used.
Any suggestions? I''ve tried everything I can think of...
(At the moment I''m thinking maybe I''ll have to create a new
image that I somehow have a Graphics for, and copy the image
I have into that, then render and copy back...somehow... or
something).
If you actually have the Image then just use:
Image myimage;
Graphics mygraphics;
mygraphics=myimage.getGraphics();
Then it is a trivial matter to use setFont() and setColor() and Graphics.drawString(..);
However when using a memoryimagesource you would usually modify the pixel data at the data array level.
Keep in mind however that a memoryimagesource is not an image, it is an image producer, to use it you must have an imageconsumer registered for it. So in short you cant have a Graphics of a memoryimagesource.
Image myimage;
Graphics mygraphics;
mygraphics=myimage.getGraphics();
Then it is a trivial matter to use setFont() and setColor() and Graphics.drawString(..);
However when using a memoryimagesource you would usually modify the pixel data at the data array level.
Keep in mind however that a memoryimagesource is not an image, it is an image producer, to use it you must have an imageconsumer registered for it. So in short you cant have a Graphics of a memoryimagesource.
If only life were so easy:
Exception in thread "main" java.lang.IllegalAccessError: getGraphics() only valid for images created with createImage(w, h)
at sun.awt.motif.X11Image.getGraphics(X11Image.java:26)
at ass1$Graphic.buildImage(ass1.java:737)
at ass1$Flow.flow(ass1.java:1323)
at ass1.run(ass1.java:159)
at ass1.main(ass1.java:120)
Which means get image doesn''t work. So how ELSE can I get a graphics for an image?
Nb. Yeah yeah. I know; you have to make an image from the
memory image source. No problem. The problem is once I have the
image, I can''t get the fsking Graphics out of it (Error).
Cheers.
Exception in thread "main" java.lang.IllegalAccessError: getGraphics() only valid for images created with createImage(w, h)
at sun.awt.motif.X11Image.getGraphics(X11Image.java:26)
at ass1$Graphic.buildImage(ass1.java:737)
at ass1$Flow.flow(ass1.java:1323)
at ass1.run(ass1.java:159)
at ass1.main(ass1.java:120)
Which means get image doesn''t work. So how ELSE can I get a graphics for an image?
Nb. Yeah yeah. I know; you have to make an image from the
memory image source. No problem. The problem is once I have the
image, I can''t get the fsking Graphics out of it (Error).
Cheers.
I had the same problem and no solution. I think that it is impossible to get the "Graphics" of such an Image from what i tried and read about that topic. Now, i'm directly blitting the text into the pixelarray...
[edited by - EgonOlsen on September 1, 2002 11:41:20 AM]
[edited by - EgonOlsen on September 1, 2002 11:41:20 AM]
I got it:
That should work, you can also use:
Jiim Jonez
www.javage.net
Edit(s): Changed the formatting.
[edited by - Jiim on September 1, 2002 2:03:07 PM]
[edited by - Jiim on September 1, 2002 2:04:14 PM]
[edited by - Jiim on September 1, 2002 2:05:33 PM]
MemoryImageSource src;Image img=Toolkit.getDefaultToolkit().createImage(src);Graphics g=img.getGraphics();g.setFont(....);g.drawString(....);
That should work, you can also use:
Component c;Image img=c.createImage(src);
Jiim Jonez
www.javage.net
Edit(s): Changed the formatting.
[edited by - Jiim on September 1, 2002 2:03:07 PM]
[edited by - Jiim on September 1, 2002 2:04:14 PM]
[edited by - Jiim on September 1, 2002 2:05:33 PM]
Neither of those work when I tried them.
It bitched Component was abstract and "Exception in thread "main" java.lang.IllegalAccessError: getGraphics() only valid for images created with createImage(w, h)" as before. If you have a working
code snippet, please post it.
It bitched Component was abstract and "Exception in thread "main" java.lang.IllegalAccessError: getGraphics() only valid for images created with createImage(w, h)" as before. If you have a working
code snippet, please post it.
The problem is due to the MemoryImageSource, Java awt will not allow you to get a graphics handle to an image that was created from a memory source. The reason, I believe, is that the AWT api passes all calls directly to native code that uses the OS native windowing api. Surfaces that you want to draw to, like an image, must be surfaces that the windowing api is aware of. So when you create an image using Component::createImage(int,int), it is actually creating an image in the native windowing environment, whereas an image created from a image producer does not, or at least does not necessarily. So, the native windowing API is not aware of the MemoryImageSource created image. So calls like Graphics::drawString that are actually implemented in the native windowing environment cannot manipulate the image.
So.. The only way to create a drawable image would be to create an image using Component::createImage(int,int). If you need to use an image created from an ImageProducer, I believe that you will have to create that image separately and then draw it to an image created from Component::createImage(int,int).
An example
>>>>>>>>>>
import java.awt.*;
import java.awt.image.*;
public class test {
public static void main ( String args[] ) {
test instance = new test();
instance.run();
}
public void run() {
imgContainer imgHolder = new imgContainer();
Frame mainFrame = new Frame();
mainFrame.add(imgHolder);
mainFrame.show();
imgHolder.createImage();
}
public class imgContainer extends Canvas{
Image imgMem;
Image imgWin;
public void imgContainer () {
imgMem = null;
imgWin = null;
}
public void createImage() {
int w = 100;
int h = 100;
int pix[] = new int[w * h];
int index = 0;
for (int y = 0; y < h; y++) {
int red = (y * 255) / (h - 1);
for (int x = 0; x < w; x++) {
int blue = (x * 255) / (w - 1);
pix[index++] = (255 << 24) | (red << 16) | blue;
}
}
imgMem = createImage(new MemoryImageSource(w, h, pix, 0, w));
imgWin = createImage(w,h);
Graphics g = imgWin.getGraphics();
g.drawImage(imgMem, 0, 0, null);
g.drawString("Hi", 50, 50);
g.dispose();
}
public void paint( Graphics g ) {
if ( imgWin != null ) {
g.drawImage(imgWin, 0, 0, null);
}
}
}
}
<<<<<<<<<<<<<<<<<<<<<<
So.. The only way to create a drawable image would be to create an image using Component::createImage(int,int). If you need to use an image created from an ImageProducer, I believe that you will have to create that image separately and then draw it to an image created from Component::createImage(int,int).
An example
>>>>>>>>>>
import java.awt.*;
import java.awt.image.*;
public class test {
public static void main ( String args[] ) {
test instance = new test();
instance.run();
}
public void run() {
imgContainer imgHolder = new imgContainer();
Frame mainFrame = new Frame();
mainFrame.add(imgHolder);
mainFrame.show();
imgHolder.createImage();
}
public class imgContainer extends Canvas{
Image imgMem;
Image imgWin;
public void imgContainer () {
imgMem = null;
imgWin = null;
}
public void createImage() {
int w = 100;
int h = 100;
int pix[] = new int[w * h];
int index = 0;
for (int y = 0; y < h; y++) {
int red = (y * 255) / (h - 1);
for (int x = 0; x < w; x++) {
int blue = (x * 255) / (w - 1);
pix[index++] = (255 << 24) | (red << 16) | blue;
}
}
imgMem = createImage(new MemoryImageSource(w, h, pix, 0, w));
imgWin = createImage(w,h);
Graphics g = imgWin.getGraphics();
g.drawImage(imgMem, 0, 0, null);
g.drawString("Hi", 50, 50);
g.dispose();
}
public void paint( Graphics g ) {
if ( imgWin != null ) {
g.drawImage(imgWin, 0, 0, null);
}
}
}
}
<<<<<<<<<<<<<<<<<<<<<<
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement