Results 1 to 6 of 6
  1. #1
    mwr1976 is offline Senior Member
    Join Date
    Oct 2011
    Posts
    106
    Rep Power
    0

    Default 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!
    Java 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) 
    	{
    		
    		
    	}
    }
    Java 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();
    	}
    	
    
    
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default 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
    that is in response to a key pressed event
    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default 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.
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    mwr1976 is offline Senior Member
    Join Date
    Oct 2011
    Posts
    106
    Rep Power
    0

    Default 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:
    Java 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

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default 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.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Constant scrolling background

    Quote Originally Posted by Norm View Post
    5 ms is pretty short.
    Very short. Consider this: most videos run at between 24 and 30 fps: approx. 42 to 33 ms.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Cannot use as static constant
    By KiranA in forum Eclipse
    Replies: 1
    Last Post: 12-01-2011, 09:19 AM
  2. Constant Variables
    By harshakantha in forum New To Java
    Replies: 3
    Last Post: 06-13-2011, 07:51 AM
  3. switch() don't work with constant
    By jarek in forum Advanced Java
    Replies: 3
    Last Post: 11-19-2010, 08:59 AM
  4. static constant and performance
    By felixtfelix in forum New To Java
    Replies: 2
    Last Post: 08-31-2010, 01:43 PM
  5. Constant size for JTextArea
    By itaipee in forum AWT / Swing
    Replies: 1
    Last Post: 12-30-2008, 04:12 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •