当前位置: 移动技术网 > IT编程>开发语言>Java > Java设计图形与多媒体处理

Java设计图形与多媒体处理

2019年07月22日  | 移动技术网IT编程  | 我要评论

本文实现了两个效果:

第一种,同心圆效果图:

/** 
 *程序要求:新建一个600*600像素的应用程序窗口,并在窗口中绘制5个不同颜色的同心圆, 
 *所有圆心都是屏幕的中心点,相邻两个圆直接的半径相差50像素 
 *效果图如下图所示(颜色随机设置),源程序保存为ex7_1.java。 
 *作者:wwj 
 *日期:2012/4/25 
 *功能:显示一个有5个不同颜色的同心圆 
 **/ 
 
 import javax.swing.*; 
 import java.awt.*; 
 import java.awt.color; 
 public class ex7_1 extends jframe 
 { 
   int red,green,blue; 
   color color; 
 
   public ex7_1() 
   { 
     super("一个有5个不同颜色的同心圆");  //显示窗口名称 
     setsize(600,600);           //设置窗口大小 
     setvisible(true);           //设置为可见 
     setdefaultcloseoperation(jframe.exit_on_close);//设置窗口关闭动作 
   
   } 
 
   
   public void paint(graphics g) 
   { 
     //第一个圆 
    red=(int)(math.random()*255); 
    green=(int)(math.random()*255); 
    blue=(int)(math.random()*255); 
    color=new color(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=new color(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=new color(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=new color(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=new color(red,green,blue); 
    g.setcolor(color); 
    g.filloval(275,275,50,50); 
 
   }     
   
   public static void main(string[] args) 
   { 
     ex7_1 e = new ex7_1();    
   } 
 
 } 

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

/** 
 *程序要求:编写一个applet的小程序,准备5幅图片和三个音乐文件,绘制到applet中, 
 *并增加几个按钮,控制图片的切换、放大、缩小和音乐文件的播放。 
 *作者:wwj 
 *日期:2012/4/29 
 *参考:neicole 
 *功能:能进行图片和歌曲的选择变换的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 
 { 
 
   //创建两个面板 
   jpanel p1=new jpanel(); 
   jpanel p2=new jpanel(); 
   jpanel p3=new jpanel(); 
   //声音对象 
   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("连续"); 
   jbutton stop=new jbutton("停止"); 
   jbutton nextsound=new jbutton("下一首"); 
   //曲目下拉列表 
   jcombobox xx; 
   string names[]={ "曲目1.wav","曲目2.wav","曲目3.wav"}; 
   
  //创建画布对象 
  mycanvasl showphotos; 
 
    
 
   public void init() 
   { 
     //窗口布局 
     this.setlayout(new borderlayout()); 
 
     //为图片控制按钮注册监听器 
     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 = new jcombobox(names); 
    xx.additemlistener(this); 
 
    //为控制播放音乐按钮注册监听器 
    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){    //按下循环按钮 
      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); 
     //获取当前目录下的图片 
     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方法用来在窗口显示图片 
   public void paint(graphics g){ 
      g.drawimage(img[nowimageindex],coordinatex,coordinatey,currentwidth,currentheight,this); 
 
   } 
  } 
 } 

 以上就是关于java的图形设计以及多媒体处理的全部内容,希望对大家的学习有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网