Although this example is still valid, an easier approach for the control of an array of LEDs can be achieved with the PixelBlaze Output Expander as described here.
The LedMatrix is a template class, that you can use in your own Java-project. It is an extension of the class LEDStrip. An LED matrix can be built from one LED strip. To do this, separate the LED strip at the desired point and place the individual strips under each other or next to each other. The individual ends can then be connected to each other with a wire. The constructor can be passed either as a rectangular matrix or a user-defined matrix with different numbers of LEDs in the individual strips. A software compatible LED strip is for example the WS28xx-chip set LED Strip.
Make sure to check if SPI is enabled in your RaspberryPI. Check the SPI Address. Default is “SPI0 MOSI” Pin (#19).
A simple example on how to use the LEDMatrix-Class from the Hardware-Catalog:
System.out.println("LED matrix app started ...");
int Rows = 3;
int Columns = 4;
double brightness = 0.5;
System.out.println("Initialising the matrix");
LedMatrix ledMatrix = new LedMatrix(pi4j, Rows, Columns, brightness);
System.out.println("Setting all LEDs to Red.");
ledMatrix.setMatrixColor(LedStrip.PixelColor.RED);
ledMatrix.render();
delay(3000);
System.out.println("setting the second strip to green");
ledMatrix.setStripColor(1, LedStrip.PixelColor.GREEN);
ledMatrix.render();
delay(3000);
System.out.println("Setting the third led of the third strip to Yellow");
ledMatrix.setPixelColor(2, 2, LedStrip.PixelColor.YELLOW);
ledMatrix.render();
delay(3000);
ledMatrix.close();
System.out.println("LED matrix app done.");
The class is not yet implemented in a project.