Animation and Double-Buffering. The animation methods described here are based on standard techniques of double-buffering applicable to most high-level.

Slides:



Advertisements
Similar presentations
Layered Panes Manages the painting of components to simulate different depths. The highest depth level number is on top. Components at the same depth level.
Advertisements

The Point Class public class Point { public double x; public double y; public Point(double x0, double y0) { x = x0; y = y0; } public double distance(Point.
Chapter 9 Color, Sound and Graphics
COMPUTER PROGRAMMING I Objective 8.03 Apply Animation and Graphic Methods in a Windows Form (4%)
More Java Drawing in 2D Animations with Timer. Drawing Review A simple two-dimensional coordinate system exists for each graphics context or drawing surface.
1 Chapter 8 Objects and Classes Lecture 2 Prepared by Muhanad Alkhalisy.
 2007 Dr. Natheer Khasawneh. Chapter 13. Graphical User Interface Concepts: Part 1.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 21 - “Cat and Mouse” Painter Application.
GUI and Swing, part 2 The illustrated edition. Scroll bars As we have previously seen, a JTextArea has a fixed size, but the amount of text that can be.
IEG3080 Tutorial 3 Prepared by Ryan. Outline Object Oriented Programming Concepts Encapsulation Inheritance Polymorphism Delegation Course Project.
Introduction to the C# Programming Language for the VB Programmer.
Graphics and Multimedia. Outline Introduction Graphics Contexts and Graphics Objects Color Control.
Vertical Retrace Interval An introduction to VGA techniques for smooth graphics animation.
Copyright © 2012 Pearson Education, Inc. Chapter 6 Modularizing Your Code with Methods.
Copyright © 2012 Pearson Education, Inc. Chapter 2 Introduction to Visual C#
Based on slides created by Edward Angel
Computer-Based Animation. ● To animate something – to bring it to life ● Animation covers all changes that have visual effects – Positon (motion dynamic)
User Interface Programming in C#: Direct Manipulation Chris North CS 3724: HCI.
Graphics and Multimedia. Introduction The language contains many sophisticated drawing capabilities as part of namespace System.Drawing and the other.
1 Chapter 26 D&D – Graphics Outline 26.1 Introduction 26.3 Graphics Contexts and Graphics Objects 26.4 Color Control 26.5 Font Control 26.6 Drawing Lines,
Lecture 2 Managing Windows OS Introduction to.NET Framework C# & Microsoft Visual Studio.NET 2008.
Computer Programming and Basic Software Engineering 9 Building Graphical User Interface A Brief Introduction to GDI+ S.R.G. Fraser, Pro Visual C++/CLI.
Developing Applications for Mobile Devices Dr. Frank McCown GUI Programming Fall 2008.
CST238 Week 6 Questions / Concerns? Announcements – HW#2 due next Monday (project concept/preliminary design) – Check-off Take Home lab#5 Comment about.
Object Oriented Programming Graphical User Interfaces Dr. Mike Spann
COMPUTER PROGRAMMING I Objective 8.03 Apply Animation and Graphic Methods in a Windows Form (4%)
Lecture 8 Building an MDI Application. Introduction The MDI (Multiple Document Interface) provides a way to display multiple (child) windows forms inside.
Object Oriented Programming Graphics and Multimedia Dr. Mike Spann
CSC 298 Windows Forms.
Graphics and Multimedia Part #2
Copyright © 2006 Thomas P. Skinner1 Chapter 5 Indexers, Interfaces, and Enumerators.
BIM211 – Visual Programming Objects, Collections, and Events 1.
1Computer Graphics Input and Interaction Lecture 8 John Shearer Culture Lab – space 2
Computer Graphics I, Fall 2010 Input and Interaction.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 7 The Game Loop and Animation Starting Out with Games & Graphics.
Graphics Concepts CS 2302, Fall /3/20142 Drawing Paths.
Chapter 11 Bitmaps, Icons and Cursors. Copyright 2006 © Thomas P. Skinner2 Background Bitmaps are easy in.NET. Two kinds of graphics: 1.Vector 2.Bitmaps.
Visual C# 2012 How to Program 1. 2  A graphical user interface (GUI) allows a user to interact visually with a program.  Figure 14.1 shows a Visual.
Lecture 16: Multithreaded Programming. public partial class Form1 : Form { Thread ct; Thread rt; public static int circle_sleep = 0; public static int.
Programming for Artists ART 315 Dr. J. R. Parker Art/Digital Media Lab Lec 10 Fall 2010.
Copyright © 2012 Pearson Education, Inc. Chapter 9 Classes and Multiform Projects.
Chapter 5: Ball Worlds Features 2 classes, constant data fields, constructors, extension through inheritance, graphics.
Programming Video Games
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Introduction to Android (Part.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 21 - “Cat and Mouse” Painter Application.
CIS 3.5 Lecture 2.2 More programming with "Processing"
Computer Programming and Basic Software Engineering 9 Building Graphical User Interface Creating a Multiple-Form Interface.
1 Input and Interaction. 2 Objectives Introduce the basic input devices ­Physical Devices ­Logical Devices ­Input Modes Event-driven input Introduce double.
University of New Mexico
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Objects and Classes.
CS COMPUTER GRAPHICS LABORATORY. LIST OF EXPERIMENTS 1.Implementation of Bresenhams Algorithm – Line, Circle, Ellipse. 2.Implementation of Line,
Slide 1 VB Graphics Controls & Timer Control. Slide 2 Default Controls.
Game Project 1 Homage to Pong. Project Rules: The primary project is Pong, the design and development of which will be discussed in detail here. If you.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
Playing with Sprites. XNA Game Lifecycle In the faceBall demo program we bounced a smiley face around the graphical display against a background image.
GAM666 – Introduction To Game Programming ● DirectDraw, the 2D component of DirectX, uses the term “surface” to mean an area of memory in which pixel data.
 2002 Prentice Hall. All rights reserved. 1 Outline Mouse Event Handling Keyboard Event Handling Graphical User Interface Concepts:
Computer Science I Animations. Bouncing ball. The if statement. Classwork/homework: bouncing something. Compress and upload work to Moodle.
Review Objects – data fields – constructors – Methods Classes.
Functions. 2 Modularity What is a function? A named block of code Sometimes called a ‘module’, ‘method’ or a ‘procedure’ Some examples that you know are:
CPT 450 Computer Graphics 12th Lecture – Animation.
Understand the Fundamentals of Classes
Computer Programming I
Graphics and Multimedia
Introduction to Object-oriented Program Design
MUTENESS ASSİSTMENT 1)WHY CHOICE ? 2)ABOUT DESİGN 3)WHICH CODES USING
MDI Picture Viewer Application
Copyright © 2006 Thomas P. Skinner
Game Loop Update & Draw.
Copyright © 2006 Thomas P. Skinner
Presentation transcript:

Animation and Double-Buffering

The animation methods described here are based on standard techniques of double-buffering applicable to most high-level programming languages. It DOES NOT make use of.NET or C# api double-buffering controls. The trick to animation is to generate and display successive images without creating flicker or ghost images caused by an interference between the monitor/display refresh rate and/or the drawing of the animated objects in a way that is visible to the viewer. Essential elements of a smooth animation: (1) Frame Rate – When successive images of an animation are displayed at a rate faster than about 24 frames per second the human eye-brain perceives a continuous flow rather than a sequence of static images. (2) Continuity – The amount of motion of “moving” objects should be relatively small between successive frames. When we need to animate a very fast moving object we have to simulate limitations in the human eye-brain by artificially blurring the image of the moving object in the direction of motion. (Not an issue in most animation applications.) (3) Double-Buffering – The viewer SHOULD NOT see the image while it is being generated. Rather the viewer is shown the previous image in the sequence until the next image is ready. Then the new image is transferred to the display in a single display interval. Double-buffering requires a hidden location in which to generate new image (called a frame buffer) and a means of transferring an image to the display in a single time unit (called the bitmap block transfer or BitBlt). Introduction

FormMain – this is the main form of the application it is comprised of a picBox and a timer to control the animation. picBox – this pictureBox is docked to fill the available space in FormMain. It's background image is “starfield.jpg” with properties set to tile the picBox if it is larger than the image. timerAnim – the interval for this timer is set to 20 milliseconds it calls a method to move the balls and to bounce any that have reached the border of the pictureBox aBitmap – in order to enable double-buffering we create a device context (DC) in which to draw the objects being animated. Then the image aBitmap is ready it is passed to the pictureBox by picBox.Image = aBitmap ball class – this class defines the ball object, whose properties include position, x and y, velocity vx and vy, diameter called size and a color. This class also defines methods move( ) and bounce( ) for moving the ball and bouncing it off the containing boundaries List - a generic list to hold the collection of balls being animated Balls in SPACE !!! Overview

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Drawing.Drawing2D; using System.Linq; using System.Text; using System.Windows.Forms; namespace BallsInSpace { public partial class FormMain : Form { List balls = new List (); private Bitmap aBitmap; Random rnd = new Random(); public FormMain() { InitializeComponent(); makeBalls(); timerAnim.Interval = 30; timerAnim.Start(); } Source Code aBitmap will be our frame buffer generic List to hold members of the ball class 30 millisecond frame time 1/0.03 = frames/sec

private void resetFrameBuffer() { Graphics g = CreateGraphics(); if (aBitmap != null) aBitmap.Dispose(); aBitmap = new Bitmap(picBox.Width, picBox.Height, g); } We will be using a bitmap image called aBitmap in which to draw the new frames of our animation. The size of the picBox (since it is docked to FormMain) can be changed by the user. When this occurs we need to create a new frame buffer (aBitmap). resetFrameBuffer( )

private void drawBalls() { resetFrameBuffer(); Graphics g = Graphics.FromImage(aBitmap); System.Drawing.SolidBrush aBrush = new System.Drawing.SolidBrush(Color.Black); foreach (ball b in balls) { aBrush.Color = b.Color; g.FillEllipse(aBrush, b.X -b.Size/2.0F, b.Y, b.Size/2.0F, b.Size, b.Size); } picBox.Image = aBitmap; } drawBalls( ) Graphics region g not visible in picBox center of Ellipse (ball) color of ball width, height of ball transfer finished frame to picBox

private void updateBalls() { foreach (ball b in balls) { b.move(); b.bounce(picBox.Width, picBox.Height); } private void timerAnim_Tick(object sender, EventArgs e) { drawBalls(); updateBalls(); } updateBalls( ) & timerAnim_Tick(...) public void move() { x += vx; y += vy; } public void bounce(float w, float h) { if (x < size/2.0F) { vx *= -1.0F; x = size/2.0F; } if (x > w - size/2.0F) { vx *= -1.0F; x = w - size/2.0F; } if (y < size/2.0F) { vy *= -1.0F; y = size/2.0F; } if (y > h - size/2.0F) { vy *= -1.0F; y = h - size/2.0F; } methods from the ball Class

private void makeBalls() { int w = picBox.Width; int h = picBox.Height; float vmax = 10.0F; float s; for (int i = 0; i < 200; i++) { s = (float)(rnd.Next(40) + 10); balls.Add(new ball( (float)rnd.Next()/(float)int.MaxValue*(w - s) + s / 2.0F, (float)rnd.Next() / (float)int.MaxValue * (h - s) + s / 2.0F, (float)rnd.Next() / (float)int.MaxValue * vmax - vmax / 2.0F, s, Color.FromArgb(rnd.Next(128)+127, rnd.Next(128)+127, rnd.Next(128)+127))); } makeBalls( ) calls the constructor for the ball Class

Original Size of FormMain

Effect of resizing FormMain

private void picBox_MouseDown(object sender, MouseEventArgs e) { float xp; float yp; xp = (float)(MousePosition.X - FormMain.ActiveForm.Location.X); yp = (float)(MousePosition.Y - FormMain.ActiveForm.Location.Y); foreach (ball b in balls) { b.X = xp; b.Y = yp; } picBox_MouseDown(...) This method resets the x,y positions of all the balls to the mouse click location, without changes the ball velocities vx, vy.

Effect of Clicking in FormMain

using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Collections.Generic; using System.Linq; using System.Text; namespace BallsInSpace { class ball { protected float x; protected float y; protected float vx; protected float vy; protected float size; protected Color color; public float Y { get { return y; } set { y = value; } public float X { get { return x; } set { x = value; } ball Class ball Class defines the properties and provides methods for an instance of a ball. Properties: position, velocity, size and color Methods: move( ), and bounce( )

public float Vx { get { return vx; } set { vx = value; } public float Vy { get { return vx; } set { vx = value; } public float Size { get { return size; } set { size = value; } public Color Color { get { return color; } set { color = value; } More Accessors for ball Class Properties

public ball(float newX, float newY, float newVx, float newVy, float newSize, Color newColor) { x = newX; y = newY; vx = newVx; vy = newVy; size = newSize; color = newColor; } public void bounce(float w, float h) { if (x < size/2.0F) { vx *= -1.0F; x = size/2.0F; } if (x > w - size/2.0F) { vx *= -1.0F; x = w - size/2.0F; } if (y < size/2.0F) { vy *= -1.0F; y = size/2.0F; } if (y > h - size/2.0F) { vy *= -1.0F; y = h - size/2.0F; } public void move() { x += vx; y += vy; } ball Constructor and move( ) & bounce( ) methods Expressing the position and velocity of the balls as floats rather than integers significanly improves the flow of the animation. This is true even though all objects are drawn using integer data types. The bounce(...) method checks to see if the ball position is outside the drawing boundary, moves the ball back inside the boundary and reverses the sign of the velocity.