Painting and drawing the GUI background

You can implement a custom BackgroundPainter that performs some painting on the background (using java's Graphics primitives). Surface painters must be attached to the JTopology, as shown in this example (here the painter class is called BackgroundPainter, defined below).

public static void main(String[] args) {
    Topology tp = new Topology();
    JViewer jv = new JViewer(tp);
    jv.getJTopology().addBackgroundPainter(new BackgroundPainter());
}    

In this example, two operations are performed on the background:

The picture used in this example can be downloaded here: forest.jpg.

import jbotsim.Topology;

import java.awt.*;

public class BackgroundPainter
        implements jbotsim.ui.painting.BackgroundPainter {
    @Override
    public void paintBackground(Graphics2D g, Topology tp) {
        // Paints a background image
        Toolkit tk = Toolkit.getDefaultToolkit();
        Image image = tk.getImage(getClass().getResource("/forest.jpg"));
        g.drawImage(image, 0, 0, null);

        // Draws a grid (line by line)
        g.setColor(Color.gray);
        for (int i = 0; i < 600; i++){
            for (int j = 0; j < 400; j++){
                g.drawLine(i*50, 0, i*50, 400);
                g.drawLine(0, j*50, 600, j*50);
            }
        }
    }
}    

Then JBotSim superposes the usual text (nodes, links, etc.). Note that several surface painters can be used; they are called in the registration order.