16第十六章 多媒体技术实验代码.docx
《16第十六章 多媒体技术实验代码.docx》由会员分享,可在线阅读,更多相关《16第十六章 多媒体技术实验代码.docx(23页珍藏版)》请在冰点文库上搜索。
16第十六章多媒体技术实验代码
实验十六多媒体技术
程序清单16-1
//AnimationText.java
1importjavax.swing.*;
2importjava.awt.*;
3publicclassAnimationTextextendsJApplet{
4Stringstring="Welcome,Javaworld";
5intx,y;
6publicvoidinit(){
7this.setBackground(Color.red);
8this.setForeground(Color.white);
9x=0;
10y=100;
11}
12publicvoidpaint(Graphicsg){
13g.drawString(string,x,y);
14try{
15x+=10;
16if(x>=500)
17x=0;
18}catch(InterruptedExceptione){
19}
20repaint();
21}
22}
程序清单16-2
---AnimationText.html--->
文字动画
程序清单16-3
//AnimationText2.java
importjavax.swing.*;
importjava.awt.*;
importjava.awt.event.*;
publicclassAnimationText2extendsJPanelimplementsActionListener{
Timertimer;//timer驱动动画;
intx,y;//文字的坐标
Stringstring="进入动画世界";//要显示文字
publicAnimationText2(){
x=0;
y=100;
this.setBackground(Color.black);
this.setForeground(Color.red);
}
publicvoidpaintComponent(Graphicsg){
代码1;//清除上次的绘制内容;
g.setFont(newFont("姚体",Font.BOLD,30));
g.drawString(string,x,y);
x+=10;
if(x>400)x=0;
}
publicvoidstartAnimation(){
if(timer==null){
timer=代码2;//创建一个定时器timer;
代码3;//timer开始驱动动画;
}
if(!
timer.isRunning()){//如果timer没有运行;
代码4;//让timer重新开始驱动动画;
}
}
publicvoidstopAnimation(){
timer.stop();
}
publicDimensiongetMinmumSize(){
returngetPerferredSize();
}
publicDimensiongetPerferredSize(){
returnnewDimension(200,200);
}
publicvoidactionPerformed(ActionEvente){
repaint();//重新绘制文字
}
publicstaticvoidmain(String[]args){
AnimationText2text2=newAnimationText2();
JFrameframe=newJFrame("文字动画演示");
Containercontainer=frame.getContentPane();
container.add(text2);
frame.setSize(400,240);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
text2.startAnimation();
}
}
程序清单16-4:
//PicturePoint1.java
importjavax.swing.*;
importjava.awt.*;
publicclassPicturePoint1extendsJApplet{
Imageimage;
publicvoidinit(){
image=getImage(getDocumentBase(),"Sunset.jpg");
}
publicvoidpaint(Graphicsg){
g.drawImage(image,0,0,this);
}
}
}
程序清单16-5:
---PicturePoint1.html--->
一张图片的显示
程序清单16-6:
//ImagePoint.java
importjavax.swing.*;
importjava.awt.*;
importjava.awt.event.*;
importjava.util.ArrayList;
importjava.io.*;
publicclassImagePointextendsJFrameimplementsActionListener{
JMenuBarmenuBar;
JMenupointMenu;
JMenuItemplayItem,setItem,exitItem;
Imageimage[];
JDesktopPanedesktop=newJDesktopPane();
publicImagePoint(){
super("幻灯片1.0");
createMenu();//创建菜单;
initImage();//装载图片;
}
publicvoidinitImage(){//装载图片;
try{
image=newImage[13];
for(inti=0;i代码1;
//创建image[i]象,将image路径下的图形文件文件Ti.gif(i:
为1~13)装入;
}
}catch(Exceptionex){
System.err.println("装载图片失败");
System.exit(0);
}
}
publicvoidcreateMenu(){//定义菜单;
menuBar=newJMenuBar();
pointMenu=newJMenu("幻灯片");
playItem=newJMenuItem("观看放映");
playItem.addActionListener(this);
setItem=newJMenuItem("设置放映图片");
setItem.addActionListener(this);
exitItem=newJMenuItem("退出");
exitItem.addActionListener(this);
pointMenu.add(playItem);
pointMenu.add(setItem);
pointMenu.add(exitItem);
menuBar.add(pointMenu);
setJMenuBar(menuBar);//设置菜单;
}
publicvoidactionPerformed(ActionEvente){
if(e.getSource()==playItem){//放映幻灯片;
JInternalFrameinterFrame=
newJInternalFrame("幻灯片放映",true,true,true,true);
//定义幻灯片放映窗口;
ImagePaneimagePane=newImagePane();//创建一个放映面板;
interFrame.add(imagePane);//将放映面板放置到窗口中
interFrame.setVisible(true);//设置窗口可视
interFrame.setSize(200,200);//设置窗口的尺寸200×200
desktop.add(interFrame);//将放映窗口放入桌面面板;
getContentPane().add(desktop);//将桌面面板放入容器
imagePane.startAnimation();//开始放映
}
elseif(e.getSource()==exitItem){
System.exit(0);
}
}
publicstaticvoidmain(String[]args){
ImagePointpoint=newImagePoint();
point.setSize(400,500);
point.setVisible(true);
point.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
/*定义一个内部类ImagePane,图形面版*/
classImagePaneextendsJPanelimplementsActionListener{
publicImagePane(){
setSize(100,100);
setVisible(true);
}
publicvoidplayImage(Graphicsg){//按顺序绘制图片;
for(inti=0;ig.drawImage(image[i],0,0,this);
width=image[i].getWidth(this);
height=image[i].getHeight(this);
}
}
publicvoidpaint(Graphicsg){
代码2//放映图片;
}
publicvoidstartAnimation(){//开始动画处理;
if(timer==null){
timer=newTimer(1000,this);
timer.start();
}
if(!
timer.isRunning()){
timer.restart();
}
}
publicvoidstopAnimation(){//停止动画处理
timer.stop();
}
publicvoidactionPerformed(ActionEvente){
代码3;//重新绘制动画;
}
}
}
程序清单16-17:
//Reflection.java
importjava.awt.*;
importjavax.swing.*;
publicclassReflectionextendsJAppletimplementsRunnable{
Imageimage,buffer;
GraphicsgContext;
Threadthread;
intwidth,height;
//Initializetheapplet
publicvoidinit(){
image=getImage(getCodeBase(),"T1.gif");
MediaTrackertracker=代码1;//创建图像加载跟踪器
tracker.代码2;//添加要跟踪的图片;
try{
tracker.waitForID(0);//等待图片加载完毕;
}
catch(InterruptedExceptione){}
width=代码3;//获得图片image的宽度;
height=代码4;//获得图片image的高度;
buffer=createImage(2*width,height);//创建缓冲区,是原始图片2倍宽度;
gContext=buffer.getGraphics();//创建缓冲区的图形上下文/后台屏幕。
gContext.代码5;//将图片按照同样大小绘制到后台屏幕;
for(inti=0;igContext.copyArea(0,i,width,1,width,(height-1)-2*i);
}
代码6;//清除后台屏幕的左半部;
}
publicvoidstart(){
if(thread==null)
thread=newThread(this);
thread.start();
}
publicvoidstop(){
if(thread!
=null)
thread=null;
}
publicvoidrun(){
intdy,num=0;
doubled;
while(true){
d=num*Math.PI/6;
for(inti=0;idy=(int)((i/12.0D+1)*Math.sin(height/12.0D*(height-i)/(i+1)+d));
//计算y轴的偏移量;
gContext.copyArea(width,i+dy,width,1,-width,-dy);
//从左到右复制生成波纹;
}
repaint();//重新绘制;
num=++num%12;
try{
Thread.代码7;//设置时间间隔;
}
catch(InterruptedExceptione){
}
}
}
publicvoidupdate(Graphicsg){
paint(g);
}
publicvoidpaint(Graphicsg){
g.代码8;//从坐标(0,0)处绘制原始图片image;
g.代码9;//将缓冲区图片buffer绘制到原始图片的正下方;
}
}
程序清单16-8:
//SoundTest.java
importjava.applet.*;
importjava.awt.*;
importjava.awt.event.*;
importjavax.swing.*;
publicclassSoundTestextendsJFrameimplementsActionListener{
JButtonplayButton;
JButtonloopButton;
JButtonstopButton;
JLabelstatusLabel;
JPanelbuttonPanel;
Containercontainer;
StringbgMusicName="flourish.mid";
AudioClipbgMusicClipOnce,bgMusicClipLoop;
booleanloopStatus;
publicSoundTest(){
super("简易音乐播放器");
container=getContentPane();
container.setLayout(newBorderLayout());
playButton=newJButton("播放");//定义播放按钮
loopButton=newJButton("循环");//定义循环播放按钮
stopButton=newJButton("停止");//定义停止播放按钮
playButton.addActionListener(this);
loopButton.addActionListener(this);
stopButton.addActionListener(this);
buttonPanel=newJPanel();
buttonPanel.add(playButton);
buttonPanel.add(loopButton);
buttonPanel.add(stopButton);//将按钮放置到buttonPanel中;
container.add(buttonPanel,BorderLayout.NORTH);
statusLabel=newJLabel("状态:
装载声音");//设置状态标签
container.add(statusLabel,BorderLayout.SOUTH);
//放置状态面板到窗口最下方
loadMusic();//装载音乐;
setSize(500,500);
setVisible(true);
}
publicvoidloadMusic(){
try{
bgMusicClipOnce=代码1;//装载一次播放的背景音乐flourish.mid
bgMusicClipLoop=代码2;//装载可循环播放的背景音乐flourish.mid
}catch(Exceptione){
System.err.println("不能打开声音文件!
!
!
");
}
}
publicvoidstartMusic(){//开始播放声音
if(loopStatus)//如果循环播放状态;
bgMusicClipLoop.代码3;//循环播放声音bgMusicClipLoop
else
bgMusicClipOnce.代码4;//播放一次声音bgMusicClipOnce
}
publicvoidloopMusic(){//循环播放声音
if(!
loopStatus){//如果当前状态是非循环状态
loopStatus=true;//改变为循环状态为真;
bgMusicClipOnce.代码5;//一次播放声音片段bgMusicClipOnce停止;
bgMusicClipLoop.代码6;//循环播放声音片段bgMusicClipLoop播放;
}
bgMusicClipLoop.代码7;/声音片段bgMusicClipLoop/循环播放;
}
publicvoidstopMusic(){//停止播放声音;
if(loopStatus){
bgMusicClipLoop.stop();
}
bgMusicClipOnce.stop();
}
publicvoidactionPerformed(ActionEvente){
if(e.getSource()==playButton){
startMusic();
playButton.setEnabled(false);
loopButton.setEnabled(true);
stopButton.setEnabled(true);
statusLabel.setText("状态:
播放....");
}
elseif(e.getSource()==loopButton){
loopMusic();
loopButton.setEnabled(false);
playButton.setEnabled(true);
stopButton.setEnabled(true);
statusLabel.setText("状态:
循环播放中....");
}
elseif(e.getSource()==stopButton){
stopMusic();
stopButton.setEnabled(false);
playButton.setEnabled(true);
loopButton.setEnabled(true);
statusLabel.setText("状态:
停止....");
}
}
publicstaticvoidmain(Stringargs[]){
SoundTestst=newSoundTest();
st.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
程序清单16-9:
//MusicCalendar.java
importjava.awt.*;
importjavax.swing.*;
importjava.util.*;
importjava.awt.event.*;
importjava.applet.*;
importjavax.swing.event.*;
publicclassMusicCalendarextendsJFrame
implementsActionListener,ChangeListener,ItemListener{
//定义GUI组件
JPanelmusicPanel,calendarPanel,controlPanel;
//定义音乐面板musicPanel、日历显示面板calendarPanel、日历控制面板controlPanel
JLabelmusicLabel;//“选择音乐“标签
JComboBoxmusicBx;//音乐选择下拉框
JButtonplayBtn,stopBtn;//定义播放和停止按钮;
JLabel[]title=newJLabel[7];//定义日历的星期标签,从星期日~星期六;
JLabel[]day=newJLabel[42];//定义日历的日期标签;
JSpinneryearSpinner;//定义拾取列表组件yearSpinner,进行年份选择;
JComboBoxmonthBx;//下拉列表,定义月份选择;
JLabelyearLabel,monthLabel;//定义年标签yearLabel,月标签monthLabel;
//定义音乐控制;
AudioClipmusicClip[];
String[]musicName={"1.wav","2.wav",