Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: help with KeyListener

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question help with KeyListener

    This is the code that I have written. The goal is that if you hit the arrow keys, the dot will move whichever direction you press. What is happening is that it will draw a dot in the middle of the screen (which is what it is supposed to do), But it will not react to the key presses. What do I need to change?

    P.S. I have never done KeyListeners before I only learned this much off of the internet so sorry if I am making a stupid mistake.

    import java.awt.*;
    import java.awt.event.*;
     
    import java.applet.*;
    import java.util.*;
    import static java.lang.System.*;
     
    public class forum_practice extends Applet implements KeyListener,ActionListener{
     
    	Image dot; 
    	int x = 0;
    	int y = 0;
     
    	public void init() {
    	    dot = getImage(getCodeBase(), "black.gif");
    	    x = 100;
    	    y = 100;
    	}
    	public void paint(Graphics g) {
    	    g.drawImage(dot, x, y, this);
    	}
    	public void move(Graphics g,int x,int y){
    		g.drawImage(dot,x,y,this);
    	}
    	public void actionPerformed(ActionEvent e) {
     
    	}
    	@Override
    	public void keyPressed(KeyEvent e) {
    		if(e.getKeyCode()==37){
    			x-=10;
    			move(x,y);
    		}
    		if(e.getKeyCode()==38){
    			y-=10;
    			move(x,y);
    		}
    		if(e.getKeyCode()==39){
    			x+=10;
    			move(x,y);
    		}
    		if(e.getKeyCode()==40){
    			y+=10;
    			move(x,y);
    		}
    	}
    	@Override
    	public void keyReleased(KeyEvent e) {
     
    	}
    	@Override
    	public void keyTyped(KeyEvent e) {
    		// TODO Auto-generated method stub
     
    	}
     
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,273
    Thanks
    177
    Thanked 822 Times in 765 Posts
    Blog Entries
    5

    Default Re: help with KeyListener

    I don't see where the KeyListener is added to any component (also don't see how this compiles, but that's a separate issue). Call addKeyListener to do so (See How to Write a Key Listener (The Java™ Tutorials > Creating a GUI With JFC/Swing > Writing Event Listeners) )

  3. #3
    Junior Member
    Join Date
    Apr 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: help with KeyListener

    Alright so here is my modified code. It now does what I wanted but there is a white flash every time it moves, and you can only see the dot on like every tenth move. How can I make the animation smooth?

    import java.awt.*;
    import java.awt.event.*;
     
    import java.applet.*;
    import java.util.*;
    import static java.lang.System.*;
     
    public class forum_practice extends Applet implements KeyListener{
     
    	int x = 0;
    	int y = 0;
    	byte direction;
    	final byte UP = 1;
    	final byte RIGHT = 2;
    	final byte DOWN = 3;
    	final byte LEFT = 4;
     
    	public void init() {
    	    x = 100;
    	    y = 100;
    	    addKeyListener(this); 
    	}
    	public void paint(Graphics g) {
    		g.setColor(Color.black);
    		g.fillOval(x, y, 10, 10);
    		run();
    	}
    	public void run(){
    		try {
    			Thread.sleep(100);
    		} catch (InterruptedException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
     
    		if(direction==UP){
    			y-=10;
    		}
    		if(direction==DOWN){
    			y+=10;
    		}
    		if(direction==LEFT){
    			x-=10;
    		}
    		if(direction==RIGHT){
    			x+=10;
    		}
     
    		repaint();
     
     
    	}
    	@Override
    	public void keyPressed(KeyEvent e) {
    		if(e.getKeyCode()==37){
    			direction = LEFT;
    		}
    		if(e.getKeyCode()==38){
    			direction = UP;
    		}
    		if(e.getKeyCode()==39){
    			direction = RIGHT;
    		}
    		if(e.getKeyCode()==40){
    			direction = DOWN;
    		}
    		repaint();
    	}
    	@Override
    	public void keyReleased(KeyEvent e) {
     
    	}
    	@Override
    	public void keyTyped(KeyEvent e) {
    		// TODO Auto-generated method stub
     
    	}
    }

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,273
    Thanks
    177
    Thanked 822 Times in 765 Posts
    Blog Entries
    5

    Default Re: help with KeyListener

    Your running into a common issue when it comes to drawing things with swing. Swing is a single threaded model, and updates and listeners are called on this thread. When you call Thread.sleep you end up sleeping this thread, and no painting can be performed until the sleep has finished. If you wish to perform animation, use a SwingTimer or throw the work onto another thread (if you use the other thread approach, make sure you place swing calls onto the EDT using SwingUtilties).

  5. #5
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: help with KeyListener

    Thread moved to - AWT / Java Swing
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

Similar Threads

  1. Replies: 1
    Last Post: February 10th, 2011, 07:57 AM
  2. keylistener small problem
    By matecno in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 14th, 2010, 07:51 PM
  3. My KeyListener is not Working!!
    By DarrenReeder in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 28th, 2010, 04:18 PM
  4. Problem with KeyListener
    By r12ki in forum AWT / Java Swing
    Replies: 3
    Last Post: October 1st, 2009, 01:18 PM

Tags for this Thread