Results 1 to 6 of 6
Thread: Moving a ball with Arrow Keys
- 02-25-2011, 06:49 PM #1
Member
- Join Date
- Feb 2011
- Posts
- 4
- Rep Power
- 0
Moving a ball with Arrow Keys
Ok guys. I'm making a game. For now I made a program in which you can move ball with arrow keys. UP, DOWN, RIGHT and LEFT.
My first question is:
I clicked one button... Ball moves... but not instantly!! I just hate this. Like it's somehow delayed or something. At key click it moves only for 5 sources than it moves normally. But I want that ball moves constantly... Try it out with my code. How to make ball moving nice and without dalaying?
My second question is:
I can move left and right, up and down. :D how to move up and left at the same time? when I click Up and Left keys I want that ball moves up and left. not just left or just up. how to do that?
here is the code:
Java Code:import java.awt.Canvas; import java.awt.Dimension; import java.awt.Graphics2D; import java.awt.image.BufferStrategy; import java.awt.event.*; import javax.swing.*; public class HandlingEvents implements Runnable { JFrame frame; int myX = 400; int myY = 400; Canvas canvas; BufferStrategy bufferStrategy; boolean running = true; public HandlingEvents() { frame = new JFrame("Basic Game"); JPanel panel = (JPanel) frame.getContentPane(); panel.setPreferredSize(new Dimension(500, 500)); panel.setLayout(null); canvas = new Canvas(); canvas.setBounds(0, 0, 500, 500); canvas.setIgnoreRepaint(true); panel.add(canvas); canvas.addKeyListener(new KeyAdapter() { @Override public void keyPressed(KeyEvent evt) { moveIt(evt); } }); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setResizable(false); frame.setVisible(true); canvas.createBufferStrategy(2); bufferStrategy = canvas.getBufferStrategy(); canvas.requestFocus(); } public void run() { while (running = true) { Paint(); try { Thread.sleep(25); } catch (InterruptedException e) { } } } public static void main(String[] args) { HandlingEvents ex = new HandlingEvents(); new Thread(ex).start(); } public void Paint() { Graphics2D g = (Graphics2D) bufferStrategy.getDrawGraphics(); g.clearRect(0, 0, 500, 500); Paint(g); bufferStrategy.show(); } protected void Paint(Graphics2D g) { g.fillOval(myX, myY, 30, 30); } public void moveIt(KeyEvent evt) { switch (evt.getKeyCode()) { case KeyEvent.VK_DOWN: myY += 5; break; case KeyEvent.VK_UP: myY -= 5; break; case KeyEvent.VK_LEFT: myX -= 5; break; case KeyEvent.VK_RIGHT: myX += 5; break; } } }
- 02-25-2011, 08:30 PM #2
Senior Member
- Join Date
- Feb 2011
- Posts
- 118
- Rep Power
- 0
I just compiled and ran your code as-is, and I see no delay in moving the ball after I've clicked. I also see it moving diagonally when I press two adjacent arrow keys at once.
- 02-25-2011, 08:44 PM #3
Member
- Join Date
- Feb 2011
- Posts
- 4
- Rep Power
- 0
- 02-25-2011, 08:45 PM #4
Senior Member
- Join Date
- Feb 2011
- Posts
- 118
- Rep Power
- 0
While I'm at it, here are some tips:
1. Get rid of all that BufferStrategy nonsense. It works, but it's overkill.
2. Remove the "implements Runnable" and the new Thread crap in main().
3. Make your class extend Canvas.
3. Override your Canvas's paint() method to do the clearing and drawing that are in your miscapitalized Paint() methods.
4. Remove the call to setIgnoreRepaint().
5. You don't need a Graphics2D object; just use a java.awt.Graphics one.
6. At the end of your keyPressed() method, call repaint().
The result? Much, much clearer code. To wit:
Java Code:import java.awt.*; import java.awt.event.*; import javax.swing.JFrame; public class HandlingEvents extends Canvas { int myX = 400; int myY = 400; public HandlingEvents() { setSize(new Dimension(500, 500)); addKeyListener(new KeyAdapter() { @Override public void keyPressed(KeyEvent evt) { moveIt(evt); } }); } public void paint(Graphics g) { g.fillOval(myX, myY, 30, 30); } public void moveIt(KeyEvent evt) { switch (evt.getKeyCode()) { case KeyEvent.VK_DOWN: myY += 5; break; case KeyEvent.VK_UP: myY -= 5; break; case KeyEvent.VK_LEFT: myX -= 5; break; case KeyEvent.VK_RIGHT: myX += 5; break; } repaint(); } public static void main(String[] args) { JFrame frame = new JFrame("Basic Game"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); HandlingEvents ex = new HandlingEvents(); frame.getContentPane().add(ex); frame.pack(); frame.setResizable(false); frame.setVisible(true); ex.requestFocus(); } }
-
- 02-25-2011, 10:15 PM #6
Senior Member
- Join Date
- Feb 2011
- Posts
- 118
- Rep Power
- 0
Similar Threads
-
Help in bow and arrow game
By sahildave1991 in forum AWT / SwingReplies: 6Last Post: 10-18-2010, 02:34 PM -
Arrow Button Example
By Java Tip in forum SWTReplies: 0Last Post: 07-11-2008, 04:44 PM -
How to navigate a SWT table cells with arrow keys
By Java Tip in forum SWTReplies: 0Last Post: 07-11-2008, 03:07 PM -
Draw an arrow
By Albert in forum SWT / JFaceReplies: 3Last Post: 02-01-2008, 08:11 AM -
Problem deleting ball from bouncing ball app
By adlb1300 in forum New To JavaReplies: 2Last Post: 12-03-2007, 09:08 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks