代码之家  ›  专栏  ›  技术社区  ›  L. Cornelius Dol

有没有办法在Java中画一个“倒装”的剪辑区域?

  •  3
  • L. Cornelius Dol  · 技术社区  · 16 年前

    我想用graphics.fillRoundRect()填充一个区域,但我想在它中间有一个矩形 被填满。

    本质上,给定一个100x30的组件,我想将剪裁设置为10,10的80x10的矩形,但仅填充该区域 外部 那个80x10的矩形。原因是我想要一个n像素的边界,在不影响内部组件区域的情况下绘制一个曲线轮廓。

    到目前为止,我能看到的最好的方法是剪辑到10,10 90x10并执行FillRoundRect(),然后剪辑到90,10 10x10并执行FillRect()以填充右手边、角落下方和上方。

    如果我只是简单地重新绘制一个单行矩形,那么我会在拐角处出现“斑点”,因为曲线并不完全相邻(和/或因为a a会影响周围的像素)。

    编辑:警告-我需要一种方法来完成它,它将与J2ME AWT(个人概要1.1的CDC)以及J2SE一起工作。


    编辑:另一个 similar question 有一个 an answer 我能适应。对于我的情况,正确工作的代码是作为一个自我应答发布的。

    2 回复  |  直到 16 年前
        1
  •  2
  •   Community Mohan Dere    9 年前

    我有一个 similar answer 另一个问题也是,使用多边形作为awt剪辑。也许这在J2ME中得到了支持?您需要知道要排除的矩形的边界,以及绘图区域的外部边界。

    +-------------------+
    | clip drawing area |
    +---+-----------+   |
    |   | excluded  |   |
    |   |   area    |   |
    |   +-----------+   |
    |                   |
    +-------------------+
    

    从OP编辑。

    这个答案对我有效,并且J2ME支持API。另一个问题的答案似乎有一个错误——坐标集需要从左上和内上的点开始,以便创建一个封闭的多边形。我的最终代码如下:

    为了创建剪切形状,我使用了以下方法:

    static public Shape getOutsideEdge(Graphics gc, Rectangle bb, int top, int lft, int btm, int rgt) {
        int                                 ot=bb.y            , it=(ot+top);
        int                                 ol=bb.x            , il=(ol+lft);
        int                                 ob=(bb.y+bb.height), ib=(ob-btm);
        int                                 or=(bb.x+bb.width ), ir=(or-rgt);
    
        return new Polygon(
         new int[]{ ol, ol, or, or, ol, ol,   il, ir, ir, il, il },
         new int[]{ it, ot, ot, ob, ob, it,   it, it, ib, ib, it },
         11
         );
        }
    

    我将其设置为图形上下文,然后填充矩形:

    Rectangle tmp=new Rectangle(px,py,pw,ph);
    gc.setClip(getOutsideEdge(gc,tmp,thickness,thickness,thickness,thickness));
    gc.fillRoundRect(px,py,pw,ph,RADIUS,RADIUS);
    

    然后我在每个角落画了一个点,创造了一个圆角的错觉:

    gc.setClip(px,py,pw,ph);
    gc.drawLine((px   +thickness  ),(py   +thickness  ),(px   +thickness  ),(py   +thickness  ));
    gc.drawLine((px+pw-thickness-1),(py   +thickness  ),(px+pw-thickness-1),(py   +thickness  ));
    gc.drawLine((px   +thickness  ),(py+ph-thickness-1),(px   +thickness  ),(py+ph-thickness-1));
    gc.drawLine((px+pw-thickness-1),(py+ph-thickness-1),(px+pw-thickness-1),(py+ph-thickness-1));
    
        2
  •  1
  •   Community Mohan Dere    9 年前

    请查一下我的答案 this 问题。它非常相似。

    编辑:您可能想检查Alphacomposite是否在J2ME中可用。在爪哇,你可以通过改变Alpha合成模式(我不记得我到底认为它的SRCIN)和通过在黑白区域上绘制图像来模拟剪辑。你可能想看看。

    推荐文章