<< Chapter < Page Chapter >> Page >

Extract and draw another rectangular section with color filtering

The third call to the draw method in Listing 6 extracts a rectangular section from the original image and draws it with a different size and also applies a color filter. I will leave it as anexercise for the student to go to the Slick2D documentation for an explanation of the eight parameters of type float .

Draw image based on its center

The previous examples have drawn the image in a location based on its upper-left corner. The statement in Listing 7 draws the image on the center right in Figure 1 . However, instead of positioning the image based on its upper-left corner, the image is drawn withits center located at 399,266.

Listing 7 . Draw image based on its center.
rabbit.drawCentered(399f,266f);

Draw a flipped copy

The code in Listing 8 makes a call to the getFlippedCopy method of the Image class, followed by a call to the draw method to draw the image in the bottom-right of Figure 1 . Note that the rabbit is facing the opposite direction in that image. The boolean parameters specify whether the image is to be flipped on the horizontal,vertical, or both axes. In this case, a value of true caused the image to be flipped on the horizontal axis only.

Listing 8 . Draw a flipped copy.
Image tempImage = rabbit.getFlippedCopy(true,false); tempImage.draw(266f,399f);}//end render }//end class Slick0160a

Listing 8 also signals the end of the render method and the end of the class named Slick0160a .

The Slick2D Image class provides many additional capabilities that are not illustrated in this program. I will leave it as anexercise for the student to explore them. However, there is one other set of three overloaded methods named drawFlash that I will illustrate in this module. That is the topic of the next section.

The program named Slick0160b

Beginning of the class named Slick0160b

A complete listing of this program is provided in Listing 14 . As before, I will break this program down and discuss it in fragments.

Listing 9 shows the beginning of the class named Slick0160b down through the init method.

Listing 9 . Listing 9, Beginning of the Slick0160b class.
import org.newdawn.slick.AppGameContainer; import org.newdawn.slick.BasicGame;import org.newdawn.slick.GameContainer; import org.newdawn.slick.Graphics;import org.newdawn.slick.Image; import org.newdawn.slick.SlickException;import org.newdawn.slick.Color; public class Slick0160b extends BasicGame{Image spider = null;float spiderWidth;float spiderHeight;Color silohetteColor = Color.white; long timeAccumulator = 0;long flashInterval = 128; //Target frame rateint targetFPS = 60; //----------------------------------------------------//public Slick0160b(){//constructor //Set the titlesuper("Slick0160b, baldwin"); }//end constructor//----------------------------------------------------// public static void main(String[]args) throws SlickException{AppGameContainer app = new AppGameContainer( new Slick0160b(),384,240,false);app.start(); }//end main//----------------------------------------------------// @Overridepublic void init(GameContainer gc) throws SlickException {spider = new Image("spider.png"); spiderWidth = spider.getWidth();spiderHeight = spider.getHeight();System.out.println("spiderWidth: " + spiderWidth); System.out.println("spiderHeight: " + spiderHeight);gc.setShowFPS(false) ; gc.setTargetFrameRate(targetFPS);//set frame rate}//end init

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