调整画布大小时图像消失

时间:2016-07-14 20:17:29

标签: java image canvas processing

我从讲座中启动此演示代码

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"});
    }
}

问题在于,当我尝试将画布的大小更改为(600,600)以上时,背景图像会消失。这就是我所说的: 这是我在https://www.mediafire.com/?dnpghfefeo7rl5o

上工作的图像的副本

在:

before

后:

after

1 个答案:

答案 0 :(得分:0)

您没有在draw方法中显示图片。相反,您只需在setup中显示一次。调整画布大小时,将清除窗口并调用draw那里你应该画出图像:

public void draw() {

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

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