调整 canvas 大小时图像消失
Image disappears when resizing the canvas
我正在从讲座中启动这个演示代码
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"});
}
}
问题是,当我尝试将 canvas 的大小更改为大于 (600,600) 时,背景图像就会消失。这就是我要说的:
这是我正在处理的图像的副本 https://www.mediafire.com/?dnpghfefeo7rl5o
之前:
之后:
您没有在 draw
方法中显示图像。相反,您只在 setup
中显示一次。当您调整 canvas 的大小时,window 将被清除并调用 draw
。 那里 你应该画图像:
public void draw() {
image(img, 0, 0); // <---- Add this here!
//Add drawing code for MyPApplet
...
...
我正在从讲座中启动这个演示代码
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"});
}
}
问题是,当我尝试将 canvas 的大小更改为大于 (600,600) 时,背景图像就会消失。这就是我要说的: 这是我正在处理的图像的副本 https://www.mediafire.com/?dnpghfefeo7rl5o
之前:
之后:
您没有在 draw
方法中显示图像。相反,您只在 setup
中显示一次。当您调整 canvas 的大小时,window 将被清除并调用 draw
。 那里 你应该画图像:
public void draw() {
image(img, 0, 0); // <---- Add this here!
//Add drawing code for MyPApplet
...
...