文件接收柜tcp課程源碼教程-經(jīng)典_第1頁(yè)
文件接收柜tcp課程源碼教程-經(jīng)典_第2頁(yè)
文件接收柜tcp課程源碼教程-經(jīng)典_第3頁(yè)
文件接收柜tcp課程源碼教程-經(jīng)典_第4頁(yè)
文件接收柜tcp課程源碼教程-經(jīng)典_第5頁(yè)
已閱讀5頁(yè),還剩38頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

第十三章多媒體技術(shù)13.1圖像處理13.2聲音文件的播放13.3用Java實(shí)現(xiàn)動(dòng)畫(huà)13.4利用JMF來(lái)播放視頻13.1在Applet中圖像的繪制13.1.1圖像文件的裝載getImage()方法 Applet類(lèi)中提供了getImage()方法用來(lái)將準(zhǔn)備好的圖像文件裝到applet中 ImagegetImage(URLurl)ImagegetImage(URLurl,Stringname)我們必須首先指明圖像文件所存貯的位置.Java采用URL(UniversalResourceLocation,統(tǒng)一資源定位器)來(lái)定位圖像文件的網(wǎng)絡(luò)位置。絕對(duì)URL形式指明了網(wǎng)絡(luò)資源的全路徑名相對(duì)URL形式,分別由基準(zhǔn)URL(即baseURL)再加上相對(duì)于基準(zhǔn)URL下的相對(duì)URL這兩部分組成(2)URL的獲取構(gòu)造方法:URL(Stringspec)URL(URLcontext,Stringspec)Applet類(lèi)中提供了兩個(gè)方法來(lái)幫助我們方便地獲取基準(zhǔn)URL對(duì)象,它們的調(diào)用格式如下:

