夥計這是我的代碼。所用庫的文檔可在processing library docs和unfolding maps library docs找到。它在畫布中顯示Google地圖。 setup()方法被調用一次,然後重複調用draw()方法。只要按下鍵盤上的w按鈕,當keyPressed()被調用時,背景將變爲白色。是否有涉及事件處理的單獨線程?
import processing.core.*;
import de.fhpotsdam.unfolding.*;
import de.fhpotsdam.unfolding.providers.Google;
public class MyDefaultEventExample extends PApplet {
private UnfoldingMap map;
public void setup(){
size (800, 600, OPENGL);
map = new UnfoldingMap (this, 50, 50, 700, 500, new Google.GoogleMapProvider());
}
public void draw(){
map.draw();
}
public void keyPressed(){
if(key == 'w'){
background (255, 255, 255);
}
}
}
現在我不明白幕後發生了什麼。我已經考慮過並且從中理解了一些東西。我不知道我是否正確。請檢查這是否真的發生。
據我 -
存在這樣被我只要一運行程序,它負責顯示畫布和調用設置(稱爲PApplet類中的主要方法)和繪製( ) 方法。僞代碼(不是十分「精準」,只是給的什麼,我覺得一個要點),它可以用來形容這是
public static void main(){ PApplet myApplet = new PApplet(); myApplet.displayCanvas(); //displays a default canvas with a default background lets say gray. myapplet.setup();// calls the setup method. /* code for creating and calling a separate thread to handle events. */ // repeatedly calls the draw method until the user closes the canvas window and closes the program while(!windowClosed){ myApplet.draw(); } }
另外的僞代碼(不是十分「精準」,只給一個要點什麼感覺),用於監聽事件是在單獨的線程根據我應該是這樣的:
while(!windowClosed){ Event event = listenForEvents(); handleEvent(event); // this accounts for calling keyPressed(). }
這是思想的正確的方式或者這一切在主線程本身發生在while循環中,或者可能涉及2個以上的線程。我寫了我的感受。任何建議/幫助將非常感激:)