High Level Languages - PowerPoint PPT Presentation

1 / 26
About This Presentation
Title:

High Level Languages

Description:

Graphics programming can seem intimidating but it need not be. ... To avoid flicker. Good for complex drawing programs. Animation ... – PowerPoint PPT presentation

Number of Views:27
Avg rating:3.0/5.0
Slides: 27
Provided by: scie205
Category:

less

Transcript and Presenter's Notes

Title: High Level Languages


1
High Level Languages
  • Delphi
  • Lecture 6

2
Last Lecture
  • Last Lecture we covered the following
  • Menus in Delphi
  • The menu designer
  • Creating menus by hand
  • Example menu item code
  • Databases
  • Borland Database
  • SQL in Delphi

3
This Lecture
  • In Todays Lecture we will cover
  • Basic Graphics Components
  • Device Contexts
  • Graphics the Windows API way
  • Graphics the Delphi way
    (Examples of Graphical Objects in Action)
  • Bitmaps
  • Multimedia Programming

4
1. Delphi Graphics
  • Graphics programming can seem intimidating but it
    need not be. In fact Delphi provides VCL
    components that can make it positively easy.
  • Three simple examples are
  • Shape Component
  • Image Component
  • PaintBox Component

5
TShape Component
Simply Drag and Drop a Shape component onto your
Form.
  • In Additional Tab of the Component Palette.
  • Just drag, drop and alter the Brush, Pen and
    Shape properties as your heart desires.
  • Produces circles, ellipses, squares, rectangles.
  • Brush changes the Background Colour of the shape,
    Pen the shapes border colour and thickness.

6
TImage Component
  • Used to display a Bitmap (as a TPicture) on a
    form.
  • Useful for all sorts of graphical operations,
    including a bitmap background for a form.
  • You can select the bmp at Design time or Run
    Time
  • Image1.Picture.Bitmap.LoadFromFile(BritneySpears.
    bmp)
  • Useful properties are Stretch, Center and
    Autosize.

7
PaintBox Component
  • If you want drawing confined to a certain part of
    a form, the PaintBox component provides a canvas
    upon which you can draw.
  • Anything that spills outside the PaintBox area is
    clipped.
  • The only significant property of PaintBox is
    Canvas. This property is an instance of the
    TCanvas Class.

8
2. Device Contexts
  • Device Context is a Windows term, describing any
    digital canvas upon which you can draw.
  • These surfaces can take many forms such as
  • - To a client area in a form
  • - To the whole window
  • - To the whole desktop
  • - To memory
  • - To a printer

9
3. Graphics the Windows way
  • Procedure TForm1.RedCircleBlueOutline(Sender
    TObject)
  • var DC HDC
  • Brush, OldBrush HBrush
  • Pen, OldPen HPen
  • begin
  • DC GetDC(Handle)
  • Brush CreateSolidBrush(RGB(255, 0, 0))
  • Pen CreatePen(PS_SOLID, 1,
    RGB(0,0,255))
  • OldBrush SelectObject(DC, Brush)
  • OldPen SelectObject(DC, Pen)
  • Ellipse(DC, 20, 20, 120, 120)
  • SelectObject(DC, OldBrush)
  • SelectObject(DC, OldPen)
  • ReleaseDC(Handle, DC)
  • end

10
4. Graphics the Delphi Way
  • Canvas.Brush.Color ClRed
  • Canvas.Pen.Color ClBlue
  • Canvas.Ellipse(20, 20, 120, 120)

11
Object Orientation Again!
  • The above code used a TCanvas component from the
    VCL to simplify the graphical process.
  • Not only is the code shorter and more readable it
    is more robust - The TCanvas class takes care of
    freeing resources as needed for you.
  • Hence TCanvas is a simpler and more effective way
    of approaching graphics than using the API.

12
TCanvas Properties
  • Font
  • Pen
  • Brush
  • Handle
  • PenPos
  • Pixels
  • ClipRect

13
Some TCanvas Methods
  • Arc - Draws an arc on the canvas.
  • Ellipse - Draws an ellipse on the canvas
  • Polygon - Draws polygon from an array of
    points
  • Pie - Places a pie shape on the canvas
  • Rectangle - Draws a rectangle on the canvas
  • RoundRect - Draws a filled rectangle with
    rounded corners
  • Draw - Copies an image from memory to the canvas
  • Floodfill - Fills an area of the canvas with the
    brush colour
  • LineTo - Draws a line to the specified (x,y)
    position
  • MoveTo - Sets the current drawing start position
  • TextExtent, TextHeight, Textout, TextRect.

