Presentation is loading. Please wait.

Presentation is loading. Please wait.

COM148X1 Interactive Programming Lecture 6. Topics Today Generate Random Numbers Graphics Animation.

Similar presentations


Presentation on theme: "COM148X1 Interactive Programming Lecture 6. Topics Today Generate Random Numbers Graphics Animation."— Presentation transcript:

1 COM148X1 Interactive Programming Lecture 6

2 Topics Today Generate Random Numbers Graphics Animation

3 Generate Random Number In VB, random numbers can be generated using Rnd() function Rnd() function returns random number in the range [0 … 1) (include 0 but exclude 1) Example x = Rnd() ‘0 ≤ x < 1 x = Rnd() * 5 ‘0 ≤ x < 5 x = Int(Rnd() * 5) + 1 ‘1 ≤ x ≤ 5, integer only

4 Randomize() Function The number sequence generated by Rnd() will be the same when the application start Randomize() function is used to shuffle the sequence again Call Randomize() once when the application start

5 Pseudo Random Number Sometimes, non-repeatable random number is required, for example, shuffle a deck of card (A, 2, 3, …, J, Q, K) Use the technique called pseudo random number to generate non-repeatable random number

6 Pseudo Random Number Algorithm Use array to store all number Use Rnd() function to pick two array position randomly and then SWAP the content Repeat the step above several times and use the array as random result 23456789101112131 21045678931112131 471 681225391011

7 Pseudo Random Number Example Dim cards(13) As Integer Dim j, k, card1, card2 As Integer For j = 1 To 13 cards(j) = j Next j PseudoRandom(cards)

8 Pseudo Random Number Example (cont’) ‘Perform pseudo random from 1 … UBound(a) Sub PseudoRandom(ByRef a() As Integer) For k = 1 to 10000 card1 = Int(Rnd() * UBound(a)) + 1 card2 = Int(Rnd() * UBound(a)) + 1 Swap(a(card1), a(card2)) Next k End Sub

9 Pseudo Random Number Example (cont’) Sub Swap(ByRef x As Integer, ByRef y As Integer) Dim temp As Integer = x x = y y = temp End Sub

10 Graphics

11 Coordinate System Coordinate of form use (x, y) coordinate system Upper left corner is the origin x value increase from left to right y value increase from top to bottom All UI controls use their upper left corner as their reference position

12 Coordinate System x y (0, 0) (300,300) (120, 50)

13 Point Class Point object represents a coordinate Example Dim p As new Point(10, 20) Dim x, y As Integer x = p.X() ‘x = 10 y = p.Y() ‘y = 20 ‘change coordinate p = new Point(100, 150)

14 Color Class Color object represents a color Default color available for use Black, Silver, Gray, White, Maroon, Red, Purple, Fuchsia, Green, Lime, Olive, Yellow, Navy, Blue, Teal, Aqua … Example Dim c1 As Color = Color.Green ‘use default color Dim c2 As Color c2.FromArgb(128, 128, 128) ‘RGB value

15 SolidBrush Class Brushes specified how to fill an object SolidBrush specified which color used to fill an object Dim brush_name As New SolidBrush(color) Example Dim b As SolidBrush(Color.Red)

16 Pen Class Pen class is used to draw difference kinds of lines and curves Dim pen_name As New Pen(brush,width) Example Dim b As New SolidBrush(Color.Red) Dim p As New Pen(b, 1.5)

17 Graphics Class Before start drawing on UI controls, Graphics object of the UI control is required Use CreateGraphics() method of the UI control to obtain Graphics object Example Dim g As Graphics g = Panel1.CreateGraphics()

18 Commonly Use Graphics Functions FunctionDescription DrawLine(pen, p1, p2) Draw line from p1 to p2 DrawRectangle(pen, x, y, w, h) Draw w x h rectangle with top left corner at (x, y) DrawEllipse(pen, x, y, w, h) Draw w x h ellipse with top left corner at (x, y) DrawPolygon(pen, points()) Draw polygon by given array of points DrawCurve(pen, points()) Draw curve by given array of points

19 Commonly Use Graphics Functions FunctionDescription FillRectangle(brush, x, y, w, h) Fill w x h rectangle with top left corner at (x, y) FillEllipse(brush, x, y, w, h) Fill w x h ellipse with top left corner at (x, y) FillPolygon(brush, points()) Fill polygon by given array of points Fill ClosedCurve(brush, points()) Fill closed curve by given array of points

20 Graphics Functions Example Dim g As Graphics g = Panel1.CreateGraphics() Dim brush1 As New SolidBrush(Color.Red) Dim pen as New Pen(brush1, 10) g.DrawLine(pen, p1, p2) Dim brush2 As New SolidBrush(Color.Green) g.FillRectangle(brush, p1.X(), p1.Y(), 100, 150)

21 Animation

22 Image Preparation Use ImageList control to prepare a set of images for application

23 Properties of ImageList Images is set of Images, index start from 0 ImageSize is the size of image, maximum size is 256x256 Number of images in the ImageList can be found in the Count properties of Images Example x = ImageList1.Images.Count

24 Show Picture (Image) Use picture control to make different animation effects

25 Properties of PictureBox Control Location represents the top-left corner of PictureBox Top represents the top most coordinate of PictureBox Left represents the left most coordinate of PictureBox Width represents the width of PictureBox Height represents the height of PictureBox Image represents the picture showing in the PictureBox, set Image to Nothing in order to make the PictureBox shows nothing Example PictureBox1.Image = Nothing

26 Playing Animation Use Timer control to control the frame rate of animation and make the application responsive to other event when the animation is playing

27 Animation Example ‘move image from left to right while playing ‘image sequence inside PictureBox Dim i As Integer = 0 Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick Dim x, y as Integer x = PictureBox1.Location.X + 1 y = PictureBox1.Location.Y PictureBox1.Location = new Point(x, y) PictureBox1.Image = ImageList1.Images(i) i = (i + 1) mod ImageList1.Images.Count End Sub


Download ppt "COM148X1 Interactive Programming Lecture 6. Topics Today Generate Random Numbers Graphics Animation."

Similar presentations


Ads by Google