Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Silly Java problem needs help

  1. Jun 28, 2010 #1
    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 eachother), 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, im 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: Jun 28, 2010
  2. jcsd
  3. Jun 28, 2010 #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);
    }
    }
     
  4. Jun 28, 2010 #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();
    }

    }
     
  5. Jun 28, 2010 #4

    Hurkyl

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    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.
     
  6. Jun 28, 2010 #5


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

    no objections yet btw- much appreciated. im trying it now
     
  7. Jun 28, 2010 #6
    something like

    public method keypressed(){
    java.awt.event.KeyEvent;
    }
    ?
    sorry, let me try that again, lol. thats not it.
     
  8. Jun 28, 2010 #7

    Hurkyl

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    You're not paying attention to what you have actually written -- you have a typo.
     
  9. Jun 28, 2010 #8
    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 cant find symbol error of

    symbol : method getImage()
     
  10. Jun 28, 2010 #9

    Hurkyl

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    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.


    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.
     
  11. Jun 29, 2010 #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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Silly Java problem needs help
Loading...