Animation: how to “roll” circle down the y-axis

2019-09-04 09:55发布

问题:

Here is the entirety of my program. The problem is in the ballroll method, getting the red dot/ ball to move straight down the screen on the y-axis to reach the inner blue circle for a "ripple effect" illusion. Please help me do this. I changed the code from the ball moving along the x axis, it needs to move vertically only. Thank you in advance for your help.

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);
    }
}