动画图形界面综合应用_第1页
动画图形界面综合应用_第2页
动画图形界面综合应用_第3页
动画图形界面综合应用_第4页
动画图形界面综合应用_第5页
已阅读5页,还剩20页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

动画图形界面综合应用第一页,共二十五页,2022年,8月28日内容介绍20.1任务预览20.2气球飘飘20.3图像幻灯片20.4动画20.5本章小结20.6实训20:编写动画程序第二页,共二十五页,2022年,8月28日20.1任务预览本章实训程序运行结果:

第三页,共二十五页,2022年,8月28日20.2气球飘飘【例20-1】编写在窗框绘制椭圆的程序,要求在窗框上绘制10个彩色椭圆,各椭圆大小和位置不一,但最大不超过窗框尺寸的五分之一,并且各椭圆的色彩不尽相同。分析:各椭圆位置和大小不一,色彩不一,涉及到随机数问题。

第四页,共二十五页,2022年,8月28日classFrame1extendsJFrame{//窗框类

Randomrand=newRandom();//随机对象

publicFrame1(){ this.setTitle("随机绘制10个彩色椭圆"); this.setBounds(100,100,300,250); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true);} publicvoidpaint(Graphicsg){//绘制方法

intwidth,height;//窗框宽、高

width=this.getWidth();height=this.getHeight(); g.setColor(Color.WHITE);g.fillRect(0,0,width,height); intx,y,w,h;Colorcolor;//椭圆左、上、宽、高与颜色

for(inti=1;i<=10;i++){ x=rand.nextInt(width);y=rand.nextInt(height); w=rand.nextInt(width/5);h=rand.nextInt(height/5); color=newColor(rand.nextInt(256), rand.nextInt(256),rand.nextInt(256)); g.setColor(color);g.drawOval(x,y,w,h);}}}第五页,共二十五页,2022年,8月28日【例20-2】编写“气球飘飘”程序:在窗框中每隔一定时间(如半秒)随机产生10个模拟气球的实心椭圆。各气球大小、位置和色彩不一,最大不超过窗框尺寸的五分之一。本例代码绝大部分与例20-1相同。

第六页,共二十五页,2022年,8月28日classFrame2extendsJFrame{//窗框类

Randomrand=newRandom();//随机对象

publicFrame2(){…}//构造方法

publicvoidpaint(Graphicsg){//绘制方法

intwidth,height;//窗框宽、高

… intx,y,w,h;…//椭圆左、上、宽、高

for(inti=1;i<=10;i++){//循环10次

x=rand.nextInt(width);… color=newColor(rand.nextInt(256),…);//颜色随机

g.setColor(color);g.fillOval(x,y,w,h); } try{Thread.sleep(500);//休眠500毫秒,即半秒

}catch(InterruptedExceptione){} this.repaint();//重绘

}}第七页,共二十五页,2022年,8月28日运用多线程编写“气球飘飘”程序,修改例20-2代码:classFrame2extendsJFrameimplementsRunnable{ ... publicvoidrun(){//线程运行方法

while(true){ try{Thread.sleep(500);//休眠500毫秒,即半秒

}catch(InterruptedExceptione){} this.repaint();//重绘

}} publicvoidpaint(Graphicsg){...}//去掉sleep和repaint语句}publicclassExample2{//主类

publicstaticvoidmain(String[]args){ Frame2frame=newFrame2(); Threadthread=newThread(frame);//构建线程

thread.start();}}第八页,共二十五页,2022年,8月28日20.3图像幻灯片【例20-3】编写图像幻灯片程序,运行时循环放映6幅存放在文件中的图像。说明:为方便编程,各个图像文件统一格式命名,如命名为child0.jpg~child5.jpg,并放在图像文件夹images中。在Eclipse环境中编程,把images放在项目的根目录中。不能直接显示图像文件,必须构建6个图像对象。使用数组来存放。

