Fixing Abstract Method Error in Java Shooter Game

  • Java
  • Thread starter the other guy
  • Start date
  • Tags
    Java
In summary, The conversation is about a person struggling to build a simple shooter game and facing multiple errors in their code. They mention using jdk1.6.0_20 and having their code set up in a jdk folder. The conversation also includes the code for the game as well as the Bullet and Player classes. The expert summarizer points out that the issue may be that the Shooter class is not overriding the method keyPressed from the KeyListener interface. They also suggest using [code]...[/code] tags to properly format the source code.
  • #1
the other guy
19
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
  • #2
//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);
}
}
 
  • #3
//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();
}

}
 
  • #4
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 [code]...[/code] tags to format your source code properly.
 
  • #5
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 [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
 
  • #6
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 [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.
 
  • #7
You're not paying attention to what you have actually written -- you have a typo.
 
  • #8
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()
 
  • #9
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.
 

What is the silly Java problem?

The silly Java problem is a programming issue that involves finding a solution using the Java programming language. It may involve a specific error or bug that needs to be fixed.

Why is it called a "silly" problem?

The term "silly" is often used to describe a problem that may seem trivial or simple, but can be difficult to solve. It does not necessarily mean that the problem is not important or valuable to solve.

What kind of help is needed for the silly Java problem?

The type of help needed for the silly Java problem may vary. It could involve someone with knowledge and experience in Java programming to provide guidance or assistance in finding a solution. It could also involve online resources or forums for troubleshooting and getting feedback from others.

How can I approach solving the silly Java problem?

There are a few different approaches you can take when trying to solve the silly Java problem. You can try to break down the problem into smaller parts and tackle each one individually. You can also do research and look for similar problems and solutions that have been posted online. Additionally, seeking help from others and asking for advice can also be helpful in finding a solution.

What can I do if I can't find a solution to the silly Java problem?

If you are struggling to find a solution to the silly Java problem, don't give up. You can try reaching out to other programmers or experts in the field for assistance. You can also take a break from the problem and come back to it with a fresh perspective. Sometimes, stepping away from the problem can help you think of new ideas or approaches to try. Additionally, seeking help from online communities or forums can also provide valuable insights and suggestions for solving the problem.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Programming and Computer Science
Replies
4
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
11
Views
9K
Back
Top