# How to apply changes to a particular object of the class in Processing?

**URL:** https://discourse.processing.org/t/how-to-apply-changes-to-a-particular-object-of-the-class-in-processing/26855
**Category:** Libraries
**Created:** [January 7, 2021, 10:29am UTC](https://discourse.processing.org/t/how-to-apply-changes-to-a-particular-object-of-the-class-in-processing/26855 "2021-01-07T10:29:13Z")
**Posts on this page:** 16
**Page:** 1

<div class="post-metadata">

### Author: ![Elmpt](https://avatars.discourse-cdn.com/v4/letter/e/8491ac/32.png) [@Elmpt](https://discourse.processing.org/u/Elmpt)
#### Post date: [January 7, 2021, 10:29am UTC](https://discourse.processing.org/t/how-to-apply-changes-to-a-particular-object-of-the-class-in-processing/26855/1 "2021-01-07T10:29:13Z")

</div>

Hi! I am a little confused with the handling of objects. I have created class “Stone” and then I have created 3 objects Stone stone1, stone2, stone3 in the main scetch. What I need is to make a stone transparent when user moves it. The problem is that when the stone is moved, all 3 stones that I have created become transparent, not the particular stone that was moved. Can you please give me a hint, how can I apply transparency just for the moved stone?  
My “Stone” class:

```auto
   class Stone {
    Playfield p;
    PImage stone;
    boolean overStone = false;
    boolean locked = false;
    boolean stoneMoved;
    int positionX;
    int positionY;
    int stoneXOld;
    int stoneYOld;
    int stoneSize;
    boolean transparencyOn;

    Stone(int stoneXtemp, int stoneYtemp, int stoneSizeTemp, Playfield pf) {
        stone = loadImage("stone.png");

        positionX = stoneXtemp;
        positionY = stoneYtemp;
        stoneXOld = positionX;
        stoneYOld = positionY;

        imageMode(CENTER);
        stoneSize = stoneSizeTemp;
        p = pf;
    }

    void display() {
        if (transparencyOn == true) {
            tint(255, 126);
        } else {
            noTint();
        }
        image(stone, positionX, positionY, stoneSize, stoneSize);
    }

    void update() {
        if (mouseX > positionX - stoneSize / 2 && mouseX < positionX + stoneSize / 2 &&
                mouseY > positionY - stoneSize / 2 && mouseY < positionY + stoneSize / 2) {
            overStone = true;
            if (!locked) {
                tint(160);
            }
        } else {
            noTint();
            overStone = false;
        }
    }

    void interact() {
        if (overStone && mousePressed) {
            locked = true;
            tint(230);
            positionX = mouseX;
            positionY = mouseY;
        } else {
            locked = false;
            noTint();
        }
    }
    
    boolean stoneMoved() {
        return stoneMoved = (!overStone && !locked && positionX > stoneXOld + 100 && positionY > stoneYOld + 100);
    }

    void afterMove() {
           transparencyOn = true;  

    }
}

```

My main scetch:

```auto
import processing.video.*;
Movie water;
Stone stone1,stone2,stone3;
PImage img;

void setup()
{
size(800,800);
water=new Movie(this,"Water.mp4");
water.loop();

stone1=new Stone(70,70,100,this);
stone2=new Stone(180,70,80,this);
stone3=new Stone(270,70,60,this);

}

void draw(){
background(water);

stone1.display();
stone2.display();
stone3.display();

stone1.update();
stone2.update();
stone3.update();

stone1.interact();
stone2.interact();
stone3.interact();
}

void movieEvent(Movie m){
m.read();
}

void mouseReleased(){
stone1.afterMove();
stone2.afterMove();
stone3.afterMove();
}

```

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [January 7, 2021, 10:56am UTC](https://discourse.processing.org/t/how-to-apply-changes-to-a-particular-object-of-the-class-in-processing/26855/2 "2021-01-07T10:56:53Z")

</div>

Hello,

Please provide a minimal example that we can work with.

I was interested but… try the sketch you posted and you will see why.

`:)`

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [January 7, 2021, 10:58am UTC](https://discourse.processing.org/t/how-to-apply-changes-to-a-particular-object-of-the-class-in-processing/26855/3 "2021-01-07T10:58:12Z")

</div>

You can’t rely on tint() outside display()

Instead

- set transparencyOn correctly everywhere and
- evaluate it in display() to set tint / noTint

---

<div class="post-metadata">

### Author: ![Elmpt](https://avatars.discourse-cdn.com/v4/letter/e/8491ac/32.png) [@Elmpt](https://discourse.processing.org/u/Elmpt)
#### Post date: [January 7, 2021, 11:16am UTC](https://discourse.processing.org/t/how-to-apply-changes-to-a-particular-object-of-the-class-in-processing/26855/4 "2021-01-07T11:16:46Z")

</div>

That’s my bad. I tried to delete code that is irrelevant for the issue and didn’t see that some things don’t match. I edited post, thanks

---

<div class="post-metadata">

### Author: ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)
#### Post date: [January 7, 2021, 11:16am UTC](https://discourse.processing.org/t/how-to-apply-changes-to-a-particular-object-of-the-class-in-processing/26855/5 "2021-01-07T11:16:52Z")

</div>

As said above only use tint() when you are actually rendering the object. Also you can isolate changes in drawing style using push/pop style like this

```auto
    void display() {
        pushStyle();
        if (transparencyOn == true) {
            tint(255, 126);
        } else {
            noTint();
        }
        image(stone, stoneX, stoneY, stoneSize, stoneSize);
        popStyle();
    }

```

---

<div class="post-metadata">

### Author: ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)
#### Post date: [January 7, 2021, 11:20am UTC](https://discourse.processing.org/t/how-to-apply-changes-to-a-particular-object-of-the-class-in-processing/26855/6 "2021-01-07T11:20:19Z")

</div>

You still have a call to tint() inside the interact and update methods which is not good. Create an attribute in the Stone class to store the tint level you want during interaction etc then use it in display method

---

<div class="post-metadata">

### Author: ![Elmpt](https://avatars.discourse-cdn.com/v4/letter/e/8491ac/32.png) [@Elmpt](https://discourse.processing.org/u/Elmpt)
#### Post date: [January 7, 2021, 11:25am UTC](https://discourse.processing.org/t/how-to-apply-changes-to-a-particular-object-of-the-class-in-processing/26855/7 "2021-01-07T11:25:26Z")

</div>

got it, I’m going to try it now

---

<div class="post-metadata">

### Author: ![Elmpt](https://avatars.discourse-cdn.com/v4/letter/e/8491ac/32.png) [@Elmpt](https://discourse.processing.org/u/Elmpt)
#### Post date: [January 7, 2021, 12:33pm UTC](https://discourse.processing.org/t/how-to-apply-changes-to-a-particular-object-of-the-class-in-processing/26855/8 "2021-01-07T12:33:04Z")

</div>

I added 2 attributes:

```auto
int tintLevel;
int base;

```

initialise them in constructor:

```auto
base = 255;
tintLevel = 255;

```

in display() I use tint() with base, tintLevel

```auto
tint(base,tintLevel);

```

and then in code instead of tint(), I change just tintLevel value  
e.g: `tintLevel=126;`

should it be something like that?

---

<div class="post-metadata">

### Author: ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)
#### Post date: [January 7, 2021, 12:34pm UTC](https://discourse.processing.org/t/how-to-apply-changes-to-a-particular-object-of-the-class-in-processing/26855/9 "2021-01-07T12:34:46Z")

</div>

Yes and don’t forget the push popStyle calls this will ensure that each stone is rendered without regard to the others.

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [January 7, 2021, 12:40pm UTC](https://discourse.processing.org/t/how-to-apply-changes-to-a-particular-object-of-the-class-in-processing/26855/10 "2021-01-07T12:40:15Z")

</div>

> [@quark](#):
>
> `transparencyOn `

What did you intend your variable `transparencyOn` for?

---

<div class="post-metadata">

### Author: ![Elmpt](https://avatars.discourse-cdn.com/v4/letter/e/8491ac/32.png) [@Elmpt](https://discourse.processing.org/u/Elmpt)
#### Post date: [January 7, 2021, 12:43pm UTC](https://discourse.processing.org/t/how-to-apply-changes-to-a-particular-object-of-the-class-in-processing/26855/11 "2021-01-07T12:43:39Z")

</div>

Firstly I wanted to use it to mark when I need the stone to be transparent and when I do not need it. Now I think I would better create method to set transparency, not sure

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [January 7, 2021, 12:45pm UTC](https://discourse.processing.org/t/how-to-apply-changes-to-a-particular-object-of-the-class-in-processing/26855/12 "2021-01-07T12:45:28Z")

</div>

Well yeah… you made 2 new attributes now, ignoring `transparencyOn` which already had the job in the first place…

just reread my first post

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [January 7, 2021, 2:21pm UTC](https://discourse.processing.org/t/how-to-apply-changes-to-a-particular-object-of-the-class-in-processing/26855/13 "2021-01-07T14:21:22Z")

</div>

more complicate than I thought it was

I made a global var drag that tells us when ANY stone is dragged, then the others are ignored.

(bit of house cleaning might be required)

```auto

import processing.video.*;

// Movie water;

Stone stone1, stone2, stone3;

PImage img;

boolean drag=false; 

void setup()
{
  size(800, 800);
  //water=new Movie(this,"Water.mp4");
  //water.loop();

  stone1=new Stone(70, 70, 100 );
  stone2=new Stone(180, 70, 80 );
  stone3=new Stone(270, 70, 60 );
}

void draw() {
  background(0); // water 

  stone1.display();
  stone2.display();
  stone3.display();

  stone1.update();
  stone2.update();
  stone3.update();

  stone1.interact();
  stone2.interact();
  stone3.interact();
}

void mousePressed() {
  stone1.initMove();
  stone2.initMove();
  stone3.initMove();
}

void mouseReleased() {

  drag=false; 
  stone1.afterMove();
  stone2.afterMove();
  stone3.afterMove();
}

// ===========================================================================================

class Stone {
  // Playfield p;
  PImage stone;

  boolean overStone = false;
  boolean locked = false;
  boolean stoneMoved;
  int positionX;
  int positionY;
  int stoneXOld;
  int stoneYOld;
  int stoneSize;
  boolean transparencyOn;

  Stone(int stoneXtemp, int stoneYtemp, 
    int stoneSizeTemp) {
    stone = loadImage("stone.jpg");

    positionX = stoneXtemp;
    positionY = stoneYtemp;
    stoneXOld = positionX;
    stoneYOld = positionY;

    imageMode(CENTER);
    stoneSize = stoneSizeTemp;
    //p = pf;
  }

  void display() {
    if (transparencyOn == true) {
      tint(255, 126);
    } else {
      noTint();
    }
    image(stone, 
      positionX, positionY, 
      stoneSize, stoneSize);
  }

  void update() {
    if (mouseX > positionX - stoneSize / 2 &&
      mouseX < positionX + stoneSize / 2 &&
      mouseY > positionY - stoneSize / 2 && 
      mouseY < positionY + stoneSize / 2) {
      overStone = true;
      if (!locked&&!drag) {
        transparencyOn=true;
      }
    } else {
      transparencyOn=false; 
      overStone = false;
    }
  }

  void initMove() {
    if (drag)
      return; // leave

    if (overStone && mousePressed) {
      locked = true;
      drag=true;
    } else {
      locked = false;
    }
  }

  void interact() {
    if (drag&&locked) {
      positionX = mouseX;
      positionY = mouseY;
    }
  }

  boolean stoneMoved() {
    return stoneMoved = (!overStone && !locked && positionX > stoneXOld + 100 && positionY > stoneYOld + 100);
  }

  void afterMove() {
    transparencyOn = true;
    locked = false;
  }
}

```

---

<div class="post-metadata">

### Author: ![Elmpt](https://avatars.discourse-cdn.com/v4/letter/e/8491ac/32.png) [@Elmpt](https://discourse.processing.org/u/Elmpt)
#### Post date: [January 7, 2021, 2:37pm UTC](https://discourse.processing.org/t/how-to-apply-changes-to-a-particular-object-of-the-class-in-processing/26855/14 "2021-01-07T14:37:28Z")

</div>

Oh, Thanks a lot, I’ll try it now!

---

<div class="post-metadata">

### Author: ![Elmpt](https://avatars.discourse-cdn.com/v4/letter/e/8491ac/32.png) [@Elmpt](https://discourse.processing.org/u/Elmpt)
#### Post date: [January 7, 2021, 2:41pm UTC](https://discourse.processing.org/t/how-to-apply-changes-to-a-particular-object-of-the-class-in-processing/26855/15 "2021-01-07T14:41:11Z")

</div>

Oh! so I can use drag from all the classes in project! That is astonishing!

---

<div class="post-metadata">

### Author: ![Elmpt](https://avatars.discourse-cdn.com/v4/letter/e/8491ac/32.png) [@Elmpt](https://discourse.processing.org/u/Elmpt)
#### Post date: [January 7, 2021, 3:05pm UTC](https://discourse.processing.org/t/how-to-apply-changes-to-a-particular-object-of-the-class-in-processing/26855/16 "2021-01-07T15:05:15Z")

</div>

This is top! I have added some more things that I need, and it seems to be working perfectly!
