title: View Doesn't Open When I Try To Run My Program?
body: <p>I am following tutorials from this gentleman (<a href="https://www.youtube.com/watch?v=_NCw5S7PUXw" rel="nofollow">https://www.youtube.com/watch?v=_NCw5S7PUXw</a>) messing around in Java trying to make a Tower Defense game. I have finished the above video, and so far my code is as follows. I have it divided into three classes all in the same package:</p>
  
  <p><strong>GameView.java</strong></p>
  
  <pre><code>package jTowerDefensev1;
  
  import javax.swing.JFrame;
  
  public class GameView extends JFrame{
  
  public static void main (String [] args) {
      new GameView();
  }
  
  public GameView() {
      JFrame view = new JFrame();
  
      view.setSize(800, 600);
      view.setTitle("jTowerDefense");
      view.setDefaultCloseOperation(EXIT_ON_CLOSE);
      view.setResizable(false);
      view.setLocationRelativeTo(null);
  
      BattleGrid grid = new BattleGrid(this);
      this.add(grid);
  
  }
  
  }
  </code></pre>
  
  <p><strong>BattleGrid.java</strong></p>
  
  <pre><code>package jTowerDefensev1;
  
  import java.awt.Color;
  import java.awt.Graphics;
  //import java.awt.event.ActionEvent;
  //import java.awt.event.ActionListener;
  
  
  import javax.swing.JPanel;
  //import javax.swing.Timer;
  
  public class BattleGrid extends JPanel implements Runnable {
  
   GameView view;
  
   Thread thread = new Thread(this);
  
   public boolean running = false; //Is the game running?
  
   private int fps = 0; //The Frames Per Second (FPS) of the view
  
   public int scene = 0;
  
  //ActionListener update = new ActionListener() { 
      //@Override public void actionPerformed(ActionEvent ae) { repaint(); }
  //};
  
  //Timer timer = new Timer(1, update); 
  
  public BattleGrid (GameView view) {
  
      this.view = view;
  
      this.view.addKeyListener(new KeyEventHandler(this));
  
      //timer.start();
      thread.start();
  
  }
  
  public void paintComponent (Graphics g) {
      g.clearRect(0, 0, this.view.getWidth() ,this.view.getHeight()); //Make sure painting can proceed as prev. layer is done painting
      g.drawString(fps + "", 10, 10);
  
      if (scene == 0) {
          g.setColor(Color.BLUE); //Pre-game
      }
  
      else if (scene ==1){
          g.setColor(Color.GREEN); //During game
      }
  
      else{
          g.setColor(Color.WHITE); //Anywhere else
      }
  
      g.fillRect(0, 0, this.view.getWidth(), this.view.getHeight());
  
  }
  
  public void run() {
  
      //System.out.println("Frame Made!");
  
      long lastView = System.currentTimeMillis();
      int frames = 0;
  
      running = true;
      scene = 0;
  
      while (running) {
  
          repaint();
  
          frames++;
  
          if(System.currentTimeMillis() - 1000 &gt;= lastView) { //At 1000 milliseconds (1 second), set the fps
              fps = frames;
              frames = 0;
              lastView = System.currentTimeMillis();
          }
  
          try {
              Thread.sleep(1);
          } catch (InterruptedException e) {
              e.printStackTrace();
            }
  
      }
  
      System.exit(0);
  
  }
  
  
  public class KeyPressed {
  
      public void KeyESC(){ //Close game on ESC key press
          running = false;
  
      }
  
      public void KeySPACE() {
          scene = 1;
  
      }
  }
  
  
  }
  </code></pre>
  
  <p><strong>KeyEventHandler.java</strong></p>
  
  <pre><code>package jTowerDefensev1;
  
  import java.awt.event.KeyEvent;
  import java.awt.event.KeyListener;
  
  public class KeyEventHandler implements KeyListener {
  
  private BattleGrid grid;
  private BattleGrid.KeyPressed keyPressed;
  
  public KeyEventHandler(BattleGrid grid) {
      this.grid = grid;
      this.keyPressed = this.grid.new KeyPressed();
  }
  
  public void keyTyped(KeyEvent e) {
  
  
  }
  
  public void keyPressed(KeyEvent e) {
      int keyCode = e.getKeyCode();
  
      if(keyCode == 27){ //ESC key to close game
          this.keyPressed.KeyESC();
      }
  
      if(keyCode == 32){ //Space to begin main game
          this.keyPressed.KeySPACE();
      }
  
  }
  
  public void keyReleased(KeyEvent e) {
  
  
  }
  
  }
  </code></pre>
  
  <p>Now my problem: I have been trying to run GameView as a Java Application to see the results of my work. But it just seems to hang there until I have to kill the process - no GUI view appears. Is it likely that it  is a quirk of my code, or is it the fault of my PC/IDE? I am using Eclipse and JavaSE 1.7. Ignore some of the commented timer stuff - I was just trying different ways than illustrated.</p>
  
  <p><strong>UPDATE:</strong> Thanks for the pointer on setting visibility. However, now that the frame has popped, I can't see the colours set in my BattleGrid class. Any ideas there?</p>
  
  <p>Thanks very much for any and all assistance!</p>