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

  同心圆效果图:

  

  /**   *程序要求:新建一个600*600像素的应用程序窗口,并在窗口中绘制5个不同颜色的同心圆,   *所有圆心都是屏幕的中心点,相邻两个圆直接的半径相差50像素   *效果图如下图所示(颜色随机设置),源程序保存为Ex7_1.java。   *作者:wwj   *日期:2012/4/25   *功能:显示一个有5个不同颜色的同心圆   **/  importjavax.swing.*;   importjava.awt.*;   importjava.awt.Color;   publicclassEx7_1extendsJFrame   {   intred,green,blue;   Colorcolor;   publicEx7_1()   {   super("一个有5个不同颜色的同心圆");//显示窗口名称   setSize(600,600);//设置窗口大小   setVisible(true);//设置为可见   setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置窗口关闭动作      }      publicvoidpaint(Graphicsg)   {   //第一个圆   red=(int)(Math.random()*255);   green=(int)(Math.random()*255);   blue=(int)(Math.random()*255);   color=newColor(red,green,blue);   g.setColor(color);   g.fillOval(175,175,250,250);   //第二个圆   red=(int)(Math.random()*255);   green=(int)(Math.random()*255);   blue=(int)(Math.random()*255);   color=newColor(red,green,blue);   g.setColor(color);   g.fillOval(200,200,200,200);   //第三个圆   red=(int)(Math.random()*255);   green=(int)(Math.random()*255);   blue=(int)(Math.random()*255);   color=newColor(red,green,blue);   g.setColor(color);   g.fillOval(225,225,150,150);   //第四个圆   red=(int)(Math.random()*255);   green=(int)(Math.random()*255);   blue=(int)(Math.random()*255);   color=newColor(red,green,blue);   g.setColor(color);   g.fillOval(250,250,100,100);   //第五个圆   red=(int)(Math.random()*255);   green=(int)(Math.random()*255);   blue=(int)(Math.random()*255);   color=newColor(red,green,blue);   g.setColor(color);   g.fillOval(275,275,50,50);   }      publicstaticvoidmain(String[]args)   {   Ex7_1e=newEx7_1();   }   }

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

  

  /**   *程序要求:编写一个Applet的小程序,准备5幅图片和三个音乐文件,绘制到Applet中,   *并增加几个按钮,控制图片的切换、放大、缩小和音乐文件的播放。   *作者:wwj   *日期:2012/4/29   *参考:neicole   *功能:能进行图片和歌曲的选择变换的applet小程序   **/  importjavax.swing.*;   importjava.awt.*;   importjava.awt.event.*;   importjava.applet.Applet;   importjava.applet.AudioClip;      publicclassEx7_2extendsAppletimplementsActionListener,ItemListener   {   //创建两个面板   JPanelp1=newJPanel();   JPanelp2=newJPanel();   JPanelp3=newJPanel();   //声音对象   AudioClip[]sound=newAudioClip[3];   intplayingSong=0;   //切换图片的按钮   JButtonlastPic=newJButton("上一张");   JButtonsetLarge=newJButton("放大");   JButtonsetLittle=newJButton("缩小");   JButtonnextPic=newJButton("下一张");   //切换歌曲的按钮   JButtonlastSound=newJButton("上一首");   JButtonplay=newJButton("播放");   JButtonloop=newJButton("连续");   JButtonstop=newJButton("停止");   JButtonnextSound=newJButton("下一首");   //曲目下拉列表   JComboBoxxx;   Stringnames[]={"曲目1.wav","曲目2.wav","曲目3.wav"};      //创建画布对象   MyCanvaslshowPhotos;      publicvoidinit()   {   //窗口布局   this.setLayout(newBorderLayout());   //为图片控制按钮注册监听器   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();      //实例化下拉列表对象   xx=newJComboBox(names);   xx.addItemListener(this);   //为控制播放音乐按钮注册监听器   lastSound.addActionListener(this);   play.addActionListener(this);   loop.addActionListener(this);   stop.addActionListener(this);   nextSound.addActionListener(this);   for(inti=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=newMyCanvasl();   p3.add(showPhotos);   p3.repaint();   //把面板p1和p2分别布置到窗口的北部和南部   add(p1,BorderLayout.NORTH);   add(p2,BorderLayout.SOUTH);   add(p3,BorderLayout.CENTER);   this.repaint();   }   //按钮的事件处理   publicvoidactionPerformed(ActionEvente)   {      if(e.getSource()==lastPic){   showPhotos.changePhotoShow(‘P’);   }   elseif(e.getSource()==nextPic){   showPhotos.changePhotoShow(‘N’);   }   elseif(e.getSource()==setLarge){   showPhotos.changePhotoSize(‘B’);   }   elseif(e.getSource()==setLittle){   showPhotos.changePhotoSize(‘S’);   }      elseif(e.getSource()==lastSound){//上一首   sound[playingSong].stop();   playingSong=(playingSong-1+3)%3;   xx.setSelectedIndex(playingSong);   sound[playingSong].play();   }   elseif(e.getSource()==play){//按下播放按钮   sound[playingSong].play();   }   elseif(e.getSource()==loop){//按下循环按钮   sound[playingSong].loop();   }   elseif(e.getSource()==stop){//按下停止按钮   sound[playingSong].stop();   }   else{//下一首   sound[playingSong].stop();   playingSong=(playingSong+1)%3;   xx.setSelectedIndex(playingSong);   sound[playingSong].play();   }   }   //下拉列表的事件处理   publicvoiditemStateChanged(ItemEvente)   {      sound[playingSong].stop();   sound[playingSong]=getAudioClip(getCodeBase(),"music/"+xx.getSelectedItem());   }   classMyCanvaslextendsCanvas   {      publicImage[]img=newImage[5];   intMaxWidth=600;   intMaxHeight=500;   intnowImageIndex=0;   intcoordinateX=0;   intcoordinateY=0;   intcurrentWidth=MaxWidth;   intcurrentHeight=MaxHeight;      MyCanvasl(){   setSize(MaxWidth,MaxHeight);   //获取当前目录下的图片   for(inti=0;i<5;i++){   img[i]=getImage(getCodeBase(),"image/"+Integer.toString(i+1)+".jpg");   }   }   privatevoidchangePhotoIndex(intindex){   nowImageIndex=index;   changePhotoSize(‘M’);   }   publicvoidchangePhotoShow(charcommand){   if(‘P’==command){   changePhotoIndex((nowImageIndex+5-1)%5);   }   elseif(‘N’==command){   changePhotoIndex((nowImageIndex+1)%5);   }   }      publicvoidchangePhotoSize(charcommand){   if(‘M’==command){   currentWidth=MaxWidth;   currentHeight=MaxHeight;   }   elseif(‘B’==command){   if(MaxWidth>=(currentWidth+100)&&MaxHeight>=(currentHeight+100)){   currentWidth+=100;   currentHeight+=100;   }   }   elseif(‘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方法用来在窗口显示图片   publicvoidpaint(Graphicsg){   g.drawImage(img[nowImageIndex],coordinateX,coordinateY,currentWidth,currentHeight,this);   }   }   }

看自家总在期待,不知将来好歹,新乐吧总在不断等待,

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

相关文章:

你感兴趣的文章:

标签云: