2016-11-12 46 views
0

伙计这是我的代码。所用库的文档可在processing library docsunfolding 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); 
     } 
    } 
} 

现在我不明白幕后发生了什么。我已经考虑过并且从中理解了一些东西。我不知道我是否正确。请检查这是否真的发生。

据我 -

  1. 存在这样被我只要一运行程序,它负责显示画布和调用设置(称为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(); 
         } 
        } 
    
  2. 另外的伪代码(不是十分“精准”,只给一个要点什么感觉),用于监听事件是在单独的线程根据我应该是这样的:

    while(!windowClosed){ 
        Event event = listenForEvents(); 
        handleEvent(event); // this accounts for calling keyPressed(). 
    } 
    

这是思想的正确的方式或者这一切在主线程本身发生在while循环中,或者可能涉及2个以上的线程。我写了我的感受。任何建议/帮助将非常感激:)

回答