text stringlengths 0 897 |
|---|
]gfx.print "Hello world!", 600, 300, color.aqua, "small" |
]gfx.print "Hello world!", 600, 250, color.aqua, "large" |
``` |
That first line above displays the parameter list of the `gfx.print` function. This is a handy trick that works with any function. |
D> If you remember the name of a function but need to review its parameters, enter an at sign (@) followed by the function name. The REPL will display the parameter names and default values. |
The rest of the above example draws our classic greeting in various colors and sizes. |
{width:"75%"} |
 |
Of course there are costs to the increased flexibility of drawing text with `gfx`: it is substantially slower than using `text`, you can't easily change the text once it's drawn, and you certainly can't read the text back from the display, as it's just a bunch of pixels. |
## Working with Images |
There are only a couple more methods left on the `gfx` module: `gfx.drawImage` and `gfx.getImage`. But to understand them, we first need to cover the Image class. |
{i:"`Image`;Mini Micro, `Image` class;pictures, in Mini Micro"} |
`Image` is a class built into Mini Micro for representing pictures. Images are usually loaded from disk, but they can also be built pixel by pixel from scratch, or plucked right out of a graphics display. Once you have an image, you can either draw it directly to a graphics display, or (as we'll see in Chapter 22) tu... |
Let's jump in with an example first, and then we'll go over the details. |
```terminal |
]img = file.loadImage("/sys/pics/Mochi.png") |
]gfx.drawImage img, 0, 0 |
]gfx.drawImage img, 120, 0, img.width*6, img.height*6 |
``` |
{gap:20} |
{width:"50%"} |
 |
All the `file` methods you learned in Chapter 13 still apply in Mini Micro. But in Mini Micro there are a couple of additional functions, including `file.loadImage`, which simply returns an image from the given file path. |
The following table shows the properties and methods of the `Image` class. |
{caption:"Image properties (all read-only) and methods.", colWidths:"150,*"} |
| `img.width` | get the image width, in pixels | |
| `img.height` | get the image height, in pixels | |
| `img.pixel(x, y)` | get the color of pixel at *x*, row *y* | |
| `img.setPixel x, y, c` | set the color of pixel at *x*, row *y* to *c* | |
| `img.getImage(left, bottom, width, height)` | get a portion of the image as a new Image | |
| `Image.create(width, height, c)` | create a new image filled with color c | |
The identifier `Image` is capitalized because, unlike `color`, `text`, and `graphics`, you are expected to be working with an *instance* of this class, rather than calling methods on `Image` itself. The one exception is `Image.create`, which is the method for creating an image from scratch. So in the table above, we ... |
Now you know all the methods available on the Image class, but honestly, you will almost never use any of them. The typical pattern is to load an image from disk, and then draw it to the screen, just as we did above. Rarely do you need to modify the content of an image, but it's nice to know that you can. |
Getting back to the `gfx` module, `gfx.drawImage` takes more parameters than anything else in the Mini Micro API — a total of nine! But you can think of it as: the image to draw, the area to draw it in, and the area of the image to use. These let you basically take any rectangular portion of the source image, and str... |
To illustrate, follow the example above with one more try that uses all nine parameters to draw just the mochi's smile, super-sized: |
```terminal |
]gfx.drawImage img, 0,0,500,100, 52,14,20,10 |
``` |
This grabs the rectangular area of the image with a bottom-left corner of 52,14, a width of 20, and a height of 10; and draws it onto the screen with its bottom-left corner at 0,0, and a stretched-out size of 500 wide by 100 tall. |
## Exploring the Demos |
{i:"`/sys/demo`,`sunset`"} |
Of the demos in `/sys/demo`, the `sunset.ms` program is the best one to review at this point. It uses only the functions you've seen here to make a lovely picture, with details that are slightly different every time. The `stars.ms` demo is also good, although it's so simple, you may find it too easy! |
{i:"turtle graphics;`/sys/demo`,`turtleDemo`"} |
The other drawing demo is `turtleDemo.ms`. This uses a trick you'll learn more about tomorrow: `import`. The `import` function lets you load some code from another file — in this case, a "turtle" module that provides for a very different approach to drawing called *turtle graphics*. The basic idea here is that there... |
A> **Chapter Review** |
A> - You explored the Mini Micro pixel display, accessed via `gfx`. |
A> - You learned how to get and set individual pixels, as well as how to draw lines, rectangles, ellipses, and polygons. |
A> - You drew text in three different sizes and in various colors. |
A> - You learned about the Image class, including how to load an image from disk and draw it (in whole or in part) to the screen. |
{chapterHead: "Day 19: Display Layers, Mouse, and Import", startingPageNum:225} |
{width: "50%"} |
 |
Q> In some ways, programming is like painting. You start with a blank canvas and certain basic raw materials. You use a combination of science, art, and craft to determine what to do with them. |
Q>— Andrew Hunt (software developer and author) |
A> **Chapter Objectives** |
A> - Learn about the 8-layer display system in Mini Micro. |
A> - Discover how to get the position of the mouse cursor and the state of its buttons. |
A> - Learn how to use `import` to load extra functionality into your program. |
PRINT>You've learned how to draw to the default graphics display with `gfx`, and how to print text to the screen with `text`. But it turns out that the Mini Micro display system is more sophisticated than this. It actually consists of eight layers, any of which can be set to any display mode. The available display m... |
EBOOK>You've learned how to draw to the default graphics display with `gfx`, and how to print text to the screen with `text`. But it turns out that the Mini Micro display system is more sophisticated than this. It actually consists of eight layers, any of which can be set to any display mode. The available display m... |
{i:"display modes", "Mini Micro, display modes"} |
| 0 | off | no display | |
| 1 | solid color | displays one color over the whole screen | |
| 2 | text | 26 row, 68 column text display | |
| 3 | pixel | 960 by 640 (by default) pixel display | |
| 4 | tile | displays a rectangular grid of small images | |
| 5 | sprite | displays a set of scaleable, rotatable, resizable images | |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.