`
yangshen998
  • 浏览: 1248500 次
文章分类
社区版块
存档分类
最新评论

Graphics的setClip与clipRect的区别

 
阅读更多

有区别的。

单独使用时效果看起来是一样的,但是如果进行重复使用就不同了。

对于clipRect,API是这么说的:Intersects the current clip with thespecified rectangle. The resulting clipping area is theintersection of the current clipping area and the specifiedrectangle. This method can only be used to make the current clipsmaller. To set the current clip larger, use thesetClip 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:20clipY:20

clipW:90clipH:90


总结如下:

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

clipRect() 用(之前的裁剪区域)和(新的矩形区域)的交集作为新的裁剪区

分享到:
评论

相关推荐

    java 用2d美化JButtonDemo.rar

    主要代码: @Override public void paint(Graphics g,... g2d.setClip(clip); g2d.setPaint(p1); g2d.drawRoundRect(0,0,w-1,h-1,20,20); g2d.setPaint(p2); g2d.drawRoundRect(1,1,w-3,h-3,18,18); }

    JButton 美化(圆角矩形立体效果加鼠标响应)Demo.rar

    g2d.setClip(clip); // 画一个黑色边框 paint = new GradientPaint(0,0,new Color(0,0,0), 0,btnHeight-1,new Color(100,100,100)); g2d.setPaint(paint); g2d.drawRoundRect(0,0,btnWidth-1,btnHeight-...

    java多种改变字体样式实例

    g2.setClip(shape); g2.setColor(Color.blue); g2.fill(shape.getBounds()); g2.setColor(Color.yellow); for (int j = shape.getBounds().y; j ().y + shape.getBounds().height; j=j+3) { Line2D line = new...

    j2meshouji

    public class WelcomForm extends Canvas implements CommandListener ... public void paint(Graphics g) { int Width=this.getWidth(); int height=this.getHeight(); g.setClip(0, y, Width, 5);

    j2me手机游戏代码

    "附身:召唤影子与主角并肩作战,使主角生命力和攻击力按比例上升。"+ "\n\n" + "回血:恢复主角一定比例的生命力。"+ "\n\n" + "回气:恢复主角一定比例的气力,但要耗费一定量的金钱。"; public SwordCanvas()...

    j2me动画结构.doc

    游戏开发教程:动画篇 Table of Contents 1 Introduction 4 1.1 Purpose and Scope 4 1.2 Abbreviation 4 1.3 Reference 4 2 游戏开发教程:2D动画...Figure 2 11 使用setClip和drawImage画Sprite,以Sprite 2为例 15 1

    PDFsharp-MigraDocFoundation-1_50-新.rar

    针对里面pdfsharp-gdi进行了部分修改,解决setclip无效的问题;另外针对之前不支持文字加粗的问题,此版本也得到了解决;针对一直存在的不支持宋体的问题,是因为pdfsharp只支持.ttf格式的字体,下载.tffs格式的宋体...

    【原创】新版发布 nge2 PSP新版类库

    9 支持各种图像裁剪,例如J2ME2的SetClip函数 二 如何编译: VC6 1 将include和lib设置成VC6的包含和库目录 2 将win32depends/include和win32depends/lib设置成VC6的包含和库目录 3 打开win32builds/libnge2....

Global site tag (gtag.js) - Google Analytics