Hello everybody. I`ve a problem:
I`ve a array of ellipse Objects. Every object has his variable of size in the constructor. In a second screen, via controlP5, a slider that change the size of every ellipse. What i want is change the size of an ellipse object when isClicked is true, and in that particular object, and store that value. But i cant, and i dont know exaclty why. I think that when i change the size in the constructor every object take the same value.
here the code, sorry for my very bad english, hope you understand:
(in resume: click the object, change it value, store the value. click in another object, and change it value (different of object 1)…etc.)
import controlP5.*;
ArrayList<Sphere>s;
PWindow controller;
void settings() {
size(500, 500, P3D);
}
void setup() {
controller = new PWindow();
s = new ArrayList<Sphere>();
s.add(new Sphere(new PVector(random(width), random(height)), 255, 50, 0));
}
void draw() {
background(0);
pushMatrix();
select();
for (Sphere s : s) {
s.display();
s.isOver(mouseX, mouseY);
}
popMatrix();
}
void select() {
for (Sphere s : s) {
if (s.isClick) {
println(s.id, "HOLA");
s.c = 25;
println("");
}
}
}
void mousePressed() {
if (mouseButton==RIGHT) s.add(new Sphere(new PVector(mouseX, mouseY), 255, 50, s.size()+1));
for (Sphere s : s) {
if (mouseButton==LEFT && s.over) {
s.isClick = true;
} else {
s.isClick = false;
}
}
}
class Sphere {
float c, size;
float x, y;
ArrayList<PVector> p = new ArrayList<PVector>();
boolean over = false;
boolean isClick = false;
int id;
int initialValue = 50;
Sphere(PVector pos, float _color, float _size, int _id) {
p.add(pos);
c = _color;
size = _size;
id = _id;
}
void display() {
for (PVector pos : p) {
pushMatrix();
translate(pos.x, pos.y);
fill(c);
ellipse(0, 0, size, size);
popMatrix();
}
}
boolean isOver(float px, float py) {
ellipseMode(RADIUS);
for (PVector pos : p) {
float d = dist(px, py, pos.x, pos.y);
if (d < size) {
over = true;
c = 150;
return true;
} else {
over = false;
c = 255;
return false;
}
}
return false;
}
}
public class PWindow extends PApplet {
PWindow() {
super();
PApplet.runSketch(new String[] {this.getClass().getSimpleName()}, this);
}
ControlP5 cp5;
int vertices = 50;
float ss;
Slider abc;
void settings() {
size(500, 250, P2D);
}
void setup() {
cp5 = new ControlP5(this);
background(0);
cp5.addSlider("vertices")
.setPosition(25, 50)
.setSize(200, 20)
.setRange(0, 50)
;
}
void draw() {
background(0);
for (int i = s.size()-1; i >= 0; i--) {
Sphere obj = s.get(i);
ss = vertices;
if (obj.isClick==true) {
fill(255);
stroke(255);
textSize(13);
text(i, width/2, 150);
println(i);
obj.size = vertices;
}
}
}
}