Java程序设计:图形与多媒体处理(2)
本文主要介绍了Java的图形设计以及多媒体处理,源码作者也做了详细的注释,对于初学者应该不难。
详细请看下文
播放音乐和切换图片的小程序效果图:
1. /**
2. *程序要求:编写一个Applet的小程序,准备5幅图片和三个音乐文件,绘制到Applet中,
3. *并增加几个按钮,控制图片的切换、放大、缩小和音乐文件的播放。
4. *作者:wwj
5. *日期:2012/4/29
6. *参考:neicole
7. *功能:能进行图片和歌曲的选择变换的applet小程序
8. */
10. import javax.swing.\*;
11. import java.awt.\*;
12. import java.awt.event.\*;
13. import java.applet.Applet;
14. import java.applet.AudioClip;
17. public class Ex7\_2 extends Applet implements ActionListener,ItemListener
18. {
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"};
42. //创建画布对象
43. MyCanvasl showPhotos;
47. public void init()
48. {
49. //窗口布局
50. this.setLayout(new BorderLayout());
52. //为图片控制按钮注册监听器
53. lastPic.addActionListener(this);
54. setLarge.addActionListener(this);
55. setLittle.addActionListener(this);
56. nextPic.addActionListener(this);
58. //向面板p1添加组件
59. p1.add(lastPic);
60. p1.add(setLarge);
61. p1.add(setLittle);
62. p1.add(nextPic);
63. p1.repaint();
65. //实例化下拉列表对象
66. xx = new JComboBox(names);
67. xx.addItemListener(this);
69. //为控制播放音乐按钮注册监听器
70. lastSound.addActionListener(this);
71. play.addActionListener(this);
72. loop.addActionListener(this);
73. stop.addActionListener(this);
74. nextSound.addActionListener(this);
76. for(int i=0;i<3;i++)
77. {
78. sound\[i\]=getAudioClip(getCodeBase(),"music/"+"曲目"
79. +Integer.toString(i+1)+".wav");
80. }
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();
93. showPhotos = new MyCanvasl();
94. p3.add(showPhotos);
95. p3.repaint();
97. //把面板p1和p2分别布置到窗口的北部和南部
98. add(p1,BorderLayout.NORTH);
99. add(p2,BorderLayout.SOUTH);
100. add(p3,BorderLayout.CENTER);
102. this.repaint();
104. }
107. //按钮的事件处理
108. public void actionPerformed(ActionEvent e)
109. {
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. }
125. else if(e.getSource()==lastSound){ //上一首
126. sound\[playingSong\].stop();
127. playingSong=(playingSong-1+3)%3;
128. xx.setSelectedIndex(playingSong);
129. sound\[playingSong\].play();
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();
147. }
148. }
151. //下拉列表的事件处理
152. public void itemStateChanged(ItemEvent e)
153. {
155. sound\[playingSong\].stop();
156. sound\[playingSong\]=getAudioClip(getCodeBase(),"music/"+xx.getSelectedItem());
157. }
159. class MyCanvasl extends Canvas
160. {
162. public Image\[\] img=new Image\[5\];
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;
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. }
182. private void changePhotoIndex(int index){
183. nowImageIndex = index;
184. changePhotoSize('M');
185. }
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. }
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);
225. }
226. }
227. }
原文链接:http://blog.csdn.net/wwj_748/article/details/7522672
» 本文链接:https://blog.apires.cn/archives/764.html
» 转载请注明来源:Java地带
» 《Java程序设计:图形与多媒体处理(2)》
» 本文章为Java地带整理创作,欢迎转载!转载请注明本文地址,谢谢!
» 部分内容收集整理自网络,如有侵权请联系我删除!
» 订阅本站:https://blog.apires.cn/feed/