Hi, for my main menu I’m trying to create an animated background similar to the Matrix green code but for binary. I currently draw a bunch of UnicodeFont Strings onto the screen but I recently found out that it’s an expensive operation. Here is a screenshot.
Here is the code I use to draw it. Sorry for the code being so messy, essentially it randomizes every aspect of the binary numbers and draws it, setting the x position back to the start of the screen once it has flown off.
// Number of lines to draw, currently 300 for stress testing.
private int number_Of_Lines = 300;
private StringBuffer background[] = new StringBuffer[number_Of_Lines];
private int xArray[] = new int[number_Of_Lines];
private int xSpeed[] = new int[number_Of_Lines];
private int alpha[] = new int[number_Of_Lines];
private float randomSize[] = new float[number_Of_Lines];
public void init() {
Random dice = new Random();
for (int i = 0; i < xArray.length; i++) {
background[i] = new StringBuffer();
if (i == 0) {
xArray[i] = 800;
} else {
xArray[i] = dice.nextInt(900) + 800;
}
xSpeed[i] = dice.nextInt(20) + 1;
alpha[i] = dice.nextInt(200) + 56;
randomSize[i] = dice.nextFloat() + .3f;
for (int j = 0; j < 120; j++) {
if (dice.nextInt(2) == 0) {
background[i].append('0');
} else {
background[i].append('1');
}
}
}
}
public void render(GameContainer gc, StateBasedGame sb, Graphics g) {
// Using regular g.drawString works much faster
// If I set the font though, it also slows down
// g.setFont(FontManager.smallDefaultFont);
for (int i = 0; i < xArray.length; i++) {
g.scale(randomSize[i], randomSize[i]);
FontManager.smallDefaultFont.drawString(xArray[i], FontManager.smallDefaultFont.getHeight("1") * i, background[i].toString(), new Color(76, 256, 0,
alpha[i]));
// g.drawString( background[i].toString(),xArray[i], FontManager.smallDefaultFont.getHeight("1") * i);
g.resetTransform();
}
}
public void update(GameContainer gc, StateBasedGame sb, int delta) {
for (int i = 0; i < xArray.length; i++) {
if (i == 0) {
xArray[i] -= 5;
} else {
xArray[i] -= xSpeed[i];
}
if (xArray[i] < -xArray.length * 9) {
xArray[i] = 800 + xArray.length * 9;
}
}
}
I’m wondering why using UnicodeFont is so expensive. Should I just use images for the background or should I create a good algorithm?