简体   繁体   中英

Image disappears when resizing the canvas

I'm launching this demo code from lectures

package demos;
import processing.core.PApplet;
import processing.core.PImage;

public class MyPApplet extends PApplet{
    PImage img;

    public void setup() {
        //Add setup code for MyPApplet
        size(600,600);  //set canvas size>>changed to size(700,800)
        background(255);            //set canvas color
        stroke(0);              //set pen color
        img = loadImage("palmTrees.jpg", "jpg");
        img.resize(0, height);          //resize loaded image to full height of canvas
        image(img, 0, 0);           //display image 
    }

    public void draw() {
        //Add drawing code for MyPApplet
        int[] color = sunColorSec(second());        //calculate color code for sun
        fill(color[0],color[1],color[2]);   //set sun color
        ellipse(width/4,height/5,width/4,height/5); //draw sun
    }


    /** Return the RGB color of the sun at this number of seconds in the minute */
    public int[] sunColorSec(float seconds)
    {
        int[] rgb = new int[3];
        // Scale the brightness of the yellow based on the seconds.  0 seconds 
        // is black.  30 seconds is bright yellow.
        float diffFrom30 = Math.abs(30-seconds);

        float ratio = diffFrom30/30;
        rgb[0] = (int)(255*ratio);
        rgb[1] = (int)(255*ratio);
        rgb[2] = 0;

        //System.out.println("R" + rgb[0] + " G" + rgb[1] + " B" + rgb[2]);
        return rgb;
    }   

    public static void main (String[] args) {
        //Add main method for running as application
        PApplet.main(new String[] {"--present", "MyPApplet"});
    }
}

The problem is that when I try to change the size of my canvas to more than (600,600), then the background image disappears. Here is what I'm talking about: and here is a copy of the image i 'm working on https://www.mediafire.com/?dnpghfefeo7rl5o

Before:

之前

After:

后

You are not displaying the image in your draw method. Instead, you are only displaying it once, in setup . When you resize the canvas, then the window will be cleared and draw will be called. There you should paint the image:

public void draw() {

    image(img, 0, 0);  // <---- Add this here!

    //Add drawing code for MyPApplet
    ...
...

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