<< Chapter < Page Chapter >> Page >

Draw the rightmost ladybug

After that, Listing 4 calls the setDrawMode method on the incoming Graphics parameter to set the drawing mode to MODE_ALPHA_BLEND and calls the same draw method of the ladybug Image object to draw the rightmost ladybug in Figure 5 .

Note that this drawing of the ladybug image does not honor transparent pixels.

Call the drawImage method of the Graphics class

Finally, Listing 4 calls the setDrawMode method on the incoming Graphics parameter to set the drawing mode back to MODE_NORMAL and then calls the drawImage method on the incoming Graphics parameter to draw the middle ladybug in Figure 5 . (This approach is similar to the approach that would be used to draw an image using the standard edition Java library.)

Note that the reference to the ladybug Image object and the drawing coordinates are passed as parameters to the drawImage method. Some of the overloaded drawImage methods provide scaling. However, there is no scale parameter for this version of the drawImage method so the ladybug was drawn at actual size.

Many overloaded drawing methods

There are many overloaded versions of the draw and the drawImage methods.

That completes the discussion of the program named Slick0140a .

Run the program

I encourage you to copy the code from Listing 5 Compile the code and execute it,making changes, and observing the results of your changes. Make certain that you can explain why your changes behave as they do.

Summary

You learned that while there are many classes, interfaces, and methods in the Slick2D library with names that match classes, interfaces, and methods in thestandard edition Java library, they are not the same.

You learned that you can access the Slick2D documentation at (External Link) . (A copy of the documentation is also included in the distribution zip file.)

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

You learned 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.

What's next?

In the next module, you will learn how to make sprites move at a constant speed in front of an image in the face of a widely varying frame rate. You willalso learn about a rudimentary form of collision detection.

Miscellaneous

This section contains a variety of miscellaneous information.

Housekeeping material
  • Module name: Slick0140: A first look at Slick2D bitmap graphics
  • File: Slick0140.htm
  • Published: 02/04/13
  • Revised: 06/09/15 for 64-bit
Disclaimers:

Financial : Although the Connexions site makes it possible for you to download a PDF file for thismodule at no charge, and also makes it possible for you to purchase a pre-printed version of the PDF file, you should beaware that some of the HTML elements in this module may not translate well into PDF.

I also want you to know that, I receive no financial compensation from the Connexions website even if you purchase the PDF version of the module.

In the past, unknown individuals have copied my modules from cnx.org, converted them to Kindle books, and placed them for sale on Amazon.com showing me as the author. Ineither receive compensation for those sales nor do I know who does receive compensation. If you purchase such a book, please beaware that it is a copy of a module that is freely available on cnx.org and that it was made and published withoutmy prior knowledge.

Affiliation :: I am a professor of Computer Information Technology at Austin Community College in Austin, TX.

Complete program listing

Listing 5 contains a complete listing of the program named Slick0140a .

Listing 5 . Source code for the program named Slick0140a.
/*Slick0140a.java Copyright 2012, R.G.BaldwinIllustrates drawing a sprite image with transparent parts on a background image using two differentapproaches. Tested using JDK 1.7 under WinXP *********************************************************/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;public class Slick0140a extends BasicGame{ Image ladybug = null;Image background = null; float leftX = 100;//leftmost position of ladybugfloat leftY = 100;float middleX = 200;//middle position of ladybug float middleY = 50;float rightX = 300;//rightmost position of ladybugfloat rightY = 100;float leftScale = 0.75f;//drawing scale factorsfloat rightScale = 1.25f; //----------------------------------------------------//public Slick0140a(){//constructor //Set the titlesuper("Slick0140a, baldwin"); }//end constructor//----------------------------------------------------// public static void main(String[]args) throws SlickException{AppGameContainer app = new AppGameContainer( new Slick0140a(),414,307,false);app.start(); }//end main//----------------------------------------------------// @Overridepublic void init(GameContainer gc) throws SlickException {ladybug = new Image("ladybug.png"); background = new Image("background.jpg");gc.setShowFPS(false);//disable FPS display gc.setTargetFrameRate(60);//set frame rate}//end init //----------------------------------------------------//@Override public void update(GameContainer gc, int delta)throws SlickException{ //No updates required in this program.}//end update //----------------------------------------------------//public void render(GameContainer gc, Graphics g) throws SlickException{//Note that the names of the drawMode constants seem // to be backwards.//Draw the background and two versions of the// ladybug by calling a draw method of the Image // class.g.setDrawMode(g.MODE_NORMAL);//honors transparency background.draw(0,0);ladybug.draw(leftX,leftY,leftScale); g.setDrawMode(g.MODE_ALPHA_BLEND);//no transparencyladybug.draw(rightX,rightY,rightScale);//Draw a third version of the ladybug by calling // a drawImage method of the Graphics class.g.setDrawMode(g.MODE_NORMAL); g.drawImage(ladybug,middleX,middleY);}//end render }//end class Slick0140a

-end-

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