Results 1 to 4 of 4
Thread: Need help moving a shape!
- 10-16-2011, 03:22 AM #1
Member
- Join Date
- Oct 2011
- Posts
- 9
- Rep Power
- 0
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 ...
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.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 } }
Thanks!
-
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.
- 10-16-2011, 04:25 AM #3
Member
- Join Date
- Oct 2011
- Posts
- 9
- Rep Power
- 0
Re: Need help moving a shape!
Thank you, Fubarable! I just moved "addKeyListener(this)" to the Paint method, and it works fine.
Thanks!
-
Re: Need help moving a shape!
Similar Threads
-
Help with creating a shape and moving it with mouse and keyboard!
By konhee93 in forum New To JavaReplies: 1Last Post: 06-08-2011, 10:22 PM -
create own shape
By kepep in forum Java 2DReplies: 2Last Post: 12-15-2010, 07:43 PM -
Make shape rounding (moving in circle)
By mneskovic in forum New To JavaReplies: 8Last Post: 08-17-2010, 03:05 PM -
how to change the shape of the JFrame to a oval shape
By kiki2009 in forum Java 2DReplies: 1Last Post: 04-02-2010, 12:48 PM -
rezise shape
By frankenstein in forum Java 2DReplies: 5Last Post: 07-30-2009, 12:44 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks