问题
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!
回答1:
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, e.g. by cmd-clicking to bring up a Halo. If you don't want that, you can also paint on the screen directly. E.g.:
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.
来源:https://stackoverflow.com/questions/36521633/drawing-a-rectangle-in-smalltalk-squeak