6 Replies - 17352 Views - Last Post: 05 December 2006 - 12:41 PM Rate Topic: -----

#1 beef  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 141
  • Joined: 02-November 06

KeyListener in an Applet

Posted 27 November 2006 - 06:09 PM

Hey guys, I'm having trouble getting a keyListener to cooperate with an applet. The code is a bit much to post so I'll give you a few snippets and if you need to see anything else, let me know.

here's the class declaration:
public class blahBlahBlah extends Applet implements Runnable
												  ,MouseListener
												  ,MouseMotionListener
												  ,KeyListener



My listener methods:
	public void keyTyped(KeyEvent e){kt = "Key Typed - " + e.getKeyCode();}
	
	public void keyPressed(KeyEvent e){kt = "Key Pressed - " + e.getKeyCode();}
	
	public void keyReleased(KeyEvent e){kt = "Key Released - " + e.getKeyCode();}



and the relevent parts of my paint:
	public void paint(Graphics g)
	{
		og.setColor(new Color(150,150,150));
		og.fillRect(0,0,width,height);

		blah blah blah....

		this.requestFocusInWindow();
		//this.requestFocus();
		//this.requestFocus(true);
		
		og.setColor(new Color(0,0,0));
		og.setFont(statusFont);
		og.drawString(
				"(" + x + ", " + y + ")"
				,10
				,og.getFont().getSize()*2);
		og.drawString(
				"(" + mvx + ", " + mvy + ")"
				,10
				,og.getFont().getSize()*6);
		og.drawString(kt,10,og.getFont().getSize()*7);
		
		g.drawImage(offImg,0,0,this);
	}



I know I'm overlooking something stupid but I can't find it...

Is This A Good Question/Topic? 0
  • +

Replies To: KeyListener in an Applet

#2 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: KeyListener in an Applet

Posted 27 November 2006 - 06:22 PM

Where in your code do you bind the KeyListener to your applet?

An example would be:
addKeyListener(this);



Actually post your init() method.
Was This Post Helpful? 0
  • +
  • -

#3 beef  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 141
  • Joined: 02-November 06

Re: KeyListener in an Applet

Posted 27 November 2006 - 06:28 PM

Does it need to be bound? The mouse listener's work fine, I assumed the keyListener would as well...

-edit-
nevermind... I found it, I've done so little with the init I had almost forgot about the poor little guy. Thanks

This post has been edited by beef: 27 November 2006 - 06:28 PM

Was This Post Helpful? 0
  • +
  • -

#4 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: KeyListener in an Applet

Posted 27 November 2006 - 06:35 PM

Yes, they should always be bound, even the mouse listener.

Also did you set setFocusable(true); in your init() method?

Post your init() method, please.

edit: did you solve the problem?
Was This Post Helpful? 0
  • +
  • -

#5 beef  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 141
  • Joined: 02-November 06

Re: KeyListener in an Applet

Posted 27 November 2006 - 06:53 PM

Yes, I fixed it, I'll go ahead and post my init though in case someone else runs into the same thing:

	public void init()
	{
		offImg = createImage(width,height);
		og = offImg.getGraphics();
		
		//pieces wide,tall, width of piece,height,diff pieces
		game = new Game(8,8,70,70,6);//16
		
		addMouseListener(this);
		addMouseMotionListener(this);
		addKeyListener(this);
		
		t = new Thread(this);
		t.start();
		
		i=0;
		
		titleFont = new Font("Helvetica",Font.BOLD,32);
		statusFont = new Font("courier new",Font.PLAIN,15);
		setBackground(new Color(240,255,240));

								miscellanious settings stuff....
	}


Was This Post Helpful? 0
  • +
  • -

#6 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 205
  • View blog
  • Posts: 4,807
  • Joined: 23-December 05

Re: KeyListener in an Applet

Posted 27 November 2006 - 08:10 PM

you can write an applet as an application and then remove the main method once satisfied. I wrote the conversion and applet base under the code segments :)
sometimes it saves time to just write the app.
Was This Post Helpful? 0
  • +
  • -

#7 beef  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 141
  • Joined: 02-November 06

Re: KeyListener in an Applet

Posted 05 December 2006 - 12:41 PM

yeah, I think with what I've been tinkering around with it would have taken longer to write the application...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1