java 程序设计 (04) java programming
DESCRIPTION
JAVA 程序设计 (04) JAVA Programming. 湖南大学软件学院 主讲:荣辉桂 Email [email protected]. 第 2 章 Java 小应用. 2.1 所有小应用程序的根源 2.2 小试身手 2.3 图形操作 2.4 URL 类 2.5 载入现有图像文件 2.6 声音处理 小 结. 2.1 所有小应用程序的根源. 2.1.1 小应用的特点 回忆一下小应用程序的书写格式 import java.applet.*; public class MyApplet extends Applet - PowerPoint PPT PresentationTRANSCRIPT
2
第 2 章 Java 小应用• 2.1 所有小应用程序的根源• 2.2 小试身手• 2.3 图形操作• 2.4 URL 类 • 2.5 载入现有图像文件• 2.6 声音处理• 小 结
3
2.1 所有小应用程序的根源• 2.1.1 小应用的特点• 回忆一下小应用程序的书写格式 import java.applet.*;
public class MyApplet extends Applet
{ ;}
• applet 都继承自 java.applet.Applet 类 , 由 Sun 公司事先定义好了 .
• 每个小应用都有一个主程序类 , 之前必须加上 public.
42.1 所有小应用程序的根源http://someLocation/file.html 1. Browser loads URL
<Html>
<Applet code= ….>
</Applet>
2. Browser loads
HTML document
Applet class3. Browser loads
applet classes
Location: http://someLocation/file.html
Loading...
4. Browser
run applet
5
2.1 所有小应用程序的根源• Applet 的限制
applet
BrowserSERVER本地程序
file
SERVERlocal
connection
connection
Applet 被下载的
与 applet 无关的本地方法
6
2.1 所有小应用程序的根源• 2.1.2 applet 的生命周期
• paint() 虽不在生命周期内 , 但它的作用相当于applet 的灵魂
Java.applet.Applet
public void init() public void destroy()
public void start() public void stop()
public void paint(Graphics g)
7
2.1 所有小应用程序的根源• 一个 applet 的可视周期
init
start
stop
destroy
离开 web 页面 重新装入或改变页面大小或返回 Web 页面
8
2.1 所有小应用程序的根源• 有关 paint() 方法• Applet 本身是一个容器 , 因此任何输出都必须
用图形方法 paint()• 当小应用首次被装载,以及每次窗口放大、
缩小、刷新时都要自动调用 paint 方法• paint() 是由浏览器调用的 , 而不是由程序调
用,当程序希望调用 paint 方法时,用 repaint 命令
• paint 方法的参数是 Graphics 类的对象 g ,它在 java.awt.Graphics 内
• paint ( Graphicd g ) { 。。。 }
9
2.1 所有小应用程序的根源AWT thread ( waiting )
update (){ clear arae call paint ()
paint ()
repaint() Exposure
10
2.2 小试身手• 2.2.1 起始页上的时间和日期
• 介绍两个类 :
1. 类名 :Date
创建一个实例 Date timeNow=new Date();
2. 类名 Font
创建一个实例 Font msgFont=new
Font(“TimesRoman”,Font.ITALIC,30);
0Mon Dec 07 14:23:50 GMT+08:00 1998
11
2.2 小试身手 看下面的例子 , 想一想生命周期的四个方法哪去了 ?import java.awt.*; import java.util.Date;
public class showDate extends java.applet.Applet
{ Date timeNow=new Date();
Font msgFont=new
Font(“TimesRoman”,Font.ITALIC,30);
public void paint(Graphics g)
{ g.setFont(msgFont);
g.setColor(Color.blue);
g.darwString(timeNow.toString(),5,50);
}
12
2.2 小试身手2.2.2 在起始页中加入 applet• html 中有关的代码 <APPLET CODE=“showdate.class” width=600 height=80> </APPLET>• CODEBASE 的作用 当 class 文件与起始页文件不在同一个目录下
时 , 使用 CODEBASE 说明 <APPLET CODE=“showdate.class” width=600 height=80> CODEBASE=“\myjava\class”</APPLET>
13
2.2 小试身手<APPLET CODE=“showdate.class” width=600 height=80> CODEBASE=“\myjava\class”</APPLET>
C:\
public
Index.html
myjava
class
showdate
javacode
C:\
public
Index.html
myjava
class
showdate
javacode
<APPLET CODE=“showdate.class” width=600 height=80></APPLET>
14
2.2 小试身手• ALIGN,HSPACE,VSPACE
Java applet
其它文字
其它文字vspace
hsp
ace
<APPLET CODE=“showdate.class” width=600 height=80>vspace=100 hspace=100</APPLET>
15
2.2 小试身手• 向 applet 传递参数的两个步骤 1. 在起始页中要有 <PARAM> 标签 2. 在 applet 中要有 getParameter 方法在起始页中有 :<applet code=showdate width=600 heigt=80><param name=rem value=“ 时间是 : ”></applet>在 applet 中有 :string title=getParameter(rem);在显示时间的命令中加入 title:g.drawString(title+timeNow.toString(),5,50);
16
2.2 小试身手import java.awt.*; import java.util.Date;
public class showDate extends java.applet.Applet
{ Date timeNow=new Date(); String title;
Font msgFont=new
Font(“TimesRoman”,Font.ITALIC,30);
public void init(){title=getParameter (“rem”); if (title==null) title=“”;}
public void paint(Graphics g){ g.setFont(msgFont); g.setColor(Color.blue); g.darwString(title+ timeNow.toString(),5,50);}
17
2.2 小试身手• 例 : 利用一个可以显示运行字符串的类 , 显示
自己的字符串 (htmlpara.html)
<applet code=htmlpara.class width=300 heigh=200>
<param name=MESSAGE value=”this is a test">
<param name=FONT value="BOLD">
<param name=POINT_SIZE value=20></applet>
18
2.2 小试身手public void init(){ String paramete;
parameter=getParameter("MESSAGE");if (parameter!=null)
message=parameter; parameter=getParameter("FONT");
if (parameter!=null)font_to_use=parameter;
parameter=getParameter("POINT_SIZE"); if (parameter!=null)
point_size=Integer.parseInt(parameter);}
19
2.3 图形处理2.3.1 图形坐标系统任何与绘图有关的操作第一个要用的是java.awt.Graphics 类Graphics 类的对象不是由 new 产生的 , 而是由系统或其他方式直接将生好的 Graphics 对象当作方法的参数 , 再交给程序设计者去处理 . 例如 :
paint(Graphics g)
x
y
0
20
2.3 图形处理• Graphics 的方法paint(Graphics g){ g.clearRect(); g.copyArea(); g.drawAre() ; g.drawLine(); g.drawOval();g.drawRect(); g.drawPolygon(); g.fillArc(); g.fillOval(); g.fillPolygen(); g.fillRect(); g.getColor();
g.getFont() g.setFont(); g.setColor(); g.getFontMetrics()
g.fillRoundRect()}
21
2.3 图形处理2.3.2 字型和颜色的设置2.3.2.1 字型设置的方法 Font font=new
Font(“TimesRoman”,Font.ITALIC,24);
g.setFont(font);
• 在小应用程序中显示输出的方法 g.drawString(String, int x, int y);
g.drawChars(char data[], int offset, int length,
int x, int y);
22
2.3 图形处理g.drawBytes(byte data[],int offset, int length,
int x, int y);
例 :g.drawString(“This is a test”,5,10);
• 获取字体的属性 Font font=g.getFont();
• Font 类中常用的方法 GetFamily() getName() getSize() getStyle()
isItalic() isPlain() isBold() toString()
23
2.3 图形处理import java.awt.Graphics; import java.awt.Font;
public class drawtext extends java.applet.Applet
{ Font fn=new Font("TimesRoman",Font.ITALIC,20);
public void paint(Graphics g)
{ g.setFont(fn);
g.drawString(”Font demo”,5,10);
}
}
Font demo
24
2.3 图形处理•获取更详细的数据 请查阅有关 FontMetrics 类的方法 fontMetrics=getFontMetrics(font);
•FontMetrics 中比较重要的方法有 :
stringWidth, charWidth, getAscent, getDescent,
getLeading, getHeigh
25
2.3 图形处理2.3.2.2 颜色的调整• Color 对象的使用 创造自己的颜色 : Color mycolor=new Color(int red, int blue, int green);
• g.setColor(Color.yellow)
• g.setColor(mycolor);
• 例 : 随机产生颜色 , 并画圆
26
2.3 图形处理import java.awt.Graphics; import java.awt.Color;
public class drawcircle extends java.applet.Applet {
public void paint(Graphics g) {
int red,green,blue,x;
for (x=0;x<370;x+=30){
red=(int)Math.floor(Math.random()*256);
green=(int)Math.floor(Math.random()*256);
blue=(int)Math.floor(Math.random()*256);
g.setColor(new Color(red,green,blue));
g.fillOval(x,0,30,30); }}}
27
2.4 URL 类• java.net.URL 类和 java.net.URLConnction
类使编程人员能很方便地利用 URL 在 Internet 上进行网络通信。
• URL 类定义了 WWW 的一个统一资源定位器和可以进行的一些操作。由 URL 类生成的对象指向 WWW 资源(如 WEB 页,图形图像文件,音频,视频等)
• URL 类有以下几种方式创建 URL 对象:• URL(String,String,int,String)• URL(String,String,String) 使用默认端口号• URL(URL,String) 和 URL(String)
28
2.4 URL 类2.4.2 构造 URL 类 (全名 java.net.URL)• 绝对 URL 的构造方法 :
URL(String spec)
例 : URL url=new URL
(http://www.hit.edu.cn/cv/index.html”)• 相对 URL 的构造方法 :
某绝对地址 :http://rainy.hit.edu.cn/test.html 在该目录下有两个文件 mywork.html myfamily.html
29
2.4 URL 类 URL base=new URL(“http://rainy.hit.edu.cn”);
URL url1=new (base, “mywork.html”);
URL url2=new (base, “mywork.html”);
• 其他 URL 的构造方法 :
URL url=new URL
(“http”, “www.hit.edu.cn”,“/~dyf/test.html”);
30
2.4 URL 类2.4.3 获取小应用程序 HTML 页面的 URL 和
小应用程序本身的 URL• URL html=getDocumentBase();• System.out.print(html);• URL codebase=getCodeBase();• System.out.print(codebase);
浏览器 服务器
html
applet
web page
31
2.4 URL 类• 2.4.4 URL异常 (MalformedURLException)
当创建 URL 时发生错误 , 系统会产生异常,这是非运行时异常,必须在程序中捕获处理。
try{ URL url=new URL(str);
}catch(MalformedURLException( e)
{ DisplayErrorMessage();}
• 2.4.5 URL 类的基本方法 String getProtocol(), 获取该 URL 的协议名 String getHost(), 获取该 URL 的主机 ing getPort(), String getFile(),
String getRef()
32
2.4 URL 类• 构造 URL 的实例import java.net.URL;import java.net.MalformedURLException;public class Test{ URL url1,url2,url3; void test() { try { url1= new URL(“file:/D:/image/example.gif”); url2= new URL(“http://www.hit.edu.cn/cv/”); url1= new URL(url2, “hit.gif”); }catch (MalformedURLException e); // 处理例外 } } }
33
2.5 载入现有图像文件Image 类• java支持 gif 和 jpg 两种格式的图像• 图像文件的 URL:
URL picurl= new URL
(“http://xxx.yyy.edu/Applet/img1.gif”);
• 取一幅图像构成图像对象 Image img1 = getImage(picurl);
Image img2 =
getImage(getCodeBase(), “img2.gif”);
34
2.5 载入现有图像文件• 显示一幅图像 :
g.drawImage(img1, x, y, this);
g.drawImage(img1, x, y,Color.red, this);
g.drawImage(image1, x, y,x2,y2,Color.red, this);
规定背景规定尺寸
35
2.5 载入现有图像文件• 完整的过程不要忘记 AWT包
定义 Image 对象了吗?指定图像的 URL 了吗 ?
把图像取出来吧 .
还记得画图像用什么方法和命令吗 ?
在类中
在 init0 中
在 paint0 中
36
2.5 载入现有图像文件import java.applet.*;import java.awt.*;import java.awt.Image;public class image extends Applet { Image img; public void init() { img=getImage(getCodeBase(),“win2000.gif");} public void paint(Graphics g) { int width=img.getWidth(this);
int height=img.getHeight(this); g.drawRect(52,52,width+30,height+30);g.drawImage(img,57,57,width+20,height+20,this);}}
37
2.6 声音处理• Java提供了一个处理数字音频功能强大的类库——
javax.sound ,使 java 能处理、播放声音或数字音频文件。这就使得大家在浏览网页的时候,不但可以欣赏动画,还可以欣赏音乐。
• Java语言中支持的声音格式有: AU,WAV,MIDI 等。而且只有在 Applet 中才能播放声音。
• 有两种方法可以在 Applet 中播放声音,利用 Applet提供的 play ()方法直接播放声音。
• Public void play(URL url)• Public void play(URL url ,String name)• For Example
38
小 结• 线程是实现动态效果的核心 , 运行线程必须继
承 Thread 类或者实现 Runable 接口 .• run 是线程的主体 , 它反复调用 repaint() 方
法 , 其中必须有休眠 sleep().• sleep()语句要捕获中断异常 (后面讲 )• try{Thread.sleep(100);}• catch(InterruptedException e) {}• 有时候 ,start(),stop() 方法必须重写 .• 需要获取网络资源时 (包括本地资源 ), 要用 U
RL 类
39
下课了。。。
追求
休息一会儿。。。