开发者

Java app will not display, but the solution must have runnable outside of main class

开发者 https://www.devze.com 2023-01-31 14:20 出处:网络
I am trying to make a simple Java tetris game.I am following a tutorial on GameDev.net.I want to go outside the tutorial by keeping the runnable Frame class outside 开发者_运维问答of the main class,

I am trying to make a simple Java tetris game. I am following a tutorial on GameDev.net. I want to go outside the tutorial by keeping the runnable Frame class outside 开发者_运维问答of the main class, rather than putting everything in one class like the tutorial says (the code works if I do so).

This code is my attempt to do the following (below), and I presume that the runnable is called, but the app doesn't display for some reason.

Can someone clarify what I did wrong and what I need to do while keeping my runnable in the Frame class

Main.java class

import java.lang.String;

public class Main {
public static void main(String args[])
{
    //boolean victory = false;
    Frame bob = new Frame();
    bob.init();
}
}

Frame.java class:

import java.applet.*;
import java.awt.Graphics;

public class Frame extends Applet implements Runnable {
Thread t;
int i;

public void init()
{
    t = new Thread(this);
    t.start();
    i = 0;
}

public void run()
{
    while(true)
    {
      i++;
      repaint();

      try {
        t.sleep(1000/30);
      } catch (InterruptedException e) { ; }
    }
}

public void paint(Graphics g)
{
    g.drawString("i = "+i,10,20);
}
}

Help would be much appreciated!


To run an applet, create an HTML document.

sample.html

<applet code="Frame" width="100" height="100"></applet>

Now open "sample.html" document in web-browser or from the command-prompt, issue following command

>appletviewer sample.html
0

精彩评论

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

关注公众号