Solved

Cannongame    error

Posted on 2003-11-26
9
308 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
Comment Utility
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
Comment Utility
Make sure you check for no args
0
 
LVL 35

Expert Comment

by:TimYates
Comment Utility
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
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
if(args.length < 1) {
      System.out.println("Usage: java CannonGame <number>");
      System.exit(-1);
}
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 35

Expert Comment

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

Expert Comment

by:CEHJ
Comment Utility
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
Comment Utility
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
Comment Utility
0
 
LVL 35

Expert Comment

by:TimYates
Comment Utility
:-( :-( :-(
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now