- #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
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: