I am using the StdDraw library from https://introcs.cs.princeton.edu/java/stdlib/StdDraw.java
. To my knowledge it just implements a simple interface for drawing to the screen using JFrame.
In my implementation, I simply have a 2d array of pixels, and I am drawing each one like so:
StdDraw.clear()
for (int y = 0; y < Engine.HEIGHT; y += 1) {
for (int x = 0; x < Engine.WIDTH; x += 1) {
map[x][y].draw((float) x / Engine.WIDTH, (float) y / Engine.HEIGHT);
}
}
This, however, is extremely slow. Like insanely slow. Not to mention that I have to redraw the entire screen every time I want to update it.
I clear the screen and then redraw every pixel. There is no way this is how it supposed to work, but I just don't really know where to look. Some suggestions on what I should look out for would be really helpful!
This is not the final answer, but a way to put some measurable code on the table.
BufferedImage
is the way to go, but for reference on my not-too-fast system, this code takes 5 seconds to draw 40 million pixels, or 125 milliseconds per megapixel. How may pixels do you have?!? This code uses a call toline
as a proxy for drawing a single pixel because the class doesn't offer single pixel drawing, but theoretically code that draws an actual pixel like you do should be faster.What class is YOUR
map
?How many pixels?
What amount of time is "insanely slow"?