GAMES LESSON FIFTEEN: Key and Mouse Events Without Adapter Classes

Using adapter classes is the easiest way to enable events. But you should know that there are other ways to set up events. In this lesson we will take a look at how to deal with implementing the event interfaces directly. We will only use the mouseListener and keyListener interfaces, but this will give you some idea why it is usually easier to just use the adapter classes.

First off, the one big advantage of adapter classes is that they take care of all the methods which must be implemented when implementing an interface. For instance, the mouseListener interface requires that you create methods called mouseClicked, mouseEntered, mouseReleased, mousePressed, and mouseExited. The keyListener interface requires that you create methods called keyPressed, keyReleased, and keyTyped.

Although we actually create all the methods required by the keyListener and mouseListener interfaces, we only actually supply actions for two of them: keyPressed and mouseClicked (as shown below).

public void keyPressed(KeyEvent k){ if(k.getKeyCode() == KeyEvent.VK_BACK_SPACE || k.getKeyCode() == KeyEvent.VK_DELETE){ input=""; } else if(k.getKeyCode() == KeyEvent.VK_ENTER){ for(int x = 0; x<code.length; x++){ if(input.equals(code[x])){ message = response[x]; repaint(); return; } } message = "Keep trying."; } else if(k.getKeyCode() != KeyEvent.VK_SHIFT){ input+=k.getKeyChar(); } repaint(); }
public void mouseClicked(MouseEvent m){ i++; i%=c.length; repaint(); }

Also notice the lines in the init method which actually load the event listeners.

NOADAPT.java

import java.awt.*; import java.applet.*; import java.awt.event.*; public class NOADAPT extends Applet implements KeyListener, MouseListener{ String input = ""; Color c[] = { Color.red, Color.yellow, Color.cyan, Color.magenta, Color.white, Color.green }; String code[] = { "ping", "pong", "ding", "dong" }; String response[] = { "Good afternoon.", "How do you do?", "That's interesting.", "Oh, really?" }; int i; String message = "Start"; Dimension d; public void init(){ d = getSize(); addMouseListener(this); addKeyListener(this); requestFocus(); } public void keyPressed(KeyEvent k){ if(k.getKeyCode() == KeyEvent.VK_BACK_SPACE || k.getKeyCode() == KeyEvent.VK_DELETE){ input=""; } else if(k.getKeyCode() == KeyEvent.VK_ENTER){ for(int x = 0; x<code.length; x++){ if(input.equals(code[x])){ message = response[x]; repaint(); return; } } message = "Keep trying."; } else if(k.getKeyCode() != KeyEvent.VK_SHIFT){ input+=k.getKeyChar(); } repaint(); } public void keyReleased(KeyEvent k){ } // do nothing public void keyTyped(KeyEvent k) { } // do nothing public void mouseClicked(MouseEvent m){ i++; i%=c.length; repaint(); } public void mouseEntered(MouseEvent m){} //do nothing public void mouseExited(MouseEvent m){} //do nothing public void mousePressed(MouseEvent m){} //do nothing public void mouseReleased(MouseEvent m){} //do nothing public void paint(Graphics g){ g.setColor(c[i]); g.fillRect(0,0,d.width,d.height); g.setColor(Color.black); g.drawString(message, 20,40); g.drawString("INPUT: " + input, 20, 60); } }


ASSIGNMENT: