簡體   English   中英

如何顯示來自另一個類的內容

[英]How to display content from another class

我的家庭作業有問題。 我正在嘗試進行三球比賽。 我創建了球,碰撞的情況,但是當我嘗試顯示碰撞發生時的點時,它沒有顯示。

這是我的球類

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 開始然后什么也沒有發生。 謝謝您的回答。

歡迎來到 SO。 評估代碼中的問題非常困難,因為它的結構很差。 您還沒有封裝球的細節,因此邏輯在一個地方。 相反,您已經多次重復相同的邏輯。 這使您的代碼容易出錯且難以閱讀。

我建議您首先創建一個代表單個球的球類。 這是一個適合您的框架。 我建議您為每種方法編寫單元測試,並確保它們在編寫“主”循環之前有效工作。 如果您可以使用此結構重新發布,我懷疑您會發現幫助更有效:

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

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM