Basic movement and timing

This example illustrates several key aspects in JBotSim:

The code is composed of two elements: a class MovingNode that defines the new type of node, and a main() method (to be put in any class, possibly the same) that tells JBotSim to use this type of node as default.

import jbotsim.Node;

public class MovingNode extends Node{
    public void onStart(){
    public void onClock(){

Inheritance: You can create new types of nodes by "extending" the Node class.

Clock and Timing: Overriding the onClock() method allows you to perform some operation periodically, such as, here, moving your node. You can also initialize things when the node is started by overriding onStart().

Movement Specification: Movements of your node can be specified in many different ways. Here, we first draw at random some direction on startup by calling setDirection() (see class Node for details) with a random angle (in radian). Then, in each round, our node moves by one unit of distance in this direction, using the move() method. (This method can also take an argument, specifying the distance to be moved.) The call to wrapLocation() re-locates the node in a toroidal fashion if it gets out of bounds.

public static void main(String[] args) {
    Topology tp = new Topology(400, 300);
    new JViewer(tp);

Setting default node type: When the user left-clicks on the surface, a new node is added to the topology. The type of node to be used by default can be set to the desired class, by calling setDefaultNodeModel() as above. This instruction causes future left-clicks to create MovingNode instances instead of Node instances.