I'm trying to draw an Image to a BufferedImage using a Graphics2D object from the createGraphics() method. My code is shown below:
BufferedImage myImage = new BufferedImage(17*32, 12*32, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = myImage.createGraphics();
Image currImage = app.getImage(app.getCodeBase(), "tile.png");
g2d.drawImage(currImage, 5, 5, app);
app is an Applet object passed in through the constructer in an earlier segment of code. When I run the applet it doesn't show the image. I've tried drawing rectangles before and after the image loading/displaying, and the rectangles show up but without the image. I tried loading the image in my applet class and the image loaded. What am I doing wrong?
drawImage(currImage, 5, 5, null) ?
by the way: it could be, that loading the image is not finished, when you paint it.
for such cases you have to use the mediatracker, which will wait for the image to be loaded before it continues.
and also: you paint the image on the buffered image and not on the applet.
you will have to paint your image on the applets graphics object, eg in the paint(Graphics g) method of the applet:
((Graphics2D)g).drawImage(currImage, 5, 5, null)
and not on a seperately created buffered image.
Well I need to get a bunch of Images onto a BufferedImage later, but right now I'm just trying to get it to draw one to get it to work. I tried putting null instead of app and nothing works. I also used a MediaTracker to make it not add the image until it was loaded. None of those worked. Any other ideas?
Edit: nevermind, found that none of my images were loading for some reason.
you have to paint your buffered image onto the graphics of the applet. as far as can see, you paint everything on the buffered image, but not the buffered image on the applet.
I already had a method to paint the buffered image. It takes in a Graphics object and paints the buffered image to that graphics object. I just didn't include it because I felt it was unnecessary to. Also, I check how many images had loaded during each frame and it keeps saying 0 have loaded.
Edit: figured it out. I forgot to make the mediatracker check all the images every frame.