将Java小程序转换为应用程序

将Java小程序转换为应用程序,java,applet,Java,Applet,所以我遵循一本免费手册,指导如何用Java编写小行星,它运行得非常好。我想将它添加到一个程序中,这样我可以调用它,运行一段时间,然后让它返回一个int。我正在制作的程序是一个应用程序,我已经搜索了一段时间,尝试了,但失败了,我想知道如何使它作为一个应用程序运行。我在这里读到的东西说我应该这样做: public class Main extends JFrame { public static void main(String[] args) { AsteroidsG

所以我遵循一本免费手册,指导如何用Java编写小行星,它运行得非常好。我想将它添加到一个程序中,这样我可以调用它,运行一段时间,然后让它返回一个int。我正在制作的程序是一个应用程序,我已经搜索了一段时间,尝试了,但失败了,我想知道如何使它作为一个应用程序运行。我在这里读到的东西说我应该这样做:

public class Main extends JFrame {
 public static void main(String[] args) {         
     AsteroidsGame game = new AsteroidsGame();
     JFrame frame = new JFrame();
     frame.setLayout(new GridLayout (1,1));
     frame.add(game);
     frame.setSize(500,500);         
     game.init();
     game.start();       
     frame.setVisible(true);
   }
}

但是我得到了一个错误

线程“main”java.lang.NullPointerException中出现异常

在asterosgame.init(asterosgame.java:49)

Main.Main(Main.java:15)

这将我指向下面的第49行,我的game.init()指向第15行

        g = img.getGraphics();
这是小行星游戏的代码,我太累了,想不起来了。我只需要让它作为一个应用程序运行,在这一点上任何其他代码更改都是无关的。我知道这有很多代码,但我希望这是我错过的一个简单的东西。非常感谢您的帮助。谢谢大家!

    import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;

public class AsteroidsGame extends Applet implements Runnable, KeyListener {
Thread thread;
Dimension dim;
Image img;
Graphics g;
long endTime, startTime, framePeriod;
Ship ship;
boolean paused; // True if the game is paused. Enter is the pause key
Shot[] shots;
int numShots;
boolean shooting;
Asteroid[] asteroids; // the array of asteroids
int numAsteroids; // the number of asteroids currently in the array
double astRadius, minAstVel, maxAstVel; // values used to create
// asteroids
int astNumHits, astNumSplit;
int level; // the current level number
int money;
double fuel;
Random rand = new Random();

public void init() {
    resize(500, 500);
    shots = new Shot[41]; // 41 is a shot's life period plus one.
    // since at most one shot can be fired per frame,
    // there will never be more than 41 shots if each one only
    // lives for 40 frames.
    numAsteroids = 0;
    level = rand.nextInt(25)+1; // will be incremented to 1 when first level is set up
    astRadius = 10; // values used to create the asteroids
    minAstVel = 2;
    maxAstVel = 8;
    astNumHits = 3;
    astNumSplit = 2;
    money = 0;
    fuel = 100;

    endTime = 0;
    startTime = 0;
    framePeriod = 25;
    addKeyListener(this); // tell it to listen for KeyEvents
    dim = getSize();
    img = createImage(dim.width, dim.height);
    g = img.getGraphics();
    thread = new Thread(this);
    thread.start();
}

public void setUpNextLevel() { // start new level with one more asteroid
    level++;
    // create a new, inactive ship centered on the screen
    // I like .35 for acceleration, .98 for velocityDecay, and
    // .1 for rotationalSpeed. They give the controls a nice feel.
    ship = new Ship(250, 250, 0, .35, .98, .1, 12);
    numShots = 0; // no shots on the screen at beginning of level
    paused = false;
    shooting = false;
    // create an array large enough to hold the biggest number
    // of asteroids possible on this level (plus one because
    // the split asteroids are created first, then the original
    // one is deleted). The level number is equal to the
    // number of asteroids at it's start.
    asteroids = new Asteroid[level
            * (int) Math.pow(astNumSplit, astNumHits - 1) + 1];
    numAsteroids = level;
    // create asteroids in random spots on the screen
    for (int i = 0; i < numAsteroids; i++)
        asteroids[i] = new Asteroid(Math.random() * dim.width,
                Math.random() * dim.height, astRadius, minAstVel,
                maxAstVel, astNumHits, astNumSplit);
}

public void paint(Graphics gfx) {
    g.setColor(Color.black);
    g.fillRect(0, 0, 500, 500);
    for (int i = 0; i < numShots; i++)
        // draw all the shots on the screen
        shots[i].draw(g);
    for (int i = 0; i < numAsteroids; i++)
        asteroids[i].draw(g);
    ship.draw(g); // draw the ship
    g.setColor(Color.cyan); // Display level number in top left corner
    g.drawString("Level " + level, 20, 20);
    g.drawString("Money " + money, 80, 20);
    g.drawString("Fuel " + fuel, 20, 50);
    gfx.drawImage(img, 0, 0, this);
}

public void update(Graphics gfx) {
    paint(gfx);
}

public void run() {
    for (;;) {
        startTime = System.currentTimeMillis();
        // start next level when all asteroids are destroyed
        if (numAsteroids <= 0)
            setUpNextLevel();
        if (!paused) {
            ship.move(dim.width, dim.height); // move the ship
            // move shots and remove dead shots
            for (int i = 0; i < numShots; i++) {
                shots[i].move(dim.width, dim.height);
                // removes shot if it has gone for too long
                // without hitting anything
                if (shots[i].getLifeLeft() <= 0) {
                    // shifts all the next shots up one
                    // space in the array
                    deleteShot(i);
                    i--; // move the outer loop back one so
                    // the shot shifted up is not skipped
                }

            }
            if(ship.accelerating && fuel>0){
                fuel -= 1.5;
            }
            if(ship.accelerating && fuel == 0)
            {
                ship.accelerating = false;
            }
            // move asteroids and check for collisions
            updateAsteroids();
            if (shooting && ship.canShoot()) {
                // add a shot on to the array
                shots[numShots] = ship.shoot();
                numShots++;
            }
        }
        repaint();
        try {
            endTime = System.currentTimeMillis();
            if (framePeriod - (endTime - startTime) > 0)
                Thread.sleep(framePeriod - (endTime - startTime));
        } catch (InterruptedException e) {
        }
    }
}

private void deleteShot(int index) {
    // delete shot and move all shots after it up in the array
    numShots--;
    for (int i = index; i < numShots; i++)
        shots[i] = shots[i + 1];
    shots[numShots] = null;
}

private void deleteAsteroid(int index) {
    // delete asteroid and shift ones after it up in the array
    numAsteroids--;
    for (int i = index; i < numAsteroids; i++)
        asteroids[i] = asteroids[i + 1];

    asteroids[numAsteroids] = null;
}

private void addAsteroid(Asteroid ast) {
    // adds the asteroid passed in to the end of the array
    asteroids[numAsteroids] = ast;
    numAsteroids++;
}

private void updateAsteroids() {
    for (int i = 0; i < numAsteroids; i++) {
        // move each asteroid
        asteroids[i].move(dim.width, dim.height);
        // check for collisions with the ship, restart the
        // level if the ship gets hit
        if (asteroids[i].shipCollision(ship)) {
            money+= Math.random()*1000;
            deleteAsteroid(i);

            //level--; // restart this level
            //numAsteroids = 0;
            return;
        }
        // check for collisions with any of the shots
        for (int j = 0; j < numShots; j++) {
            if (asteroids[i].shotCollision(shots[j])) {
                // if the shot hit an asteroid, delete the shot
                deleteShot(j);
                // split the asteroid up if needed
                if (asteroids[i].getHitsLeft() > 1) {
                    for (int k = 0; k < asteroids[i].getNumSplit(); k++)
                        addAsteroid(asteroids[i].createSplitAsteroid(
                                minAstVel, maxAstVel));
                }
                // delete the original asteroid
                deleteAsteroid(i);
                j = numShots; // break out of inner loop - it has
                // already been hit, don't need to check
                // for collision with other shots
                i--; // don't skip asteroid shifted back into
                // the deleted asteroid's position
            }
        }
    }
}

public void keyPressed(KeyEvent e) {
    if (e.getKeyCode() == KeyEvent.VK_ENTER) {
        // These first two lines allow the asteroids to move
        // while the player chooses when to enter the game.
        // This happens when the player is starting a new life.
        if (!ship.isActive() && !paused)
            ship.setActive(true);
        else {
            paused = !paused; // enter is the pause button
            if (paused) // grays out the ship if paused
                ship.setActive(false);
            else

                ship.setActive(true);
        }
    } else if (paused || !ship.isActive()) // if the game is
        return; // paused or ship is inactive, do not respond
    // to the controls except for enter to unpause
    else if (e.getKeyCode() == KeyEvent.VK_UP)
    {if(fuel>0){
        ship.setAccelerating(true);

        //fuel -= 1.5;  
        }
    else{
        fuel = 0;
    }
}
    else if (e.getKeyCode() == KeyEvent.VK_LEFT)
    {

        ship.setTurningLeft(true);
    }

    else if (e.getKeyCode() == KeyEvent.VK_RIGHT)
        ship.setTurningRight(true);
        //else if (e.getKeyCode() == KeyEvent.VK_CONTROL)
        //shooting = true; // Start shooting if ctrl is pushed
}

public void keyReleased(KeyEvent e) {
    if (e.getKeyCode() == KeyEvent.VK_UP)
        ship.setAccelerating(false);
    else if (e.getKeyCode() == KeyEvent.VK_LEFT)
        ship.setTurningLeft(false);
    else if (e.getKeyCode() == KeyEvent.VK_RIGHT)
        ship.setTurningRight(false);
    //else if (e.getKeyCode() == KeyEvent.VK_CONTROL)
        //shooting = false;
}

public void keyTyped(KeyEvent e) {
}
}
import java.applet.*;
导入java.awt.*;
导入java.awt.event.*;
导入java.util.Random;
公共类AsterosGame扩展小程序实现可运行的KeyListener{
螺纹;
尺寸标注;
图像img;
图形g;
长结束时间、开始时间、帧周期;
船舶;
布尔暂停;//如果游戏暂停,则为True。Enter是暂停键
射击[]射击;
国际努姆肖特;
布尔射击;
小行星[]小行星;//小行星阵列
int numAsteroids;//阵列中当前小行星的数量
双astRadius、minAstVel、maxAstVel;//用于创建
//小行星
int astNumHits,astNumSplit;
int level;//当前级别的编号
国际货币;
双燃料;
Random rand=新的Random();
公共void init(){
调整大小(500500);
快照=新快照[41];//41是快照的生命周期加上一。
//因为每帧最多只能发射一次子弹,
//如果每一次都是这样,那么就不会有超过41次的射击
//寿命为40帧。
核仁母细胞=0;
level=rand.nextInt(25)+1;//在设置第一个级别时将递增为1
astRadius=10;//用于创建小行星的值
MinastLevel=2;
maxAstVel=8;
astNumHits=3;
astNumSplit=2;
货币=0;
燃料=100;
endTime=0;
起始时间=0;
帧周期=25;
addKeyListener(this);//告诉它侦听KeyEvents
dim=getSize();
img=createImage(尺寸宽度、尺寸高度);
g=img.getGraphics();
线程=新线程(此);
thread.start();
}
public void setUpNextLevel(){//使用一个或多个小行星开始新级别
级别++;
//在屏幕中央创建一艘新的非活动船舶
//我喜欢.35的加速度,98的速度,还有
//.1的旋转速度。他们给控制一个很好的感觉。
船舶=新船舶(250250,0,35,98,1,12);
numShots=0;//关卡开始时屏幕上没有快照
暂停=错误;
射击=假;
//创建一个足够大的数组以容纳最大的数字
//在这一级别上可能存在的小行星数量(加上一个,因为
//首先创建分裂的小行星,然后创建原始小行星
//一个已删除)。级别编号等于
//开始时小行星的数量。
小行星=新小行星[级别]
*(int)Math.pow(astNumSplit,astNumHits-1)+1];
珠母粒=水平;
//在屏幕上的随机点中创建小行星
对于(int i=0;iimg = createImage(dim.width, dim.height);
g = img.getGraphics();
thread = new Thread(this);
thread.start();
 new Thread(new Runnable() {
    public void run() {
        do {
            try {
                Thread.sleep(25);
            } catch(InterruptedException e) {
                System.out.println("wait interrupted");
                break;
            }
        } while(!isDisplayable());
        // Now it should be safe to make these calls.
        img= createImage(dim.width, dim.height);
        g = img.getGraphics();
        start();
    }
}).start();