Java Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingJava Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old May 2nd, 2012, 01:12 PM
healthy999 healthy999 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2012
Posts: 1 healthy999 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 m 36 sec
Reputation Power: 0
Problem with KeyEvents

I'm having a problem trying to display a small box that moves around with the press of an arrow key. It seems really simple to me, so I'm not sure why it's not been working. Also, the box displays way to large (rather than 10 x 10, it's much larger).

I get the following error on keypress:

Exception in thread "AWT-EventQueue-1" java.lang.Error: Unresolved compilation problem:
The type Draw must implement the inherited abstract method KeyListener.keyReleased(KeyEvent)

Here's the main code:

Code:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;


public class Draw extends Applet implements KeyListener {
	
	Thing thing1 = new Thing();
	
	public void init() {
		addKeyListener(this);		
	}
	
	public void paint(Graphics g) {
		
		g.drawRect(10, 10, thing1.xlocation, thing1.ylocation);
	
	
	}
	
	public void KeyReleased() {};
	public void KeyTyped(KeyEvent evt) {};
	
	
	public void KeyPressed(KeyEvent evt) {
		
		int key = evt.getKeyCode();
		
		if(key == KeyEvent.VK_LEFT) { thing1.xlocation--; }
		else if(key == KeyEvent.VK_RIGHT) { thing1.xlocation++; }
		else if(key == KeyEvent.VK_DOWN) { thing1.ylocation--; }
		else if(key == KeyEvent.VK_UP) { thing1.ylocation++; }
		repaint();
	}
	

	

}



Here's the code for my thing class:


import java.awt.event.KeyEvent;

Code:
public class Thing {

	public
	
		int xlocation = 300;
		int ylocation = 300;
	

	
}

Reply With Quote
  #2  
Old May 24th, 2012, 08:04 AM
williamxaviers williamxaviers is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2012
Posts: 5 williamxaviers User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 40 m 39 sec
Reputation Power: 0
Quote:
Originally Posted by healthy999
I'm having a problem trying to display a small box that moves around with the press of an arrow key. It seems really simple to me, so I'm not sure why it's not been working. Also, the box displays way to large (rather than 10 x 10, it's much larger).

I get the following error on keypress:

Exception in thread "AWT-EventQueue-1" java.lang.Error: Unresolved compilation problem:
The type Draw must implement the inherited abstract method KeyListener.keyReleased(KeyEvent)

Here's the main code:

Code:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;


public class Draw extends Applet implements KeyListener {
	
	Thing thing1 = new Thing();
	
	public void init() {
		addKeyListener(this);		
	}
	
	public void paint(Graphics g) {
		
		g.drawRect(10, 10, thing1.xlocation, thing1.ylocation);
	
	
	}
	
	public void KeyReleased() {};
	public void KeyTyped(KeyEvent evt) {};
	
	
	public void KeyPressed(KeyEvent evt) {
		
		int key = evt.getKeyCode();
		
		if(key == KeyEvent.VK_LEFT) { thing1.xlocation--; }
		else if(key == KeyEvent.VK_RIGHT) { thing1.xlocation++; }
		else if(key == KeyEvent.VK_DOWN) { thing1.ylocation--; }
		else if(key == KeyEvent.VK_UP) { thing1.ylocation++; }
		repaint();
	}
	

	

}



Here's the code for my thing class:


import java.awt.event.KeyEvent;

Code:
public class Thing {

	public
	
		int xlocation = 300;
		int ylocation = 300;
	

	
}


The value of the type at the moment is not looking that it is being used or implemented in the program. However the errors showing for the implementation of type draw, could be true, but for the time being I am just thinking that draw has not been called. I will return to the solution after checking it.

Reply With Quote
  #3  
Old July 31st, 2012, 10:54 AM
lukwes1 lukwes1 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2012
Posts: 3 lukwes1 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 11 m 50 sec
Reputation Power: 0
Re

The methods must looks like this:

public void keyPressed(KeyEvent e) {

}

public void keyReleased(KeyEvent e) {

}

public void keyTyped(KeyEvent e) {

}

And not:

public void keyReleased(){}

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingJava Development > Problem with KeyEvents


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap