8 Replies - 462 Views - Last Post: 05 December 2012 - 04:13 AM Rate Topic: -----

#1 Amadrid064  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 03-October 12

getting a keylistener and keytyped error, please help

Posted 04 December 2012 - 03:12 PM

Getting a error java:49: SpacePanel.MoveListener is not abstract and does not override abstract method keyTyped(java.awt.event.KeyEvent) in java.awt.event.KeyListener and I have looked at the api and all the examples in my book and can not figure out why my code is giving me this error. Here is my entire in case I may have put something in the wrong location but I don't believe I have.

//*Armando Madrid*//
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SpacePanel extends JPanel
{
  private final int WIDTH = 500, HEIGHT = 500;
  private final int IMAGE_SIZE = 40;
  
  private ImageIcon ship, space;
  private int x, y, moveX, moveY;
  
  public SpacePanel()
  {
    addKeyListener (new MoveListener());
    
    ship = new ImageIcon ("ship.gif");
    space = new ImageIcon ("url.jpg");
    
    x = 250;
    y = 250;
    moveX = moveY = 2;
    
    setPreferredSize (new Dimension (WIDTH, HEIGHT));
    //page.setBackground (space);
    setFocusable (true);
  }
  
  public void paintComponent (Graphics page)
  {
    super.paintComponent (page);
    ship.paintIcon (this, page, x, y);
    ship.paintIcon (this, page, x - WIDTH, y);
    ship.paintIcon (this, page, x - WIDTH, y - HEIGHT);
    ship.paintIcon (this, page, x, y - HEIGHT);
    
    x += moveX;
    y += moveY;
    
    if (x == 0)
      x += WIDTH;
    x = x % WIDTH;
    if (y == 0)
      y += HEIGHT;
    y = y % HEIGHT;
  }
  
  private class MoveListener implements KeyListener
  {
    public void keyPressed (KeyEvent event){}
    
    public void keyReleased (KeyEvent event)
    {
      switch (event.getKeyCode())
      {  
        case KeyEvent.VK_UP:
          moveY += -1;
          break;
        case KeyEvent.VK_DOWN:
          moveY += 1;
          break;
        case KeyEvent.VK_LEFT:
          moveX += 1;
          break;
        case KeyEvent.VK_RIGHT:
          moveX += -1;
          break;
          
          repaint();
      }
    }
  }
}

 

This post has been edited by no2pencil: 04 December 2012 - 03:14 PM
Reason for edit:: Corrected Code Tags


Is This A Good Question/Topic? 0
  • +

Replies To: getting a keylistener and keytyped error, please help

#2 Amadrid064  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 03-October 12

Re: getting a keylistener and keytyped error, please help

Posted 04 December 2012 - 03:41 PM

sorry forgot to say that my error is occurring on line 49
Was This Post Helpful? 0
  • +
  • -

#3 Takwas  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 33
  • Joined: 05-September 12

Re: getting a keylistener and keytyped error, please help

Posted 04 December 2012 - 06:20 PM

Try this:

I just did a quick Eclipse fix. Hope it works.

package practice;

//*Armando Madrid*//
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class SpacePanel extends JPanel
{
  private final int WIDTH = 500, HEIGHT = 500;
  private final int IMAGE_SIZE = 40;
  
  private ImageIcon ship, space;
  private int x, y, moveX, moveY;
  
  public SpacePanel()
  {
    addKeyListener (new MoveListener());
    
    ship = new ImageIcon ("ship.gif");
    space = new ImageIcon ("url.jpg");
    
    x = 250;
    y = 250;
    moveX = moveY = 2;
    
    setPreferredSize (new Dimension (WIDTH, HEIGHT));
    //page.setBackground (space);
    setFocusable (true);
  }
  
  public void paintComponent (Graphics page)
  {
    super.paintComponent (page);
    ship.paintIcon (this, page, x, y);
    ship.paintIcon (this, page, x - WIDTH, y);
    ship.paintIcon (this, page, x - WIDTH, y - HEIGHT);
    ship.paintIcon (this, page, x, y - HEIGHT);
    
    x += moveX;
    y += moveY;
    
    if (x == 0)
      x += WIDTH;
    x = x % WIDTH;
    if (y == 0)
      y += HEIGHT;
    y = y % HEIGHT;
  }
  
  private class MoveListener implements KeyListener
  {
    public void keyPressed (KeyEvent event){}
    
    public void keyReleased (KeyEvent event)
    {
      switch (event.getKeyCode())
      {  
        case KeyEvent.VK_UP:
          moveY += -1;
          repaint();
          break;
        case KeyEvent.VK_DOWN:
          moveY += 1;
          repaint();
          break;
        case KeyEvent.VK_LEFT:
          moveX += 1;
          repaint();
          break;
        case KeyEvent.VK_RIGHT:
          moveX += -1;
          repaint();
          break;
          
          
      }
    }

	@Override
	public void keyTyped(KeyEvent arg0) {
		// empty method body
		
	}
  }
}

 




Was This Post Helpful? 1
  • +
  • -

#4 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,020
  • Joined: 12-October 11

