954,549 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Buffering Images

Hello,

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.drawImage(image,imgX,imgY,this);		
	b.setColor(Color.BLUE);
	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!

--Turt2Live

turt2live
Junior Poster in Training
85 posts since Jan 2011
Reputation Points: 15
Solved Threads: 3
 

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.

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

Thank You very much!

It worked :D

turt2live
Junior Poster in Training
85 posts since Jan 2011
Reputation Points: 15
Solved Threads: 3
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: