开发者

What is Wrong in this Simple Java Game Loop?

I've tried to figure this out or find another way around for about 3 Days now, but I can't make it work... Basic I'm converting an Game Applet to an Application, but I can't get the Game Loop Working. Plus in NetBeans, I made a Window, but it doesn't show up even if I Set it Visible. If you got a tutorial on how to make a Simple Game loop, it would be great. I'm desperate, Please Help Me!

Here's the code of my main Class

package MainClass;

import javax.swing.*;

 public class MainClass implements Runnable{

    Painter panel = new Painter();
    JavaPowderToy Screen = new JavaPowderToy();
    Thread t = new Thread();

    public void run()
    {

    Initialize();

    while(true)
    {
        try
        {
            panel.Paint();
            Thread.sleep(15);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }   
    }

    }

void MainClass()
{
  t.start();  

}

public static void main(String[] args) {
    MainClass Java = new MainClass();



}

private void Initialize()
{
    panel.InitializePainting();
    new Window().setVisible(true);
}

}

Here's my Painter Class:

    package thejavapowdertoy;

    import java.awt.Color;
    import java.awt.Graphics2D;
    import java.awt.Toolkit;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.awt.image.BufferedImage;
    import javax.swing.JPanel;


    public class Painter extends JPanel implements KeyListener{



    BufferedImage buffer;

   public Painter()
    {
        setIgnoreRepaint(true);
        addKeyListener(this);
        setFocusable(true);
    }

   public void InitializePainting()
   {

   }

   public void Paint()
   {
       Graphics2D b = buffer.createGraphics();
       Graphics2D g = (Graphics2D)this.getGraphics();
 开发者_Go百科      b.setColor(Color.red);
       b.fillRect(50, 50, 50, 50);
       b.dispose();
       g.drawImage(buffer, 0, 0, this);
       Toolkit.getDefaultToolkit().sync();
       g.dispose();
   }

    public void keyTyped(KeyEvent e) {
        throw new UnsupportedOperationException("Not supported yet.");
    }


    public void keyPressed(KeyEvent e) {
        throw new UnsupportedOperationException("Not supported yet.");
    }


    public void keyReleased(KeyEvent e) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

}

Thanks


Your MainClass implements Runnable thus I assume you want it to be run by the thread. The thread t, however, doesn't know that.

You need to pass the MainClass instance to the thread's constructor: new Thread(this);

Additionally, your Painter panel is not connected to the window you create and thus it would not be visible. Try creating a JFrame or JWindow and place the painter in it.


void MainClass()
{
  t.start();  

}

This is not a constructor, but a method! This is because the thread is not started. Remove the void

And also look at the other answers. Basically you have two threads. One the MainClass itself and then t inside the MainClass.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