java - 我希望球知道它们位于 JPanel 中的矩形内

标签 java swing

我正在制作一个小程序,其中一些球在盒子内弹跳。其逻辑已在名为 Ball.java 的类中完成,并且球正常弹跳和移动。

我想更进一步,在 Jframe 内部绘制一个矩形,并让球在内部有两个时以特定方式运行。应该只有 2 个球在矩形边界内移动。进入矩形的其他球应该卡住,直到 2 个球之一 当前移动的球离开它。 离开矩形的移动球应该卡住,除非有 盒子里的另一个球意味着矩形永远不应该是空的。

下面是我应该实现的逻辑,我在 paincomponents() 方法中用 g.drawRect(75, 75, 200, 200); 绘制了矩形,我在制作时遇到问题球知道它们在那个矩形内。

package bouncingball;

import bouncingball.Ball;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import javax.swing.JPanel;

public class BallPanel extends JPanel {

private Ball ball;
private ArrayList<Ball> balls = new ArrayList();
private int ballsInside = 0;

public BallPanel() {
    addMouseListener(new Mouse());
}

//method for creating a new ball
private void newBall(MouseEvent click) {
    ball = new Ball(this);
    balls.add(ball);
    Thread t = new Thread(ball);
    t.setPriority(Thread.NORM_PRIORITY);
    t.start();
    System.out.println("New ball created");
}

public void animate() {
    while (true) {
        repaint();
}

/* public synchronized void countIn() throws InterruptedException {
 while (ballsInside < 2){
 wait();
 }
 ballsInside++;
 notifyAll();

 }

 public synchronized void countMaxIn() throws InterruptedException {
 while (ballsInside == 2){
 wait();
 }
 ballsInside--;
 notifyAll();

 }*/
private class Mouse extends MouseAdapter {

    @Override
    public void mousePressed(final MouseEvent event) {
        newBall(event);
    }
}

@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.drawRect(75, 75, 200, 200);
    for (Ball ball : balls) {
        if (ball != null) {
            ball.draw(g);
        }
    }
}
}

和我的球课

package bouncingball;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.util.Random; 
import java.lang.Runnable;
import javax.swing.JPanel;
import java.util.ArrayList;

public class Ball implements Runnable {

public final static Random random = new Random();

final static int SIZE = 30;
final static int MAX_SPEED = 5;

BallPanel panel = new BallPanel();

private int x;
private int y;

private int dx;
private int dy;

private Color color = Color.BLUE;
//private ArrayList<Ball> balls ;


@Override   
public void run() {
    while (true) {
        move();
        try {
            Thread.sleep(40);  // wake up roughly 25 frames per second
        } catch (InterruptedException exception) {
            exception.printStackTrace();
        }
    }
}

public Ball(BallPanel panel) {
    this.panel = panel;
    x = random.nextInt(panel.getWidth());
    y = random.nextInt(panel.getHeight());
    dx = random.nextInt(2 * MAX_SPEED) - MAX_SPEED;
    dy = random.nextInt(2 * MAX_SPEED) - MAX_SPEED;
}

public void draw(Graphics g) {
    g.setColor(color);
    g.fillOval(x, y, SIZE, SIZE);
}

public void move() {

    // check for bounce and make the ball bounce if necessary
    //
    if (x < 0 && dx < 0) {
        //bounce off the left wall 
        x = 0;
        dx = -dx;
    }
    if (y < 0 && dy < 0) {
        //bounce off the top wall
        y = 0;
        dy = -dy;
    }
    if (x > panel.getWidth() - SIZE && dx > 0) {
        //bounce off the right wall
        x = panel.getWidth() - SIZE;
        dx = -dx;
    }
    if (y > panel.getHeight() - SIZE && dy > 0) {
        //bounce off the bottom wall
        y = panel.getHeight() - SIZE;
        dy = -dy;
    }

    //make the ball move
    x += dx;
    y += dy;
}
}

最佳答案

使用一个整数 semaphore ,允许 2 个球进入,直到其他球阻挡为止。

private final Semaphore entryTickets = new Semaphore(2, true);

在球中:

entryTickets.acquire(); // Enter
...
entryTickets.release(); // Leave

关于java - 我希望球知道它们位于 JPanel 中的矩形内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32911947/

相关文章:

java - 像 String[] strs 这样的 Java 数组有哪些方法和属性可用?

java - 使用二维数组熄灯游戏

java - JFrame 到 JApplet 在浏览器中不起作用

java - 引用特定对象的实例方法

java - 使用命令行 gradle 从测试源执行 main()

java - 带有游戏图片和说明的 Facebook Feed 对话框如下

java - 保存项目数据。你会怎么做?

java - 同时使用java bufferstrategy和swing元素

java - 如何阅读 NumberFormatException 错误信息?

java - Android - 旋转树