ClipRect和setClip的区别

本文详细解析了Java中Graphics类的clipRect与setClip方法的区别及应用。通过具体实例展示了如何利用这两种方法来精确控制图形的裁剪区域,进而实现复杂的图形绘制效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[转]http://www.j2medev.com/bbs/dv_rss.asp?s=xhtml&boardid=11&id=4811&page=8

对于clipRect,API是这么说的:Intersects the current clip with the specified rectangle. The resulting clipping area is the intersection of the current clipping area and the specified rectangle. This method can only be used to make the current clip smaller. To set the current clip larger, use the setClip method.

大概意思是:当前裁剪与指定的矩形相交,新的裁剪区是当前裁剪区和指定矩形的交集,clipRect只能使当前裁剪区域更小,如果希望增大裁剪区域,则使用setClip.

用代码说明一下:

private void Test(Graphics g) {

//clipRect()
g.setColor(0xff0000);
g.clipRect(10, 10, 100, 100);
g.clipRect(20, 20, 100, 100);
g.fillRect(0, 0, getWidth(), getHeight());

//绘制两次设定的矩形边框
g.setClip(0, 0, getWidth(), getHeight());
g.setColor(0x0000ff);
g.drawRect(10, 10, 100, 100);
g.drawRect(20, 20, 100, 100);

//求出裁剪区域坐标和大小
clipX = g.getClipX();
clipY = g.getClipY();
clipW = g.getClipWidth();
clipH = g.getClipHeight();
print();
}

public void print(){
System.out.println("clipX:"+clipX+" clipY:"+clipY);
System.out.println("clipW:"+clipW+" clipH:"+clipH);
}

结果:

clipX:20 clipY:20

clipW:90 clipH:90

显示如下:


此主题相关图片如下001.jpg:



很容易看出,填充的是两个矩形的交集。

总结如下:

setClip() 清除之前的裁剪区,重新设定裁剪区

clipRect() 用(之前的裁剪区域)和(新的矩形区域)的交集作为新的裁剪区
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值