1 / 15

Java 程序设计课程

Java 程序设计课程. 清华大学继续教育学院 IT 学习认证中心. 图形和声音处理. Java 中使用 Graphics 和 Graphics2D 对象定义图形外观 本章学习要点 Graphics 的概念 绘制基本图形 填充各种基本图形 学习使用 java 的字体 学习使用颜色 声音处理. 屏幕坐标体系. 0. X 轴. (x,y). Y 轴. 图形类. 图形类 (java.awt.Graphics) 是一个抽象类,用于定义一个真正的工具,接受图形操作。 该类具有 47 个公共方法:用于显示图像和文本、绘制和填充形状、剪贴图像等

Download Presentation

Java 程序设计课程

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java程序设计课程 清华大学继续教育学院 IT学习认证中心

  2. 图形和声音处理 • Java中使用Graphics和Graphics2D对象定义图形外观 • 本章学习要点 • Graphics的概念 • 绘制基本图形 • 填充各种基本图形 • 学习使用java的字体 • 学习使用颜色 • 声音处理

  3. 屏幕坐标体系 0 X轴 (x,y) Y轴

  4. 图形类 • 图形类(java.awt.Graphics)是一个抽象类,用于定义一个真正的工具,接受图形操作。 • 该类具有47个公共方法:用于显示图像和文本、绘制和填充形状、剪贴图像等 • 调用和返回Graphics的相关方法见教材(P245表12-1、12-2)

  5. 绘图和填充 • Graphics可以绘制以下图形: • 直线(Lines) • 折线(Polylines) • 矩形(Rectangles) • 弧(Arcs) • 椭圆(Ovals) • 多边形(Ploygons) • 文本(Text)

  6. 画线 • 画直线 • Graphics.drawLine(int x1,int y1 ,int x2,int y2); • //x1和y1为起点坐标;x2和y2是终点坐标 • 画折线 • Graphics.drawPolyline(int[] x,int[] y ,int num); • //数组x和y用于指定每个点的坐标,num表示要画的折线的点数,折线的点数等于折线段数加1。曲线开闭由起终点的位置是否重合确定。

  7. 绘制矩形 • 矩形的绘制填充的方法如下 • Void clearRect(int x,int y,int w,int h); • Void drawRect(int x,int y, int w, int h); • Void drawRoundRect(int x,int y, int w, int h,int acrW,int acrH); • Void draw3DRect(int x,int y, int w, int h, boolean raise); • Void fillRoundRect(int x,int y,int w,int h, int acrW,int acrH); • Void fillRect(int x,int y,int w,int h); • Void fill3DDrect(int x, int y, int w, int h, boolean raise); • 上面的参数x,y,w,h均为决定坐标的参数,绘制和填充3D矩形的方法增加了一个boolean型的参数,指定凸(true)凹(false);acrW为弧的水平直径,acrH为弧的垂直直径

  8. 绘制多边形 • 多边形的绘制方法: • drawPolygon(int[] x,int[] y,int n); • fillPolygon(int[] x,int[] y,int n); • //x,y为顶点坐标,n为顶点个数

  9. 绘制圆弧 • 绘制弧形的方法: • drawArc(int x,int y,int w, int h, int starAngle,int arcAngle); • fillArc(int x,int y,int w,int h, int starAngle,int arcAngle); • //x,y 定义左上角的点;弧的宽度(w)和高度(h);起始角度(starAngle),弧扫过的角度(arcAngle)

  10. 绘制椭圆 • 绘制椭圆的方法 • drawOval(int x,int y,int w,int h);//空心椭圆 • fillOval(int x,int y,int w,int h);//实心椭圆 • X、y表示椭圆的x、y轴的距离,单位是像素,w、h表示高和宽 • 当w=h时 画出来的就是圆

  11. 文本和文字 • 字体是一个字符集,字体通过指定其逻辑字体名、字形和字体大小来实例化 • Java的Font类提供的一套基本字体和字型在应用中通常转换成本地平台支持的字体 • 创建Font对象 • Font(String fontName,int fontStyle,int fontSize); • 参数的意义:字体名,字体样式,字体大小 • 绘制字符和字符串 • drawChars(char charArr[],int cc,int len,int x,int y); • 参数意义:字符数组,起始位置和长度,显示坐标xy • drawString(String str,int x,int y)//字符,坐标x,y

  12. 查找字体信息 • Font类中的几个方法用于查找当前使用字体的信息 • getFamily() //字体名字 • getName() //字体名称 • getStyle() //字体样式 • getSize() //字体大小

  13. 颜色(Color类) • Java的颜色系统采用RGB标准系统 • Color类定义好的13种颜色(见文档Color类说明)

  14. 测试和设置颜色 • 颜色类的构造方法 • Color(float r,float g,float b); • r,g,b的值在0.0到1.0之间 • Color(float r,float g,float b,float a); • a设置颜色的alpha值,在0.0-1.0之间 • Color(int rgb); • 三个0-255的二进制代码组合,可以使用16进制表示 • Color(int r,int g,int b); • r,g,b的范围为0-255的整数 • Color(int r,int g,int b,int a); • 参数为0-255的整数

  15. Java的简单声音处理 • Applet用于播放声音的静态方法: • newAudioClip(URL url,String Sname) • getAudioClip(URL url,String Sname) • 以上两个方法返回AudioClip对象,该对象具备以下方法: • play( ) 播放声音文件 • loop( ) 循环播放 • stop( ) 停止播放 • 支持的声音文件有:au,aiff,wav,midi,rfm等

More Related