5. GraphicsWindow ObjectPreviewWe saw the text window is useful for input and output of text based information. In this chapter, we look at the object used to host most Small Basic programs – the GraphicsWindow. With this object, we can build very power applications. GraphicsWindow ObjectGraphicsWindow Coordinates: The GraphicsWindow object is a cornerstone of Small Basic programming. In the graphics window, we can draw lines, shapes, and text in many colors. We can host controls (buttons and text boxes). We can receive mouse and keyboard input from a user. The coordinate system used by the graphics window is: |
This chapter is adapted from the book The Developer’s Reference Guide To Microsoft Small Basic by Philip Conrod and Lou Tylee. To purchase this book in its entirety, please see the Computer Science For Kids web site. |
The window is Width pixels wide and Height pixels high. We use two values (coordinates) to identify a single pixel in the window. The x (horizontal) coordinate increases from left to right, starting at 0. The y (vertical) coordinate increases from top to bottom, also starting at 0. Points in the region are referred to by the two coordinates enclosed in parentheses, or (x, y). GraphicsWindowProperties: BackgroundColor BrushColor CanResize FontBold FontItalic FontName FontSize Height LastKey LastText Left MouseX MouseY PenColor PenWidth Title Top Width GraphicsWindowMethods: Clear() DrawBoundText(x, y, text, w, h)
DrawEllipse(x, y, w, h) DrawImage(image, x, y) DrawLine(x1, y1, x2, y2) DrawRectangle(x, y, w, h) DrawResizedImage(image, x, y, w, h) DrawText(x, y, text) DrawTriangle(x1, y1, x2, y2, x3, y3) FillEllipse(x, y, w, h) FillRectangle(x, y, w, h) FillTriangle(x1, y1, x2, y2, x3, y3) GetColorFromRGB(red, green, blue) GetRandomColor() Hide() SetPixel(x, y, c) Show() ShowMessage(text, title)
GraphicsWindowEvents: KeyDown KeyUp MouseDown MouseMove MouseUp TextInput
GraphicsWindow FeaturesBy default, the graphics window has a white background: The default window is 624 pixels wide (Width) by 444 pixels high (Height). To change the background color for the entire window, set the GraphicsWindow.BackgroundColor. Similar to the text window, the graphics window is located on your computer screen as follows: To center the graphics window on your computer screen, use these relations: GraphicsWindow ColorsThe graphics window (and also the text window) uses colors for various program elements. Colors are specified by color names. The color names used by Small Basic are listed in Appendix I to this guide. There are two other ways to get colors in Small Basic. The GetRandomColor method: GraphicsWindow.GetRandomColor() will return a random color. It is fun to use for display and games. The GetColorFromRGB method: GraphicsWindow.GetColorFromRGB(Red, Green, Blue) builds a color based on three specified components: Red,Green, Blue, each of which range from 0 to 255. The graphics window background color is set by: GraphicsWindow.BackgroundColor And, as mentioned, the default value for this color is “White”. Lines and shapes in Small Basic are drawn using a “pen.” The color and width of the pen is specified by: The default value for PenColor is “Black” and the default PenWidth is 2. Shapes and text (yes, text) are filled (painted) using a “brush.” The color of the brush is specified by: GraphicsWindow.BrushColor The default value for BrushColor is “SlateBlue”. GraphicsWindow FontThe font used to draw text in the graphics window is specified by four different properties The FontName property is the name of the font. The default value is “Tahoma”. Other font names can be found by opening a word processor and selecting the change font option. The FontSize property sets the size of the current font. The default value is 12. FontBold can have one of two values. If “true”, the font will be bold. If “false”, it will not be bold. The default value is “true”. Similarly, FontItalic indicates if a font is italicized. The default value is “false” – no italics. Example 5-1. GraphicsWindow PropertiesWrite a program that writes “Graphics Window” in a large, bold, italic font in the middle of a yellow graphics window. Set window size to 400 by 150 pixels. Small Basic Code: Saved as Example 5-1 in Guide to Small Basic\Programs\Chapter 5 folder. Save and Run the program to see the results: Example 5-2. SetPixel MethodWrite a program that fills a graphics window with randomly colored pixels. Small Basic Code: Saved as Example 5-2 in Guide to Small Basic\Programs\Chapter 5 folder. This code just “marches” through all the pixels and assigns a random color to each. Save and Run the program to see the results (it takes a while for the window to fill): Example 5-3. DrawLine MethodWrite a program that draws randomly colored lines from the center of a graphics window out to some random point in the window. Small Basic Code: Saved as Example 5-3 in Guide to Small Basic\Programs\Chapter 5 folder. Each line has the same starting point (the center of the window). Then, lines are drawn to the random point (X, Y). Save and Run the program to see the results. Look at all the pretty lines: Example 5-4. Drawing RectanglesWrite a program that draws a red rectangle, surround by a blue border. Small Basic Code: Saved as Example 5-4 in Guide to Small Basic\Programs\Chapter 5 folder. Save and Run the program to see the rectangle: We fill the rectangle first, then draw. This insures the border is not erased by the fill operation. Example 5-5. Drawing EllipsesWrite a program that draws a yellow ellipse, surround by a red border. Small Basic Code: Saved as Example 5-5 in Guide to Small Basic\Programs\Chapter 5 folder. Save and Run the program to see the ellipse: Again, fill then draw to see the full border. Example 5-6. Drawing TrianglesWrite a program that draws a green-bordered triangle that connects these three points (250, 50), (50, 200), (350, 250). Small Basic Code: Saved as Example 5-6 in Guide to Small Basic\Programs\Chapter 5 folder. Save and Run the program to see the triangle (identify the three points): Chapter ReviewAfter completing this chapter, you should understand:
We will do a lot more with the graphics window as we learn more about Small Basic. In particular, we will learn about using the graphics window events to recognize key presses and mouse clicks. Next, we learn about some of the controls associated with Small Basic and the idea of event-driven programming. © Copyright 2010 By BibleByte Books. All Rights Reserved. BibleByte Books, the BibleByte Books Logo, Computer Science For Kids, the Computer Science For Kids logo, and related trade dress are trademarks or registered trademarks of BibleByte Books. |