设有两个弹弹球窗口并排相邻,当一个球跑到两个窗口相邻的边界时,不弹回,而是穿越到响邻的窗口中继续奔跑,每个窗口只有相邻的边才能被穿越,如左窗口的右边和右窗口的左边
时间: 2023-07-19 19:14:34 浏览: 55
要实现这个需求,可以在弹弹球运动的过程中监听球的位置,当球到达边界时,判断是否需要穿越到相邻的窗口。
以下是一个简单的示例代码:
```java
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
public class MultipleWindowsExample {
public static void main(String[] args) {
// Create the first window
JFrame window1 = new JFrame("Window 1");
window1.setSize(400, 300);
window1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
BallPanel panel1 = new BallPanel();
window1.add(panel1);
window1.setVisible(true);
// Create the second window
JFrame window2 = new JFrame("Window 2");
window2.setSize(400, 300);
window2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
BallPanel panel2 = new BallPanel();
window2.add(panel2);
window2.setVisible(true);
// Set the neighbors of each panel
panel1.setRightPanel(panel2);
panel2.setLeftPanel(panel1);
}
}
class BallPanel extends JPanel implements ActionListener {
private static final int WIDTH = 400;
private static final int HEIGHT = 300;
private static final int BALL_RADIUS = 20;
private static final int BALL_SPEED = 5;
private int ballX = 0;
private int ballY = 0;
private int ballVx = BALL_SPEED;
private int ballVy = BALL_SPEED;
private Color ballColor = Color.RED;
private BallPanel leftPanel;
private BallPanel rightPanel;
public BallPanel() {
Timer timer = new Timer(50, this);
timer.start();
}
public void setLeftPanel(BallPanel leftPanel) {
this.leftPanel = leftPanel;
}
public void setRightPanel(BallPanel rightPanel) {
this.rightPanel = rightPanel;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(ballColor);
g.fillOval(ballX - BALL_RADIUS, ballY - BALL_RADIUS, BALL_RADIUS * 2, BALL_RADIUS * 2);
}
@Override
public void actionPerformed(ActionEvent e) {
// Move the ball
ballX += ballVx;
ballY += ballVy;
// Check if the ball hits the edges
if (ballX + BALL_RADIUS >= WIDTH) {
if (rightPanel != null) {
ballX = BALL_RADIUS + 1;
ballVx = BALL_SPEED;
rightPanel.receiveBall(ballY);
} else {
ballX = WIDTH - BALL_RADIUS - 1;
ballVx = -BALL_SPEED;
}
} else if (ballX - BALL_RADIUS <= 0) {
if (leftPanel != null) {
ballX = WIDTH - BALL_RADIUS - 1;
ballVx = -BALL_SPEED;
leftPanel.receiveBall(ballY);
} else {
ballX = BALL_RADIUS + 1;
ballVx = BALL_SPEED;
}
}
if (ballY + BALL_RADIUS >= HEIGHT) {
ballY = HEIGHT - BALL_RADIUS - 1;
ballVy = -BALL_SPEED;
} else if (ballY - BALL_RADIUS <= 0) {
ballY = BALL_RADIUS + 1;
ballVy = BALL_SPEED;
}
// Redraw the panel
repaint();
}
public void receiveBall(int y) {
ballY = y;
ballVx = -ballVx;
}
}
```
这个例子中,BallPanel 类继承了 JPanel,用于显示弹弹球。每个 BallPanel 对象维护了一个球的位置和速度,并且可以接收相邻窗口传递过来的球。
在 actionPerformed() 方法中,每次移动球的位置,如果球到达了窗口边界,就检查是否需要穿越到相邻的窗口。如果相邻窗口存在,就将球传递给相邻窗口,同时修改球的位置和速度,让球从相邻窗口的另一侧出现。如果相邻窗口不存在,就让球弹回。
在 receiveBall() 方法中,当相邻窗口传递球时,修改球的位置和速度,让球从当前窗口的相邻边穿越到相邻窗口。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)