The Capsis training online

Back to the table of contents

Solution: 11. Random regeneration

Try to work with the helping elements before looking at the solution…

Helping elements

a. Options for regenerationMax
// beware in okAction (), regenerationMax is not a double like gp4:
double gp4 = TextFieldChecker.getPositiveDoubleValue(growthP4, Translator.swap("TraInitialDialog.growthP4"));

// A possible check for the user value of rm:
int rm = TextFieldChecker.getPositiveIntValue (regenerationMax, Translator.swap("TraInitialDialog.regenerationMax"))
b. Random numbers and tree coordinates

The java random numbers generator

Random is a random number generator. an import java.util.Random is needed.

It can be asked random ints or random doubles by calling nextInt(n): returns a value between 0 included and n excluded, or nextDouble(): returns a double value between 0 included and 1 excluded (uniform distribution)

  • create an instance with random = new Random ()
  • to draw a random integer in [0, n[, use random.nextInt(n)
  • to draw a random integer in [1, regenerationMax], use random.nextInt(regenerationMax) + 1
  • to draw an random number (double) in [0, 1[, use random.nextDouble ()
  • to draw an random number (double) in [0, xSize[, use random.nextDouble () * xSize
  • to draw an random number (double) in [originX, originX + xSize[, use originX + random.nextDouble () * xSize
  • Check the online javadoc by Oracle for more details on the Random class
  • e.g. double proba = random.nextDouble (); # between 0 and 1 (1 excluded)

Origin and size of the scene

In Capsis, the scene coordinates and sizes can be found in the scene class (i.e. the subclass of capsis.kernel.GScene. You can get the values with these accessors, they are all in meters:
  • getOrigin (): returns an instance of Vertex3d with x, y and z of the point with the lower (x, y) coordinates
  • getXSize () and getYSize () return the size of the scene along resp. the x coordinates axis and the y axis
  • you will need to import jeeb.lib.util.Vertex3d;
c. Tree identifiers and trees creation
TraTree t = new TraTree (id, newScene, age, height, dbh, crownBaseHeight, crownRadius, x, y, z);

newScene.addTree (t);

A possible solution

Solution elements, to be used in a TraModel.regeneration (TraScene newScene) method A close example to be adapted from a former training module: MaddModel.processRegeneration(MaddScene newStand) A view of the initial dialog for option 2: A textfield has been added to ask user a value for regenerationMax, but translation are missing in the langage properties files Checking: opening a 3D viewer from the 2D viewer after a 20 years long simulation: the new trees are visible, they are growing with the others The initial dialog with the fixed translations

Back to the table of contents