I have written several extensions for NetLogo. The most general purpose one is the ‘graphics’ extension, which exposes a number of the Java Graphics2D commands for drawing on the canvas. Additional extensions are ‘piechart’, which draws a piechart for a specified distribution at a point, and ‘urn’, which provides Polya and Hoppe-Polya urns as primitive objects. (If you would like the source for the ‘piechart’ or ‘urn’ extensions, please email me.)
The applet below provides a simple illustration of what can be done with the graphics extension. Upon clicking 'setup', a bezier spline is drawn, with the two control points identified as dark green circles. Clicking 'go' makes the control points draggable, with the spline updated in real time. The source code is available for download. (Please note that the NetLogo code is slightly more complex than it would need to be, if all you wanted to do was make the control points draggable. The additional complexity minimises flickering of the display.)
Please note that the graphics extension is currently in beta. Although it mostly feature-complete, it is likely that there are some issues which need to resolved.
You can download the extension (compiled for NetLogo 5.0) or, alternatively, the source code.
A more detailed illustration of what is possible with the graphics extension is available. As is a simple foraging model which uses a gradient fill to illustrate the in-cone of an agent.
The graphics extension has several state variables which determine how objects are drawn. These are: the current stroke-color, the current stroke (i.e., thin, thick, dashed, etc.), the current fill-color, the current font, the current font-color, the amount of extra space to pad in the x or y dimension when drawing text, and two gradient fills. The gradient fills are either a linear gradient fill (used by the "fill-gradient-*" commands) or a radial gradient fill (used by the "fill-radial-gradient-*" commands). These can all be changed using the corresponding "set-*" command described below.
Graphics primitives:graphics:initialize graphics:draw-arc graphics:draw-oval graphics:draw-circle graphics:draw-polygon graphics:draw-path graphics:draw-rectangle graphics:draw-line graphics:draw-text graphics:fill-arc graphics:fill-gradient-arc graphics:fill-radial-gradient-arc graphics:fill-oval graphics:fill-gradient-oval graphics:fill-radial-gradient-oval graphics:fill-circle graphics:fill-gradient-circle graphics:fill-radial-gradient-circle graphics:fill-polygon graphics:fill-gradient-polygon graphics:fill-radial-gradient-polygon graphics:fill-path graphics:fill-gradient-path graphics:fill-radial-gradient-path graphics:fill-rectangle graphics:fill-gradient-rectangle graphics:fill-radial-gradient-rectangle graphics:set-font graphics:set-gradient graphics:set-radial-gradient graphics:set-stroke-thickness graphics:set-fill-color graphics:set-text-color graphics:set-stroke-color graphics:set-stroke graphics:set-text-padding
This function needs to be called before any other functions are used. The three parameters passed provide the necessary information to translate from NetLogo coordinates to Java graphics coordinates.
Let E be an ellipse defined by the bounding box with upper-left corner at (x,y) with the given width and height. Then the arc drawn is that part of the perimeter of E falling between start-angle and end-angle. Note that the angles are specified using standard geometric conventions — not the NetLogo convention used for turtle headings.
Draw an ellipse falling in the bounding box with upper left corner at (x,y) and the given width and height.
Draw a circle centered at (x,y) with the given radius.
Draw a polygon with the specified vertices. Each point is a list of the form [xi, yi].
Draw a path beginning at point with edges consisting of either straight lines, quadratic curves, or bezier splines. The shape of the ith edge is determined by the form of the ith specifier. Specifiers may be of the following forms:
| [ x y ] | Draw a straight line from the previous point on the path to (x,y). |
| Draw a quadratic curve from the previous point on the path to (x2, y2) using (x1, y1) as a control point. | |
| Jump to the point (x, y) without stroking any path between the previous point and the new point. | |
| Draw a bezier spline from the previous point on the path to (x3, y3) using (x1, y1) and (x2, y2) as control points. |
Draw a rectangle with upper-left corner at (x, y) and having the specified width and height.
Draw a straight line through the specified points.
Draw string using the current font, font-color and padding at point (x, y) according to the specified anchor. The anchor indicates whether the string should be centered (horizontally and vertically) at that point, or aligned according to some other part of its bounding box. The anchor is a string consisting of exactly one of the following: C, N, S, E, W, NE, NW, SE, SW. So, using "C" as the anchor will center the text. Using "SE" will draw the text such that the south-east corner of its bounding box is at the point (x, y).
Fills the specified arc (see draw-arc for details) with the current color.
Fills the specified arc (see draw-arc for details) with the current linear gradient paint.
Fills the specified arc (see draw-arc for details) with the current radial gradient paint.
Fills the specified oval (see draw-oval for details) with the current fill color.
Fills the specified oval (see draw-oval for details) with the current linear gradient paint.
Fills the specified oval (see draw-oval for details) with the current radial gradient paint.
Fills the specified circle (see draw-circle for details) with the current fill color.
Fills the specified circle (see draw-circle for details) with the current linear gradient paint.
Fills the specified circle (see draw-circle for details) with the current radial gradient paint.
Fills the specified polygon (see draw-polygon for details) with the current fill color. The fill algorithm currently uses the even-odd fill rule for overlapping polygons.
Fills the specified polygon (see draw-polygon for details) with the current linear gradient paint. The fill algorithm currently uses the even-odd fill rule for overlapping polygons.
Fills the specified polygon (see draw-polygon for details) with the current radial gradient paint. The fill algorithm currently uses the even-odd fill rule for overlapping polygons.
Fills the specified path (see draw-path for details) with the current fill color. The fill algorithm currently uses the even-odd fill rule for overlapping paths. Notice that using the "moveto" specifier enables one to create and fill paths which are not simply connected.
Fills the specified path (see draw-path for details) with the current linear gradient paint. The fill algorithm currently uses the even-odd fill rule for overlapping paths. Notice that using the "moveto" specifier enables one to create and fill paths which are not simply connected.
Fills the specified path (see draw-path for details) with the current radial gradient paint. The fill algorithm currently uses the even-odd fill rule for overlapping paths. Notice that using the "moveto" specifier enables one to create and fill paths which are not simply connected.
Fills the specified rectangle (see draw-rectangle for details) with the current fill color.
Fills the specified rectangle (see draw-rectangle for details) with the current linear gradient paint.
Fills the specified rectangle (see draw-rectangle for details) with the current radial gradient paint.
Sets the current font to the one requested. Note that for maximal portability the font-name should be one of the following logical font names: "Serif", "SansSerif", "Monospaced", "Dialog", or "DialogInput", as these are guaranteed by Java to be present on all platforms. You can specify other font names, such as "Garamond", but this may make your NetLogo model less portable.
The style should either be "plain" or a combination of "italic" or "bold". You can mix the latter two, with both "bold italic" or "italic bold" being valid.
The size is the font size in printer's points (1/72 of a inch).
A linear gradient paint is defined by specifying two points (x1, y1) and (x2, y2), colors identified with both of those points, and a boolean value indicating whether the gradient should cycle or not. The line connecting (x1, y1) and (x2, y2) does two things: it represents the "direction" of the gradient (i.e., horizontal, vertical, or sloped) and it also represents how fast or slow the gradient occurs. The gradient is defined so that color1 is 100% present at (x1, y1) and color2 is 100% present at (x2, y2).
Colors are standard NetLogo colors in either RGB or RGBA form. So, for example, [255 0 0] is red and [0 255 0 125] is a partially opaque green.
If the gradient is not cyclic, any points lying outside the gradient region will be purely color1 or color2. Here is an example: suppose the first argument is [-5 0 [255 0 0]] and the second argument is [5 0 [0 255 0]]. This defines a horizontal gradient. If cyclic is false, then any point in a shape whose x-coordinate is less than -5 will be red, and any point whose x-coordinate is greater than 5 will be green. If cyclic is true, then the gradient will repeat.
Radial gradients are more complex than linear gradients.
A radial gradient is a circular gradient centered at (x, y) with the specified radius. Unlike linear gradients, more than two colors can be specified. The fourth argument is a list of fractions which must be in the range [0,1] and increasing in value. These fractions identify where, in the expanding circular gradient, each of the specified colors occurs at 100%.
Suppose we have a radial gradient fill consisting of four colors: red, green, blue and white. Then color-list would take the form [[255 0 0] [0 255 0] [0 0 255] [255 255 255]]. If we set fractions to [0 .33 .66 1], then the radial gradient will space the four colours evenly through the circular region. If we set fractions to [0 .1 .9 1], then most of the radial gradient fill will consist of green fading into blue.
Unlike set-gradient, there are three choices of cycle-method: "norepeat", "reflect" or "repeat".
Sets the current stroke used when drawing shapes to a solid line of width thickness.
Sets the current fill color to color, specified in either RGB or RGBA format.
Sets the current text color to color, specified in either RGB or RGBA format.
Sets the current stroke color, used when drawing shapes, to color, specified in either RGB or RGBA format.
This command has three valid forms:
| Type | Value |
|---|---|
| "solid" | thickness |
| "dashed" | thickness |
| "custom" | [thickness cap-style join-style miter-limit dash-pattern-list dash-phase] |
When the type is "custom", a list of six values must be specified. These values are as follows:
| thickness | A number indicating the thickness of the stroke. |
| cap-style | Either "butt", "round", or "square". |
| join-style | Either "bevel", "miter", or "round". |
| miter-limit | A number indicating the maximum miter length. |
| A list of the form [stroke-length space-length …]. So, for example, to create a dotted-dashed stroke, you would specify a dash-pattern of [5 5 15 5]. This would draw a stroke of 5 pixels (basically a dot), then a space of 5 pixels, then a stroke of 15 pixels (the dash), then a space of 5 pixels. This dash pattern is then repeated as necessary. | |
| dash-phase | A number between 0 and 1 (inclusive) indicating where in the dash-pattern we are to begin, when stroking a line. |
Sets the amount of padding (in pixels) around text to x-padding and y-padding.