If you want to display something on the screen, you must first open a window. You can use a class named 'ScheduledWindow' to do this. An instance of this class can have a display component. For example:
Program-1-1: (ScheduledWindow, Time, ComposedText) ---------------------------------------------------------------------- | aScheduledWindow | aScheduledWindow := ScheduledWindow new. aScheduledWindow component: Time now printString asComposedText. aScheduledWindow open ----------------------------------------------------------------------
The above Program shows how to create an instance of ScheduledWindow, and how to specify the textualized current time as a display component. However the Program lacks many things, for example a window title and an appropriate minimum size for the window. Program 1-2 adds these:
Program-1-2: (ScheduledWindow, Time, ComposedText; window label) ---------------------------------------------------------------------- | aScheduledWindow | aScheduledWindow := ScheduledWindow model: nil label: 'Time Now' minimumSize: 200 @ 200. aScheduledWindow component: Time now printString asComposedText. aScheduledWindow open ----------------------------------------------------------------------
Program 1-2 shows how to specify a window label and a minimun size for the window. You can use the message 'model:label:minimumSize:' instead of 'new' when you create an instance of 'ScheduledWindow'. Note that nil is specified as the window's model. 'model' is one aspect of the Model-View-Controller (MVC) paradigm. In this context an instance of 'ScheduledWindow' is the controller. Please move the mouse cursor into the window you opened and press the blue button. A menu will be raised to do various things related to windows, such as resizing, or moving. An instance of 'StandardSystemController' is doing this. Now let's try to replace the current time with something else. Any object which accepts display messages like 'displayOn:' can be a display component. Program 1-3 is a program which displays a bitmap taken from your screen.
Program-1-3: (ScheduledWindow, Image; create, fromUser) ---------------------------------------------------------------------- | aScheduledWindow | aScheduledWindow := ScheduledWindow model: nil label: 'Image' minimumSize: 200 @ 200. aScheduledWindow component: Image fromUser. aScheduledWindow open ----------------------------------------------------------------------
This program allows the user to select a rectangle anywhere on the screen and then creates a window in which that bitmap is displayed. In case a picture is larger than the containing window, you will want to add horizontal and vertical scroll bars. I will explain how you can do this later. In this program, I hope you understand how to create instances and specify display components.
Lets take a closer look at opening a window. You send an 'open' message to an instance of 'ScheduledWindow' to open a 'ScheduledWindow' window. Then an instance of 'ScheduledWindow' shows the minimum size of the window with dashed lines and waits for you to use the mouse to visually specify how large the window should be. A class named 'Rectangle' expresses the position and the size of a window. When you say 'Image fromUser' the 'Rectangle' class is used to obtain the coordinates from the user. The result is that you get an instance of 'Rectangle' indicated by coordinates of the screen.
Program-1-4: (Rectangle; create, fromUser) ---------------------------------------------------------------------- Rectangle fromUser ----------------------------------------------------------------------
If you inspect Program 1-4, you will see that the result of drawing a rectangle on the screen is an instance of Rectangle. You also use 'openIn:' instead of 'open' to open a window any place and any size you want. The argument must be an instance of 'Rectangle'. Program 1-5 illustrates this:
Program-1-5: (ScheduledWindow, Image; window component, openIn) ---------------------------------------------------------------------- | aScheduledWindow | aScheduledWindow := ScheduledWindow model: nil label: 'Image' minimumSize: 200 @ 200. aScheduledWindow component: Image fromUser. aScheduledWindow openIn: (100 @ 100 extent: 200 @ 200) ----------------------------------------------------------------------
Please try to understand what the 'openIn' message is doing. Program 1-6 illustrates.
Program-1-6: (ScheduledWindow, Image, Rectangle; window component, openIn) ---------------------------------------------------------------------- | aScheduledWindow | aScheduledWindow := ScheduledWindow model: nil label: 'Image' minimumSize: 200 @ 200. aScheduledWindow component: Image fromUser. aScheduledWindow openIn: Rectangle fromUser ----------------------------------------------------------------------
This program uses two rectangles. The first captures the bitmap on the screen and the second indicates the size of a new window. See how 'Image fromUser' uses 'Rectangle fromUser' in its method.
Next we will open a minimum size window at a position so that the current mouse position is at its center. You send the 'cursorPoint' message to the 'InputSensor' class to get the current coordinates of mouse.
Program-1-7: (InputSensor; cursor, mouse position) ---------------------------------------------------------------------- InputSensor cursorPoint ----------------------------------------------------------------------
Inspecting Program 1-7 will show you that a Point is returned indicating the present position of the mouse in screen coordinates. You calculate 'aRectangle' (the window area) by using 'aPoint' (the current mouse coordinates).
Program-1-8: (ScheduledWindow, InputSensor, Point; align, extent, position) ---------------------------------------------------------------------- | aScheduledWindow aPoint aRectangle | aScheduledWindow := ScheduledWindow model: nil label: 'Image' minimumSize: 200 @ 200. aScheduledWindow component: Image fromUser. aPoint := InputSensor cursorPoint. aRectangle := Point zero extent: aScheduledWindow minimumSize. aRectangle := aRectangle align: aRectangle center with: aPoint. aScheduledWindow openIn: aRectangle ----------------------------------------------------------------------
Notice the 'align:with:' message sent to 'aRectangle'. It asks the rectangle to align itself with the mouse position ('aPoint') as the center point. Now you can create a window anywhere on the screen.
Summary