Java圆形框架球动画

时间:2015-11-29 10:43:16

标签: java animation

我是java的新手。我想创建一个简单的动画,球从左到右移动,我希望给它一个圆形框架幻觉(我不知道如何用它来表达)如果球的一部分离开框架的边框(右侧),那部分将重新出现在我的框架的左侧。我不知道如何开始。到目前为止,我只是设法让球移动。

这是我的代码的一部分

public class BallAnimation extends JFrame {


private JButton left,right,up,down;
private Balls ball = new Balls();
private Ellipse2D circle;

public BallAnimation()
{
    add(ball);
}

public class Balls extends JPanel{

private double X=0,Y=0;
private int i=1;
public void paintComponent(Graphics g)
{
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D) g;
    g.setColor(Color.CYAN);
    g.fillRect(0, 0, getWidth(), (getHeight()));

    if(X==0 && Y ==0)
    {
    X=getWidth()/2.1;
    Y=getHeight()/2.3;
    g.setColor(Color.RED);
    circle= new Ellipse2D.Double(X,Y,50,50);
    g2.fill(circle);
    }
    else
    {
        g.setColor(Color.RED);
        circle= new Ellipse2D.Double(X,Y,50,50);
        g2.fill(circle);
    }
    if(i==1)
    {
        X=X+10;
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        repaint();
    }

}
}

public static void main(String[] args)
{
    BallAnimation test = new BallAnimation();
    test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    test.setSize(700,500);
    test.setVisible(true);
}

}

我四处搜索并没有找到任何内容,也许我使用的短语不对。任何帮助,将不胜感激。

1 个答案:

答案 0 :(得分:0)

你可以做一个简单的计算来检查球是否已经越过组件的边缘。例如。

Rectangle rect = circle.getBounds();
Rectangel bounds = getBounds();

if(rect.getX() + rect.getWidth()>bounds.getWidth()){
    //draw an ellipse that is offset by the width of the component.
    Ellipse2D c2 = new Ellipse2D.Double(circle.getX()-bounds.getWidth(), circle.getY(), circle.getWidth(), circle.getHeight());    
    g2.fill(c2);
}

你去,如果它在右侧超出界限,它将被绘制在左侧。

这是一个例子,而不是检查球是否包裹。我只是把球抽了9次,这样当它穿过边界时就会被画出来。

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.geom.Ellipse2D;
import java.lang.reflect.InvocationTargetException;

/**
 * Created by matt on 11/30/15.
 */
public class BouncingBall {
    int width = 600;
    int height = 600;
    double diameter = 100;
    double v = 10;
    double vx = 6;
    double vy = 8;
    Ellipse2D ball = new Ellipse2D.Double(width/4, height/4, diameter, diameter);
    Ellipse2D post = new Ellipse2D.Double(width/2 - diameter, height/2 - diameter, 2*diameter, 2*diameter);

    JPanel display;
    void buildGui(){
        JFrame frame = new JFrame("ball");

        display = new JPanel(){
            @Override
            public void paintComponent(Graphics g){
                super.paintComponent(g);
                Graphics2D g2d = (Graphics2D)g;

                g2d.setColor(Color.BLACK);
                g2d.fill(post);
                g2d.setColor(Color.RED);
                AffineTransform original = g2d.getTransform();
                for(int i = 0; i<3; i++){
                for(int j = 0; j<3; j++){
                        AffineTransform wrapper = new AffineTransform(original);
                        wrapper.translate((i-1)*width, (j-1)*height);
                        g2d.setTransform(wrapper);
                        g2d.draw(ball);
                    }
                }
                g2d.setTransform(original);
            }
        };

        display.setMinimumSize(new Dimension(width, height));
        display.setPreferredSize(new Dimension(width, height));
        display.setMaximumSize(new Dimension(width, height));

        frame.setContentPane(display);
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);


    }

    void startSimulation(){
        while (!Thread.interrupted()&&display.isVisible()) {

            double x = ball.getX() + vx;
            double y = ball.getY() + vy;

            //wrap the ball around.
            if(x>width) x = x-width;
            if(x<0) x = x+width;
            if(y>height) y = y-height;
            if(y<0) y = y+height;

            ball.setFrame(x, y, diameter, diameter);

            //check for collision.

            double dx = ball.getCenterX() - 0.5*width;
            double dy = ball.getCenterY() - 0.5*height;

            double distance = Math.sqrt(dx*dx + dy*dy);
            if(distance < 1.5*diameter){
                vx = v*dx/distance;
                vy = v*dy/distance;
            }

            display.repaint();

            try {
                Thread.sleep(33);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }

        }
    }
    public static void main(String[] args) throws Exception {
        BouncingBall b = new BouncingBall();
        EventQueue.invokeAndWait(b::buildGui);
        b.startSimulation();
    }

}