Java 我如何启动鼠标点击时从舞台上任何地方生长的粒子?
我希望能够从我在舞台上单击的任何位置创建新粒子。我只是停留在添加鼠标部分上,我尝试添加/传递参数,在尝试设置参数时总是出错。有什么建议吗 这是我当前的代码:Java 我如何启动鼠标点击时从舞台上任何地方生长的粒子?,java,processing,mouselistener,Java,Processing,Mouselistener,我希望能够从我在舞台上单击的任何位置创建新粒子。我只是停留在添加鼠标部分上,我尝试添加/传递参数,在尝试设置参数时总是出错。有什么建议吗 这是我当前的代码: float parSpeed = 1; //speed of particles int nParticles = 1000; //# of particles Particle[] particles; void setup() { size(700,700); //size of image (8.5 x 11 @ 300px
float parSpeed = 1; //speed of particles
int nParticles = 1000; //# of particles
Particle[] particles;
void setup() {
size(700,700); //size of image (8.5 x 11 @ 300px) is 3300,2550)
frameRate(60); //framerate of stage
background(0); //color of background
particles = new Particle[nParticles];
//start particle array
for(int i=0; i<nParticles; i++) {
particles[i] = new Particle();
}
}
void draw() {
fill(0,0,0,5); //5 is Alpha
rect(0,0,width,height); //color of rectangle on top?
translate(width/2, height/2); //starting point of particles
//start particle array
for(int i=0; i<nParticles; i++) {
particles[i].update();
particles[i].show();
}
}
//Particle Class
class Particle {
PVector pos; //position
float angle; //angle
float dRange; //diameter range
float dAngle; //beginning angle?
color c; //color
Particle() {
pos = new PVector(0,0);//new position for the particles.
angle = 1; //controls randomization of direction in position when multiplied
dRange = 0.01; // how big of a circle shold the particles rotate on
dAngle = 0.15; // -- maximum angle when starting
c = color(0,0,random(100, 255)); //set color to random blue
}
void update() {
float cor = .25*dRange*atan(angle)/PI;
float randNum = (random(2)-1)*dRange-cor; //Random number from (-dRange, dRange)
dAngle+=randNum; //We don't change the angle directly
//but its differential - source of the smoothness!
angle+=dAngle; //new angle is angle+dAngle -- change angle each frame
pos.x+=parSpeed*cos(angle);//random direction for X axis multiplied by speed
pos.y+=parSpeed*sin(angle);//rabdin durectuib for y axis multiplied by speed
}
void show() {
fill(c); //fill in the random color
noStroke(); //no stroke
ellipse(pos.x,pos.y,10,10); //make the shape
smooth(); //smooth out the animation
}
}
void keyPressed() {
print("pressed " + int(key) + " " + keyCode);
if (key == 's' || key == 'S'){
saveFrame("image-##.png");
}
}
void mouseReleased() {
print("mouse has been clicked!");
}
float parSpeed=1//粒子速度
int nParticle=1000;/#粒子数
粒子[]粒子;
无效设置(){
大小(700700);//图像大小(8.5 x 11@300px)为33002550)
帧率(60);//舞台的帧率
背景(0);//背景颜色
粒子=新粒子[nParticle];
//启动粒子阵列
对于(int i=0;i覆盖()方法:
在这里,您需要:
捕捉鼠标的位置
创建新粒子
更新新创建的粒子的位置
将其添加到阵列(粒子系统)
这看起来可能很简单,但你必须记住数组不能改变大小。我建议你创建一个ParticleSystem类,负责在系统中添加和删除粒子
<强>编辑:您可能需要考虑使用AARYLIST而不是一个粒子数组。请看
在伪代码中,如下所示:
void mouseReleased() {
int particleX = mouseX;
int particleY = mouseY;
Particle P = new Particle();
P.setPos ( new PVector ( particleX, particleY ) ); // this needs to be implemented
ParticleSystem.add ( P ); // this needs to be implemented
}
我希望这将是一个良好的开端
啊标记已删除,因为我看不出上面的代码与此编程语言有任何关系。如果我错误地删除了标记,请为我澄清这一点。您使用的GUI库是什么?如果您在使用此代码时遇到错误,最好显示错误文本。编辑:我知道,您使用的是处理语言,但不会出现错误r您正在使用JavaScript端口Processing.js,因此我也删除了该标记。Processing,是的,对不起,我认为js有类似的语法,并且认为它也可以应用于此,谢谢!太棒了,谢谢,我会研究它!我尝试了ArrayList方法,但还没有完全理解它,可能还需要进一步使用它。