简体   繁体   中英

Drawing A Rectangle in Smalltalk Squeak

given two integer variables 'a' and 'b' in a rectangle class, how do you draw a rectangle? I'm new to smalltalk and im studying it for a course. thanks!

Squeak uses Morphic as its default UI. So the simplest thing would be to create a Morph :

RectangleMorph new
    extent: 300@200;
    openInWorld

Evaluate all three lines at once. This creates a new RectangleMorph instance, sets its extent to a Point created from 300 and 200 (by sending the message @ to 300 with an argument of 200 ), and also sends it the openInWorld message so it appears in the world. It will open in the top-left screen corner. We could have sent it the position: message with another Point argument, but you can as easily just grab it with your mouse pointer and move it anywhere you please.

In your class you might use a@b to create the extent point (assuming a and b are the rectangle's width and height in pixels).

Morphic is nice because it creates real objects that you can manipulate interactively, eg by cmd-clicking to bring up a Halo . If you don't want that, you can also paint on the screen directly. Eg:

Display fill: (0@0 extent: 300@200) fillColor: Color red.

... where Display is a global Form instance (containing a Bitmap ) refering to the whole Squeak display. But since that expression just puts pixels on the screen, they will be overwritten quickly. Morphs , in contrast, know how to redraw themselves whenever needed.

It's also possible to create your own Morph subclass and implement a custom drawOn: method. But that would be overkill for something as simple as showing a rectangle.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM