<< Chapter < Page Chapter >> Page >

What you will learn

In this module, you will learn that while there are many classes, interfaces, and methods in the Slick2D library with names that match classes, interfaces, andmethods in the standard edition Java library, they are not the same.

You will learn how to set the drawing mode so that bitmap images drawn in the game window will either honor or not honor transparent pixels.

You will learn how to draw bitmap images in the game window using both the draw methods of the Image class and the drawImage methods of the Graphics class.

General background information

Bitmaps and shapes

Many game programs communicate primarily with the player using interactive graphics. Sometimes those graphics involve drawing bitmap images. Both theSlick2D Image class and the Slick2D Graphics class provide methods for drawing bitmap images.

Sometimes game programs involve drawing shapes such as circles, rectangles, polygons, arcs, etc. The Slick2D Graphics class and other classes such as the Slick2D Shape class provide methods for creating and drawing such shapes and filling closed shapes with color.

And of course, some game programs involve a combination of the two.

This module concentrates on drawing bitmap images both honoring and not honoring transparent pixels.

Common class names

The Slick2D library contains numerous classes, interfaces, and methods with names that match the names in the Java standard edition library, such as Color , Graphics , Image , Line , Rectangle , Shape , Transform , TextField , etc.

You need to be aware, however, that even though the names are the same, and the behaviors may be similar, these are not standard Java classes. Theirbehaviors will often be different from standard Java classes. Therefore, ready access to the documentation at (External Link) while you are programming in Slick2D isvery important even if you are a seasoned Java programmer.

Illustrate major differences

The program that I will present in this module will illustrate some of the major differences between the two libraries insofar as graphics programming isconcerned. For example, both libraries have a class named Image , which has generally the same purpose in both libraries. However, the Image class in the Slick2D library provides about ten different overloaded draw methods that you can call to draw images in the game window.

The Image class in the Java standard library doesn't have any draw methods. Instead, it is necessary to get a graphics context on theimage and then call the drawImage method on that context to draw the image.

Draw bitmap images two different ways

The Slick2D render method receives an incoming parameter of type Graphics , which also provides a drawImage method that can be used to draw an image in the game window.

I will show you how to draw bitmap images using the draw methods of the Image class and also show you how to draw bitmapimages using the drawImage methods of the Graphics class.

Discussion and sample code

Listing 5 provides a complete listing of a Slick2D program named Slick0140a . Before getting into the details of the code, however, I will show you the input and output images.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Anatomy of a game engine. OpenStax CNX. Feb 07, 2013 Download for free at https://legacy.cnx.org/content/col11489/1.13
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Anatomy of a game engine' conversation and receive update notifications?

Ask