我正在尝试实现一种弹出窗口来显示有关给定国家/地区的作者的信息。我加载了我的交互式地图,当我点击该国家时,我收到一条弹出消息。然而,当我释放按钮时,弹出窗口消失。
我想弹出那个留在那里直到我右键单击它会消失的弹出窗口。
所以我在实现它时遇到问题,以便在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 :(得分:0)
问题是你的绘图功能会连续重复,但你只是在按下lmb时绘图。一旦它被释放,下一次绘制调用将绘制你的弹出窗口。
解决这个问题的一种方法是存储一个变量(比如说“shouldDraw”)来告诉你是否要绘制。按下lmb时,您可以将其设置为true,当按下rmb时,您将其设置为false。然后检查shouldDraw。
而不是检查LEFT我不熟悉你正在使用的框架,但是可能有一个onClick方法或者类似的方法,这个方法比改变你应该绘制变量的绘图更合适。
如果单击然后移动鼠标,则会更改标签。如果你想要保留相同的文本,你可以改为存储一个String,并在没有任何东西可以绘制时将其设置为null。
答案 1 :(得分: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(),还有一些内容检查参考。