欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

Java程序设计:图形与多媒体处理(1)(2)

来源: javaer 分享于  点击 28117 次 点评:137

播放音乐和切换图片的小程序效果图:

  1. /**  
  2.  *程序要求:编写一个Applet的小程序,准备5幅图片和三个音乐文件,绘制到Applet中,  
  3.  *并增加几个按钮,控制图片的切换、放大、缩小和音乐文件的播放。  
  4.  *作者:wwj  
  5.  *日期:2012/4/29  
  6.  *参考:neicole  
  7.  *功能:能进行图片和歌曲的选择变换的applet小程序  
  8.  **/ 
  9.  
  10.  import javax.swing.*;  
  11.  import java.awt.*;  
  12.  import java.awt.event.*;  
  13.  import java.applet.Applet;  
  14.  import java.applet.AudioClip;  
  15.  
  16.    
  17.  public class Ex7_2 extends Applet implements ActionListener,ItemListener  
  18.  {  
  19.  
  20.      //创建两个面板  
  21.      JPanel p1=new JPanel();  
  22.      JPanel p2=new JPanel();  
  23.      JPanel p3=new JPanel();  
  24.      //声音对象  
  25.      AudioClip[] sound=new AudioClip[3];  
  26.      int playingSong=0;  
  27.      //切换图片的按钮  
  28.      JButton lastPic=new JButton("上一张");  
  29.      JButton setLarge=new JButton("放大");  
  30.      JButton setLittle=new JButton("缩小");  
  31.      JButton nextPic=new JButton("下一张");  
  32.      //切换歌曲的按钮  
  33.      JButton lastSound=new JButton("上一首");  
  34.      JButton play=new JButton("播放");  
  35.      JButton loop=new JButton("连续");  
  36.      JButton stop=new JButton("停止");  
  37.      JButton nextSound=new JButton("下一首");  
  38.      //曲目下拉列表  
  39.      JComboBox xx;  
  40.      String names[]={ "曲目1.wav","曲目2.wav","曲目3.wav"};  
  41.       
  42.     //创建画布对象  
  43.     MyCanvasl showPhotos;  
  44.  
  45.        
  46.  
  47.      public void init()  
  48.      {  
  49.          //窗口布局  
  50.          this.setLayout(new BorderLayout());  
  51.  
  52.          //为图片控制按钮注册监听器  
  53.          lastPic.addActionListener(this);  
  54.          setLarge.addActionListener(this);  
  55.          setLittle.addActionListener(this);  
  56.          nextPic.addActionListener(this);  
  57.  
  58.          //向面板p1添加组件  
  59.          p1.add(lastPic);  
  60.          p1.add(setLarge);  
  61.          p1.add(setLittle);  
  62.          p1.add(nextPic);  
  63.          p1.repaint();  
  64.       
  65.         //实例化下拉列表对象  
  66.         xx = new JComboBox(names);  
  67.         xx.addItemListener(this);  
  68.  
  69.         //为控制播放音乐按钮注册监听器  
  70.         lastSound.addActionListener(this);  
  71.         play.addActionListener(this);  
  72.         loop.addActionListener(this);  
  73.         stop.addActionListener(this);  
  74.         nextSound.addActionListener(this);  
  75.  
  76.         for(int i=0;i<3;i++)  
  77.          {  
  78.             sound[i]=getAudioClip(getCodeBase(),"music/"+"曲目" 
  79.                     +Integer.toString(i+1)+".wav");  
  80.          }  
  81.           
  82.  
  83.           
  84.         //向面板p2添加组件  
  85.          p2.add(xx);  
  86.          p2.add(lastSound);  
  87.          p2.add(play);  
  88.          p2.add(loop);  
  89.          p2.add(stop);  
  90.          p2.add(nextSound);  
  91.          p2.repaint();  
  92.           
  93.         showPhotos = new MyCanvasl();  
  94.         p3.add(showPhotos);  
  95.          p3.repaint();  
  96.  
  97.         //把面板p1和p2分别布置到窗口的北部和南部   
  98.          add(p1,BorderLayout.NORTH);  
  99.          add(p2,BorderLayout.SOUTH);  
  100.          add(p3,BorderLayout.CENTER);  
  101.  
  102.          this.repaint();  
  103.  
  104.      }  
  105.  
  106.  
  107.      //按钮的事件处理  
  108.      public void actionPerformed(ActionEvent e)  
  109.      {  
  110.  
  111.           
  112.         if(e.getSource() == lastPic){  
  113.             showPhotos.changePhotoShow('P');  
  114.         }  
  115.         else if(e.getSource() == nextPic){  
  116.             showPhotos.changePhotoShow('N');  
  117.         }  
  118.         else if(e.getSource() == setLarge){  
  119.             showPhotos.changePhotoSize('B');  
  120.         }  
  121.         else if(e.getSource() == setLittle){  
  122.             showPhotos.changePhotoSize('S');  
  123.         }  
  124.       
  125.         else if(e.getSource()==lastSound){  //上一首  
  126.             sound[playingSong].stop();  
  127.             playingSong=(playingSong-1+3)%3;  
  128.             xx.setSelectedIndex(playingSong);  
  129.             sound[playingSong].play();  
  130.  
  131.         }  
  132.         else if(e.getSource()==play){       //按下播放按钮  
  133.             sound[playingSong].play();  
  134.         }  
  135.         else if(e.getSource()==loop){       //按下循环按钮  
  136.             sound[playingSong].loop();  
  137.         }  
  138.         else if(e.getSource()==stop){       //按下停止按钮  
  139.             sound[playingSong].stop();  
  140.         }  
  141.         else{                               //下一首  
  142.             sound[playingSong].stop();  
  143.             playingSong=(playingSong+1)%3;  
  144.             xx.setSelectedIndex(playingSong);  
  145.             sound[playingSong].play();  
  146.  
  147.         }     
  148.      }  
  149.  
  150.  
  151.      //下拉列表的事件处理  
  152.      public void itemStateChanged(ItemEvent e)  
  153.      {  
  154.            
  155.          sound[playingSong].stop();  
  156.          sound[playingSong]=getAudioClip(getCodeBase(),"music/"+xx.getSelectedItem());  
  157.      }  
  158.  
  159.     class MyCanvasl extends Canvas  
  160.     {  
  161.           
  162.         public Image[] img=new Image[5];  
  163.  
  164.         int MaxWidth = 600;  
  165.         int MaxHeight = 500;  
  166.         int nowImageIndex = 0;  
  167.         int coordinateX = 0;  
  168.         int coordinateY = 0;  
  169.         int currentWidth = MaxWidth;  
  170.         int currentHeight = MaxHeight;  
  171.  
  172.           
  173.         MyCanvasl(){  
  174.          setSize(MaxWidth,MaxHeight);  
  175.          //获取当前目录下的图片  
  176.          for(int i=0;i<5;i++){  
  177.              img[i]=getImage(getCodeBase(),"image/"+Integer.toString(i+1)+".jpg");  
  178.          }  
  179.         }  
  180.  
  181.  
  182.         private void changePhotoIndex(int index){  
  183.             nowImageIndex = index;  
  184.             changePhotoSize('M');  
  185.         }  
  186.  
  187.  
  188.  
  189.         public void changePhotoShow(char command){  
  190.             if('P' == command){  
  191.                 changePhotoIndex((nowImageIndex + 5 - 1 ) % 5);  
  192.             }  
  193.             else if('N' == command){  
  194.                 changePhotoIndex((nowImageIndex + 1) % 5);  
  195.             }  
  196.         }  
  197.           
  198.  
  199.  
  200.          public void changePhotoSize(char command){  
  201.             if ('M' == command){  
  202.                 currentWidth = MaxWidth;  
  203.                 currentHeight = MaxHeight;  
  204.             }  
  205.             else if ('B' == command){  
  206.                 if(MaxWidth >= (currentWidth + 100) && MaxHeight >= (currentHeight + 100)){  
  207.                     currentWidth += 100;  
  208.                     currentHeight += 100;  
  209.                 }  
  210.             }  
  211.             else if('S' == command){  
  212.                 if((0 < (currentWidth - 100)) && (0 < (currentHeight - 100))){  
  213.                     currentWidth = currentWidth - 100;  
  214.                     currentHeight = currentHeight - 100;  
  215.                 }  
  216.             }  
  217.             coordinateX = (MaxWidth - currentWidth) / 2;  
  218.             coordinateY = (MaxHeight - currentHeight) / 2;  
  219.             repaint();  
  220.         }  
  221.             //paint方法用来在窗口显示图片  
  222.      public void paint(Graphics g){  
  223.            g.drawImage(img[nowImageIndex],coordinateX,coordinateY,currentWidth,currentHeight,this);  
  224.  
  225.      }  
  226.     }  
  227.  }  


相关栏目:

用户点评