Hi !
Featured games (84)
games approved by the League of Dukes
Games in Showcase (604)
Games in Android Showcase (171)
games submitted by our members
Games in WIP (654)
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 6068 times)
0 Members and 1 Guest are viewing this topic.
« 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 Spiffy Duke »

Medals: 272
Projects: 25
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.


Offline cylab

JGO Wizard

Medals: 89

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

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?

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.

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
// 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
// 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
« 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.

bilznatch (27 views)
2015-08-04 11:03:17

SHC (44 views)
2015-08-01 03:58:20

Jesse (25 views)
2015-07-29 04:35:27

Riven (48 views)
2015-07-27 16:38:00

Riven (26 views)
2015-07-27 15:35:20

Riven (28 views)
2015-07-27 12:26:13

Riven (19 views)
2015-07-27 12:23:39

BurntPizza (42 views)
2015-07-25 00:14:37

BurntPizza (56 views)
2015-07-24 22:06:39

BurntPizza (35 views)
2015-07-24 06:06:53
List of Learning Resources
by gouessej
2015-07-09 11:29:36

How Do I Expand My Game?
by bashfrog
2015-06-14 11:34:43

List of Learning Resources
by PocketCrafter7
2015-05-31 05:37:30

Intersection Methods
by Roquen
2015-05-29 08:19:33

List of Learning Resources
by SilverTiger
2015-05-05 10:20:32

How to: JGO Wiki
by Mac70
2015-02-17 20:56:16

2D Dynamic Lighting
by ThePixelPony
2015-01-01 20:25:42

How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21 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‑
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!