Q: How to make a scrolling effect for a background image I need to make a scrolling background for this platformer. It needs to scroll a 2400x500 image while the frame size is about 1440x900. If it could just gradually change over time that would be great. This is my code:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;  
import javax.swing.*;

public class Main extends JFrame {
    public Main() {

        //Creates Title Image 
        JLabel title = new JLabel(" ");
        ImageIcon tl = new ImageIcon("title.gif");
        title.setIcon(tl);

        //Creates Start Image
        final JButton start = new JButton("");
        ImageIcon st = new ImageIcon("start.gif");
        start.setIcon(st);

        //Creates Options Image
        JButton options = new JButton("");
        ImageIcon opt = new ImageIcon("options.gif");
        options.setIcon(opt);
        options.setBackground(Color.BLACK);

        //Creates label for level 0 background image
        JLabel background = new JLabel(" ");
        ImageIcon back = new ImageIcon("level0.gif");
        background.setIcon(back);

        //Creates a panel for level 0
        final JPanel p5 = new JPanel();
        p5.setLayout (new BorderLayout(1, 1));
        p5.add(background);

        //Create first frame for "Start" button
        final JPanel p1 = new JPanel();
        p1.setLayout(new GridLayout(1, 1));
        p1.add(start, BorderLayout.CENTER);

        //Create second panel for title label
        final JPanel p2 = new JPanel(new BorderLayout());
        p2.setLayout(new GridLayout(1, 3));
        p2.add(title, BorderLayout.WEST);

        //Create third panel for "Options" button
        final JPanel p3 = new JPanel(new BorderLayout());
        p3.setLayout(new GridLayout(1, 1));
        p3.add(options, BorderLayout.SOUTH);

        //Creates fourth panel to organize all other primary
        final JPanel p4 = new JPanel(new BorderLayout());
        p4.setLayout(new GridLayout(1, 3));
        p4.add(p1, BorderLayout.WEST);
        p4.add(p2, BorderLayout.CENTER);
        p4.add(p3, BorderLayout.EAST);

        //When button is clicked, it changes the level
        start.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(start.isEnabled()) {
                    remove(p4);
                    add(p5, BorderLayout.CENTER);
                    invalidate();
                    validate();
                }
                else {
                    return;
                }
            }
        });

        //Adds fourth panel to frame
        add(p4, BorderLayout.CENTER);
    }

    public static void main(String args[]) {
        Main frame = new Main();

        //Finds screen size of monitor
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

        //Creates the frame
        frame.setTitle("Cockadoodle Duty: Awakening");
        frame.setSize(screenSize);
        frame.setLocale(null); 
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        String background = "#000000";
        frame.setBackground(Color.decode(background));
    }
}


A: *

*Add the label containing the icon to a scroll panel

*Use a Swing Timer to schedule the scrolling

*When the Timer fires you can scroll background.


The scrolling code might be something like:
JViewport viewport = scrollPane.getViewport();
Point position = viewport.getViewPosition();
position.x += 2; 
viewport.setViiewPosition( position );

