Solved

Cannongame    error

Posted on 2003-11-26
9
311 Views
Last Modified: 2006-11-17
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();
     
   }
}

  }


0
Comment
Question by:gupisone
  • 4
  • 3
  • 2
9 Comments
 
LVL 35

Expert Comment

by:TimYates
ID: 9825636
CannonGame world = new CannonGame (Integer.valueOf(args[0]));

you have to pass it an integer as a prameter:

java CannonGame 4

for example
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9825642
Make sure you check for no args
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9825647
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 :-)
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 86

Expert Comment

by:CEHJ
ID: 9825652
if(args.length < 1) {
      System.out.println("Usage: java CannonGame <number>");
      System.exit(-1);
}
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9825662
(CEHJ has a much more useful message printed out...mine's a bit cryptic ;-))
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9825697
Actually it should really be

if(args.length < 1) {
     System.err.println("Usage: java CannonGame <number>");
     System.exit(-1);
}
0
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 9827186
Or you could use a default, (the code appears to already have a default of 45 but never uses it):

        public static void main (String [ ] args)
        {
                Integer theta = new Integer(45);
                if (args.length>0)
                {
                   theta = Integer.valueOf(args[0]);
                }
                CannonGame world = new CannonGame (theta);
                world.show ();
        }
0
 
LVL 92

Expert Comment

by:objects
ID: 9841683
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9848744
:-( :-( :-(
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question