gpt4 book ai didi

java - 有什么方法可以让 "inverted"剪辑区域用于 Java 绘画?

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:04:44 25 4
gpt4 key购买 nike

我想使用 Graphics.fillRoundRect() 填充一个区域,但我希望它中间的一个矩形被填充。

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

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

如果我简单地重新绘制一个单线矩形,那么我最终会在角上“发现”,因为曲线并没有完全邻接(和/或因为 AA 影响周围的像素)。

编辑:警告 - 我需要一种方法来完成它,它可以与 J2ME AWT(CDC with Personal Profile 1.1)以及 J2SE 一起工作。


编辑:另一个similar question有一个 an answer我能够适应。适合我的情况的代码作为 self 回答发布。

最佳答案

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

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

EDIT FROM OP.

This answer worked for me and the API's are supported on J2ME. The answer of the other question appears to have one mistake - the set of coordinates needs to start a the point on the outer-left and inner top in order to create an enclosed polygon. My final code which worked follows:

To create a clipping Shape, I used this method:

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));

关于java - 有什么方法可以让 "inverted"剪辑区域用于 Java 绘画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1273688/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com