1
我正在製作一個小程序,其中一些球在一個盒子裏彈跳。這個邏輯已經在一個名爲Ball.java的類中完成,球正在彈跳並正常移動。我想讓球知道他們在一個JPanel中的矩形內
我想更進一步,在Jframe內部繪製一個矩形,並在內部有兩個球的時候讓球表現出特定的方式。應該只有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;
}
}
看看答案爲[這個問題](http://stackoverflow.com/questions/32746683/ball-sometimes-bounces-sometimes-doesnt/32748251#32748251) - 這不完全像你的情況,但它可能會幫助你開始。 –