




已阅读5页,还剩49页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
第9章Java绘图 本章要点 1 Graphics类2 Graphics2D类3 Color类4 Font类5 图像的处理 9 1Graphics类9 1 1绘制直线 graphics类绘制直线的方法是drawLine 调用格式为 drawLine intx1 inty1 intx2 inty2 作用 自 x1 y1 点开始至 x2 y2 点结束绘制一条直线 方法中参数解释 X1 y1 x2 y2分别为所绘直线的起始点坐标 x1 y1 和终止点坐标 x2 y2 举例如下 例9 1drawLine 2 3 2 7 此语句的作用是绘制出一条起始点坐标为 2 3 和终止点坐标为 2 7 的直线 例9 2importjava applet importjava awt publicclassDraw lineextendsApplet publicvoidpaint Graphicsg g setColor Color red g drawLine 2 4 12 400 g drawLine 40 80 100 200 g drawLine 0 0 500 500 9 1 2绘制矩形 graphics类绘制矩形的方法有四个 它们分别为drawRect fillRect drawRoundRect fillRoundRect 其各自的使用格式和作用分述如下 1 drawRect 调用格式 drawRect intx inty intwidth intheight 作用 以 x y 为矩形的左上角顶点 绘制一个宽 高分别为width和height的矩形 方法中的参数解释如下 x y 所绘制矩形左上角坐标 Width height 所绘制矩形的宽和高 该方法的使用举例如下 例9 3importjava awt Graphics importjava applet Applet importjava awt Color classdrawR protectedintx y width height drawR inta intb intc intd x a y b width c height d publicclassdrawRectextendsApplet drawRt publicvoidpaint Graphicsg t newdrawR 35 35 50 100 g setColor Color red g drawRect t x t y t width t height 该例绘制出一个左上角坐标为 35 35 宽为50 高为100的矩形 运行效果如图 2 fillRect 调用格式为 fillRect intx inty intwidth intheight 作用 以 x y 为矩形的左上角顶点 绘制一个宽 高分别为width和height有填充色的实心矩形 方法中的参数解释如下 x y 所绘矩形左上角坐标 Width height 所绘矩形的宽和高 与drawRect 的区别 所绘制的矩形中可有填充色 该方法的使用举例如下 例9 4importjava awt Graphics importjava applet Applet importjava awt Color classdrawR protectedintx y width height drawR inta intb intc intd x a y b width c height d publicclassshowdrawRextendsApplet drawRt publicvoidpaint Graphicsg t newdrawR 35 35 50 100 g setColor Color red g fillRect t x t y t width t height 运行效果如下 3 drawroundRect 调用格式为 drawRoundRect intx inty intwidth intheight intarcW intarcH 作用 以 x y 为矩形的左上角顶点 绘制一个宽 高分别为width和height的圆角矩形 方法中的参数解释如下 x y 所绘矩形左上角坐标 Width height 所绘矩形的宽和高 arcW arcH分别为所绘矩形圆角的宽和高 该方法的使用举例如下 例9 5importjava awt Graphics importjava applet Applet importjava awt Color classdrawR protectedintx y width height arw arh drawR inta intb intc intd intw inth x a y b width c height d arw w arh h publicclassshowdrawRextendsApplet drawRt publicvoidpaint Graphicsg t newdrawR 35 35 50 100 20 20 g setColor Color red g drawRoundRect t x t y t width t height t arw t arh 该程序运行的效果如下 4 fillRoundRect 调用格式为 fillRoundRect intx inty intwidth intheight intarcW intarcH 方法作用 以 x y 为矩形的左上角顶点 绘制一个宽 高分别为width和height可有填充色的圆角矩形 方法中的参数解释如下 x y 所绘矩形左上角坐标 Width height 所绘矩形的宽和高 arcW arcH分别为所绘矩形圆角的宽和高 与drawRoundRect 的区别 该方法所绘制的圆角矩形中可有填充色 该方法的使用举例如下 例9 6importjava awt Graphics importjava applet Applet importjava awt Color classdrawR protectedintx y width height arw arh drawR inta intb intc intd intw inth x a y b width c height d arw w arh h publicclassshowdrawRextendsApplet drawRt publicvoidpaint Graphicsg t newdrawR 35 35 50 100 20 20 g setColor Color red g fillRoundrect t x t y t width t height t arw t arh 该程序运行的效果如下 9 1 3绘制3D矩形 顾名思义 就是java语言所具有的三维矩形绘制功能 它是利用draw3DRect 和fill3DRect 函数来实现的 1 Draw3DRect 调用格式 draw3DRect intx inty intwidth intheight Booleanb 功能 以左上角坐标为 x y 用指定的宽width和高height绘制一个三维矩形 当变量b的值为true时 所绘矩形为凸起的 当变量b的值为false时 所绘矩形为下凹的 参数介绍如下 x y 三维矩形的左上角坐标 width height 三维矩形的宽和高 举例如下 例9 7importjava awt importjava applet publicclassDraw recextendsApplet publicvoidpaint Graphicsg g setColor Color red g draw3DRect 10 150 100 100 false 上例绘制出一个具有下凹三维效果的矩形 2 fill3DRect 调用格式 fill3DRect intx inty intwidth intheight Booleanb 功能 以左上角坐标为 x y 用指定的宽width和高height绘制一带有填充色的三维矩形 当变量b的值为true时 所绘矩形为凸起的 当变量b的值为false时 所绘矩形为下凹的 参数介绍如下 x y 三维矩形的左上角坐标 width height 三维矩形的宽和高 举例如下 例9 8importjava awt importjava applet publicclassDraw recextendsApplet publicvoidpaint Graphicsg g setColor Color red g fill3DRect 10 150 100 100 false g fill3DRect 10 150 100 100 true 上例绘制出一个具有凸起且内填充色为蓝色的三维矩形 9 1 4绘制弧形Graphics类绘制弧形的函数有两个 drawArc 和fillArc 前一个用于绘制无填充色的弧形 后一个用于绘制有填充色的弧形 1 drawArc 调用格式 drawArc intx inty intwidth intheight intanglestart intangleend 作用 绘制一段圆弧 方法中参数解释 x y 该圆弧外接矩形的左上角坐标 Width height 该圆弧外接矩形的宽和高 Anglestart angleend 该圆弧的起始角和终止角 单位为 度 0 零 度角为x轴的正方向 正的角度按逆时针方向旋转 负的角度按顺时针方向旋转 该方法举例如下 例9 9importjava applet importjava awt publicclassDraw arcextendsApplet publicvoidpaint Graphicsg g setColor Color red g drawArc 0 40 100 100 0 180 g drawArc 300 40 30 100 0 360 g drawArc 200 100 50 50 0 270 运行效果如下 2 fillArc 调用格式 fillArc intx inty intwidth intheight intanglestart intangleend 作用 绘制一段着色圆弧 扇形 参数解释 x y 该圆弧外接矩形的左上角坐标 Width height 该圆弧外接矩形的宽和高 Anglestart angleend 该圆弧的起始角和终止角 单位为 度 0 零 度角为x轴的正方向 正的角度按逆时针方向旋转 负的角度按顺时针方向旋转 该方法举例如下 例9 10importjava applet importjava awt publicclassDraw arcextendsApplet publicvoidpaint Graphicsg g setColor Color red g fillArc 0 40 100 100 0 180 g fillArc 300 40 30 100 0 360 g fillArc 200 100 50 50 0 270 运行效果如下 9 1 5绘制椭圆graphics类绘制椭圆的函数有两个 drawOval 和fillOval 前一个用于绘制无填充色的椭圆 后一个用于绘制有填充色的椭圆 1 drawOval 调用格式 drawOval intx inty intwidth intheight 作用 绘制一个椭圆 参数解释 x y 该椭圆的中心坐标 Width height 该椭圆的宽和高 例9 11importjava applet importjava awt publicclassDraw ovalextendsApplet publicvoidpaint Graphicsg g setColor Color red g drawOval 100 40 50 24 g drawOval 50 50 30 100 g drawOval 10 10 20 70 运行效果如下 2 fillOval 调用格式 fillOval intx inty intwidth intheight 作用 绘制一个实心着色椭圆 参数解释 x y 该椭圆的中心坐标 Width height 该椭圆的宽和高 该方法举例如下 例9 12importjava applet importjava awt publicclassDraw ovalextendsApplet publicvoidpaint Graphicsg g setColor Color red g fillOval 100 40 50 24 g fillOval 50 50 30 100 g fillOval 10 10 20 70 其中setcolor 用于指定椭圆着色的色值 运行效果如下 9 1 6绘制多边形graphics类绘制多边形的函数有两个 DrawPolygon 和fillPolygon 前一个用于绘制无填充色的多边形 后一个用于绘制有填充色的多边形 1 drawPolygon 调用格式 drawPolygon intx inty n 作用 绘制一个无填充色的多边形 方法中参数解释 n 多边形的顶点个数加1 X y 多边形中n个顶点的坐标值 该方法举例如下 例9 13importjava applet importjava awt publicclassDraw polyextendsApplet publicvoidpaint Graphicsg intx1 20 50 70 80 80 20 x1 0 x1 5 相同inty1 30 30 50 80 100 30 y1 0 y1 5 相同g setColor Color red g drawPolygon x1 y1 6 补充 165页 g drawLine 20 30 50 30 g drawLine 50 30 70 50 g drawLine 70 50 80 80 g drawLine 80 80 80 100 g drawLine 80 100 20 30 运行效果如图 2 fillPolygon 调用格式 fillPolygon intx inty n 作用 绘制一个有填充色的多边形 方法中参数解释 n 多边形的顶点个数 X y 多边形中n个顶点的坐标值 该方法举例如下 例9 14importjava applet importjava awt publicclassDraw polyextendsApplet publicvoidpaint Graphicsg intx1 20 50 70 80 80 20 inty1 30 30 50 80 100 30 g setColor Color red g fillPolygon x1 y1 6 运行效果如图 9 2Graphics2D类 Java2D拥有强大的二维图形处理能力 这些功能主要是由Graphics2D类来完成的 该类是Graphics类的一个子类 它的特点是把所绘的图形当成一个对象来处理 绘制前先创建对象 9 2 1绘制2D直线调用格式 先创建2D线形对象 Line2Dline newLine2D Double doublex1 doubley1 doublex2 doubley2 然后再绘制2D直线line 作用 绘制一条二维直线 参数说明 x1 y1 二维直线的起始坐标 x2 y2 二维直线的终点坐标 举例如下 例9 15importjava awt importjava applet importjava awt geom publicclassdraw 2d lineextendsApplet publicvoidpaint Graphicsg Graphics2Dd2 d2 Graphics2D g 转换d为2D对象d2 setColor Color red Line2Dline newLine2D Double 100 10 100 300 创建line对象d2 draw line 画出2D直线d2 setColor Color blue Line2Dline1 newLine2D Double 0 300 100 300 创建line对象d2 draw line1 画出2D直线 运行效果如下 9 2 2绘制2D矩形 调用格式 先创建2D矩形对象 Rectangle2Drect newRectangle2D Double doublex1 doubley1 doublewidth doubleheight 然后再绘制2D矩形 作用 绘制一个2D矩形 参数说明 x1 y1 2D矩形的左上角坐标 width height 2D矩形的宽和高 举例如下 例9 16importjava awt importjava applet importjava awt geom publicclassdraw 2d RectextendsApplet publicvoidpaint Graphicsg Graphics2Dd2 d2 Graphics2D g 转换g为2D对象d2 setColor Color red Rectangle2Drect newRectangle2D Double 40 50 250 300 创建矩形对象d2 draw rect 画出矩形d2 setColor Color blue Rectangle2Drect1 newRectangle2D Double 80 100 250 300 创建矩形对象d2 draw rect1 画出矩形 运行效果如图 9 2 3绘制2D圆角矩形 调用格式 先创建2D圆角矩形对象 RoundRectangle2Dr rect newRoundRectangle2D Double doublex1 doubley1 doublewidth doubleheight doublearcw doublearch 然后再绘制2D圆角矩形 作用 绘制一个2D圆矩形 参数说明 x1 y1 width height 分别为2D圆角矩形的左上角坐标 宽和高 arcw arch 分别为圆角的长轴和短轴 举例如下 例9 17importjava awt importjava applet importjava awt geom publicclassdraw 2d RectextendsApplet publicvoidpaint Graphicsg Graphics2Dd2 d2 Graphics2D g 转换g为2D对象d2 setColor Color red RoundRectangle2Drect newRoundRectangle2D Double 40 50 50 30 20 20 创建矩 形对象d2 draw rect 画出矩形d2 setColor Color blue RoundRectangle2Drect1 newRoundRectangle2D Double 80 100 50 30 20 20 创建 矩形对象d2 draw rect1 画出矩形 运行效果如图 9 2 4绘制椭圆 调用格式 先创建2D椭圆对象 Ellipse2Dellipse newEllipse2D Double doublex1 doubley1 doublewidth doubleheight 然后再绘制2D椭圆 作用 绘制一个2D椭圆 参数说明 x1 y1 width height 分别为2D椭圆的左上角坐标 宽和高 举例如下 例9 18importjava awt importjava applet importjava awt geom publicclassdraw 2d RectextendsApplet publicvoidpaint Graphicsg Graphics2Dd2 d2 Graphics2D g 转换g为2D对象d2 setColor Color red Ellipse2Dellipse newEllipse2D Double 10 10 100 200 创建椭圆对象d2 draw ellipse 画出椭圆 d2 setColor Color blue Ellipse2Dellipse1 newEllipse2D Double 20 20 100 200 创建椭圆对象d2 draw ellipse1 画出矩形 运行效果如图 9 2 5绘制2D园弧 调用格式 先创建2D圆弧对象 Arc2Darc newArc2D Double doublex1 doubley1 doublewidth doubleheight doublestarang doubleendang arc2D pie 然后再绘制2D圆弧 作用 绘制一条2D圆弧 参数说明 x1 y1 width height 2D圆弧外接矩形的左上角坐标 宽和高 Starang ending 圆弧的起始角和终止角 单位为 度 最后一个参数的取值为 Arc2D OPEN Arc2D PIE Arc2D CHORD分别代表开弧 饼弧 弓弧 举例如下 例9 19importjava awt importjava applet importjava awt geom publicclassdraw 2d ArcextendsApplet publicvoidpaint Graphicsg Graphics2Dd2 d2 Graphics2D g 转换g为2D对象d2 setColor Color red Arc2Darc1 newArc2D Double 20 30 80 55 0 90 Arc2D OPEN 创建对象d2 draw arc1 画出开弧 红色d2 setColor Color blue Arc2Darc2 newArc2D Double 40 30 80 55 0 90 Arc2D PIE 创建对象d2 draw arc2 画出饼弧 蓝色d2 setColor Color green Arc2Darc3 newArc2D Double 80 30 80 55 0 90 Arc2D CHORD 创建对象d2 draw arc3 画出弓弧 绿色 运行效果如图 9 2 6绘制二次曲线 调用格式 先创建2D二次曲线对象 QuadCurve2Ddraw c newQuadCurve2D Double doublex1 doubley1 doublex2 doubley2 doublex3 doubley3 然后再绘制二次曲线 作用 绘制二次曲线 参数说明 x1 y1 x2 y2 分别为两个端点坐标 X3 y3 控制点坐标 举例如下 例9 20importjava awt importjava applet importjava awt geom publicclassdraw 2d RectextendsApplet publicvoidpaint Graphicsg Graphics2Dd2 d2 Graphics2D g 转换g为2D对象d2 setColor Color red QuadCurve2Ddraw c1 newQuadCurve2D Double 10 50 100 100 20 40 创建对象d2 draw draw c1 画出d2 setColor Color blue QuadCurve2Ddraw c2 newQuadCurve2D Double 20 60 100 100 40 80 创建对象d2 draw draw c2 画出d2 setColor Color green QuadCurve2Ddraw c3 newQuadCurve2D Double 30 70 100 100 80 160 创建对象d2 draw draw c3 画出 运行效果如图 9 3Color类 java语言的颜色类 Color类为我们定义了丰富的颜色值常量和颜色值产生的方法 能够让我们控制各种色彩的显示 其每一种颜色都是由 红 绿 蓝 三原色合成 9 3 2Color类中的常用方法 1Color 调用格式 publiccolor intred intgreen intblue 作用 配置颜色 参数解释 red green blue 分别为配置三原色 红 绿 蓝 的色值 举例如下 例9 21importjava awt Graphics importjava applet Applet importjava awt Color classcircle protectedintx y width height circle x 35 y 35 width 50 height 100 publicclassshowcircleextendsApplet circlet newcircle publicvoidpaint Graphicsg Colorco newColor 255 0 0 g setColor co g drawOval t x 150 t y 150 t width t height Colorco1 newColor 0 0 255 g setColor co1 g fillOval t x t y t width t height 运行效果如图 2Getred 调用格式 publicintgetred 作用 返回当前色值中的红色含量值 3Getblue 调用格式 publicintgetblue 作用 返回当前色值中的蓝色含量值 4GetGreen 调用格式 publicintgetGreen 作用 返回当前色值中的绿色含量值 以上三个方法举例如下 例9 22importjava awt Graphics importjava applet Applet importjava awt Color publicclassshowcircleextendsApplet publicvoidpaint Graphicsg Colorco newColor 255 0 0 g setColor co Colorc g getColor g drawString 红色含量值 c getRed 10 20 g drawString 绿色含量值 c getGreen 10 40 g drawString 蓝色含量值 c getBlue 10 60 5Setcolor 调用格式 publicabstactvoidSetcolor Colorc 作用 设置当前颜色值 参数解释 c 颜色值 运行事例参看 例19 6Getcolor 调用格式 publicabstactcolorgetcolor 作用 返回当前颜色值 举例如下 例9 23importjava awt Graphics importjava applet Applet importjava awt Color publicclassshowcircleextendsApplet publicvoidpaint Graphicsg g setColor g getColor g fillOval 20 30 40 50 9 4Font类 在前面章节的很多例子中我们都使用了输出文本的方法 在没有设置字体 字型号的情况下 其输出都是采用的默认字体和字型号 在java语言中 我们可以通过Font类来设定不同的输出字体 字型号 使我们的界面更美观 更漂亮 下面我们就Font类的使用做一详解 Java语言中 设置字体输出的操作顺序一般是 先用Font类创建字体对象 然后用setFont 方法设置输出字体 最后用drawString 输出结果 9 4 1利用Font类建立字体 1Font类建立字体调用格式 Fontcrea Font newFont stringfont name intfont style intfont size 2参数说明 font name 字体的名称 如 SansSerif Times 黑体 宋体 等 font style 字体的样式 如 Font PLAIN 正常体 Font BOLD 加粗体 Font ITALIC 斜体 等 font size 字体大小的磅值 是一个数字 如 10 12 14 18 20等 创建字体的使用举例 例9 24importjava awt Graphics importjava applet publicclassshowfontextendsApplet Fontfont1 newFont TimesNewRoman Font BOLD 14 Fontfont2 newFont SansSerif Font ITALIC 48 Fontfont3 n
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 护士报考试题库及答案解析
- 寿司店营销策划方案
- (浙江新高考专用版)2024-2025学年高中物理 第十一章 机械振动 4 单摆说课稿 新人教版选修3-4
- Unit 3 Toys Lesson 1(教学设计)-2024-2025学年人教精通版(2024)英语三年级上册
- 华西医院建筑方案设计
- 高考语文专题试题汇编与解析
- 小学科学实验操作步骤教学设计
- 建筑施工项目进度计划制定技巧
- 余姚开业活动策划方案公司
- 高考物理全真模拟试卷及解析
- 2025贵州贵安城市置业开发投资有限公司招聘32人考试参考题库及答案解析
- 露天煤业安全生产培训课件
- 2025全国科普日科普知识竞赛题库及答案
- 2025年全国医学基础知识试题(附答案)
- 食堂安全培训课件
- 【课件】角的概念+课件+2025-2026学年人教版(2024)七年+数学级上册+
- 2025企业劳动合同范本新版
- 2025年防雷检测专业技术人员能力认定考试题库及答案
- 《房屋市政工程生产安全重大事故隐患判定标准(2024版)》解读
- 美发裁剪理论知识培训课件
- 舞蹈老师自我介绍课件
评论
0/150
提交评论