Constant scrolling background
This is my first attempt at a game. I have a background image and an heavily transparent stars image. I made both images the same size as my Panel. I am thinking I may need 2 stars images to pull off what I am trying to do. I want the stars images to scroll down (y++) constantly, giving the slight affect of forward movement. I am assuming it would probably be best if this was in its own thread? Here's my code....maybe someone could point me in the right direction. I was unable to find anything in the documentation or tutorials on how to accomplish this. There is a lot of examples of scroll the background while a sprite stays constant but that is in response to a key pressed event, I want this scroll to start when the game loads and continuously excute without stopping.
Thanks!
Code:
package spaceGame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
public class GameBoard extends JPanel implements ActionListener
{
private Image background;
private Image stars;
private Timer time;
private int aniX, aniY;
public GameBoard()
{
ImageIcon backgroundIcon = new ImageIcon("src/images/space_background.png");
ImageIcon starsIcon = new ImageIcon("src/images/stars.png");
stars = starsIcon.getImage();
background = backgroundIcon.getImage();
time = new Timer(5, this);
}
public void paint(Graphics g)
{
super.paint(g);
Graphics2D gameGraphics = (Graphics2D)g;
gameGraphics.drawImage(background, 0, 0, null);
gameGraphics.drawImage(stars, 0, 0, null);
}
@Override
public void actionPerformed(ActionEvent arg0)
{
}
}
Code:
package spaceGame;
import java.awt.Color;
import javax.swing.JFrame;
public class GameWindow extends JFrame
{
private static final int WIDTH = 1200;
private static final int HEIGHT = 1000;
public GameWindow()
{
add(new GameBoard());
setSize(WIDTH, HEIGHT);
setTitle("Space Shooter 2D");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args)
{
GameWindow game = new GameWindow();
}
}
Re: Constant scrolling background
You need to use a javax.swing.Timer. The code in its ActionListener#actionPerformed will be much the same as the code you have seen Quote:
that is in response to a key pressed event
db
Re: Constant scrolling background
Recommendation: override the paintComponent() method instead of paint() for Swing components.
Have a Timer that changes the y location of where the moving image will be drawn next. Not sure about how to wrap the image to have continuous coverage. The drawImage() method has some overrides that could allow you to draw different parts of the image at different locations.
Re: Constant scrolling background
I have a pretty good idea, but i cannot find a timer end event. In Silverlight using c# there is a timer.completed event.What I am thinking is every time the timer expires I redraw the image so many pixels lower.
so if I do:
Code:
Timer time = new Timer(5, this);
time.start();
I was thinking about increasing by 3-4 pixels every 5 ms, but I cannot find a way to trigger it when the timer expires.
thanks
Re: Constant scrolling background
That sounds like a reasonable way to do it. You need to look at the API doc for the Timer class.
5 ms is pretty short. I think 15 may be better.
Re: Constant scrolling background
Quote:
Originally Posted by
Norm
5 ms is pretty short.
Very short. Consider this: most videos run at between 24 and 30 fps: approx. 42 to 33 ms.
db