Background Loop causes performance drop

Hi,

I’m working on an auto-sidescroller like R-Type at the moment.
I implemented a simple background picture which scrolls automatically in order to “simulate” the movement.
But then I wanted to add a second layer to the background (stars which are “closer” to the player and scroll faster).
I took a transparent .png pic which had the same size as my background and added it to the paintComponent()-method of the panel on which everything is drawn (i’m using Swing at the moment).
Unfortunately that pretty much “killed” the performance.
So I decided to take a smaller picture of the layer and just let it loop.
I thought: Once the end of the current layer-picture reaches the right edge of the frame (while scrolling to the left) the same picture has to be created at just that position (and once the current picture is completely out of sight it can be deleted in order to avoid trash objects).
So I added the pictures to a Linked List and everytime the gameview draws the frame (respectively has to get the linked list) my algorithm checks the above mentioned conditions and adds/deletes pictures from the list.
This worked much better than the big picture, but now I have the following problem:
Everytime a new picture is created/added to the list the game has a performance drop for a split second.

This is the algorithm where i add the pictures to the linked list.

	
	public LinkedList<GraphicBackground> getLayers(){
		
		if(layers.getLast().getX()+layers.getLast().getWidth()<this.width){
			this.layers.add(new GraphicLayer(1));
			System.out.println("New Layer drawn!");
		}
		if(layers.getFirst().getX()+layers.getFirst().getWidth()<0){
			layers.removeFirst();
			System.out.println("Old Layer removed!");
		}
		return layers;
	}

After this the linked list is given to the draw-method of the gameview, which goes through all objects and draws them.

“layers.getLast().getX()+layers.getLast().getWidth()<this.width” is the moment where the end of the layer-picture reaches the right edge of the frame.
When this line is executed:
this.layers.add(new GraphicLayer(1));
the performance drop occurs.

The GraphicLayer Class (you can ignore the modifier next to the 1245 (which is the frame size)):

public class GraphicLayer extends GraphicBackground {

	public GraphicLayer(int mod) {
		super(1245*mod, 0, "images/layer.png", 10);
		
	}

}

And these are the two super classes:

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class GraphicBackground extends GraphicObject {
	File imagepath;
	BufferedImage image;
	int speed;
	int width;
		
	public int getX(){
		return this.x;
	}

	public int getStartingPos(){
		return this.startingpos;
	}
	
	public int getWidth(){
		return this.width;
	}
	
	public GraphicBackground(int x, int y, String imgepth, int speed){
		this.x=x;
		this.y=y;
		this.speed=speed;
		
		imagepath = new File(imgepth);
		try{
			this.image = ImageIO.read(imagepath);
		}catch(IOException e){
			System.out.println("Can't find file!");
		}
		this.width=image.getWidth();
	}
	
	public void draw(Graphics g){
		if(this.x>-10000+1245){
			this.x-=speed;
		}
		g.drawImage(this.image, this.x, this.y, null);
	}
	
}
import java.awt.Graphics;

public abstract class GraphicObject {
	int x;
	int y;
	
	public abstract void draw(Graphics g);
}

Any ideas how i can get rid of this problem?

Cheers
Wolfner

Edit:
Btw the .png-pic only has 18,5kb.

if you really want fast performance of sounds and graphics (especially the type expected in bullet hell shmups) then its better you use a proper java games library which has hardware acceleration instead of Swing. Swing isn’t really designed for high performance games.

I’d recommend Slick2D, it has very similar api to Java2D so should be very easy to move your code over, you get fast graphics (accelerated by opengl) and fast sound (accelerated by openal).

Regardless of the rendering backend, you should not load compressed images from disk on the rendering thread. Even OpenGL acceleration will have hickups when you use ImageIO.read during rendering.

Create some thread(s) that do the loading of the images in the background, give the threads a low priority.

Make some kind of queueing mechanism, so that you can request an image way ahead of time, and can be reasonably sure the image will be there when you need it. Using the classes in java.util.concurrent will greatly simplify the job.

Loading the picture in advance solved the “hickup” problem thanks!
Nevertheless i guess i will also take a look at Slick2D. Thanks for the tip!