Results 1 to 8 of 8
  1. #1
    Falcrow is offline Member
    Join Date
    Oct 2015
    Posts
    4
    Rep Power
    0

    Default 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. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default 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?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Falcrow is offline Member
    Join Date
    Oct 2015
    Posts
    4
    Rep Power
    0

    Default 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. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default 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.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Falcrow is offline Member
    Join Date
    Oct 2015
    Posts
    4
    Rep Power
    0

    Default 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
    Thank you for your help.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default 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.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    Falcrow is offline Member
    Join Date
    Oct 2015
    Posts
    4
    Rep Power
    0

    Default 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
    Thank you for your help.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Processing Help: Fades to White

    What are you being taught about adding comments to your code to describe what it is trying to do?
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. white space in variables?
    By letitworknow in forum New To Java
    Replies: 4
    Last Post: 01-14-2013, 01:50 PM
  2. Replies: 2
    Last Post: 06-29-2011, 09:08 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •