Sign in to follow this  
tufflax

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

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();
		cp.add(panel);	
	
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


		frame.setSize(WIDTH, HEIGHT);
		frame.setVisible(true);
		panel.setVisible(true);


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

		test();
	}	
	
	private void test() {
		drawplayer(ig);
		panel.paintComponent(pg);
		
	}

	public void drawplayer(Graphics2D g) {
		g.setColor(Color.black);
		g.fillRect(0, 0, 50, 50);
	}


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

		protected void paintComponent(Graphics g) {
			super.paintComponent(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);

to

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this