程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> Java程序設計:圖形與多媒體處理(2)

Java程序設計:圖形與多媒體處理(2)

編輯:關於JAVA

播放音樂和切換圖片的小程序效果圖:

  1. /**
  2. *程序要求:編寫一個Applet的小程序,准備5幅圖片和三個音樂文件,繪制到Applet中,
  3. *並增加幾個按鈕,控制圖片的切換、放大、縮小和音樂文件的播放。
  4. *作者:wwj
  5. *日期:2012/4/29
  6. *參考:neicole
  7. *功能:能進行圖片和歌曲的選擇變換的applet小程序
  8. **/
  9. import Javax.swing.*;
  10. import Java.awt.*;
  11. import Java.awt.event.*;
  12. import Java.applet.Applet;
  13. import Java.applet.AudioClip;
  14. public class Ex7_2 extends Applet implements ActionListener,ItemListener
  15. {
  16. //創建兩個面板
  17. JPanel p1=new JPanel();
  18. JPanel p2=new JPanel();
  19. JPanel p3=new JPanel();
  20. //聲音對象
  21. AudioClip[] sound=new AudioClip[3];
  22. int playingSong=0;
  23. //切換圖片的按鈕
  24. JButton lastPic=new JButton("上一張");
  25. JButton setLarge=new JButton("放大");
  26. JButton setLittle=new JButton("縮小");
  27. JButton nextPic=new JButton("下一張");
  28. //切換歌曲的按鈕
  29. JButton lastSound=new JButton("上一首");
  30. JButton play=new JButton("播放");
  31. JButton loop=new JButton("連續");
  32. JButton stop=new JButton("停止");
  33. JButton nextSound=new JButton("下一首");
  34. //曲目下拉列表
  35. JComboBox xx;
  36. String names[]={ "曲目1.wav","曲目2.wav","曲目3.wav"};
  37. //創建畫布對象
  38. MyCanvasl showPhotos;
  39. public void init()
  40. {
  41. //窗口布局
  42. this.setLayout(new BorderLayout());
  43. //為圖片控制按鈕注冊監聽器
  44. lastPic.addActionListener(this);
  45. setLarge.addActionListener(this);
  46. setLittle.addActionListener(this);
  47. nextPic.addActionListener(this);
  48. //向面板p1添加組件
  49. p1.add(lastPic);
  50. p1.add(setLarge);
  51. p1.add(setLittle);
  52. p1.add(nextPic);
  53. p1.repaint();
  54. //實例化下拉列表對象
  55. xx = new JComboBox(names);
  56. xx.addItemListener(this);
  57. //為控制播放音樂按鈕注冊監聽器
  58. lastSound.addActionListener(this);
  59. play.addActionListener(this);
  60. loop.addActionListener(this);
  61. stop.addActionListener(this);
  62. nextSound.addActionListener(this);
  63. for(int i=0;i<3;i++)
  64. {
  65. sound[i]=getAudioClip(getCodeBase(),"music/"+"曲目"
  66. +Integer.toString(i+1)+".wav");
  67. }
  68. //向面板p2添加組件
  69. p2.add(xx);
  70. p2.add(lastSound);
  71. p2.add(play);
  72. p2.add(loop);
  73. p2.add(stop);
  74. p2.add(nextSound);
  75. p2.repaint();
  76. showPhotos = new MyCanvasl();
  77. p3.add(showPhotos);
  78. p3.repaint();
  79. //把面板p1和p2分別布置到窗口的北部和南部
  80. add(p1,BorderLayout.NORTH);
  81. add(p2,BorderLayout.SOUTH);
  82. add(p3,BorderLayout.CENTER);
  83. this.repaint();
  84. }
  85. //按鈕的事件處理
  86. public void actionPerformed(ActionEvent e)
  87. {
  88. if(e.getSource() == lastPic){
  89. showPhotos.changePhotoShow('P');
  90. }
  91. else if(e.getSource() == nextPic){
  92. showPhotos.changePhotoShow('N');
  93. }
  94. else if(e.getSource() == setLarge){
  95. showPhotos.changePhotoSize('B');
  96. }
  97. else if(e.getSource() == setLittle){
  98. showPhotos.changePhotoSize('S');
  99. }
  100. else if(e.getSource()==lastSound){ //上一首
  101. sound[playingSong].stop();
  102. playingSong=(playingSong-1+3)%3;
  103. xx.setSelectedIndex(playingSong);
  104. sound[playingSong].play();
  105. }
  106. else if(e.getSource()==play){ //按下播放按鈕
  107. sound[playingSong].play();
  108. }
  109. else if(e.getSource()==loop){ //按下循環按鈕
  110. sound[playingSong].loop();
  111. }
  112. else if(e.getSource()==stop){ //按下停止按鈕
  113. sound[playingSong].stop();
  114. }
  115. else{ //下一首
  116. sound[playingSong].stop();
  117. playingSong=(playingSong+1)%3;
  118. xx.setSelectedIndex(playingSong);
  119. sound[playingSong].play();
  120. }
  121. }
  122. //下拉列表的事件處理
  123. public void itemStateChanged(ItemEvent e)
  124. {
  125. sound[playingSong].stop();
  126. sound[playingSong]=getAudioClip(getCodeBase(),"music/"+xx.getSelectedItem());
  127. }
  128. class MyCanvasl extends Canvas
  129. {
  130. public Image[] img=new Image[5];
  131. int MaxWidth = 600;
  132. int MaxHeight = 500;
  133. int nowImageIndex = 0;
  134. int coordinateX = 0;
  135. int coordinateY = 0;
  136. int currentWidth = MaxWidth;
  137. int currentHeight = MaxHeight;
  138. MyCanvasl(){
  139. setSize(MaxWidth,MaxHeight);
  140. //獲取當前目錄下的圖片
  141. for(int i=0;i<5;i++){
  142. img[i]=getImage(getCodeBase(),"image/"+Integer.toString(i+1)+".jpg");
  143. }
  144. }
  145. private void changePhotoIndex(int index){
  146. nowImageIndex = index;
  147. changePhotoSize('M');
  148. }
  149. public void changePhotoShow(char command){
  150. if('P' == command){
  151. changePhotoIndex((nowImageIndex + 5 - 1 ) % 5);
  152. }
  153. else if('N' == command){
  154. changePhotoIndex((nowImageIndex + 1) % 5);
  155. }
  156. }
  157. public void changePhotoSize(char command){
  158. if ('M' == command){
  159. currentWidth = MaxWidth;
  160. currentHeight = MaxHeight;
  161. }
  162. else if ('B' == command){
  163. if(MaxWidth >= (currentWidth + 100) && MaxHeight >= (currentHeight + 100)){
  164. currentWidth += 100;
  165. currentHeight += 100;
  166. }
  167. }
  168. else if('S' == command){
  169. if((0 < (currentWidth - 100)) && (0 < (currentHeight - 100))){
  170. currentWidth = currentWidth - 100;
  171. currentHeight = currentHeight - 100;
  172. }
  173. }
  174. coordinateX = (MaxWidth - currentWidth) / 2;
  175. coordinateY = (MaxHeight - currentHeight) / 2;
  176. repaint();
  177. }
  178. //paint方法用來在窗口顯示圖片
  179. public void paint(Graphics g){
  180. g.drawImage(img[nowImageIndex],coordinateX,coordinateY,currentWidth,currentHeight,this);
  181. }
  182. }
  183. }
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved