弹跳球,Java中的“不反弹”

时间:2013-01-09 21:09:06

标签: java swing

我真的很难被困在这里。就日食而言,我的代码中没有错误。我的主要问题是;球没有弹跳,它保持稳定。你能控制下面的代码吗?提前谢谢。

Top(Ball)课程:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JPanel;

public class Top extends JPanel {

    private int topx, topy, vx, vy, r;

    public Top() {
        r = 20;
        topx = r;
        topy = r;
        vx = 10;
        vy = 12;

        /* THREAD.. PROBLEMS IN HERE I THINK */
        Thread zipThread = new Thread() {
            public void run() {
                while (true) {
                    if (topx - r < 0) {
                        vx = -vx;
                        topx = r;
                    } else if (topx + r > 300) {
                        vx = -vx;
                        topx = 300 - r;
                    }

                    if (topy - r < 0) {
                        vy = -vy;
                        topy = r;
                    } else if (topy + r > 300) {
                        vy = -vy;
                        topy = 300 - r;
                    }
                    repaint();
                    try {
                        Thread.sleep(30);
                    } catch (InterruptedException ex) {
                    }
                }
            }
        };
        zipThread.start();
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.ORANGE);
        g.fillOval(10, 10, r, r);
    }

}

主要课程:

import java.awt.Color;
import javax.swing.JFrame;

class Pencere extends JFrame {
    private Top top;

    public Pencere() {
        top = new Top();
        top.setBackground(Color.BLACK);
        add(top);
    }
}

public class Zipla {

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                Pencere p = new Pencere();
                p.setSize(300, 300);
                p.setVisible(true);
                p.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            }
        });

    }
}

2 个答案:

答案 0 :(得分:4)

你永远不会改变球的坐标:

g.fillOval(10, 10, r, r);

您应该将坐标存储在xy中,并相应地更改为vxvy

这样的事情:

private int topx, topy, vx, vy, r, x, y;

public Top() {
    r = 20;
    topx = r;
    topy = r;
    vx = 10;
    vy = 12;

    /*THREAD.. PROBLEMS IN HERE I THINK*/
    final Thread zipThread = new Thread() {
        @Override
        public void run() {
            while (true) {
                if (topx - r < 0) {
                    vx = -vx;
                    topx = r;
                } else if (topx + r > 300) {
                    vx = -vx;
                    topx = 300 - r;
                }

                if (topy - r < 0) {
                    vy = -vy;
                    topy = r;
                } else if (topy + r > 300) {
                    vy = -vy;
                    topy = 300 - r;
                }
                x += vx;
                y += vy;
                repaint();
                try {
                    Thread.sleep(30);
                } catch (final InterruptedException ex) {
                }
            }
        }
    };
    zipThread.start();
}

public void paintComponent(final Graphics g) {
    super.paintComponent(g);
    g.setColor(Color.ORANGE);
    g.fillOval(x, y, r, r);
}

答案 1 :(得分:1)

除了任何其他错误之外,您还尝试从事件派发线程外部not permitted调用repaint()

相反,您应该使用javax.swing.Timer](http://docs.oracle.com/javase/6/docs/api/javax/swing/Timer.html)来控制动画。