动画:如何沿y轴“滚动”圆圈

时间:2016-11-14 06:07:15

标签: java animation graphics

这是我的整个程序。问题出在滚珠方法中,让红点/球在y轴上直线向下移动到达内部蓝色圆圈以获得“涟漪效应”错觉。请帮我这样做。我改变了球沿x轴移动的代码,它只需要垂直移动。提前感谢您的帮助。

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);
        drawCircles(g);
        sunrayspotlight(g);
    }

    public static void background(Graphics g) {
        g.setColor(Color.BLUE);
        g.fillRect(0, 175, 350, 175);
        g.setColor(Color.ORANGE);
        g.fillRect(0, 0, 350, 175);

    }

    public static void sunrayspotlight(Graphics g) {
        int radius = 5;
          int x = 0;
          while(x <= 100) {
            g.setColor(Color.YELLOW);
            g.fillOval(0, 0, radius, radius);              
            try {
              Thread.sleep(5);
            } catch (InterruptedException e) {

            }
            x++;
            radius = radius + 5;
          }
    }

    public static void drawCircles(Graphics g) {
          int radius = 5;
          int x = 0;
          while(x <= 25) {
            g.setColor(Color.CYAN);
            int z = radius / 2;
            g.drawOval(245 - z, 245 - z, radius, radius);              
            try {
              Thread.sleep(100);
            } catch (InterruptedException e) {

            }
            x++;
            radius = radius + 5;
          }
    }


    private static void ballroll(DrawingPanel panel, Graphics g) {
        //draw and roll the ball now
        g.setColor(Color.RED);
        int x = 115, y = 110, direction=1;
        while(y<175){
            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(800);
    }
}

0 个答案:

没有答案