动画:如何使圆/球沿y轴向下移动

时间:2016-11-14 07:57:19

标签: java animation graphics sleep motion

我试图让红点沿着y轴直线移动。请帮忙。现在它不动了。我需要它向下移动x = 235,y = 0直到x = 235,y = 235才能产生运动错觉。这是代码(它很乱,因为我从左到右改变了球的运动,但是不能让它向下移动)。

import java.awt.Color;
import java.awt.Graphics;

public class Animation {

    public static void main(String [] args) {
        DrawingPanel panel = new DrawingPanel(350, 350);
        Graphics g = panel.getGraphics();
        background(g);
        ballroll(panel, g);
    }
    public static void ballroll(DrawingPanel panel, Graphics g) {
        //draw and roll the ball now
        g.setColor(Color.RED);
        int x = 245, y = 0, direction=1;
        while(y<245){
            g.fillOval(235, 0, 20, 20);
            //if (x==0){
                y+=60;
                direction *= -1;
            }
            //else if (x < 115){
                //direction *= -1;
                //y+=60;
            //}
            y+=direction*15;
            System.out.println(x);
            panel.sleep(80);

        }
        panel.sleep(350);
    //}
}

1 个答案:

答案 0 :(得分:0)

您计算y值,但您没有使用它。 我认为它应该是g.fillOval(235, y, 20, 20);