Results 1 to 3 of 3
  1. #1
    Join Date
    May 2011
    Posts
    1
    Rep Power
    0

    Default KeyListener Graphics Problem

    Hi there,
    I'm pretty new to Java but interested in eventually making games of some kind. Right now I'm struggling with the KeyListener. I'm trying to set up basic WASD-like movement, but the graphics don't seem to respond. KeyPressed is supposed to change the x-coord of a component, but no visual movement happens. However, the System.out.println returns changing coordinates, which means that the Listener itself is working, right? Any help would be appreciated, thanks.

    Java Code:
    public class MoveViewer
    {
        static JFrame frame=new JFrame("Move Viewer");
        static JPanel panel=new JPanel();
        static int yPos=10;
        static int xPos=10;
        static MoveComponent mc=new MoveComponent(xPos,yPos);
        public static void main(String[] args)
        {
    	frame.setSize(600,600);
    	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	panel.add(mc);
    	panel.setLayout(new OverlayLayout(panel));
    	frame.add(panel);
    	panel.setFocusable(true);
    	panel.requestFocus();
    class MoveListener implements KeyListener
    {
        public void keyTyped(KeyEvent e)
        {
        }
        public void keyPressed(KeyEvent e)
        {
    	int key=e.getKeyCode();
    	if(key==KeyEvent.VK_RIGHT)
    	    {
    		xPos=xPos+50;
    		mc=new MoveComponent(xPos,yPos);
    		System.out.println(""+xPos+", "+yPos);
    		panel.repaint();
    	    }
        }
        public void keyReleased(KeyEvent e)
        {
        }
    }
    MoveListener ml=new MoveListener();
    panel.addKeyListener(ml);
    frame.setVisible(true);
        }
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    What does panel's paintComponent method do?
    What code is supposed to cause the movement?

    What is supposed to happen to the object pointed to by mc in the listener?
    Last edited by Norm; 05-25-2011 at 11:01 PM.

  3. #3
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default

    Java Code:
    mc=new MoveComponent(xPos,yPos);
    This DOES NOT add a component to the GUI. All it does is create a new Object in memory.

    If you want to create a new component then you would do:

    Java Code:
    mc=new MoveComponent(xPos,yPos);
    panel.add( mc );
    panel.revalidate();
    panel.repaint();
    However even that code is wrong because now you would have 2 components on the panel.

    The proper way to do this is to just invoke mc.setLocation(...) to move the existing component to a new location.

Similar Threads

  1. Replies: 2
    Last Post: 05-11-2011, 06:29 PM
  2. Problem with Keylistener, some help pls
    By syon in forum AWT / Swing
    Replies: 1
    Last Post: 01-21-2011, 01:31 AM
  3. AWT KeyListener Problem
    By plm-pusik in forum New To Java
    Replies: 15
    Last Post: 11-10-2010, 03:38 PM
  4. KeyListener problem
    By siyi90 in forum AWT / Swing
    Replies: 7
    Last Post: 02-08-2010, 10:16 AM
  5. Problem with KeyListener in Jtable
    By sandeepsai17 in forum New To Java
    Replies: 0
    Last Post: 06-30-2009, 10:14 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
  •