Question

Hey guys I'm new to Java game programming and I'm working with the Runnable interface right now. For some reason, my run() method never gets called and I'm not sure why. I've tried putting many System.out.println statements in there but they never get printed. Any help would be greatly appreciated!

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JPanel;


public class GamePanel extends JPanel implements Runnable
{
private final int WIDTH = 160;
private final int HEIGHT = WIDTH/12 *9;
private final int RATIO = 3;

private Thread animator;
private volatile boolean running;
private volatile boolean gameOver;

private double FPS = 60D;
private double period = 1000/FPS;

private Image dbImage;
private Graphics dbg;

public GamePanel()
{
    setPreferredSize(new Dimension(WIDTH *3, HEIGHT*3));
    setBackground(Color.WHITE);
    setFocusable(true);
    requestFocus();
    terminate();
}

public void addNotify()
{
    super.addNotify();
    startGame();
}

public void startGame()
{
    System.out.println("Thread started");
    animator = new Thread();
    animator.start();
}

public void stopGame()
{
    System.out.println("Thread stopped");
    running = false;
}

public void run() 
{
    long beforeTime, timeDiff, sleepTime;
    beforeTime = System.currentTimeMillis();
    System.out.println(beforeTime);

    running = true;
    while (running)
    {
        System.out.println("Running");
        gameUpdate();
        gameRender();
        paintScreen();

        timeDiff = System.currentTimeMillis() - beforeTime;
        sleepTime = (long) period - timeDiff;

        if(sleepTime <= 0)
            sleepTime = 5;

        try 
        {
            Thread.sleep(sleepTime);
        } 
        catch (InterruptedException e) 
        {
            e.printStackTrace();
        }
        beforeTime = System.currentTimeMillis();
    }
    System.exit(0);
}

public void gameRender() 
{
    if (dbImage == null)
    {
        dbImage = createImage(WIDTH, HEIGHT);
    }
    else
        dbg = dbImage.getGraphics();

    dbg.setColor(Color.WHITE);
    dbg.fillRect(0, 0, WIDTH, HEIGHT);
}

public void paintComponent(Graphics g)
{
    super.paintComponent(g);
    g.drawImage(dbImage, 0, 0, null);
}

public void gameUpdate()
{

}

private void paintScreen()
{
    Graphics g;
    try
    {
        g = this.getGraphics();
        if (g!= null && dbImage!= null)
            g.drawImage(dbImage, 0, 0, null);
        Toolkit.getDefaultToolkit().sync();
        g.dispose();
    }
    catch (Exception e)
    {
        System.out.println("Error: " + e.getMessage());
    }
}

public void terminate()
{
    addKeyListener (new KeyAdapter()
    {
        public void keyPressed(KeyEvent e)
        {
            int keyCode = e.getKeyCode();
            if (keyCode == KeyEvent.VK_ESCAPE)
            {
                stopGame();
            }
        }
    });
}


}


import javax.swing.JFrame;


public class GameFrame extends JFrame
{
private final int WIDTH = 160;
private final int HEIGHT = WIDTH/12*9;
private final int RATIO = 3;

public GameFrame()
{
    setTitle("User Input Game");
    setSize(WIDTH*3,HEIGHT*3);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setResizable(false);
    GamePanel mainPanel = new GamePanel();
    add(mainPanel);
}
}




public class Main 
{

public static void main(String[] args) 
{
    new GameFrame().setVisible(true);
}

}
Was it helpful?

Solution

You need to change your startGame() method:

    animator = new Thread(new GamePanel());

You need to pass a Runnable (of which GamePanel is one) into the Thread constructor. The thread then runs the runnable when it starts.

OTHER TIPS

You don't seem to have a main method anywhere. Either in this class or an external class you need a main method that creates an instance of GamePanel and pass it as an argument to a Thread class. Like so:

public class Test
{
  public static void main(String[] args)
  {
    Thread t = new Thread(new GamePanel()).start();
  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top