Hello, i’m a new user and a processing beginner, i’m writing a program and i need some help!
My program idea is this:
I draw a square (for now, more than one can be difficult -for me-), when i click on it i select the square and i change the stroke. If i re-click, i just delete the “selection” and the stroke.
When i click over my square and it’s selected, my square have to do some actions with keypressed:
1: with the key ‘r’ - the square have to rotate;
2: with the keys ’ + ’ have to change the scale of 10%, otherwise with key ‘-’ have to change scale of “-10%”
3: with the spacebar i change the fill color randomly
Now i’ve described the program and i can tell you my problems.
- i don’t know how i can select and unselect my square, and, i don’t know how to make actions only when is selected. I was thinking something like this, but if i try to write keypressed functions this program doesn’t work.
As you can see in this code, i can select and unselect with click, but if i try to put some actions with keyPressed or the rotation this doesn’t work!
square[] square = new square[1];
void setup()
{
size(700, 500);
strokeWeight(3);
for(int i=0; i < square.length; i++)
{
square[i] = new square();
}
}
void draw()
{
background(0);
for(int i=0; i < square.length; i++)
{
square[i].displayUpdate();
}
}
void mouseReleased()
{
for(int i=0; i < square.length; i++)
{
square[i].select();
}
}
//class square
class square {
float xpos;
float ypos;
float sz = 140;
boolean selected = false;
square()
{
xpos = 300;
ypos = 150;
}
void displayUpdate()
{
if (isOver() || selected)
{
if(selected)
stroke(255,0,0);
else
noStroke ();
}
else
{
noStroke();
}
fill(57, 192, 178);
rect(xpos, ypos, sz, sz);
}
boolean isOver()
{
return (mouseX > xpos && mouseX < xpos + sz && mouseY > ypos && mouseY < ypos + sz);
}
void select()
{
if (isOver())
selected = !selected;
}
}
Can i work on this program to add these actions or not? Should be great!
I have wrote another program with the functions (+ & - ) and (spacebar change color);
in this program if i try to put the code for make it rotate, the program doesn’t work.
I copy the second program
square Q1;
int x = 300;
int y = 300;
int s = 50;
float r = 0;
void setup() {
size(800, 400);
background (0);
Q1 = new square (140, 300, 150, 255);
}
void draw() {
Q1.square ();
}
void keyPressed(){
if (keyCode == 521 )
Q1.morescale ();
if(keyCode == 45)
Q1.lesscale ();
if (keyCode == 32)
Q1.colorsquare ();
if (key 'r')
Q1.rrotate ();
}
//class square
class square {
int size= 140;
int xPos;
int yPos;
color colorsquare;
int r ;
float rr ;
square (int d, int x, int y, color c) {
xPos = x;
yPos = y;
colorsquare = color (c);
}
void square () {
rect (xPos, yPos, size, size);
}
void rrotate() {
pushMatrix();
translate(x,y);
rotate(radians(rr+=0.5));
stroke(255);
popMatrix();
}
void morescale () {
size ++;
}
void lesscale () {
size --;
}
void colorsquare () {
fill( random(255), random(255), random(255), random(255));
}
}
This is the second code “idea”, i need to change the scale of 10% and -10% but i don’t know how to make it, so i’ve just wrote a easy code size++; and size–;
So, my primary question is: i can put the second code, with rotation and scale functions in the first program? If yes, how can i do? I’ve tried tons of times, if someone can help me i’m here Thank you!