简体   繁体   中英

Trying to resize image in Processing (PDE) language

I am in the holiday spirit, and I was thinking about writing a little countdown timer for Christmas, but ran into some difficulty that I cannot resolve. I have 2 PImage variables, one that was directly loaded from an image on the hard drive called "origbkg", while the other is the actual image that is being displayed as the background for my program. I made it this way so that if the user changes the size of the window, the image will resize correspondingly. However, when I make the window very small, and then try to maximize it again it loses resolution, although it should be getting reset to the original full resolution image that was loaded off the drive.

When I have tried to display the "origbkg" on the screen, it also is getting modified for some reason when all I am doing is setting bkgimg=origbkg .

Here is my code:

//Christmas Countdown Clock
//by: Ben Templin

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




//-------------------------Variables-------------------------


boolean bkgsized;
String background;
PImage origbkg;
PImage bkgimg;
int count;
int psize;
int size;


//-------------------------Setup-------------------------


void setup()
{
  frameRate(30);
  smooth();
  size(displayWidth-250, displayHeight-250);
  frame.setResizable(true);
  background="default";
  textSize(20);
  fill(255, 0, 0);
  text("Loading Images...", width/2-100, height/2);
  origbkg=loadImage("Christmas-Tree.png");
  bkgsized=false;
  bkgimg=origbkg;
}


//-------------------------Draw-------------------------


void draw()
{
  size=width+height;
  if(psize!=size){
    bkgsized=false;
  }

  if(bkgsized==false){
    sizebkg();
  }
  psize=width+height;
}


//-------------------------Size the Background-------------------------


void sizebkg()
{
  bkgimg=origbkg;
  bkgimg.resize(width, height);
  image(bkgimg, 0, 0);
  bkgsized=true;
}

Can someone please tell me why "origbkg" is getting changed? Thanks in advance, and

Merry Christmas!

bkgimg=origbkg;
bkgimg.resize(width, height);

Variable origbkg holds a reference to a PImage object. First, this code copies that reference into bkgimg, so that both bkgimg and origbkg refer to the same PImage. Then, it resizes the image which bkgimg refers to--which is the same image that origbkg refers to! To use this technique, you'll have to use some method to clone or copy the image, and set bkgimg to the copy, not the original. However, for this use case, it is actually more efficient to simply use the image() method with additional parameters to rescale the image to fit into your width and height:

image(origbkg, 0, 0, width, height);

This way, you don't have to create a new copy of the image each time it is resized.

As a side note, your draw() method's check to see if the window has been resized is flawed: It checks whether the sum of width+height has changed, but this check fails to account for the possibility that the window is resized such that its width grows by the same distance that height shrinks, or vice versa. To properly check for a resize, you must store both a pwidth variable and a pheight variable and compare them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM