title: Menu in a simple java game
body: <p>I created a GUI named Menu class for my Start, Help and Exit button. my problem is my game won't start, it hangs everytime I press the Start button. Can someone check my error and explain it to me because its my case study at school.</p>
  
  <hr>
  
  <pre><code>package spaceSip;
  
  import javax.swing.*;
  
  import java.awt.*;
  
  import java.awt.event.*;
  
  import java.awt.image.BufferedImage;
  
  
  
  
  public class Menu extends JFrame implements ActionListener
  {
      private JLabel titleL;
      private JButton startB, helpB, exitB;
  
      static JFrame frame1 = new JFrame();
  
      public Menu()
      {
          frame1.setSize(500,250);
  
          Container mainP = frame1.getContentPane();
          mainP.setLayout(null);
  
          titleL = new JLabel("WELCOME");
          startB = new JButton("Start");
          helpB = new JButton("Help");
          exitB = new JButton("Exit");
  
  
          mainP.add(titleL);
          titleL.setFont(new Font("Chiller",Font.BOLD,50));
          titleL.setBounds(100, 30, 200, 50);
  
          mainP.add(startB);
          startB.setMnemonic(KeyEvent.VK_S);
          startB.setBounds(200, 80, 100, 20);
  
          mainP.add(helpB);
          helpB.setMnemonic(KeyEvent.VK_H);
          helpB.setBounds(200, 100, 100, 20);
  
  
          mainP.add(exitB);
          exitB.setMnemonic(KeyEvent.VK_E);
          exitB.setBounds(200, 120, 100, 20);
  
  
          startB.addActionListener(this);
          helpB.addActionListener(this);
          exitB.addActionListener(this);
  
          frame1.setVisible(true);
          frame1.setResizable(false);
  
  
      }
  
  
      public void actionPerformed(ActionEvent e)
      {
          String key = e.getActionCommand();
  
          if(key == "Start")
          {
              frame1.dispose();
              new SpaceShipMain();
          }
  
          else if(key == "Help")
          {
  
          }
  
          else
              System.exit(0);
      }
  
      public static void main(String[]args)
      {
          new Menu();
      }
  
  }
  </code></pre>
  
  <hr>
  
  <pre><code>package spaceSip;
  
  import javax.swing.*;
  import java.util.*;
  import java.awt.*;
  import java.awt.event.*;
  import java.io.*;
  
  public class SpaceShipMain implements MouseListener
  {
  
      private JFrame background;
      private SpaceShipPanel back;
  
      public static boolean paused;
      public static boolean crashed;
      public static boolean started;
      public static boolean playedOnce;        
  
      public boolean goingUp;
      private double upCount;
  
      public static int distance;
      public static int maxDistance;
  
      public final int XPOS;
      public final int NUMRECS;
      public final int RECHEIGHT;
      public final int RECWIDTH;
  
      private int moveIncrement;
      private int numSmoke;
  
      private ArrayList&lt;SpaceShipImage&gt; toprecs;
      private ArrayList&lt;SpaceShipImage&gt; bottomrecs;
      private ArrayList&lt;SpaceShipImage&gt; middlerecs;
      private ArrayList&lt;SpaceShipImage&gt; smoke;
      private SpaceShipImage helicopter;
  
  
      public SpaceShipMain()
      {
          NUMRECS = 100;
          RECHEIGHT = 70;
          RECWIDTH = 30;
          XPOS = 200;
          playedOnce = false;
          maxDistance = 0;
  
          load(new File("C:\\Users/Travelmate/workspace/GAME/src/spaceSip/Best.txt"));
  
          initiate();
      }
  
      public void load(File file)
      {
          try
          {
              Scanner reader = new Scanner(file);
              while(reader.hasNext())
              {
                  int value = reader.nextInt();
                  if(value &gt; maxDistance)
                      maxDistance = value;
              }
          }
          catch(IOException i )
          {
              System.out.println("Error. "+i);
          }
      }
  
      public void save()
      {
          FileWriter out;
          try
          {
              out = new FileWriter("C:\\Users/Travelmate/workspace/GAME/src/spaceSip/Best.txt");
              out.write("" + maxDistance);
              out.close();
          }
          catch(IOException i)
          {
              System.out.println("Error: "+i.getMessage());
          }
      }
  
      public void initiate()
      {
          if(!playedOnce)
          {
              background = new JFrame("Space Ship Game"); 
              background.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //closes the program when the window is closed
              background.setResizable(false); //don't allow the user to resize the window
              background.setSize(new Dimension(800,500));
              background.setVisible(true);
  
              back = new SpaceShipPanel("C:\\Users/Travelmate/workspace/GAME/src/spaceSip/starfield.jpg");
              background.add(back);
  
              back.addMouseListener(this);
          }
          playedOnce = true;
          goingUp = false;
          paused = false;
          crashed = false;
          started = false;
  
          distance = 0;
          upCount = 0;
  
          moveIncrement = 2;
          numSmoke = 15;
  
  
          toprecs = new ArrayList&lt;SpaceShipImage&gt;();
          middlerecs = new ArrayList&lt;SpaceShipImage&gt;();
          bottomrecs = new ArrayList&lt;SpaceShipImage&gt;();
          smoke = new ArrayList&lt;SpaceShipImage&gt;();
  
          helicopter = new SpaceShipImage("C:\\Users/Travelmate/workspace/GAME/src/spaceSip/rocketship.GIF",XPOS,200);
  
          for(int x = 0; x &lt; NUMRECS; x++)
              toprecs.add(new SpaceShipImage("C:\\Users/Travelmate/workspace/GAME/src/spaceSip/rec2.JPG",RECWIDTH*x,5));
          for(int x = 0; x &lt; NUMRECS; x++)
              bottomrecs.add(new SpaceShipImage("C:\\Users/Travelmate/workspace/GAME/src/spaceSip/rec2.JPG",RECWIDTH*x,410));
  
          middlerecs.add(new SpaceShipImage("C:\\Users/Travelmate/workspace/GAME/src/spaceSip/asteroid2.jpg",1392,randomMidHeight()));
          middlerecs.add(new SpaceShipImage("C:\\Users/Travelmate/workspace/GAME/src/spaceSip/asteroid2.jpg",1972,randomMidHeight()));
  
  
          drawRectangles();
      }
  
      public void drawRectangles()
      {
          long last = System.currentTimeMillis();
          long lastCopter = System.currentTimeMillis();
          long lastSmoke = System.currentTimeMillis();
          long lastSound = System.currentTimeMillis();
          int firstUpdates = 0;
          double lastDistance = (double)System.currentTimeMillis();
          while(true)
          {
              if(!paused &amp;&amp; !crashed &amp;&amp; started &amp;&amp; (double)System.currentTimeMillis() - (double)(2900/40) &gt; lastDistance)
              {   
                  lastDistance = System.currentTimeMillis();
                  distance++;
              }   
  
  
              if(!paused &amp;&amp; !crashed &amp;&amp; started &amp;&amp; System.currentTimeMillis() - 10 &gt; lastCopter)
              {
                  lastCopter = System.currentTimeMillis();
                  updateCopter();
                  updateMiddle();
              }
              if(!paused &amp;&amp; !crashed &amp;&amp; started &amp;&amp; System.currentTimeMillis() - 100 &gt; last)
              {
                  last = System.currentTimeMillis();
                  updateRecs();
              }
              if(!paused &amp;&amp; !crashed &amp;&amp; started &amp;&amp; System.currentTimeMillis() - 75 &gt; lastSmoke)
              {
                  lastSmoke = System.currentTimeMillis();
                  if (firstUpdates &lt; numSmoke)
                  {
                      firstUpdates++;
                      smoke.add(new SpaceShipImage("C:\\Users/Travelmate/workspace/GAME/src/spaceSip/smoke.GIF",187,helicopter.getY()));
                      for(int x = 0; x &lt; firstUpdates; x++)
                          smoke.set(x,new SpaceShipImage("C:\\Users/Travelmate/workspace/GAME/src/spaceSip/smoke.GIF",smoke.get(x).getX() - 12, smoke.get(x).getY()));
                  }
                  else
                  {
                      for(int x = 0; x &lt; numSmoke - 1; x++)
                          smoke.get(x).setY(smoke.get(x+1).getY());
                      smoke.set(numSmoke - 1,new SpaceShipImage("C:\\Users/Travelmate/workspace/GAME/src/spaceSip/smoke.GIF",187,helicopter.getY()));
                  }
                      }
                      back.updateImages(middlerecs,helicopter,smoke);
                  }
      }
  
      public void updateRecs()
      {
          for(int x = 0; x &lt; (NUMRECS - 1); x++) //move all but the last rectangle 1 spot to the left
          {
              toprecs.set(x,new SpaceShipImage("C:\\Users/Travelmate/workspace/GAME/src/spaceSip/rec2.JPG",RECWIDTH*x,toprecs.get(x+1).getY()));
              bottomrecs.set(x,new SpaceShipImage("C:\\Users/Travelmate/workspace/GAME/src/spaceSip/rec2.JPG",RECWIDTH*x,bottomrecs.get(x+1).getY()));
          }
  
      }
  
  
  
      public void randomDrop()
      {
          toprecs.get(26).setY(toprecs.get(26).getY() + (463 - bottomrecs.get(26).getY()));
          bottomrecs.get(26).setY(463);
      }
  
  
  
  
      public int randomMidHeight()
      {
          int max = 10000;
          int min = 0;
  
          for(int x = 0; x &lt; NUMRECS; x++)
          {
              if(toprecs.get(x).getY() &gt; min)
                  min = (int)toprecs.get(x).getY();
              if(bottomrecs.get(x).getY() &lt; max)
                  max = (int)bottomrecs.get(x).getY();
          }
          min += RECHEIGHT;
          max -= (RECHEIGHT + min);
          return min + (int)(Math.random() * max);
      }
  
      //moves the randomly generated middle rectangles
      public void updateMiddle()
      {
          if(middlerecs.get(0).getX() &gt; -1 * RECWIDTH)
          {
              middlerecs.set(0,new SpaceShipImage("C:\\Users/Travelmate/workspace/GAME/src/spaceSip/asteroid.gif",middlerecs.get(0).getX() - (RECWIDTH/5), middlerecs.get(0).getY()));
              middlerecs.set(1,new SpaceShipImage("C:\\Users/Travelmate/workspace/GAME/src/spaceSip/asteroid.gif",middlerecs.get(1).getX() - (RECWIDTH/5), middlerecs.get(1).getY()));
          }
          else
          {
              middlerecs.set(0,new SpaceShipImage("C:\\Users/Travelmate/workspace/GAME/src/spaceSip/asteroid.gif",middlerecs.get(1).getX() - (RECWIDTH/5), middlerecs.get(1).getY()));
              middlerecs.set(1,new SpaceShipImage("C:\\Users/Travelmate/workspace/GAME/src/spaceSip/asteroid.gif",middlerecs.get(0).getX() + 580,randomMidHeight()));
          }
      }
  
      public boolean shoot()
      {
          for(int x = 3; x &lt;= 7; x++)
              if(helicopter.getY() &gt;= bottomrecs.get(x).getY())
                  return true;
  
          for(int y = 3; y &lt;= 7; y++)
                  if(helicopter.getY() &lt;= toprecs.get(y).getY())
                      return true;
          for(int z = 0; z &lt;= 1; z++)
              if(isInMidRange(z))
                  return true;
          return false;
      }
  
      public boolean isInMidRange(int num)
      {
          Rectangle middlecheck = new Rectangle((int)middlerecs.get(num).getX(),(int)middlerecs.get(num).getY(),RECWIDTH,RECHEIGHT);
          Rectangle coptercheck = new Rectangle((int)helicopter.getX(),(int)helicopter.getY(),70,48); //asteroid X and y bump
          return middlecheck.intersects(coptercheck);
      }
  
      public void crash()
      {
          crashed = true;
          if(distance &gt; maxDistance) 
          {
              maxDistance = distance;
              save();
          }
          int reply = JOptionPane.showConfirmDialog(null, "               RESTART ?", "GAME OVER", JOptionPane.YES_NO_OPTION);    
          if(reply == JOptionPane.YES_OPTION)
              initiate();
  
          else 
              System.exit(0);
  
  
          initiate();
      }
  
      //moves the spaceship
      public void updateCopter()
      {
          upCount += .10;
          if(goingUp)
          {
              if(upCount &lt; 3.5)
                  helicopter.setPosition(XPOS,(double)(helicopter.getY() - (.3 + upCount)));
              else
                  helicopter.setPosition(XPOS,(double)(helicopter.getY() - (1.2 + upCount)));
              helicopter.setImage("C:\\Users/Travelmate/workspace/GAME/src/spaceSip/rocketship.GIF"); 
          }
          else
          {
              if(upCount &lt; 1)
                  helicopter.setPosition(XPOS,(double)(helicopter.getY() + upCount));
              else
                  helicopter.setPosition(XPOS,(double)(helicopter.getY() + (1.2 + upCount)));
              helicopter.setImage("C:\\Users/Travelmate/workspace/GAME/src/spaceSip/rocketship.GIF");
          }
          if(shoot())
              crash();
      }
  
      //Called when the mouse exits the game window
      public void mouseExited(MouseEvent e)
      {
              paused = true;
      }
  
      //Called when the mouse enters the game window
      public void mouseEntered(MouseEvent e)
      {
  
      }
  
      //Called when the mouse is released
      public void mouseReleased(MouseEvent e)
      {
          goingUp = false;
          upCount = -1;
          if(paused)
              paused = false;
      }
  
      //Called when the mouse is pressed
      public void mousePressed(MouseEvent e)
      {
          if (!started)
              started = true;
          goingUp = true;
          upCount = 0;
      }
  
      //Called when the mouse is released
      public void mouseClicked(MouseEvent e)
      {
  
      }
  }
  </code></pre>
  
  <hr>
  
  <pre><code>package spaceSip;
  
  import java.awt.Image;
  
  import javax.swing.ImageIcon;
  
  public class SpaceShipImage
  {
      private Image image;        //The picture
      private double x;           //X position
      private double y;           //Y position
  
      //Construct a new Moving Image with image, x position, and y position given
      public SpaceShipImage(Image img, double xPos, double yPos)
      {
          image = img;
          x = xPos;
          y = yPos;
      }
  
      //Construct a new Moving Image with image (from file path), x position, and y position given
      public SpaceShipImage(String path, double xPos, double yPos)
      {
          this(new ImageIcon(path).getImage(), xPos, yPos);   
              //easiest way to make an image from a file path in Swing
      }
  
      //They are set methods.  I don't feel like commenting them.
      public void setPosition(double xPos, double yPos)
      {
          x = xPos;
          y = yPos;
      }
  
      public void setImage(String path)
      {
          image = new ImageIcon(path).getImage();
      }
  
      public void setY(double newY)
      {
          y = newY;
      }
  
      public void setX(double newX)
      {
          x = newX;
      }
  
      //Get methods which I'm also not commenting
      public double getX()
      {
          return x;
      }
  
      public double getY()
      {
          return y;
      }
  
      public Image getImage()
      {
          return image;
      }
  }
  </code></pre>
  
  <hr>
  
  <pre><code>package spaceSip;
  
  import java.awt.Color;
  import java.awt.Dimension;
  import java.awt.Font;
  import java.awt.Graphics;
  import java.awt.Image;
  import java.util.ArrayList;
  
  import javax.swing.ImageIcon;
  import javax.swing.JPanel;
  
  class SpaceShipPanel extends JPanel 
  {
      private Image background;           
      private ArrayList&lt;SpaceShipImage&gt; middle;
      private SpaceShipImage copter;
      private ArrayList&lt;SpaceShipImage&gt; smoke;
  
      //Constructs a new ImagePanel with the background image specified by the file path given
      public SpaceShipPanel(String img) 
      {
          this(new ImageIcon(img).getImage());    
              //The easiest way to make images from file paths in Swing
      }
  
      //Constructs a new ImagePanel with the background image given
      public SpaceShipPanel(Image img)
      {
          background = img;
          Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));    
              //Get the size of the image
          //Thoroughly make the size of the panel equal to the size of the image
          //(Various layout managers will try to mess with the size of things to fit everything)
          setPreferredSize(size);
          setMinimumSize(size);
          setMaximumSize(size);
          setSize(size);
  
  
          middle = new ArrayList&lt;SpaceShipImage&gt;();
          smoke = new ArrayList&lt;SpaceShipImage&gt;();
      }
  
      //This is called whenever the computer decides to repaint the window
      //It's a method in JPanel that I've overwritten to paint the background and foreground images
      public void paintComponent(Graphics g) 
      {
          //Paint the background with its upper left corner at the upper left corner of the panel
          g.drawImage(background, 0, 0, null); 
          //Paint each image in the foreground where it should go
  
          for(SpaceShipImage img : middle)
              g.drawImage(img.getImage(), (int)(img.getX()), (int)(img.getY()), null);
          for(SpaceShipImage img : smoke)
              g.drawImage(img.getImage(), (int)(img.getX()), (int)(img.getY()), null);
          if(copter != null)
              g.drawImage(copter.getImage(), (int)(copter.getX()), (int)(copter.getY()), null);
          drawStrings(g);
      }
  
      public void drawStrings(Graphics g)
      {
          g.setColor(Color.WHITE);
          g.setFont(new Font("Arial",Font.BOLD,20));
          g.drawString("Distance: " + SpaceShipMain.distance,30,440);
          g.setFont(new Font("Arial",Font.BOLD,20));
          if (SpaceShipMain.distance &gt; SpaceShipMain.maxDistance)
              g.drawString("Best: " + SpaceShipMain.distance,650,440);
          else
              g.drawString("Best: " + SpaceShipMain.maxDistance,650,440);
          if(SpaceShipMain.paused)
          {
  
                  g.setFont(new Font("Chiller",Font.BOLD,72));
                  g.drawString("Paused",325,290);
                  g.setFont(new Font("Chiller",Font.BOLD,30));
                  g.drawString("Click to unpause.",320,340);
          }
      }
  
      //Replaces the list of foreground images with the one given, and repaints the panel
      public void updateImages(ArrayList&lt;SpaceShipImage&gt; newMiddle,SpaceShipImage newCopter,ArrayList&lt;SpaceShipImage&gt; newSmoke)
      {
          copter = newCopter;
          middle = newMiddle;
          smoke = newSmoke;
          repaint();  //This repaints stuff... you don't need to know how it works
      }
  }
  </code></pre>