`

ClipRect和setClip的区别

阅读更多
[转]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() 用(之前的裁剪区域)和(新的矩形区域)的交集作为新的裁剪区
分享到:
评论

相关推荐

    开发中j2me与android的对比

    设置剪切区域,J2ME的setClip在Android中对应的API是clipRect,并且Android还提供了Region.Op参数。至于旋转,J2ME通常使用drawRegion或drawImage配合旋转角度,而Android可以创建一个新的Bitmap进行翻转后再绘制,...

    Java多媒体技术.ppt

    如果需要进行裁剪,可以利用`setClip()`或`clipRect()`方法设置显示区域。 在显示图像时,`drawImage()`方法会立即返回,因此如果图像尚未完全加载,可能会看到不完整的图像或者空白。为了确保图像完全加载,可以...

    c#绘制图形及其简单操作

    g.SetClip(clipRect); // 绘制直线 Pen linePen = new Pen(Color.Blue, 2); Point p1 = new Point(50, 50); Point p2 = new Point(300, 300); g.DrawLine(linePen, p1, p2); // 恢复默认裁剪 g.ResetClip(); ``` ...

    Java程序设计实践教程PPT教案学习.pptx

    坐标系统是相对于Graphics对象的转换原点,所有呈现操作只影响当前剪贴区内的像素,剪贴区可以通过`setClip`或`clipRect`方法调整。然而,`Graphics`对象也有一些限制,比如不能绘制复杂图形、虚线、宽度可变的线条...

    开发中j2me与android的比较.pdf

    综上所述,虽然J2ME和Android在许多方面有相似之处,但它们之间的区别也很明显。J2ME更侧重于轻量级应用的开发,而Android则提供了更为丰富的特性和更高的性能。随着技术的发展,Android已成为主流的移动开发平台之...

Global site tag (gtag.js) - Google Analytics