Fixing Abstract Method Error in Java Shooter Game

  • Context: Java 
  • Thread starter Thread starter the other guy
  • Start date Start date
  • Tags Tags
    Java
Click For Summary

Discussion Overview

The discussion revolves around resolving an "Abstract Method Error" encountered while developing a simple shooter game in Java. Participants are sharing code snippets and seeking assistance with specific errors related to implementing the KeyListener interface and constructor issues in their Player class.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant describes encountering an error indicating that the Shooter class does not override the abstract method keyPressed from KeyListener.
  • Another participant suggests that the issue may stem from not defining the keyPressed method correctly, emphasizing the need for attention to method signatures.
  • There are mentions of additional errors related to constructor calls for the Player class, specifically regarding the expected parameters.
  • Some participants point out typographical errors in method names, indicating that case sensitivity in Java is crucial.
  • One participant expresses uncertainty about the proper implementation of the keyPressed method and seeks clarification on its structure.
  • Another participant acknowledges a typo related to the method name and indicates they will continue to address other errors.

Areas of Agreement / Disagreement

Participants generally agree on the nature of the errors being related to method definitions and constructor parameters, but there is no consensus on the complete resolution of the issues, as multiple errors remain unresolved.

Contextual Notes

Participants have noted that some errors are cascading from the initial abstract method error, and there are unresolved issues related to constructor definitions and method implementations that depend on the specific class designs.

Who May Find This Useful

Readers interested in Java programming, particularly those working on game development or dealing with similar abstract method errors and constructor issues in their code.

the other guy
Messages
19
Reaction score
0
I've been doing homework all day. I can't seem to figure out how to fix this. I dunno...maybe I'm depressed.

So, in my attempt to build a simple shooter (kinda like pong, only you shoot at each other), It seems all my problems root from this error;

Shooter.java:9: Shooter is not abstract and does not override abstract method keyPressed(java.awt.event.KeyEvent) in java.awt.event.KeyListener

