Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6677349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:06:30+00:00 2026-05-26T04:06:30+00:00

In my side-scroller, I want to have 3 backgrounds that keep looping. Whenever you

  • 0

In my side-scroller, I want to have 3 backgrounds that keep looping. Whenever you get through a stage it calls the function nextStage() that sends you to the next background. In the class:

package com.erikbalen.game.rpg;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.*;
public class World extends JPanel implements ActionListener{

/**
 * 
 */
private static final long serialVersionUID = 2834816426699432121L;
Player p1;
Image background;
Timer time;

public World() {
    p1 = new Dps();
    addKeyListener(new AL());
    setFocusable(true);
    ImageIcon icon = new ImageIcon("C:\\Program Files (x86)\\EriksRPG\\Images\\Backgrounds\\background.png");
    background = icon.getImage();
    time = new Timer(5, this);
    time.start();
}

public void actionPerformed(ActionEvent e) {
    p1.move();
    repaint();
}

public void paint(Graphics g) {
    super.paint(g);
    Graphics g2d = (Graphics2D) g;

    g2d.drawImage(background, 0, 0, null);
    g2d.drawImage(p1.getImage(), p1.getX(), p1.getY(), null);

}

private class AL extends KeyAdapter {
    public void keyReleased(KeyEvent e) {
        p1.keyReleased(e);
    }

    public void keyPressed(KeyEvent e) {
        p1.keyPressed(e);
    }       
    }       
}

Basically I want to know how I can make an array of images called backgrounds, load those three files, and make a method called nextStage() that loads background[stage] and if stage > 2 stage = 0

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T04:06:31+00:00Added an answer on May 26, 2026 at 4:06 am

    one possible solution:

    make “background” an array of 3 elements

    Image[] background = new Image[3];
    

    load the three background images one at a time into background[0], background[1] and background[2].

    create a new private variable, perhaps called stage, and increment when advancing:

    private int stage = 0;
    
    public void nextStage() { stage++; }
    

    finally, in paint(), draw the background you want, according to the value of stage:

    g2d.drawImage(background[stage % 3], 0, 0, null);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a header that has a css background image that I want to
Ok so I have developed a 2d side scroller (platformer) and it is pretty
I have a side scrolling navigation with scroll spy (that I grabbed from bootstrap)
I'm working on a side scroller that has a character that fires a bullet
Possible Duplicate: overflow-x:hidden one side only? I have a container that scrolls horizontally and
I have two wrapper divs side by side. I want to line up divs
I have a JSplitPane. i want to add background image to the left side
I'm making a 2d side scroller and for the life of me I can't
I am doing server-side javascript and i need to have a typed array of
While developer a client side application I ran into an error that I believe

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.