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.  }
  184. 查看Java程序设计:图形与多媒体处理(1)

原文链接:http://blog.csdn.net/wwj_748/article/details/7522672


» 本文链接:https://blog.java-zone.org/archives/764.html
» 转载请注明来源:Java地带  » 《Java程序设计:图形与多媒体处理(2)》

» 本文章为Java地带整理创作,欢迎转载!转载请注明本文地址,谢谢!
» 部分内容收集整理自网络,如有侵权请联系我删除!

» 订阅本站:https://blog.java-zone.org/feed/

标签: Java程序设计, Java图形编程, Java多媒体处理技术

添加新评论