Results 1 to 4 of 4
  1. #1
    chrishans is offline Member
    Join Date
    Oct 2011
    Posts
    9
    Rep Power
    0

    Default Need help moving a shape!

    Hi, I'm a beginner and recently started to see some of JPanel and graphics. I want to make a small square of 10x10 px. move around the screen by pressing the arrows, one pixel for every time I press the arrow. But I can't! Instead of adding or subtracting one pixel in x or y, it adds or subtracts z pixels (where z is the number of times I pressed the vertical or horizontal arrows).
    For example, the first time I press RIGHT, it moves one pixel to the right, the second moves 2 px from the last position. If you then press LEFT moves 3 px to the left. And so on ... Got it?

    Here's the code to see if you can help ...

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class Paint extends JPanel implements KeyListener {
    	int x = 0;
    	int y = 0;
    
    	public Paint() {
    		setSize(400, 400);
    
    	}
    
    	public boolean isFocusable() {
    		return true;
    	}
    
    	public void paintComponent(Graphics g) {
    		super.paintComponent(g);
    		addKeyListener(this);
    		this.requestFocus();
    		System.out.println(x+","+y);
    		dibujar(g);
    
    	}
    
    	public void dibujar(Graphics g) {
    		g.setColor(Color.black);
    		g.fillRect(x, y, 10, 10);
    
    	}
    
    	public void keyPressed(KeyEvent e) {
    
    	}
    
    	public void keyReleased(KeyEvent e) {
    		if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
    
    			x++;
    		} else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
    
    			x--;
    
    		}
    		if (e.getKeyCode() == KeyEvent.VK_UP) {
    
    			y--;
    
    		} else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
    
    			y++;
    
    		}
    		this.repaint();
    
    	}
    
    	public void keyTyped(KeyEvent arg0) {
    		// TODO Auto-generated method stub
    
    	}
    }
    In addition to this class "Paint", there is a "Windows" (for JFrame), which creates an object Paint, and the class "Main", which creates the Windows object.

    Thanks!

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

    Default Re: Need help moving a shape!

    Don't add a key listener or do any such logic in your paintComponent method since every time the GUI repaints, you add another listener and they all are calling their code at the same time, causing x to increment many-fold as the program progresses. Not only that, but this method is for painting only, not for program logic.

    Instead, why not add the key listener once in the class's constructor. Even better would be to use key bindings rather than a key listener.
    Last edited by Fubarable; 10-16-2011 at 04:13 AM.

  3. #3
    chrishans is offline Member
    Join Date
    Oct 2011
    Posts
    9
    Rep Power
    0

    Default Re: Need help moving a shape!

    Thank you, Fubarable! I just moved "addKeyListener(this)" to the Paint method, and it works fine.
    Thanks!

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

    Default Re: Need help moving a shape!

    Quote Originally Posted by chrishans View Post
    Thank you, Fubarable! I just moved "addKeyListener(this)" to the Paint method, and it works fine.
    Thanks!
    You're welcome, but sorry to nitpick -- it's "the Paint constructor". That's a big difference.

Similar Threads

  1. Replies: 1
    Last Post: 06-08-2011, 10:22 PM
  2. create own shape
    By kepep in forum Java 2D
    Replies: 2
    Last Post: 12-15-2010, 07:43 PM
  3. Make shape rounding (moving in circle)
    By mneskovic in forum New To Java
    Replies: 8
    Last Post: 08-17-2010, 03:05 PM
  4. Replies: 1
    Last Post: 04-02-2010, 12:48 PM
  5. rezise shape
    By frankenstein in forum Java 2D
    Replies: 5
    Last Post: 07-30-2009, 12:44 AM

Posting Permissions

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