Image 操纵我的图像的比例
我必须为我的学校项目处理工作。所以我在一个数组中加载图片,然后在一个新的类中,我有另一个数组,每次我创建这个类时,我都把图片放在这个数组中。每次我创建该类时,我都会将图像放在不同的位置,这很好,但只要我尝试调整这些图像的比例,它就会调整其他类中图像的比例。我假设它改变了所有这些图像的比例,因为它改变了我第一个数组中图像的比例,而我没有创建一个新数组。有办法解决这个问题吗?这是我的密码。这是我加载图像的地方:Image 操纵我的图像的比例,image,processing,Image,Processing,我必须为我的学校项目处理工作。所以我在一个数组中加载图片,然后在一个新的类中,我有另一个数组,每次我创建这个类时,我都把图片放在这个数组中。每次我创建该类时,我都会将图像放在不同的位置,这很好,但只要我尝试调整这些图像的比例,它就会调整其他类中图像的比例。我假设它改变了所有这些图像的比例,因为它改变了我第一个数组中图像的比例,而我没有创建一个新数组。有办法解决这个问题吗?这是我的密码。这是我加载图像的地方: class ItemLoader { PImage[] itemArray; A
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;
}
}
}
这就是我在数组中循环以设置图像动画的地方:
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));
}
}
}
类itempawn
{
PImage[]动画帧;
PImage-lastFrame;
浮点帧计数器;
int x_loc;
国际奥委会;
浮动图像比例=0;
int-ImageRotation=0;
ItemSpawn(int x_loc_par、int y_loc_par、int_itemType、float_strokeSize、ArrayList_tempArray)
{
animationFrames=(PImage[])\u tempArray.get(\u itemType);
x_loc=x_loc_par;
y_loc=y_loc_par;
ApplyScaleRotation();
}
无效ApplyScaleRotation()
{
ImageScale=0.5;
ImageRotation=int(随机(0360));
对于(int i=0;i
在代码中,ItemSpawn类的每个对象都使用相同的PImage[]
数组。当您在一个itemsawn
instance中更改图像时,所有其他实例都会看到此更改
每个itemsawn
对象都应该有映像的私有副本,这样它就可以安全地修改映像
考虑这样的简化类:
class X {
int[] array;
X(int[] array) {
this.array = array;
}
void modify() {
array[0] = 99;
}
}
我们可以使用相同的数组创建此类的2个不同的对象(x1和x2):
int[] values = {1,2,3,4};
X x1 = new X(values);
X x2 = new X(values);
接下来x1
修改数组:
x1.modify();
println(x1.array[0]); // output: 99
println(x2.array[0]); // output: 99
解决方案是为itempawn
类的每个对象创建PImage[]
数组的深度副本:
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
}
...
}
itempawn(int x_loc_par、int y_loc_par、int_itemType、float_strokeSize、ArrayList_tempary)
{
//旧的:
//animationFrames=(PImage[])\u tempArray.get(\u itemType);
//新的:
PImage[]images=\u tempArray.get(\u itemType);
animationFrames=new PImage[images.length];//创建具有相同长度的新数组
//复制所有图像
对于(int i=0;i
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
}
...
}