Question

Yes, I admit, I AM watching a tutorial for my game. Yet he can run it perfectly. I just have one issue. This is my main, and then I'll show you my problem. Or maybe by the title you can find it in my main. Oh, those pasted code lines are also part of my main.

package game.Mixit.Game.main;

public class main {
    import java.awt.Canvas;

    import javax.swing.JFrame;

    public class Game extends Canvas implements Runnable {

            private static final long serialVersionUID = 1L;

            public static final int WIDTH = 160;
            public static final int HEIGHT = WIDTH/ 12*9;
            public static final int SCALE = 3;
            public static final String name = "Game";

            private static Object start;

            private JFrame frame;

            public boolean running = false;

            public Game() {
                setMinimumSize(new Dimension(WIDTH*SCALE, HEIGHT*SCALE));
                setMaximumSize(new Dimension(WIDTH*SCALE, HEIGHT*SCALE));
                setPreferredSize(new Dimension(WIDTH*SCALE, HEIGHT*SCALE));

                frame = new JFrame(NAME);

                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());

                frame.add(this.BorderLayoud.CENTER);
                frame.pack();

                frame.setResizable(false);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);


                public synchronized void start() {
                  running = true
                  new Thread(this).start();

                }

                public synchronized void stop() {
                  running = false
                }

            }

        public void run() {
          while (running) {
              System.out.println("Hello World");}
          }



        public static void main(String[] args){
            new Game().start();
        }



        public void start() {

        }
    }

}

No correct solution

OTHER TIPS

You've got your main method defined in a non static inner class, so the JVM cannot access it.

You have a class defined inside a class here that looks odd and you have a number of syntax errors that your IDE should be yelling at you about. You probably want something like this:

package game.mixit;                // <= Lowercased package

// added a few imports
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Dimension;

import javax.swing.JFrame;

// Removed the main class 

public class Game extends Canvas implements Runnable { 

  private static final long serialVersionUID = 1L;

  public static final int WIDTH = 160;
  public static final int HEIGHT = WIDTH / 12 * 9;
  public static final int SCALE = 3;
  public static final String NAME = "Game";

  private static Object start;

  private JFrame frame;

  public boolean running = false;

  public Game() {
    setMinimumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
    setMaximumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
    setPreferredSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));

    frame = new JFrame(NAME);

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new BorderLayout());

    frame.pack();

    frame.setResizable(false);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
  }

  public synchronized void start() {
    // fixed some missing ;
    running = true;
  }


  public synchronized void stop() {
    running = false;
    System.out.println("Stopping");
  }

  public void run() {
    start();
    while (running) {
      System.out.println("Hello World");
    }
  }

  public static void main(String[] args) throws InterruptedException {
    Game game = new Game();
    new Thread(game).start();

    // Just for kicks wait a bit then 'stop' the game
    Thread.sleep(1000);
    game.stop();

    System.out.println("Stopped");
  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top