Results 1 to 10 of 10
Like Tree1Likes
  • 1 Post By quad64bit

Thread: Why do I keep getting these errors?

  1. #1
    psx2514 is offline Member
    Join Date
    Oct 2012
    Posts
    50
    Rep Power
    0

    Default Why do I keep getting these errors?

    Lines 95 and 97. I can't figure it out. It says cannot find symbol: b1 (for line 95) and b2 (for line 97), but I initialized those variables. What's wrong?
    Java Code:
    import java.applet.Applet;
    import java.awt.*;
    import java.awt.event.*;
    import java.applet.AudioClip;
    import java.awt.Button;
      
    public class SpaceShip2 extends Applet
      implements MouseListener, MouseMotionListener {
      private final int APPLET_WIDTH = 200;
      private final int APPLET_HEIGHT = 200;
      private Point point1 = null;
      private Point point2 = null;
      private Point point3 = null;
    
      private int shots_fired = 0;
      private int laser_hits = 0;
      private int x = 0;
      private int y = 0;
      private boolean laser_fired = false;
      private boolean soundon = false;
      private AudioClip audio;
      private Button b1 = new Button("Sound off");
      private Button b2 = new Button("reset");
      
       public SpaceShip2()
      {
        
        this.point1 = null;
        this.point2 = null;
        this.point3 = null;
        this.laser_fired = false;
        this.soundon = false;
        this.shots_fired = 0;
        
      }
       
     public void drawShip (Graphics g) {
        g.setColor(Color.orange);
        if ((this.point1 != null) && (this.point2 != null))
        {
          g.fillOval(this.point2.x - 10, this.point2.y - 10, 70, 10);
          g.setColor(Color.red);
          g.fillRect(this.point2.x + 22, this.point2.y - 17, 5, 7);
        }
      
      
      if ((this.point2 != null) && (this.point3 != null)) {
          int R = (int) (1 + Math.random()*255);
          int G = (int)(1 + Math.random()*255);
          int B = (int)(1 + Math.random()*255);
          Color randomColor = new Color(R, G, B);
          
          switch (this.shots_fired) {
          case 0:
            g.setColor(randomColor);
            this.x = 0; 
            this.y = 0;
            break;
          case 1:
            g.setColor(randomColor);
            this.x = 200; this.y = 0;
            break;
          case 2:
            g.setColor(randomColor);
            this.x = 100; this.y = 200;
            break;
          case 3:
            g.setColor(randomColor);
            this.x = 200; this.y = 200;
            break;
          case 4:
            g.setColor(randomColor);
            this.x = 100; this.y = 200;
            break;
          default:
            this.x = 0; this.y = 0;
          }
          
          if (this.laser_fired == true) {
            this.laser_fired = false;
            g.drawLine(this.point3.x + 25, this.point3.y - 17, this.x, this.y);
            if (this.soundon == true)
              this.audio.play();
          }
      }
    }
    
    public void init()
      {
        this.b1 = new Button("Sound off");
        this.b2 = new Button("Reset shot count");
        addMouseListener(this);
        addMouseMotionListener(this);
    
        this.b1.addActionListener(new SpaceShip2.b1(this));
        add(this.b1);
        this.b2.addActionListener(new SpaceShip2.b2(this));
        add(this.b2);
    
        this.audio = getAudioClip(getCodeBase(), "bonk.au");
        setBackground(Color.black);
        setSize(200, 200);
      }
    
    public void mouseClicked(MouseEvent MouseE)
      {
      }
    
      public void mouseDragged(MouseEvent MouseE)
      {
      }
    
      public void mouseEntered(MouseEvent MouseE)
      {
        this.point1 = MouseE.getPoint();
      }
    
      public void mouseExited(MouseEvent MouseE)
      {
      }
    
      public void mouseMoved(MouseEvent MouseE) {
        this.point2 = MouseE.getPoint();
        repaint();
      }
    
      public void mousePressed(MouseEvent MouseE)
      {
        this.point3 = MouseE.getPoint();
        this.laser_fired = true;
        this.laser_hits += 1;
    
        if (this.shots_fired >= 5)
          this.shots_fired = 0;
        else
          this.shots_fired += 1;
        repaint();
      }
    
      public void mouseReleased(MouseEvent MouseE)
      {
      }
    
      public void paint(Graphics g)
      {
        if (this.soundon == false) {
          this.b1.setLabel("sound on");
        }
        else {
          this.b1.setLabel("sound off");
        }
    
        g.setColor(Color.white);
        String str = " " + this.laser_hits;
        g.drawString("Shots fired" + 
          str, 5, 195);
    
        drawShip(g); 
      
    
        g.setColor(Color.white);
        str = " " + this.laser_hits;
        g.drawString("Shots" + 
          str, 5, 195);
    
        drawShip(g);
        }
    }

  2. #2
    techiepro is offline Member
    Join Date
    Nov 2012
    Posts
    7
    Rep Power
    0

    Default Re: Why do I keep getting these errors?

    The line --> this.b1.addActionListener(new SpaceShip2.b1(this)); . What is new SpaceShip2.b1(this) ???? . That is causing the problem. Same for the second line.
    Hope this helps.

  3. #3
    psx2514 is offline Member
    Join Date
    Oct 2012
    Posts
    50
    Rep Power
    0

    Default Re: Why do I keep getting these errors?

    Quote Originally Posted by techiepro View Post
    The line --> this.b1.addActionListener(new SpaceShip2.b1(this)); . What is new SpaceShip2.b1(this) ???? . That is causing the problem. Same for the second line.
    Hope this helps.
    Please be more clear. I know that's the line that has the error (I said that). WHY does it have that error? Please look through my entire program before jumping to an answer. Thank you.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default Re: Why do I keep getting these errors?

    There is no SpaceShip2.b1 class.
    There is an instance variable, but that is not the same thing.

    Since your SpaceShip2 object eimplements the various Listeners you are interested in I suspect you just want to pass in 'this'.
    Please do not ask for code as refusal often offends.

  5. #5
    psx2514 is offline Member
    Join Date
    Oct 2012
    Posts
    50
    Rep Power
    0

    Default Re: Why do I keep getting these errors?

    I added these classes:
    Java Code:
    private class b1 implements ActionListener {
       
       public void actionPerformed(ActionEvent e) {
       
      
      }
    
     }
      private class b2 implements ActionListener {
        public void actionPerformed(ActionEvent e) {
      
      
      
      }
    
     }
    Plus I changed the lines with errors to
    Java Code:
     this.b1.addActionListener(new SpaceShip2.b1());
        add(this.b1);
        this.b2.addActionListener(new SpaceShip2.b2());
        add(this.b2);
    I have another problem though. My buttons don't work. The first button is supposed to change from "sound on" to "sound off" when clicked; it doesn't. The second button is supposed to reset the counter to 0 when it's clicked; it doesn't. How do I fix this? Also, when the first button is says "sound off" a sound effect is supposed to play when the laser is shot. Note: "Sound off" means the sound is on, and it's asking to click it if you want the sound to be on.
    Last edited by psx2514; 11-07-2012 at 11:55 AM.

  6. #6
    techiepro is offline Member
    Join Date
    Nov 2012
    Posts
    7
    Rep Power
    0

    Default Re: Why do I keep getting these errors?

    My apologies for not to be spoon feeder.

  7. #7
    psx2514 is offline Member
    Join Date
    Oct 2012
    Posts
    50
    Rep Power
    0

    Default Re: Why do I keep getting these errors?

    Quote Originally Posted by psx2514 View Post
    I added these classes:
    Java Code:
    private class b1 implements ActionListener {
       
       public void actionPerformed(ActionEvent e) {
       
      
      }
    
     }
      private class b2 implements ActionListener {
        public void actionPerformed(ActionEvent e) {
      
      
      
      }
    
     }
    Plus I changed the lines with errors to
    Java Code:
     this.b1.addActionListener(new SpaceShip2.b1());
        add(this.b1);
        this.b2.addActionListener(new SpaceShip2.b2());
        add(this.b2);
    I have another problem though. My buttons don't work. The first button is supposed to change from "sound on" to "sound off" when clicked; it doesn't. The second button is supposed to reset the counter to 0 when it's clicked; it doesn't. How do I fix this? Also, when the first button is says "sound off" a sound effect is supposed to play when the laser is shot. Note: "Sound off" means the sound is on, and it's asking to click it if you want the sound to be on.
    Will someone please just tell me how to fix this, or at least direct me to a forum that WILL tell me how to fix this. This is due in an hour, and I don't have time to figure this out.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default Re: Why do I keep getting these errors?

    Where do you change the soundon flag?
    You're doing nothing at all in the action listeners assigned to the buttons, so nothing is going to happen when you click them.
    Please do not ask for code as refusal often offends.

  9. #9
    psx2514 is offline Member
    Join Date
    Oct 2012
    Posts
    50
    Rep Power
    0

    Default Re: Why do I keep getting these errors?

    Quote Originally Posted by Tolls View Post
    Where do you change the soundon flag?
    You're doing nothing at all in the action listeners assigned to the buttons, so nothing is going to happen when you click them.
    That's what I'm trying to figure out. Look I know I should figure this out on my own blah blah blah, but I've been up all night trying to figure this out. Will you please just tell me how to fix this. If not, please direct me to someone who will. Thank you.

  10. #10
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    6

    Default Re: Why do I keep getting these errors?

    Will you please just tell me how to fix this. If not, please direct me to someone who will. Thank you.
    I was about to help until I saw the attitude you lathered all over your last post. We don't work for you, nor do we have any obligation to help someone that isn't willing to also put effort forth. You think it was easy for us to learn this stuff? Some things are just hard, and if you expect patience from us, then we expect willingness to listen and participate from you.
    DarrylBurke likes this.

Similar Threads

  1. Can't fix my errors!
    By abdul9383 in forum New To Java
    Replies: 5
    Last Post: 11-21-2011, 08:44 PM
  2. Help with errors please...
    By ertopp in forum New To Java
    Replies: 1
    Last Post: 10-08-2011, 04:27 AM
  3. Gui with errors
    By scooter24 in forum New To Java
    Replies: 2
    Last Post: 07-04-2011, 05:29 PM
  4. Replies: 4
    Last Post: 01-04-2011, 12:36 AM
  5. Replies: 3
    Last Post: 03-08-2009, 01:51 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •