Java-当给定不同的任务时,对象彼此跟随
我一直在开发这个java应用程序。 到目前为止,它没有任何意义,只是一个随机颜色的球在周围弹跳。 但是现在,当我想在弹跳应用程序中添加另一个球时,这些球彼此跟随。 这是到目前为止我的代码Java-当给定不同的任务时,对象彼此跟随,java,object,Java,Object,我一直在开发这个java应用程序。 到目前为止,它没有任何意义,只是一个随机颜色的球在周围弹跳。 但是现在,当我想在弹跳应用程序中添加另一个球时,这些球彼此跟随。 这是到目前为止我的代码 import java.awt.*; import javax.swing.*; public class MainFrame extends JPanel implements Runnable { Color color = Color.red; int dia = 60; 物体的直径
import java.awt.*;
import javax.swing.*;
public class MainFrame extends JPanel implements Runnable {
Color color = Color.red;
int dia = 60;
物体的直径
long delay = 20;
延迟时间
private int x = (int)Math.floor(Math.random() * 580);
private int y = (int)Math.floor(Math.random() * 900);
private int xx = (int)Math.floor(Math.random() * 580);
private int yy = (int)Math.floor(Math.random() * 900);
上面是对象的位置
private int dx = (int)Math.floor(Math.random() * 7);
private int dy = (int)Math.floor(Math.random() * 7);
private int dxx = (int)Math.floor(Math.random() * 7);
private int dyy = (int)Math.floor(Math.random() * 7);
上面是物体速度
protected void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g.setColor(color);
g.fillOval(x,y,60,60);
g.setColor(color);
g.fillOval(xx,yy,60,60);
}
图形。
下面是计算,thread.sleep和JFrame
public void run() {
while(isVisible()) {
try {
Thread.sleep(delay);
} catch(InterruptedException e) {
System.out.println("interrupted");
}
move();
repaint();
}
}
public void move() {
if(x + dx < 0 || x + dia + dx > getWidth()) {
dx *= -1;
color = getColor();
}
if(y + dy < 0 || y + dia + dy > getHeight()) {
dy *= -1;
color = getColor();
}
if(xx + dxx < 0 || xx + dia + dxx > getWidth()) {
dxx *= -1;
color = getColor();
}
if(yy + dyy < 0 || yy + dia + dyy > getHeight()) {
dyy *= -1;
color = getColor();
}
x += dx;
y += dy;
xx += dx;
yy += dy;
}
private Color getColor() {
int rval = (int)Math.floor(Math.random() * 256);
int gval = (int)Math.floor(Math.random() * 256);
int bval = (int)Math.floor(Math.random() * 256);
return new Color(rval, gval, bval);
}
private void start() {
while(!isVisible()) {
try {
Thread.sleep(25);
} catch(InterruptedException e) {
System.exit(1);
}
}
Thread thread = new Thread(this);
thread.setPriority(Thread.NORM_PRIORITY);
thread.start();
}
public static void main(String[] args) {
MainFrame test = new MainFrame();
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(test);
f.setSize(640, 960);
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
f.setLocation(dim.width/2-f.getSize().width/2, dim.height/2-f.getSize().height/2);
f.setVisible(true);
test.start();
}
}
public void run(){
while(isVisible()){
试一试{
睡眠(延迟);
}捕捉(中断异常e){
系统输出打印项次(“中断”);
}
move();
重新油漆();
}
}
公开作废动议(){
如果(x+dx<0 | | x+dia+dx>getWidth()){
dx*=-1;
color=getColor();
}
如果(y+dy<0 | | y+dia+dy>getHeight()){
dy*=-1;
color=getColor();
}
如果(xx+dxx<0 | | xx+dia+dxx>getWidth()){
dxx*=-1;
color=getColor();
}
如果(yy+dyy<0 | | yy+dia+dyy>getHeight()){
dyy*=-1;
color=getColor();
}
x+=dx;
y+=dy;
xx+=dx;
yy+=dy;
}
私有颜色getColor(){
int rval=(int)Math.floor(Math.random()*256);
int gval=(int)Math.floor(Math.random()*256);
int bval=(int)Math.floor(Math.random()*256);
返回新颜色(rval、gval、bval);
}
私有void start(){
而(!isVisible()){
试一试{
睡眠(25);
}捕捉(中断异常e){
系统出口(1);
}
}
螺纹=新螺纹(此);
线程.setPriority(线程.NORM\u优先级);
thread.start();
}
公共静态void main(字符串[]args){
大型机测试=新的大型机();
JFrame f=新的JFrame();
f、 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f、 getContentPane().add(测试);
f、 设置大小(640960);
维度dim=Toolkit.getDefaultToolkit().getScreenSize();
f、 设置位置(尺寸宽度/2-f.getSize().width/2,尺寸高度/2-f.getSize().height/2);
f、 setVisible(真);
test.start();
}
}
我就是想不出来。
我知道答案会很简单。你应该定义一个类
Ball
,并创建它的两个实例,而不是重复变量,在该类中有x,y坐标和速度dx和dy
这两个球相互跟随,因为两个球的速度始终相同:
x += dx;
y += dy;
xx += dx;
yy += dy;
您应该定义一个类
Ball
,并创建它的两个实例,而不是重复变量,并且在该类中具有x、y坐标和速度dx和dy
这两个球相互跟随,因为两个球的速度始终相同:
x += dx;
y += dy;
xx += dx;
yy += dy;
您应该定义一个类
Ball
,并创建它的两个实例,而不是重复变量,并且在该类中具有x、y坐标和速度dx和dy
这两个球相互跟随,因为两个球的速度始终相同:
x += dx;
y += dy;
xx += dx;
yy += dy;
您应该定义一个类
Ball
,并创建它的两个实例,而不是重复变量,并且在该类中具有x、y坐标和速度dx和dy
这两个球相互跟随,因为两个球的速度始终相同:
x += dx;
y += dy;
xx += dx;
yy += dy;
... 现在我觉得自己像个白痴。谢谢它的帮助。你的意思是创建一个类
Ball
,然后使用两个实例,对吗?。。。现在我觉得自己像个白痴。谢谢它的帮助。你的意思是创建一个类Ball
,然后使用两个实例,对吗?。。。现在我觉得自己像个白痴。谢谢它的帮助。你的意思是创建一个类Ball
,然后使用两个实例,对吗?。。。现在我觉得自己像个白痴。感谢它的帮助。您的意思是创建一个类Ball
,然后使用两个实例,对吗?