Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2011
    Posts
    6
    Rep Power
    0

    Default Having a problem with a scroll game background image

    Hey everybody, I am rather new to Java, I do have some experience with VB and a small small amount of c++. I am trying to create a sidescroll game in 2D, I am having some issues that I am trying to play around with and I have been able to solve some but I would at least like some outside input on what I am writing. I have three classes of information that I will post up, my problem is the transition between screens, now i know that the background does not go together all that well and I am fine with that as this is not something that is for anything aside from learning the basics and I am not sure if I will have to post up the two jpgs that I am using.

    first class

    Java Code:
    package OurGame;
    
    import javax.swing.JFrame;
    
    public class Frame {
    
    	public static void main(String[] args)
    	{
    		JFrame frame = new JFrame("2D Game");
    		frame.add(new Board());
    		frame.setTitle(("2-D Test Game"));
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setSize(1200, 365);
    		frame.setVisible(true);
    	}
    }
    
    
    second class
    
    
    package OurGame;
    
    import java.awt.Image;
    import java.awt.event.KeyEvent;
    
    import javax.swing.ImageIcon;
    
    public class Dude {
    
    int x, dx, y, dy, nx2, nx;
    Image still;
    
    public Dude(){
    	ImageIcon i = new ImageIcon("C:/stickfigure.jpg");
    	still = i.getImage();
    	x = 75;
    	nx2 = 1150;
    	nx = 0;
    	y = 172;
    }
    public void move(){
    	x = x + dx;
    	nx2 = nx2 + dx;
    	nx = nx + dx;
    	y = y +dy;
    }
    public int getX(){
    	return x;
    }
    public int getY(){
    	return y;
    }
    public Image getImage (){
    	return still;
    }
    public void keyPressed(KeyEvent e){
    	int key = e.getKeyCode();
    	
    	if (key == KeyEvent.VK_LEFT)
    	dx = -1;
    	
    	if (key == KeyEvent.VK_RIGHT)
    	dx = +1;
    	
    	if (key == KeyEvent.VK_UP) dy = -1;
    	
    	if (key == KeyEvent.VK_DOWN) dy = +1;
    	
    	
    }
    
    public void keyReleased(KeyEvent e){
    	int key = e.getKeyCode();
    	
    	if (key == KeyEvent.VK_LEFT);
    	dx = 0;
    	
    	if (key == KeyEvent.VK_RIGHT);
    	dx = 0;
    	
    	if (key == KeyEvent.VK_UP) dy = 0;
    	
    	if (key == KeyEvent.VK_DOWN) dy = 0;
    	
    }
    
    
    
    
    
    
    }
    
    
    third class
    
    
    package OurGame;
    
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Image;
    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 Board extends JPanel implements ActionListener{
    
    Dude p;
    Image img;
    Timer time;
    
    	public Board(){
    		p = new Dude();
    		addKeyListener(new AL());
    		setFocusable(true);
    		ImageIcon i = new ImageIcon("C:/BoardDemo.jpg");
    		img = i.getImage();
    		time = new Timer(5, this);
    		time.start();
    		
    	}
    	
    	public void actionPerformed(ActionEvent e){
    		p.move();
    		repaint();
    	}
    	
    	public void paint(Graphics g){
    		super.paint(g);
    			Graphics2D g2d = (Graphics2D) g;
    	if (p.getX() == 1250)
    		p.nx2 = 0;
    	if (p.getX() == 2275)
    		p.nx = 0;
    	
    			
    			g2d.drawImage(img, 1100-p.nx2, 0, null);
    		if (p.getX() >= 1100);
    			g2d.drawImage(img, 1100-p.nx, 0, null);
    
    			g2d.drawImage(p.getImage(), 75, p.getY(), null);
    	}
    	
    	private class AL extends KeyAdapter{
    		public void keyReleased(KeyEvent e){
    			p.keyReleased(e);
    	}
    		public void keyPressed(KeyEvent e){
    			p.keyPressed(e);
    		}
    	}	
    	
    	
    	
    }
    thanks everybody
    Last edited by Norm; 09-16-2011 at 05:44 PM. Reason: added code tags

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

    Default Re: Having a problem with a scroll game background image

    I get this error on several lines in your code when I compile it:
    warning: [empty] empty statement after if
    What compiler are you using? Here is the command line I use to compile with:
    D:\Java\jdk1.6.0_25\bin\javac.exe -Xlint -g -deprecation

  3. #3
    Join Date
    Jan 2011
    Posts
    6
    Rep Power
    0

    Default Re: Having a problem with a scroll game background image

    I am using Eclipse, I have not had any issues with errors, I just fixed my personal problem with it, I had the screen set to display as big as the background was so it was disappearing a bit too early, so I changed the frame to be 1000 wide instead of 1200, although if anybody sees anything that I could be doing more efficiently I would definitely listen.

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

    Default Re: Having a problem with a scroll game background image

    Can you explain what your problem is?
    I can move the small image up and down
    and the background left and right.

Similar Threads

  1. use image for background
    By tony_stark in forum AWT / Swing
    Replies: 1
    Last Post: 05-31-2011, 08:20 PM
  2. Problem with zooming background image
    By LianaN in forum AWT / Swing
    Replies: 0
    Last Post: 01-28-2011, 09:50 PM
  3. Replies: 6
    Last Post: 07-21-2010, 06:15 AM
  4. problem with scroll bar
    By r.gnanaprakasam in forum AWT / Swing
    Replies: 0
    Last Post: 04-22-2009, 01:24 PM
  5. Background image
    By leiferouis in forum New To Java
    Replies: 9
    Last Post: 03-08-2009, 05:49 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
  •