User's Guide

Drawing rectangles and triangles

To draw other kinds of shapes, add more drawing methods to your part and widget classes. Add two instance methods to MyShape, in your runtime application:

rectangle
  "Ask the widget to draw a filled rectangle."
  self widget drawFilledRectangle
triangle
  "Ask the widget to draw a filled triangle."
  self widget drawFilledTriangle

Then add two instance methods to MyCwShape, in your runtime application. The first method draws a filled rectangle:

drawFilledRectangle
  "Draw a filled rectangle with an outline around it."
  | gc border |
  gc := self createGC.
  border := self borderWidth.
  self window fillRectangle: gc
       x: 0
       y: 0
       width: (self width)
       height: (self height).
  border > 0 ifTrue: [
    self backgroundColor isNil ifFalse: [
        gc setForeground:
             (self window getPalette nearestPixelValue:
                               (self backgroundColor))].
  self window drawRectangle: gc
       x: border // 2
       y: border // 2
       width: (self width - border)
       height: (self height - border).
  ].
  gc freeGC.

The second method draws a filled triangle:

drawFilledTriangle
  "Draw a filled triangle with an outline around it."
  | gc points w h border |
  points := OrderedCollection new.
  w := self width.
  h := self height.
  points add: 0 @ h;
         add: (self width // 2) @ 0;
         add: (self width) @ h;
         add: 0 @ h.
gc := self createGC.
  border := self borderWidth.
  self clear: gc.
  self window fillPolygon: gc
       points: points
       shape: Convex
       mode: CoordModeOrigin.
  border > 0 ifTrue: [
    self backgroundColor isNil ifFalse: [
       gc setForeground:
          (self window getPalette nearestPixelValue:
                            (self backgroundColor))].
  points := OrderedCollection new.
  border := border // 2.
  points add: border @ (h - border);
         add: ((w // 2)) @ border;
         add: (self width - border) @ (h - border);
         add: border @ (h - border).
  self window drawLines: gc
       points: points
       mode: CoordModeOrigin.
  ].
  gc freeGC.


[ Top of Page | Previous Page | Next Page | Table of Contents | Index ]