Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (480)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (547)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  KeyListener Interface or KeyAdapter class?  (Read 5092 times)
0 Members and 1 Guest are viewing this topic.
Avenger
Guest
« Posted 2006-09-03 23:14:18 »

Well .. while reading to try to refresh my programming knowledge I am starting to have questions again.

By re-reading Kev's coke and code tutorial I saw he used a private class extension of the KeyAdapter abstract class.While somewhere else I saw a class implementing the KeyListener interface.What is the difference?By checking the java doc I came to the conclusion that the only difference is that with KeyAdapter you can define whichever method you want , while with KeyListener you have to define all the abstract methods of the interface.

BUT .. since I really want to learn corectly and since my personal conclusions more than often tend to be wrong , any clarifications here?What is the difference between those 2?
Offline kevglass

JGO Kernel


Medals: 152
Projects: 23
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #1 - Posted 2006-09-04 00:39:43 »

KeyAdapter is a utlity class which prevents you haveing to implement all the methods of KeyListener by implementing them for you with empty methods. You can then override what you want. The significant difference between the two is one is a class and on is an interface.

So, it's a convienience thing. KeyAdapter can be used where you don't mind using your one and only super class allowance on a utility to save time. Personally I generally always have inner/annonymous classes for keylisteners so use KeyAdapter rather alot.

Kev

Offline cylab

JGO Ninja


Medals: 43



« Reply #2 - Posted 2006-09-04 00:45:08 »

Quote
BUT .. since I really want to learn corectly and since my personal conclusions more than often tend to be wrong , any clarifications here?What is the difference between those 2?

First:
The Adapter class is some sort of convenience utility. If you are too "lazy" to implement the full interface... or if you just want to keep your code uncluttered, you use the Adapter class.

Second:
You don't have multiple inheritance in java. A java class cannot extend more than one base class, but you can implement as most interfaces as you like. So if your class extends KeyAdapter, it cannot extend e.g. JPanel anymore. If on the other hand your class  implements the KeyListener interface, it can extend whatever base class is needed.

As a rule of thumb:
You normally use adapter classes as anonymous inner classes, like
1  
2  
3  
4  
5  
6  
// add a anonymous utility class for handling inputs to "panel"
panel.addKeyListener(new KeyAdapter()
{
  // implement the method you need
});
// (...)

so you use the input functionality.

You use interfaces, if you want to create a new type of utility component, like
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
// This class is a transformation matrix to steer a starship
// based on mouse and keyboard input
class Steering extends Matrix implements MouseListener, MouseMotionListener, KeyboardListener
{
  public Steering(float minX,float maxX,float minY,float maxY)
  {
     // set up your steering algorithms based on the constructor parameters
 }

  // implement the different Listener interfaces to compute the transformation matrix
}
// (...)

so you extend the input functionality.

Mathias - I Know What [you] Did Last Summer!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Avenger
Guest
« Reply #3 - Posted 2006-09-04 07:48:58 »

Hey big thanks to both of you.You tottaly covered my question.Thanks a lot  Cheesy
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

atombrot (23 views)
2014-08-19 09:29:53

Tekkerue (22 views)
2014-08-16 06:45:27

Tekkerue (21 views)
2014-08-16 06:22:17

Tekkerue (12 views)
2014-08-16 06:20:21

Tekkerue (19 views)
2014-08-16 06:12:11

Rayexar (57 views)
2014-08-11 02:49:23

BurntPizza (37 views)
2014-08-09 21:09:32

BurntPizza (29 views)
2014-08-08 02:01:56

Norakomi (36 views)
2014-08-06 19:49:38

BurntPizza (66 views)
2014-08-03 02:57:17
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!