Solved

Cannongame    error

Posted on 2003-11-26
9
309 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
 
LVL 86

Expert Comment

by:CEHJ
ID: 9825652
if(args.length < 1) {
      System.out.println("Usage: java CannonGame <number>");
      System.exit(-1);
}
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java Loop 6 48
session migration servlets 2 23
nextBoolean(double p) for Random class 3 35
reverse digits of a number using for loop 5 36
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This video teaches viewers about errors in exception handling.

939 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

11 Experts available now in Live!

Get 1:1 Help Now