我有一个任务,我必须在JFrame
中制作应用程序。无法找出球动画的障碍
它将包含5个球,它们随机地围绕窗体移动,它们不能退出窗体并且必须碰到边界。在表格中间还有一个矩形,这是主要问题,因为我无法弄清楚如何让球从长方形反弹。
我已经开始做一些事情,但是球只是在窗体中的某些随机位置反弹。
任务:
- 创建
JFrame
(完成)- 创建5
Balls
,它走动,并产卵在随机位置(完成)- 创建的形式的中间
Rectangle
(完成)- 使球弹出矩形。 (完成)
最终找出结果。
这里是我的代码:
import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class Main extends JPanel {
Random rn = new Random();
int blueX = rn.nextInt(650) + 1;
int blueY = rn.nextInt(650) + 1;
int redX = rn.nextInt(650) + 1;
int redY = rn.nextInt(650) + 1;
int yellowX = rn.nextInt(650) + 1;
int yellowY = rn.nextInt(650) + 1;
int greenX = rn.nextInt(650) + 1;
int greenY = rn.nextInt(650) + 1;
int magentaX = rn.nextInt(650) + 1;
int magentaY = rn.nextInt(650) + 1;
int blueAngleX = rn.nextInt(10) + 1;
int blueAngleY = rn.nextInt(20) + 1;
int redAngleX = rn.nextInt(10) + 1;
int redAngleY = rn.nextInt(50) + 1;
int yellowAngleX = rn.nextInt(40) + 1;
int yellowAngleY = rn.nextInt(50) + 1;
int greenAngleX = rn.nextInt(30) + 1;
int greenAngleY = rn.nextInt(20) + 1;
int magentaAngleX = rn.nextInt(20) + 1;
int magentaAngleY = rn.nextInt(50) + 1;
int rectX = 325, rectY = 325, rectW = 100, rectH = 100;
int speed = 5;
private void move() {
rectContact();
if (blueX + blueAngleX < 0) {
blueAngleX = speed;
} else if (blueX + blueAngleX > getWidth() - 30) {
blueAngleX = -speed;
} else if (blueY + blueAngleY < 0) {
blueAngleY = speed;
} else if (blueY + blueAngleY > getHeight() - 30) {
blueAngleY = -speed;
}
blueX = blueX + blueAngleX;
blueY = blueY + blueAngleY;
///////////////////////////////////////////////////////////////////////////////////////////
if (redX + redAngleX < 0) {
redAngleX = speed;
} else if (redX + redAngleX > getWidth() - 30) {
redAngleX = -speed;
} else if (redY + redAngleY < 0) {
redAngleY = speed;
} else if (redY + redAngleY > getHeight() - 30) {
redAngleY = -speed;
}
redX = redX + redAngleX;
redY = redY + redAngleY;
///////////////////////////////////////////////////////////////////////////////////////////
if (yellowX + yellowAngleX < 0) {
yellowAngleX = speed;
} else if (yellowX + yellowAngleX > getWidth() - 30) {
yellowAngleX = -speed;
} else if (yellowY + yellowAngleY < 0) {
yellowAngleY = speed;
} else if (yellowY + yellowAngleY > getHeight() - 30) {
yellowAngleY = -speed;
}
yellowX = yellowX + yellowAngleX;
yellowY = yellowY + yellowAngleY;
///////////////////////////////////////////////////////////////////////////////////////////
if (greenX + greenAngleX < 0) {
greenAngleX = speed;
} else if (greenX + greenAngleX > getWidth() - 30) {
greenAngleX = -speed;
} else if (greenY + greenAngleY < 0) {
greenAngleY = speed;
} else if (greenY + greenAngleY > getHeight() - 30) {
greenAngleY = -speed;
}
greenX = greenX + greenAngleX;
greenY = greenY + greenAngleY;
///////////////////////////////////////////////////////////////////////////////////////////
if (magentaX + magentaAngleX < 0) {
magentaAngleX = speed;
} else if (magentaX + magentaAngleX > getWidth() - 30) {
magentaAngleX = -speed;
} else if (magentaY + magentaAngleY < 0) {
magentaAngleY = speed;
} else if (magentaY + magentaAngleY > getHeight() - 30) {
magentaAngleY = -speed;
}
magentaX = magentaX + magentaAngleX;
magentaY = magentaY + magentaAngleY;
}
public void rectContact() {
if (blueY + 30 >= rectY && (blueX >= rectX - 25 && blueX <= rectX)
|| (blueY >= rectY + 100 && (blueX >= rectX && blueX <= rectX + 100))) {
blueAngleX = -speed;
}
if (blueX + 10 >= rectX && (blueY >= rectY && blueY <= rectY - 25)
|| (blueX >= rectX && (blueY >= rectY && blueY <= rectY))) {
blueAngleY = -speed;
}
}
@Override
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.BLUE);
g.fillOval(blueX, blueY, 30, 30);
g.setColor(Color.RED);
g.fillOval(redX, redY, 30, 30);
g.setColor(Color.YELLOW);
g.fillOval(yellowX, yellowY, 30, 30);
g.setColor(Color.GREEN);
g.fillOval(greenX, greenY, 30, 30);
g.setColor(Color.MAGENTA);
g.fillOval(magentaX, magentaY, 30, 30);
g.setColor(Color.RED);
g.fillRect(rectX, rectY, rectW, rectH);
}
public static void main(String[] args) throws InterruptedException {
JFrame frame = new JFrame("Moving Ball!");
Main main = new Main();
frame.add(main);
frame.setBounds(300, 0, 750, 750);
frame.setVisible(true);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
while (true) {
main.move();
main.repaint();
Thread.sleep(10);
}
}
}
1.不要覆盖'漆”,'重写paintComponent' 2.如果你想动画,我会推荐使用[javax.swing.Timer](https://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html) – copeg
1)为了更好的帮助更快地发布[MCVE]或[简短,独立,正确的示例](http://www.sscce.org/)。 *“创建5个'球'”*只将一个球放入MCVE/SSCCE中。如果你可以使用它,它应该以与5完全相同的方式工作。 2)另请参阅[具有复杂形状的碰撞检测](http://stackoverflow.com/a/14575043/418556)了解(稍微)有关的示例。 –
@copeg所以我切换到计时器,现在我遇到了另一个问题...下面添加了新的代码。也许你可以帮我解决问题或提供一些提示。 – UniQLostInTheCode