Home
The Hugs Graphics Library (Version 2.0)
Contents
1. Pen gt Graphic gt Graphic For example the following program uses a 50 x 50 pixel non bold italic courier font to draw red text on a green background at an angle of 45 degrees across the screen gt fontDemo runGraphics do gt w lt openWindow Font Demo Window 100 100 gt drawInWindow w gt withTextColor RGB 255 0 0 gt mkFont 50 100 pi 4 False True courier font gt gt withFont font gt withBkColor RGB 0 255 0 gt withBkMode Opaque gt text 50 50 Font Demo gt getKey w gt closeWindow w A default font is substituted if the requested font does not exist The rotation angle is ignored if the font is not a TrueType font e g for System font on Win32 Portability Note e X11 does not directly support font rotation so mkFont always ignores the rotation angle argument in the X11 implementation of this library e Many of the font families typically available on Win32 are not available on X11 and vice versa In our experience the font families courier helvetica and times are somewhat portable End Portability Note 2 5 Brushes Pens and Text Colors If you were counting you ll have noticed that there are five separate ways of specifying colors gt mkBrush 33 RGB gt Brush gt Graphic gt Graphic gt mkPen Style gt Int gt RGB gt Pen gt Graphic gt Graphic gt withTextColor RGB gt Graphic gt Graphic g
2. data RedrawMode gt Unbuffered gt DoubleBuffered This extended version of openWindow takes extra parameters which specify e the initial position of a window e how to display a graphic on a window and e the time between ticks in milliseconds The function openWindow is defined using openWindowEx gt openWindow name size openWindowEx name Nothing size Unbuffered Nothing The drawing mode can be either DoubleBuffered which uses a double buffer to reduce flicker or Unbuffered which draws directly to the window and runs slightly faster but is more prone to flicker You should probably use DoubleBuffered for animations The timer generates tick events at regular intervals The function getWindowTick waits for the next tick event to occur gt getWindowTick Window gt IO Aside With normal events like button presses we store every event that happens until you remove that event from the queue If we did this with tick events and your program takes a little too long to draw each frame of an animation the event queue could become so swamped with ticks that you d never respond to user input To avoid this problem we only insert a tick into the queue if there s no tick there already End aside Here s a simple example of how to use timers Note the use of setGraphic instead of drawInWindow gt timerDemo do gt w lt openWindowEx gt Timer demo title gt Just
3. 0 0 gt Magenta RGB 255 0 255 gt Yellow RGB 255 255 0 gt White RGB 255 255 255 gt 2 7 Bitmaps Bitmaps can be displayed in three ways 1 with no transformation at a point 2 stretched to fit a rectangle 3 rotated and sheared to fit a parallelogram Rectangles are specified by a pair of points the top left and bottom right corners of the rectangle gt bitmap Point gt Bitmap gt Graphic gt stretchBitmap Point gt Point gt Bitmap gt Graphic gt shearBitmap Point gt Point gt Point gt Bitmap gt Graphic Bitmaps are read in from files and disposed of using gt readBitmap String gt IO Bitmap gt deleteBitmap Bitmap gt I0 but be sure that the current Graphic on a Window doesn t contain a reference to a Bitmap before you delete the Bitmap This operation gets the size of a bitmap gt getBitmapSize Bitmap gt IO Int Int Portability Note e The Bitmap functions are not currently provided in the X11 implementation of this library e shearBitmap is supported on Win NT but not Win 95 End Portability Note 2 8 Regions Regions can be viewed as an efficient representation of sets of pixels They are created from rectangles ellipses polygons and combined using set operations intersection union difference and xor symmetric difference These are the operations available gt emptyRegion a Region gt rectangle
4. 500 500 initial position of window gt 100 100 initial size of window gt DoubleBuffered drawing mode see above gt Just 50 tick rate gt let gt loop x do gt setGraphic w text 0 50 show x gt getWindowTick w wait for next tick on window gt loop x 1 gt loop 0 14 5 Concurrent Haskell If you want to use multiple windows or each window contains a number of essentially independent compo nents it is convenient to use separate threads for handling each window Hugs provides a simple mechanism for doing that The simplest concurrency primitives are par and par gt par IO a gt IO b gt IO a b gt par_ IO a gt IO b gt I0 These are both exported from the GraphicsUtils module These run two I0 actions in parallel and terminate when both actions terminate The function par discards the results of the actions Aside The underscore in the name par_ is derived from the use of the underscore in the definition of par gt par_ p q p par q gt gt _ gt return This naming convention is also used in the Haskell Prelude and standard libraries mapM_ zipWithM_ etc End aside The function parMany generalizes par_ to lists gt parMany I0 gt I0 gt parMany foldr par_ return Of course you ll quickly realise that there s not much point in being able to create concurrent threads if threads can t communicate with
5. each other Hugs provides an implementation of the Concurrent Haskell primitives described in the Concurrent Haskell paper 3 to which we refer the enthusiastic reader 6 The Draw monad The Graphic type operations and combinators provide a flexible efficient and convenient way of drawing images on a window and encapsulate good programming practice by cleaning up any changes they must make to the state of the window In some applications though it is appropriate to use a lower level more error prone interface for drawing images For example when building a library on top of the Graphics library one might want to build on a slightly more efficient less secure interface Or when teaching courses on computer graphics it would not be possible to demonstrate low level aspects of graphics using an interface which hides those aspects This section describes the Draw monad an imperative graphics interface and describes how this is used to implement the Graphic type a declarative graphics interface This section can be ignored by most readers 6 1 The Draw monad and the Graphic type The Graphic type lets you describe what an image should look like the Draw monad lets you describe how to build an image These views intersect for atomic graphics For example the function to draw a line can serve both as a description and as the implementation This is exploited in the graphics library by defining Graphic as an instance of the Draw monad Th
6. gt withTextAlignment Alignment gt Graphic gt Graphic gt withBkColor RGB gt Graphic gt Graphic gt withBkMode BkMode gt Graphic gt Graphic gt withPen Pen gt Graphic gt Graphic gt withBrush Brush gt Graphic gt Graphic gt withRGB RGB gt Graphic gt Graphic The effect of these modifiers is to modify the way in which a graphic will be drawn For example if courier Font is a 10 point Courier font then drawing withFont courier text 100 100 Hello will draw the string Hello on the window using the 10 point Courier font Modifiers are cumulative a series of modifiers can be applied to a single graphic For example the graphic gt withFont courier gt withTextColor red gt withTextAlignment Center Top gt text 100 100 Hello World gt gt gt e horizontally aligned so that the centre of the text is at 100 100 e vertically aligned so that the top of the text is at 100 100 e colored red e displayed in 10 point Courier font Modifiers nest in the obvious way so gt withTextColor red gt withTextColor green gt text 100 100 What Color Am I gt gt will produce green text as expected Aside As you write more and more complex graphics you ll quickly realize that it s very tedious to insert all those parentheses and to keep everything indented in a way that reveals its structure Fortunately the Haskel
7. Font modifier could be implemented like this gt withFont new g do gt old lt selectFont new gt 8 gt selectFont old gt return Aside This pattern of use is very common in imperative programs so the Haskell 10 library provides two combinators which encapsulate this behavior The bracket function takes three operations as arguments a pre operation left a post operation right and an operation middle and performs them in the order left middle right The arguments are provided in the order left right middle because the left and right operations are often inverses of each other such as openFile and closeFile The bracket_ function is similar and is used when the middle operation does not require the result of the left operation gt bracket IO a gt a gt IO b gt a gt IO c gt IO c gt bracket_ IO a gt a gt IO b gt IO c gt IO c gt gt bracket left right middle do gt a lt left gt c lt middle a gt right a gt return c 16 gt gt bracket_ left right middle bracket left right const middle End aside The graphics library provides similar combinators for the Draw monad gt bracket Draw a gt a gt Draw b gt a gt Draw c gt Draw c gt bracket_ Draw a gt a gt Draw b gt Draw c gt Draw c Aside In fact the bracket and bracket_ functions do slightly more than the above description suggests Those provided in
8. Mode gt Graphic gt Graphic gt Graphic gt Graphic Brush gt Graphic gt Graphic gt Graphic gt Graphic deriving Eq Ord Bounded Enum Ix Show Read colorList colorTable withColor par par_ parMany Color RGB Array Color RGB Color gt Graphic gt Graphic IO a gt IO b gt IO a b IO a gt IO b gt IO gt I0 0 I0 0O A 3 Portability notes e polyBezier is not provided in the X11 implementation of this library e shearEllipse is implemented by polygons on both Win32 and X11 e X11 does not directly support font rotation so mkFont always ignores the rotation angle argument in the X11 implementation of this library e Many of the font families typically available on Win32 are not available on X11 and vice versa In our experience the font families courier helvetica and times are somewhat portable 20 On Win32 the pen is also used to draw a line round all the filled shapes so the pen color also affects how polygons ellipses and regions are drawn One of the Win32 gotchas is that the choice of Style only applies if the width is 1 or less With greater widths the pen style will always be Solid no matter what you try to select This problem does not apply to X11 The Bitmap functions are not currently provided in the X11 implementation of this library shearBitmap is supported on Win NT but not Win 95 emptyRegion is not provid
9. Region Point gt Point gt Region gt ellipseRegion Point gt Point gt Region gt polygonRegion Point gt Region gt gt intersectRegion Region gt Region gt Region gt unionRegion Region gt Region gt Region gt subtractRegion Region gt Region gt Region gt xorRegion Region gt Region gt Region gt gt regionToGraphic Region gt Graphic withBrush affects the color of regionToGraphic Portability Note e emptyRegion is not provided in the Win32 implementation of this library It is possible to use an empty rectangle region instead e ellipseRegion is implemented using polygons in the X11 implementation of the library End Portability Note 2 9 The Graphic Algebra The Graphic modifiers satisfy a large number of useful identities For example e The triple Graphic overGraphic emptyGraphic forms a monoid If this wasn t true we wouldn t find the overGraphics function very useful e Modifiers and generators all distribute over overGraphic That is gt mkFoo lt args gt p1 overGraphic p2 gt mkFoo lt args gt p1 overGraphic mkFoo lt args gt p2 gt withFoo foo pi overGraphic p2 gt withFoo foo p1 overGraphic withFoo foo p2 These laws are especially useful when trying to make programs more efficient see section 2 10 Independent modifiers commute with each other For example gt withT
10. The Hugs Graphics Library Version 2 0 Alastair Reid Department of Computer Science University of Utah reid cs utah edu April 9 2003 1 Introduction The Hugs Graphics Library is designed to give the programmer access to most interesting parts of the Win32 Graphics Device Interface and X11 library without exposing the programmer to the pain and anguish usually associated with using these interfaces To give you a taste of what the library looks like here is the obligatory Hello World program module Hello where import GraphicsUtils gt gt gt gt gt helloWorld I0 gt helloWorld runGraphics do gt w lt openWindow Hello World Window 300 300 gt drawInWindow w text 100 100 Hello gt drawInWindow w text 100 200 World gt getKey w gt closeWindow w gt Here s what each function does e runGraphics I0 gt IO get Hugs ready to do graphics runs an action here the action is a sequence of 5 subactions and cleans everything up at the end 1 e openWindow Title gt Point gt I0 Window opens a window specifying the window title Hello World Window and the size of the window 300 pixels x 300 pixels e drawInWindow Window gt Graphic gt IO draws a Graphic on a Window 1The description of runGraphics is rather vague because of our promise to protect you from the full horror of Win32 X11 programming If you really want to know we
11. ain this which should run about 3 times faster gt overGraphics gt mkBrush red redBrush gt withBrush redBrush ellipse 00 00 10 10 gt mkBrush red redBrush gt withBrush redBrush ellipse 10 10 20 20 gt mkBrush red redBrush gt withBrush redBrush ellipse 20 20 30 30 gt 2 10 2 Lifting generators to the top of Graphics Another important optimization is to avoid creating many identical brushes pens or fonts when one will do We do this by lifting brush creation out to the top of a graphic For example this graphic gt overGraphics gt mkBrush red redBrush gt withBrush redBrush ellipse 00 00 10 10 gt mkBrush red redBrush gt withBrush redBrush ellipse 10 10 20 20 gt mkBrush red redBrush gt withBrush redBrush ellipse 20 20 30 30 gt creates three red brushes It would be more efficient to rewrite it like this gt mkBrush red redBrush gt gt overGraphics gt withBrush redBrush ellipse 00 00 10 10 gt withBrush redBrush ellipse 10 10 20 20 gt withBrush redBrush ellipse 20 20 30 30 gt If your program uses a lot of brushes it may be more convenient to store the brushes in a palette i e an array of brushes gt mkBrush red redBrush gt gt mkBrush blue blueBrush gt gt let palette array minBound maxBound gt Red redBrush Blue blueBrush gt
12. aphicsCore and it is expected that most users will only import GraphicsUtils the GraphicsCore interface is aimed solely at those wishing to use the graphics library as a base on which to build their own library or who find the GraphicsUtils interface inappropriate for their needs 17 A 1 Module GraphicsCore VN NNN NNN NNN VV VV VV VV VV VV VV VV VV VV VV VV VV VV VV VV VV VV VOY type Title String type Point Int Int type Size Int Int type Angle Double type Time Word32 milliseconds data RGB RGB Words Word8 Word8 data BkMode Opaque Transparent type Alignment HAlign VAlign names have a tick to distinguish them from Prelude names blech data HAlign Left Center Right deriving Enum Eq Ord Ix Show data VAlign Top Baseline Bottom deriving Enum Eq Ord Ix Show data Style Solid Dash eg oe ee Dot ieee DashDot E DashDotDot _ _ _ Null InsideFrame runGraphics I0 OQ gt I0 getTime IO Time data Window openWindowEx Title gt Maybe Point gt Size gt RedrawMode gt Maybe T Time gt I0 Window closeWindow Window gt I0 getWindowRect Window gt IO Point Point getWindowEvent Window gt IO Event getWindowTick Window gt I0 maybeGetWindowEvent Window gt IO Maybe Event type Graphic Draw setGraphic Window gt Graphic gt I0 getGraphic Window gt IO Graphic
13. c w gt setGraphic w p over oldGraphic gt gt gt clearWindow Window gt I0 clearWindow w setGraphic w emptyGraphic 4 Events The graphics library supports several different input devices the mouse the keyboard etc each of which can generate several different kinds of event mouse movement mouse button clicks key presses key releases window resizing etc 4 1 Keyboard events In section 1 we saw the function getKey being used to wait until a key was pressed and released The function getKey is defined in terms of a more general function getKeyEx 11 gt getKeyEx Window gt Bool gt IO Char which can be used to wait until a key is pressed getKeyEx w True or until it is released getKeyEx w False The definition of getKey using this function is trivial gt getKey Window gt IO Char gt getKey w do getKeyEx w True getKeyEx w False 4 2 Mouse events As well as waiting for keyboard events we can wait for mouse button events We provide three functions for getting these events getLBP and getRBP are used to wait for left and right button presses Both functions are defined using getButton which can be used to wait for either the left button or the right button being either pressed or released gt getLBP Window gt IO Point gt getRBP Window gt IO Point gt getButton Window gt Bool gt Bool gt IO Point gt gt getLBP w getButton w True Tru
14. e gt getRBP w getButton w False True 4 3 General events The functions getKeyEx and getButton described in the previous sections are not primitive functions Rather they are defined using the primitive function getWindowEvent gt getWindowEvent Window gt IO Event which waits for the next event on a given Window Events are defined by the following data type gt data Event gt Key char Char isDown Bool gt Button pt Point isLeft isDown Bool gt MouseMove pt Point gt Resize gt Closed gt deriving Show These events are e Key char isDown occurs when a key is pressed isDown True or released isDown False char is the keycode for the corresponding key This keycode can be a letter a number or some other value corresponding to the shift key control key etc e Button pt isLeft isDown occurs when a mouse button is pressed isDown True or released isDown False pt is the mouse position when the button was pressed and isLeft indicates whether it was the left or the right button e MouseMove pt occurs when the mouse is moved inside the window pt is the position of the mouse after the movement 12 e Resize occurs when the window is resized The new window size can be discovered using these functions gt getWindowRect Window gt IO Point Size gt getWindowSize Window gt IO Size gt getWindowSize w do gt pt s
15. ed in the Win32 implementation of this library It is possible to use an empty rectangle region instead ellipseRegion is implemented using polygons in the X11 implementation of the library Programmers should assume that the Event datatype will be extended in the not too distant future and that individual events may change slightly As a minimum you should add a match anything alternative to any function which pattern matches against Events X11 systems typically have three button mice Button 1 is used as the left button button 3 as the right button and button 2 the middle button is ignored 21
16. equivalent graphic gt mkBrush colorTable Red redBrush gt gt overGraphics gt withBrush redBrush ellipse 000 000 100 100 gt withBrush redBrush ellipse 100 100 200 200 gt withBrush redBrush ellipse 200 200 300 300 gt Briefly the problems are that withColor sets the color of the brush the pen and the text but ellipses only use the brush color and we re calling withColor 3 times more than we have to This wouldn t matter if brush creation was cheap and easy However most typical workstations can only display at most 256 or 65536 different colors on the screen at once but allow you to specify any one of 16777216 different colors when selecting a drawing color finding a close match to the requested color can be as expensive as drawing the primitive object itself This doesn t matter much for a graphic of this size but if you re drawing several thousand graphic elements onto the screen as part of an animation it can make the difference between a quite respectable frame rate of 20 30 frames per second and an absolutely unusable frame rate of 2 3 frames per second 2 10 1 Eliminate calls to withRGB and withColor At the risk of pointing out the obvious the first step in optimizing a program in this way is to expand all uses of the withRGB and withColor functions and eliminating unnecessary calls to mkBrush mkPen and withTextColor Applying this optimization to the above Graphic we obt
17. extColor c withTextAlignment a p gt withTextAlignment a withTextColor c p Generators commute with modifiers For example gt mkBrush c b gt withBrush b p withBrush b mkBrush c b gt p if b and b are distinct Generators commute with other generators For example gt mkBrush c b gt mkBrush c b gt p gt mkBrush c b gt mkBrush c b gt p if b and b are distinct Irrelevant modifiers and generators can be added or removed at will For example the text color has no effect on line drawing gt withTextColor c line pO pi line pO pl and there s no need to create a brush if you don t use it gt mkBrush c b gt p p if b does not occur in p This last law can also be stated in the form gt mkBrush c b gt atomic atomic for any atomic operation The practical upshot of all this is that there are many ways to rearrange a graphic so that it will be drawn more or less efficiently We explore this topic in the next section 2 10 Efficiency Considerations The other sections provide a very simple set of functions for creating graphics but at the cost of ignoring efficiency For example this innocent looking graphic gt overGraphics gt withColor Red ellipse 000 000 100 100 gt withColor Red ellipse 100 100 200 200 gt withColor Red ellipse 200 200 300 300 gt will take longer to draw than this
18. highly recommend Charles Petzold s book Programming Windows 2 which does an excellent job with a difficult subject or Adrian Nye s Xlib Programming Manual 1 which is almost adequate e text Point gt String gt Graphic creates a Graphic consisting of a String at a given screen location e getKey Window gt IO Char waits for the user to press and release a key This is necessary to prevent the window from closing before you have a chance to read what s on the screen e closeWindow Window gt I0 closes the window The rest of this document is organized as follows Section 2 describes the Graphic type a declarative way of drawing pictures Section 3 describes Windows Section 4 describes Events Section 5 describes the Concurrent Haskell primitives which you need to create complex interfaces and Section 6 describes the Draw monad a more imperative side to the Graphic type 2 Graphics In section 1 we used these two functions to draw to a window gt drawInWindow Window gt Graphic gt I0 gt text Point gt String gt Graphic This section describes other ways of creating graphics that can be drawn to the screen 2 1 Atomic Graphics Here s a list of the atomic operations gt emptyGraphic Graphic gt ellipse Point gt Point gt Graphic gt shearEllipse Point gt Point gt Point gt Graphic gt arc Point gt Point gt Angle gt Angle gt G
19. in 10 gt overGraphics gt withBrush palette Red ellipse 00 00 10 10 gt withBrush palette Blue ellipse 10 10 20 20 gt withBrush palette Red ellipse 20 20 30 30 gt 2 10 3 Lifting generators out of graphics Even this program has room for improvement every time the graphic is redrawn e g whenever the window is resized it will create fresh brushes with which to draw the graphic The graphics library provides a way round this but it s more difficult and fraught with danger Outline This section will talk about using explicit creation and deletion functions to create brushes fonts etc The situation isn t very happy at the moment because it s easy to forget to deallocate brushes before you quit or to deallocate them before you change the graphic End outline 3 Windows In section 1 we saw the function drawInWindow for drawing a Graphic on a Window It turns out that drawInWindow is not a primitive function but rather it is defined using these two primitive functions which read the current Graphic and set a new Graphic gt getGraphic Window gt IO Graphic gt setGraphic Window gt Graphic gt I0 Here s how these functions are used to define the function drawInWindow which we used in section 1 and another useful function clearWindow gt drawInWindow Window gt Graphic gt I0 gt drawInWindow w p do gt oldGraphic lt getGraphi
20. l Prelude provides a right associative application operator gt a gt b gt a gt b which eliminates the need for almost all parentheses when defining Graphics Using the operator the above example can be rewritten like this gt withTextColor red gt withTextColor green gt text 100 100 What Color Am I End aside 2 3 Combining Graphics The other useful property of Graphics is that they can be combined using the overGraphic combinator gt overGraphic Graphic gt Graphic gt Graphic For example drawing this graphic produces a red triangle on top of or in front of a blue square gt overGraphic gt withBrush red polygon 200 200 400 200 300 400 gt withBrush blue polygon 100 100 500 100 500 500 100 500 Notice that modifiers respect the structure of a graphic modifiers applied to one part of a graphic have no effect on other parts of the graphic For example the above graphic could be rewritten like this gt withBrush blue gt overGraphic gt withBrush red polygon 200 200 400 200 300 400 gt polygon 100 100 500 100 500 500 100 500 The overGraphics function is useful if you want to draw a list of graphics It s type and definition are gt overGraphics Graphic gt Graphic gt overGraphics foldr overGraphic emptyGraphic Notice that graphics at the head of the list are drawn in front of g
21. modGraphic Window gt Graphic gt Graphic gt I0 directDraw Window gt Graphic gt I0 selectFont Font gt Draw Font setTextColor RGB gt Draw RGB setTextAlignment Alignment gt Draw Alignment setBkColor RGB gt Draw RGB setBkMode BkMode gt Draw BkMode selectPen Pen gt Draw Pen 18 selectBrush bracket bracket_ data Font createFont deleteFont data Brush mkBrush data Pen mkPen createPen arc line polyline ellipse shearEllipse polygon text data Region emptyRegion rectangleRegion ellipseRegion polygonRegion intersectRegion unionRegion subtractRegion xorRegion regionToGraphic data Event Key Button pt MouseMove pt Resize Closed deriving Show Brush RGB char gt Draw Brush Draw a gt a gt Draw b gt a gt Draw c gt Draw a gt a gt Draw b gt Draw c gt Draw c Point gt Angle gt Bool gt Bool gt String gt Font gt I0 gt Brush gt Draw a gt Style gt Int gt RGB gt Pen gt Draw a gt Style gt Int gt RGB gt IO Pen Point gt Point gt Angle gt Angle gt Graphic Point gt Point gt Graphic Point gt Graphic Point gt Point gt Graphic Point gt Point gt Point gt Graphic Point gt Graphic Point gt String gt Graphic Region Point gt Point gt Region Point gt Poi
22. nt gt Region Point gt Region Region gt Region gt Region Region gt Region gt Region Region gt Region gt Region Region gt Region gt Region Region gt Graphic Char isDown Bool Point isLeft isDown Bool Point A 2 Module GraphicsUtils Draw c IO Font Draw a Draw a unfilled unfilled unfilled filled filled filled filled Note that this document repeats the definitions of all the functions defined in GraphicsUtils openWindow gt gt gt gt clearWindow Reexports GraphicsCore Title gt Size gt IO Window Window gt I0 19 VN NNN NNN VV VV VV VV VV VV VV VV VV VV VV VV VV VV VV VV drawInWindow getWindowSize getLBP getRBP getButton getKey getKeyEx emptyGraphic overGraphic overGraphics withFont withTextColor withTextAlignment withBkColor withBkMode withPen withBrush withRGB data Color Black Blue Green Cyan Red Magenta Yellow White Window Window Window Window Window Window Window Graphic Graphic gt Graphic gt Graphic Graphic gt I0 IO Size IO Point IO Point Bool gt Bool gt IO Point IO Char Bool gt IO Char Graphic gt Graphic Font RGB Pen RGB gt Graphic gt Graphic gt Graphic gt Graphic Alignment gt Graphic gt Graphic RGB gt Graphic gt Graphic Bk
23. raphic gt line Point gt Point gt Graphic gt polyline Point gt Graphic gt polygon Point gt Graphic gt polyBezier Point gt Graphic gt text Point gt String gt Graphic emptyGraphic is a blank Graphic ellipse is a filled ellipse which fits inside a rectangle defined by two Points on the window shearEllipse is a filled ellipse inside a parallelogram defined by three Points on the window arc is an unfilled elliptical arc which fits inside a rectangle defined by two Points on the window The angles specify the start and end points of the arc the arc consists of all points from the start angle counter clockwise to the end angle Angles are in degrees 0 360 rather than radians 0 27 line is a line between two Points polyline is a series of lines through a list of Points polyBezier is a series of unfilled bezier curves defined by a list of 3n 1 control Points polygon is a filled polygon defined by a list of Points text is a rendered String Portability Note e polyBezier is not provided in the X11 implementation of this library e shearEllipse is implemented by polygons on both Win32 and X11 End Portability Note 2 2 Graphic Modifiers One of the most useful properties of Graphics is that they can be modified in various ways Here is a selection of the modifiers available gt withFont Font gt Graphic gt Graphic gt withTextColor RGB gt Graphic gt Graphic
24. raphics at the tail of the list 2 4 Attribute Generators The graphic modifiers listed at the start of Section 2 2 use attributes with types like Font RGB and Brush but so far we have no way of generating any of these attributes Some of these types are concrete you can create them using normal data constructors and some are abstract you can only create them with special attribute generators Here s the definitions of the concrete types Text background modes data BkMode Opaque Transparent gt type Angle Double gt type Dimension Int gt type Point Dimension Dimension gt data RGB RGB Int Int Int gt gt Text alignments gt type Alignment HAlign VAlign gt names have a tick to distinguish them from Prelude names blech gt data HAlign Left Center Right gt data VAlign Top Baseline Bottom gt gt gt The attributes Font Brush and Pen are abstract and are a little more complex because we want to delete the font brush or pen once we ve finished using it This gives the attribute generators a similar flavour to the modifiers seen in section 2 2 these functions are applied to an argument of type a Graphic and return a Graphic gt mkFont Point gt Angle gt Bool gt Bool gt String gt gt Font gt Graphic gt Graphic gt mkBrush RGB gt Brush gt Graphic gt Graphic gt mkPen Style gt Int gt RGB gt
25. t withBkColor os RGB gt Graphic gt Graphic gt withRGB z RGB gt Graphic gt Graphic What do these different modifiers and attributes control Brushes are used when filling shapes so the brush color is used when drawing polygons ellipses and regions Pens are used when drawing lines so the pen color is used when drawing arcs lines polylines and polyBeziers Pens also have a style and a width The Style argument is used to select solid lines or various styles of dotted and dashed lines gt data Style gt Solid gt Dash Iae gt Dot Serer gt DashDot oe gt DashDotDot _ _ _ gt Null gt InsideFrame TextColor is used as the foreground color when drawing text BkColor is used as the background color when drawing text with background mode Opaque The back ground color is ignored when the mode is Transparent Finally withRGB is a convenience function which sets the brush pen and text colors to the same value Here is its definition gt withRGB RGB gt Graphic gt Graphic gt withRGB c g gt mkBrush c brush gt withBrush brush mkPen Solid 2 c pen gt withPen pen withTextColor c 8 Portability Note VVVV MV e On Win32 the pen is also used to draw a line round all the filled shapes so the pen color also affects how polygons ellipses and regions are drawn e One of the Win32 gotchas is that the choice of S
26. the I0 library use Haskell s error catching facilities to ensure that the right operation is performed even if the middle operation raises an I0Error whilst those in the Graphics library use Hugs exception handling facilities to ensure that the right operation is performed even if the middle operation raises an exception End aside Using these combinators it is trivial to implement the modifiers described in section 2 2 gt withFont x bracket_ selectFont x selectFont gt withTextColor x bracket_ setTextColor x setTextColor gt withTextAlignment x bracket_ setTextAlignment x setTextAlignment gt withBkColor x bracket_ setBkColor x setBkColor gt withBkMode x bracket_ setBkMode x setBkMode gt withPen x bracket_ selectPen x selectPen gt withBrush x bracket_ selectBrush x selectBrush References 1 A Nye Xlib Programming Manual O Reilly and Associates Inc 1988 ISBN 0 937175 26 9 2 C Petzold Programming Windows Microsoft Press 1999 ISBN 1 57321 995 X hardback 3 S Peyton Jones A Gordon and S Finne Concurrent Haskell In Conference record of POPL 96 23rd ACM SIGPLAN SIGACT Symposium on Principles of Programming Languages pages 295 308 St Petersburg Beach FL January 1996 ACM press A Quick Reference The exported stable interface of the library consists of all symbols exported from GraphicsCore and GraphicsUtils GraphicsUtils reexports all symbols exported by Gr
27. tyle only applies if the width is 1 or less With greater widths the pen style will always be Solid no matter what you try to select This problem does not apply to X11 End Portability Note 2 6 Named Colors Working with RGB triples is a pain in the neck so the GraphicsUtils module provides these built in colors as convenient abbreviations gt data Color gt Black gt Blue gt Green gt Cyan gt Red gt Magenta gt Yellow gt White gt deriving Eq Ord Bounded Enum Ix Show Read This type is useful because it may be used to index an array of RGB triples gt colorTable Array Color RGB For example we provide this function which looks up a color in the colorTable and uses that color for the brush pen and text color gt withColor Color gt Graphic gt Graphic It s worth pointing out that there s nothing magical about the Color type or our choice of colors If you don t like our choice of colors our names or the way we mapped them onto RGB triples you can write your own To get you started here s our implementation of withColor and colorTable gt withColor c withRGB colorTable c gt gt colorTable array minBound maxBound colorList gt gt colorList Color RGB gt colorList gt Black RGB 0 o0 0 gt Blue RGB 0O O 255 gt Green RGB O 255 0 gt Cyan RGB O 255 255 gt Red RGB 255
28. us all Graphic types and operations listed in section 2 can also be used with the Draw monad 15 data Draw a instance Functor Draw where instance Monad Draw where VVVV MV type Graphic Draw The emptyGraphic and overGraphic functions are implemented using this monad Their definitions should not be surprising return g2 gt gt gi gt emptyGraphic gt g1 overGraphic g2 6 2 Draw modifiers and generators The difference between the Draw monad and the Graphic type is that the Graphic modifiers and combinators respect the structure of the graphic see section 2 3 For example the withBrush modifier only affects the color of the Graphic it is applied to it does not affect the color of the Graphic it is embedded in In contrast the Draw monad provides operations which change the effect of subsequent drawing operations The following operations correspond to the graphics modifiers described in section 2 2 gt selectFont Font gt Draw Font gt setTextColor RGB gt Draw RGB gt setTextAlignment Alignment gt Draw Alignment gt setBkColor RGB gt Draw RGB gt setBkMode BkMode gt Draw BkMode gt selectPen Pen gt Draw Pen gt selectBrush Brush gt Draw Brush These operations all have a type of the form a Draw a The value returned is the old value of the attribute being changed and can be used to restore the attribute to its previous value For example the with
29. z lt getWindowRect w gt return sz e Resize occurs when the window is closed Portability Note e Programmers should assume that the Event datatype will be extended in the not too distant future and that individual events may change slightly As a minimum you should add a match anything alternative to any function which pattern matches against Events e X11 systems typically have three button mice Button 1 is used as the left button button 3 as the right button and button 2 the middle button is ignored End Portability Note As examples of how getWindowEvent might be used in a program here are the definitions of getKeyEx and getButton gt getKeyEx Window gt Bool gt IO Char gt getKeyEx w down loop gt where gt loop do gt e lt getWindowEvent w gt case e of gt Key char c isDown gt isDown down gt gt return c gt _ gt loop gt getButton Window gt Bool gt Bool gt IO Point gt getButton w left down loop gt where gt loop do gt e lt getWindowEvent w gt case e of gt Button pt isLeft isDown gt isLeft left amp amp isDown down gt gt return pt gt _ gt loop 4 4 Using Timers If you want to use a timer you have to open the window using openWindowEx instead of openWindow 13 gt openWindowEx Title gt Maybe Point gt Size gt gt RedrawMode gt Maybe Time gt IO Window gt gt
Download Pdf Manuals
Related Search
Related Contents
Optique Installation Instructions and Operating Instructions SI acaba de mejorar más! - VIP Wireless Wholesale TAFCO WINDOWS NU2-331V-W Installation Guide 電源を入れる 団 重量センサーの「0点調節」をする 画 コロル ネコトイレ55Hすのこ付 Instruções Manual de Instruções Universal Trustix Secure Linux 3.0 Installation Manual Wpro UCF006 Calculated Industries Time Master II Copyright © All rights reserved.
Failed to retrieve file