Link to home
Start Free TrialLog in
Avatar of gupisone
gupisoneFlag for United Arab Emirates

asked on

Cannongame error

I have this famous code of cannongame.java ,
but when I try to run this code I have an error saying :
"ava.lang.ArrayIndexOutOfBoundsException
        at CannonGame.main(CannonGame.java:8)"

I hope that I will get help.

The code is:


import java.awt.*;


class CannonGame extends Frame {

        public static void main (String [ ] args)
        {
                CannonGame world = new CannonGame (Integer.valueOf(args[0]));
                world.show ();
        }

        public static final int FrameWidth = 600;
        public static final int FrameHeight = 400;

        private int angle = 45;
        private String message = "";
        private CannonBall cannonBall = null;
        private static final int barrelLength = 30;
        private static final int barrelWidth = 10;

        public CannonGame (Integer theta)
        {
                       
                setSize (FrameWidth, FrameHeight);
                setTitle ("Cannon Game");
                       
                angle = theta.intValue();
                message = "Angle = " + angle;
                       
                double radianAngle = angle * Math.PI / 180.0;
                double sinAngle = Math.sin(radianAngle);
                double cosAngle = Math.cos(radianAngle);
                double initialVelocity = 12;
                       
                cannonBall = new CannonBall (
                        20 + (int) (barrelLength * cosAngle),
                        dy(5+(int) (barrelLength * sinAngle)),
                        5, 12 * cosAngle, -12 * sinAngle);
       
}
        public static int dy (int y) {  return FrameHeight - y; }

        public void paint (Graphics g)
        {
                int x = 20;    
                int y = 5;
                double radianAngle = angle * Math.PI / 180.0;
                int lv = (int) (barrelLength * Math.sin(radianAngle));
                int lh = (int) (barrelLength * Math.cos(radianAngle));
                int sv = (int) (barrelWidth * Math.sin(radianAngle + Math.PI/2));
                int sh = (int) (barrelWidth * Math.cos(radianAngle + Math.PI/2));
                       
                g.setColor(Color.green);
                g.drawLine(x, dy(y), x+lh, dy(y+lv));
                g.drawLine(x+lh, dy(y+lv), x+lh+sh, dy(y+lv+sv));
                g.drawLine(x+lh+sh, dy(y+lv+sv), x+sh, dy(y+sv));
                g.drawLine(x+sh, dy(y+sv), x, dy(y));
                g.drawOval(x-8, dy(y+10), 12, 12);
                       
                g.setColor(Color.red);
                g.fillRoundRect(FrameWidth-100, dy(12), 50, 10, 6, 6);
                       
                if (cannonBall != null) {
                        cannonBall.move();
                        cannonBall.paint(g);
                        try {
                                Thread.sleep(20);
                                } catch(InterruptedException e) { }
                        if (dy(cannonBall.y()) > 0)
                                repaint();
                        else {
                                int targetX = FrameWidth - 100;
                                if ((cannonBall.x() > targetX) && 
                                (cannonBall.x() < (targetX + 50)))
                                        message = "You Hit It!";
                                else
                                        message = "Missed!";
                                cannonBall = null;
                                }
                        }
                       
                g.drawString(message, FrameWidth/2, FrameHeight/2);
     }
     
     public class Ball
{
   protected Rectangle location;
   protected double    dx;
   protected double    dy;
   protected Color     color;

   public Ball (int x, int y, int r)
   {
      location = new Rectangle(x - r, y - r, 2 * r, 2 * r);
      dx       = 0;
      dy       = 0;
      color    = Color.blue;
   }

   
   public void setColor (Color newColor)
   {
      color = newColor;
   }

   public void setMotion (double ndx, double ndy)
   {
      dx = ndx;
      dy = ndy;
   }

   
   public int radius ()
   {
      return location.width / 2;
   }

   public int x ()
   {
      return location.x + radius();
   }

   public int y ()
   {
      return location.y + radius();
   }

   public double xMotion ()
   {
      return dx;
   }

   public double yMotion ()
   {
      return dy;
   }

   public Rectangle region ()
   {
      return location;
   }

   
   public void moveTo (int x, int y)
   {
      location.setLocation(x, y);
   }

   public void move ()
   {
      location.translate(( int ) dx, ( int ) dy);
   }

   public void paint (Graphics g)
   {
      g.setColor(color);
      g.fillOval(location.x, location.y, location.width, location.height);
   }
}
public class CannonBall extends Ball
{
   public CannonBall (int sx, int sy, int r, double dx, double dy)
   {
      super(sx, sy, r);

      setMotion(dx, dy);
   }

   public void move ()
   {
      dy = dy + 0.3;

     
      super.move();
     
   }
}

  }


Avatar of TimYates
TimYates
Flag of United Kingdom of Great Britain and Northern Ireland image

CannonGame world = new CannonGame (Integer.valueOf(args[0]));

you have to pass it an integer as a prameter:

java CannonGame 4

for example
Make sure you check for no args
Changing main to:

        public static void main (String [ ] args)
        {
                if( args.length < 1 )
                {
                      System.out.println( "You need to pass theta as an integer parameter" ) ;
                      System.exit( -1 ) ;
                }
                CannonGame world = new CannonGame (Integer.valueOf(args[0]));
                world.show ();
        }

will make it a bit more robust :-)
if(args.length < 1) {
      System.out.println("Usage: java CannonGame <number>");
      System.exit(-1);
}
(CEHJ has a much more useful message printed out...mine's a bit cryptic ;-))
Actually it should really be

if(args.length < 1) {
     System.err.println("Usage: java CannonGame <number>");
     System.exit(-1);
}
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
:-( :-( :-(