Re: getting a keylistener and keytyped error, please help

Posted 04 December 2012 - 08:06 PM

You are getting the error because whenever you implement an interface, you MUST declare every method in your class (unless is it abstract). An interface is a contract for a class: "If you implement me, you must be able to do something for each of my methods" Usually, you want to do something meaningful for all the methods.

Your MoveListener class IS A KeyListener and should be able to be placed wherever you can put a KeyListener. So if I say give me a KeyListener, and you give me your MoveListener, I should be able to call keyTyped() on your MoveListener, because all KeyListeners have that method. In the case of abstract classes, you don't need to declare it because an abstract class can never be instantiated. But the subclass of your abstract class must declare it. In short, abstract classes which don't implement the interface fully are passing the buck to their subclass.

In the case of a KeyListener, doing nothing IS meaningful, so putting an empty body there is saying "when the user types a key, don't do anything". That is what Takwas did for you.
Was This Post Helpful? 3
  • +
  • -

#5 Amadrid064  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 03-October 12

Re: getting a keylistener and keytyped error, please help

Posted 05 December 2012 - 02:48 AM

Thank you for the help this code is the last one of my semester and its a fun one
Was This Post Helpful? 0
  • +
  • -

#6 Amadrid064  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 03-October 12

Re: getting a keylistener and keytyped error, please help

Posted 05 December 2012 - 03:09 AM

lol now if only I can get my ship to show up and test the KeyEvents.

View PostSheph, on 04 December 2012 - 08:06 PM, said:

You are getting the error because whenever you implement an interface, you MUST declare every method in your class (unless is it abstract). An interface is a contract for a class: "If you implement me, you must be able to do something for each of my methods" Usually, you want to do something meaningful for all the methods.

Your MoveListener class IS A KeyListener and should be able to be placed wherever you can put a KeyListener. So if I say give me a KeyListener, and you give me your MoveListener, I should be able to call keyTyped() on your MoveListener, because all KeyListeners have that method. In the case of abstract classes, you don't need to declare it because an abstract class can never be instantiated. But the subclass of your abstract class must declare it. In short, abstract classes which don't implement the interface fully are passing the buck to their subclass.

In the case of a KeyListener, doing nothing IS meaningful, so putting an empty body there is saying "when the user types a key, don't do anything". That is what Takwas did for you.



Can't believe I forgot my keyTyped() thank you for pointing that out.
Was This Post Helpful? 0
  • +
  • -

#7 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1390
  • View blog
  • Posts: 3,075
  • Joined: 05-April 11

Re: getting a keylistener and keytyped error, please help

Posted 05 December 2012 - 03:20 AM

So you move faster if you repeatedly push the same arrow key over and over

Also you will move super fast if you resize the window at the same time ^^ bad idea to move your spaceship in your paintComponent method
Was This Post Helpful? 0
  • +
  • -

#8 Amadrid064  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 03-October 12

Re: getting a keylistener and keytyped error, please help

Posted 05 December 2012 - 03:51 AM

Yeah I have my speed set to a 2 value right now and that will be one of the last things I tweak after I got it up and running.
Was This Post Helpful? 0
  • +
  • -

#9 Amadrid064  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 03-October 12

Re: getting a keylistener and keytyped error, please help

Posted 05 December 2012 - 04:13 AM

So I fixed my Keylistener issue now what I can't seem to figure out is why my space ship isn't showing up. Any suggestions?

[/
//*Armando Madrid*//
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SpacePanel extends JPanel
{
private final int WIDTH = 500, HEIGHT = 500;
private final int IMAGE_SIZE = 40;

private ImageIcon ship, space, graphic;
private int x, y, moveX, moveY;

public SpacePanel()
{
addKeyListener (new MoveListener());

ship = new ImageIcon ("ship.gif");
space = new ImageIcon ("url.jpg");
graphic = ship;

x = 250;
y = 250;
moveX = moveY = 1;

setPreferredSize (new Dimension (WIDTH, HEIGHT));
setBackground (Color.black);
setFocusable (true);
}

public void paintComponent (Graphics page)
{
super.paintComponent (page);
ship.paintIcon (this, page, x, y);
ship.paintIcon (this, page, x - WIDTH, y);
ship.paintIcon (this, page, x - WIDTH, y - HEIGHT);
ship.paintIcon (this, page, x, y - HEIGHT);

if (x == 0)
x += WIDTH;
x = x % WIDTH;
if (y == 0)
y += HEIGHT;
y = y % HEIGHT;
}

private class MoveListener implements KeyListener
{
public void keyPressed (KeyEvent event)
{
switch (event.getKeyCode())
{
case KeyEvent.VK_UP:
graphic = ship;
moveY += -1;
break;
case KeyEvent.VK_DOWN:
graphic = ship;
moveY += 1;
break;
case KeyEvent.VK_LEFT:
graphic = ship;
moveX += 1;
break;
case KeyEvent.VK_RIGHT:
graphic = ship;
moveX += -1;
break;

}
repaint();
}
public void keyTyped (KeyEvent event) {}
public void keyReleased (KeyEvent event) {}
}
}]
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1