1. Member
Join Date
Oct 2015
Posts
4
Rep Power
0

## Processing Help: Fades to White

Hi, I am fairly new to CS and am still trying to learn. I am trying to create three color sliders to produce a color for a rectangle on the right of the sliders. The problem is I can't get the sliders to slide back (won't decrease in x value). If anyone can help me I would greatly appreciate it, I just want to understand what I did wrong. Also if anyone has any suggestions to my code to better the performance, that would be amazing.

Thank You!

class Slider{

float x;

float y;

//float w;

//float h;

//color c;

boolean isPressed;

void drawObject(){

stroke(0);

strokeWeight(2);

fill(255);

rect(x,y,25,50);

}

void updateObject(boolean changeSlider){

if(x > 305){

x = 305;

}

if(x < 50){

x = 50;

}

}

boolean hitTest(float xClicked, float yClicked){

if(xClicked > x && xClicked < x+25 && yClicked > y && yClicked < y+50){

x=mouseX;

return true;

}

return false;

}

}

Slider S1;

Slider S2;

Slider S3;

void setup(){

size(500,500);

S1 = new Slider();

S2 = new Slider();

S3 = new Slider();

S1.x = 50;

S1.y = 100;

S2.x = 50;

S2.y = 200;

S3.x = 50;

S3.y = 300;

S1.updateObject(true);

S2.updateObject(true);

S3.updateObject(true);

}

void draw(){

background(120);

S1.updateObject(false);

S2.updateObject(false);

S3.updateObject(false);

S1.drawObject();

S2.drawObject();

S3.drawObject();

fill(S1.x,S2.x,S3.x);

rect(400,100,100,300);

}

void mouseDragged(){

if(S1.hitTest(mouseX,mouseY)){

S1.isPressed = !S1.isPressed;

if(!S1.isPressed){

S1.updateObject(true);

}

}

if(S2.hitTest(mouseX,mouseY)){

S2.isPressed = !S2.isPressed;

if(!S2.isPressed){

S2.updateObject(true);

}

}

if(S3.hitTest(mouseX,mouseY))

{

S3.isPressed = !S3.isPressed;

if(!S3.isPressed){

S3.updateObject(true);

}

}

}

2. ## Re: Processing Help: Fades to White

Please edit your post and wrap the code in code tags to make it easier to read and understand.
Also can you remove the extra blank lines?

3. Member
Join Date
Oct 2015
Posts
4
Rep Power
0

## Re: Processing Help: Fades to White

Hopefully this is what you meant in order to better understand it?

Java Code:
```class Slider{
float x;
float y;
boolean isPressed;
void drawObject(){
stroke(0);
strokeWeight(2);
fill(255);
rect(x,y,25,50);
}

void updateObject(boolean changeSlider){
if(x > 305){
x = 305;
}
if(x < 50){
x = 50;
}
}

boolean hitTest(float xClicked, float yClicked){
if(xClicked > x && xClicked < x+25 && yClicked > y && yClicked < y+50){
x=mouseX;
return true;
}
return false;
}
}

Slider S1;
Slider S2;
Slider S3;

void setup(){
size(500,500);
S1 = new Slider();
S2 = new Slider();
S3 = new Slider();
S1.x = 50;
S1.y = 100;
S2.x = 50;
S2.y = 200;
S3.x = 50;
S3.y = 300;
S1.updateObject(true);
S2.updateObject(true);
S3.updateObject(true);
}

void draw(){
background(120);
S1.updateObject(false);
S2.updateObject(false);
S3.updateObject(false);
S1.drawObject();
S2.drawObject();
S3.drawObject();
fill(S1.x,S2.x,S3.x);
rect(400,100,100,300);
}

void mouseDragged(){
if(S1.hitTest(mouseX,mouseY)){
S1.isPressed = !S1.isPressed;
if(!S1.isPressed){
S1.updateObject(true);
}
}

if(S2.hitTest(mouseX,mouseY)){
S2.isPressed = !S2.isPressed;
if(!S2.isPressed){
S2.updateObject(true);
}
}

if(S3.hitTest(mouseX,mouseY)){
S3.isPressed = !S3.isPressed;
if(!S3.isPressed){
S3.updateObject(true);

}
}
}```

4. ## Re: Processing Help: Fades to White

That's a lot better. However there shouldn't be }s one above another. The }s should be in the same column that the start of the line with the paring {.

The way the code is now its very hard to see what statements are nested inside of other statements.

How is the code tested? I don't see a main() method to start execution or a JFrame to hold the GUI components.

Also there are no comments in the code describing what it is trying to do. That also makes the code harder to understand.
Last edited by Norm; 10-21-2015 at 01:30 AM.

5. Member
Join Date
Oct 2015
Posts
4
Rep Power
0

## Re: Processing Help: Fades to White

I am actually being taught this style of writing. I didn't know if this style is easy or difficult to be read. This code is tested in Processing.exe

6. ## Re: Processing Help: Fades to White

This code is tested in Processing.exe
That means I can't help much. I use the javac and java commands to compile and execute my java programs. I have no way to compile and execute your code to test it and see what it does.

I am actually being taught this style of writing.
I personally don't like it and I doubt you will find anyone here that does. The proper formatting of code vastly improves its readability making it easier to understand the code's logic. The alignment of } with its pairing { is very important.
Last edited by Norm; 10-21-2015 at 02:20 AM.

7. Member
Join Date
Oct 2015
Posts
4
Rep Power
0

## Re: Processing Help: Fades to White

I am actually being taught this style of writing. I didn't know if this style is easy or difficult to be read. This code is tested in Processing.exe