i want to fix a dynamic value and convert it in a final value
in
Programming Questions
•
9 months ago
can i convert a dynamic value in a final value?
In this case, i try with a basic example ( based on arraylist class example) i just want to , when using keyPress event lock the dimensions of the rect: so i need to change the constructor value x and y into final values.
Maybe there are other ways to lock these values, using an array or something like that, i think it's a problem of formulation
error message is : "the final field arraylistclassball cannot be assigned"
----------------------------------------
ArrayList balls;
int ballWidth = 48;
void setup() {
size(200, 200);
smooth();
noStroke();
balls = new ArrayList();
balls.add(new Ball(width/2, 0, ballWidth));
}
void draw() {
background(255);
for (int i = balls.size()-1; i >= 0; i--) {
Ball ball = (Ball) balls.get(i);
ball.display();
}
}
void mousePressed() {
balls.add(new Ball(mouseX, mouseY, ballWidth));
}
class Ball {
float x;
float y;
float w;
final float fx=0;
final float fy=0;
Ball(float tempX, float tempY, float tempW) {
x = tempX;
y = tempY;
w = tempW;
}
void fix(){
if(keyPressed){fx=this.x;fy=this.y; this.x=fx; this.y=fy;} //CRASH
}
void display() {
fill(0);
rect(x,y,mouseX,mouseY);
}
}
1