从另一个类启动小程序

时间:2019-04-13 06:38:31

标签: java

我正在尝试从另一个类(称为NewClass)中启动一个小应用程序(称为Tennis),在该类中,用户输入一个数字,并根据该数字应运行该小应用程序。我可以创建该小应用程序的对象,但是当我调用init函数时,我得到一个nullpointer异常。但是当我从没有新类的小程序本身运行它时,它运行良好。

public class NewClass {

    public static void main (String [] args) {

        Tennis t = new Tennis();
        t.init();  
    }

}
 public class Tennis extends Applet implements Runnable, KeyListener {

    final int WIDTH = 700, HEIGHT = 500;
    int pts = 0;
    Thread thread;
    HumanPaddle p1;
    AIPaddle p2;
    Ball b1;
    boolean gameStarted;
    boolean lost;
    Graphics gfx;
    Image img;
    String points;
    int level = 0;
    String user;
    public Tennis(String user){
        this.user = user;
    }
    public Tennis(){
    }


    public void init(){

        this.resize(WIDTH,HEIGHT);

        this.addKeyListener(this);
        p1 = new HumanPaddle(1);
        b1 = new Ball();
        p2 = new AIPaddle(2, b1);
        img = createImage(WIDTH,HEIGHT);

        gameStarted = false;
        lost = false;
        thread = new Thread(this);
        thread.start();
        gfx = img.getGraphics();

    }
    public void reset() {
        b1.x=350;
        b1.y=250; 
        b1.xVel = b1.getRandomSpeed() * b1.getRandomDirection();
        b1.yVel = b1.getRandomSpeed() * b1.getRandomDirection();
        gameStarted = false;
        p1.y=210;
        p2.y=210;
        lost = true;

    }

    public void paint (Graphics g){
        gfx.setColor(Color.black);
        gfx.fillRect(0, 0, WIDTH, HEIGHT);
        points = ""+b1.pts;
        if(b1.getX() < -10 || b1.getX() > 710){
            reset();
        } else {
        p1.draw(gfx);
        p2.draw(gfx);
        b1.draw(gfx);
        gfx.drawString("Score: " +points , 10 , 30);


        }

        if(!gameStarted && !lost){
            gfx.setColor(Color.white);
            gfx.drawString("Press Enter to Begin", 290,160);
            gfx.drawString("CS Project",310,130);

        } else if (!gameStarted && lost) {
            gfx.setColor(Color.red);
            gfx.drawString("Game Over" , 320 , 120);
            gfx.drawString("Score: " + points , 330 , 140);
            gfx.drawString("Press Enter to Begin", 300,160);

        }

        g.drawImage(img, 0, 0, this);
    }

    public void update (Graphics g){
        paint(g);


    }

    public void run(){
        for(;;){
            if (gameStarted) {

                b1.move();
                p1.move();
                p2.move();

                b1.checkPaddleCollision(p1,p2);
                //gfx.setColor(Color.red);
               //gfx.drawString("Score: " + pts, 300,150);
            }
            repaint();
            try {
                Thread.sleep(10);
            } catch (InterruptedException e){
                e.printStackTrace();
            }
        }

    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_UP){
            p1.setUpAccel(true);
        } else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
            p1.setDownAccel(true);
        } else if(e.getKeyCode() == KeyEvent.VK_ENTER && lost){
            gameStarted = true;
            points = "";
            b1.pts = 0;
        } else if(e.getKeyCode() == KeyEvent.VK_ENTER && !lost){
            gameStarted = true;
            level++;
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_UP){
            p1.setUpAccel(false);
        } else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
            p1.setDownAccel(false);
        }
    }

}
Exception in thread "main" java.lang.NullPointerException
    at infMode.Tennis.init(Tennis.java:56)
    at infMode.NewClass.main(NewClass.java:25)

(Tennis.java:56)

gfx = img.getGraphics();

(NewClass.java:25)

t.init();

0 个答案:

没有答案