2 Replies - 3609 Views - Last Post: 02 November 2014 - 10:58 AM Rate Topic: -----

#1 shortbus   User is offline

  • D.I.C Head

Reputation: 17
  • View blog
  • Posts: 235
  • Joined: 26-March 13

how to make a scrolling background in java

Posted 01 November 2014 - 08:11 PM

i am trying to make a platform game in java and to do this im trying to make a scrolling bacground. i can get the background image to scroll. However, i cant get the image to scroll forever, here is the code.

GamePanel class ( the jpanel )


public class GamePanel extends JPanel implements ActionListener{
	
	static ArrayList<BackGround> store = new ArrayList<BackGround>();
	public GamePanel(){
		setFocusable(true);
		Timer time = new Timer(5,this);
		time.start();
		store.add(new BackGround(0,-200));
	}
	
	public void paint(Graphics g){
		super.paint(g);
		for(int a = 0; a < store.size(); a++){
			store.get(a).drawModel(g);
		}
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
		for(int a = 0; a < store.size(); a++){
			store.get(a).update();
			store.get(a).displayX();
			if(store.get(a).getX() <= -950 ){
					GamePanel.store.get(a).setX(-900);
					GamePanel.store.add(new BackGround(-951,-200));
					
				}
			}
		
		repaint();
	}
	
}


the background class

import java.awt.Graphics;
import java.awt.Image;

import javax.swing.ImageIcon;


public class BackGround {
	private int x = 0;
	private int y = -200;
	public BackGround(int x , int y){
		this. x = x;
		this.y = y;
	}
	
	public void update(){
		x -= 1;
		
	}
	public void drawModel(Graphics g){
		g.drawImage(getBackgroundImage(),x,y,null);
		
		
	}
	
	public Image getBackgroundImage(){
		ImageIcon ic = new ImageIcon("woods.png");
		
		return ic.getImage();
	}
	public void displayX(){
		System.out.println(x);
	}
	public int getX(){
		return x;
	}
	public void setX(int x){
		this.x = x;
	}
}


the problem i have is that i want the bacground to loop. however, once the first instance of the background is done scrolling it freezes and doesnt load anymore. here is the code for adding a new background to the list
if(store.get(a).getX() <= -950 ){
					GamePanel.store.get(a).setX(-900);
					GamePanel.store.add(new BackGround(-951,-200));
					
			}



Is This A Good Question/Topic? 0
  • +

Replies To: how to make a scrolling background in java

#2 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5239
  • View blog
  • Posts: 14,035
  • Joined: 18-April 07

Re: how to make a scrolling background in java

Posted 01 November 2014 - 10:58 PM

I notice that when you first add a background, you start the X at a position of 0 and then count down until it reaches -950. At which time you set the first background to -900? Then why do you add another background at -951? I would think that any time you add another background to the list, you would also start it at the end of the proceeding graphic.

For instance, let's assume you have a panel that is 1000 pixels wide. You have a graphic that is 1200 pixels wide you want to scroll. Graphic 1 would start at 0 and graphic 2 would be set at 1200. As graphic 1 scrolls down, once it reaches -200, the second graphic is going to come onto the panel. Once background 1 reaches -1200, background 1 is completely off the screen and that means background 2 will now be at 0. So background 1 can be set to 1200 again and the whole process repeats.

Also when a background instance is totally off the screen, you would remove it from the beginning of the list and move it to the end of the list. Otherwise you will continue to add more and more classes to your list and take up a lot of resources and after awhile crash your computer. The most you should have is 2 or 3 background images for your standard repeating background.

So perhaps you can explain your if statement and why you are resetting a background from -950 back to -900 and starting your next background at -951. :)
Was This Post Helpful? 0
  • +
  • -

#3 shortbus   User is offline

  • D.I.C Head

Reputation: 17
  • View blog
  • Posts: 235
  • Joined: 26-March 13

Re: how to make a scrolling background in java

Posted 02 November 2014 - 10:58 AM

i set it 900 and back to 951 so it doesnt spawn two backgrounds when a new one is added. however, when a new one is added to the arraylist the whole program just freezes and i dont know know why it does that
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1