What are the ways to render a 2d game?

Forgive my ignorance guys, I’m completely lost and afraid! After reading and watching tutorials, I concluded that there are many ways to render and manipulate an image on the screen, some use JPanel and others Canvas. Some use BufferedImage and some use vectors (draw.fillRect), some use paint or paintComponent.

Can you please list down the ways 2d java game programmers do to render images for their games? What are the recommended way by Oracle? What are their pros and cons? There are just so many ways to implement this (rendering an image)! Can someone please enlighten me?

Built-in to Java:
AWT/Canvas, with or without BufferStrategy
Java2D
JavaFX

3rd party low-level rendering libraries:
LWJGL2/3 (OpenGL)
JOGL (OpenGL)

3rd party higher-level rendering libraries/frameworks:
libgdx (built on LWJGL or JOGL)
Slick2D (obsolete, don’t use)
…any others I forget?

Cas :slight_smile:

THANK YOU! I want to create a game using Canvas with BufferStrategy, do you have know any article that explain how to achieve this?

EDIT:

Oh and, from AWT/Canvas with/out BufferStrategy, Java2D, and JavaFX, which one would you prefer to use and why?

In the simplest terms, the way I use BS is:-


public final class GameWindow extends JFrame {

		private BufferStrategy BS;

		public GameWindow() {
				this.createBufferStrategy(2);
				BS = this.getBufferStrategy();
		}

		public void drawStuff() {
				Graphics g = BS.getDrawGraphics();

				[do drawing code]

				BS.show();
		}
}


As for which one you should use, it all depends on what kind of game. IMHO, If you’re writing a simple game to run on as many different computers as possible that doesn’t use any complex graphics, I’d go with BS. Otherwise use LWJGL, or something that sits on top of that like LibGDX.

I use plain LWJGL, but that’s coz I’ve spent years making my own 2D libraries on top of it when there weren’t any decent alternatives :slight_smile:

JavaFX is the new hotness. Pretty much guaranteed performance.

Cas :slight_smile:

[quote=“meSmashsta,post:1,topic:57861”]
That’s the thing about programming: there is never a single best way to do anything. There are always a million ways to do any one thing, and each approach has its own pros and cons. Many of those pros and cons depend on you more than anything. How you think, what you prioritize, what you’ve learned so far, etc.

So asking us to list every single way and all of their pros and cons is a little bit like asking us what you should eat for dinner. We can tell you what we eat for dinner, but that might not be what you should eat. The only way to figure out what is best for you is to try a million different things out and see what you like and don’t like.

I will say that I personally love Processing, especially for novices.

Here’s what I do for AWT: Note that my game runs as an applet, inside a Frame. Also note this is probably not the best practice, but it works.


/**
* SET UP DOUBLE BUFFERING
*/
private Image i;
private Graphics doubleG;
URL url;



/**
* THIS METHOD HANDLES DOUBLE BUFFERING
*/
public void update(Graphics g) {
	if (i == null) {
		i = createImage(this.getSize().width, this.getSize().height);
		doubleG = i.getGraphics();
	}
	doubleG.setColor(GUI.SKYBLUE);
	doubleG.fillRect(0, 0, this.getSize().width, this.getSize().height);
	doubleG.setColor(GUI.SKYBLUE);
	paint(doubleG);
	g.drawImage(i, 0, 0, this);
}

public void run(){
    while(true){
        repaint();
        try {
		Thread.sleep(17);
	} catch (InterruptedException e) {
		e.printStackTrace();
	}
    }
}

public void paint(Graphics g){
    // draw all your graphics here
}