Quartz2D简单绘制之不规则形状

本文演示如何使用Core Graphics API在iOS应用中绘制不规则形状,包括设置颜色、宽度、填充和描边。

 

// 画不规则形状
CGContextRef contextRef = UIGraphicsGetCurrentContext();
CGContextSetRGBStrokeColor(contextRef, 1.0f, 1.0f, 1.0f, 1); // 填充时用不到
CGContextSetLineWidth(contextRef, 2.0f); // 填充时用不到
CGFloat components[] = { 1.0f, 0.0f, 0.0f, 1.0f};
CGContextSetFillColor(contextRef, components);
CGContextMoveToPoint(contextRef, 150.0f, 150.0f);
CGContextAddLineToPoint(contextRef, 200.0f, 170.0f);
CGContextAddLineToPoint(contextRef, 180.0f, 300.0f);
CGContextAddLineToPoint(contextRef, 80.0f, 300.0f);
CGContextClosePath(contextRef);
//CGContextFillPath(contextRef); // 填充
CGContextStrokePath(contextRef); // 不填充



Java绘制不规则几何图形,比如划曲线,写字,线条随意画,如截图所示,甚至可以写出文字:   不规则图形绘制代码:   public class IrregularShapeDemo extends JFrame {    GeneralPath gPath= new GeneralPath(); //GeneralPath对象实例   //构造函数   public IrregularShapeDemo() {    super("不规则图形绘制"); //调用父类构造函数    enableEvents(AWTEvent.MOUSE_EVENT_MASK|AWTEvent.MOUSE_MOTION_EVENT_MASK); //允许事件    setSize(300, 200); //设置窗口尺寸    setVisible(true); //设置窗口可视    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //关闭窗口时退出程序    }    public void paint(Graphics g) { //重载窗口组件的paint()方法    Graphics2D g2D = (Graphics2D)g; //获取图形环境    g2D.draw(gPath); //绘制路径    }    public static void main(String[] args) {    new IrregularShapeDemo();    }    protected void processMouseEvent(MouseEvent e) { //鼠标事件处理    if(e.getID() == MouseEvent.MOUSE_PRESSED) {    aPoint = e.getPoint(); //得到当前鼠标点    gPath = new GeneralPath(); //重新实例化GeneralPath对象    gPath.moveTo(aPoint.x,aPoint.y); //设置路径点    }    }    protected void processMouseMotionEvent(MouseEvent e) { //鼠标运动事件处理    if(e.getID() == MouseEvent.MOUSE_DRAGGED) {    aPoint = e.getPoint(); //得到当前鼠标点    gPath.lineTo(aPoint.x, aPoint.y); //设置路径    gPath.moveTo(aPoint.x, aPoint.y);    repaint(); //重绘组件    }    }   }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值