Similar to 3D game engines, GLib handles most rendering for you. However, they may be a good reason to override the default drawing behaviour, especially considering GLib’s infancy. For example, there is virtually no support for scrolling levels.

It is easiest to override sprite drawing, and we handle that first.

If you’ve seen the code for GLib, you would have noticed that the Sprite class has two methods for drawing: render and draw. Render draws the current image, if any, and then calls draw. The draw method is blank, and acts as the hook for overriding. Simply override the draw method for any custom code. Draw takes a single parameter, a Graphics object you can safely downcast to a Graphics2D object.

We now look at the steps in the drawing process and discuss the methods of overriding them.

  1. First, a background color (A java.awt.Color object) is drawn across the screen. This step can only be skipped, call the setBackgroundColor method of the Screen object with parameter null.
  2. A background image (A java.awt.image.BufferedImage object) is drawn. This also can only be skipped, by calling setBackground(null) on the Screen object.
  3. The world is then drawn, using the world object. This can also be only skipped, by calling super.world=null; in the subclass, or setWorld(null) on the object.
  4. The game object’s preDraw() method is called. This method accepts a Graphics object, and can be used for all custom drawing.
  5. All sprites are drawn. This step can only be skipped, but each sprite can have custom rendering as discussed earlier.