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

开发 后端
本文主要介绍了Java的图形设计以及多媒体处理,源码作者也做了详细的注释,对于初学者应该不难。详细请看下文

同心圆效果图:

  1. /**  
  2.  *程序要求:新建一个600*600像素的应用程序窗口,并在窗口中绘制5个不同颜色的同心圆,  
  3.  *所有圆心都是屏幕的中心点,相邻两个圆直接的半径相差50像素  
  4.  *效果图如下图所示(颜色随机设置),源程序保存为Ex7_1.java。  
  5.  *作者:wwj  
  6.  *日期:2012/4/25  
  7.  *功能:显示一个有5个不同颜色的同心圆  
  8.  **/ 
  9.  
  10.  import javax.swing.*;  
  11.  import java.awt.*;  
  12.  import java.awt.Color;  
  13.  public class Ex7_1 extends JFrame  
  14.  {  
  15.      int red,green,blue;  
  16.      Color color;  
  17.  
  18.      public Ex7_1()  
  19.      {  
  20.          super("一个有5个不同颜色的同心圆");    //显示窗口名称  
  21.          setSize(600,600);                      //设置窗口大小  
  22.          setVisible(true);                      //设置为可见  
  23.          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置窗口关闭动作  
  24.       
  25.      }  
  26.  
  27.       
  28.      public void paint(Graphics g)  
  29.      {  
  30.          //第一个圆  
  31.         red=(int)(Math.random()*255);  
  32.         green=(int)(Math.random()*255);  
  33.         blue=(int)(Math.random()*255);  
  34.         color=new Color(red,green,blue);  
  35.         g.setColor(color);  
  36.         g.fillOval(175,175,250,250);  
  37.         //第二个圆  
  38.         red=(int)(Math.random()*255);  
  39.         green=(int)(Math.random()*255);  
  40.         blue=(int)(Math.random()*255);  
  41.         color=new Color(red,green,blue);  
  42.         g.setColor(color);  
  43.         g.fillOval(200,200,200,200);  
  44.         //第三个圆  
  45.         red=(int)(Math.random()*255);  
  46.         green=(int)(Math.random()*255);  
  47.         blue=(int)(Math.random()*255);  
  48.         color=new Color(red,green,blue);  
  49.         g.setColor(color);  
  50.         g.fillOval(225,225,150,150);  
  51.         //第四个圆  
  52.         red=(int)(Math.random()*255);  
  53.         green=(int)(Math.random()*255);  
  54.         blue=(int)(Math.random()*255);  
  55.         color=new Color(red,green,blue);  
  56.         g.setColor(color);  
  57.         g.fillOval(250,250,100,100);  
  58.         //第五个圆  
  59.         red=(int)(Math.random()*255);  
  60.         green=(int)(Math.random()*255);  
  61.         blue=(int)(Math.random()*255);  
  62.         color=new Color(red,green,blue);  
  63.         g.setColor(color);  
  64.         g.fillOval(275,275,50,50);  
  65.  
  66.      }         
  67.       
  68.      public static void main(String[] args)  
  69.      {  
  70.          Ex7_1 e = new Ex7_1();       
  71.      }  
  72.  
  73.  } 

