向相反方向移动两个球 [英] Moving two balls in opposite direction

查看:28
本文介绍了向相反方向移动两个球的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Netbeans 创建一个带有两个球的 Java 程序(一个位于顶部,另一个位于底部),执行时它们向相反的方向移动并离开屏幕.

I am trying to create a java program, using Netbeans, with two balls (one position at the top and the other bottom) and when executed they move in the opposite direction and go offscreen.

原始代码是使用一个球提供给我们的,我们被要求添加第二个面板,因此代码中存在混淆.

The original code was given to us using one ball and we were asked to add a second panel hence the confusion within the code.

我的问题是当我使用 BoxLayout.Y_AXIS 执行代码时,球在中心相遇并由于面板排列而消失.我希望球穿过中心进入其他面板.

My problem is when I execute the code using BoxLayout.Y_AXIS the balls meet in center and disappear due to the panel arrangement. I want the balls to cross the center into the other panels.

我尝试过使用边框布局,但我丢了一个球.

I have tried using border layout, but i loose one ball.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


public class RandomBall {

    JFrame frame;
    public int x = 0;
    public int y = 0;
    public int z = 300;
    public int deltaX;
    public int deltaY;
    public int deltaZ;
    public int posNeg;
    public int diameter = 50;
    final static public int MULT = 5;
    Ball1DrawPanel drawPanel1;
    Ball2DrawPanel drawPanel2;
    JPanel pan;

    public static void main(String[] args) {
        RandomBall gui = new RandomBall();
    }

    public void go() {
        frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        drawPanel1 = new Ball1DrawPanel();
        drawPanel2 = new Ball2DrawPanel();
        pan = new JPanel();
        pan.setLayout(new BoxLayout(pan, BoxLayout.Y_AXIS));
        pan.add(BorderLayout.NORTH, drawPanel1);
        pan.add(BorderLayout.SOUTH, drawPanel2);

        frame.getContentPane().add(BorderLayout.CENTER, pan);

        frame.setSize(500, 500);
        frame.setVisible(true);

        deltaX = (int) (Math.random() * MULT);  //randomly set the displacement in the  x direction (across)
        deltaY = (int) (Math.random() * MULT);  //randomly set the displacement in the y direction (down)
        deltaZ = (int) (Math.random() * MULT);  //randomly set the displacement in the y direction (down)

        while ((deltaX == 0) && (deltaY == 0)) {
            deltaX = (int) (Math.random() * MULT); //to prevent both values being zero - ball will not move
            deltaY = (int) (Math.random() * MULT);
            deltaZ = (int) (Math.random() * MULT);
        }

        posNeg = (int) (Math.random() * 2);
        if (posNeg == 0) {
            deltaX = deltaX * -1;  //randomly set the direction to left or right
        }
    }

    public RandomBall() {
        go();
    }

    class Ball1DrawPanel extends JPanel {
        @Override
        public void paintComponent(Graphics g) {
            try {
                Thread.sleep(10);
            } catch (Exception e) {
            }
            g.setColor(Color.red);
            g.fillOval((this.getWidth() + (x) - diameter) / 2, (0 + (y)) / 2, diameter, diameter);
            frame.repaint();
            x = x + deltaX;
            y = y + deltaY;
        }
    }

    class Ball2DrawPanel extends JPanel {
        @Override
        public void paintComponent(Graphics g) {
            try {
                Thread.sleep(10);
            } catch (Exception e) {
            }
            g.setColor(Color.BLUE);
            g.fillOval((this.getWidth() + (x) - diameter) / 2, (0 + (z)) / 2, diameter, diameter);
            frame.repaint();
            x = x + deltaX;
            z = z - deltaZ;
        }
    }
}

是否有任何 layout 或任何实现,我可以使用它允许球穿过对面的小程序而不是在与另一个碰撞时在中心断裂?

Is there any layout, or any implementation, I can use that will allow the balls to cross into the opposite applet rather than breaking at the center when the collide with the other?

推荐答案

感谢所有帮助,但我做了一些研究并设法找到了我问题的解决方案 ..

I appreciate all the help, but did some research and managed to find the solution to my question ..

我创建了第二个面板,

pan = new JPanel(); 
pan.setLayout(new OverlayLayout(pan));

在此之后,我只是将两个球添加到pan"面板和 VOILAAAA..

After this, I just added the two balls to the "pan" panel and VOILAAAA..

再次感谢大家的帮助..

Again thanks for all the help..

这篇关于向相反方向移动两个球的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