anonymous-dev
anonymous-dev

Reputation: 3539

Manipulate the scale of my images

I have to work in processing for my school project. So I load in pictures in an array and then in a new class I have another array and I put the pictures in that array every time I create that class. Everytime I create that class I put the images on a different location and that works fine but as soon as I try to adjust the scale of those images it adjusts the scale for the images in the other classes as well. I assume that it changes the scale of all those images because it changes the scale of the images in my first array and I dont create a new one. Is there a way to work around this? This is my code. This is where I load my images:

class ItemLoader
{
  PImage[] itemArray;
  ArrayList itemCollection; 
  String itemType;
  int amountOfFrames;
  int amountOfItems = 1;

  ItemLoader()
  {
    itemCollection = new ArrayList();
    LoadImage();
  }

  void LoadImage()
  {
    for(int ii = 0; ii < amountOfItems; ii++)
    {
     AssignItemType(ii);
     itemArray = new PImage[amountOfFrames];

      for(int i = 0; i < amountOfFrames; i ++)
      {
        String filenaam = itemType + nf(i, 5) + ".png";
        itemArray[i] = loadImage(filenaam);
      }
    itemCollection.add(itemArray);
    }
  }

  void AssignItemType(int itemNumber)
  {
    switch(itemNumber)
    {
      case 0: itemType = "Leaves"; amountOfFrames = 21;
      break;

      case 1: itemType = "Apple";
      break;

      case 2: itemType = "Bannana";
      break;

      case 3: itemType = "Pear";
      break;

      case 4: itemType = "Cherry";
      break;

      case 5: itemType = "Owl";
      break;

      case 6: itemType = "Bird";
      break;
    }
  }
}

Then this is when I create an instance of the class:

itemList.add(new ItemSpawn(randomX,randomY,0, strokeThickness,itemLoader.itemCollection));

And this is where I loop trough the array to animate the images:

class ItemSpawn
{
  PImage[] animationFrames;
  PImage lastFrame;
  float frameCounter;

  int x_loc;
  int y_loc;
  float ImageScale = 0;
  int ImageRotation = 0;  

 ItemSpawn(int x_loc_par, int y_loc_par, int _itemType, float _strokeSize, ArrayList _tempArray)
 {
  animationFrames = (PImage[]) _tempArray.get(_itemType);
  x_loc = x_loc_par;
  y_loc = y_loc_par;
  ApplyScaleRotation();
 }

  void ApplyScaleRotation()
  {
    ImageScale = 0.5;
    ImageRotation = int(random(0,360));

    for(int i = 0; i < animationFrames.length; i++)
    {
      animationFrames[i].resize(int(animationFrames[i].width * ImageScale), int(animationFrames[i].height * ImageScale));
    }    
  }

  void LoopAnimation()
  {
  int convertCount = int(round(frameCounter));

    if(convertCount < animationFrames.length - 1)
    {
     image(animationFrames[convertCount],x_loc - (animationFrames[convertCount].width/2) ,y_loc - (animationFrames[convertCount].height/2));
     frameCounter += 0.4;

    }else
    {
      image(animationFrames[animationFrames.length - 1],x_loc - (animationFrames[convertCount].width/2),y_loc - (animationFrames[convertCount].height/2));
    }
  }
}

Upvotes: 1

Views: 67

Answers (1)

przemek hertel
przemek hertel

Reputation: 4024

In your code each object of class ItemSpawn uses the same PImage[] array. When you change image in one of ItemSpawn instance - all other instances see this change.

Each ItemSpawn object shoud have private copy of image - then it can modify image safely.

Consider such simplified class:

class X {
    int[] array;

    X(int[] array) {
        this.array = array;
    }
    void modify() {
        array[0] = 99;
    }
}

We can create 2 different objects (x1 and x2) of this class using the same array:

int[] values = {1,2,3,4};
X x1 = new X(values);
X x2 = new X(values);

And next x1 modifies array:

x1.modify();

println(x1.array[0]);   // output: 99
println(x2.array[0]);   // output: 99

The solution is to create deep copy of PImage[] array for each object of ItemSpawn class:

ItemSpawn(int x_loc_par, int y_loc_par, int _itemType, float _strokeSize, ArrayList _tempArray)
{

  // old:
  // animationFrames = (PImage[]) _tempArray.get(_itemType);

  // new:
  PImage[] images = _tempArray.get(_itemType);
  animationFrames = new PImage[images.length];  // create new array with the same length

  // copy all images
  for (int i = 0; i < images.length; i++) {
      PImage img = images[i];
      animationFrames[i] = img.get();  // returns copy of this image
  }
  ...
}

Upvotes: 2

Related Questions