1 / 32

第 6 章 Java Applet

第 6 章 Java Applet. 5.1 什么是 Applet 5.2 Applet 方法介绍 5.3 Applet 的 AWT 绘制 5.4 Applet 的参数传递 5.5 Applet 的多媒体支持 5.6 Java 存档文件. 1. Applet 概念. Java Applet 也称为 小应用程序 ,它是网页内容的一个组成部分 在 HTML 中通过以下标记来标识一个 Applet. <applet code="myapplet.class" height=200 width=300> </applet>

cortez
Download Presentation

第 6 章 Java Applet

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. 第6章Java Applet 5.1 什么是Applet 5.2 Applet方法介绍 5.3 Applet的AWT绘制 5.4 Applet的参数传递 5.5 Applet的多媒体支持 5.6 Java存档文件

  2. 1. Applet概念 • Java Applet也称为小应用程序,它是网页内容的一个组成部分 • 在HTML中通过以下标记来标识一个Applet.<applet code="myapplet.class" height=200 width=300></applet> • 其中,code、height、width是3个必须的属性,code指定Applet的字节码文件名;height和width决定Applet在页面中的大小,分别规定高度和宽度。

  3. 除了以上3个基本属性外,Applet中还提供一些可选属性: alt为小应用程序的说明信息,当浏览器不支持Java时将在Applet所处位置显示该信息。 Align属性用来控制Applet在页面中的相对对齐方式。Align值为left表示左对齐,right表示右对齐,middle表示居中对齐。 HSPACE和VSPACE属性分别用来设定Applet与周围文本之间的水平和垂直间距(单位为像素)。 可选属性

  4. CODEBASE属性用于指示Applet类文件的URL路径。默认情况下,Applet类文件与HTML文件放在同一文件夹下。如果Applet是存放在另一文件夹下,则要指示与HTML文件的相对路径或者某个绝对URL路径。下例表示类文件在HTML文件所处路径的java子文件夹下。 <applet code="myapplet.class“codebase="java" height=200 width=300 > </applet> 可选属性(续)

  5. 在支持Java的浏览器页面中运行 使用J2SDK提供的Appletviewer程序查看 2. 查看Applet执行

  6. init( )方法---Applet实例的初始化工作 start( )方法 paint( )方法 ---调整浏览窗口大小、缩放浏览窗口、移动窗口或刷新等操作都会导致执行paint()方法实现Applet重绘 stop( )方法 destroy( )方法 update()方法 repaint()方法 repaint()--->update(g)--->paint(g) 6.2 Applet方法介绍

  7. drawLine(int x1, int y1, int x2, int y2) drawRect(int x, int y, int width, int height) drawOval(int x, int y, int width, int height) drawArc(int x, int y, int width, int height, int startAngle, int arcAngle) drawPolygon(int[] xPoints, int[] yPoints, int nPoints) drawRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight) 1. 常用图形元素的绘制方法

  8. fillOval(int x, int y, int width, int height) fillRect(int x, int y, int width, int height) fillRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight) fillArc(int x, int y, int width, int height, int startAngle, int arcAngle) 1. 常用图形元素的绘制方法(续)

  9. Font myFont = new Font("宋体", Font.BOLD, 12); 定义字体为宋体,大小为12号,粗体。 Font.PLAIN,Font.ITALIC,Font.BOLD分别表示普通、斜体和粗体,如果要同时兼有几种风格可以通过"+"号连接。 例如:new Font("TimesRoman", Font.BOLD+ Font.ITALIC, 28); 1.创建Font类的对象

  10. 利用Graphics类的setFont()方法确定使用定义的字体g.setFont(myFont); 给某个GUI部件设定字体可以使用该部件的setFont()方法。例如:Button btn=new Button("确定"); btn.setFont(myFont); 使用getFont()方法返回当前的Graphics对象或GUI部件使用的字体。 2.给图形对象或GUI部件设置字体

  11. 用getFontMetrics(Font)方法得到一个FontMetrics对象引用。 int stringWidth(String str) 返回给定字符串所占宽度 int getHeight() 获得字体的高度 int charWidth(char ch) 返回给定字符的宽度 用FontMetrics类获得字体的更多信息

  12. import java.awt.*;public class FontDemo extends java.applet.Applet{ public void paint(Graphics g){ String str="欢迎您!"; Font f=new Font("黑体" , Font.PLAIN , 24); g.setFont(f); FontMetrics fm = getFontMetrics(f); int x =(getWidth() - fm.stringWidth(str))/2; int y = getHeight() / 2; g.drawString(str,x,y); }} 例6-3 在Applet的中央显示"欢迎您!" 返回部件的高度

  13. public Color(int Red, int Green, int Blue) 每个参数的取值范围在0~255之间 public Color(float Red, float Green, float Blue) 每个参数的取值范围在0.0~1.0之间 public Color(int RGB) 类似HTML网页中用数值设置颜色。 Color类构造方法

  14. 2.颜色常量

  15. setColor(Color.blue); //将画笔定为兰色 getColor( )--获取当前的绘图颜色。 Component类中定义方法 setBackground()方法----设置组件的背景色 setForeground()方法 ----设置组件的前景色 getBackground( ) getForeground() 思考: 设置一个红色按钮,上面写黄色字如何实现? 3. 常用方法

  16. <applet code="My_param.class" height=200 width=300> <param name="vs" value="可变大小的字符串"> <param name="size" value=24> </applet> 在Applet利用 getParamter("参数名")方法获取 HTML传递的参数值。 6.4.1 在HTML文件中给Applet提供参数

  17. import java.applet.Applet; import java.awt.Graphics; public class My_param extends Applet { private String s = "" ; private int size; public void init() { s = getParameter("vs"); size = Integer.parseInt(getParameter("size")); } public void paint(Graphics g) { g.setFont(new Font("宋体",Font.PLAIN,size)); g.drawString(s,30, 40); } } 例 6-5 Applet参数的使用

  18. 将各种图形命令存储在字符串中,每条命令之间用符号“/”分隔,一条命令以一个识别符开头,后跟若干参数,命令和参数之间用“,”分隔。例如:以下为程序中的两条命令: rect,x,y,w,h //绘制矩形,其中,rect为命令识别符 oval,x,y,w,h //绘制椭圆,其中,oval为命令识别符 例6-6 利用Applet参数传递绘制图形信息

  19. <html> <body> <applet code="ParaDraw.class" width=200 height=200> <param name="graph" value="rect,10,20,100,110/oval,40,60,50,50/rect,20,30,110,120"> </applet> </body> </html> HTM文件

  20. import java.applet.Applet; import java.awt.Graphics; public class ParaDraw extends Applet { String graph; public void init() { graph=getParameter("graph"); } public void paint(Graphics g) { String para[]; int x,y,w,h; String commands[]=graph . split("/") ; 例6-6 利用Applet参数传递绘制图形信息

  21. for (int k=0;k<commands.length ;k++ ) { para=commands[k].split(",") ; if (para[0].equals("oval")) { x=Integer.parseInt(para[1]); y=Integer.parseInt(para[2]); w=Integer.parseInt(para[3]); h=Integer.parseInt(para[4]); g.drawOval(x,y,w,h); } 例6-6 利用Applet参数传递绘制图形信息(续1)

  22. else if (para[0].equals("rect")) { x=Integer.parseInt(para[1]); y=Integer.parseInt(para[2]); w=Integer.parseInt(para[3]); h=Integer.parseInt(para[4]); g.drawRect(x,y,w,h); } } } } 例6-6 利用Applet参数传递绘制图形信息(续2)

  23. public Image getImage(URL, String) 利用如下两个方法得到绝对地址: (1) getCodeBase():Applet字节码文件的URL地址。 (2) getDocumentBase() :html文件的URL地址。 1. 图像的获取 2.图像绘制 • public void drawImage(Image, x, y, imageObserver) 监视图象下载,获取图像的构建信息

  24. import java.awt.*; import java.applet.Applet; public class DrawMyImage extends Applet {Image myImage; public void init() { myImage = getImage(getDocumentBase(), "model2.GIF"); } public void paint(Graphics g) { g.drawImage(myImage, 0, 0, this); } } 例6-7 绘制一个图像

  25. import java.awt.*; import java.applet.*;public class mthread extends Applet {Image img; public void init() {  img = createImage(300,300);Graphics gimg = img.getGraphics();gimg.drawOval(60,60,100,100); gimg.fillOval (75,90,25,10); gimg.fillOval(120,90,25,10); gimg.drawLine(110,95,110,130); gimg.drawArc(85,110,50,40,0,-180); gimg.drawLine(110,130,100,120); } 3.利用双缓冲区绘图(例6-8 移动的笑脸 )

  26. public void paint(Graphics g) { for(int i = 0; i<700; i +=5) {for(int j = 0 ; j<9900000; j++) ;//起延时作用g.drawImage(img, i , 100, this); repaint(); } } } } 例6-8 移动的笑脸(续)

  27. import java.applet.*; import java.awt.*; public class ShowAnimator extends Applet{ Image[] m_Images; int totalImages = 18; int currentImage = 0; public void init(){ m_Images = new Image[totalImages]; for(int i=0; i<totalImages; i++) m_Images[i] = getImage(getDocumentBase(), "images\\img00" +(i+1)+".gif"); } 例6-9 通过图片的更换显示形成动画

  28. public void start() { currentImage = 0; } public void paint(Graphics g) { g.drawImage( m_Images[currentImage] ,10,10,this); currentImage = ++currentImage % totalImages; try{ Thread.sleep(2000); } catch(InterruptedException e) { } repaint();} } 例6-9 通过图片的更换显示形成动画(续) 开始第一张片 什么作用 休息 !! 与循环延时的差异?

  29. play(URL,String) play(URL) 方法1是将URL路径与声音文件名分开 方法2将文件名包含在URL中。例如:play(getDocumentBase(),"passport.mid"); 1.利用Applet类的play()方法直接播放

  30. AudioClip接口定义了声音文件的常用处理方法: public void play() 开始播放一个声音文件,本方法每次调用,都从头开始重新播放。 public void loop() 循环播放当前声音文件 public void stop() 停止播放当前声音文件 可以使用Applet类的 getAudioClip()方法 或new AudioClip()方法获取AudioClip类型的对象。 2. 使用AudioClip接口

  31. import java.applet.*; public class sounda extends Applet {  AudioClip ac;  public void init() {     ac = getAudioClip ( getCodeBase(), "sloop.au");  }  public void start() {  ac.loop();  }  public void stop() {  ac.stop();  }} 例6-10 在Applet中播放声音

  32. 1)根据Applet大小绘制若干同心圆,相邻圆之间的间距为10个象素。1)根据Applet大小绘制若干同心圆,相邻圆之间的间距为10个象素。 提示:通过getWidth()和getHeight()方法可获得applet的宽度和高度。 2)通过Applet参数提供两个间距在100以内的整数,找出这两个整数之间的所有素数,按每行5个在Applet画面上输出。 3) 在Applet画面中绘制一个19×19的围棋棋盘。棋盘的颜色由白(white)、灰(gray)、橘黄(orange)、浅红(pink)几种颜色中随机选取。 作业

More Related