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 13 of 13

Thread: Error on KeyListener

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Error on KeyListener

    Hi,

    i'm starting studing java, i have found a tutorial on youtube where explain to intercept pressions from Keyboard. I try to do this but seems that's something wrong (made by me) somewhere but i can't find the problem..
    this is the code:

     
     
     
    import java.awt.Graphics;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
     
    import javax.swing.JFrame;
     
     
    public class JavaGame extends JFrame {
     
    	int x;
    	int y;
     
    	public class AL extends KeyAdapter{
    		public void KeyPressed(KeyEvent e){
    			int KeyCode = e.getKeyCode();
     
    			if (KeyCode == e.VK_UP){
    				y--;
    				System.out.println(y);
    			}
     
    			if (KeyCode == e.VK_DOWN){
    				y++;
    				System.out.println(y);
    			}
     
    			if (KeyCode == e.VK_LEFT){
    				x--;
    				System.out.println(x);
    			}
     
    			if (KeyCode == e.VK_RIGHT){
    				x++;
    				System.out.println(x);
    			}
     
    		}
     
    		public void KeyReleased(KeyEvent e){
     
    		}
     
    		public void paint (Graphics g){
    			g.fillOval(x, y, 15, 15);
     
    			repaint();
    		}
    	}
     
    	public JavaGame(){
    		addKeyListener(new AL());
    		setTitle("JavaGame");
    		setSize(400,400);
     
    		setResizable(false);
    		setVisible(true);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
    		x = 150;
    		y = 150;
    	}
     
     
    	public static void main(String[] args) {
     
    		new JavaGame();
     
    	}
     
    }

    with this code i make a window, then with keybord i should paint on this window an oval that follow up down left right buttons on keyboard.
    when running, the code write the window, but there isn't interception of keys...


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Error on KeyListener

    You should call repaint() in the event handler because that is where you know the repaint is needed. And not in the painting code.

    You should also use standard Java coding conventions and start all methods and variables with a lowercase letter. This makes the code more readable and will help avoid problems. KeyAdapter has a method keyPressed() that will be called when a key is pressed, not KeyPressed().

  3. The Following User Says Thank You to pbrockway2 For This Useful Post:

    Colino (January 9th, 2012)

  4. #3
    Junior Member
    Join Date
    Jan 2012
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Error on KeyListener

    many thanks, now with lower case seems to intercept keys but still doesn't paint... not enter in paint method

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Error on KeyListener

    Add a println statement to the paint method to see if it is being called.
    What color will the fillOval() use?

  6. #5
    Junior Member
    Join Date
    Jan 2012
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Error on KeyListener

    Quote Originally Posted by Norm View Post
    Add a println statement to the paint method to see if it is being called.
    What color will the fillOval() use?
    hi,
    i have already done with println but seems that there isn't the call to that method...
    i tried the same code with eclipse into ubuntu and xp , in ubuntu it is printed black, in xp the oval not appear..

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Error on KeyListener

    there isn't the call to that method...
    That's strange. There should be calls to the paint() method.
    Try adding an @Override statement just before the paint method to see if the compiler thinks that your paint mehtod is an override or not.
    in ubuntu it is printed black,
    This is confusing, if the method is not called, then how is the oval drawn?

  8. #7
    Junior Member
    Join Date
    Jan 2012
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Error on KeyListener

    i noticed another thing

    this is the code:
     
     
    import java.awt.*;
    import java.awt.Graphics;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
     
    import javax.swing.JFrame;
     
    public class JavaGame extends JFrame {
     
    	int x;
    	int y;
     
     
    	public class AL extends KeyAdapter{
    		public void keyPressed(KeyEvent e){
    			int keyCode = e.getKeyCode();
     
    			if (keyCode == e.VK_UP){
    				y--;
     
    				System.out.println(y);
    			}
     
    			if (keyCode == e.VK_DOWN){
    				y++;
     
    				System.out.println(y);
    			}
     
    			if (keyCode == e.VK_LEFT){
    				x--;
    				System.out.println(x);
    			}
     
    			if (keyCode == e.VK_RIGHT){
    				x++;
    				System.out.println(x);
     
    			}
     
     
    		}
     
    		public void keyReleased(KeyEvent e){
     
    		}
     
     
     
    		public void paint(Graphics g){
     
    			System.out.println("I\'m here");
    			g.fillOval(x, y, 15, 15);
    			repaint();
     
    		}
     
    	}
     
    	public JavaGame(){
    		addKeyListener(new AL());
    		setTitle("JavaGame");
    		setSize(400,400);
    		setBackground(new Color(255,155,0));
    		setResizable(false);
    		setVisible(true);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
    		x = 150;
    		y = 150;
     
    	}
     
     
    	public static void main(String[] args) {
     
    		new JavaGame();
     
    	}
     
    }

    the statemant
    setBackground(new Color(255,155,0));
    doesn't make the background of that color...
    and
    System.out.println("I\'m here");
    isn't printed in console..

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Error on KeyListener

    Did you add the @Override statement to ask the compiler to check your code?

  10. #9
    Junior Member
    Join Date
    Jan 2012
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Error on KeyListener

    i put @Override on paint mathod and eclipse show this error msg
    "The method paint(Graphics) of type JavaGame.AL must override or implement a supertype method"

  11. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Error on KeyListener

    That says your paint method is not overriding a method in the AL class.
    Does the AL class have a paint method that you can override?

  12. The Following User Says Thank You to Norm For This Useful Post:

    Colino (January 9th, 2012)

  13. #11
    Junior Member
    Join Date
    Jan 2012
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Error on KeyListener

    i put paint method outside AL class, now works..
    many thanks norm, for the help!

  14. #12
    Junior Member
    Join Date
    Jan 2012
    Location
    Mumbai
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Error on KeyListener

    I think you have to implements the KeyListener interface..

  15. #13
    Junior Member
    Join Date
    Jan 2012
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Error on KeyListener

    Quote Originally Posted by RaoPatek View Post
    I think you have to implements the KeyListener interface..
    i tried but eclipse underline it in yellow and tells me that isn't used.. and so i delete that implement

Similar Threads

  1. KeyListener bug
    By nivangerow in forum AWT / Java Swing
    Replies: 1
    Last Post: September 5th, 2011, 06:10 AM
  2. Help with KeyListener and graphics
    By 123099 in forum AWT / Java Swing
    Replies: 21
    Last Post: July 18th, 2011, 01:26 PM
  3. help with KeyListener
    By all_pro in forum AWT / Java Swing
    Replies: 4
    Last Post: April 14th, 2011, 06:51 AM
  4. My KeyListener is not Working!!
    By DarrenReeder in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 28th, 2010, 05:18 PM
  5. Problem with KeyListener
    By r12ki in forum AWT / Java Swing
    Replies: 3
    Last Post: October 1st, 2009, 01:18 PM