Size

Concept

Establishing the size of a picture is sometimes needed for complex layout, and the Size algebra provides this.

Bounding Box

The core method, boundingBox, gets the BoundingBox of a picture.

import doodle.core.*
import doodle.java2d.*
import doodle.syntax.all.*

val circle =
  Picture
    .circle(100)
    .strokeColor(Color.midnightBlue)

val circleBoundingBox: Picture[BoundingBox] =
  circle.boundingBox

As the type annotation suggests, this returns a Picture[BoundingBox] rather than a BoundingBox. In other words, the BoundingBox will be calculated when the Picture is rendered and is not immediately available. This is necessary because the size may depend on backend-specific calculations, such as the space occupied by text.

To make use of the BoundingBox in the creation of a picture we can use the flatMap method on Picture.

val boundingBox =
  circleBoundingBox.flatMap(bb => 
    Picture
      .roundedRectangle(bb.width, bb.height, 3.0)
      .strokeColor(Color.hotpink)
      .strokeWidth(3.0)
  )
// boundingBox: Picture[FromGifBase64 & Basic & Bitmap & FromBufferedImage & FromPngBase64 & FromJpgBase64, Unit] = doodle.algebra.Picture$$anon$1@793f2325
  
val picture = circle.on(boundingBox)
// picture: Picture[Bitmap & FromBufferedImage & FromPngBase64 & FromGifBase64 & Basic & FromJpgBase64, Unit] = doodle.syntax.LayoutSyntax$$anon$1@5ed580d8

This produces the picture shown below.

A circle with a bounding box

Other Methods

The Size algebra defines a few utilities derived from the boundingBox method:

Implementation

The Size algebra supports all the features described above. Image does not currently support these operations.

Drawing to the Screen→