Java:球与框架的碰撞

Java:球与框架的碰撞,java,swing,jframe,awt,java-2d,Java,Swing,Jframe,Awt,Java 2d,不知道该怎么做真让我受不了。我现在有一个框架,上面有一个球,它沿对角线方向向下和向右移动,当它与框架边缘碰撞时,它需要反弹。我能自己解决的弹跳部分是,我需要帮助的是球知道它什么时候击中框架的边缘 主要内容: 查看器类: public class Viewer extends JFrame { JButton go = new JButton("GO"); JButton stop = new JButton("STOP"); JPanel buttons = new JPanel(); Time

不知道该怎么做真让我受不了。我现在有一个框架,上面有一个球,它沿对角线方向向下和向右移动,当它与框架边缘碰撞时,它需要反弹。我能自己解决的弹跳部分是,我需要帮助的是球知道它什么时候击中框架的边缘

主要内容:

查看器类:

public class Viewer extends JFrame {
JButton go = new JButton("GO");
JButton stop = new JButton("STOP");
JPanel buttons = new JPanel();
Timer timer;

ControlledBall cbPanel = new ControlledBall();
JPanel left = new JPanel();
JPanel right = new JPanel();
JPanel top = new JPanel();


class gogoListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        cbPanel.gogo();
        timer.start();
    }
}

class nonoListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        cbPanel.nono();
        timer.stop();
    }
}

class TimerListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        cbPanel.moveRight(5);
        cbPanel.moveDown(5);
        repaint();
    }
}

public Viewer() {
    buttons.add(go);
    buttons.add(stop);
    this.add(buttons, BorderLayout.SOUTH);
    this.add(cbPanel, BorderLayout.CENTER);
    this.add(right, BorderLayout.EAST);
    this.add(top, BorderLayout.NORTH);
    this.add(left, BorderLayout.WEST);

    timer = new Timer(50, new TimerListener());
    go.addActionListener(new gogoListener());
    stop.addActionListener(new nonoListener());
    timer.start();
    cbPanel.setBorder(BorderFactory.createLineBorder(Color.black));
}

}

您需要某种确定球移动方向的增量,而不是调用
moveRight
/
moveDown
,您只需要求球根据此增量更新自身

调用(更新方法)时,它会将当前增量应用于x/y位置,并计算球是否已超出可用边界,球是否应在边上重新定位,以及三角形是否翻转(乘以-1),这将更改增量的方向

水平和垂直位置都需要三角形;)

例如:

有关工作示例,请参阅。尽管无可否认,一个圆(球)和一个可显示区域(一个矩形)会让它接受一个比我的示例中的JRE执行的简单得多的公式。
public class Viewer extends JFrame {
JButton go = new JButton("GO");
JButton stop = new JButton("STOP");
JPanel buttons = new JPanel();
Timer timer;

ControlledBall cbPanel = new ControlledBall();
JPanel left = new JPanel();
JPanel right = new JPanel();
JPanel top = new JPanel();


class gogoListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        cbPanel.gogo();
        timer.start();
    }
}

class nonoListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        cbPanel.nono();
        timer.stop();
    }
}

class TimerListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        cbPanel.moveRight(5);
        cbPanel.moveDown(5);
        repaint();
    }
}

public Viewer() {
    buttons.add(go);
    buttons.add(stop);
    this.add(buttons, BorderLayout.SOUTH);
    this.add(cbPanel, BorderLayout.CENTER);
    this.add(right, BorderLayout.EAST);
    this.add(top, BorderLayout.NORTH);
    this.add(left, BorderLayout.WEST);

    timer = new Timer(50, new TimerListener());
    go.addActionListener(new gogoListener());
    stop.addActionListener(new nonoListener());
    timer.start();
    cbPanel.setBorder(BorderFactory.createLineBorder(Color.black));
}

}