一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務器之家 - 編程語言 - JAVA教程 - Java設計圖形與多媒體處理

Java設計圖形與多媒體處理

2020-01-05 14:59lijiao JAVA教程

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

本文實現了兩個效果:

第一種,同心圓效果圖:

Java設計圖形與多媒體處理

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/**
 *程序要求:新建一個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();   
   }
 
 }

第二種,播放音樂和切換圖片的小程序效果圖:

Java設計圖形與多媒體處理

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/**
 *程序要求:編寫一個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的圖形設計以及多媒體處理的全部內容,希望對大家的學習有所幫助。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 午夜福利理论片高清在线 | 国产亚洲精aa在线观看香蕉 | caoporm碰最新免费公开视频 | 国产精品性视频免费播放 | 黄色aaa级片 | 国产良心大作白丝精厕 | 免费高清视频免费观看 | 日韩毛片免费 | 欧美一级级a在线观看 | 国产欧美又粗又猛又爽老 | 丰满艳妇亲伦视频 | 我被黑人彻底征服的全文 | 网站在线播放 | 美女下面揉出水免费视频 | 91精品手机国产露脸 | 青草草在线观看 | 天堂8在线天堂资源bt | 男人边吃奶边做好爽视频免费 | 亚洲人成网站在线观看青青 | 国产小视频免费看 | 99成人国产精品视频 | 国产裸舞在线一区二区 | 好紧好爽的午夜寂寞视频 | 精品AV综合导航 | 俄罗斯15一16处交 | 视频国产精品 | 午夜在线观看免费完整直播网页 | 久久这里有精品 | www视频免费看 | 2018成年动漫在线观看 | 色综合天天综合网站中国 | 国产精品午夜久久 | 好紧好爽范冰冰系列 | 午夜人妻理论片天堂影院 | 亚洲视频一区在线播放 | 九九国产在线视频 | 成人动漫影院 | 欧美日韩国产一区二区三区不卡 | 国产色图片 | 免费看国产一级特黄aa大片 | 国产精品九九免费视频 |