Thursday, April 17, 2014

Soldiering on with Eclipse...

Today we continued with Eclipse by watching video #4 of The Cherno's game tutorial. I have to say that I'm more than a bit humbled by the amount of Java I don't know because I have been relying on programs like BlueJ and GreenFoot to set up the windows and manage the workspace for me. That being said, I'm trying to push through it, but please understand that I'm struggling with some of this new material as well.

I believe that once we get past the initial "set up the game" process, we'll be back into regular programming with loops and variables and math. :) Let's keep pushing in hopes that we get it.

Our code [after today] looked like this:
package com.version001.rain;

import java.awt.Canvas;
import java.awt.Dimension;

import javax.swing.JFrame;

public class Game extends Canvas implements Runnable {
 private static final long serialVersionUID = 1L;

 public static int width = 300;
 public static int height = width / 16 * 9;
 public static int scale = 3;

 private Thread thread;
 private JFrame frame;
 private boolean running = false;
 
 public Game() {
  Dimension size = new Dimension(width * scale, height * scale);
  setPreferredSize(size);  
  
  frame = new JFrame();
 }

 public synchronized void start() {
  running = true;
  thread = new Thread(this, "Display");
  thread.start();
 }

 public synchronized void stop() {
  running = false;
  try {
   thread.join();
  } catch (final InterruptedException e) {
   e.printStackTrace();
  }
 }

 public void run() {
  while (running) {
   System.out.println("Running...");
  }
 }
 
 public static void main(String[] args) {
  Game game = new Game();
  game.frame.setResizable(false); // Do not allow user to resize window
  game.frame.setTitle("Rain");
  game.frame.add(game);
  game.frame.pack();
  game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Close when you hit X button
  game.frame.setLocationRelativeTo(null); // Centers our window
  game.frame.setVisible(true); // Makes window visible.
  
  game.start(); // Starts the game
 }
}

No comments:

Post a Comment