我有一個任務,我必須在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