Results 1 to 8 of 8
Thread: Problems with KeyListener
- 03-24-2009, 02:04 AM #1
Member
- Join Date
- Mar 2009
- Posts
- 6
- Rep Power
- 0
Problems with KeyListener
I have no idea where this would go. I'm not a complete newbie. I'm not expert either. I can do most types of projects (that don't involve networking) by looking up stuff. Now, I'm stuck. I'm trying to write a simple game for my own entertainment. A part of the game is to have a circle move around with the arrow keys. Here's my code:
But, all that shows up on the screen is a black background and nothing else. Please help!Java Code:import java.applet.*; import java.awt.*; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; public class AppletBasics extends Applet implements Runnable, KeyListener { int xVal=0; int yVal=540; boolean moveRight = false; boolean moveLeft = false; public void start () { // define a new thread Thread th = new Thread (this); // start this thread th.start (); } public void run () { // lower ThreadPriority Thread.currentThread().setPriority(Thread.MIN_PRIORITY); // run a long while (true) this means in our case "always" while (true) { // repaint the applet repaint(); try { //Stop thread for 20 milliseconds Thread.sleep (10); } catch (InterruptedException ex) { // do nothing } //set ThreadPriority to maximum value Thread.currentThread().setPriority(Thread.MAX_PRIORITY); } } public void init() { setBackground(Color.black); addKeyListener( this ); } public void paint (Graphics g, KeyEvent e) { setSize(600,600); g.setColor(Color.blue); if(moveLeft ==true & xVal>10) { g.fillOval(xVal--, yVal, 50, 50); } if(moveRight==true & xVal<840) { g.fillOval(xVal++, yVal, 50, 50); } } @Override public void keyPressed(KeyEvent e) { if(e.getKeyCode()== e.VK_LEFT) { moveLeft = true; } if(e.getKeyCode()== e.VK_RIGHT) { moveRight=true; } } @Override public void keyReleased(KeyEvent e) { // TODO Auto-generated method stub } @Override public void keyTyped(KeyEvent e) { } public static void main(String[] args) { } }
-
Mr. Poincare, please do not cross-post the same question on multiple fora. Imagine you are a volunteer spending free time to consider someone's problem, work out a code solution and post it, only to find out that the question had already been answered in a cross-post hours previously, and so that in effect you have just wasted your time. For this reason, to avoid frustration, and wasting time many here and elsewhere will refuse to answer a cross-posted question or help a habitual cross-poster.
- 03-24-2009, 02:14 AM #3
Member
- Join Date
- Mar 2009
- Posts
- 6
- Rep Power
- 0
are you talking about the post in sun's java forums? If you are, I'm really sorry. I won't do that again. :(
-
Yes, the Sun post. And I agree with Cotton: change from AWT to Swing. It's much more robust and flexible.
- 03-24-2009, 03:06 AM #5
Member
- Join Date
- Mar 2009
- Posts
- 6
- Rep Power
- 0
I'll try that. But, just for my knowledge, what is the problem?
-
One big problem is as Cotton points out: paint's method signature doesn't take two parameters. Your paint method must match that of Container's exactly.
The other you seem to have some sort of futile loop in your code. But otherwise I'm not sure as I don't code in AWT but rather in Swing.
- 03-24-2009, 03:17 AM #7
Member
- Join Date
- Mar 2009
- Posts
- 6
- Rep Power
- 0
Can you please help me with using swing? I haven't used swing or listeners or GUI in a a little more than 8 months.
-
Similar Threads
-
keyListener isn't working for me
By lost_in_java in forum AWT / SwingReplies: 7Last Post: 12-05-2008, 04:24 AM -
Unfocused keylistener
By rolfrolf in forum New To JavaReplies: 3Last Post: 11-06-2008, 09:21 AM -
KeyListener Example
By Java Tip in forum SWTReplies: 0Last Post: 07-11-2008, 04:46 PM -
how to add a KeyListener
By leonard in forum New To JavaReplies: 1Last Post: 08-06-2007, 04:44 PM -
Help with KeyListener in applet
By mathias in forum Java AppletsReplies: 1Last Post: 08-06-2007, 02:22 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks