图形图像和多媒体.ppt_第1页
图形图像和多媒体.ppt_第2页
图形图像和多媒体.ppt_第3页
图形图像和多媒体.ppt_第4页
图形图像和多媒体.ppt_第5页
已阅读5页,还剩39页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

图形 图象与多媒体 本章重点设置字型设置颜色几何图形绘制方法图像显示技术基础 绘图基础 基本图形包括点 线 圆 矩形等 是构成复杂图形的基础 绘制基本图形要使用AWT中的Graphics类 它提供了各种基本图形的绘制方法 可以直接引用这些方法 确定平面坐标系 坐标原点 0 0 位于整个区域的左上角 一个坐标点对应屏幕上的一个像素 必须是整数 Graphics类的基本功能 java awt包中的Graphics提供建立字体 设定颜色 显示图像 文本 绘制和填充各种图形的功能 Graphics2D类继承Graphics提供更多的状态和属性 使应用程序能绘制出更加丰富多彩的图形 要在某个组件中绘图 需要在这个组件所属的类中重写paint 方法 在该方法中绘图 如果要在JComponent子类的组件中绘图 则需重写paintComponent 方法 自型和颜色 绘制文本的方法有三种 在指定的位置绘制字符串drawString Stringstr intx inty g drawString 中国Wxyz 10 60 10 60 字型和颜色 文字字型有三个要素 字体 name 风格 style 字号 size 字体 宋体 黑体 TimesNewRoman等风格 Font PLAIN 正常 Font BOLD 粗体 Font ITALIC 斜体 Font BOLD Font ITALIC字号 整数 单位是磅Java中Font类的对象代表字体Fontfont newFont 宋体 Font PLAIN 12 绘图时使用Graphics对象的setFont方法设置字体 字型和颜色 Java中使用Color类设置颜色 生成颜色的方法有两种 使用预定义的颜色 Color RED Color YELLOW等通过红绿蓝 RGB 的值合成颜色 例如使用3个0 255的整数创建对象Colorc newColor 255 0 0 绘图时使用Graphics对象的setColor 方法设置颜色 使用Component对象的setBackground 方法设置背景色 绘图模式 绘图模式指绘制的图形与之前绘制的图形有重叠时 重叠部分的颜色如何确定 正常模式 后绘制的图形覆盖原先的图形 setPaintMode 异或模式 将正要绘图的颜色 原先图形的颜色和设定的颜色做特定的运算 得到实际的绘图颜色 setXORMode g setColor Color BLUE g drawString dsbdsfsfs 20 50 g setColor Color YELLOW g fillRect 10 10 100 50 g setColor Color BLUE g drawString dsbdsfsfs 20 50 g setColor Color YELLOW g fillRect 10 10 100 50 XOR绘图模式 setBackground Color yellow 设此颜色为Bg setXORMode Color red 设此颜色为Cg setColor Color green 设此颜色为D规则1 用背景色画图出现设置的颜色CB B CsetBackground Color yellow g setXORMode Color red g setColor Color YELLOW g fillRect 20 20 80 40 红色 XOR绘图模式 规则2 一个图形重画时会清除原有图形D D BsetBackground Color yellow g setXORMode Color red g setColor Color BLUE g fillRect 20 20 80 40 g fillRect 20 20 80 40 清除 XOR绘图模式 规则3 背景色和绘图颜色不一样时 为两者的混合色B D B和D的混合色setBackground Color yellow g setXORMode Color red g setColor Color BLUE g fillRect 20 20 80 40 黄 蓝 XOR绘图模式 规则4 某区已经用D着色 再用E着色D E B和E的混合色 B和E不同 setBackground Color yellow g setXORMode Color red g setColor Color BLUE g fillRect 20 20 80 40 g setColor Color GREEN g fillRect 20 20 40 40 黄 蓝 绿 Graphics的绘图方法 画线段drawLine intx1 inty1 intx2 inty2 普通矩形drawRect intx inty intwidth intheight 用线框围起来的矩形fillRect intx inty intwidth intheight 填充矩形圆角矩形drawRoundRect intx inty intwidth intheight intarcW intarcH 用线框围起来的圆角矩形fillRoundRect intx inty intwidth intheight intarcW intarcH 填充圆角矩形如果后四个参数相等 画出来的是圆形 Graphics的绘图方法 三维矩形draw3DRect intx inty intwidth intheight booleanraised fill3DRect intx inty intwidth intheight booleanraised raised为true表示突出的 false是凹陷的画椭圆drawOval intx inty intwidth intheight fillOval intx inty intwidth intheight 画以 x y 为原点 即矩形的左上角 宽为width 高为height的矩形的内切椭圆 Graphics的绘图方法 画圆弧drawArc intx inty intwidth intheight intstartAngle intarcAngle fillArc intx inty intwidth intheight intstartAngle intarcAngle 得到的弧由startAngle开始 并以当前颜色转arcAngle度 角度的0度位于3点钟位置 正值指示逆时针旋转 负值则指示顺时针旋转 g drawRect 5 5 100 50 g drawArc 5 5 100 50 0 360 g drawRect 5 5 100 50 g drawArc 5 5 100 50 0 270 g drawRect 5 5 100 50 g drawArc 5 5 100 50 90 90 Graphics的绘图方法 画多边形drawPolygon int xPoints int yPoints intnPoints fillPolygon int xPoints int yPoints intnPoints int px1 50 90 10 50 int py1 10 50 50 10 g fillPolygon px1 py1 4 50 10 90 50 10 50 Graphics的绘图方法 使用Polygon类创建多边形 Polygonp newPolygon p addPoint 50 10 p addPoint 90 50 p addPoint 10 50 p addPoint 50 10 g fillPolygon p 50 10 90 50 10 50 Graphics的绘图方法 擦除矩形块用背景色填充一个矩形块 相当于擦除矩形块位置的图形clearRect intx inty intwidth intheight 50 10 90 50 10 50 int px1 50 90 10 50 int py1 10 50 50 10 g fillPolygon px1 py1 4 g clearRect 35 30 15 15 clipRect 限定作图显示区域 区域内的图形可以显示clipRect intx inty intwidth intheight g setColor Color YELLOW g fillRect 10 10 100 100 g clipRect 20 20 50 50 g setColor Color YELLOW g fillRect 10 10 100 100 复制图形 copyArea intx inty intwidth intheight intdx intdy x 源矩形的x坐标 y 源矩形的y坐标 width 源矩形的宽度 height 源矩形的高度 dx 水平偏移 右为正 左为负dy 垂直偏移 下为正 上为负 g setColor Color YELLOW g fillRect 0 0 100 100 g setColor Color red g drawRect 30 30 50 50 g copyArea 30 30 50 50 100 100 paint repaint update方法 paint 应该绘制组件的内容时调用此方法 组件第一次在屏幕上显示 组件的大小改变了 部件显示的内容受损需要维护 比如 先前挡住组件的其它物体移走了 于是组件被挡住的部分曝露出来 repaint 对于重量级组件 JFrame JApplet JDialog和awt组件 先调用update方法 在update方法中实现部分图形的修改 然调用paint方法 对于轻量级 JComponent的子孙 组件调用paint方法 Graphics2D类的绘图方法 java awt Graphics2D拥有更强大二维的图形处理能力 提供坐标变换 颜色管理以及文字布局等更精确的控制 publicabstractclassGraphics2DextendsGraphics Stroke属性 Stroke属性控制线条的宽度 笔形的样式 线段连接方式或短划线图案 该属性的设置需要创建BasicStroke对象 再调用setStroke 方法来设置 cap样式 join样式 publicvoidpaint Graphicsg Graphics2Dg2d Graphics2D g Line2Dline newLine2D Double 30 0 10 0 180 0 10 0 g2d draw line BasicStrokebs newBasicStroke 20f BasicStroke CAP BUTT BasicStroke JOIN ROUND g2d setStroke bs Line2Dline2 newLine2D Double 30 0 40 0 180 0 40 0 g2d draw line2 bs newBasicStroke 20f BasicStroke CAP SQUARE BasicStroke JOIN ROUND g2d setStroke bs Line2Dline3 newLine2D Double 30 0 80 0 180 0 80 0 g2d draw line3 bs newBasicStroke 20f BasicStroke CAP ROUND BasicStroke JOIN ROUND g2d setStroke bs Line2Dline4 newLine2D Double 30 0 120 0 180 0 120 0 g2d draw line4 publicvoidpaint Graphicsg bs newBasicStroke 20f BasicStroke CAP BUTT BasicStroke JOIN ROUND g2d setStroke bs int x 30 160 200 int y 160 400 200 g2d setColor Color GREEN g2d drawPolygon x y 3 JOIN ROUND JOIN MITER JOIN BEVEL paint属性 paint属性控制填充效果 BasicStrokebs newBasicStroke 5 5f g2d setStroke bs GradientPaintgp newGradientPaint 30 0f 30 0f Color RED 180 0f 30 0f Color GREEN g2d setPaint gp Line2Dline newLine2D Double 30 0 30 0 180 0 30 0 g2d draw line transform属性 Transform属性用来实现常用图形的平移 缩放 旋转和斜切变换等操作 旋转变换 AffineTransformrotate AffineTransform getRotateInstance Math PI 2 30 0 30 0 g2d setTransform rotate Line2Dline newLine2D Double 30 0 30 0 180 0 30 0 g2d draw line AffineTransformrotate newAffineTransform rotate rotate Math PI 2 30 0 150 0 g2d setTransform rotate Line2Dline newLine2D Double 30 0 30 0 180 0 30 0 g2d draw line transform属性 缩放 AffineTransformscale AffineTransform getScaleInstance 2 2 g2d setTransform scale Line2Dline newLine2D Double 30 0 30 0 180 0 30 0 g2d draw line transform属性 平移 AffineTransformtransform AffineTransform getTranslateInstance 10 50 g2d setTransform transform Line2Dline newLine2D Double 30 0 30 0 180 0 30 0 g2d draw line transform属性 斜切 AffineTransformshear AffineTransform getShearInstance 5 2 g2d setTransform shear Line2Dline newLine2D Double 30 0 30 0 180 0 30 0 g2d draw line clipe属性 clipe属性用于实现剪裁的效果 区域内可见Line2Dline newLine2D Double 30 0 30 0 180 0 30 0 g2d setClip 30 30 160 30 g2d draw line Composit属性 Composit设置图形重叠区的效果AlphaCompositea AlphaComposite getInstance intrule floatalpha Alpha为0表示透明 1 0表示不透明 Graphics2D的绘图方法 Graphics2D类保留了Graphics类的绘图方法 同时增加了一些新方法 新方法把几何形状作为对象来绘制 在java awt geom包中声明的一系列类 分别用于创建各种几何图形 publicvoidpaint Graphicsg 将参数g强制类型转换成Graphics2D类型Graphics2Dg2d Graphics2D g 使用图形类的静态Double方法创建几何图形对象Line2Dline newLine2D Double 30 0 30 0 180 0 30 0 以图形对象为参数 调用Graphics2D对象的draw方法绘制图形g2d draw line Graphics2D的几何图形类 线段起点 30 30 终点 180 30 Line2Dline newLine2D Double 30 30 180 30 g2d draw line publicabstractclassLine2DimplementsShape Cloneable publicstaticclassDoubleextendsLine2D publicDouble doubleX1 doubleY1 doubleX2 doubleY2 Graphics2D的几何图形类 矩形左上角点坐标 20 20 宽100 高50Rectangle2Drect newRectangle2D Double 20 20 100 50 g2d draw rect g2d fill rect Graphics2D的几何图形类 圆角矩形左上角点坐标 20 20 宽100 高50 圆角横向直径20 纵向直径40RoundRectangle2DroundRect newRoundRectangle2D Double 20 20 100 50 20 40 g2d draw roundRect Graphics2D的几何图形类 椭圆左上角点 20 30 宽100 高50矩形的内切椭圆Ellipse2Dellipse newEllipse2D Double 20 30 100 50 g2d draw ellipse Graphics2D的几何图形类 圆弧左上角坐标 20 30 宽100 高50矩形的内切圆弧 90度开始 逆时针转90度Arc2Darc newArc2D Double 20 30 100 50 90 90 Arc2D OPEN g2d draw arc Arc2Darc2 newArc2D Double 20 30 100 50 0 90 Arc2D CHORD g2d draw arc2 Arc2Darc3 newArc2D Double 20 30 100 50 0 90 Arc2D PIE g2d draw arc3 二次曲线 一条二次曲线需要三个点确定 始点 控制点和终点起点 20 10 控制点 90 65 终点 55 115 QuadCurve2Dqc newQuadCurve2D Double 20 10 90 65 55 115 QuadCurve2Dqc2 newQuadCurve2D Double 20 10 15 63 55 115 QuadCurve2Dqc3 newQuadCurve2D Double 20 10 54 64 55 115 g2d draw qc g2d

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论