我希望球能够知道它们位于JPanel的矩形内

时间:2015-10-02 16:44:18

标签: 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;
}
}

1 个答案:

答案 0 :(得分:2)

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

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

在球中:

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