简体   繁体   English

在Smalltalk吱吱声中绘制矩形

[英]Drawing A Rectangle in Smalltalk Squeak

given two integer variables 'a' and 'b' in a rectangle class, how do you draw a rectangle? 给定矩形类中的两个整数变量“ a”和“ b”,如何绘制矩形? I'm new to smalltalk and im studying it for a course. 我是Smalltalk的新手,正在为课程学习。 thanks! 谢谢!

Squeak uses Morphic as its default UI. Squeak使用Morphic作为默认UI。 So the simplest thing would be to create a Morph : 因此,最简单的方法是创建一个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. 这将创建一个新的RectangleMorph实例,将其范围设置为在300200创建的Point (通过将消息@发送到300以及参数200 ),还openInWorld发送openInWorld消息,使其出现在世界上。 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. 我们可以使用另一个Point参数将其发送给position:消息,但是您可以轻松地用鼠标指针将其移到任意位置。

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). 在您的课程中,您可以使用a@b创建范围点(假设ab是矩形的宽度和高度,以像素为单位)。

Morphic is nice because it creates real objects that you can manipulate interactively, eg by cmd-clicking to bring up a Halo . Morphic很不错,因为它创建了可以交互操作的真实对象,例如,通过cmd单击以显示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. ...,其中Display是一个全局Form实例(包含Bitmap ),引用了整个Squeak显示。 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. 相反, Morphs知道如何在需要时重绘自身。

It's also possible to create your own Morph subclass and implement a custom drawOn: method. 也可以创建自己的Morph子类并实现自定义drawOn:方法。 But that would be overkill for something as simple as showing a rectangle. 但是,对于像显示矩形这样的简单操作来说,这太过分了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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