14
5. The TBitmap Object
  • It is easy to put a Bitmap onto a canvas without
    using the TImage component.
  • The TBitmap class encapsulates a bitmap object in
    Delphi - simply load a bitmap from file into a
    TBitmap instance, and use the draw method to
    place it on the canvas
  • var Bitmap TBitmap
  • begin
  • Bitmap TBitmap.create
  • Bitmap.LoadFromFile(HearSay.bmp)
  • Canvas.Draw(0, 0, Bitmap)
  • Bitmap.free
  • end

15
Memory
  • Memory, its properties and organization is a very
    simple idea to comprehend.
  • One should keep in mind the simple idea that a
    computer is little more than a over-grown
    calculator with better I/O
  • Memory is nothing more than a linear set of
    bytes.  The computer needs a method to access all
    the memory on the system and it does this through
    addressing giving each byte in memory an
    identity. 

16
Segments
  • Memory within a computer typically is made of
    segments - These segments are simply regions of
    memory with a starting and ending address,
    assigned attributes. 
  • A program cannot write to a segment that does
    not have the write permission attribute assigned
    to it. 
  • To do with common problems like General
    Protection Faults, Unrecoverable Application
    Errors, and such like

17
Segments
  • When a program runs in memory, it has the
    following memory organization
  •      

EVERYTHING ELSE!
Where the statements themselves are stored
This is where the programs global variables are.
The home of all the local variables
18
Bits you need to know about
  • Stack
  • Heap
  • Create()
  • Free()
  • We can create any type of object in runtime.
  • Memory Leaks

19
6. Memory Bitmaps
  • Offscreen Bitmaps are commonly used in windows
    programming.
  • They enable you to build up an image in memory
    before you draw it onscreen.
  • Why?
  • To avoid flicker
  • Good for complex drawing programs
  • Animation
  • Most Popular use is in the Microsoft DirectX SDK

20
Creating a memory Bitmap
  • This is a 3 step process
  • - Create an Offscreen bitmap in memory
  • - Draw upon this memory bitmap
  • - Copy the memory bitmap to screen
  • This is exactly what we did in the last example,
    except we didnt draw on the memory bitmap but
    rather loaded a picture onto it from file.
  • However we could just as easily drawn on it
    before we put it on screen

21
Example
  • Bitmap TBitmap.create
  • Bitmap.LoadFromFile(HearSay.bmp)
  • Bitmap.Canvas.Brush.Color ClRed
  • Bitmap.Canvas.LineTo(100,100)
  • Bitmap.Canvas.MoveTo(0,100)
  • Bitmap.Canvas.LineTo(100,0)
  • Bitmap.Canvas.MoveTo(50,50)
  • Bitmap.Canvas.TextOut(STOP! Please)
  • Canvas.Draw(0, 0, Bitmap)
  • Bitmap.free

22
7. Multimedia Programming
  • Wave Playback is one of the simplest functions
    that the Windows API has. As such a Delphi VCL
    component isnt necessary.
  • Just add uses MMSystem to the start of your
    program and use the PlaySound Function
  • Playsound(test.wav, 0, SND_FILENAME)

SND Flag
API Command
File Path
23
Wave Playback
  • The SND Flag controls how PlaySound acts. Other
    options are
  • SND_ALIAS
  • tells the program to play a system sound. EG
  • SND_SYNC
  • control is only returned to the program after the
    wave has finished playing
  • SND_ASYNC
  • The wave is played while your program goes about
    the rest of its business

24
TMediaPlayer Component
  • This is the component that the Delphi VCL
    provides to perform multimedia operations.
  • It can play wave sounds, avi movies and even
    control the cd player.
  • Like all other components from the VCL it can be
    dragged and dropped onto the form, and then
    referred to in your code by its name.
  • Has too many features to go through now - refer
    to the online tutorials or Delphi help to get the
    most out of it.

25
Avi Example
  • Simply create a TMediaPlayer component on your
    form an use code such as
  • Player.wait True
  • Player.FileName Film.avi
  • Player.Open
  • Player.Play
  • If you use the default settings this will play
    the video in a seprate pop up window. However if
    you set up a panel on your form called AVIPanel
    the following code would make the video appear in
    that panel.

MediaPlayer.Display AVIPanel
26
Final Words
  • Multimedia can make your programs entertaining
    but do not go overboard!
  • The coursework Deadline is March 21st 5pm. Make
    sure that you submit the coursework in the
    required way.
  • Do not leave the coursework to the last minute.
    Start now. The coursework is not easy, but should
    be rewarding.
  • Thats it! Its all over! Good luck with perl,
    Thank You and Goodnight.
Write a Comment
User Comments (0)
About PowerShow.com