第九页,共二十五页,2022年,8月28日classFrame3extendsJFrame{//窗框类

Image[]imgs=newImage[6];//构建图像数组

intindex=0;//数组索引

publicFrame3(){…}//构造方法

publicvoidinitialize(){//初始化方法

for(inti=0;i<6;i++){ imgs[i]=Toolkit.getDefaultToolkit(). createImage("images/child"+i+".jpg"); }} publicvoidpaint(Graphicsg){//绘制方法

g.setColor(Color.WHITE); g.fillRect(0,0,this.getWidth(),this.getHeight()); g.drawImage(imgs[index],10,40,120,150,this); try{Thread.sleep(500);//休眠500毫秒

}catch(InterruptedExceptione){} index++;if(index==6){index=0;}//索引循环

this.repaint();}}第十页,共二十五页,2022年,8月28日【例20-4】编写可控制的图像幻灯片程序,通过工具栏的5个按钮“放映”、“停止”、“定时”、“上翻”和“下翻”,以控制图像的放映状态。classFrame4extendsJFrame{//窗框类

JToolBartoolBar=newJToolBar("工具栏"); JButtonbuttonPlay=newJButton("放映"); JButtonbuttonStop=newJButton("停止"); JButtonbuttonTime=newJButton("定时"); JButtonbuttonUp=newJButton("上翻"); JButtonbuttonDwon=newJButton("下翻");

第十一页,共二十五页,2022年,8月28日

Image[]imgs=newImage[6];//构建图像数组

intindex=0;inttime=500; MyCanvascanvas=newMyCanvas();//自定义的画布

Threadthread;booleanstopFlag=true; publicFrame4(){…}//构造方法

publicvoidinitialize(){//初始化方法

for(inti=0;i<6;i++){ imgs[i]=Toolkit.getDefaultToolkit(). createImage("images/child"+i+".jpg"); } buttonPlay.addActionListener(newActionHandler());

… toolBar.add(buttonPlay);

… this.add(toolBar,BorderLayout.NORTH); this.add(canvas,BorderLayout.CENTER); buttonStop.setEnabled(false);//初始禁用“停止”按钮

}第十二页,共二十五页,2022年,8月28日

//按钮动作事件监听处理类(内部类):

classActionHandlerimplementsActionListener{ publicvoidactionPerformed(ActionEvente){ if(e.getSource()==buttonPlay){ thread=newThread(canvas);//构建线程

stopFlag=false; buttonPlay.setEnabled(false);//禁用“放映”按钮

buttonStop.setEnabled(true);//启用“停止”按钮

thread.start();//启动线程

} elseif(e.getSource()==buttonStop){//若是“停止”按钮

stopFlag=true;//设置停止标记

thread=null; buttonPlay.setEnabled(true);//启用“放映”按钮

buttonStop.setEnabled(false);//禁用“停止”按钮

}第十三页,共二十五页,2022年,8月28日

elseif(e.getSource()==buttonTime){//若是“定时”按钮

Stringstr=JOptionPane.showInputDialog( "请设定每幅图的放映时间(单位:毫秒)",time); if(str==null){return;}//若输入框按了“取消”按钮

try{intt=Integer.parseInt(str); if(t<0){thrownewException("警告:请输入正整数!");} time=t; }catch(Exceptionex){ JOptionPane.showMessageDialog(null,ex.getMessage()); } } elseif(e.getSource()==buttonUp){//若是“上翻”按钮

index--;if(index==-1){index=5;}//索引循环

canvas.repaint(); } elseif(e.getSource()==buttonDwon){//若是“下翻”按钮

index++;if(index==6){index=0;}//索引循环

canvas.repaint();}} }第十四页,共二十五页,2022年,8月28日

//自定义画布类(内部类),该类与线程关联:

classMyCanvasextendsCanvasimplementsRunnable{ publicvoidpaint(Graphicsg){//绘制方法

g.setColor(Color.WHITE); g.fillRect(0,0,this.getWidth(),this.getHeight()); g.drawImage(imgs[index],10,10,120,150,this); } publicvoidrun(){//线程运行方法

while(true){ if(stopFlag){break;}//停止放映

try{Thread.sleep(time);} catch(InterruptedExceptione){} index++; if(index==6){index=0;}//索引循环

this.repaint();//执行paint方法重绘}} }}第十五页,共二十五页,2022年,8月28日20.4动画动画:活动的图画。在屏幕上显示第一帧图片,隔一小段时间如24分之一秒(约42毫秒)再显示下一帧图片,如此循环往复。定时刷新椭圆“气球”、自动放映图像,都可以说是动画,因为画面不断在“动”。程序可实现动画。最简单动画是图像沿着一条固定轨迹作直线运动。为了渲染气氛,可以使用一个背景图作衬托。第十六页,共二十五页,2022年,8月28日20.4.1窗框实现动画【例20-5】编写“空中飞翔”动画程序:蓝天白云背景下,一个飞鸟从窗框右下角向左上角飞去,越飞越远,图像越来越小,消失后,下一飞鸟重复飞翔过程。分析:只需使用两个图像文件,一作背景,二是飞鸟(前景)。两个文件可放在Eclipse项目根目录的images目录。飞鸟往左上角方向运动越来越小,只需不断减少图像的显示尺寸便可。为增强飞翔效果,飞鸟使用GIF图像文件。

第十七页,共二十五页,2022年,8月28日classFrame5extendsJFrame{//窗框类

ImagebackImage,foreImage;//背景图、前景图

intx=240,y=240,width=80,height=80;//前景图位置、尺寸

publicFrame5(){//构造方法

this.setTitle("简单动画"); this.setBounds(100,100,300,300); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); initialize();//调用初始化方法

this.setVisible(true); } publicvoidinitialize(){//初始化方法

Toolkittoolkit=Toolkit.getDefaultToolkit(); backImage=toolkit.createImage("images/cloud.jpg"); foreImage=toolkit.createImage("images/flyer.gif"); }第十八页,共二十五页,2022年,8月28日

publicvoidpaint(Graphicsg){//绘制方法

g.drawImage(backImage,0,0,300,300,this); g.drawImage(foreImage,x,y,width,height,this); try{Thread.sleep(42);}//休眠42毫秒

catch(InterruptedExceptione){} x-=6;y-=6;width-=2;height-=2;//更改前景图位置和尺寸

if(x<0){ x=300;y=300;width=100;height=100;//位置尺寸复位

} this.repaint();//执行paint方法重绘}}publicclassExample5{//主类

publicstaticvoidmain(String[]args){ newFrame5();}}程序没有使用多线程,没有工具栏和按钮。第十九页,共二十五页,2022年,8月28日20.4.2面板实现动画【例20-6】改进例20-5的“空中飞翔”动画程序,添加工具栏和5个按钮:“放映”、“停止”、“定时”、“上飞”和“下飞”,控制图像放映状态。classFrame6extendsJFrame{ JToolBartoolBar=newJToolBar("工具栏"); JButtonbuttonPlay=newJButton("放映"); JButtonbuttonStop=newJButton("停止"); JButtonbuttonTime=newJButton("定时"); JButtonbuttonUp=newJButton("上飞"); JButtonbuttonDwon=newJButton("下飞"); inttime=42; MyPanelpan=newMyPanel();//自定义面板

Threadthread;//线程

booleanstopFlag=true; ImagebackImage,foreImage; intx=150,y=150,width=50,height=50;

第二十页,共二十五页,2022年,8月28日

publicFrame6(){…}//构造方法

publicvoidinitialize(){//初始化方法

Toolkittoolkit=Toolkit.getDefaultToolkit();//工具包

backImage=toolkit.createImage("images/cloud.jpg"); foreImage=toolkit.createImage("images/flyer.gif"); buttonPlay.addActionListener(newActionHandler()); buttonStop.addActionListener(newActionHandler()); buttonTime.addActionListener(newActionHandler()); buttonUp.addActionListener(newActionHandler()); buttonDwon.addActionListener(newActionHandler()); toolBar.add(buttonPlay);… this.add(toolBar,BorderLayout.NORTH); this.add(pan,BorderLayout.CENTER); buttonStop.setEnabled(false);//初始禁用“停止”按钮

}第二十一页,共二十五页,2022年,8月28日

//按钮动作事件监听处理类(内部类):classActionHandlerimplementsActionListener{ publicvoidactionPerformed(ActionEvente){ if(e.getSource()==buttonPlay){//“放映”按钮

thread=newThread(pan);stopFlag=false; buttonPlay.setEnabled(false);…//禁用“放映”按钮

thread.start();//启动线程

}elseif(e.getSource()==buttonStop){//“停止”按钮

stopFlag=true;thread=null; buttonPlay.setEnabled(true);…//启用“放映”按钮

}elseif(e.getSource()==buttonTime){//“定时”按钮

Stringstr=JOptionPane.showInputDialog( "请设定每幅图的放映时间(单位:毫秒)",time); if(str==null){return;}//若输入框按了“取消”按钮

try{intt=Integer.parseInt(str); if(t<0){thrownewException("警告:请输入正整数!");} time=t; }catch(Exceptionex){…showMessageDialog…;} }第二十二页,共二十五页,2022年,8月28日

elseif(e.getSource()==buttonU

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论