第20章动画——图形界面综合应用.pptVIP

  • 1
  • 0
  • 约1.16万字
  • 约 25页
  • 2017-05-29 发布于北京
  • 举报
第20章 动画——图形界面 综合应用 能力目标: 能编写“气球飘飘”程序,定时播放若干个大小不等的彩色椭圆。 能编写图像幻灯片程序,并结合多线程,设定时间间隔自动放映。 能编写“空中飞翔”动画程序,并能设定间隔时间以控制放映的速度,还能手工定格动画画面。 内容介绍 20.1 任务预览 20.2 气球飘飘 20.3 图像幻灯片 20.4 动画 20.5 本章小结 20.6 实训20:编写动画程序 20.1 任务预览 本章实训程序运行结果: 20.2 气球飘飘 【例20-1】编写在窗框绘制椭圆的程序,要求在窗框上绘制10个彩色椭圆,各椭圆大小和位置不一,但最大不超过窗框尺寸的五分之一,并且各椭圆的色彩不尽相同。 分析:各椭圆位置和大小不一,色彩不一,涉及到随机数问题。 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 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/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 Frame2(){ … } //构造方法 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(InterruptedException e){ } this.repaint(

文档评论(0)

1亿VIP精品文档

相关文档