pgraphics文本(工具提示)显示在图像顶部显示为乱码

hwamh0ep  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(229)

我已经挣扎了一段时间,没有找到解决办法。当用户将鼠标悬停在pgraphics对象中显示的特定标记上时,我尝试显示工具提示(一个不同颜色的矩形框,其中包含一些文本)。它是用java编程的,并使用papplet类作为javaapplet运行。
问题是,文本看不清楚,因为并非所有文本都停留在其他图像的顶部。尽管颜色已更改并保留为工具提示的颜色,但其他标记的边仍保持在顶部。
下面是代码的一部分,可以更好地解释我要做的事情:

// 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();
    }

有没有可能删除一些不显示的标记的边缘,或者以其他方式确保工具提示始终保持在顶部?提前谢谢

5kgi1eie

5kgi1eie1#

我已经找到解决办法了。您所要做的是仅在显示(绘制)其他图像/对象之后才显示(绘制)文本(工具提示)。这将使文本始终停留在停止和出现清晰没有问题。为此,必须在draw方法的主类中使用它,例如:

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

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

        }

    }

希望其他人觉得这个有用。

相关问题