Question

I am trying to have a box appear with the input of the velocity and angle, as well as showing the number of times the ball has been fired. It is giving me a runtime error and I'm not sure why. The piece that is causing the error is JOptionPane.showInternalMessageDialog

import java.awt.Graphics2D;

import javax.swing.JFrame;
import javax.swing.JOptionPane;


public class CannonGame {

    private static final int WIDTH=800;
    private static final int HEIGHT=600;
    private static final int WAIT_TIME=10;

    public static void main(String[] args) {
        JFrame frame=new JFrame("Cannon Game");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(WIDTH,HEIGHT);
        MyComponent comp=new MyComponent();
        frame.add(comp);
        frame.setVisible(true);
        comp.initializeLocations();
        frame.repaint();
        boolean cont = true;
        //prompt user for angle and velocity

        while(cont){

            String vel=JOptionPane.showInputDialog(frame,"Enter the Velocity (1-100)");
            if(vel == null)
                System.exit(0);
            Double velocity=Double.parseDouble(vel);

            while(velocity < 0 || velocity > 100){
                String vel1 = JOptionPane.showInputDialog(frame,"Enter the Velocity (1-100)");
                if(vel == null)
                    System.exit(0);
                Double velocity1 = Double.parseDouble(vel1);
                velocity = velocity1;
            }

            String ang=JOptionPane.showInputDialog(frame,"Enter the angle (0-90)");
            if(ang == null)
                System.exit(0);
            Double angle=Math.PI*Double.parseDouble(ang)/180.0;

            while(angle < 0 || angle > 1.57111111111111){
                String ang1=JOptionPane.showInputDialog(frame,"Enter the angle (0-90)");
                if(ang == null)
                    System.exit(0);
                Double angle1=Math.PI*Double.parseDouble(ang1)/180.0;
                angle = angle1;
            }


            JOptionPane.showInternalMessageDialog(frame, "Balls fired: " + comp.getNumBallsFired(),
                    "velocity: " + velocity + "angle: " + angle, JOptionPane.INFORMATION_MESSAGE);

            //animate the cannonball until it hits the ground
            comp.getReadyForShot(angle,velocity);
            while(comp.stillInFlight()) {
                comp.update(System.currentTimeMillis());
                frame.repaint();
                try {
                    Thread.sleep(WAIT_TIME);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

            //print how many tries and if they hit it
            if(comp.isTargetHit()){
                JOptionPane.showMessageDialog(frame, "You fired "+comp.getNumBallsFired()+" times\n and you "+
                        (comp.isTargetHit()?"":"did not")+" hit the target!");

                int answer = JOptionPane.showConfirmDialog(null,
                        "Would you like to fire again?", "choose one", JOptionPane.YES_NO_OPTION);

                if(answer == JOptionPane.YES_OPTION){
                    cont = true;
                    comp.initializeLocations();
                }
                else {
                    cont = false;
                    System.exit(0);
                }
            }else{
                JOptionPane.showMessageDialog(frame, "You fired "+comp.getNumBallsFired()+" times\n and you "+
                        (comp.isTargetHit()?"":"did not")+" hit the target!");

                int answer = JOptionPane.showConfirmDialog(null,
                        "Would you like to fire again?", "choose one", JOptionPane.YES_NO_OPTION);

                if(answer == JOptionPane.YES_OPTION){
                    cont = true;
                }
                else {
                    cont = false;
                    System.exit(0);
                }
            }

        }

    }
}
Was it helpful?

Solution 2

You have to pass the Content Pane of your JFrame as the first parameter in the method not just the JFrame like you are doing now. So you need to call your message statement like this (notice that frame.getContentPane() is used, not just frame):

JOptionPane.showMessageDialog(frame.getContentPane(), "You fired "+comp.getNumBallsFired()+" times\n and you "+
                    (comp.isTargetHit()?"":"did not")+" hit the target!");

Reference: Here.

OTHER TIPS

As per the API, You're supposed to use showInternalMessageDialog's with JInternalFrames, and you're not doing this. Solution: don't use this JOptionPane but rather use one of the other more appropriate ones such as showMessageDialog.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top