Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Java Game BufferedImage tiles with .png help

Status
Not open for further replies.

clax99

Programmer
Oct 29, 2001
23
0
0
US
Hi,
Im writing a small game in java (applet) and I am using 66x66 tiles saved as .png. I load the images as Image objects using getImage(), and then I create BufferedImage objects from them using the code below. The problem is I cant get the BufferedImage objects to render properly; they just show all black. The original Image objects, however, always render properly. Its driving me crazy. Here is the code snippet I use to create the BufferedImage objects:

//assume Image origImage exists

BufferedImage bImage = new BufferedImage(66,66,BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = bImage.createGraphics();
g2.drawImage(origImage,0,0,this);


I then render the BufferedImage objects in the normal fashion:

public void paint(Graphics g){
Graphics2D g2 = (Graphics2D)g;
g2.drawImage(bImage,x,y,this);
}

Any help would be much appreciated. Also, I have tried every single combination of BufferedImage type. Im not saying this couldnt be the problem, but, well, you know.
Thanks...
 
If using 1.4 or above, try loading the image like this :

BufferedImage bi = javax.imageio.ImageIO.read(new File(fileName));
 
Thanks for the help, but I tried ImageIO.read() last night and still the same problems. The only difference is that I passed ImageIO.read an InputStream. Hmm...
 
Try this :

Code:
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import javax.swing.*;

public class DrawImage extends JComponent {
	String file = "";

	public DrawImage(String file) {
		this.file = file;
	}

	public void paint(Graphics g) {
		try {
			Graphics2D g2 = (Graphics2D)g;
			BufferedImage image = javax.imageio.ImageIO.read(new File(file));
			g2.drawImage(image, 0, 0, null);

		} catch (Exception e) {
			e.printStackTrace(System.err);
		}
	}


	public static void main (String args[]) {
		JFrame f = new JFrame();
		f.setSize(600, 600);
		f.getContentPane().add(new DrawImage(args[0]));
		f.setVisible(true);

	}

}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top