I am working on a GameBoy emulator and using Java2D for drawing. However my current method of drawing is causing a lot of flickering on my Linux machine. This flickering is not present on my friend's Mac.
I should explain graphics on the GameBoy a bit first. The GameBoy does not maintain an array of pixel data to be drawn, but instead has a list of sprite and tile data that is rendered. There is also a byte in memory that contains the current x coordinate of the vertical line that is being rendered.
Because I need to monitor the current x position of what is being drawn, I figured that the best way to draw everything was to loop through all the tiles and sprites, rendering them to a BufferedImage, and then go through this BufferedImage and plot it pixel by pixel while updating the spot in memory with the current x coordinate.
Here is my code:
@Override
public void paint(Graphics graphics) {
super.paint(graphics);
Graphics2D g = (Graphics2D) graphics;
BufferedImage secondBuffer = new BufferedImage(160, 144, BufferedImage.TYPE_INT_RGB);
Graphics2D bg = secondBuffer.createGraphics();
display.draw(ram, buffer.createGraphics());
for (int y = 0; y < 144; y++) {
for (int x = 0; x < 160; x++) {
bg.setPaint(new Color(buffer.getRGB(x, y)));
bg.drawLine(x, y, x, y);
ram.getMemory().put(Ram.LY, (byte) x);
}
}
bg.dispose();
g.drawImage(secondBuffer, null, 0, 0);
cpu.setInterrupt(Cpu.VBLANK);
}
I am not an expert with Java2D, so there could be something going on under the hood that I am not familiar with. I cannot figure out why this is code is causing flickering.
EDIT: It may or may not be relevant that I am using OpenJDK. I have heard that it has some graphical issues, but I do not know if that is the cause.