使用GlassPane在Java中的内部透明选择窗口 [英] Inner-Transparent Selection Window in Java using GlassPane

查看:156
本文介绍了使用GlassPane在Java中的内部透明选择窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力实现以下目标

I am trying to achieve the following

http://www.qksnap.com/i/3hunq/4ld0v/screenshot.png

我目前能够画画使用以下代码在半透明玻璃板背景上成功建立矩形:

I am currently able to draw rectangles successfully on a semi-transparent glasspane background using the following code:

    protected void paintComponent(Graphics g) {
          Graphics2D g2 = (Graphics2D) g;
          g.setColor(Color.black); // black background
          g.fillRect(0, 0, frame.getWidth(), frame.getHeight());
          g2.setColor(Color.GREEN.darker());
          if (getRect() != null && isDrawing()) {
            g2.draw(getRect()); // draw our rectangle (simple Rectangle class)
          }
         g2.dispose();
}

哪个效果很好,但是,我希望在矩形内有区域完全透明,而外面仍然像上面的截图一样变暗。

Which works great, however, I would love to have the area within the rectangle be completely transparent while the outside was still darken much like the screenshot above.

任何想法?

推荐答案


..矩形内的区域是完全透明的,而外部仍然像上面的截图一样变暗。

..have the area within the rectangle be completely transparent while the outside was still darken much like the screenshot above.




  • 创建矩形 componentRect )这是要绘制的组件的大小。

  • 创建区域 componentArea )该形状(新区域(componentRect))。

  • 创建区域 selectionRectangle 的c $ c>( selectionArea )。

  • 致电 componentArea.subtract(selectionArea)删除所选部分。

  • 调用 Graphics.setClip(componentArea)

  • 绘制半透明颜色。

  • (如果需要更多绘画操作,请清除剪裁区域。)

    • Create a Rectangle (componentRect) that is the size of the component being painted.
    • Create an Area (componentArea) of that shape (new Area(componentRect)).
    • Create an Area (selectionArea) of the selectionRectangle.
    • Call componentArea.subtract(selectionArea) to remove the selected part.
    • Call Graphics.setClip(componentArea)
    • Paint the semi-transparent color.
    • (Clear the clipping area if more paint operations are required).
    • 这篇关于使用GlassPane在Java中的内部透明选择窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