I currently have this code to buffer an image:

public void paintComponent(Graphics g){
	Graphics2D g2 = (Graphics2D) g;
	ImageIcon icon = new ImageIcon("back.gif");
    Image image = icon.getImage();
    BufferedImage buff = new BufferedImage(image.getWidth(this), image.getHeight(this), BufferedImage.TYPE_INT_ARGB);
    Graphics2D b = (Graphics2D) buff.createGraphics();
	b.fillRect((window.getWidth()/2)-13, (window.getHeight()/2)-13, 26, 26);
	g2.drawImage(buff, 0, 0, this);

What it (should) do is load a background image. The background moves as I press the arrow keys (changing the imgX and imgY variables). The back.gif image is 5000 pixels square.

The issue is that when I don't buffer it flickers, when I use the above code, it takes FOREVER to move.

I know that 5000 pixel image isn't the best way to do this. And I think I'm on the right track when I say I need to crop it.

I would prefer to crop it in Java, although the code I find simply does not work. It either doesn't crop or doesn't show.

For reference: My JFrame is 1000x800.

Any advice/code is appreciated!


5 Years
Discussion Span
Last Post by turt2live

You re-load the image from file every time you draw it. That's wh yit's slow.
Read the file and create the buffered image once, at startup.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.