#p#

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

 

  1. /**  
  2.  *程序要求:编写一个Applet的小程序,准备5幅图片和三个音乐文件,绘制到Applet中,  
  3.  *并增加几个按钮,控制图片的切换、放大、缩小和音乐文件的播放。  
  4.  *作者:wwj  
  5.  *日期:2012/4/29  
  6.  *参考:neicole  
  7.  *功能:能进行图片和歌曲的选择变换的applet小程序  
  8.  **/ 
  9.  
  10.  import javax.swing.*;  
  11.  import java.awt.*;  
  12.  import java.awt.event.*;  
  13.  import java.applet.Applet;  
  14.  import java.applet.AudioClip;  
  15.  
  16.    
  17.  public class Ex7_2 extends Applet implements ActionListener,ItemListener  
  18.  {  
  19.  
  20.      //创建两个面板  
  21.      JPanel p1=new JPanel();  
  22.      JPanel p2=new JPanel();  
  23.      JPanel p3=new JPanel();  
  24.      //声音对象  
  25.      AudioClip[] sound=new AudioClip[3];  
  26.      int playingSong=0;  
  27.      //切换图片的按钮  
  28.      JButton lastPic=new JButton("上一张");  
  29.      JButton setLarge=new JButton("放大");  
  30.      JButton setLittle=new JButton("缩小");  
  31.      JButton nextPic=new JButton("下一张");  
  32.      //切换歌曲的按钮  
  33.      JButton lastSound=new JButton("上一首");  
  34.      JButton play=new JButton("播放");  
  35.      JButton loop=new JButton("连续");  
  36.      JButton stop=new JButton("停止");  
  37.      JButton nextSound=new JButton("下一首");  
  38.      //曲目下拉列表  
  39.      JComboBox xx;  
  40.      String names[]={ "曲目1.wav","曲目2.wav","曲目3.wav"};  
  41.       
  42.     //创建画布对象  
  43.     MyCanvasl showPhotos;  
  44.  
  45.        
  46.  
  47.      public void init()  
  48.      {  
  49.          //窗口布局  
  50.          this.setLayout(new BorderLayout());  
  51.  
  52.          //为图片控制按钮注册监听器  
  53.          lastPic.addActionListener(this);  
  54.          setLarge.addActionListener(this);  
  55.          setLittle.addActionListener(this);  
  56.          nextPic.addActionListener(this);  
  57.  
  58.          //向面板p1添加组件  
  59.          p1.add(lastPic);  
  60.          p1.add(setLarge);  
  61.          p1.add(setLittle);  
  62.          p1.add(nextPic);  
  63.          p1.repaint();  
  64.       
  65.         //实例化下拉列表对象  
  66.         xx = new JComboBox(names);  
  67.         xx.addItemListener(this);  
  68.  
  69.         //为控制播放音乐按钮注册监听器  
  70.         lastSound.addActionListener(this);  
  71.         play.addActionListener(this);  
  72.         loop.addActionListener(this);  
  73.         stop.addActionListener(this);  
  74.         nextSound.addActionListener(this);  
  75.  
  76.         for(int i=0;i<3;i++)  
  77.          {  
  78.             sound[i]=getAudioClip(getCodeBase(),"music/"+"曲目" 
  79.                     +Integer.toString(i+1)+".wav");  
  80.          }  
  81.           
  82.  
  83.           
  84.         //向面板p2添加组件  
  85.          p2.add(xx);  
  86.          p2.add(lastSound);  
  87.          p2.add(play);  
  88.          p2.add(loop);  
  89.          p2.add(stop);  
  90.          p2.add(nextSound);  
  91.          p2.repaint();  
  92.           
  93.         showPhotos = new MyCanvasl();  
  94.         p3.add(showPhotos);  
  95.          p3.repaint();  
  96.  
  97.         //把面板p1和p2分别布置到窗口的北部和南部   
  98.          add(p1,BorderLayout.NORTH);  
  99.          add(p2,BorderLayout.SOUTH);  
  100.          add(p3,BorderLayout.CENTER);  
  101.  
  102.          this.repaint();  
  103.  
  104.      }  
  105.  
  106.  
  107.      //按钮的事件处理  
  108.      public void actionPerformed(ActionEvent e)  
  109.      {  
  110.  
  111.           
  112.         if(e.getSource() == lastPic){  
  113.             showPhotos.changePhotoShow('P');  
  114.         }  
  115.         else if(e.getSource() == nextPic){  
  116.             showPhotos.changePhotoShow('N');  
  117.         }  
  118.         else if(e.getSource() == setLarge){  
  119.             showPhotos.changePhotoSize('B');  
  120.         }  
  121.         else if(e.getSource() == setLittle){  
  122.             showPhotos.changePhotoSize('S');  
  123.         }  
  124.       
  125.         else if(e.getSource()==lastSound){  //上一首  
  126.             sound[playingSong].stop();  
  127.             playingSong=(playingSong-1+3)%3;  
  128.             xx.setSelectedIndex(playingSong);  
  129.             sound[playingSong].play();  
  130.  
  131.         }  
  132.         else if(e.getSource()==play){       //按下播放按钮  
  133.             sound[playingSong].play();  
  134.         }  
  135.         else if(e.getSource()==loop){       //按下循环按钮  
  136.             sound[playingSong].loop();  
  137.         }  
  138.         else if(e.getSource()==stop){       //按下停止按钮  
  139.             sound[playingSong].stop();  
  140.         }  
  141.         else{                               //下一首  
  142.             sound[playingSong].stop();  
  143.             playingSong=(playingSong+1)%3;  
  144.             xx.setSelectedIndex(playingSong);  
  145.             sound[playingSong].play();  
  146.  
  147.         }     
  148.      }  
  149.  
  150.  
  151.      //下拉列表的事件处理  
  152.      public void itemStateChanged(ItemEvent e)  
  153.      {  
  154.            
  155.          sound[playingSong].stop();  
  156.          sound[playingSong]=getAudioClip(getCodeBase(),"music/"+xx.getSelectedItem());  
  157.      }  
  158.  
  159.     class MyCanvasl extends Canvas  
  160.     {  
  161.           
  162.         public Image[] img=new Image[5];  
  163.  
  164.         int MaxWidth = 600;  
  165.         int MaxHeight = 500;  
  166.         int nowImageIndex = 0;  
  167.         int coordinateX = 0;  
  168.         int coordinateY = 0;  
  169.         int currentWidth = MaxWidth;  
  170.         int currentHeight = MaxHeight;  
  171.  
  172.           
  173.         MyCanvasl(){  
  174.          setSize(MaxWidth,MaxHeight);  
  175.          //获取当前目录下的图片  
  176.          for(int i=0;i<5;i++){  
  177.              img[i]=getImage(getCodeBase(),"image/"+Integer.toString(i+1)+".jpg");  
  178.          }  
  179.         }  
  180.  
  181.  
  182.         private void changePhotoIndex(int index){  
  183.             nowImageIndex = index;  
  184.             changePhotoSize('M');  
  185.         }  
  186.  
  187.  
  188.  
  189.         public void changePhotoShow(char command){  
  190.             if('P' == command){  
  191.                 changePhotoIndex((nowImageIndex + 5 - 1 ) % 5);  
  192.             }  
  193.             else if('N' == command){  
  194.                 changePhotoIndex((nowImageIndex + 1) % 5);  
  195.             }  
  196.         }  
  197.           
  198.  
  199.  
  200.          public void changePhotoSize(char command){  
  201.             if ('M' == command){  
  202.                 currentWidth = MaxWidth;  
  203.                 currentHeight = MaxHeight;  
  204.             }  
  205.             else if ('B' == command){  
  206.                 if(MaxWidth >= (currentWidth + 100) && MaxHeight >= (currentHeight + 100)){  
  207.                     currentWidth += 100;  
  208.                     currentHeight += 100;  
  209.                 }  
  210.             }  
  211.             else if('S' == command){  
  212.                 if((0 < (currentWidth - 100)) && (0 < (currentHeight - 100))){  
  213.                     currentWidth = currentWidth - 100;  
  214.                     currentHeight = currentHeight - 100;  
  215.                 }  
  216.             }  
  217.             coordinateX = (MaxWidth - currentWidth) / 2;  
  218.             coordinateY = (MaxHeight - currentHeight) / 2;  
  219.             repaint();  
  220.         }  
  221.             //paint方法用来在窗口显示图片  
  222.      public void paint(Graphics g){  
  223.            g.drawImage(img[nowImageIndex],coordinateX,coordinateY,currentWidth,currentHeight,this);  
  224.  
  225.      }  
  226.     }  
  227.  }  

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

【编辑推荐】

  1. Java集合框架总结:TreeSet类的排序问题
  2. 使用JNI进行混合编程:在C/C++中调用Java代码
  3. Java理论与实践: Web层的状态复制
  4. Java代码编写的30条建议
  5. Java Excel API及详细教程
责任编辑:林师授 来源: wwj_748的博客
相关推荐

2014-07-16 16:17:00

2023-08-15 13:57:08

开发者

2010-09-17 09:08:49

Java多线程

2010-09-25 13:47:14

Java跨平台

2009-02-10 09:53:41

多线程程序设计Java

2013-08-28 16:08:19

多媒体Windows8.1

2011-05-03 09:25:39

程序设计

2011-07-04 13:31:15

2011-04-18 09:22:38

多线程

2011-07-22 13:41:57

java

2010-06-30 10:38:05

2011-12-01 11:24:13

2013-12-12 16:30:20

Lua脚本语言

2011-06-09 10:07:28

Qt phonon

2010-01-11 10:34:22

C++程序

2010-04-08 21:14:41

多媒体视频信息通信红杉树网络

2009-06-22 14:03:00

java教材程序设计

2009-12-04 10:53:06

VS WEB

2010-12-28 10:12:39

PHP
点赞
收藏

51CTO技术栈公众号