スライドショーやアニメーションを再生する
例を使用して、スライドとアニメーションを再生する方法を説明します。
[例] 小さなアプリケーションは、まずスライドを配列に読み込んで保存し、マウスをクリックしてスライドを切り替えて 1 枚ずつ表示します。
import java.applet.*import java.awt.*;import java.awt.event.*;public class Example7_7 extends Appletimplements MouseListener{ //スライドが 50 枚あると仮定します int count = 0; ] カード = 新しい画像 [番号]; public void init(){ addMouseListener(this); for (int i = 0; i < 番号; i++){ カード [i] = getImage(getCodeBase(), "DSC0033" + i + ".jpg"); } } public voidPaint(Graphics g){ if ((card[count]) != null) g.drawImage(card[count], 10) , 10, カード[カウント].getWidth(this),card[カウント].getHeitht(this), this); MousePressed(MouseEvent e){ count = (count + 1) % number; //ループして repaint() } public void MouseRelease(MouseEvent e){} public void MouseExited(Mouse Event) e){} public void MouseClicked(MouseEvent e){}}
[例] アプレットはアニメーションの再生方法を説明します。このプログラムでは、画像のグループをすばやく表示することで、アニメーションを表示する効果を作成するために、再生する画像とアプレットを同じディレクトリに配置する必要があります。この小さなアプリケーションは、スレッドを使用してアニメーション画像の表示を 1 つずつ制御します。
import java.applet.*;import java.awt.*;import java.awt.event.*;public class Example7_8 extends アプレット実装 Runnable{ 最終 int 数 = 50; スレッド mythread; new Image[数値]; public void init(){ setSize(300, 200); for (int i = 0; i <= number; i++) //アニメーション画像を読み込み pic[i - 1] = getImage(getCodeBase(), "DSC0033" + i + ".jpg") } } public void start(){ //A を作成します。 thread mythread.start(); //スレッドの実行を開始する} public void stop(){ mythread = null; } public void run(){ //スレッドの実行コード while (true){ repaint(); = (count + 1) % number; //表示するピクチャ番号を変更 try{ mhythread.sleep(200) } catch (InterruptedExeception e){} } } public voidPaint(Graphics g){ if ((pic[count] != null) g.drawImage(pic[count], 10, 10, pic[count].getwidth(this), pic[count].getHeight(this),これ); }}
サウンドを再生する
Java 言語の古いベースには、au、aiff、wav、midi、rfm など、多くのオーディオ形式があります。アプレットでオーディオ ファイルを再生するには、java.applet.AudioClip クラス ライブラリで定義されている AudioClip クラスを使用できます。アプレットはまず AudioClip オブジェクトを作成し、getAudioClip() メソッドを使用してそれを初期化します。コード形式は次のとおりです。
AudioClip audioClip = getAudioClip(getCodeBase(),"myAudioClipFile.au");
インターネットからオーディオ ファイルを取得する場合は、メソッド getAudioClip(URL url, String name) を使用して、URL アドレスとオーディオ ファイル名に基づいて再生可能なオーディオ オブジェクトを取得できます。
サウンドの再生を制御するメソッドは 3 つあります。サウンドを再生する play()、再生をループするloop()、再生を停止する stop() です。
[例] サウンドを再生できる小さなアプリケーション。
import java.applet.*;import java.awt.*;import java.awt.event.*;public class Example7_9 extends Appletimplements ActionListener{ //オーディオ オブジェクトの宣言 ButtonPlay, buttonLoop, buttonStop; (){ Clip = getAudioClip(getCodeBase(), "2.wav"); //プログラムのアドレスにあるサウンド ファイル 2.wav に基づいてオーディオ オブジェクトを作成します。 //Applet クラスの getCodeBase() メソッドは、アプレットが配置されている HTML ページの URL アドレスを取得できます。 buttonPlay = new Button("再生を開始"); buttonLoop = new Button("再生を停止"); buttonPlay.addActionListener(this); (これ); 追加(ボタンループ); } //このページを離れるときは再生を停止します} 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() }}
【例】サウンドファイルが大きい場合やネットワーク速度が遅い場合、ミニプログラムの初期化に影響します。これはマルチスレッド技術で解決できます。オーディオ オブジェクトの作成は、下位レベルのスレッドで完了します。つまり、サウンド ファイルはバックグラウンドでロードされ、フォアグラウンドで再生されます。
import java.applet.*;import java.awt.*;import java.awt.event.*;public class Hanoi extends appletimplements Runnable, ActionListener{ //オーディオオブジェクトを宣言します textField Thread thread; , buttonLoop, buttonStop; public void init(){ thread = new Thread(this); // 新しいスレッドを作成します。 .setPriority(Thread.MIN_PRIORITY); buttonPlay = new Button("再生開始"); buttonLoop = new Button("再生停止"); .addActionListener(this); buttonStop.addActionListener(this); add(buttonLoop); add(text); } public void start(){ thread.start(); } public void actionPerformed() if (e.getSource() == buttonPlay(){clip.play(); } else if (e.getSource() == buttonLoop(){clip.loop(); } else if (e.getSource() == buttonStop(){ Clip.stop(); } } public void run(){ //スレッド内にオーディオオブジェクトクリップを作成 thread = getAudioclip(getCodeBase(), "2.wav"); text .setText("お待ちください"); if(clip ! = null){ buttonLoop.setBackground(Color.green); text.setText("再生できます") } //オーディオオブジェクトを取得したら、通知を再生できます}}