Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Firstly create a new class that extends the <code>WorldView</code> class. I called my new class <code>Background</code>. So in this new class import all the Java packages you will need in order to override the <code>paintBackground</code> method. This should be:</p> <pre><code>import city.soi.platform.*; import java.awt.Graphics2D; import java.awt.Image; import java.awt.image.ImageObserver; import javax.swing.ImageIcon; import java.awt.geom.AffineTransform; </code></pre> <p>Next after the class name make sure that it says extends <code>WorldView</code>. Something like this:</p> <pre><code>public class Background extends WorldView </code></pre> <p>Then declare the variables game of type <code>Game</code> and an image variable of type <code>Image</code> something like this:</p> <pre><code>private Game game; private Image image; </code></pre> <p>Then in the constructor of this class make sure the game of type <code>Game</code> is in the signature of the constructor and that in the call to <code>super</code> you will have to initialise the <code>WorldView</code>, initialise the game and initialise the image variables, something like this:</p> <pre><code>super(game.getCurrentLevel().getWorld(), game.getWidth(), game.getHeight()); this.game = game; bg = (new ImageIcon("lol.png")).getImage(); </code></pre> <p>Then you just override the <code>paintBackground</code> method in exactly the same way as you did when overriding the <code>paint</code> method in the <code>Player</code> class. Just like this:</p> <pre><code>public void paintBackground(Graphics2D g) { float x = getX(); float y = getY(); AffineTransform transform = AffineTransform.getTranslateInstance(x,y); g.drawImage(bg, transform, game.getView()); } </code></pre> <p>Now finally you have to declare a class level reference to the new class you just made in the <code>Game</code> class and initialise this in the <code>Game</code> constructor, something like this:</p> <pre><code>private Background image; And in the Game constructor: image = new Background(this); </code></pre> <p>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 <code>frame</code> has been declared:</p> <pre><code>frame.add(image); </code></pre> <p>Make sure you add this code just before <code>frame.pack();</code>. Also make sure you use a background image that isn't too big!</p> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload