Java图形用户界面.ppt

上传人:max****ui 文档编号:6359923 上传时间:2020-02-23 格式:PPT 页数:46 大小:262.55KB
返回 下载 相关 举报
Java图形用户界面.ppt_第1页
第1页 / 共46页
Java图形用户界面.ppt_第2页
第2页 / 共46页
Java图形用户界面.ppt_第3页
第3页 / 共46页
点击查看更多>>
资源描述
第6章Java图形用户界面 6 1图形用户界面概述和java awt包简介6 2字体和颜色的设置 图形绘制和图像显示6 3Graphics2D画图简介 6 1图形用户界面概述和java awt包简介 图形用户界面GUI GraphicsUserInterface 就是应用程序提供给用户操作的图形界面 包括窗口 菜单 按钮 工具栏和其他各种界面元素 在Java里有两个包为GUI设计提供了丰富的功能 awt abstractwindowstoolkit 包和swing包 awt是javaGUI的早期版本 组件种类有限 只提供基本的GUI设计类 swing包是SUN公司对早期版本的改进版本 它提供了更加丰富的组件和功能 swing会用到awt中许多知识 学习awt包是学习GUI编程的基础 java awt包中的一些常用类 GUI组件分类 在AWT的概念中 窗口系统中所显示的各种对象都统称为 GUI组件 Component 组件有基本组件和容器组件之分 基本组件是不能包含其它组件的组件 是构成图形用户界面的基本元素 容器组件是用来包含其他组件的 故称之为容器 container 用户可以把各种组件放入到容器中 也可以把容器放到另一个容器中 从而形成具有层次的组件结构 组件类 Component 容器类 Container 基本组件类 面板类panel 窗体类windows Applet小程序类 Frame框架类 Dialog对话框类 无边框 有边框 6 2字体和颜色的设置 图形绘制和图像显示 6 2 1设置字体 一 创建Font类的对象 Font Stringname intstyle intsize 使用java awt包中的Font类的构造函数创建字体类的对象 例 Fontfont1 newFont 楷体 GB2312 Font BOLD Font ITALIC 50 二 设置字体 publicvoidsetFont Fontfont 设置字体publicFontgetFont 返回当前字体对象 字体的创建和设置应在显示前进行 否则以系统默认字体显示 例1 编写一个简单的设置字体的程序 将字体设置为宋体 黑体 大小为30个像素 并在小程序窗口输出 TestFont javaimportjava applet Applet importjava awt Graphics importjava awt Font publicclassTestFontextendsApplet publicvoidpaint Graphicsg Fontfont1 newFont 宋体 Font BOLD 30 g setFont font1 Strings 中国北京 g drawString s 10 30 Graphics drawstring Stringstr intx inty 其中的坐标x和y指的是整个文本块显示时左下角的位置 page1 htmlJava小程序 运行后结果如下 思考 运行后结果如下 6 2 2设置颜色 利用java awt包中的Color类可以创建颜色类的对象 一 创建color对象 publicColor intr intg intb publicColor intr intg intb inta 第一种是用r g b值创建一种不透明的颜色 第二种是用r g b值创建一种透明的颜色 a表示透明度 参数取值为0 255 publicColor floatr floatg floatb publicColor floatr floatg floatb floata 第一种是用r g b值创建一种不透明的颜色 第二种是用r g b值创建一种透明的颜色 a表示透明度 参数取值为0 0f 1 0f 二 Color类的颜色常量 Color类共有13种颜色常量 使用的时候可以查询API文档 如 Color RED表示红色 三 设置颜色 publicvoidsetColor Colorc 设置颜色publicColorgetColor 返回当前颜色对象publicintgetRed 得到当前颜色对象的red值publicintgetGreen 得到颜色对象的green值publicintgetBlue 得到颜色对象的blue值publicintgetAlpha 得到颜色对象的alpha值 例2 编写一个颜色设置的程序 TestColor javaimportjava applet Applet importjava awt publicclassTestColorextendsApplet publicvoidinit setBackground Color black publicvoidpaint Graphicsg Colorc1 newColor 255 0 0 Colorc2 newColor 255 0 0 128 Colorc3 Color pink Fontfont1 newFont 宋体 Font BOLD Font ITALIC 15 Strings 中国北京 g setColor c1 g setFont font1 g drawString 红色 s 10 20 g setColor c2 g drawString 红色 s 10 40 g setColor c3 g drawString 粉色 s 10 60 page2 htmlJava小程序 运行后结果如下 6 2 3字符数组和字节数组的显示 以下三个方法是Graphics类 该类在java awt包中 的方法 1 绘制字符串 publicvoiddrawString Stringstr intx inty 坐标点 x y 与要绘制的字符串Str左下角对应 2 绘制字符数组 publicvoiddrawChars Charchars intoffset intnumber intx inty 参数offset是数组的起始下标 参数number是要绘制的元素个数 坐标点 x y 含义同上 3 绘制字节数组 publicvoiddrawBytes Charbytes intoffset intnumber intx inty 含义同字符数组 例 编写程序 显示字符数组和字节数组 DrawCharByte javaimportjava applet importjava awt publicclassDrawCharByteextendsApplet publicvoidinit setBackground Color black publicvoidpaint Graphicsg Strings 中国北京 g setColor Color red g drawString s 20 40 charch newchar A B C D E g setColor Color white g drawChars ch 0 5 80 40 bytebyt newbyte 65 66 67 68 69 g setColor Color yellow g drawBytes byt 0 3 82 42 page3 javaJava小程序 运行后结果如下 6 2 4java的图形绘制 预备知识 Java图形界面窗口的坐标原点 0 0 在窗口左上角 水平向右为x的正方向 垂直向下为y的正方向 坐标值单位是像素 小程序窗口的坐标系同上 通过getSize 方法可得到小程序窗口界面的宽和高 小程序窗口宽w intw getSize width 小程序窗口高h inth getSize height 1 画直线 publicvoiddrawLine intx1 inty1 intx2 inty2 功能 在点 x1 y1 和 x2 y2 之间画直线 2 画矩形 1 画矩形 publicvoiddrawRect intx inty intwidth intheight 功能 以给定坐标 x y 为左上角坐标 画宽度为width 高度为height的矩形 2 用当前设置的颜色画填充矩形 publicvoidfillRect intx inty intwidth intheight 3 画圆角矩形 publicvoiddrawRoundRect intx inty intwidth intheight intarcWidth intarcHeight publicvoidfillRoundRect intx inty intwidth intheight intarcWidth intarcHeight 前4个参数的意义同上 第5 6个参数arcWidth和arcHeight分别为水平方向圆弧总宽度 垂直方向圆弧总宽度 注 若圆弧的宽度和高度分别等于矩形的宽度和高度 则绘制的是椭圆 4 画3D矩形 publicvoiddraw3DRect intx inty intwidth intheight booleanbool publicvoidfill3DRect intx inty intwidth intheight booleanbool 功能 绘制一个有立体感的矩形 当bool为true时 矩形为突出的 当bool为false时 矩形为凹陷的 3 画椭圆和圆弧 1 画椭圆 publicvoiddrawOval intx inty intwidth intheight publicvoidfillOval intx inty intwidth intheight 功能 x y是椭圆外切矩形左上角的坐标 参数width和height是椭圆外切矩形的宽和高 上面的两个方法分别画椭圆和画填充的椭圆 2 画圆弧 publicvoiddrawArc intx inty intwidth intheight intstartAngle intarcAngle publicvoidfillArc intx inty intwidth intheight intstartAngle intarcAngle 功能 前4个参数同画椭圆的参数 后两个参数含义startAngle是弧的起始角度 arcAngle表示从起始角度算起转多少度 逆时针为正 顺时针为负 他们的单位都是度 取值为0到360度之间 若超过则取360的余数 例4 编写程序演示画填充的椭圆和填充的扇形 DrawOval javaimportjava applet importjava awt publicclassDrawOvalextendsApplet publicvoidpaint Graphicsg g setColor Color BLUE g drawRect 20 20 100 60 g setColor Color RED g fillOval 20 20 100 60 g setColor Color BLACK g drawOval 140 20 100 60 g fillArc 140 20 100 60 0 60 page4 htmlJava小程序 运行后结果如下 6 2 5图像的显示 一 声明和获取一个图像类的对象 1 定义一个图像类的对象 Imagepic 2 获取一个图像的对象 这里只介绍小程序中获取图像的方法 publicImagegetImage URLurl Stringname 该方法是java applet Applet类提供的方法 用于从文件加载图像到内存 以便显示图像 name 图像名称 格式可以是gif jpg和png等 URL 图像存放目录 URL getDocumentBase 图像存放在HTML文档目录下或其子目录下时 使用该方法指明图像路径 getCodeBase 图像存放在程序代码所在目录下或其子目录下时 使用该方法指明图像路径 3 得到指定图像的高度及宽度 intgetHeight ImageObserverobserver intgetWidth ImageObserverobserver 参数observer是加载图像时的图像观察器 一般是在本类显示 其值为this 如Imagepic getImage getCodeBase a jpg intw h w pic getWidth this h pic getHeight this 二 显示图像的方法 1 以图像本身的大小显示图像 publicbooleandrawImage Imageimg intx inty ImageObserverobserver 说明 参数x y是被显示的图像在窗口左上角的图标 2 放大或缩小显示图像 publicbooleandrawImage Imageimg intx inty intwidth intheight ImageObserverobserver 说明 这里的width和height表示图像在窗口显示的尺寸 例5 编写程序 演示按原图大小显示图像 缩小为原图一半显示图像 importjava applet importjava awt publicclassShowImageextendsApplet publicvoidpaint Graphicsg Imagepic getImage getCodeBase 计算 gif intw pic getWidth this inth pic getHeight this intd 5 g drawImage pic 0 0 this 原图大小显示g drawImage pic w d 0 w 2 h 2 this 缩小为原图宽高一半并显示 page5 htmlJava小程序 运行后结果如下 6 3Graphics2D画图简介 Java类库中的java awt包中还提供了另外一个类Graphics2D供画图显示使用 它是Graphics类的子类 与Graphics的区别 Graphic2D有更强大的图形处理功能 它把要绘制的图形当作对象来处理 通过方法draw 和fill 绘制和填充图形 方法的参数都是图形对象 如直线 Line2D 矩形 Rectangle2D 和椭圆 Ellipse2D 等 上述的创建图形对象的类都存在于java awt geom中 因此若要使用Graphics2D画图 需在程序前面引入java awt geom包中相应的类 通常用paint Graphicsg 绘图时 要通过以下语句把父类对象强制转换为其子类Graphics2D的对象g2d进行画图显示 Graphics2Dg2d Graphics2D g 例6用Graphics2D画一条直线 importjava applet importjava awt importjava awt geom publicclassTestGraphics2DextendsApplet publicvoidpaint Graphicsg Graphics2Dg2d Graphics2D g BasicStrokebstroke newBasicStroke 3 0f g2d setStroke bstroke Line2Dline newLine2D Double 20 0 20 0 32 0 20 0 g2d draw line 调用类Line2D的构造函数 参数为Double型 见API的java awt geom包 page6 htmlJava小程序 运行后结果如下
展开阅读全文
相关资源
相关搜索

当前位置:首页 > 图纸专区 > 课件教案


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!