I am developing a simple platform game using Java using BlueJ as the IDE. Right now I have player/enemy sprites, platforms and other items in the game drawn using polygons and simple shapes. Eventually I hope to replace them with actual images.
For now I would like to know what is the simplest solution to setting an image (either URL or from local source) as the 'background' of my game window/canvas?
I would appreciate it if it isn't something long or complex as my programming skills aren't very good and I want to keep my program as simple as possible. Kindly provide example codes with comments to elaborate on their function, and also if it's in its own class, how to call on relevant methods used by it on other classes.
Thank you very much.
Firstly create a new class that extends the
WorldView
class. I called my new classBackground
. So in this new class import all the Java packages you will need in order to override thepaintBackground
method. This should be:Next after the class name make sure that it says extends
WorldView
. Something like this:Then declare the variables game of type
Game
and an image variable of typeImage
something like this:Then in the constructor of this class make sure the game of type
Game
is in the signature of the constructor and that in the call tosuper
you will have to initialise theWorldView
, initialise the game and initialise the image variables, something like this:Then you just override the
paintBackground
method in exactly the same way as you did when overriding thepaint
method in thePlayer
class. Just like this:Now finally you have to declare a class level reference to the new class you just made in the
Game
class and initialise this in theGame
constructor, something like this:Lastly all you have to do is add the background to the frame! That's the thing I'm sure we were all missing. To do that you have to do something like this after the variable
frame
has been declared:Make sure you add this code just before
frame.pack();
. Also make sure you use a background image that isn't too big!Now that's it! Ive noticed that the game engines can handle JPEG and PNG image formats but could also support others. Even though this helps include a background image in your game, it is not perfect! Because once you go to the next level all your platforms and sprites are invisible and all you can see is your background image and any JLabels/Jbuttons you have included in the game.
The Path is the only thing you really have to worry about if you are really new to Java. You need to drag your image into the main project file, and it will show up at the very bottom of the list.
Then the file path is pretty straight forward. This code goes into the constructor for the class.
CS2 is the name of my project, and everything before that is leading to the workspace.
The answer will vary slightly depending on whether the application or applet is using AWT or Swing.
(Basically, classes that start with
J
such asJApplet
andJFrame
are Swing, andApplet
andFrame
are AWT.)In either case, the basic steps would be:
Image
object.Component
you want to draw the background in.Step 1. Loading the image can be either by using the
Toolkit
class or by theImageIO
class.The
Toolkit.createImage
method can be used to load anImage
from a location specified in aString
:Similarly,
ImageIO
can be used:Step 2. The painting method for the
Component
that should get the background will need to be overridden and paint theImage
onto the component.For AWT, the method to override is the
paint
method, and use thedrawImage
method of theGraphics
object that is handed into thepaint
method:For Swing, the method to override is the
paintComponent
method of theJComponent
, and draw theImage
as with what was done in AWT.Simple Component Example
Here's a
Panel
which loads an image file when instantiated, and draws that image on itself:For more information on painting:
Or try this ;)