在此草圖中,它水平繪製一個對象列表。每個對象的x位置都依賴於其左側的對象。另外,當對象不在屏幕上時,它不會繪製。但是當我移動時,對象抽搐。我真的不知道爲什麼,這可能是因爲處理的加載圖片需要一些時間,所以會有一些延遲?我只用繪製矩形(rect())測試了這個問題,並且問題沒有發生。繪圖處理中抽搐的圖像
下面是吸引了來自具有相同算法的網絡加載圖像的代碼:(可能需要一段時間的草圖加載,因爲它檢索從不同的網站的圖像)運行後草圖,使用左右箭頭鍵左右移動。使用向上和向下箭頭更改移動速度。
PImage backgroundImage;
PFont font;
int x = 0;
int xoffset = 0;
int deltax = 5;
float scale = 512/30;
float deltascale = 0.02;
ArrayList<Object> ObjectList;
class Object {
String name;
float diameter;
String unit;
String sprite;
PImage img;
int wid;
int hei;
int xpos;
Object(String Oname, float Odiameter, String Ounit, String Oimg) {
name = Oname;
diameter = Odiameter;
unit = Ounit;
img = loadImage(Oimg);
sprite = Oimg;
//img = loadImage(Oimg);
wid = img.width;
hei = img.height;
}
}
void setup() {
size(1500, 800);
frameRate(200);
fill(255,255,255);
textSize(32);
textAlign(CENTER, CENTER);
text("Images are loading (this may take a while)",700,400);
ObjectList = new ArrayList();
readObjects();
}
void addObject(String Aname, float Adiameter, String Aunit, String Aimg) {
ObjectList.add(new Object(Aname, Adiameter, Aunit, Aimg));
}
void readObjects() {
addObject("RED", 30, "m", "https://s-media-cache-ak0.pinimg.com/736x/8f/1b/cc/8f1bcc72c81d1e9370597f7239ee476a.jpg");
//addObject("ORANGE", 60, "m", "http://6iee.com/data/uploads/36/489075.jpg");
addObject("YELLOW", 70, "m", "https://i.stack.imgur.com/aez1V.jpg");
addObject("GREEN", 100, "m", "http://space-facts.com/wp-content/uploads/mars.jpg");
addObject("PURPLE", 105, "m", "http://space-facts.com/wp-content/uploads/jupiter.png");
addObject("PINK", 110, "m", "https://vignette1.wikia.nocookie.net/starwars/images/4/4a/Alderaan.jpg");
addObject("BROWN", 150, "m", "https://cdn.pixabay.com/photo/2013/07/12/16/33/venus-151142_960_720.png");
addObject("GRAY", 180, "m", "http://www.lpi.usra.edu/lpi_40th/images/1989/neptune.jpg");
//addObject("WHITE", 200, "m", "http://www.clipartkid.com/images/19/log-in-sign-up-upload-clipart-Dpr48a-clipart.png");
addObject("INDIGO", 300, "m", "https://upload.wikimedia.org/wikipedia/commons/thumb/a/a0/Sun_in_February.jpg/155px-Sun_in_February.jpg");
addObject("SKYBLUE", 301, "m", "https://upload.wikimedia.org/wikipedia/commons/e/ea/Uranus_(Edited).jpg");
addObject("BLUE", 1000, "m", "https://vignette2.wikia.nocookie.net/heman/images/3/36/Earth.jpg");
}
void draw() {
fill(0,0,0);
rect(0, 0, 1500, 800);
int x = 0 + xoffset;
for (Object drawObject : ObjectList) {
float dia = drawObject.diameter;
if (x+drawObject.img.width >= 0 && x <= 1500) {
//checks if image is too small to be drawn
int drawWidth = int(scale * dia);
int drawHeight = int(drawWidth * drawObject.hei/drawObject.wid);
if (drawWidth > 0 && drawHeight > 0) {
drawObject.img.resize(drawWidth, drawHeight);
image(drawObject.img, x, 400-(drawObject.img.height/2));
//calculates font size relative to object's current size
int textSize;
//checks if the name or diameter is longer, then uses the longer one for calculation
fill(255,255,255);
if (drawObject.name.length() < str(drawObject.diameter).length()) {
textSize = int(drawObject.img.width/str(drawObject.diameter).length());
} else {
textSize = int(drawObject.img.width/drawObject.name.length());
}
if (textSize > 0) {
textSize(textSize);
float texty = 418+(drawObject.img.height/2);
if (texty < (418+(drawObject.img.height/2))) {
texty = 418;
}
text(drawObject.name, x+(drawObject.img.width/2), texty);
text(drawObject.diameter+drawObject.unit, x+(drawObject.img.width/2), texty+textSize);
}
}
drawObject.xpos = x;
x += (drawObject.img.width*0.08 + drawObject.img.width);
}
//display deltax
textSize(32);
text(str(deltax), 1450, 10);
text(str(deltascale), 1450, 50);
println(str(scale));
}
}
void mouseWheel(MouseEvent event) {
float e = event.getCount();
if (e < 0) {
scale *= (1 + deltascale);
} else if (e > 0) {
scale *= (1 - deltascale) ;
}
}
void keyPressed() {
if (key == CODED) {
if (keyCode == LEFT) {
xoffset += deltax;
} else if (keyCode == RIGHT) {
xoffset -= deltax;
} else if (keyCode == UP) {
deltax += 5;
} else if (keyCode == DOWN) {
deltax -= 5;
}
}
if (key == 'r') {
//reloadObjects();
} else if (key == 'q') {
deltascale += 0.02;
} else if (key == 'a') {
deltascale -= 0.02;
}
if (deltax < 0) {
deltax = 0;
} else if (deltascale < 0) {
deltascale = 0;
}
}
下面的原代碼將無法工作,因爲它使用的是隻有我有圖片。
我希望有人能幫助我解決這個bug,或者幫助我提高我的算法。 編輯:代碼現在可以驗證,並提出我遇到的問題
請發表演示問題的[mcve]。就像你說的那樣,你發佈的代碼不會出現問題,所以調試起來會有點困難。此外,你真的應該嘗試格式化你的代碼 - Processing編輯器甚至會爲你做這件事。還有一件事:你真的不應該使用一個名爲'Object'的類,因爲已經有一個名爲'Object'的Java類,這是令人困惑的。 –
@KevinWorkman我已更新帖子,現在包含可驗證的示例,而不是展示該問題。 –