0

I just wanted to know why it is that when I use the Frame Application and mix Dialog boxes and Animations, the text boxes are completely cut out.

Here is my code:

/* The "SpaceTravels" class.
   Date: March 2015
   Author Barrington Reid
   Description: This program asks user for identification,
   then will display a spaceship travelling through the night sky using Graphics.
   The spaceship is controlled by a slow setting, a medium setting and a fast setting.
*/
import javax.swing.*;           //Allows JOptionPane to work
import java.awt.*;

public class SpaceTravels extends JFrame
{
    ImageIcon backGnd, rckt;

    int x = 0;                //Setting the x variable as 0
    int y = 600;              //Setting the y variable as 600
    int xSpeed = 10;          //Setting the xSpeed as 10
    int ySpeed = 10;          //Setting the ySpeed as 10
    int number = 0;           //stores the verification number


    public SpaceTravels ()
    {
        super ("SpaceTravels");  // Set the frame's name
        setSize (1360, 730);     // Set the frame's size
        backGnd = new ImageIcon ("night.gif");    //Night background
        rckt = new ImageIcon ("orangerocket.png");       //Rocket
        setDefaultCloseOperation (EXIT_ON_CLOSE);       //Close the program by clicking exit
        setVisible (true);       // Show the frame
    } // Constructor


    public void paint (Graphics g)
    {
        for (int i = 0 ; i < 1000000 ; i = i + 1)
        {
            backGnd.paintIcon (this, g, 0, 0);
            rckt.paintIcon (this, g, x, y);

            y = y + ySpeed;

            for (int j = 0 ; j < 1000000 ; j = j + 1)
            {
                double k = 1;
                Math.pow (k, 2);
            }
            if ((y > 600) || (y < 50))
            {
                ySpeed = ySpeed * -1;
            }


            for (int k = 0 ; k < 1000000 ; k = k + 1)
            {
                double l = 1;
                Math.pow (l, 2);
            }
            if ((x > 600 || (x < 50))
            {
                xSpeed = xSpeed * -1;
            }
        }
    } // paint method


    public static void main (String[] args)
    {
        new SpaceTravels ();    // Create a SpaceTravels frame
        String name;            //Stores names
        int tries = 0;          //Stores the amount of tries
        String verCode = "1234567";                         //Verification Code is 1234567
        String code = "1234567";                            //Verification Code is 1234567

        JOptionPane.showMessageDialog (null, "Hello, and welcome to the Canadian Space Agency's Spacecraft Control and Simulation Program!"); //Welcome
        JOptionPane.showMessageDialog (null, "Your verification code is 1234567.");             //This is the verification code

        name = JOptionPane.showInputDialog (null, "For safety purposes, please enter your name");           //Asking for user name
        verCode = JOptionPane.showInputDialog (null, "Now, " + name + ", please enter your verification code."); //Enter the verification code: 1234567

        while ((verCode.equalsIgnoreCase ("1234567") != true) && (tries < 3))                      //3 tries to get this correct
        {
            verCode = JOptionPane.showInputDialog (null, "You have entered the wrong verification code, please try again.");
            tries = tries + 1;          //Tries goes up once each time this loop occurs.
        }

        if (code.equals (verCode))
        {
            String[] options = new String[]  //This is a code that I found on (http://stackoverflow.com/questions/1257420/making-a-joptionpane-with-4-options)
            {
                "Slow", "Medium", "Fast"     //Speed options, slow, medium and fast
            }
            ;
            int response = JOptionPane.showOptionDialog (null, "What would you like the velocity of the rocket to be?", "Rocket speed",
                    JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE,
                    null, options, options [0]);  //This is the name of the box, and what the box displays inside.
        }

        else
        {
            JOptionPane.showMessageDialog (null, "Sorry, " + name + ", you have unsuccessfully identified your session. Please try again in 10 minutes.");
        }

    }


    // main method
} // SpaceTravels class

When I remove the graphics code, the message boxes work the way they should work.

  • 2
    You're overriding `paint` (of a top level container) but not calling `super.paint`, breaking the paint chain – MadProgrammer Mar 31 '15 at 01:17
  • I'm not a very adept programmer, as I have only started a few days ago. So I am not sure what super.paint means – Rechie Pina Mar 31 '15 at 01:20
  • "Swing programs should override `paintComponent()` instead of overriding `paint()`."—[*Painting in AWT and Swing: The Paint Methods*](http://www.oracle.com/technetwork/java/painting-140037.html#callbacks). – trashgod Mar 31 '15 at 01:23
  • You've overridden the method `paint`, which defined within the class hierarchy inherited from `JFrame`. `paint` does a number of important jobs within this class hierarchy, but you've broken, by not call `super.paint(g)` first to ensure that it's functionality is executed before you perform your own. Having said that, it's not recommended to override `paint` generally and even more so for top level containers like `JFrame` – MadProgrammer Mar 31 '15 at 01:24
  • What I might recommend is, you have a read through [Inheritance](https://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html) and gain a little more understanding of Object Oriented Programming before you try and tackle complex subjects like user interfaces, graphics and game design - IMHO – MadProgrammer Mar 31 '15 at 01:25
  • Okay, I will try doing that. – Rechie Pina Mar 31 '15 at 01:28

1 Answers1

1
  • Overriding paint without calling super.paint first will break the functionality provided by the parent class and cause no end of issues with how the painting works. Instead of extending from JFrame and overriding it's paint routines (which are complex enough), start with a JPanel and override it's paintComponent method (calling super.paintComponent before you do any custom painting). See Painting in AWT and Swing and Performing Custom Painting for more details.
  • Make sure you are running your UI from within the context of the event dispatching thread. See Initial Threads for more details

For example...

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                ex.printStackTrace();
            }

            String name;            //Stores names
            int tries = 0;          //Stores the amount of tries
            String verCode = "1234567";                         //Verification Code is 1234567
            String code = "1234567";                            //Verification Code is 1234567

            JOptionPane.showMessageDialog(null, "Hello, and welcome to the Canadian Space Agency's Spacecraft Control and Simulation Program!"); //Welcome
            JOptionPane.showMessageDialog(null, "Your verification code is 1234567.");             //This is the verification code

            name = JOptionPane.showInputDialog(null, "For safety purposes, please enter your name");           //Asking for user name
            verCode = JOptionPane.showInputDialog(null, "Now, " + name + ", please enter your verification code."); //Enter the verification code: 1234567

            while ((verCode.equalsIgnoreCase("1234567") != true) && (tries < 3)) //3 tries to get this correct
            {
                verCode = JOptionPane.showInputDialog(null, "You have entered the wrong verification code, please try again.");
                tries = tries + 1;          //Tries goes up once each time this loop occurs.
            }

            if (code.equals(verCode)) {
                String[] options = new String[] //This is a code that I found on (http://stackoverflow.com/questions/1257420/making-a-joptionpane-with-4-options)
                {
                    "Slow", "Medium", "Fast" //Speed options, slow, medium and fast
                };
                int response = JOptionPane.showOptionDialog(null, "What would you like the velocity of the rocket to be?", "Rocket speed",
                                JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE,
                                null, options, options[0]);  //This is the name of the box, and what the box displays inside.
            } else {
                JOptionPane.showMessageDialog(null, "Sorry, " + name + ", you have unsuccessfully identified your session. Please try again in 10 minutes.");
            }
        }

        // Create main UI??
    });
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366