教学课件第04章Applet及其应用

上传人:re****.1 文档编号:569321989 上传时间:2024-07-28 格式:PPT 页数:36 大小:480.97KB
返回 下载 相关 举报
教学课件第04章Applet及其应用_第1页
第1页 / 共36页
教学课件第04章Applet及其应用_第2页
第2页 / 共36页
教学课件第04章Applet及其应用_第3页
第3页 / 共36页
教学课件第04章Applet及其应用_第4页
第4页 / 共36页
教学课件第04章Applet及其应用_第5页
第5页 / 共36页
点击查看更多>>
资源描述

《教学课件第04章Applet及其应用》由会员分享,可在线阅读,更多相关《教学课件第04章Applet及其应用(36页珍藏版)》请在金锄头文库上搜索。

1、Java语言程序设计马 皓1第四章 Applet及其应用1.Applet概述2.Applet类3.Applet程序与HTML文件4.Applet的应用2Applet概述nJava程序的两种基本形式nJava Application(应用程序),可独立运行nJava Applet(小程序),嵌入在浏览器中运行n介绍nApplet的结构特点、实现方法、工作原理n掌握nApplet的编辑、编译和运行方法3一个Applet小程序的例子import ;import ;public class Exam4_1 extends Applet String str;public void init() str

2、 = “Here is an Applet”;public void paint(Graphics g) g.drawString(str, 100, 100);4HTML文件n超文本标记语言(HTML)nWWW浏览器nApplet小程序n嵌入在/写入在HTML文件中n从WWW服务器下载到本地WWW浏览器n由WWW浏览器中的Java解释器来运行5HTML文件n实现过程nApplet小程序编写,编译,得到字节码文件njavac Exam4_1.javan嵌入到HTML文件中,保存为Exam4_1.htmlnnnnnn6HTML文件n浏览器打开Exam4_1.html文件7Applet的特点n通常

3、作为Applet类的子类,格式如下:public class 类名 extends Applet n嵌入在HTML文件中,利用WWW浏览器或Appletviewer来运行n利用了WWW浏览器或Appletviewer所提供的图形用户界面功能8Applet的工作原理Applet源程序字节码文件嵌入到HTML文件WWW浏览器打开 该HTML文件9第四章 Applet及其应用1.Applet概述2.Applet类3.Applet程序与HTML文件4.Applet的应用10Applet类的继承关系类Applet默认情况下,布局管理器FlowLayout使用11Applet类的主要方法1.init()方

4、法n完成初始化操作n在Applet程序第一次加载时调用,仅执行一次2.start()方法n启动Applet主线程运行n重启时也被调用(reload或返回)3.paint()方法n将结果输出/绘制到界面上n被自动调用(启动后/窗口改变/repaint()调用)12Applet类的主要方法4.stop()方法n暂停Applet程序执行5.destroy()方法n终止Applet程序执行,释放所占用的资源13Applet类的主要方法import ;import java.awt.*;public class Exam extends Applet public void init( ) /初始化Ap

5、plet程序public void start( ) /启动Applet线程public void paint(Graphics g) /绘制输出显示信息public void stop( ) /暂停线程public void destroy( ) /释放系统资源,结束线程14第四章 Applet及其应用1.Applet概述2.Applet类3.Applet程序与HTML文件4.Applet的应用15HTML文件n超文本标记语言(HTML)n和nHtml文件开始和结束的标记n和nWWW浏览器窗口标题内容的标记n和nHtml文件在浏览器窗口中显示内容的标记n和n嵌入到Html文件中Applet程

