Scrolling backround using swing

Hi,
I have been trying to find out how to make a scrolling backroud for a 2D game, but i cant find a good tutorial that explains it well enough.
If it is not too much trouble, could someone try to explain its principle. And if possible, a simple example program with comments on the lines that matter the most for it to work.

Re: Scrolling backround using swing

Re: Scrolling backround using swing

Just create a variable named xs representing x scroll, whenever you draw something to the screen excluding the player subtract xs from it. Draw the player in the middle of the screen. Whenever the player moves increments xs by the same amount.

Re: Scrolling backround using swing

Re: Scrolling backround using swing

You mean it like this: the xs represents my position on the backround. whenever i move the backround moves depending of my movement. And the backround isnt drawn fully on the screen, but the part of where i am.

Next question is: how can i draw a part of the picture on the screen?

Sorry for grammar mistakkes and stuff like that, i just came back from a week long camp in the woods and my mind doesent work fully yet.

Re: Scrolling backround using swing

Originally Posted by vastrolorde

You mean it like this: the xs represents my position on the backround. whenever i move the backround moves depending of my movement. And the backround isnt drawn fully on the screen, but the part of where i am.

Next question is: how can i draw a part of the picture on the screen?

Sorry for grammar mistakkes and stuff like that, i just came back from a week long camp in the woods and my mind doesent work fully yet.

Re: Scrolling backround using swing

You could store the background image in a BufferedImage, override paintComponent() on your display component, and use the methods of the Graphics context to copy the appropriate part of the image into the component. If your game is made up of repeating tiles, you could do this many times to draw each screen. Once you get this working, replace the BufferedImage with a VolatileImage. VolatileImages are trickier to use because they can be can be invalidated at any time; you have to check if the image is valid before and after copying from it. But they're potentially hardware accelerated, so many times faster than a software copy from a BufferedImage.

Re: Scrolling backround using swing

Question 1:

Java Code:

public class Player
{
private int posX, posY, scrollX, scrollY;
/*
* posX is the x position on the screen
* posY is the y position on the scrren
* scrollX is the amount to which the background scrolls in the x dimension
* scrollY is the amount to which the background scrolls in the y dimension
*/
private BufferedImage img, img2;
/*
* img The background of that you want to scroll
* img2 the character image
*/
public Player()
{
//set all the values up, I didn't set the image up in the code I assumed you can do that.
posX = 75;
posY = 100;
scrollX = 0;
scrollY = 0;
}
public void draw(Graphics g)
{
Graphics2D g2d = (Graphics2D) g;
/*
* to draw a scrolling background there is some obvious things we need to do
* have a dynamic variable as a coordinate
* control it so that it moves slowly and doesn't jump
* move from left to right(this is a presumption on my part)
* so lets tackle one at a time
* we set it so that the image is drawn at the scrollX and scrollY values instead of constant x and y values
* in the move methods below you will see that we only increase one each time this is so that it gives the impression of moving and not jumping from place to place
* again if you look at the move methods below when you move right the backgrounds coordinates get more negative(-) but when you go left it gets more possative
*/
g2d.drawImage(img, scrollX, scrollY, null);
g2d.drawImage(img2,posX,posY,null);
}
public void moveRight()
{
scrollX --;
}
public void moveLeft()
{
scrollX ++;
}
public void moveUp()
{
scrollY ++;
}
public void moveDown()
{
scrollY ++;
}
}

this is obviously the most bare bones idea but just to try and clear things up if they where not.

Question 2:

well if I understand you right the best way I think is to use java.awt.Graphics method

Draws as much of the specified area of the specified image as is currently available, scaling it on the fly to fit inside the specified area of the destination drawable surface. Transparent pixels do not affect whatever pixels are already there.
This method returns immediately in all cases, even if the image area to be drawn has not yet been scaled, dithered, and converted for the current output device. If the current output representation is not yet complete then drawImage returns false. As more of the image becomes available, the process that draws the image notifies the specified image observer.

This method always uses the unscaled version of the image to render the scaled rectangle and performs the required scaling on the fly. It does not use a cached, scaled version of the image for this operation. Scaling of the image from source to destination is performed such that the first coordinate of the source rectangle is mapped to the first coordinate of the destination rectangle, and the second source coordinate is mapped to the second destination coordinate. The subimage is scaled and flipped as needed to preserve those mappings.

Parameters:
img - the specified image to be drawn
dx1 - the x coordinate of the first corner of the destination rectangle.
dy1 - the y coordinate of the first corner of the destination rectangle.
dx2 - the x coordinate of the second corner of the destination rectangle.
dy2 - the y coordinate of the second corner of the destination rectangle.
sx1 - the x coordinate of the first corner of the source rectangle.
sy1 - the y coordinate of the first corner of the source rectangle.
sx2 - the x coordinate of the second corner of the source rectangle.
sy2 - the y coordinate of the second corner of the source rectangle.
observer - object to be notified as more of the image is scaled and converted.

Returns:
true if the current output representation is complete; false otherwise.

this works based on your players x coordinate and the point to which the background changes divided by the scrolling images width taken twice and that remainder. if this still don't make complete sense of this(because I honestly don't really know how to explain this any better) I suggest you take it plug some numbers in and it might make some more sense. just remember that actPoint is where the next background will begin

Re: Scrolling backround using swing

Bout that scrolling backound to make it infinite scroll. I madeit so: it draws 2 backround at once. (cordinates on X axis only) 0 - 400 is the first image and 400-800 is the second image. When the variable sxrollX raches 400 it will be zeroed out and loop start at the beginning.