简体   繁体   中英

Adding a counter on screen in Java

I have just started Java course at university, my last assingment was to create a simple ball using GOval and make it bounce off window borders. I wanted to add a simple counter over the ball that would increase with every bounce.

I know how to make to counter count hits, i just dont know how to make it show on screen, put it over my ball and follow it.

public class Ball extends GraphicsProgram{
    private static final double BALL_SIZE=50;
    private static final double SPEED=3;
    private static final double GRAVITY=0;
    private static final double ELASTICITY=1;
    private static final double PAUSE = 1000/48.0;
    private static boolean HIT = false;
    private static boolean MOVE_TOP = false;
    private static int COUNTER=0;

    public void run(){
        GOval ball = makeBall();
        add(ball);
        bounce(ball);
    }

    private GOval makeBall(){
        GOval result = new GOval (0,0,BALL_SIZE,BALL_SIZE);
    result.setFilled(true);
    result.setColor(Color.BLACK);
    return result;

}

private void bounce(GOval ball){
    double dx=SPEED;
    double dy=20;
    while(true){
        ball.move(dx,dy);
        if(MOVE_TOP==false){
            dy+=GRAVITY;
            }
        else{
            dy-=GRAVITY;
            }
        if(ballHitBottom(ball) && dy>=0){
            dy*=-ELASTICITY;
            COUNTER++;
            if(HIT==false)
            HIT=true;

        }
        if(ballHitTop(ball) && dy<=0){
            if(HIT){
            dy*=-ELASTICITY;
            COUNTER++;
            }
        }

        if(ballHitRight(ball) && dx>=0){
            if(HIT){
            dx*=-ELASTICITY;
            COUNTER++;
            }
        }

        if(ballHitLeft(ball) && dx<=0){
            if(HIT){
            dx*=-ELASTICITY;
            COUNTER++;
            }
        }
        pause(PAUSE);
    }
}
private boolean ballHitBottom(GOval ball){
    MOVE_TOP=true;
    double bottomY=ball.getY() + ball.getHeight();
    return bottomY >= getHeight();
}

private boolean ballHitTop(GOval ball){
    double topY=ball.getY();
    MOVE_TOP=false;
    return topY <= 0;
}

private boolean ballHitRight(GOval ball){
    double rightX=ball.getX() + ball.getWidth();
    return rightX >= getWidth();
}

private boolean ballHitLeft(GOval ball){
    double leftX=ball.getX();
    return leftX <= 0;
}
}
private GLabel label;

public void run(){
    GOval ball = makeBall();
    label = new GLabel(String.valueOf(COUNTER), BALL_SIZE/2, BALL_SIZE/2);
    add(ball);
    add(label);
    bounce(ball);
}

And inside move , under ball.move(dx,dy) :

label.move(dx,dy);

And inside move , above pause(PAUSE) :

label.setLabel(String.valueOf(COUNTER));

要使计数器更新,请在每次击球时使用label.revalidate()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM