2 Replies - 1401 Views - Last Post: 11 December 2012 - 06:00 PM Rate Topic: -----

#1 extremeblueness  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 186
  • Joined: 22-October 12

Abstract Inheritence from either JApplet or KeyListener

Posted 11 December 2012 - 06:56 AM

My error message is:

Quote

SaveChristmas is not abstract and does not override abstract method keyReleased(java.awt.event.KeyEvent) in java.awt.event.KeyListener


Here's my code so far:

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/*
<applet code="SaveChristmas" width=300 height=50>
</applet>
*/

public class SaveChristmas extends JApplet
    implements KeyListener
{
    boolean pause;
    char ch = 0;
    String hw = "HW";
    
    public void init()
    {
        addKeyListener(this);
    }
    
    public void start()
    {
        pause = false;
    }
    
    public void run()
    {
        char ch;
        
        for( ; ; )
        {
            try
            {
                repaint();
                if(pause)
                    break;
            }
            catch(InterruptedException exc) {}
        }
    }
    
    public void stop()
    {
        pause = true;
    }
    
    public void keyTyped(KeyEvent e)
    {
        ch = getKeyChar();
    }
    
    public void paint(Graphics g)
    {
        g.drawString(hw, 0, 0);
    }
}



I am trying to make an RPG, in which the user will input single key commands and am trying to use getKeyChar() to read the key typed. PLEASE NOTE: I'm trying to do this WITHOUT a special text field for input. I've seen it done without making the class abstract, and since I don't know what implications that would have on what my program does, I'm gonna try to avoid that. Please note that I will be using a GridLayout manager, so please make any suggestions compatible with this.

What I have tried:

changing the extends from JApplet to Applet
putting filler code into paint()

I'm fresh out of ideas on how to fix this. Could anyone possible help me? Any amount of help, no matter how small, is gleefully welcomed.

Is This A Good Question/Topic? 0
  • +

Replies To: Abstract Inheritence from either JApplet or KeyListener

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: Abstract Inheritence from either JApplet or KeyListener

Posted 11 December 2012 - 07:16 AM

implementing KeyListener (an interface) requires you to include implementations for all of the KeyListener methods, even if they don't do anything (are empty).

Here's a tutorial.
Was This Post Helpful? 2
  • +
  • -

#3 extremeblueness  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 186
  • Joined: 22-October 12

Re: Abstract Inheritence from either JApplet or KeyListener

Posted 11 December 2012 - 06:00 PM

Thanks for all the help. Because of you, I finally understood the error message and was able to override methods keyReleased(KeyEvent e) and keyPressed(KeyEvent e).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1