栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java设计图形与多媒体处理

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java设计图形与多媒体处理

本文实现了两个效果:

第一种,同心圆效果图:

 
 
 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();    
   } 
 
 } 

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

 
 
 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的图形设计以及多媒体处理的全部内容,希望对大家的学习有所帮助。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/151228.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号