Java 正在处理,无法为ArrayList中的对象设置alpha值
这是对此的后续行动 我希望让一堆粒子对象保持在某个“alpha”值。此alpha值将根据其与相邻粒子的接近程度增加/减少 目前,我的代码导致所有粒子都保持在max alpha。我相信这是由于对ArrayList的迭代导致alpha被多次重绘。程序也因此运行缓慢Java 正在处理,无法为ArrayList中的对象设置alpha值,java,arraylist,processing,alpha,Java,Arraylist,Processing,Alpha,这是对此的后续行动 我希望让一堆粒子对象保持在某个“alpha”值。此alpha值将根据其与相邻粒子的接近程度增加/减少 目前,我的代码导致所有粒子都保持在max alpha。我相信这是由于对ArrayList的迭代导致alpha被多次重绘。程序也因此运行缓慢 class Particle{ PVector velocity, location; //PVector variables for each particle. Particle(){ //Constructor -
class Particle{
PVector velocity, location; //PVector variables for each particle.
Particle(){ //Constructor - random location and speed for each particle.
velocity = new PVector(random(-0.5,0.5), random(-0.5,0.5));
location = new PVector(random(0,width),random(0,width));
}
void update() { location.add(velocity); } //Motion method.
void edge() { //Wraparound case for particles.
if (location.x > width) {location.x = 0;}
else if (location.x < 0) {location.x = width;}
if (location.y > height) {location.y = 0;}
else if (location.y < 0) {location.y = height;}
}
void display(ArrayList<Particle> p){ //Display method to show lines and ellipses between particles.
for(Particle other: p){ //For every particle in the ArrayList.
float d = PVector.dist(location,other.location); //Get distance between any two particle.
float a = 255 - map(d,0,112,0,255); //Map variable 'a' as alpha based on distance. E.g. if distance is high, d = 100, alpha is low, a = 255 - 225 = 30.
if(other==this){continue;}
println("Lowest distance of any two particle =" + d); //Debug output.
if(d<112){ //If the distance of any two particle falls bellow 112.
noStroke(); //No outline.
fill(0,a); //Particle are coloured black, 'a' to vary alpha.
ellipse(location.x, location.y, 8, 8); //Draw ellipse based on location of particle.
}else{
noStroke(); //No outline.
fill(0,30); //For particles far away, set them to a fix alpha of '30'
ellipse(location.x, location.y, 8, 8); //Draw ellipse based on location of particle.
}
}
}
}
ArrayList<Particle> particles = new ArrayList<Particle>(); //Create a new arraylist of type Particle.
void setup(){
size(640,640,P2D); //Setup frame of sketch.
for (int i=0; i<40; i++) {
particles.add(new Particle()); //Add five Particle elements into arraylist.
}
}
void draw(){
background(255); //Set white background.
for(Particle p: particles){ //For every 'p' of type Particle in arraylist particles.
p.update(); //Update location based on velocity.
p.display(particles); //Display each particle in relation to other particles.
p.edge(); //Wraparound if particle reaches edge of screen.
}
}
类粒子{
PVector速度,位置;//每个粒子的PVector变量。
Particle(){//Constructor-每个粒子的随机位置和速度。
速度=新PVector(随机(-0.5,0.5),随机(-0.5,0.5));
位置=新PVector(随机(0,宽度),随机(0,宽度));
}
void update(){location.add(velocity);}//运动方法。
void edge(){//粒子的包围盒。
如果(location.x>width){location.x=0;}
如果(location.x<0){location.x=width;}
如果(location.y>height){location.y=0;}
如果(location.y<0){location.y=height;}
}
void display(ArrayList p){//显示粒子之间的直线和椭圆的显示方法。
对于(Particle other:p){//对于ArrayList中的每个粒子。
float d=PVector.dist(location,other.location);//获取任意两个粒子之间的距离。
浮点a=255-映射(d,0112,0255);//根据距离将变量“a”映射为alpha。例如,如果距离高,d=100,alpha低,a=255-225=30。
如果(other==this){continue;}
println(“任意两个粒子的最小距离=”+d);//调试输出。
如果(d你也有同样的问题:对于每个粒子
,你正在循环通过每个其他粒子
,因此你最终为每个粒子
绘制了40个椭圆。相反,你需要循环通过粒子
找到最近的邻居,然后根据你的阿尔法计算一个粒子
换句话说,您的绘图代码应该发生在for
循环的之后
查找最近的邻居如下所示:
Particle closestNeighbor = null;
float closestDistance = 100000;
for (Particle other : p) { //For every particle in the ArrayList.
if (other == this) {
continue;
}
float d = PVector.dist(location, other.location);
if (d < closestDistance) {
closestDistance = d;
closestNeighbor = other;
}
}
请在交叉柱之间链接:
float a = 255 - map(closestDistance, 0, 112, 0, 255); //Map variable 'a' as alpha based on distance. E.g. if distance is high, d = 100, alpha is low, a = 255 - 225 = 30.
if (closestDistance<112) {
noStroke(); //No outline.
fill(0, a); //Particle are coloured black, 'a' to vary alpha.
ellipse(location.x, location.y, 8, 8); //Draw ellipse based on location of particle.
} else {
noStroke(); //No outline.
fill(0, 30); //For particles far away, set them to a fix alpha of '30'
ellipse(location.x, location.y, 8, 8); //Draw ellipse based on location of particle.
}