国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 開(kāi)發(fā) > Java > 正文

使用Java編寫(xiě)瀏覽圖片和播放音樂(lè)的程序

2023-06-10 12:42:58
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

本文介紹了Java中瀏覽圖片和播放音樂(lè)的實(shí)現(xiàn)方法。

/**
 *程序要求:編寫(xiě)一個(gè)Applet的小程序,準(zhǔn)備5幅圖片和三個(gè)音樂(lè)文件,繪制到Applet中,
 *并增加幾個(gè)按鈕,控制圖片的切換、放大、縮小和音樂(lè)文件的播放。
 *作者:wwj
 *日期:2012/4/29
 *參考:neicole
 *功能:能進(jìn)行圖片和歌曲的選擇變換的applet小程序
 **/

 import javax.swing.*;
 import java.awt.*;
 import java.awt.event.*;
 import java.applet.Applet;
 import java.applet.AudioClip;

 public class Ex7_2 extends Applet implements ActionListener,ItemListener
 {

  //創(chuàng)建兩個(gè)面板
  JPanel p1=new JPanel();
  JPanel p2=new JPanel();
  JPanel p3=new JPanel();
  //聲音對(duì)象
  AudioClip[] sound=new AudioClip[3];
  int playingSong=0;
  //切換圖片的按鈕
  JButton lastPic=new JButton("上一張");
  JButton setLarge=new JButton("放大");
  JButton setLittle=new JButton("縮小");
  JButton nextPic=new JButton("下一張");
  //切換歌曲的按鈕
  JButton lastSound=new JButton("上一首");
  JButton play=new JButton("播放");
  JButton loop=new JButton("連續(xù)");
  JButton stop=new JButton("停止");
  JButton nextSound=new JButton("下一首");
  //曲目下拉列表
  JComboBox xx;
  String names[]={ "曲目1.wav","曲目2.wav","曲目3.wav"};
 
 //創(chuàng)建畫(huà)布對(duì)象
 MyCanvasl showPhotos;

  public void init()
  {
   //窗口布局
   this.setLayout(new BorderLayout());

   //為圖片控制按鈕注冊(cè)監(jiān)聽(tīng)器
   lastPic.addActionListener(this);
   setLarge.addActionListener(this);
   setLittle.addActionListener(this);
   nextPic.addActionListener(this);

   //向面板p1添加組件
   p1.add(lastPic);
   p1.add(setLarge);
   p1.add(setLittle);
   p1.add(nextPic);
   p1.repaint();
 
  //實(shí)例化下拉列表對(duì)象
  xx = new JComboBox(names);
  xx.addItemListener(this);

     //為控制播放音樂(lè)按鈕注冊(cè)監(jiān)聽(tīng)器
  lastSound.addActionListener(this);
  play.addActionListener(this);
  loop.addActionListener(this);
  stop.addActionListener(this);
  nextSound.addActionListener(this);

  for(int i=0;i<3;i++)
   {
   sound[i]=getAudioClip(getCodeBase(),"music/"+"曲目"
     +Integer.toString(i+1)+".wav");
   }
  

  
     //向面板p2添加組件
   p2.add(xx);
   p2.add(lastSound);
   p2.add(play);
   p2.add(loop);
   p2.add(stop);
   p2.add(nextSound);
   p2.repaint();
  
  showPhotos = new MyCanvasl();
  p3.add(showPhotos);
   p3.repaint();

  //把面板p1和p2分別布置到窗口的北部和南部
   add(p1,BorderLayout.NORTH);
   add(p2,BorderLayout.SOUTH);
   add(p3,BorderLayout.CENTER);

   this.repaint();

  }


  //按鈕的事件處理
  public void actionPerformed(ActionEvent e)
  {

  
  if(e.getSource() == lastPic){
   showPhotos.changePhotoShow('P');
  }
  else if(e.getSource() == nextPic){
   showPhotos.changePhotoShow('N');
  }
  else if(e.getSource() == setLarge){
   showPhotos.changePhotoSize('B');
  }
  else if(e.getSource() == setLittle){
   showPhotos.changePhotoSize('S');
  }
 
  else if(e.getSource()==lastSound){ //上一首
   sound[playingSong].stop();
   playingSong=(playingSong-1+3)%3;
   xx.setSelectedIndex(playingSong);
   sound[playingSong].play();

  }
  else if(e.getSource()==play){  //按下播放按鈕
   sound[playingSong].play();
  }
  else if(e.getSource()==loop){  //按下循環(huán)按鈕
   sound[playingSong].loop();
  }
  else if(e.getSource()==stop){  //按下停止按鈕
   sound[playingSong].stop();
  }
  else{        //下一首
   sound[playingSong].stop();
   playingSong=(playingSong+1)%3;
   xx.setSelectedIndex(playingSong);
   sound[playingSong].play();

  } 
  }


  //下拉列表的事件處理
  public void itemStateChanged(ItemEvent e)
  {
  
   sound[playingSong].stop();
   sound[playingSong]=getAudioClip(getCodeBase(),"music/"+xx.getSelectedItem());
  }

 class MyCanvasl extends Canvas
 {
  
  public Image[] img=new Image[5];

  int MaxWidth = 600;
  int MaxHeight = 500;
  int nowImageIndex = 0;
  int coordinateX = 0;
  int coordinateY = 0;
  int currentWidth = MaxWidth;
  int currentHeight = MaxHeight;

  
  MyCanvasl(){
   setSize(MaxWidth,MaxHeight);
   //獲取當(dāng)前目錄下的圖片
   for(int i=0;i<5;i++){
    img[i]=getImage(getCodeBase(),"image/"+Integer.toString(i+1)+".jpg");
   }
  }


  private void changePhotoIndex(int index){
   nowImageIndex = index;
   changePhotoSize('M');
  }

  public void changePhotoShow(char command){
   if('P' == command){
    changePhotoIndex((nowImageIndex + 5 - 1 ) % 5);
   }
   else if('N' == command){
    changePhotoIndex((nowImageIndex + 1) % 5);
   }
  }
  


   public void changePhotoSize(char command){
   if ('M' == command){
    currentWidth = MaxWidth;
    currentHeight = MaxHeight;
   }
   else if ('B' == command){
    if(MaxWidth >= (currentWidth + 100) && MaxHeight >= (currentHeight + 100)){
     currentWidth += 100;
     currentHeight += 100;
    }
   }
   else if('S' == command){
    if((0 < (currentWidth - 100)) && (0 < (currentHeight - 100))){
     currentWidth = currentWidth - 100;
     currentHeight = currentHeight - 100;
    }
   }
   coordinateX = (MaxWidth - currentWidth) / 2;
   coordinateY = (MaxHeight - currentHeight) / 2;
   repaint();
  }
   //paint方法用來(lái)在窗口顯示圖片
  public void paint(Graphics g){
        g.drawImage(img[nowImageIndex],coordinateX,coordinateY,currentWidth,currentHeight,this);

  }
 }
 }

 最終效果如下圖所示:

Java實(shí)現(xiàn)圖片瀏覽和音樂(lè)播放

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 韶山市| 应用必备| 潼南县| 郎溪县| 庆云县| 色达县| 龙岩市| 瑞金市| 荆门市| 论坛| 东平县| 错那县| 广昌县| 得荣县| 綦江县| 获嘉县| 保亭| 枣强县| 瓦房店市| 且末县| 龙南县| 繁峙县| 长白| 通榆县| 北安市| 孙吴县| 达日县| 祁门县| 蓝山县| 凤冈县| 马边| 扶绥县| 延安市| 进贤县| 甘谷县| 信阳市| 河间市| 乐业县| 鲜城| 武鸣县| 丰城市|