动画——图形界面综合应用

上传人:san****019 文档编号:70211313 上传时间:2019-01-16 格式:PPT 页数:25 大小:1.41MB
返回 下载 相关 举报
动画——图形界面综合应用_第1页
第1页 / 共25页
动画——图形界面综合应用_第2页
第2页 / 共25页
动画——图形界面综合应用_第3页
第3页 / 共25页
动画——图形界面综合应用_第4页
第4页 / 共25页
动画——图形界面综合应用_第5页
第5页 / 共25页
点击查看更多>>
资源描述

《动画——图形界面综合应用》由会员分享,可在线阅读,更多相关《动画——图形界面综合应用(25页珍藏版)》请在金锄头文库上搜索。

1、第20章 动画图形界面 综合应用,能力目标: 能编写“气球飘飘”程序,定时播放若干个大小不等的彩色椭圆。 能编写图像幻灯片程序,并结合多线程,设定时间间隔自动放映。 能编写“空中飞翔”动画程序,并能设定间隔时间以控制放映的速度,还能手工定格动画画面。,内容介绍,20.1 任务预览 20.2 气球飘飘 20.3 图像幻灯片 20.4 动画 20.5 本章小结 20.6 实训20:编写动画程序,20.1 任务预览,本章实训程序运行结果:,20.2 气球飘飘,【例20-1】编写在窗框绘制椭圆的程序,要求在窗框上绘制10个彩色椭圆,各椭圆大小和位置不一,但最大不超过窗框尺寸的五分之一,并且各椭圆的色彩

2、不尽相同。 分析:各椭圆位置和大小不一,色彩不一,涉及到随机数问题。,class Frame1 extends JFrame /窗框类 Random rand = new Random(); /随机对象 public Frame1() this.setTitle(“随机绘制10个彩色椭圆“); this.setBounds(100, 100, 300, 250); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); public void paint(Graphics g) /绘制方法 int

3、width, height; /窗框宽、高 width = this.getWidth(); height = this.getHeight(); g.setColor(Color.WHITE); g.fillRect(0, 0, width, height); int x, y, w, h; Color color; /椭圆左、上、宽、高与颜色 for (int i=1; i=10; i+) x = rand.nextInt(width); y = rand.nextInt(height); w = rand.nextInt(width/5); h = rand.nextInt(height

4、/5); color = new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256); g.setColor(color); g.drawOval(x, y, w, h); ,【例20-2】编写“气球飘飘”程序:在窗框中每隔一定时间(如半秒)随机产生10个模拟气球的实心椭圆。,各气球大小、位置和色彩不一,最大不超过窗框尺寸的五分之一。 本例代码绝大部分与例20-1相同。,class Frame2 extends JFrame /窗框类 Random rand = new Random(); /随机对象 public Fra

5、me2() /构造方法 public void paint(Graphics g) /绘制方法 int width, height; /窗框宽、高 int x, y, w, h; /椭圆左、上、宽、高 for (int i=1; i=10; i+) /循环10次 x = rand.nextInt(width); color = new Color(rand.nextInt(256), ); /颜色随机 g.setColor(color); g.fillOval(x, y, w, h); try Thread.sleep(500); /休眠500毫秒,即半秒 catch(InterruptedE

6、xception e) this.repaint(); /重绘 ,运用多线程编写“气球飘飘”程序,修改例20-2代码: class Frame2 extends JFrame implements Runnable . public void run() /线程运行方法 while(true) try Thread.sleep(500); /休眠500毫秒,即半秒 catch(InterruptedException e) this.repaint(); /重绘 public void paint(Graphics g) . /去掉sleep和repaint语句 public class Ex

7、ample2 /主类 public static void main(String args) Frame2 frame = new Frame2(); Thread thread = new Thread(frame); /构建线程 thread.start(); ,20.3 图像幻灯片,【例20-3】编写图像幻灯片程序,运行时循环放映6幅存放在文件中的图像。 说明:为方便编程,各个图像文件统一格式命名,如命名为child0.jpgchild5.jpg,并放在图像文件夹images中。 在Eclipse环境中编程,把images放在项目的根目录中。 不能直接显示图像文件,必须构建6个图像对象

8、。使用数组来存放。,class Frame3 extends JFrame /窗框类 Image imgs = new Image6; /构建图像数组 int index = 0; /数组索引 public Frame3() /构造方法 public void initialize() /初始化方法 for (int i=0; i6; i+) imgsi = Toolkit.getDefaultToolkit(). createImage(“images/child“+i+“.jpg“); public void paint(Graphics g) /绘制方法 g.setColor(Color

9、.WHITE); g.fillRect(0, 0, this.getWidth(), this.getHeight(); g.drawImage(imgsindex, 10, 40, 120, 150, this); try Thread.sleep(500); /休眠500毫秒 catch(InterruptedException e) index +; if (index=6) index = 0; /索引循环 this.repaint(); ,【例20-4】编写可控制的图像幻灯片程序,通过工具栏的5个按钮“放映”、“停止”、“定时”、“上翻”和“下翻”,以控制图像的放映状态。,class

10、 Frame4 extends JFrame /窗框类 JToolBar toolBar = new JToolBar(“工具栏“); JButton buttonPlay = new JButton(“放映“); JButton buttonStop = new JButton(“停止“); JButton buttonTime = new JButton(“定时“); JButton buttonUp = new JButton(“上翻“); JButton buttonDwon = new JButton(“下翻“);,Image imgs = new Image6; /构建图像数组 i

11、nt index = 0; int time = 500; MyCanvas canvas = new MyCanvas(); /自定义的画布 Thread thread; boolean stopFlag = true; public Frame4() /构造方法 public void initialize() /初始化方法 for (int i=0; i6; i+) imgsi = Toolkit.getDefaultToolkit(). createImage(“images/child“+i+“.jpg“); buttonPlay.addActionListener(new Acti

12、onHandler(); toolBar.add(buttonPlay); this.add(toolBar, BorderLayout.NORTH); this.add(canvas, BorderLayout.CENTER); buttonStop.setEnabled(false); /初始禁用“停止”按钮 ,/按钮动作事件监听处理类(内部类): class ActionHandler implements ActionListener public void actionPerformed(ActionEvent e) if(e.getSource()=buttonPlay) thre

13、ad = new Thread(canvas); /构建线程 stopFlag = false; buttonPlay.setEnabled(false); /禁用“放映”按钮 buttonStop.setEnabled(true); /启用“停止”按钮 thread.start(); /启动线程 else if(e.getSource()=buttonStop) /若是“停止”按钮 stopFlag = true; /设置停止标记 thread = null; buttonPlay.setEnabled(true); /启用“放映”按钮 buttonStop.setEnabled(false

14、); /禁用“停止”按钮 ,else if(e.getSource()=buttonTime) /若是“定时”按钮 String str = JOptionPane.showInputDialog( “请设定每幅图的放映时间(单位:毫秒)“, time); if (str = null) return; /若输入框按了“取消”按钮 try int t = Integer.parseInt(str); if (t0) throw new Exception(“警告:请输入正整数!“); time = t; catch(Exception ex) JOptionPane.showMessageDialog(null, ex.getMessage(); else if(e.getSource()=buttonUp) /若是“上翻”按钮 index -; if (index=-1) ind

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 高等教育 > 大学课件

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号