title: how to open one Jframe from main method call
body: <p>I have downloaded a snake game project in java and try to modify it. Initially the project contains three java files 
  i.e "<code>Engine.jav</code>a" , "<code>Snake.java</code>", "<code>GameBoard.java</code>". And <code>Engine.java</code> have  the <code>main()</code> method, and when i run this Engine.java class game starts running. </p>
  
  <p>To improve the user iteractivity towards the project i have created two JFrames :"<code>PlayGame.java</code>", <code>Rules.java</code></p>
  
  <p>Now this project having five java classes -</p>
  
  <pre><code>Engine.java(containing main() method) 
  Snake.java
  GameBoard.java
  PlayGame.java(is a JFrame)
  Rules.java(is a JFrame)
  
  PlayGame.java have three buttons
  
  Play - i want when play button get clicked snake game start/run.
  Rules - when clicked Rules.java Jframe should be opened 
  Exit - exits the application
  </code></pre>
  
  <p>Now what i want is at first "<code>PlayGame.java</code>" JFrame should appear(and this is appearing as the main output of the game project) and throw this game should start i.e when i click play button from <code>PlayGame JFrame</code> game should start
  The problem i am facing is when i click <code>play</code> button then <code>gamescreen</code> appears on the window but <code>snake is not moving</code>.</p>
  
  <p>Here is the code that i have included in <code>actionPerformed()</code> method of <code>Play button</code></p>
  
  <pre><code>        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
  
              JFrame frame = new JFrame("SnakeGame");
      frame.setVisible(true); 
              frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
      frame.setResizable(true);
      Canvas canvas = new Canvas();
      canvas.setBackground(Color.black);
              canvas.setPreferredSize(new Dimension(GameBoard.MAP_SIZE * GameBoard.TILE_SIZE, GameBoard.MAP_SIZE * GameBoard.TILE_SIZE));
              frame.getContentPane().add(canvas); 
              frame.pack();
              frame.setLocationRelativeTo(null);
              frame.setVisible(true);
              new Engine(canvas).startGame();
  }           
  </code></pre>
  
  <p>and also i am showing code of <code>startGame()</code> method which is in <code>Engine.java</code> class</p>
  
  <pre><code>public void startGame() {
      canvas.createBufferStrategy(2);
  
      Graphics2D g = (Graphics2D)canvas.getBufferStrategy().getDrawGraphics();
      g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  
      long start = 0L;
      long sleepDuration = 0L;
      while(true) {
          start = System.currentTimeMillis();
  
          update();
          render(g);
  
          canvas.getBufferStrategy().show();
  
          g.clearRect(0, 0, canvas.getWidth(), canvas.getHeight());
  
          sleepDuration = (1500L / UPDATES_PER_SECOND) - (System.currentTimeMillis() - start);
  
          if(sleepDuration &gt; 0) {
              try {
                  Thread.sleep(sleepDuration);
              } catch(Exception e) {
                  e.printStackTrace();
              }
          }
      }
  }
  </code></pre>
  
  <p>Here also i am attaching the Engine.java class and PlayGame.java in my question for better understandig of problem</p>
  
  <blockquote>
    <p>Engine.java </p>
  </blockquote>
  
  <pre><code>package org.psnbtech;
  
  import java.awt.Canvas;
  import java.awt.Color;
  import java.awt.Dimension;
  import java.awt.Font;
  import java.awt.Graphics2D;
  import java.awt.RenderingHints;
  import java.awt.event.KeyAdapter;
  import java.awt.event.KeyEvent;
  import javax.swing.JFrame;
  import org.psnbtech.GameBoard.TileType;
  import org.psnbtech.Snake.Direction;
  
  
  public class Engine extends KeyAdapter {
  
      private static final int UPDATES_PER_SECOND = 15;
  
      private static final Font FONT_SMALL = new Font("Arial", Font.BOLD, 20);
  
      private static final Font FONT_LARGE = new Font("Arial", Font.BOLD, 40);
  
      public Canvas canvas;
  
      public GameBoard board;
  
      public Snake snake;
  
      public int score;
  
      public boolean gameOver;
  
  
      public Engine(Canvas canvas) {
                  this.canvas = canvas;
              this.board = new GameBoard();
          this.snake = new Snake(board);
  
          resetGame();
  
          canvas.addKeyListener(this);
                  //new Engine(canvas).startGame();
      }
  
  
      public void startGame() {
          canvas.createBufferStrategy(2);
  
          Graphics2D g = (Graphics2D)canvas.getBufferStrategy().getDrawGraphics();
          g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  
          long start = 0L;
          long sleepDuration = 0L;
          while(true) {
              start = System.currentTimeMillis();
  
              update();
              render(g);
  
              canvas.getBufferStrategy().show();
  
              g.clearRect(0, 0, canvas.getWidth(), canvas.getHeight());
  
              sleepDuration = (1500L / UPDATES_PER_SECOND) - (System.currentTimeMillis() - start);
  
              if(sleepDuration &gt; 0) {
                  try {
                      Thread.sleep(sleepDuration);
                  } catch(Exception e) {
                                      e.printStackTrace();
                  }
              }
          }
      }
  
      public void update() {
          if(gameOver || !canvas.isFocusOwner()) {
              return;
          }
          TileType snakeTile = snake.updateSnake();
          if(snakeTile == null || snakeTile.equals(TileType.SNAKE)) {
              gameOver = true;
          } else if(snakeTile.equals(TileType.FRUIT)) {
              score += 10;
              spawnFruit();
          }
      }
  
      public void render(Graphics2D g) {
          board.draw(g);
  
          g.setColor(Color.WHITE);
  
          if(gameOver) {
              g.setFont(FONT_LARGE);
              String message = new String("Your Score: " + score);
              g.drawString(message, canvas.getWidth() / 2 - (g.getFontMetrics().stringWidth(message) / 2), 250);
  
              g.setFont(FONT_SMALL);
              message = new String("Press Enter to Restart the Game");
              g.drawString(message, canvas.getWidth() / 2 - (g.getFontMetrics().stringWidth(message) / 2), 350);
          } else {
              g.setFont(FONT_SMALL);
              g.drawString("Score:" + score, 10, 20);
          }
      }
  
      public void resetGame() {
          board.resetBoard();
          snake.resetSnake();
          score = 0;
          gameOver = false;
          spawnFruit();
      }
  
      public void spawnFruit() {
          int random = (int)(Math.random() * ((GameBoard.MAP_SIZE * GameBoard.MAP_SIZE) - snake.getSnakeLength())); // if '*' replace by '/' then only one fruit is there for snake
  
          int emptyFound = 0;
          int index = 0;
          while(emptyFound &lt; random) {
              index++;
              if(board.getTile(index % GameBoard.MAP_SIZE, index / GameBoard.MAP_SIZE).equals(TileType.EMPTY)) { // if '/' replaced by '*' then nothing displays on the board 
                  emptyFound++;
              }
          }
          board.setTile(index % GameBoard.MAP_SIZE, index / GameBoard.MAP_SIZE, TileType.FRUIT); // it also show nothing when replacing '/' by '/' 
      }
  
      @Override
      public void keyPressed(KeyEvent e) {
          if((e.getKeyCode() == KeyEvent.VK_UP)||(e.getKeyCode() == KeyEvent.VK_W)) {
              snake.setDirection(Direction.UP);
          }
          if((e.getKeyCode() == KeyEvent.VK_DOWN)||(e.getKeyCode() == KeyEvent.VK_S)) {
              snake.setDirection(Direction.DOWN);
          }
          if((e.getKeyCode() == KeyEvent.VK_LEFT)||(e.getKeyCode() == KeyEvent.VK_A)) {
              snake.setDirection(Direction.LEFT);
          }
          if((e.getKeyCode() == KeyEvent.VK_RIGHT)||(e.getKeyCode() == KeyEvent.VK_D)) {
              snake.setDirection(Direction.RIGHT);
          }
          if(e.getKeyCode() == KeyEvent.VK_ENTER &amp;&amp; gameOver) {
              resetGame();
          }
      }
  
           public static void main(String[] args)  {
          new PlayGame().setVisible(true);
  
          /**JFrame frame = new JFrame("SnakeGame");
          frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
          frame.setResizable(false);
  
          Canvas canvas = new Canvas();
          canvas.setBackground(Color.black);
          canvas.setPreferredSize(new Dimension(GameBoard.MAP_SIZE * GameBoard.TILE_SIZE, GameBoard.MAP_SIZE * GameBoard.TILE_SIZE));
  
          frame.getContentPane().add(canvas);
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true); 
  
          new Engine(canvas).startGame();*/
  
  
               }        
  }
  </code></pre>
  
  <blockquote>
    <p>PlayGame.java</p>
  </blockquote>
  
  <pre><code>package org.psnbtech;
  
  import java.awt.Canvas;
  import java.awt.Color;
  import java.awt.Dimension;
  import java.awt.event.KeyAdapter;
  import java.awt.event.KeyEvent;
  
  
  import javax.swing.JFrame;
  
  
  public class PlayGame extends javax.swing.JFrame  {
  
  
  
     public PlayGame() {
  
          initComponents();      
      }
  
  
      @SuppressWarnings("unchecked")
      // &lt;editor-fold defaultstate="collapsed" desc="Generated Code"&gt;                          
      private void initComponents() {
  
          jPanel1 = new javax.swing.JPanel();
          jButton2 = new javax.swing.JButton();
          jButton3 = new javax.swing.JButton();
          jButton1 = new javax.swing.JButton();
  
          setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  
          jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/psnbtech/GUID-4ED364DF-2D44-40F5-9F05-31D451F15EF1-low.png"))); // NOI18N
          jButton2.setText("Rules");
          jButton2.setMaximumSize(new java.awt.Dimension(89, 39));
          jButton2.setMinimumSize(new java.awt.Dimension(89, 39));
          jButton2.setPreferredSize(new java.awt.Dimension(89, 41));
          jButton2.addActionListener(new java.awt.event.ActionListener() {
              public void actionPerformed(java.awt.event.ActionEvent evt) {
                  jButton2ActionPerformed(evt);
              }
          });
  
          jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/psnbtech/exit (1).png"))); // NOI18N
          jButton3.setText("Exit");
          jButton3.setPreferredSize(new java.awt.Dimension(89, 41));
          jButton3.addActionListener(new java.awt.event.ActionListener() {
              public void actionPerformed(java.awt.event.ActionEvent evt) {
                  jButton3ActionPerformed(evt);
              }
          });
  
          jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/psnbtech/play.png"))); // NOI18N
          jButton1.setText("Play");
          jButton1.addActionListener(new java.awt.event.ActionListener() {
              public void actionPerformed(java.awt.event.ActionEvent evt) {
                  jButton1ActionPerformed(evt);
              }
          });
  
          javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
          jPanel1.setLayout(jPanel1Layout);
          jPanel1Layout.setHorizontalGroup(
              jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
              .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                  .addContainerGap(277, Short.MAX_VALUE)
                  .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                      .addGroup(jPanel1Layout.createSequentialGroup()
                          .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE)
                          .addContainerGap())
                      .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                          .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                              .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)
                              .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE))
                          .addGap(27, 27, 27))))
          );
          jPanel1Layout.setVerticalGroup(
              jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
              .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                  .addGap(40, 40, 40)
                  .addComponent(jButton1)
                  .addGap(35, 35, 35)
                  .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                  .addGap(27, 27, 27)
                  .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                  .addContainerGap(75, Short.MAX_VALUE))
          );
  
          javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
          getContentPane().setLayout(layout);
          layout.setHorizontalGroup(
              layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
              .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
          );
          layout.setVerticalGroup(
              layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
              .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
          );
  
          pack();
      }// &lt;/editor-fold&gt;                        
  
      private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
          System.exit(0);        
      }                                        
  
      private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
  
          this.dispose();
          new Rules().setVisible(true);
  
      }                                        
  
  
  
      private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
  
                  JFrame frame = new JFrame("SnakeGame");
          frame.setVisible(true); 
                  frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
          frame.setResizable(true);
          Canvas canvas = new Canvas();
          canvas.setBackground(Color.black);
                  canvas.setPreferredSize(new Dimension(GameBoard.MAP_SIZE * GameBoard.TILE_SIZE, GameBoard.MAP_SIZE * GameBoard.TILE_SIZE));
                  //canvas.addKeyListener((KeyListener) this);
                  frame.getContentPane().add(canvas); 
                  frame.pack();
                  frame.setLocationRelativeTo(null);
                  frame.setVisible(true);
                  new Engine(canvas).startGame();
      }                                        
  
  
      // Variables declaration - do not modify                     
      private javax.swing.JButton jButton1;
      private javax.swing.JButton jButton2;
      private javax.swing.JButton jButton3;
      private javax.swing.JPanel jPanel1;
      // End of variables declaration                   
  }
  </code></pre>