Results 1 to 8 of 8
  1. #1
    Poincare is offline Member
    Join Date
    Mar 2009
    Posts
    6
    Rep Power
    0

    Default 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:
    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) {
    	
    
    	}
    	
    }
    But, all that shows up on the screen is a black background and nothing else. Please help!

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

    Default

    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.

  3. #3
    Poincare is offline Member
    Join Date
    Mar 2009
    Posts
    6
    Rep Power
    0

    Default

    are you talking about the post in sun's java forums? If you are, I'm really sorry. I won't do that again. :(

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

    Default

    Yes, the Sun post. And I agree with Cotton: change from AWT to Swing. It's much more robust and flexible.

  5. #5
    Poincare is offline Member
    Join Date
    Mar 2009
    Posts
    6
    Rep Power
    0

    Default

    I'll try that. But, just for my knowledge, what is the problem?

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

    Default

    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.

  7. #7
    Poincare is offline Member
    Join Date
    Mar 2009
    Posts
    6
    Rep Power
    0

    Default

    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.

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

Similar Threads

  1. keyListener isn't working for me
    By lost_in_java in forum AWT / Swing
    Replies: 7
    Last Post: 12-05-2008, 04:24 AM
  2. Unfocused keylistener
    By rolfrolf in forum New To Java
    Replies: 3
    Last Post: 11-06-2008, 09:21 AM
  3. KeyListener Example
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 07-11-2008, 04:46 PM
  4. how to add a KeyListener
    By leonard in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 04:44 PM
  5. Help with KeyListener in applet
    By mathias in forum Java Applets
    Replies: 1
    Last Post: 08-06-2007, 02:22 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
  •