0
How to draw pixels on java awt.
I know you can draw pixels by calling drawLine(px,py,px,py), or by calling drawRectangle(px,py,1,1) on Graphics class. But i mean, is there a better way. Is it too GPU or CPU consuming, or is it just ok to draw each pixel calling a function each time. Example for what i tell is a 3d pixel renderer that has to decide if the pixel is drawn or not telling of if there is a surface that covers it.
1 Resposta
+ 5
The most efficient way to render real-time 3D graphics in Java is to use Java's 3D API.
You'll need a jar file for the API since it isn't in the standard Java packages.
It can be downloaded from:
https://www.oracle.com/java/technologies/javase/java-3d.html
A tutorial is at:
http://www.java3d.org/tutorial.html
That API internally uses OpenGL and the GPU to accelerate the render far better than anything you do with Java in the CPU.
If you want to draw an image pixel-by-pixel in Java using the CPU, you could use a BufferedImage and directly access the pixel data as arrays. Accessing the raw pixel data as an array is many times faster than doing something like a g.fillRect call corresponding with each pixel. When you finish calculating all the pixel values, you could then use g.drawImage to make your buffered image visible on the window or component.
There are several ways to access the raw pixel data of a BufferedImage.
An answer here shows one way:
https://community.oracle.com/thread/1265300
It speeds up if you reuse the same instance of BufferedImage and only resize when necessary.