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

Thread: how to use the keyboard up,down,right,left

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

    Post how to use the keyboard up,down,right,left

    Hi, i am making a simple program that uses Graphics and this is the first time that i used this Graphics,now what i want is to move the circle inside in my JFrame and controlled by the arrow up,down,left and right.

    How can i accomplished this.?

    import java.util.*;
    import java.awt.*;
    import javax.swing.*;
     
     
    public class TestPoint extends JFrame 
    {
     
       public TestPoint()
       {
       	     setTitle("Moving circle");
    	     setSize(400,400);
    	     setVisible(true);
    	     setDefaultCloseOperation(EXIT_ON_CLOSE);
     
       }
     
     
    	public void paint(Graphics g)
    	{
     
     
    	    super.paint(g);
     
    	    this.setBackground(Color.white);
    		g.setColor(Color.RED);
    	    g.fillOval(10,100,10,10);
     
     
     
     
    	}
     
     
    	public static void main(String[]c)
    	{
     
    		TestPoint t = new TestPoint();
    	       t.setBackground(Color.white);
     
     
     
    	}
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,265
    Thanks
    56
    Thanked 2,372 Times in 2,343 Posts

    Default Re: how to use the keyboard up,down,right,left

    You will need to use a keylistener or key binding so the program will know when one of the keys is pressed and can react to it.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Sep 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: how to use the keyboard up,down,right,left

    Quote Originally Posted by Norm View Post
    You will need to use a keylistener or key binding so the program will know when one of the keys is pressed and can react to it.
    Hi thank you for the reply, can you please show me example just only to move my circle i really don't have idea on it...this is my first time to use the keylistener i hope you can help me norm.

  4. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: how to use the keyboard up,down,right,left

    I would suggest that you use Key Bindings since this is a Swing program, and there's a great tutorial on how to use Key Bindings in the Java tutorials. This will likely help get you started.

  5. #5
    Junior Member
    Join Date
    Sep 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: how to use the keyboard up,down,right,left

    Quote Originally Posted by curmudgeon View Post
    I would suggest that you use Key Bindings since this is a Swing program, and there's a great tutorial on how to use Key Bindings in the Java tutorials. This will likely help get you started.

    hi, thank you for the reply, but i was confuse by the example.how do i control it the circle to move?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,265
    Thanks
    56
    Thanked 2,372 Times in 2,343 Posts

    Default Re: how to use the keyboard up,down,right,left

    how do i control it the circle to move?
    Change its x,y location so when the paint method is called it will be drawn at a new location.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Sep 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: how to use the keyboard up,down,right,left

    Quote Originally Posted by Norm View Post
    Change its x,y location so when the paint method is called it will be drawn at a new location.
    Do you have a snippet on this,can you please show me.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,265
    Thanks
    56
    Thanked 2,372 Times in 2,343 Posts

    Default Re: how to use the keyboard up,down,right,left

    There are lots of code samples on the forum or Google.
    Do a search.

    Change the x,y values in the fillOval() method to use variables instead of hardcoded numbers.
    Then change the values of those variables to change the location of the oval.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Sep 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: how to use the keyboard up,down,right,left

    Hi, I am having problem on my code it says cannot find symbol

    in this part of code
    int condition = JComponent.WHEN_IN_FOCUSED_WINDOW;
    InputMap inputMap = getInputMap(condition);
    ActionMap actionMap = getActionMap();
    inputMap.put(keyStroke.getKeyStroke(KeyEvent.vk_up _right),"up");
    getActionMap().put("up",up);

    import java.util.*;
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
     
     
     
    public class TestPoint extends JFrame 
    {
     
      int x = 10;
      int y=100;	
       public TestPoint()
       {
     
       	  int condition = JComponent.WHEN_IN_FOCUSED_WINDOW;
          InputMap inputMap = getInputMap(condition);
          ActionMap actionMap = getActionMap();
       	     setTitle("My Ball");
    	     setSize(400,400);
    	     setVisible(true);
    	     setDefaultCloseOperation(EXIT_ON_CLOSE);
     
    	    Action up = new AbstractAction(){
    	    	public void actionPerformed(ActionEvent e){	    		
    	    		y++;
    	    		repaint();
    	    	}
    	    };
     
     
       	    inputMap.put(keyStroke.getKeyStroke(KeyEvent.vk_up_right),"up");
       	    getActionMap().put("up",up);
     
       }
     
     
    	public void paint(Graphics g)
    	{
    	    super.paint(g);
     
    	    this.setBackground(Color.white);
    		g.setColor(Color.RED);
    	    g.fillOval(x,y,10,10);
     
    	}
     
     
     
    	public static void main(String[]c)
    	{ 
    		TestPoint t = new TestPoint();
    	    t.setBackground(Color.white);
     
    	}
     
     
    }

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,265
    Thanks
    56
    Thanked 2,372 Times in 2,343 Posts

    Default Re: how to use the keyboard up,down,right,left

    problem on my code it says cannot find symbol
    Please post the full text of the compiler's error message that shows what symbol can not be found and what source line in the code the error is on.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Need help with making a menu that sits on left side of JFrame
    By AberystwythLAD in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 2nd, 2012, 02:39 PM
  2. [SOLVED] JList display Hebrew right-to-left
    By ilan in forum AWT / Java Swing
    Replies: 3
    Last Post: October 5th, 2011, 07:43 AM
  3. Getting my paddle to move left and right in a straight line-STUCK
    By warnexus in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 20th, 2011, 09:06 PM
  4. Java:Evaluation of Mathematical Expression only from left to right only.
    By deepakl_2000 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: July 15th, 2011, 08:35 AM
  5. Left and Right rotation in a balanced binary tree problem
    By szuwi in forum Algorithms & Recursion
    Replies: 2
    Last Post: December 22nd, 2010, 07:20 PM