3 Replies - 242 Views - Last Post: 04 November 2012 - 01:26 PM Rate Topic: -----

#1 steven_bishop  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 37
  • Joined: 20-October 12

Anonomous KeyAdapter class not responding ?

Posted 04 November 2012 - 08:15 AM

Hello everyone. Ive been watching java tutorials around the clock, and its all stating to click.

so i was taught to use an inner Key Adapter class for handling Key Event methods, when i got good at that and it all worked as expected, I was then taught to use an anonymous class for handling event in the class constructor but this doesn't work as id expect it too. any ideas ? whats wrong with the code.



import javax.swing.JFrame ;

public class RunGame extends JFrame
{
	// instances	
	Canvas c ;
	
	// constructor	
	public RunGame()
	{
		setTitle("Game") ;
		setSize(600, 600) ;
		setResizable(false) ;
		setLocationRelativeTo(null) ;
		setVisible(true) ;
		c = new Canvas() ;		
		add(c) ;		
	}
		
	// main method	
	public static void main(String[] args)
	{
	 //instantiations		
	 RunGame rg = new RunGame() ;
	}
	

	
	
	
}



import javax.swing.* ;
import java.awt.* ;
import java.awt.event.KeyAdapter ;
import java.awt.event.KeyEvent ;

public class Canvas extends JPanel implements Runnable
{
	// instances
	static final int G_WIDTH = 600,G_HEIGHT = 600;
	Dimension game_dim = new Dimension(G_WIDTH,G_HEIGHT) ;
	int draw_x ;
	int draw_y ;
		
	// constructor
	
	public Canvas()
	{
		setPreferredSize(game_dim) ;
		setFocusable(false) ;
		setBackground(Color.BLUE) ;
				
        addKeyListener(new KeyAdapter()
	    {
		 @Override
		 public void keyPressed(KeyEvent e)
		 {
		 	  int key_code = e.getKeyCode() ;
		 	  
		      if(e.getKeyCode() == KeyEvent.VK_LEFT)
		      {
		      	set_draw_x(-1) ;
		      }	
		      	if(e.getKeyCode() == KeyEvent.VK_RIGHT)
		      {
		      	set_draw_x(+1) ;
		      }			
		}
		@Override
		public void keyReleased(KeyEvent e)
		{
	}
		
	    }) ;		
	}
	
	//set and get methods
	public void set_draw_y(int p_draw_y)
	{
		draw_y = p_draw_y ;
	}
	public void set_draw_x(int p_draw_x)
	{
		draw_x = p_draw_x ;
	}
	
    public void paintComponent(Graphics g)
	{   
		super.paintComponent(g) ;
		set_draw_x(200) ;
		set_draw_y(200) ;
		
		g.setColor(Color.RED) ;
		g.fillRect(draw_x,draw_y,50,50) ;
	}
	
	public void start()
	{
		Thread t1 = new Thread() ;
		t1.start() ;
	}
	public void run()
	{
	
		try
		{
	    Thread.sleep(60) ;
	          // change this to use a move method()
		}
		catch(Exception e)
		{
			
		}
	
	}
	
	
	
}


Is This A Good Question/Topic? 0
  • +

Replies To: Anonomous KeyAdapter class not responding ?

#2 CasiOo  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1373
  • View blog
  • Posts: 3,024
  • Joined: 05-April 11

Re: Anonomous KeyAdapter class not responding ?

Posted 04 November 2012 - 10:03 AM

setFocusable(false) ;

You just made your JPanel not focusable, so no you can't receive key events on it
Instead add a listener on the JFrame if you don't want your JPanel to be focusable
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Anonomous KeyAdapter class not responding ?

Posted 04 November 2012 - 10:15 AM

For a KeyListener to work, the component should have the focus so
- you'll have to remove setFocusable(false) in your Canavas class
- add c.request(focus) in your RunGame class

P.S.
- not a good idea at all to have a class named Canvbas which is a standard API class
- you should add a defaultCloseOperation() to your JFrame
- I am afraid of what you are planning to do with your Thread... don't use thread with Swing use a Swing Timer instead
Was This Post Helpful? 0
  • +
  • -

#4 steven_bishop  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 37
  • Joined: 20-October 12

Re: Anonomous KeyAdapter class not responding ?

Posted 04 November 2012 - 01:26 PM

Thank you cassiOo and PBL this completely worked. :^:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1