PGraphics 文本(工具提示)显示在图像顶部出现乱码

PGraphics text (tooltip) display appears garbled on top of images

我已经为此苦苦挣扎了一段时间,但没有找到解决方案。当用户将鼠标悬停在 PGraphics 对象旁边显示的特定标记上时,我试图显示工具提示(一个包含一些文本的不同颜色的矩形框)。它使用 PApplet class 在 java 和 运行 中编程为 java 小程序。

问题是,文本看不清楚,因为并非所有文本都位于其他图像之上。尽管颜色已更改并保留为工具提示的颜色,但其他标记的边缘仍保留在顶部。

这里是代码的一部分,可以更好地解释我正在尝试做的事情:

// Common piece of drawing method for markers; 
    // Note that you should implement this by making calls 
    // drawMarker and showTitle, which are abstract methods 
    // implemented in subclasses
    public void draw(PGraphics pg, float x, float y) {
        // For starter code just drawMaker(...)
        if (!hidden) {

            drawMarker(pg, x, y);

            if (selected) {
                showTitle(pg, x, y);  // You will implement this in the subclasses
            }

        }
    }

@Override
    public void drawMarker(PGraphics pg, float x, float y) {
        // TODO Auto-generated method stub
        pg.pushStyle();

        // IMPLEMENT: drawing triangle for each city
        pg.fill(150, 30, 30);
        pg.triangle(x, y-TRI_SIZE, x-TRI_SIZE, y+TRI_SIZE, x+TRI_SIZE, y+TRI_SIZE);

        // Restore previous drawing style
        pg.popStyle();
    }

public void showTitle(PGraphics pg, float x, float y)
    {
        // TODO: Implement this method
        pg.pushStyle();

        pg.fill(255, 255, 202);
        pg.rect(x+TRI_SIZE+1, y-TRI_SIZE, 150, 15);
        pg.textAlign(PConstants.LEFT, PConstants.CENTER);
        pg.fill(0,0,0);
        pg.text(getCity()+", " + getCountry() + ", " + getPopulation(), x+TRI_SIZE+2, y);

        // Restore previous drawing style
        pg.popStyle();
    }

是否可以删除某些标记的边缘以使其不显示或以其他方式确保工具提示始终保持在顶部?提前致谢

嗯,我已经找到了解决办法。您需要做的是仅在每隔 image/object 显示(绘制)后才显示(绘制)文本(工具提示)。这将使文本始终停留在停止状态并且清晰显示没有问题。为此,您必须在绘制方法的主要 class 中使用它,如示例所示:

public void draw() {
        background(0);
        map.draw();
        addKey();

        if (lastSelected != null)
        {
            lastSelected.showToolTip(this,lastSelected.getScreenPosition(map).x,lastSelected.getScreenPosition(map).y);

        }

    } 

希望其他人觉得这有用。