語言程序設計資料:JAVA小程序舉例_第1頁
語言程序設計資料:JAVA小程序舉例_第2頁
語言程序設計資料:JAVA小程序舉例_第3頁
語言程序設計資料:JAVA小程序舉例_第4頁
語言程序設計資料:JAVA小程序舉例_第5頁
已閱讀5頁,還剩63頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領

文檔簡介

1、1,第2章 Java小應用,2,第2章 Java小應用,2.1 所有小應用程序的根源 2.2 小試 2.3 圖形操作 2.4 URL類 2.5 載入現(xiàn)有圖像文件 2.6 動畫效果 2.7 播放聲音 2.8 小 結(jié),3,2.1 所有小應用程序的根源,2.1.1 小應用的特點 回憶一下小應用程序的書寫格式 import java.applet.*; public class MyApplet extends Applet ; applet都繼承自java.applet.Applet類,由Sun公司事先定義好了. 每個小應用都有一個主程序類, 之前必須加上public,4,2.1 所有小應用程序的根

2、源,5,2.1 所有小應用程序的根源,Applet的限制,applet,Browser,SERVER,file,SERVER,local,connection,connection,Applet被下載的,與applet無關的,本地方法,6,2.1 所有小應用程序的根源,2.1.2 applet的生命周期 paint()雖不在生命周期內(nèi),但它的作用相當于applet的靈魂,Java.applet.Applet,public void init(,public void destroy(,public void start(,public void stop(,public void paint(

3、Graphics g,7,2.1 所有小應用程序的根源,一個applet的可視周期,init,start,stop,destroy,離開web頁面,重新裝入或改變頁面大小或返回Web頁面,8,2.1 所有小應用程序的根源,有關paint()方法 Applet本身是一個容器,因此任何輸出都必須用圖形方法paint() 當小應用首次被裝載,以及每次窗口放大、縮小、刷新時都要調(diào)用paint方法 paint()是由瀏覽器調(diào)用的, 而不是由程序調(diào)用,當程序希望調(diào)用paint方法時,用repaint命令 paint方法的參數(shù)是Graphics類的對象 g,它在java.awt.Graphics內(nèi) pain

4、t(Graphicd g,9,2.1 所有小應用程序的根源,AWT thread(waiting,update() clear arae call paint(,paint(,repaint(,Exposure,10,2.2 小試身手,2.2.1 起始頁上的時間和日期 介紹兩個類: 1. 類名:Date 創(chuàng)建一個實例 Date timeNow=new Date(); 2. 類名Font 創(chuàng)建一個實例 Font msgFont=new Font(“TimesRoman”,Font.ITALIC,30,0Mon Dec 07 14:23:50 GMT+08:00 1998,11,2.2 小試身手,

5、看下面的例子,想一想生命周期的四個方法哪去了,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,5

6、0);,12,2.2 小試身手,2.2.2 在起始頁中加入applet html中有關的代碼 CODEBASE的作用 當class文件與起始頁文件不在同一個目錄下時,使用CODEBASE說明 CODEBASE=“myjavaclass,13,2.2 小試身手,CODEBASE=“myjavaclass”,14,2.2 小試身手,ALIGN,HSPACE,VSPACE,Java applet,其它文字,其它文字,vspace,hspace,vspace=100 hspace=100,15,2.2 小試身手,向applet傳遞參數(shù)的兩個步驟 1. 在起始頁中要有標簽 2. 在applet中要有g(shù)e

7、tParameter方法 在起始頁中有: 在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

8、”,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),18,2.2 小試身手,public void init() Stri

9、ng 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圖形坐標系統(tǒng) 任何與繪圖有關的操作 第一個要用的是

10、java.awt.Graphics類 Graphics類的對象不是 由new產(chǎn)生的,而是由系 統(tǒng)或其他方式直接將生好的Graphics對象當作方法的參數(shù),再交給程序設計者去處理.例如: paint(Graphics g,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.f

11、illRect(); g.getColor(); g.getFont() g.setFont(); g.setColor(); g.getFontMetrics() g.fillRoundRect(),21,2.3 圖形處理,2.3.2 字型和顏色的設置 字型設置的方法 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

12、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

13、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 圖形處理,獲取更詳細的數(shù)據(jù) 請查閱有關FontMetrics類的方法 fontMetrics=getFontMetrics(font); FontMetrics中比較重要的方法有: string

14、Width, charWidth, getAscent, getDescent, getLeading, getHeigh,25,2.3 圖形處理, 顏色的調(diào)整 Color對象的使用 創(chuàng)造自己的顏色: Color mycolor=new Color(int red, int blue, int green); g.setColor(Color.yellow) g.setColor(mycolor); 例:隨機產(chǎn)生顏色,并畫圓,26,2.3 圖形處理,import java.awt.Graphics; import java.awt.Color; public class draw

15、circle extends java.applet.Applet public void paint(Graphics g) int red,green,blue,x; for (x=0;x370;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類

16、,2.4.2 構(gòu)造URL類 (全名java.lang.URL) 絕對URL的構(gòu)造方法: URL(String spec) 例: URL url=new URL (http:/ 相對URL的構(gòu)造方法: 某絕對地址:http:/ 在該目錄下有兩個文件 mywork.html myfamily.html,28,2.4 URL類,URL base=new URL(“http:/”); URL url1=new (base, “mywork.html”); URL url2=new (base, “mywork.html”); 其他URL的構(gòu)造方法: URL url=new URL (“http”, “

17、”,“/dyf/test.html”,29,2.4 URL類,2.4.3 獲取小應用程序HTML頁面的URL和小應用程序本身的URL URL html=getDocumentBase(); System.out.print(html); URL codebase=getCodeBase(); System.out.print(codebase,30,2.4 URL類,2.4.4 URL異常(MalformedURLException) 當創(chuàng)建URL時發(fā)生錯誤,系統(tǒng)會產(chǎn)生異常 try URL url=new URL(str); catch(MalformedURLException( e) Di

18、splayErrorMessage(); 2.4.5 URL類的基本方法 String getProtocol(), String getHost(), ing getPort(), String getFile(), String getRef(,31,2.4 URL類,構(gòu)造URL的實例,import .URL; import .MalformedURLException; public class Test URL url1,url2,url3; void test() try url1= new URL(“file:/D:/image/example.gif”); url2= new U

19、RL(“http:/ url1= new URL(url2, “hit.gif”); catch (MalformedURLException e); /處理例外,32,2.5 載入現(xiàn)有圖像文件,Image類 java支持gif和jpg兩種格式的圖像 圖像文件的URL: URL picurl= new URL (“/Applet/img1.gif”); 取一幅圖像構(gòu)成圖像對象 Image img1 = getImage(picurl); Image img2 = getImage(getCodeBase(), “img2.gif”,33,2.5 載入現(xiàn)有圖像文

20、件,顯示一幅圖像: g.drawImage(img1, x, y, this); g.drawImage(img1, x, y,Color.red, this); g.drawImage(image1, x, y,x2,y2,Color.red, this,規(guī)定背景,規(guī)定尺寸,34,2.5 載入現(xiàn)有圖像文件,完整的過程,在類中,在init0中,在paint0中,35,2.5 載入現(xiàn)有圖像文件,import java.applet.*;import java.awt.*; public class image extends Applet Image img; public void init(

21、) img=getImage(getCodeBase(),img0001.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,36,2.6 動態(tài)效果-線程的應用,2.4 動態(tài)效果-線程的應用 什么是線程? 線程是執(zhí)行中的程序中的單個順序控制流. Java支持多線程,37,2.6 動態(tài)效果

22、-線程的應用,靜態(tài)的情況 import java.applet.*; import java.awt.Graphics; public class maguee extends Applet public void paint(Graphics g) g.drawString(Hello, Java!,0,0);,38,2.6 動態(tài)效果-線程的應用,動態(tài)的情況(不是多線程) public void init() x=size().width; y=size().height/2; width=x; public void paint(Graphics g) while(true) g.draw

23、String(Hello, Java!,x,y); x-=10; if(x0)x=width;,39,2.6 動態(tài)效果-線程的應用,實現(xiàn)一個線程,讓Applet類去實現(xiàn)Runable接口,創(chuàng)建一個線程類,改寫方法start,在其中產(chǎn)生一個新的線程來工作,改寫stop方法,在其中編寫結(jié)束線程的程序代碼,引入新的方法,將分給線程的工作寫到run中,40,2.6 動態(tài)效果-線程的應用,第一步:實現(xiàn)Runable接口 public class xc extends java.applet.Applet implements Runnable Thread smallthread=null; Threa

24、d是一個類,只有是它的實例才能具有線程的功能 主函數(shù)中要定義一個線程變量,41,2.6 動態(tài)效果-線程的應用,第二步:改寫方法start public void start() if(smallthread = null) smallthread= new Thread(this); smallthread.start(); /從現(xiàn)在開始程序由兩個線程在執(zhí)行 第三步:改寫stop方法 public void stop() smallthread.stop(); /停止線程 smallthread = null; /釋放線程對象,42,2.6 動態(tài)效果-線程的應用,第四步:新的方法run 將讓線

25、程要做的事放run中 public void run() while (true) repaint(); try Thread.sleep(1000); catch(InterruptedException e),43,2.6 動態(tài)效果-線程的應用,import java.applet.*; import java.awt.Graphics; public class MovingCharacter extends Applet implements Runnable int x=200; Thread my_thread=null; /- public void start() my_thr

26、ead=new Thread(this); my_thread.start();,public void run() while(true) repaint(); try Thread.sleep(100); catch(InterruptedException e),44,2.6 動態(tài)效果-線程的應用,public void paint(Graphics g) g.drawString(Hello, Java!,x,y); x-=10; if(x0)x=200;,public void stop() my_thread.stop();,45,2.6 動態(tài)效果-線程的應用,跳動的小球,up=f

27、alse; x=x-10; if(xheight) up=false; g.setColor(Color.red); g.fillOval(x,y,30,30,46,2.6 動態(tài)效果-線程的應用,例:起始頁上的小時鐘 一個必須用到的類-Date類,給出系統(tǒng)時間 Date NowTime=new Date(); NowTime.getHours(), NowTime.getMinutes() 自己需要寫什么樣的類? Clock-把數(shù)字時間成圖形表示,Hour*60*60+minute*60+second)/43200*2.0*PI,minute*60+second)/3600*2.0*PI,se

28、cond/60*2.0*PI,47,2.6 動態(tài)效果-線程的應用,48,2.6 動態(tài)效果-線程的應用,class Clock int hours,minutes,second,radius; Clock(int hrs,int min,int sec) hours=hrs%12; minutes=min; second=sec; void show(Graphics g, int x, int y,int redius) int hrs_len=(int)(radius*0.5); int min_len=(int)(radius*0.7); int sec_len=(int)(radius*

29、0.85); double theta; g.drawOval(x ,y, radius*2, radius*2,49,2.6 動態(tài)效果-線程的應用,theta=(double)(hours*60*60+minutes*60+second)/ 43200.0*2.0*Math.PI; drawNiddle(g,Color.blue, x, y, hrs_len, theta); theta=(double)(minutes*60-second)/3600.0*2.0*Math.PI; drawNiddle(g,Color.blue, x, y, min_len,theta); theta=(d

30、ouble)second/60.0*2.0*Math.PI; drawNiddle(g,Color.red, x, y, sec_len, theta);,50,2.6 動態(tài)效果-線程的應用,private void drawNiddle(Graphics g, Color c, int x, int y, int len, double theta) g.setColor(c); g.drawLine(x,y,(int)(x+len*Math.sin(theta), (int)(y-len*Math.cos(theta);,51,2.6 動態(tài)效果-線程的應用,import java.awt.

31、*;import java.util.Date; public class ClockDemo extends java.applet.Applet public void paint() Date timeNow = new Date(); Clock myClock = new Clock(timeNow.getHours(), timeNow.getMinutes(), timeNow.getSeconds(); myClock.show(g,100,100,100);,52,2.6 動態(tài)效果-線程的應用,生成時間對象,取時間 生成Clock對象,將時間傳遞給Clock對象,paint(

32、),主類,53,2.6 動態(tài)效果-線程的應用,主類,start(,stop(,run(,paint(,換算弧度,畫圖,clock類,clock(,初始化,Show(,drawNiddle(,啟動新線程,停止線程,生成clock類實例,repaint(,54,2.6 動態(tài)效果-線程的應用,例: 在主頁上顯示 字符串并且顏色從左至右不斷變化 讓我們來想一想: 需要那些數(shù)據(jù)成員? String msg, Font fnt, Color clr, spot_clr; Thread thread; String Msg=Welcome to HIT; 需要哪些方法? init, start, stop,

33、 run, paint; public void init() fnt= new Font(TimeRoman,Font.PLAIN,30); clr=new Color(255,0,0); spot_clr=new Color(0,0,255); Thread thread,55,2.6 動態(tài)效果-線程的應用,run()中做什么? 反復調(diào)用repaint public void run() while(true) repaint(); tryThread.sleep(50); catch(InterruptedException e),56,2.6 動態(tài)效果-線程的應用,paint()中做什

34、么? 輸出兩次字符串,第一次用一種顏色,第二次用另一種顏色(該顏色只作用于指定的區(qū)域 ) g.clipRect(x,y,width,height) public void paint(Graphics g) FontMetrics fntM=g.getFontMetrics(); int font_height=fntM.getHeight(); int base_line=size().height/2+font_height/2,You are Welcome to HIT,57,2.6 動態(tài)效果-線程的應用,g.setFont(fnt); g.setColor(clr); g.drawS

35、tring(Msg,0,base_line); g.clipRect(strPt-50,0,str_bk_size,size().height); g.setColor(spot_clr); g.drawString(Msg,0,base_line); strPt=(strPt+1)%(size().width+100);,58,2.6 動態(tài)效果-線程的應用,在Java中播放動畫 1.需要多張圖片 2 調(diào)用圖片的方法? getImage, 3.將多幅圖像存入圖像對象數(shù)組 Image frame=new Image10; for (int i=0;iframe.length;i+) framei

36、=getImage(getCodeBase(), “pic”+i+ “.gif”); 4. 顯示圖像 drawImage(x,y,0,0,this,59,2.6 動態(tài)效果-線程的應用,import java.awt.*; public class nina extends java.applet.Applet implements Runnable Image frame; Thread threadNina; int frame_i; int delay_time; public void init() frame=new Image10; threadNina=null; frame_i=

37、0; for (int i=0;iframe.length;i+) framei=getImage(getCodeBase(), pic+i+ .gif);,60,2.6 動態(tài)效果-線程的應用,public void paint(Graphics g) g.drawImage(frameframe_i,0,0,this,public void run() while(true) repaint(); try Thread.sleep(100); catch(InterruptedException e) frame_i=(frame_i+1)%frame.length;,61,2.7 播放聲音

38、,java支持au格式的聲音 兩個方法: void play(URL url) void play(URL url, String name) 例:play(getCodeBase(), “boing.au”); (注:它是一次性的) 如果想反復播放怎么辦? 借用類AudioClip(loop(),play(),stop(,62,2.7 播放聲音,例:AudioClip bg_sound= getAudioClip(getCodeBase(), “boing.au”); bg_sound.play(); 或: bg_sound.loop(,import java.applet.AudioClip; public class audio extends java.applet.Applet AudioClip sound=getAudioClip(getCodeBase(),boing.au); public void start() my_sound.loop(); public void stop() if(my_sound!=null) my_sound.stop(,63,2.7 播放聲音,圖像加聲音豈不是更有吸引力 1. 在init中既取圖像也取聲音片斷 framei=getImage(getCodeBase(), img00

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論