Jump to content
  • Advertisement
Sign in to follow this  

[java] Working with BufferedImage, something is worng :(

This topic is 4624 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey! I need some help figuring out what's wrong with my program. It's supposed to become an air hockey game some day. So, I need to draw graphics. The way I've chosen to do it is by calling paintCompontent to draw the graphics. In paintComponent (on my HockeyPane extends JPanel) I've chosen to draw a buffered image instead of directly drawing the puck and everything else. That way it becomes easier to create methods like drawPuck() because I can draw on the BufferedImage and then every frame draw the whole image in my paintComponent method. But, it doesn't work and I need help. I've looked at similar code at tutorials and read documentation for a couple of hours now so I'd appreciate it if someone would try to help me. Additional comments are welcomed. Here is the code: Note: in the paintComponent method; if I change drawImage to fillRect or something it works perfectly, so that suggests that there is something wrong with the way I handle the BufferedImage.
class AH {
	public static final int WIDTH = 500;
	public static final int HEIGHT = 500;

	JFrame frame;
	Container cp;
	Graphics2D pg;
	Graphics2D ig;
	HockeyPane panel;

	public AH() {
		frame = new JFrame("Airhockey");
		cp = frame.getContentPane();
		panel = new HockeyPane();

		frame.setSize(WIDTH, HEIGHT);

		pg = (Graphics2D) panel.getGraphics();
		ig = panel.getImage().createGraphics();

	private void test() {

	public void drawplayer(Graphics2D g) {
		g.fillRect(0, 0, 50, 50);

	class HockeyPane extends JPanel {
		private BufferedImage image;
		public HockeyPane() {
			image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);

		protected void paintComponent(Graphics g) {
			Graphics2D g2 = (Graphics2D) g;
			g2.drawImage(image, null, 5, 5);
		public BufferedImage getImage() {
			return image;

Share this post

Link to post
Share on other sites
Ehm, I don't know why but I saw a little dot in the frame, so something was drawn. So I got the idea, I dunno why, but I changed

image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);


image = new BufferedImage(500, 500, BufferedImage.TYPE_INT_RGB);

and it worked. Can someone tell me why?

EDIT: Lol, I guess the JPanel had WIDTH and HEIGHT fields that replaced the WIDTH and HEIGHT of AH... well problem solved :)

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!