public class Shooter extends JFrame implements KeyListener{

every error after that is a cannot find symbol.

5 errors in total, but the last 4 are related to this problem.
Here is my setup
jdk1.6.0_20
I currently have it in the jdk folder (I work out of there), in a manner that looks like this
jdk/Shooter/classes/ (this is where Shooter.java, Bullet.java, and Player.java are)
jdk/Shooter/classes/images/ (this is where images LEFT.gif and RIGHT.gif are stored)
everything is located in its standard c:\program files\java\jdk

Here is my code

//Base
//main class
import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.awt.event.*;

public class Shooter extends JFrame implements KeyListener{
Image img;
Graphics dbi;

boolean u,d,w,s;
int S;
int E;

Player p1=new Player(50,200, Color.RED,"Images,LEFT.gif");
Player p2=new Player(550,200, Color.BLUE,"Images,RIGHT.gif");
ArrayList<Bullets>b=new ArrayList<Bullets>();
public Shooter(){
setTitle("Shooter");
setSize(600,400);
setResizable(false);
setBackground(Color.BLACK);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addKeyListener(this);
u=d=w=s=false;
S=E=0;

setVisible(true);
}
public void paint(Graphics g){
img=createImage(getWidth(), getHeight());
dbi=img.getGraphics();


paintComponent(dbi);
g.drawImage(img,0,0,this);
repaint();

}
public void paintComponent(Graphics g){
if(p1.health>0&&p2.health>0){
for(Bullets bl:b){//new for loop
b1.draw(g);
}
update();
}
else{
if(p1.health<=0){
g.setColor(p2.col);
g.drawString("PLAYER 2 WINS",250,190);
}
else{
g.setColor(p1.col);
g.drawString("PLAYER 1 WINS",250,190);
}
}
p1.draw(g);
p2.draw(g);
}
public void update(){
if(w&&p1.y>24)p1.moveUp();
if(s&&p1.y<347)p1.moveDown();
if(w&&p2.y>24)p1.moveUp();
if(s&&p2.y<347)p1.moveDown();
if(E==1){
Bullets add=p2.getBull();
add.xVel=-3;
b.add(add);
E++;
}
if(S==1){
Bullets add=p1.getBull();
add.xVel=-3;
b.add(add);
S++;
}
for(int x=0;x<b.size();x++){
b.get(x).move();
if(b.get(x).rect.intersects(p1.rect)&&b.get(x).xVel<0){
p1.health--;
b.remove(x);
x--;
continue;
}
if(b.get(x).rect.intersects(p2.rect)&&b.get(x).xVel>0){
p2.health--;
b.remove(x);
x--;
continue;
}
}
}

public void KeyTyped(KeyEvent e){}
public void KeyPressed(KeyEvent e){
switch(e.getKeyCode()){
case KeyEvent.VK_UP:u=true;break;
case KeyEvent.VK_DOWN:d=true;break;
case KeyEvent.VK_W:w=true;break;
case KeyEvent.VK_S:s=true;break;

case KeyEvent.VK_SPACE:S++;break;
case KeyEvent.VK_ENTER:E++;break;
}
}
public void keyReleased(KeyEvent e){
switch(e.getKeyCode()){
case KeyEvent.VK_UP:u=false;
case KeyEvent.VK_DOWN:d=false;
case KeyEvent.VK_W:w=false;
case KeyEvent.VK_S:s=false;

case KeyEvent.VK_SPACE:S++;break;
case KeyEvent.VK_ENTER:E++;break;

}
}

public static void main(String[]beans){
KeyListener s=new Shooter();
}
}

If anyone can help me get this working it would be uber appreciated. This is just the backbone, I am going to be expanding from this.

if anyone needs the contents of Bullet.java and Player.java, they can be found in the next posts
 
Last edited:
Technology news on Phys.org
//Bullets Class

import java.awt.*;

public class Bullets{
int x,y;
int xVel;
int height, width;
Rectangle rect;
Color col;

public Bullets(){
x=y=height=width=0;
col=Color.WHITE;
rect=new Rectangle (x,y,width,height);
}
public Bullets (int x, int y, int wd, int ht, Color c){
this.x=x;
this.y=y;
this.xVel=0;
height=ht;
width=wd;
col=c;
rect=new Rectangle(x,y,width,height);
}
public void draw(Graphics g){
g.setColor(col);
g.fillOval(x,y,width,height);
}
public void move(){
x+=xVel;
rect.setLocation(x,y);
}
}
 
//Player Class
import java.awt.*;
public class Player{
int x,y;
int height,width;
int health;
Image img;
Rectangle rect;
Color col;

public Player(){
x=y=height=width=0;
img=null;
health=10;
rect=new Rectangle (x,y,width,height);
}
public Player (int x, int y){
this.x=x;
this.y=y;
height=width=0;
img=null;
health=10;
rect=new Rectangle (x,y,width,height);
col=Color.WHITE;
}
public Player (int x, int y, int ht, int wd){
this.x=x;
this.y=y;
height=ht;
width=wd;
img=null;
health=10;
rect=new Rectangle (x,y,width,height);
col=Color.WHITE;
}
public Player (int x, int y, int ht, int wd, String s){
this.x=x;
this.y=y;
height=ht;
width=wd;
img=Toolkit.getDefaultToolkit().getImage(s);
health=10;
rect=new Rectangle (x,y,width,height);
col=Color.WHITE;
}
public void draw(Graphics g){
g.drawImage(img,x,y,null);
}









//methods
public void moveUp(){
y-=3;
rect.setLocation(x,y);
}
public void moveDown(){
y+=3;
rect.setLocation(x,y);

}
public Bullets getBull(){
return new Bullets(x+8,y+23,3,3,col);
}

public void setImage(String s){
img=Toolkit.getDefaultToolkit().getImage();
}

}
 
the other guy said:
Shooter.java:9: Shooter is not abstract and does not override abstract method keyPressed(java.awt.event.KeyEvent) in java.awt.event.KeyListener

Assuming that Shooter is not supposed to be abstract and is supposed to implement KeyListener, your problem is probably that you haven't defined a method called keyPressed that takes a single argument which is of type java.awt.event.KeyEvent.


P.S. Before you object, pay attention to what you wrote...

P.P.S. use [[/color]code]...[/code] tags to format your source code properly.
 
Hurkyl said:
Assuming that Shooter is not supposed to be abstract, your problem is probably that you haven't defined a method called keyPressed that takes a single argument which is of type java.awt.event.KeyEvent.


P.S. Before you object, pay attention to what you wrote...

P.P.S. use [[/color]code]...[/code] tags to format your source code properly.



Hmm, I figured that was already in the JDK. Let me give that a shot.

no objections yet btw- much appreciated. I am trying it now
 
Hurkyl said:
Assuming that Shooter is not supposed to be abstract and is supposed to implement KeyListener, your problem is probably that you haven't defined a method called keyPressed that takes a single argument which is of type java.awt.event.KeyEvent.P.S. Before you object, pay attention to what you wrote...

P.P.S. use [[/color]code]...[/code] tags to format your source code properly.

something like

public method keypressed(){
java.awt.event.KeyEvent;
}
?
sorry, let me try that again, lol. that's not it.
 
You're not paying attention to what you have actually written -- you have a typo.
 
Hurkyl said:
You're not paying attention to what you have actually written -- you have a typo.

got it with the small case K.

thanks man. I'm going to sleep as soon as I wrap up the other 4 errors which don't seem to have systemic from the "k" problem.

cant find symbol errors of
symbol : constructor Player(int,int,int,int,java.awt.Color,java.lang.String)

from the

Player p1=new Player(50,200,10,50,Color.RED,"Images,LEFT.gif");
Player p2=new Player(550,200,10,50,Color.BLUE,"Images,RIGHT.gif");

part of the code, the second prob coming from a can't find symbol error of

symbol : method getImage()
 
the other guy said:
cant find symbol errors of
symbol : constructor Player(int,int,int,int,java.awt.Color,java.lang.String)
Well, again stating the obvious, this error is telling you that you don't have a constructor for the class Player that takes those arguments.


part of the code, the second prob coming from a can't find symbol error of

symbol : method getImage()
This error is telling you some object (I'm guessing the object returned by Toolkit.getDefaultToolkit(), since that's the only place you invoke getImage with no arguments) doesn't actually have a member getImage that takes no arguments.
 
  • #10
thanks man, youve been such a great help. I've got it running now...now if i can only get each player to shoot more than one shot ill be good, lol.
 

Similar threads

  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 12 ·
Replies
12
Views
4K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 11 ·
Replies
11
Views
10K