1

我避免了标题中的位图一词,因为在这种情况下,位图通常(?)指的是来自底层图像的位图。

我有一个图像被分割成许多不同的区域。对于每个区域,我都有一张由 1 和 0 组成的地图(位图),其中 1 代表区域内,零代表区域外。并非图像的每个部分都被一个区域覆盖,并且这些区域可能会重叠。图像尺寸为 (480x360)。

我想做的是当你用鼠标悬停该区域时用透明的红色覆盖图像。我的问题是我当前的方法非常慢,覆盖出现之前需要一两秒钟。

我目前的方法是在我的 ImagePanel 上使用 JLayer(绘制 BufferedImage 的 JPanel 的扩展)。然后我的 LayerUI 实例在鼠标移动时绘制叠加层:

public class ImageHighlightLayerUI extends LayerUI<JPanel> {
    private boolean mouseActive;
    private Point mousePoint;
    private byte[][][] masks;

    public void paint(Graphics g, JComponent c) {
        super.paint(g, c);

        if (mouseActive) {
            byte[][] curMask = null;

            // Find which region the mouse intersect
            for (int i = 0; i < masks.length; i++) {
                if (masks[i][mousePoint.x][mousePoint.y] == 1) {
                    curMask = masks[i];
                    break;
                }
            }

            // Outside region --> don't draw overlay
            if (curMask == null) return;

            //Transparent red
            g.setColor(new Color((float)1.0, 
                            (float)0.0, (float)0.0, (float)0.8));

            //Draw the mask
            for(int x = 0; x < curMask.length; x++) 
                for(int y = 0; y < curMask[y].length; y++) 
                    if (curMask[x][y] == 1) 
                       g.fillRect(x, y, 1, 1);

        }
    }
}

那么,我怎样才能提高效率呢?我愿意接受使用 JLayer 以外的其他方式的建议。我可以通过某种摆动方法以某种“魔术”方式使用我的位图吗?我可以将它与 BufferedImage 中的底层位图混合吗?消除透明度是唯一能帮助我的事情吗?(这是我想保留的东西)

另外两个与问题不一定相关但我尚未解决的问题:

  • 每次鼠标移动时都会重新绘制叠加层。这似乎是一种资源浪费。
  • 当区域重叠时,如何选择绘制哪一个?
4

0 回答 0