0

I need to make my background in a game move up all the time..

I know that I need to use some thread that added variable to 'y' coordinate of the image

I tried to do something but when it started moving all the background get streaking in some reason, cant understand why...

picture : streaking background

public class Background {
private BufferedImage image;
.....
....

public Background(int x, int y) {
    this.x = x;
    this.y = y;

    // Try to open the image file background.png
    try {
        BufferedImageLoader loader = new BufferedImageLoader();
        image = loader.loadImage("/backSpace.png");

    }
    catch (Exception e) { System.out.println(e); }

}

/**
 * Method that draws the image onto the Graphics object passed
 * @param window
 */
public void draw(Graphics window) {

    // Draw the image onto the Graphics reference
    window.drawImage(image, getX(), getY(), image.getWidth(), image.getHeight(), null);

    // Move the x position left for next time
    this.y +=1 ;
}


public class ScrollingBackground extends Canvas implements Runnable {

// Two copies of the background image to scroll
private Background backOne;
private Background backTwo;

private BufferedImage back;

public ScrollingBackground() {
    backOne = new Background();
    backTwo = new Background(backOne.getImageWidth(), 0);

    new Thread(this).start();
    setVisible(true);
}

@Override
public void run() {
    try {
        while (true) {
            Thread.currentThread().sleep(5);
            repaint();
        }
    }
    catch (Exception e) {}
}

@Override
public void update(Graphics window) {
    paint(window);
}

public void paint(Graphics window) {
    Graphics2D twoD = (Graphics2D)window;

    if (back == null)
        back = (BufferedImage)(createImage(getWidth(), getHeight()));

    Graphics buffer = back.createGraphics();

    backOne.draw(buffer);
    backTwo.draw(buffer);

    twoD.drawImage(back, null, 0, 0);

}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
user2922456
  • 391
  • 4
  • 10
  • 24
  • What do you mean be 'streaking' – Lex Webb Apr 29 '14 at 16:06
  • I edit the question and add picture of the situation.. – user2922456 Apr 29 '14 at 16:12
  • First of all you probably shouldn't be calling repaint(), its really meant to internal use. Secondly, the reason you are seeing streaking is because the graphics area is not being cleared is because calling repaint manually can prevent your canvas from being cleared. You should use update() instead. – Lex Webb Apr 29 '14 at 16:20
  • Try calling the paint method directly? – Lex Webb Apr 29 '14 at 17:04
  • 1) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete and Verifiable Example). 2) One way to get image(s) for an example is to hot-link to the images seen in [this answer](http://stackoverflow.com/a/19209651/418556). – Andrew Thompson Apr 30 '14 at 07:06

1 Answers1

1

You dont need a new thread for it, that just overcomplicates things in this case. You just need to add a factor to the Y coordinate of the background continuesly. For example:

float scrollFactor = 0.5f; //Moves the background 0.5 pixels up per call of the draw method

public void draw(Graphics window) {

    window.drawImage(image, getX(), getY(), image.getWidth(), image.getHeight(), null);

    this.y += scrollFactor;
}
flotothemoon
  • 1,882
  • 2
  • 20
  • 37