如何显示其他课程的内容

时间:2019-05-13 22:49:42

标签: java jframe jlabel

我对作业有疑问。我正在尝试制作3球游戏。我创建了球,用于碰撞的情况,但是当我尝试显示发生碰撞的点时,它不会显示。

这是我的舞会

int point = 0;
public int setpoint() { 
    point = point+1;
        System.out.println(point);
    return point;
}

double sz=40;
double Vx=2,Vy=2;
double Vx2=0,Vy2=0,Vx3=0,Vy3=0,Vx4=0,Vy4=0;
double V=10;
Ellipse2D.Double ball = new Ellipse2D.Double(200,200,sz,sz);
Ellipse2D.Double ball2 = new Ellipse2D.Double(150,100,sz,sz);
Ellipse2D.Double ball3 = new Ellipse2D.Double(400,100,sz,sz);
Ellipse2D.Double ball4 = new Ellipse2D.Double(240,100,sz,sz);
Rectangle rect = new Rectangle(0,0,3,200);
Timer t = new Timer(15,this);

@Override
public void actionPerformed(ActionEvent e) {
    updateBallPosition();


}


public void updateBallPosition() {


    ball.x+=Vx;
    ball.y+=Vy;

    ball2.x+=Vx2;
    ball2.y+=Vy2;

    ball3.x+=Vx3;
    ball3.y+=Vy3;


    ball4.x+=Vx4;
    ball4.y+=Vy4;


    Vx = Vx*0.99;
    Vy = Vy*0.99;

    Vx2 = Vx2*0.99;
    Vy2 = Vy2*0.99;

    Vx3 = Vx3*0.99;
    Vy3 = Vy3*0.99;

    Vx4 = Vx4*0.99;
    Vy4 = Vy4*0.99;




    if(ball.x<0 || ball.x>getWidth()-sz) 
        Vx=-Vx;

    if(ball.y<0 || ball.y>getHeight()-sz) Vy=-Vy;

    if(ball2.x<0 || ball2.x>getWidth()-sz) Vx2=-Vx2;
    if(ball2.y<0 || ball2.y>getHeight()-sz) Vy2=-Vy2 ;

    if(ball3.x<0 || ball3.x>getWidth()-sz) Vx3=-Vx3;
    if(ball3.y<0 || ball3.y>getHeight()-sz) Vy3=-Vy3;

    if(ball4.x<0 || ball4.x>getWidth()-sz) Vx4=-Vx4;
    if(ball4.y<0 || ball4.y>getHeight()-sz) Vy4=-Vy4;


    double dx = ball2.x - ball.x;
    double dy = ball2.y - ball.y;

    double dx1 = ball3.x - ball.x;
    double dy1 = ball3.y - ball.y;

    double dx2 = ball4.x - ball.x;
    double dy2 = ball4.y - ball.y;

    double dx4 = ball3.x - ball4.x;
    double dy4 = ball3.y - ball4.y;

    double dx5 = ball4.x - ball2.x;
    double dy5 = ball4.y - ball2.y;

    double dx6 = ball3.x - ball2.x;
    double dy6 = ball3.y - ball2.y;



    double Umag = Math.hypot(dx, dy);
    double Umag2 = Math.hypot(dx1, dy1);
    double Umag3 = Math.hypot(dx2, dy2);

    double Umag4 = Math.hypot(dx4, dy4);
    double Umag5 = Math.hypot(dx5, dy5);
    double Umag6 = Math.hypot(dx6, dy6);


    if(Umag <= ball.width)
    {


        double VrMag = (Vx*dx + Vy*dy )/ Umag;
        double Vrx = VrMag * dx / Umag;
        double Vry = VrMag * dy / Umag;

        double Vtx = Vx - Vrx;
        double Vty = Vy - Vry;


        Vx = Vtx;
        Vy = Vty;
        Vx2 = Vrx;
        Vy2 = Vry;
        setpoint();
        a++;
    }
    else if(Umag2 <= ball.width)
    {


        double VrMag1 = (Vx*dx1 + Vy*dy1 )/ Umag2;
        double Vrx1 = VrMag1 * dx1 / Umag2;
        double Vry1 = VrMag1 * dy1 / Umag2;

        double Vtx1 = Vx - Vrx1;
        double Vty1 = Vy - Vry1;


        Vx = Vtx1;
        Vy = Vty1;
        Vx3 = Vrx1;
        Vy3 = Vry1;
        setpoint();
        a++;

    }
    else if(Umag3 <= ball.width)
    {


        double VrMag2 = (Vx*dx2 + Vy*dy2 )/ Umag3;
        double Vrx2 = VrMag2 * dx2 / Umag3;
        double Vry2 = VrMag2 * dy2 / Umag3;

        double Vtx2 = Vx - Vrx2;
        double Vty2 = Vy- Vry2;


        Vx = Vtx2;
        Vy = Vty2;
        Vx4 = Vrx2;
        Vy4 = Vry2;
        setpoint();
        a++;
    }

    else if(Umag4 <= ball4.width)
    {


        double VrMag4 = (Vx4*dx4 + Vy4*dy4 )/ Umag4;
        double Vrx4 = VrMag4 * dx4 / Umag4;
        double Vry4 = VrMag4 * dy4/ Umag4;

        double Vtx4 = Vx4 - Vrx4;
        double Vty4 = Vy4 - Vry4;


        Vx4 = Vtx4;
        Vy4 = Vty4;
        Vx3 = Vrx4;
        Vy3 = Vry4;
        setpoint();

    }
    else if(Umag5 <= ball2.width)
    {


        double VrMag5 = (Vx2*dx5 + Vy2*dy5 )/ Umag5;
        double Vrx5 = VrMag5 * dx5 / Umag5;
        double Vry5 = VrMag5 * dy5 / Umag5;

        double Vtx5 = Vx2 - Vrx5;
        double Vty5 = Vy2 - Vry5;


        Vx2 = Vtx5;
        Vy2 = Vty5;
        Vx4 = Vrx5;
        Vy4 = Vry5;
        setpoint();
    }
    else if (Umag6 <= ball2.width)
    {


        double VrMag6 = (Vx2*dx6 + Vy2*dy6 )/ Umag6;
        double Vrx6 = VrMag6 * dx6 / Umag6;
        double Vry6 = VrMag6 * dy6 / Umag6;

        double Vtx6 = Vx2 - Vrx6;
        double Vty6 = Vy2 - Vry6;


        Vx2 = Vtx6;
        Vy2 = Vty6;
        Vx3 = Vrx6;
        Vy3 = Vry6;
        setpoint();

    }
And this is my main function:

    BallPanel op = new BallPanel();



    JLabel ds = new JLabel("Game is starting! ");
    JLabel asd = new JLabel(" ");


    Timer s = new Timer(1000,new ActionListener() {

        int time = 0;   
        int y = op.setpoint();

    @Override
    public void actionPerformed(ActionEvent e) {
        time++;


        ds.setText(format(time));
        asd.setText(format(y));
    }

    });
    s.start();



    JFrame frame = new JFrame("Ball");
    frame.setLayout(new BorderLayout());
    JPanel statusbar = new JPanel();
    frame.add(statusbar, BorderLayout.SOUTH);
    JLabel as = new JLabel("Time: ");
    JLabel das = new JLabel("seconds");
    statusbar.add(as);
    statusbar.add(ds);
    statusbar.add(das);
    statusbar.add(asd);


    BallPanel bp = new BallPanel();





    bp.addKeyListener(bp);
    bp.addMouseListener(bp);
    frame.add(bp);
    frame.setSize(500,500);
    frame.addKeyListener(bp);


    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}


 public static String format(int i) {
        String result = String.valueOf(i);
        if (result.length() == 1) {
            result = "0" + result;
        }
        return result;
    }

它应该增加点并显示在状态栏中,但它仅以01开头,然后什么也没有发生。谢谢您的回答。

1 个答案:

答案 0 :(得分:0)

欢迎来到SO。评估代码中存在的问题非常困难,因为它的结构很差。您没有封装球的细节,因此逻辑位于一个地方。相反,您已经重复了几次相同的逻辑。这使您的代码易于出错并且难以阅读。

我建议您首先创建一个代表单个球的球类。这是一个适合您的框架。我建议您为每种方法编写单元测试,并确保它们在编写“主”循环之前有效地工作。如果您可以使用此结构重新发布,我怀疑您会发现帮助更有效:

class Ball {
    private Ellipse2D.Double shape;
    private double xVelocity;
    private double yVelocity;

    public void update(double period) {
        ...
    }

    public boolean overlaps(Ball other) {
        ...
    }
}