6、序的标记16Applet程序的标记nnn参数n17Applet小程序import ;import java.awt.*;public class Exam4_3 extends Applet String str;int x, y, h;Font fnt;public void init() str = getParameter(“string”);h = Integer.parseInt(getParameter(“size”);x = Integer.parseInt(getParameter(“x1”);y = Integer.parseInt(getParameter(“y1”);fn

7、t = new Font(“TimesRoman”, Font.BOLD, h);public void paint(Graphics g) g.setColor(Color.red);g.setFont(fnt);g.drawString(str, x, y);18Applet小程序19第四章 Applet及其应用1.Applet概述2.Applet类3.Applet程序与HTML文件4.Applet的应用20绘制图形1.设置字体1.类2.设置文本的字体(包括字型和字号)3.构造方法1.public Font(String name, int style int size)2.设置颜色1.类

8、2.控制颜色,Color类已包含13个颜色常量3.构造方法1.public Color(int r, int g, int b)2.public Color(float r1, float g1, float b1)21绘制图形3.绘制文本1.绘制字符串1.public void drawString(String s, int x, int y)2.绘制字符1.public void drawString(char c, int offset, int number int x, int y)3.绘制字节1.public void drawString(byte b, int offset,

9、 int number int x, int y)22Applet小程序import ;import java.awt.*;public class Exam4_4 extends Applet public void paint(Graphics g) Font font1, font2, font3;font1 = new Font(“Serif”, Font.BOLD, 20);font2 = new Font(“Monospaced”, Font.BOLD+Font.ITALIC, 24);font3 = new Font(“SansSerif”, Font.PLAIN, 16);g.

10、setFont(font1);g.drawString(“Serif 20 point BOLD”, 30, 20);g.setFont(font2);g.drawString(“Monospaced 24 point BOLD + ITALIC”, 30, 80);g.setFont(font3);g.drawString(“SansSerif 16 point PLAIN”, 30, 50);int size = font2.getSize();int style = font1.getStyle();String name = font2.getName();String str = n

11、ame + “ “ + style + “ “ + size;g.drawString(str, 30, 110);23Applet小程序24Applet小程序import ;import java.awt.*;public class Exam4_6 extends Applet Font font1 = new Font(“TimesRoman”, Font.ITALIC, 25);Font font2 = new Font(“Braggadcoio”, Font.BOLD, 40);public void paint(Graphics g) String str = “I love Be

12、ijing!”;Color mycolor = new Color(192, 64, 200);g.setFont(font1);g.setColor(mycolor);g.drawString(str, 30, 40);Color darker = mycolor.darker();g.setColor(darker);g.drawString(str, 50, 80);Color brighter = mycolor.brighter();g.setColor(brighter);g.drawString(str, 70, 120);g.setFont(font2);g.setColor(

13、Color.red);g.drawString(str, 30, 170);g.setColor(Color.white);g.drawString(str, 32, 169);25Applet小程序26绘制图形4.绘制几何图形1.画直线1.void drawLine(int x1, int y1, int x2, int y2)2.画矩形1.void drawRect(int x, int y, int width, int height)2.void fillRect(int x, int y, int width, int height)3.void clearRect(int x, i

14、nt y, int width, int height)4.void drawRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight)5.void draw3DRect(int x, int y, int width, int height, boolean b)6.void fill3DRect(int x, int y, int width, int height, boolean b)27绘制图形4.绘制几何图形1.画圆弧和椭圆1.void drawArc(int x, int y, int w

15、idth, int height, int startAngle, int arcAngle)2.void fillArc(int x, int y, int width, int height, int startAngle, int arcAngle)3.void drawOval(int x, int y, int width, int height)4.void fillOval(int x, int y, int width, int height)28Applet小程序import ;import java.awt.*;public class Exam4_8 extends Ap

16、plet public void paint(Graphics g) int x0 =10, y0=20, X=150, Y=80, L, c;int arc = 0, N=10;double xy=1.0*(X-Y)/N/2;g.setColor(Color.red);g.fillRect(x0, y0, X, X);for(int i=0; i =N; i+) L=(int)(X-2*i*xy); arc=Y*i/N; c=i*240/N; g.setColor(new Color(c, c, c); g.drawRoundRect(int)(x0+i*xy), (int)(y0+i*xy

17、), L, L, arc, arc);g.setColor(Color.blue);g.draw3DRect(200, y0, X, X, true);g.setColor(Color.green);g.fill3DRect(400, y0, Y, Y, false);29Applet小程序30绘制图形4.绘制几何图形1.画多边形1.public void drawPolygon(int xPoints, int yPoints, int Points)2.public void fillPolygon(int xPoints, int yPoints, int Points)3.public

18、 void drawPolygon(Polygon p)4.public void fillPolygon(Polygon p)5.Polygon类构造方法6.Polygon()7.Polygon(int xPoints, int yPoints, int numberOfPoints)31演示图像1.定义图像对象1.类2.图像高度和宽度1.int getHeight(ImageObserver observer)2.int getWidth(ImageObserver observer)2.获取图像信息方法1.Image getImage(URL url, String name)3.显示图

19、像的操作1.drawImage(Image img, int x, int y, ImageObserver observer)2.drawImage(Image img, int x, int y, int width, int height, ImageObserver observer)32Applet小程序import ;import java.awt.*;public class Exam4_13 extends Applet public void paint(Graphics g) Image pic;pic = getImage(getDocumentBase(), “edon

20、key.jpg”);int x0 = 10, y0 = 30;int w = pic.getWidth(this);int h = pic.getHeight(this);g.drawImage(pic, x0, y0, w/8, h/8, this);g.drawImage(pic, x0+150, y0, w/12, h/12, this);g.drawImage(pic, x0+150, y0+100, w/15, h/15, this);g.drawImage(pic, x0+250, y0+30, (int)(w*0.1), (int)(h*0.2), this);33Applet小程序34演示图像4.动画1.创建Image类的对象数组2.drawImage()3.Thread.sleep()方法4.repaint()5.播放声音1.public void play(URL url)2.public AudioClip getAudio(URL url)3.play()方法/loop()方法/stop()方法35第四章 结束!36

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 医学/心理学 > 基础医学

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号