Results 1 to 10 of 10
  1. #1
    Join Date
    Nov 2012
    Posts
    29
    Rep Power
    0

    Question What am I doing wrong in moving this sprite?

    Ok, so I was testing how much I know about java, by making a simple move around. So I was going to make just a 'proof of concept' by making it move diagonally so that I could see how well it worked, but It won't work!
    DETAILS:
    The variables dx and dy are supposed to be the increments of the x and y axis of the repaints(); of the oval. In the move() method, I want it to execute that, and repaint! What Do I DO!

    Java Code:
    	package Game;
    
    	import java.awt.Graphics;
    	import java.awt.event.ActionEvent;
    	import java.awt.event.ActionListener;
    	import java.awt.event.KeyEvent;
    	import java.awt.event.KeyListener;
    	import javax.swing.JPanel;
    	import javax.swing.Timer;
    	import javax.swing.event.*;
    
    	public class Panel extends JPanel implements KeyListener, ActionListener{
    		public int x;
    		public int y;
    		public int dx;
    		public int dy;
    	
    		public Panel() {
    			x = 0;
    			y = 0;
    			dx = 0;
    			dy = 0;
    		}
    	
    		public void move() {
    			x += dx;
    			y += dy;
    		}
    	
    		public void paintComponent(Graphics g) {
    			g.fillOval(x, y, 50, 50);
    		}
    
    		@Override
    		public void keyPressed(KeyEvent e) {
    			if(e.getKeyCode() == KeyEvent.VK_DOWN){
    				dx = +1;
    				dy = +1;
    				move();
    			}
    		}
    
    		@Override
    		public void keyReleased(KeyEvent e) {
    
    		}
    
    		@Override
    		public void keyTyped(KeyEvent e) {
    		
    		}
    	
    		public void actionPerformed(ActionEvent e) {
            	move();
            	repaint();  
        	}
    	
    	}
    Last edited by Wesley.laferriere; 11-04-2012 at 04:18 AM.

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

    Default Re: What am I doing wrong in moving this sprite?

    Moved from Advanced Java. Nothing advanced about the question either.

    I don't see where you add any listener to any component. Also, naming your custom classes after an existing JDK class is almost always a bad idea.

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

  3. #3
    Join Date
    Nov 2012
    Posts
    29
    Rep Power
    0

    Default Re: What am I doing wrong in moving this sprite?

    Umm... Look at the implementations of the class:
    Java Code:
    public class Panel extends JPanel implements KeyListener, ActionListener

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

    Default Re: What am I doing wrong in moving this sprite?

    Quote Originally Posted by Wesley.laferriere View Post
    Umm... Look at the implementations of the class:
    Java Code:
    public class Panel extends JPanel implements KeyListener, ActionListener
    "Umm... "? sorry, but all this statement does is make you look a bit foolish.

    Yes your class implements the ActionListener and KeyListener interfaces, but you still must add an ActionListener and a KeyListener to whatever component needs them. I don't see anywhere in your code above something along the lines of
    Java Code:
    myButton.addActionListener(this);
    
    // or
    
    myComponent.addKeyListener(this);
    Without doing this, your class can implement whatever interface it likes, but if you don't use it in a meaningful way, it won't help you. Please do yourself a favor and read the ActionListener tutorial.

  5. #5
    Join Date
    Nov 2012
    Posts
    29
    Rep Power
    0

    Default Re: What am I doing wrong in moving this sprite?

    modified code maybe?

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

    Default Re: What am I doing wrong in moving this sprite?

    Quote Originally Posted by Wesley.laferriere View Post
    modified code maybe?
    Yes, I suggest that you modify your code. Have you tried this, and if so, what was the result of your attempt?

  7. #7
    Join Date
    Nov 2012
    Posts
    29
    Rep Power
    0

    Default Re: What am I doing wrong in moving this sprite?

    Stupid question removed
    Last edited by Wesley.laferriere; 11-06-2012 at 09:11 PM.

  8. #8
    Join Date
    Nov 2012
    Posts
    29
    Rep Power
    0

    Default Re: What am I doing wrong in moving this sprite?

    Stupid question removed
    Last edited by Wesley.laferriere; 11-06-2012 at 09:13 PM.

  9. #9
    Join Date
    Nov 2012
    Posts
    29
    Rep Power
    0

    Default Re: What am I doing wrong in moving this sprite?

    This is the modified code, but nothing is happening. Of course, this is only so that I could test the whole Idea of KeyListeners, so I got rid of the drawing parts.

    Java Code:
    package Game;
    
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    
    import javax.swing.JPanel;
    
    	public class Panel extends JPanel implements KeyListener{
    		
    		public Panel() {
    			System.out.println("Initialised");
    			this.addKeyListener(this);
    		}
    		
    		@Override
    		public void keyPressed(KeyEvent e) {
    			
    		}
    
    		@Override
    		public void keyReleased(KeyEvent e) {
    			if(e.getKeyCode() == KeyEvent.VK_UP) {
    				System.out.println("UP Was Pressed");
    			}
    			
    			if(e.getKeyCode() == KeyEvent.VK_DOWN) {
    				System.out.println("DOWN Was Pressed");
    			}
    
    			
    			if(e.getKeyCode() == KeyEvent.VK_LEFT) {
    				System.out.println("LEFT Was Pressed");
    			}
    
    			
    			if(e.getKeyCode() == KeyEvent.VK_RIGHT) {
    				System.out.println("RIGHT Was Pressed");
    			}
    
    		}
    
    		@Override
    		public void keyTyped(KeyEvent e) {
    			
    		}
    }
    Notice that this time in the constructor I made an addKeyListener() stat

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

    Default Re: What am I doing wrong in moving this sprite?

    This is one reason not to use KeyListeners since they only work when the component being listened to has the focus. You can solve this by kludge by calling focusable(true) and and then requestFocusInWindow() on the JPanel, but then if you want the KeyListener to continue to work, you must prevent other components from getting focus -- not good. A better solution is to use Key Bindings. They may seem more daunting at first, but once you get used to them, they're pretty easy to use. There's a decent tutorial that will help you learn how to use them. Check the first hit on Google for it.

Similar Threads

  1. Flickering on a 2D Sprite
    By driiper in forum Java 2D
    Replies: 5
    Last Post: 05-06-2012, 05:30 PM
  2. Java Sprite animation
    By mrhid6 in forum Java Gaming
    Replies: 5
    Last Post: 10-14-2011, 03:26 AM
  3. import sprite
    By Bimz in forum New To Java
    Replies: 0
    Last Post: 08-28-2011, 12:15 PM
  4. Confisuion about sprite.getX() and sprite.getY()
    By Basit781 in forum Java Gaming
    Replies: 0
    Last Post: 01-10-2011, 06:16 AM
  5. Sprite Movement
    By Curtiz in forum Java Gaming
    Replies: 1
    Last Post: 04-26-2010, 01:31 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
  •