Java Fixing Abstract Method Error in Java Shooter Game

  • Thread starter Thread starter the other guy
  • Start date Start date
  • Tags Tags
    Java
AI Thread Summary
The discussion centers on troubleshooting a Java program for a simple shooter game, where the user encounters multiple compilation errors. The primary issue stems from the class `Shooter` not implementing the required `keyPressed` method from the `KeyListener` interface, leading to an error indicating that `Shooter` is not abstract. The user resolves this by correcting a typo in the method name from "keypressed" to "keyPressed."Subsequent errors include "cannot find symbol" related to the `Player` class constructor and an undefined method `getImage()`. The user learns that the `Player` constructor does not match the provided arguments, which leads to further clarification on the need for proper constructor definitions. After addressing these issues, the user successfully compiles the program and expresses a desire to enhance gameplay by allowing each player to shoot multiple shots.
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
Views
3K
Replies
5
Views
3K
Replies
12
Views
3K
Replies
4
Views
4K
Replies
11
Views
9K
Back
Top