Results 1 to 6 of 6
  1. #1
    heysayjump is offline Member
    Join Date
    Mar 2011
    Posts
    7
    Rep Power
    0

    Default GUI JAVA Programming help

    I'm new to Java and I'm currently learning the basics of GUI for our project.
    I made this code through a tutorial and I think I followed it well.
    The program is supposed to move the image to the left or right depending on the pressed key and stops when the key is released.
    The problem is I can't move my object to the left when I press the left key and it only moves to the right.
    Can someone check it for me? thanks

    main

    Java Code:
    package mpGame;
    
    import javax.swing.JFrame;
    
    import mpGame.Board;
    
    public class Frame {
    	
    	public static void main(String[] args){
    		
    		JFrame frame = new JFrame("2D Game");
    		frame.add(new Board());
    		frame.setVisible(true);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setSize(1024,768);
    		frame.setLocationRelativeTo(null);
    	}
    	
    
    }
    I can't find my mistake in this part :(

    Java Code:
    package mpGame;
    
    
    import java.awt.Image;
    import java.awt.event.*;
    
    import javax.swing.ImageIcon;
    
    public class Body{
    int x, dx, y;
    Image august;
    
    	public Body(){
    		ImageIcon i = new ImageIcon("C:/august.png");
    		august = i.getImage();
    		x = 10;
    		y = 172;
    	}
    	
    	public void move(){
    		
    		x = x + dx;	
    			
    	}
    	
    	public int getX(){
    		return x;	
    	}
    	
    	public int getY(){
    		return y;	
    	}
    	
    	public Image getImage(){
    		return august;
    	}
    	
    	public void keyReleased(KeyEvent e){
    		int key = e.getKeyCode();
    		
    		if(key == KeyEvent.VK_LEFT);{
    		dx = 0;
    		}
    		if(key == KeyEvent.VK_RIGHT);{
    			dx = 0;
    		}
    	}
    	public void keyPressed(KeyEvent e){
    		int key = e.getKeyCode();
    		
    		if(key == KeyEvent.VK_LEFT);
    		dx = -1;
    		
    		if(key == KeyEvent.VK_RIGHT);
    			dx = 1;
    		
    	}
    	
    }
    Java Code:
    package mpGame;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.event.ActionListener;
    
    import javax.swing.*;
    
    public class Board extends JPanel implements ActionListener{
    Body p;
    Image img;
    Timer time; 	 	
    
    	public Board(){
    		p = new Body();
    		addKeyListener(new AL());
    		setFocusable(true);
    		ImageIcon i = new ImageIcon("C:/stage1.png");
    		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;
    			
    			g2d.drawImage(img, 0, 0, null);
    			g2d.drawImage(p.getImage(), p.getX(), p.getY(), null);
    	}
    	
    	private class AL extends KeyAdapter{
    		public void keyReleased(KeyEvent e){
    			p.keyReleased(e);
    		}
    		
    		public void keyPressed(KeyEvent e){
    			p.keyPressed(e);
    		}
    	}
    }

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    What happens when you press the left key? Does it throw an exception, or simply do nothing. Try using print statements to print how x changes when you press the left key.

  3. #3
    heysayjump is offline Member
    Join Date
    Mar 2011
    Posts
    7
    Rep Power
    0

    Default

    I already tried it. I printed x after this statement
    "if(key == KeyEvent.VK_LEFT);
    dx = -1;
    system.out..."
    The time that I pressed the left key, the output is this..
    original x = 10;

    left key pressed x = 59

    I think the negative sign cannot read. I don't know what exactly is the problem.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Surround ALL blocks including if blocks in curly braces. Do this and you'll see your way-ward semicolons which are screwing up your code.

    In other words, do this:

    Java Code:
          if (key == KeyEvent.VK_LEFT) {
             dx = -1;
          }
    
          if (key == KeyEvent.VK_RIGHT) {
             dx = 1;
          }


    not this:

    Java Code:
          if (key == KeyEvent.VK_LEFT)
             ;
          dx = -1;
    
          if (key == KeyEvent.VK_RIGHT)
             ;
          dx = 1;

  5. #5
    heysayjump is offline Member
    Join Date
    Mar 2011
    Posts
    7
    Rep Power
    0

    Default

    It worked thanks for the help :)

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You're welcome. Check your code for similar errors elsewhere. You've got some semicolons where they don't belong.

Similar Threads

  1. Replies: 21
    Last Post: 06-01-2009, 12:52 PM
  2. Replies: 1
    Last Post: 05-31-2009, 10:57 PM
  3. Replies: 1
    Last Post: 05-31-2009, 06:48 PM
  4. Replies: 1
    Last Post: 05-31-2009, 02:17 PM
  5. Replies: 16
    Last Post: 05-28-2009, 03:41 PM

Tags for this Thread

Posting Permissions

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