我有一项任务,我必须在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);
}
}
}
答案 0 :(得分:0)
我最近做了这样的事情,我遇到了Check this on。它并不完全相同,但我认为查看最佳答案会对你有所帮助。
答案 1 :(得分:0)
好的,所以我转而使用javax.swing.Timer。 现在我遇到了一些其他问题,关于球的随机产卵区域。 我注意到如果我使用坐标X,Y为球,除以5.一切都很好,但如果不是,球再次开始通过矩形。 所以我无法弄清楚为什么。 我的想法可能是为x1,y1,x2,y2等制作一个随机数生成器。 在从50到250的范围内选择一个随机数,但数字必须能够除以5.这意味着随机数将类似于5,10,15,20,25。我可以使用for循环和数组来存储值,然后让程序从数组列表中为x1选择一个随机值。
但这一切听起来完全错了,我猜我刚刚离开主题......
所以这是我的新代码:
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.Timer;
@SuppressWarnings("serial")
public class Uzd2 extends JFrame implements ActionListener, KeyListener {
Graphics2D g2d;
Timer timer = new Timer(5, this);
int width1 = 30, width2 = 30, width3 = 30, width4 = 30, width5 = 30;
int height1 = 30, height2 = 30, height3 = 30, height4 = 30, height5 = 30;
Random rn = new Random();
int x1 = 20 + rn.nextInt(50) + 1;
int x2 = 20 + rn.nextInt(50) + 1;
int x3 = 20 + rn.nextInt(50) + 1;
int x4 = 20 + rn.nextInt(50) + 1;
int x5 = 20 + rn.nextInt(50) + 1;
int y1 = 20 + rn.nextInt(50) + 1;
int y2 = 20 + rn.nextInt(50) + 1;
int y3 = 20 + rn.nextInt(50) + 1;
int y4 = 20 + rn.nextInt(50) + 1;
int y5 = 20 + rn.nextInt(50) + 1;
int x1Diff = 5, y1Diff = -5;
int x2Diff = 5, y2Diff = -5;
int x3Diff = 5, y3Diff = -5;
int x4Diff = 5, y4Diff = -5;
int x5Diff = 5, y5Diff = -5;
int y1Rect = 325, x1Rect = 325;
int y2Rect = 100, x2Rect = 150;
public Uzd2() {
timer.start();
setDefaultCloseOperation(EXIT_ON_CLOSE);
setBounds(10, 10, 750, 750);
setResizable(false);
setTitle("Animācija");
addKeyListener(this);
System.out.println("X1 = " + x1 + " X2 = " + x2 + " X3 = " + x3 + " X4 = " + x4 + " X5 = " + x5);
System.out.println("Y1 = " + y1 + " Y2 = " + y2 + " Y3 = " + y3 + " Y4 = " + y4 + " Y5 = " + y5);
}
public void paint(java.awt.Graphics g) {
super.paint(g);
g2d = (Graphics2D) g;
g2d.setStroke(new BasicStroke(5));
g2d.setColor(Color.BLUE);
animacija1(x1, y1, width1);
animacija2(x2, y2, width2);
animacija3(x3, y3, width3);
animacija4(x4, y4, width4);
animacija5(x5, y5, width5);
g2d.setColor(Color.RED);
g2d.fillRect(325, 325, 100, 100);
}
public void animacija1(int x1, int y1, int w1) {
g2d.setColor(Color.BLUE);
g2d.fillOval(x1, y1, width1, height1);
}
public void animacija2(int x2, int y2, int w2) {
g2d.setColor(Color.GREEN);
g2d.fillOval(x2, y2, width2, height2);
}
public void animacija3(int x3, int y3, int w3) {
g2d.setColor(Color.MAGENTA);
g2d.fillOval(x3, y3, width3, height3);
}
public void animacija4(int x4, int y4, int w4) {
g2d.setColor(Color.CYAN);
g2d.fillOval(x4, y4, width4, height4);
}
public void animacija5(int x5, int y5, int w5) {
g2d.setColor(Color.YELLOW);
g2d.fillOval(x5, y5, width5, height5);
}
public void actionPerformed(ActionEvent e) {
aprekini1();
aprekini2();
aprekini3();
aprekini4();
aprekini5();
repaint();
}
public void aprekini1() {
//Top, bottom.
if ((y1 <= 20 || y1 + 30 >= 760) || (y1 + 25 == y1Rect && (x1 >= x1Rect && x1 <= x1Rect + 100))
|| (y1 == y1Rect + 100 && (x1 >= x1Rect && x1 <= x1Rect + 100))) {
y1Diff = -y1Diff;
}
//Left, right.
if ((x1 <= 0 || x1 + 30 >= 750) || (x1 + 25 == x1Rect && (y1 >= x1Rect && y1 <= y1Rect + 100))
|| (x1 == x1Rect + 100 && (y1 >= x1Rect && y1 <= y1Rect + 100))) {
x1Diff = -x1Diff;
}
x1 += x1Diff;
y1 += y1Diff;
}
public void aprekini2() {
// Top, bottom.
if ((y2 <= 20 || y2 + 30 >= 760) || (y2 + 25 == y1Rect && (x2 >= x1Rect && x2 <= x1Rect + 100))
|| (y2 == y1Rect + 100 && (x2 >= x1Rect && x2 <= x1Rect + 100))) {
y2Diff = -y2Diff;
}
// Left, right.
if ((x2 <= 0 || x2 + 30 >= 750) || (x2 + 25 == x1Rect && (y2 >= x1Rect && y2 <= y1Rect + 100))
|| (x2 == x1Rect + 100 && (y2 >= x1Rect && y2 <= y1Rect + 100))) {
x2Diff = -x2Diff;
}
x2 += x2Diff;
y2 += y2Diff;
}
public void aprekini3() {
// Top, bottom.
if ((y3 <= 20 || y3 + 30 >= 760) || (y3 + 25 == y1Rect && (x3 >= x1Rect && x3 <= x1Rect + 100))
|| (y3 == y1Rect + 100 && (x3 >= x1Rect && x3 <= x1Rect + 100))) {
y3Diff = -y3Diff;
}
// Left, right.
if ((x3 <= 0 || x3 + 30 >= 750) || (x3 + 25 == x1Rect && (y3 >= x1Rect && y3 <= y1Rect + 100))
|| (x3 == x1Rect + 100 && (y3 >= x1Rect && y3 <= y1Rect + 100))) {
x3Diff = -x3Diff;
}
x3 += x3Diff;
y3 += y3Diff;
}
public void aprekini4() {
// Top, bottom.
if ((y4 <= 20 || y4 + 30 >= 760) || (y4 + 25 == y1Rect && (x4 >= x1Rect && x4 <= x1Rect + 100))
|| (y4 == y1Rect + 100 && (x4 >= x1Rect && x4 <= x1Rect + 100))) {
y4Diff = -y4Diff;
}
// Left, right.
if ((x4 <= 0 || x4 + 30 >= 750) || (x4 + 25 == x1Rect && (y4 >= x1Rect && y4 <= y1Rect + 100))
|| (x4 == x1Rect + 100 && (y4 >= x1Rect && y4 <= y1Rect + 100))) {
x4Diff = -x4Diff;
}
x4 += x4Diff;
y4 += y4Diff;
}
public void aprekini5() {
// Top, bottom.
if ((y5 <= 20 || y5 + 30 >= 760) || (y5 + 25 == y1Rect && (x5 >= x1Rect && x5 <= x1Rect + 100))
|| (y5 == y1Rect + 100 && (x5 >= x1Rect && x5 <= x1Rect + 100))) {
y5Diff = -y5Diff;
}
// Left, right.
if ((x5 <= 0 || x5 + 30 >= 750) || (x5 + 25 == x1Rect && (y5 >= x1Rect && y5 <= y1Rect + 100))
|| (x5 == x1Rect + 100 && (y5 >= x1Rect && y5 <= y1Rect + 100))) {
x5Diff = -x5Diff;
}
x5 += x5Diff;
y5 += y5Diff;
}
@Override
public void keyTyped(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_W) {
y1Rect -= 5;
}
}
@Override
public void keyPressed(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
}
public static void main(String[] args) {
Uzd2 frame = new Uzd2();
frame.setVisible(true);
}
}