Results 1 to 3 of 3
  1. #1
    Guy
    Guy is offline Member
    Join Date
    Jul 2011
    Posts
    42
    Rep Power
    0

    Question Adding keyListener to a 2D object.

    Hello, I am trying to make a simple 'game' where you can move an ellipse around a background using the wasd keys. I have managed to draw everything but I can't figure out how to add the keyListener to the Ellipse so only it will move. Here is my code, at the moment using the wasd keys move the background and ellipse around.

    Java Code:
    public class Game extends JFrame implements KeyListener {
    
    	char input;
    	JFrame f = new JFrame();
    	Draw d = new Draw();
    
    	int x, y = 0; // x and y position of ellipse
    	
    	public Game() {
    		f.setTitle("Game");
    		f.setSize(1430, 800);
    		f.setResizable(false);
    		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		f.add(d);
    		f.addKeyListener(this);
    		f.setFocusable(true);
    		f.setVisible(true);
    	}
    	
    	public void keyTyped(KeyEvent e) {
    		
    		input = e.getKeyChar();
    		move(d);
    	}
    	
    	public void keyPressed(KeyEvent e) {	
    	}
    	
    	public void keyReleased(KeyEvent e) {	
    	}
    
    	public void move(Draw d) {	
    		if (input == 'w') {
    			y -= 5;
    		}
    		else if (input == 'a') {
    			x -= 5;
    		}
    		else if (input == 's') {
    			y += 5;
    		}
    		else if (input == 'd') {
    			x += 5;
    		}
    		
    		
    		d.setLocation(x, y);
    		f.repaint();
    	}
    	
    	public static void main(String[] args) {
    		Game g = new Game();
    
    	}
    
    }
    
     class Draw extends JPanel{
    	 
    		public void paintComponent(Graphics c) {
    			Graphics2D c2D = (Graphics2D)c;	
    			c2D.setColor(Color.blue);
    			Rectangle2D.Float bG = new Rectangle2D.Float(0F, 0F, (float)getSize().width, (float)getSize().height);
    			c2D.fill(bG);
    			
    			GradientPaint fG = new GradientPaint(0F, 0F, Color.green, 1230F, 700F, Color.green.darker());
    			
    			c2D.setPaint(fG);
    			GeneralPath o = new GeneralPath();
    			
    			o.moveTo(80F, 50F);
    			o.lineTo(200F, 120F);
    			o.lineTo(230F, 300F);
    			//continues for a while...
    
    			o.closePath();
    			c2D.fill(o);
    			
    			c2D.setColor(Color.black);
    			BasicStroke bPen = new BasicStroke();
    			c2D.setStroke(bPen);
    			Ellipse2D.Float ball = new Ellipse2D.Float(600, 350, 50, 50);
    			
    			c2D.fill(ball);			
    		}
    }
    Adding a keyListener to the ball gives this error: The method addKeyListener() is undefined for the type Ellipse2D.Float
    I have tried searching the web and reading some 2D tutorials but I can't figure it out.

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

    Default

    Quote Originally Posted by Guy View Post
    Hello, I am trying to make a simple 'game' where you can move an ellipse around a background using the wasd keys. I have managed to draw everything but I can't figure out how to add the keyListener to the Ellipse so only it will move. Here is my code, at the moment using the wasd keys move the background and ellipse around.
    I don't get such an error when I compile and run your code. Is the error coming from other code not shown?

    But also, you cannot add a KeyListener to a Shape object, only to an object that derives from Component, and it must have the focus to work. Understand that it is the GUI that listens for key presses, not the ball or any non-component logical object that you create. If you want the ball to move based on key press, then it should be an object visible throughout the class (a non-static class field), and your key listener's methods should act on this object and call repaint.

    I would also recommend that you avoid creating objects inside of your paint/paintComponent methods if at all possible. These methods need to be as lean and fast as possible, and since you will only need a defined number of Shapes, create them in the constructor and just draw them in paintComponent.

    Also, look into using key bindings instead of key listeners since these offer solution for the focus issue described above.
    Last edited by Fubarable; 07-24-2011 at 06:22 PM.

  3. #3
    Guy
    Guy is offline Member
    Join Date
    Jul 2011
    Posts
    42
    Rep Power
    0

    Default

    Thanks for the helpful reply!
    I have to do lots of homework for school tomorrow so I can't look properly into what you said about key binding and stuff right now, but I think I should be able to get it working from here. If I run into problems (which I always seem to do) I'll post back here tomorrow.
    Thanks again

Similar Threads

  1. adding keylistener
    By natdizzle in forum AWT / Swing
    Replies: 2
    Last Post: 02-02-2011, 01:22 AM
  2. Help adding a keylistener to a paint class
    By Phill Phan in forum New To Java
    Replies: 3
    Last Post: 01-20-2011, 09:28 PM
  3. Adding a KeyListener to a JFrame with buttons.
    By jamhead in forum AWT / Swing
    Replies: 1
    Last Post: 12-11-2010, 07:29 PM
  4. Help me in adding keylistener
    By kumarv75 in forum CLDC and MIDP
    Replies: 0
    Last Post: 06-22-2010, 07:10 AM
  5. Help Using KeyListener to Move an Object
    By jjonesmu in forum New To Java
    Replies: 3
    Last Post: 04-20-2010, 10:35 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •