我有一個基本程式,其中庫存影像構成了更大程式的背景 - 但是許多影像的大小相互之間略有不同。
我的初始代碼加載背景影像并嘗試根據影像尺寸設定畫布大小:
PImage bg;
void setup() {
bg = loadImage("102.jpg");
println(bg.width);
println(bg.height);
wWidth = bg.width;
wHeight = bg.height;
size(wWidth,wHeight);
}
void draw() {
background(bg);
}
我得到 'IllegalStateException' 錯誤 bg.width 和 bg.height 是 806 和 1229,當我分別包含 806 和 1229 而不是 wWidth 和 wHeight 時,我得到了我想要的輸出 - 我是否需要在不同的地方宣告 size()道路?或者嘗試通過處理將 jpg 檔案調整為相同大小會更簡單嗎?
uj5u.com熱心網友回復:
這在處理 2 中有效,但在處理 3 中發生了變化:您只需要使用settings().
這是加載影像并將草圖大小更改為影像尺寸的最小草圖:
PImage img;
void settings(){
img = loadImage("https://processing.org/img/processing-web.png");
println(img.width, img.height);
size(img.width, img.height);
}
void setup(){
image(img, 0, 0);
}
你的代碼看起來像:
PImage bg;
int wWidth;
int wHeight;
void settings(){
bg = loadImage("102.jpg");
println(bg.width);
println(bg.height);
wWidth = bg.width;
wHeight = bg.height;
size(wWidth, wHeight);
}
void setup() {
}
void draw() {
background(bg);
}
稍微簡單的版本是:
PImage bg;
void settings(){
bg = loadImage("102.jpg");
println(bg.width);
println(bg.height);
size(bg.width, bg.height);
}
void setup() {
}
void draw() {
background(bg);
}
這些wWidth, wHeight變數可能是多余的,因為 Processing 的width,height變數在size()被呼叫后存盤相同的資料。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/344571.html