URLgetDocumentBase()URLgetCodeBase()其中g(shù)etDocumentBase()方法返回的基準(zhǔn)URL對(duì)象代表了包含該applet的HTML文件所處的目錄,而getCodeBase()方法返回的基準(zhǔn)URL對(duì)象代表了該applet文件(.class文件)所處的目錄。Imageimg=getImage(getDocumentBase(),"images/m1.gif");13.1.2圖像文件的顯示Graphics類(lèi)提供了一個(gè)drawImage()方法,它能完成將Image對(duì)象中的圖像顯示在屏幕的特定位置上,就象顯示文本一樣方便。drawImage()方法的調(diào)用格式如下:booleandrawImage(Imageimg,intx,inty,ImageObserverobserver)其中img參數(shù)就是要顯示的Image對(duì)象。x和y參數(shù)是該圖像左上角的坐標(biāo)值。observer參數(shù)則是一個(gè)ImageObserver接口(interface),它用來(lái)跟蹤圖像文件裝載是否已經(jīng)完成的情況,通常我們都將該參數(shù)置為this,即傳遞本對(duì)象的引用去實(shí)現(xiàn)這個(gè)接口。booleandrawImage(Imageimg,intx,inty,intwidth,intheight,ImageObserverobserver)width和height,即表示圖像顯示的寬度和高度booleandrawImage(Image1,x,y,Color1,this);booleandrawImage(Image1,x,y,width,heigh,Color1,this);實(shí)例:importjava.awt.Graphics;importjava.awt.Image;publicclassno74extendsjava.applet.Applet{Imageimg;publicvoidinit(){img=getImage(getCodeBase(),"boy.gif");}

publicvoidpaint(Graphicsg){intw=img.getWidth(this);inth=img.getHeight(this);g.drawImage(img,20,10,this);//原圖g.drawImage(img,200,10,w/2,h/2,this);//縮小一半g.drawImage(img,20,200,w*2,h/3,this);//寬扁圖g.drawImage(img,350,10,w/2,h*2,this);//瘦高圖}}13.2聲音文件的播放(*.au)13.2.1獲得聲音文件AudioClipgetAudioClip(URLurl)AudioClipgetAudioClip(URLurl,Stringname)13.2.2聲音文件的播放voidplay(URLurl)voidplay(URLurl,Stringname)loop()方法重復(fù)播放stop()方法結(jié)束放音實(shí)例:importjava.applet.AudioClip;publicclassAudiosextendsjava.applet.Applet{AudioClipbgmusic,speak;publicvoidinit(){bgmusic=getAudioClip(getDocumentBase(),"space.au");speak=getAudioClip(getDocumentBase(),"intro.au");}publicvoidstart(){if(bgmusic!=null)bgmusic.loop();if(speak!=null)speak.play();}publicvoidstop(){if(bgmusic!=null)bgmusic.stop();}}//關(guān)閉背景音樂(lè)在Application中圖像的繪制img=Toolkit.getDefaultToolkit().getImage("boy.gif");publicclassdrawImageFrameextendsFrame{Imageimg;publicdrawImageFrame(Stringss){super(ss);img=Toolkit.getDefaultToolkit().getImage("boy.gif");setSize(600,300);setVisible(true);}

publicvoidpaint(Graphicsg){intw=img.getWidth(this);inth=img.getHeight(this);g.drawImage(img,20,10,this);//原圖g.drawImage(img,200,10,w/2,h/2,this);//縮小一半g.drawImage(img,280,10,w*2,h/3,this);//寬扁圖g.drawImage(img,400,10,w/2,h*2,this);//瘦高圖}……..}在JavaAplication中播放聲音因?yàn)锳udioClip類(lèi)及其getAudioClip()方法都是屬于java.applet包的,它在Application中無(wú)法調(diào)用。解決的方法是使用一些Sun在JDK中發(fā)布但未正式注明的特點(diǎn),即在/sun/audio目錄下的sun.audio包也提供了類(lèi)似的方法。下面是實(shí)現(xiàn)的代碼:importsun.audio.*;//引入sun.audio包importjava.io.*;InputStreamin=newFileInputStream(filename);//打開(kāi)一個(gè)聲音文件作為輸入AudioStreamas=newAudioStream(in);//用輸入六創(chuàng)建一個(gè)AudioStream對(duì)象AudioPlayer.player.start(as);//player是AudioPlayer中一個(gè)靜態(tài)成員用于控制播放AudioPlayer.player.stop(as);//當(dāng)需從網(wǎng)上下載文件進(jìn)行播放時(shí),用以下代碼打開(kāi)音樂(lè)文件的網(wǎng)址:AudioStreamas=newAudioStream(url.openStream());//以下是播放一個(gè)持續(xù)的聲音的代碼:importsun.audio.*;//引入sun.audio包importjava.io.*;AudioStreamas=newAudioStream(url.openStream());AudioDatadata=as.getData();創(chuàng)建AudioData源ContinuousAudioDataStreamcas=newContinuousAudioDataStream(data);AudioPlayer.player.start(cas);AudioPlayer.player.stop(cas);publicAudioFrame(Stringss){super(ss);try{InputStreamin=newFileInputStream("space.au");AudioStreamas=newAudioStream(in);AudioPlayer.player.start(as);}catch(Exceptione){}}13.3用線程實(shí)現(xiàn)動(dòng)畫(huà)使用兩個(gè)線程,一個(gè)是原來(lái)的程序,另一個(gè)用來(lái)處理執(zhí)行無(wú)窮循環(huán)的部分.(1)定義Applet類(lèi),實(shí)現(xiàn)Runnable接口publicclass...extendsAppletimplementsRunnable{(2)init()方法------不改變(3)start()方法中,創(chuàng)建一個(gè)動(dòng)畫(huà)線程并啟動(dòng)publicvoidstart(){if(runner==null);{runner=newThread(this); //newThreadrunner.start();}}(4)stop()方法,停止線程publicvoidstop(){if(runner!=null){//runner.stop();runner=null;}}(5)run()方法,控制動(dòng)畫(huà)循環(huán)。每次循環(huán),調(diào)用repaint(),重繪刷新.publicvoidrun(){while(Thread.currentThread()==runner){ //幀號(hào)或其他標(biāo)志變量變化 repaint();}(6)paint()方法publicvoidpaint(Graphicsg){//根據(jù)幀號(hào)或其他標(biāo)志變量變化繪制}importjava.awt.*;importjava.applet.*;publicclassdonghuawordextendsAppletimplementsRunnable{publicThreadrunner;publicintxpos;publicvoidinit(){xpos=600;setBackground(Color.white);}publicvoidstart(){if(runner==null);{runner=newThread(this); //newThreadrunner.start();}}publicvoidstop(){if(runner!=null){//runner.stop();runner=null;}}publicvoidrun(){while(Thread.currentThread()==runner){ xpos=xpos-10; if(xpos==20) xpos=600; repaint();//no.1statement try{Thread.sleep(200);} //wait catch(InterruptedExceptione){} }}publicvoidpaint(Graphicsg){g.setColor(Color.white);g.fillRect(xpos,10,200,150);g.setColor(Color.red);g.setFont(newFont("TimesRoman",Font.BOLD,48));g.drawString("Hello!",xpos,60);}publicvoidupdate(Graphicsg){paint(g); }}8.3消除動(dòng)畫(huà)的閃爍 每幀圖象消失后在人的視野里只能保持幾十毫秒的時(shí)間。在動(dòng)畫(huà)實(shí)現(xiàn)時(shí),如果從前一幀圖象消失到下一幀圖象繪制完成這一段時(shí)間超過(guò)了這幾十毫秒,就會(huì)讓人產(chǎn)生閃爍感。我們可用兩種方法來(lái)減少閃爍,一種是重載update()方法,一種是使用雙緩沖技術(shù)。(1)重載update()方法當(dāng)AWT接到一個(gè)重繪請(qǐng)求時(shí),就調(diào)用update()方法.在缺省情況下,該方法會(huì)清除整個(gè)背景,再調(diào)用paint().在實(shí)際情況中,沒(méi)有必要將整個(gè)背景清除,只需將前一幀與當(dāng)前幀的不同之處清除.重載update()方法,完全接管動(dòng)畫(huà)幀的清除和顯示工作。也就是說(shuō),將原來(lái)的update()方法的清除代碼和在paint()方法中的繪圖方法都包含在新的update()方法中,從而避免了每次重繪時(shí)將整個(gè)區(qū)域清除。publicvoidpaint(Graphicsg){//清除前一幀與當(dāng)前幀的不同之處 //繪出本幀;}publicvoidupdate(Graphicsg){paint(g); }(2)雙緩沖技術(shù)在顯示一幅圖象時(shí),如果繪圖指令過(guò)多,則有可能使這幀圖象的顯示無(wú)法在一個(gè)顯示器刷新周期內(nèi)完成。這樣,也會(huì)造成閃爍感。我們可用一個(gè)數(shù)組來(lái)虛擬一個(gè)“顯示器”,將一幀圖象先繪制在這個(gè)虛擬的“顯示器”上,待整個(gè)圖象繪制完畢,再一次性顯示到屏幕上去。這樣,因?yàn)槔L制圖象的操作都是對(duì)內(nèi)存的操作,只需在最后才訪問(wèn)一次顯示器,所以圖象的顯示速度得到很大提高。但是要占用一定的內(nèi)存.在Java中,這個(gè)虛擬的顯示器被封裝在一個(gè)Image對(duì)象中.(1)說(shuō)明Image對(duì)象和Graphics對(duì)象publicImageoffscreenImg;publicGraphicsoffscreenG;(2)在init()方法中,創(chuàng)建Image對(duì)象和Graphics對(duì)象offscreenImg=createImage(600,600);offscreenG=offscreenImg.getGraphics();(3)在paint()方法中,調(diào)用Graphics對(duì)象的繪圖方法,把圖形繪到Image中.offscreenG.setColor(Color.white);offscreenG.fillRect();(4)將圖形一次性繪到顯示器上.g.drawImage(offscreenImg,0,0,this);完整的動(dòng)畫(huà)程序框架:importjava.awt.*;importjava.applet.*;publicclass...extendsAppletimplementsRunnable{Colornowcolor=Color.black;publicThreadrunner;publicImageoffscreenImg;publicGraphicsoffscreenG;publicvoidinit(){offscreenImg=createImage(600,600);offscreenG=offscreenImg.getGraphics();}publicvoidstart(){if(runner==null);{runner=newThread(this); //newThreadrunner.start();}}publicvoidstop(){if(runner!=null){//runner.stop();runner=null;}}publicvoidrun(){ intspeed=200; //getspeed while(Thread.currentThread()==runner)…//幀號(hào)或其他變量變化 repaint(); try{Thread.sleep(speed);} //睡眠 catch(InterruptedExceptione){} }}publicvoidpaint(Graphicsg){offscreenG.setColor(Color.white);offscreenG.fillRect(…);//根據(jù)幀號(hào)或其他變量繪制圖形g.drawImage(offscreenImg,0,0,this);}//繪到顯示器上publicvoidupdate(Graphicsg){paint(g); }}實(shí)例:importjava.awt.*;importjava.applet.*;publicclassexa5extendsAppletimplementsRunnable{Colornowcolor=Color.black;//nowcolor:顏色變量publicThreadrunner;publicintxpos,tag=0;//xpos:坐標(biāo)變量tag:增大縮小標(biāo)記publicImageoffscreenImg;publicGraphicsoffscreenG;publicvoidinit(){setBackground(Color.white);setForeground(Color.white);xpos=50;offscreenImg=createImage(600,600);offscreenG=offscreenImg.getGraphics();}publicvoidstart(){if(runner==null);{runner=newThread(this); runner.start();}}

publicvoidstop(){if(runner!=null){runner.stop();runner=null;}}publicvoidrun(){ intspeed=200; //getspeed while(Thread.currentThread()==runner) {if(nowcolor==Color.black) nowcolor=Color.red; …… //顏色循環(huán)變化

if(tag==0) xpos=xpos+10; else xpos=xpos-10; if(xpos==200) tag=1; if(xpos==20) tag=0;//坐標(biāo)按從小到大,從大到小循環(huán)變化 repaint();//no.1statement try{Thread.sleep(speed);} //wait catch(InterruptedExceptione){} } }publicvoidpaint(Graphicsg){offscreenG.setColor(Color.white);offscreenG.fillRect(xpos-20,xpos-20,250,250);offscreenG.setColor(nowcolor);offscreenG.fillOval(xpos,xpos,xpos,xpos);g.drawImage(offscreenImg,0,0,this);}publicvoidupdate(Graphicsg){paint(g); }}圖象動(dòng)畫(huà)的設(shè)計(jì)一組圖象文件,一幀一幀地快速顯示.(1)定義一組Image對(duì)象及幀號(hào).(2)在init()方法中,獲取若干個(gè)Image對(duì)象.getImage(getDocumentBase(),文件名);(3)在run()方法中,根據(jù)幀號(hào)循環(huán)繪制圖片While(){幀號(hào)變化;repaint();延遲;}(4)在paint()方法中,根據(jù)幀號(hào)繪制一幅圖片.在Frame中播放動(dòng)畫(huà)publicclassdonghuaFrameextendsFrameimplementsRunnable{publicThreadrunner;publicintxpos,tag=0;publicdonghuaFrame(){setTitle("donghuaFrame");setSize(600,300);setVisible(true);xpos=600;if(runner==null);{runner=newThread(this);//newThreadrunner.start();}。。。}importjava.awt.*;importjava.applet.*;publicclassdonghuaimageextendsAppletimplementsRunnable{Imageframe[];publicThreadrunner;publicintframe_i=0;publicvoidinit(){inti=0;frame=newImage[5];for(i=0;i<frame.length;i++)frame[i]=getImage(getCodeBase(),"images"+i+".gif");}publicvoidstart(){if(runner==null);{runner=newThread(this); //newThreadrunner.start();}}publicvoidstop(){if(runner!=null){runner=null;}}publicvoidrun(){while(Thread.currentThread()==runner){ repaint(); try{Thread.sleep(500);} catch(InterruptedExceptione){} frame_i=(frame_i+1)%frame.length; } }publicvoidpaint(Graphicsg){g.drawImage(frame[frame_i],0,0,this);}}13.4利用JMF來(lái)播放視頻

13.4.1什么是JMFJava媒體框架(JavaMediaFrame,簡(jiǎn)稱(chēng)JMF)是一組用來(lái)播放、處理和捕捉媒體信息的API,這些API還可以用來(lái)傳送或接收實(shí)況媒體和召開(kāi)視頻會(huì)議。,為實(shí)現(xiàn)這種功能,JMF運(yùn)用RTP實(shí)時(shí)傳輸協(xié)議。JMF的API包括11個(gè)軟件包,主包為javax.media。JMF目前最新版本是JMF2.1.1c,下載網(wǎng)址是:

在下載JMF時(shí)有兩種選擇,即下載跨平臺(tái)的JMF或下載專(zhuān)用于 Solaris或Windows的性能包。如果下載的是專(zhuān)用于windows的性能包,則運(yùn)行jmf-2_1-win.exe來(lái)安裝Windows版的JMF。JMF提供了一個(gè)三層的體系結(jié)構(gòu):第一層為高級(jí)表現(xiàn)形式(播放器),作為一個(gè)應(yīng)用程序,用戶可以播放器來(lái)收看視頻。第二層為過(guò)程處理API,軟件開(kāi)發(fā)人員通過(guò)高級(jí)API進(jìn)行交互的應(yīng)用程序的開(kāi)發(fā)。第三層為低級(jí)插入式API,通過(guò)一種可以集成到體系結(jié)構(gòu)的插件,為整個(gè)體系結(jié)構(gòu)提供一種可擴(kuò)展的能力。播放視頻播放媒體就相應(yīng)地需要一個(gè)播放器,每個(gè)播放器從數(shù)據(jù)源接收數(shù)據(jù),然后立刻以精確的時(shí)間順序提交。一個(gè)播放器具有六種狀態(tài):Unrealized:當(dāng)一個(gè)播放器已被創(chuàng)建,并對(duì)即將要播放的媒體一無(wú)所知時(shí)的狀態(tài)。Realizing:調(diào)用了播放器的realize方法后,可以判定它的資源的 請(qǐng)求。Realized:當(dāng)Realizing過(guò)程結(jié)束后進(jìn)入該狀態(tài),已知道需要那些資源以及將要播放的媒體相關(guān)的類(lèi)型信息。Prefetching:當(dāng)播放器的prefetch方法被調(diào)用后進(jìn)入該狀態(tài),正準(zhǔn)備播放媒體數(shù)據(jù)。Prefetched:當(dāng)播放器的Prefetching操作完成后,進(jìn)入該狀態(tài),此時(shí)已準(zhǔn)備啟動(dòng)播放。Started:當(dāng)start方法調(diào)用后進(jìn)入該狀態(tài)。建立一個(gè)播放器的主要步驟如下:第一步:創(chuàng)建播放器。用javax.media包中的Manager類(lèi)的createPlayer方法創(chuàng)建一個(gè)Player對(duì)象。第二步:向播放器注冊(cè)一個(gè)控制器。Player提供一個(gè)實(shí)現(xiàn)ControllerListener接口的事件處理器,該接口有一個(gè)方法controllerUpdate(ControllerEventevent),當(dāng)媒體事件發(fā)生時(shí)調(diào)用此方法。第三步:播放器進(jìn)行預(yù)提取。調(diào)用Player類(lèi)的prefetch()方法。第四步:?jiǎn)?dòng)播放器。調(diào)用Player類(lèi)的start()方法。第五步:停止播放器。調(diào)用Player類(lèi)的stop()方法。importjava.awt.*;importjava.awt.event.*;importjavax.media.*;//引入包

classplayerextendsFrameimplementsControllerListener{ Playerp; Componentvc;player(Stringss,Stringmedia

溫馨提示

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

最新文檔

評(píng)論

0/150

提交評(píng)論