Java圆形帧球动画

Java圆形帧球动画,java,animation,Java,Animation,我是java新手。我想创建一个简单的动画,其中一个球从左向右移动,我希望给它一个圆形的帧错觉(我不知道如何表达),如果球的一部分离开帧的边界(右侧),该部分将重新出现在帧的左侧。我不知道如何开始。到目前为止,我只设法让球移动 这是我代码的一部分 public class BallAnimation extends JFrame { private JButton left,right,up,down; private Balls ball = new Balls(); private Ell

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


我四处搜索,什么也没找到,也许我用的短语不对。任何帮助都将不胜感激

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

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

}
import javax.swing.JFrame;
导入javax.swing.JPanel;
导入java.awt.*;
导入java.awt.geom.AffineTransform;
导入java.awt.geom.Ellipse2D;
导入java.lang.reflect.InvocationTargetException;
/**
*马特于2015年11月30日创作。
*/
公开课弹跳球{
整数宽度=600;
内部高度=600;
双直径=100;
双v=10;
双vx=6;
双vy=8;
Ellipse2D球=新的Ellipse2D.Double(宽/4,高/4,直径,直径);
Ellipse2D桩=新的Ellipse2D.Double(宽度/2-直径,高度/2-直径,2*直径,2*直径);
JPanel显示器;
void buildGui(){
JFrame=新JFrame(“球”);
display=newjpanel(){
@凌驾
公共组件(图形g){
超级组件(g);
Graphics2D g2d=(Graphics2D)g;
g2d.setColor(Color.BLACK);
g2d.填补(职位);
g2d.setColor(Color.RED);
仿射Transform original=g2d.getTransform();

对于(int i=0;i为什么你有一个线程。在你的画图组件方法中睡眠?你需要更多的代码。我这里有一些问题,球会在左边画一个itme。在那之后,即使它超出边界也不会画。你知道吗?你的画图组件中有一堆奇怪的条件,我不知道何时/为什么/何地/h如何设置它们。您需要发布更多的代码。@ChiHong我用一个工作示例编辑了答案。非常感谢matt。我将尝试理解代码。