Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Cannongame    error

Posted on 2003-11-26
9
Medium Priority
?
316 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 2000 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

610 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