2012-10-28 156 views
0

我试图实现某种弹出窗口来显示作者在特定国家的信息,我设法做到这一点。我加载我的交互式地图,当我点击国家时,我会看到一个弹出消息。然而,当我释放按钮时,弹出消失。处理按钮事件

我想弹出绘制留在那里,直到我右键单击它应该消失。

所以我遇到问题实现它,以便在mouseReleased()方法中显示的弹出窗口保持绘制状态。

// Import libaries 
import org.gicentre.geomap.io.*; 
import org.gicentre.geomap.*; 

// Instance new GeoMap 
GeoMap geoMap; 
PImage bgImage; 

void setup() 
{ 
    size(1280, 768); 

    // Load background pattern img and implement AA for shapes 
    bgImage = loadImage("bg.jpg"); 
    smooth(); 

    // New GeoMap instance 
    geoMap = new GeoMap(this); 

    // Load map shape 
    geoMap.readFile("world"); 
} 

void draw() 
{ 
    // Fill ocean with bg image 
    background(bgImage); 

    // Change stroke color 
    stroke(255, 255, 255); 

    // Fill the continents color and draw the map 
    fill(26, 117, 181); 
    geoMap.draw(); 

    // Get country id by mouse location 
    int countryID = geoMap.getID(mouseX, mouseY); 

    // Offgrid: -1 
    if (countryID != -1) 
    { 
    // Listen for mouse event and trigger action 
    mouseReleased(countryID); 
    // Color the select country 
    fill(14, 96, 166); 
    geoMap.draw(countryID); 
    } 
} 

void mouseReleased(int countryID) 
{ 
    // Act on left clicks 
    if(mouseButton == LEFT) 
    { 
    println(getCountryName(countryID, 3)); 

    noStroke(); 
    fill(255, 192); 
    rect(0, 0, width, 20); 

    if (countryID != -1) 
    { 
     String name = getCountryName(countryID, 3); 
     fill(0); 
     textAlign(LEFT, CENTER); 
     text(name, 0, 0, width, 20); 
    } 

    } 
} 


// Returns country name by id 
String getCountryName(int id, int offset) 
{ 
    // Get country name for query 
    // getString(int id, int offset), int id: country id, int offset: 
    // Example USA 
    // offset 0 = Country Code -> 257 
    // offset 1 = Country Short Tag -> US 
    // offset 2 = Country Short Name -> USA 
    // offset 3 = Official Name -> United States 
    String name = geoMap.getAttributes().getString(id, offset); 

    return name; 
} 

回答

0

问题是您的绘图函数会不断重复,但您只是在按下lmb时才绘图。一旦它被释放,下一次绘制调用将绘制您的弹出窗口。

解决这个问题的一种方法是存储一个变量(比如说“shouldDraw”)来告诉你是否绘制或不绘制。当按下lmb时,您可以将其设置为true,并在按下人民币时,将其设置为false。然后,而不是检查LEFT,你会检查shouldDraw。

我不熟悉你使用的框架,但可能有一个onClick方法或类似的,这将是一个比绘制更适合你的shouldDraw变量更合适的地方。

这将使标签更改,如果您单击然后移动鼠标。如果您希望保留相同的文本,则可以存储一个字符串,并在没有任何内容绘制时将其设置为空。

0

有一个内置函数叫做mouseReleased,它在Processing中没有参数。你故意超载吗? 任何方式这是有点混乱。我会做一些这样的事:

(伪)

void setup() { 
// stuff 
} 

    void draw() 
{ 

if (id != -1) 
    drawPopup(id); 

} 

void mouseReleased() //default method called when mouse is released 
{ 
if (mouseButton == LEFT) 
id = geoMap.getID(mouseX, mouseY); 
} 

void drawPopup(int id){ 
println(getCountryName(countryID, 3)); 

noStroke(); 
fill(255, 192); 
rect(0, 0, width, 20); 

if (countryID != -1) 
{ 
    String name = getCountryName(id, 3); 
    fill(0); 
    textAlign(LEFT, CENTER); 
    text(name, 0, 0, width, 20); 
} 

} 这样,总会有第一后弹出,或者你需要的ID重置为-1适当的条件下或添加由mdgeorge建议的布尔值。但通常最好不要在鼠标功能内部画东西。事情变得更容易控制。 编辑:我应该指出,也有内置void mousePressed() and void mouseClicked()和更多检查参考。