打开java applet时更改showStatus消息

时间:2016-03-01 17:00:41

标签: java text applet awt show

我正在使用applet编写Java游戏突破。但是当小程序打开时,我想要文本:"按下以启动"而不是自动"小程序已启动"。

我知道我可以使用showStatus将消息放入我的代码中("按下以启动")。但我也希望文本立即消失,以便其他文本可以放在那里(例如游戏结束时)。

但是,我不清楚在我的代码中将它放在哪里。这是我的代码。

public class Breakout extends Applet implements Runnable {
    // all the properties are stated here but this is not important for my question and saves a lot of space

    public void init() {
        hitsound = getAudioClip(getDocumentBase(), sound);
        hitsound.play();
        buffer = createImage(FieldWidth, FieldHeight);
        gContext = buffer.getGraphics();
        gContext.setColor(FieldColor);
        brick = new Field();
        paddle = new Paddle();
        ball = new Ball(brick, hitsound);
        gContext.fillRect(0, 0, FieldWidth, FieldHeight);
        paddle.draw(gContext);
        ball.draw(gContext);
        brick.draw(gContext);
    }

    public void start() {
        if(animate == null) {
            animate = new Thread(this);
            animate.start();
        }
    }
    public void restart() { }   

    public void stop() {
        if(animate != null) {
            animate = null;
        }
    }

    public void paint(Graphics g) {
        try {
            g.drawImage(buffer, 0, 0, this);
        }
        catch(Exception e) {}

// In the bottom of the screen the number of lives left is displayed
        g.drawString("Number of lives left: " + (ExtraLives- numberlost), 0, FieldHeight-22);
    }

    public void run() {
        while(true) {
            paddle.clear(gContext);
            ball.clear(gContext);

            if(leftArrow) {
                paddle.move(-PaddleSpeed);
                if(ballready && paddle.moved)
                    ball.move(-PaddleSpeed,0,gContext);
            }if(rightArrow) {
                paddle.move(PaddleSpeed);
                if(ballready && paddle.moved)
                    ball.move(PaddleSpeed,0,gContext);
            }if(!ballready) {
                ball.moves(gContext,paddle,this);
            }if(brick.bricksLeft()==0)
                win();

            paddle.draw(gContext);
            ball.draw(gContext);

            try {
                Thread.sleep(Slow);
            }
            catch (InterruptedException e) {}

            repaint();
        } 
    }

// This paints the graphics on the screen
    public void update(Graphics g) {
        paint(g);
    }

// When a key is pressed an action happens. 
// If the up arrow (key 1004) is pressed, the game starts
// if the left arrow (key 1006) or right arrow (key 1007) is pressed the paddle moves (true)
    public boolean keyDown(Event e, int key) {
        showStatus("Let's destroy those bricks haha");
        if(key==1004 && ballready) {
            ballready = false;
            ball.xchange = BallSpeedX;
            ball.ychange = BallSpeedY;
            showStatus("");
        }
        if(key==1006)
            leftArrow = true;
        if(key==1007)
            rightArrow = true;
        return true;
    }

// When a key is released and action happens
// if the left arrow (key 1006) or right arrow (key 1007) is pressed the paddle stops moving (false)
    public boolean keyUp(Event e, int key) {
        if(key==1006)
            leftArrow = false;
        if(key==1007)
            rightArrow = false;
        return true;
    }

// This method is called when you win 
    public void win() {
        showStatus("Victory!");
        gContext.setColor(FieldColor);
        gContext.fillRect(0,0,FieldWidth,FieldHeight);
        repaint();
    }

// This method is called when you miss the ball with the paddle, and the game starts over
    public void lose() {
// You lost a life, but number of lives lost is smaller than number of extra lives, so game continues
        if(numberlost < ExtraLives) {
            numberlost++;
            showStatus("You just lost a life, try again. Press up to start!");
            gContext.setColor(FieldColor);
            paddle.clear(gContext);
            ball.clear(gContext);
            paddle.go(FieldWidth/2-(PaddleWidth/2), PaddlePosition);
            ball.go(FieldWidth/2-BallSize, PaddlePosition-PaddleHeight);
            ballready=true;
            PaddleWidth -= -20;
        }
        else {
// You are game over and the game is restarted
            numberlost=0;
            showStatus("You loose. Press up to start over");
            gContext.setColor(FieldColor);
            gContext.fillRect(0,0,FieldWidth, FieldHeight);
            paddle.go(FieldWidth/2-(PaddleWidth/2), PaddlePosition);
            ball.go(FieldWidth/2-BallSize, PaddlePosition-PaddleHeight);
            brick.restart();
            brick.draw(gContext);
            ballready = true;
        }
    }

    public boolean getLeftArrow() {
        return leftArrow;
    }

    public boolean getRightArrow() {
        return rightArrow;
    }
}

有人知道答案吗?

0 个答案:

没有答案