Scrolling background

Hi :slight_smile:

ok, first of all , excuse me for my english , it’s my third langage so … :stuck_out_tongue:

what i wanna do is, a scrolling background for my little game ( Raptor 2D), exactly like in this link http://www.planetalia.com/cursos/Java-Invaders/JAVA-INVADERS-25.tutorial

the problem is , that i don’t understaind what they did ! so i’m gonna show u a few parts of my code , and please tell me what i gotta do to obtaint this stupid scrolling background :smiley:

ok i have a classe scene.java , where i draw the background , in the method paint

super.paintComponent(g);
            
    
            
            Graphics2D g2d = (Graphics2D) g;
            // Dessiner l'arrière plan
            double scalex = (double)this.getWidth()  / (double)image.getIconWidth();
            double scaley = (double)this.getHeight() / (double)image.getIconHeight();
            g2d.scale(scalex,scaley);
            

            g2d.drawImage(image.getImage(),0,0,image.getImageObserver());
            g2d.scale(1/scalex,1/scaley);
            g2d.setColor(Color.LIGHT_GRAY);
            g.setFont( new Font( "", Font.ITALIC, 24 ) );
            g2d.drawString( "Vies : "+vie+"%", 10, 30 );
            g2d.drawString( "Scores : "+score, 10, 60 );
            g2d.drawString( "Missiles : 000 ", 10, 90 );
            
            
            // Afficher le vecteur
            for(int i=0;i<elements.size();i++)
            {
                elements.get(i).paintComponent(g);
            }
            
     }
    

image is the background of my game ! so help me please to scroll it

thankxxxxxxxx

UP :-[

How about this:

	
private int xoffset = 0; // how much is the tiles moved in x direction
private int yoffset = 0;
private int counter = 0; // this is an animation counter, the update method is called 20 times a second and we need to delay the update code a bit
private int dx = 1;
private int dy = -1;
private int d = 1; // the amount added when the user selects a direction with the arrow keys

public void update() {
	if (game.isKeyPressed(KeyEvent.VK_LEFT)) 
		dx += -d;
	else if (game.isKeyPressed(KeyEvent.VK_RIGHT)) 
		dx += d;

	if (game.isKeyPressed(KeyEvent.VK_UP)) 
		dy += -d;
	else if (game.isKeyPressed(KeyEvent.VK_DOWN)) 
		dy += d;
	
	counter++;
	if (counter > 10) {
		counter = 0;
		xoffset = (xoffset + dx) % TILE_SIZE;
		yoffset = (yoffset + dy) % TILE_SIZE;
		Graphics2D g2 = background.createGraphics();
		for (int row = 0; row < Y_COUNTS + 2; row++) {
			for (int col = 0; col < X_COUNTS + 2; col++) {
				g2.drawImage(circle,
					xoffset + TILE_SIZE * col - TILE_SIZE,
					yoffset + TILE_SIZE * row - TILE_SIZE,
					null);
			}
		}

		g2.dispose();
	}
}

As you see, I don’t use TexturePaint at all. This is a method invoked to update the game panel. I guess you can use TexturePaint to do a similar effect but
it will require you to create many texture paint objects with the same offset effect I created above.

Ok, there are some limitations here. Obviously, I’m painting outside the image, this is probably the lazy way of doing it.

Peter