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!


Recommended Answers

All 2 Replies

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.

Thank You very much!

It worked :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.