播放幻燈片和動畫
用實例說明播放幻燈片和動畫的方法。
【例12-7】小應用程序先將幻燈片讀入數組在存儲,單擊鼠標變換幻燈片,逐張顯示
import?java.applet.*import?java.awt.*;import?java.awt.event.*;public?class?Example7_7?extends?Applet?implements?MouseListener{????final?int?number?=?50;?//假定幻燈片有50張????int?count?=?0;????Image[]?card?=?new?Image[number];????public?void?init(){????????addMouseListener(this);????????for?(int?i?=?0;?i?<?number;?i++){????????????card[i]?=?getImage(getCodebase(),?"DSC0033"?+?i?+?".jpg");????????}????}????public?void?paint(Graphics?g){????????if?((card[count])?!=?null)????????????g.drawImage(card[count],?10,?10,?card[count].getWidth(this),card[count].getHeitht(this),?this);????}????public?void?mousePressed(MouseEvent?e){????????count?=?(count?+?1)?%?number;?//循環(huán)逐張顯示????????repaint();????}????public?void?mouseRelease(MouseEvent?e){}????public?void?mouseEntered(MouseEvent?e){}????public?void?mouseExited(Mouse?Event?e){}????public?void?mouseClicked(MouseEvent?e){}}
【例12-8】小應用程序說明播放動畫的方法,要求播放的圖片和小程序放在相同的目錄中,程序通過快速顯示一組圖片造成顯示動畫的效果。小應用程序利用線程控制動畫圖片的逐顯示
import?java.applet.*;import?java.awt.*;import?java.awt.event.*;public?class?Example7_8?extends?Applet?implements?Runnable{????final?int?number?=?50;????int?count?=?0;????Thread?mythread;????Image[]?pic?=?new?Image[number];????public?void?init(){????????setSize(300,?200);????????for?(int?i?=?0;?i?<=?number;?i++){????????????//載入動畫圖片????????????pic[i?-?1]?=?getImage(getCodebase(),?"DSC0033"?+?i?+?".jpg");????????}????}????public?void?start(){????????mythread?=?new?Thread(this);?//創(chuàng)建一個線程????????mythread.start();?//啟動線程執(zhí)行????}????public?void?stop(){????????mythread?=?null;????}????public?void?run(){????????//線程的執(zhí)行代碼????????while?(true){????????????repaint();????????????count?=?(count?+?1)?%?number;?//改變顯示的圖片號????????????try{????????????????mhythread.sleep(200);????????????}????????????catch?(InterruptedExeception?e){}????????}????}????public?void?paint(Graphics?g){????????if?((pic[count]?!=?null)????????????g.drawImage(pic[count],?10,?10,?pic[count].getwidth(this),?pic[count].getHeight(this),?this);????}}
播放聲音
Java語言老根據地的音頻格式有多種:au、aiff、wav、midi、rfm等。小程序要播放音頻文件,可使用類AudioClip,該類在java.applet.AudioClip類庫中定義。小程序先創(chuàng)建AudioClip對象,并用getAudioClip()方法為其初始化。代碼形式如下:
AudioClip audioClip=getAudioClip(getCodebase(),”myAudioClipFile.au”);
如果要從網上獲得音頻文件,可用方法getAudioClip(URL url,String name),根據url地址及音頻文件name獲得可播放的音頻對象。
控制聲音的播放有3個方法:play()播放聲音,loop()循環(huán)播放和stop()停止播放。
【例12-9】能播放聲音的小應用程序
import?java.applet.*;import?java.awt.*;import?java.awt.event.*;public?class?Example7_9?extends?Applet?implements?ActionListener{????AudioClip?clip;?//聲明一個音頻對象????Button?buttonPlay,?buttonLoop,?buttonStop;????public?void?init(){????????clip?=?getAudioClip(getCodebase(),?"2.wav");????????//根據程序所在地址處聲音文件2.wav創(chuàng)建音頻對象,????????//Applet類的getCodebase()方法可以獲得小程序所在的html頁面的URL地址。????????buttonPlay?=?new?Button("開始播放");????????buttonLoop?=?new?Button("循環(huán)播放");????????buttonStop?=?new?Button("停止播放");????????buttonPlay.addActionListener(this);????????buttonStop.addActionListener(this);????????buttonLoop.addActionListener(this);????????add(buttonPlay);????????add(buttonLoop);????????add(buttonStop);????}????public?void?stop(){????????clip.stop();?//當離開此頁面時停止播放????}????public?void?actionPerformed(ActionEvent?e){????????if?(e.getSource()?==?buttonPlay){????????????clip.play();????????}????????else?if?(e.getSource()?==?buttonLoob){????????????clip.loop();????????}????????else?if?(e.getSource()?==?buttonStop){????????????clip.stop();????????}????}}
【例12-10】如果聲音文件較大或網絡速度慢會影響小程序的初始化工作。這可用多線程技術解決。在一個級別較低的線程中完成音頻對象的創(chuàng)建,即由后臺載入聲音文件,前臺播放。
import?java.applet.*;import?java.awt.*;import?java.awt.event.*;public?class?Hanoi?extends?applet?implements?Runnable,?ActionListener{????AudioClip?clip;?//聲明一個音頻對象????textField?text;????Thread?thread;????Button?buttonPlay,?buttonLoop,?buttonStop;????public?void?init(){????????thread?=?new?Thread(this);?//創(chuàng)建新線程????????thread?.setPriority(Thread.MIN_PRIORITY);????????buttonPlay?=?new?Button("開始播放");????????buttonLoop?=?new?Button(?"循環(huán)播放");????????buttonStop?=?new?Button("停止播放");????????text?=?new?textField(12);????????buttonPlay.addActionListener(this);????????buttonStop.addActionListener(this);????????buttonLoop.addActionListener(this);????????add(buttonPlay);????????add(buttonLoop);????????add(buttonStop);????????add(text);????}????public?void?start(){????????thread.start();????}????public?void?stop(){????????clip.stop();????}????public?void?actionPerformed(ActionEvent?e){????????if?(e.getSource()?==?buttonPlay(){????????????clip.play();????????}????????else?if?(e.getSource()?==?buttonLoop(){????????????clip.loop();????????}????????else?if?(e.getSource()?==?buttonStop(){????????????clip.stop();????????}????}????public?void?run(){????????//在線程thread?中創(chuàng)建音頻對象????????clip?=?getAudioclip(getCodebase(),?"2.wav");????????text.setText("請稍等");????????if(clip?!?=?null){????????????buttonPlay.setBackground(Color.red);?buttonLoop.setBackground(Color.green);?text.setText("您可以播放了");????????}?//獲得音頻對象后通知可以播放????}}
以上就是長沙達內教育java培訓機構的小編針對“基礎知識分享:Java多媒體基礎”的內容進行的回答,希望對大家有所幫助,如有疑問,請在線咨詢,有專業(yè)老師隨時為你服務。