Java圖形設計和多媒體基礎

巫_1曲待續發表於2014-12-12

本文主要介紹了Java的圖形設計以及多媒體處理,原始碼作者也做了詳細的註釋,對於初學者應該不難。詳細請看下文。

同心圓效果圖:

/**  
 *程式要求:新建一個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);  

     }  
    }  
 }

相關文章