I'm getting into game development in java and I've watching videos about it and so far I've seen two ways of rendering:
1-Create an buffered image, change the pixels individualy each frame and then draw the image (eg to draw a rectangle by changing the pixels of the image to a color)
2-Instead of changing the pixels just draw stuff over other stuff with the graphics methods, and cleaning the canvas each frame (eg to draw a rectangle just use graphics.drawRect(...)
So, what is the best way and what are the pros and cons of each method? Because the 2nd method seems way easier.
Software rendering is still an option.
If you use Frame implementing Runnable it is faster to declare paint() and repaint() as {} and do the drawing in a loop inside run().
for example...
private static MemoryImageSource imgSrc;
private static DirectColorModel imgCm = new DirectColorModel(32,0xff0000,0xff00,0xff);
private static Image img;
private static int xsize = 256;
private static int ysize = 256;
private static int pixels[];
private static int appLeft = 0;
private static int appTop = 0;
private static void init() { // initialize
imgSrc = new MemoryImageSource(xsize, ysize, pixels, 0, xsize);
img = createImage(imgSrc);
imgSrc.setAnimated(true);
imgSrc.newPixels(pixels, imgCm, 0, xsize);
appLeft = getInsets().left;
appTop = getInsets().top;
}
public void run() {
Graphics g;
for(;;) {
// .... render something here
// then...
g = getGraphics();
imgSrc.newPixels(); // NOTE: renders array to image
g.drawImage(scr, appLeft, appTop, this);
g.dispose();
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.