Results 1 to 8 of 8
Thread: Whats wrong with this?
- 02-27-2012, 09:23 AM #1
Member
- Join Date
- Feb 2012
- Posts
- 39
- Rep Power
- 0
Whats wrong with this?
Well no movement... but the keylistener is set up as far as i know.
Java Code:package game; import java.awt.*; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import javax.swing.*; public class BallRacer extends JFrame implements KeyListener{ static int PlayerX; static int PlayerY; public BallRacer(String title) { super(title); } @Override public void keyTyped(KeyEvent e) {} @Override public void keyPressed(KeyEvent e) { if(e.getKeyCode() == KeyEvent.VK_DOWN){ PlayerY--; } if(e.getKeyChar() == KeyEvent.VK_RIGHT){ PlayerX++; } if(e.getKeyChar() == KeyEvent.VK_DOWN){ PlayerY++; } if(e.getKeyChar() == KeyEvent.VK_LEFT){ PlayerX--; } if(e.getKeyChar() == KeyEvent.VK_ESCAPE){ System.exit(0); } } @Override public void keyReleased(KeyEvent e) {} @Override public void paint(Graphics g) { super.paint(g); g.setColor(Color.blue); g.fillOval(PlayerX, PlayerY, 25, 25); g.setColor(Color.black); g.fill3DRect(0, 0, 800, 50, rootPaneCheckingEnabled); g.fill3DRect(0, 575, 800, 50, rootPaneCheckingEnabled); g.fill3DRect(0, 0, 50, 800, rootPaneCheckingEnabled); g.fill3DRect(750, 0, 50, 800, rootPaneCheckingEnabled); } private static void createAndShowGui() { JFrame MainFrame = new BallRacer("BallRacer v1"); MainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); MainFrame.pack(); MainFrame.setVisible(true); MainFrame.setResizable(false); MainFrame.setSize(800,600); } public static void main(String[] args) { while (PlayerY < 50) { PlayerY++; } while (PlayerY > 575) { PlayerY--; } while (PlayerX < 50) { PlayerX++; } while (PlayerX > 750) { PlayerX--; } SwingUtilities.invokeLater(new Runnable() { @Override public void run() { createAndShowGui(); } } ); } }
- 02-27-2012, 11:14 AM #2
Re: Whats wrong with this?
It has a very poor subject line.Whats wrong with this?
I don't see any code there that adds a KeyListener to a Component.
Also, I fail to see the point of the loops in the main(...) method that increment the badly named variables.
Moving to AWT/Swing
dbWhy do they call it rush hour when nothing moves? - Robin Williams
-
Re: Whats wrong with this?
- 02-27-2012, 09:25 PM #4
Member
- Join Date
- Feb 2012
- Posts
- 39
- Rep Power
- 0
Re: Whats wrong with this?
Could it be the static integers if it was, the while playerx and playery wouldn't work because for some reason it needs to be static any suggestions?
-
Re: Whats wrong with this?
The variables shouldn't be static, but Darryl already told you one problem -- where do you add a KeyListener to anything? If you don't add a listener to a component then nothing gets listened to. I also recommend against using KeyListeners with Swing apps but instead using Key Bindings (check the tutorial on this).
- 04-03-2012, 04:30 AM #6
Member
- Join Date
- Mar 2012
- Posts
- 8
- Rep Power
- 0
Re: Whats wrong with this?
Add the keylistener with 'mainFrame.addKeyListener(this);' and make sure you set 'mainFrame.setFocusable(true);'
-
Re: Whats wrong with this?
- 04-03-2012, 05:49 AM #8
Member
- Join Date
- Mar 2012
- Posts
- 8
- Rep Power
- 0
Similar Threads
-
whats wrong with this code?
By Petee in forum New To JavaReplies: 8Last Post: 09-30-2011, 04:39 PM -
Whats wrong with my code
By zit1343 in forum New To JavaReplies: 9Last Post: 01-24-2011, 01:06 AM -
whats wrong
By atenv in forum New To JavaReplies: 6Last Post: 06-15-2010, 01:55 PM -
whats is wrong with this app??
By mrajan in forum New To JavaReplies: 4Last Post: 06-09-2010, 10:56 PM -
Database help... whats wrong?
By neosnokia in forum JDBCReplies: 4Last Post: 06-09-2009, 11:17 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks