Drawing Graphics

Hello JGO,

I am developing a library that is easy for beginners/children to learn. Now that I’ve got the basics (Frames and sound) working, I’m to the point of adding graphics. I cannot figure out how to do that. They would be real simple for now, just squares and rectangles. How would you accomplish such a thing?

Thanks in advance,

-DarkCart

Are you using the basic java graphics libraries? java.awt, JFrames and the like?

I assume you are (as no other library was mentioned).

This is how I used to do it:
Create a class called DrawGame or something. Make this extend Canvas (I can’t remember which package this belongs to, but you could google it, or, if using eclipse, use ctrl+shift+O to automatically import it).

The DrawGame class will be where you do all of your drawing.
To prevent tearing, you need to double or triple buffer your graphics. To do this, add a member variable to DrawGame of type ‘BufferStrategy’.

In DrawGame’s constructor, call ‘createBufferStrategy(2);’ to create a buffer strategy for the canvas.
next, assign the BufferStrategy member variable to ‘getBufferStrategy()’

When you want to draw anything, simply call ‘getDrawGraphics()’ on the BufferStrategy variable, and store this value in a Graphics object.

After you finish drawing anything, call show() on the strategy and dispose() on the graphics object.

After creating your JFrame, add an instance of DrawGame to it.

To further illustrate this, here is an example class which will draw a square -

http://pastebin.java-gaming.org/e3dee740a0a13

It’s pretty dirty code though.
Have fun:)

Don’t you have a couple of games already made…? How are you making them without knowing how to draw basic graphics?

In a library, there is no main method. Drawing graphics with no main method is new to me.

How does drawing graphics have anything do to with the main method?

I think that you are confused about the architecture of a game library. Check out how my game “engine”, Cobalt, is setup. In the example, you can see how you would use it.

The user would get provided with methods like render and update. In the CobaltApp class, you control how those methods are called, and you do a lot of stuff behind the scenes that the user doesn’t know about. For example, maintaining the SpriteBatch. The CobaltApp passes this to the user all setup and ready to draw. The user can draw stuff through the SpriteBatch with the help of some drawing methods (provided by the SpriteBatch).

I would also recommend looking through the source of other engines/libraries, to see how they setup their architecture. Specifically, MERCury and LibGDX (There are many more. Just look around on the forums.)

How do you plan on creating a library for beginners and children if you yourself are a beginner? And, your games all use the paint() method to render not just shapes, but images, so…:persecutioncomplex: