-2

Okay, I'm just a greenhorn here and I'm trying some various codes. Now this game GUI src I found have some image files inside a folder and its a necessity for the whole game to work. I tried some methods, but I just can't understand how can I make the src connected to the folder. The program runs now but it only displays black screen because it can't connect to the images. Please, I need help.

What I just wanted is how can I make the program recognize the files I'm using as background images and such. The code line is there, but it displays an exception... Am I still unclear? ._.

Well it goes like this:

package moon_lander;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;

/**
 * Actual game.
 *
 * @author www.gametutorial.net
 */

public class Game {

    /**
     * The space rocket with which player will have to land.
     */
    private PlayerRocket playerRocket;

    /**
     * Landing area on which rocket will have to land.
     */
    private LandingArea landingArea;

    /**
     * Game background image.
     */
    private BufferedImage backgroundImg;

    /**
     * Red border of the frame. It is used when player crash the rocket.
     */
    private BufferedImage redBorderImg;


    public Game()
    {
        Framework.gameState = Framework.GameState.GAME_CONTENT_LOADING;

        Thread threadForInitGame = new Thread() {
            @Override
            public void run(){
                // Sets variables and objects for the game.
                Initialize();
                // Load game files (images, sounds, ...)
                LoadContent();

                Framework.gameState = Framework.GameState.PLAYING;
            }
        };
        threadForInitGame.start();
    }


   /**
     * Set variables and objects for the game.
     */
    private void Initialize()
    {
        playerRocket = new PlayerRocket();
        landingArea  = new LandingArea();
    }

    /**
     * Load game files - images, sounds, ...
     */
    private void LoadContent()
    {
        try
        {
            URL backgroundImgUrl = this.getClass().getResource("/moon_lander/resources/images/background.jpg");
            backgroundImg = ImageIO.read(backgroundImgUrl);

            URL redBorderImgUrl = this.getClass().getResource("/moon_lander/resources/images/red_border.png");
            redBorderImg = ImageIO.read(redBorderImgUrl);
        }
        catch (IOException ex) {
            Logger.getLogger(Game.class.getName()).log(Level.SEVERE, null, ex);
        }
    }


    /**
     * Restart game - reset some variables.
     */
    public void RestartGame()
    {
        playerRocket.ResetPlayer();
    }


    /**
     * Update game logic.
     *
     * @param gameTime gameTime of the game.
     * @param mousePosition current mouse position.
     */
    public void UpdateGame(long gameTime, Point mousePosition)
    {
        // Move the rocket
        playerRocket.Update();

        // Checks where the player rocket is. Is it still in the space or is it landed or crashed?
        // First we check bottom y coordinate of the rocket if is it near the landing area.
        if(playerRocket.y + playerRocket.rocketImgHeight - 10 > landingArea.y)
        {
            // Here we check if the rocket is over landing area.
            if((playerRocket.x > landingArea.x) && (playerRocket.x < landingArea.x + landingArea.landingAreaImgWidth - playerRocket.rocketImgWidth))
            {
                // Here we check if the rocket speed isn't too high.
                if(playerRocket.speedY <= playerRocket.topLandingSpeed)
                    playerRocket.landed = true;
                else
                    playerRocket.crashed = true;
            }
            else
                playerRocket.crashed = true;

            Framework.gameState = Framework.GameState.GAMEOVER;
        }
    }

    /**
     * Draw the game to the screen.
     *
     * @param g2d Graphics2D
     * @param mousePosition current mouse position.
     */
    public void Draw(Graphics2D g2d, Point mousePosition)
    {
        g2d.drawImage(backgroundImg, 0, 0, Framework.frameWidth, Framework.frameHeight, null);

        landingArea.Draw(g2d);

        playerRocket.Draw(g2d);
    }


    /**
     * Draw the game over screen.
     *
     * @param g2d Graphics2D
     * @param mousePosition Current mouse position.
     * @param gameTime Game time in nanoseconds.
     */
    public void DrawGameOver(Graphics2D g2d, Point mousePosition, long gameTime)
    {
        Draw(g2d, mousePosition);

        g2d.drawString("Press space or enter to restart.", Framework.frameWidth / 2 - 100, Framework.frameHeight / 3 + 70);

        if(playerRocket.landed)
        {
            g2d.drawString("You have successfully landed!", Framework.frameWidth / 2 - 100, Framework.frameHeight / 3);
            g2d.drawString("You have landed in " + gameTime / Framework.secInNanosec + " seconds.", Framework.frameWidth / 2 - 100, Framework.frameHeight / 3 + 20);
        }
        else
        {
            g2d.setColor(Color.red);
            g2d.drawString("You have crashed the rocket!", Framework.frameWidth / 2 - 95, Framework.frameHeight / 3);
            g2d.drawImage(redBorderImg, 0, 0, Framework.frameWidth, Framework.frameHeight, null);
        }
    }
}

And this is the Exception:

Exception in thread "Thread-2" java.lang.IllegalArgumentException: input == null!
    at javax.imageio.ImageIO.read(ImageIO.java:1362)
    at moon_lander.Framework.LoadContent(Framework.java:115)
    at moon_lander.Framework.GameLoop(Framework.java:162)
    at moon_lander.Framework.access$000(Framework.java:21)
    at moon_lander.Framework$1.run(Framework.java:90)

Process completed.
Louie
  • 13
  • 1
  • 4
  • 3
    If you don't tell us what language/framework/etc you use, it's unlikely we can provide any help. – Felix Kling Sep 22 '13 at 09:05
  • oh sorry. it's Java. I forgot to add the tag. xD – Louie Sep 22 '13 at 09:10
  • aaaaand I got a -1. thanks for that. ._. – Louie Sep 22 '13 at 09:21
  • If you say what kind of stuff you have tried... we can nudge you in the right direction. – d'alar'cop Sep 22 '13 at 09:50
  • 1
    "Java" does not tell us enough -- you can write UIs in Java using various libraries (Swing, SWT - JSP? JavaFX?). And once you've said what libraries you are using, you should post code and tell us what you have tried and what about it doesn't work. Otherwise we're just guessing about what problem you're trying to solve, and that's too often a waste of time. – arcy Sep 22 '13 at 10:25
  • @Louie : Please have a look at, how to [add Images to the Project](http://stackoverflow.com/a/9866659/1057230). Hopefully this thread be able to help you in the direction :-) – nIcE cOw Sep 22 '13 at 10:35
  • Its already in the code. ... Sorry for being so noob here. >.> – Louie Sep 22 '13 at 12:24
  • I'm just studying it for a purpose. I just can't run it properly. It only displays black screen. – Louie Sep 22 '13 at 12:26
  • :okay: You see guys, it runs perfectly without errors. After pressing "run", it displays only a black screen. I have a resource folder for those images included in the folder in which where the source code located is. I really don't get this part why it displays a black screen instead of the picture inside the resource folder. Whereas, it should be displaying the picture I put inside the resource folder... ._. – Louie Sep 22 '13 at 12:35
  • @Louie: You have stated in the edit that it displays an exception. What is the exception? If you add the exception details to the question, there is a better chance of getting a solution. – Harry Sep 22 '13 at 12:56
  • there. edited with the exception. o.o I tried to research, but nothing works well. It still displays the black screen. – Louie Sep 22 '13 at 21:01
  • @Louie : Please upload the image on the web and provide a link in the question. We'll try to answer that :-) – nIcE cOw Sep 23 '13 at 05:58
  • @Louie : Moreover, I really don't know, if you had read the link I had provided previously. If you had created a source Folder called __resources__ and added __images__ folder to it, then you will access the images by using `Game.class.getResource("/images/background.jpg")`, the last link in the link provided explains that thingy too. – nIcE cOw Sep 23 '13 at 07:05

1 Answers1

2

How you load imagines doesn't have anything to do with packages.

Normally you find an image as a resource via class path. This can be arranged any way you wish.

I tried some methods, but I just can't understand how can I make the src connected to the folder.

Usually you build an application. When you run it, you use the build, not the src. i.e. you don't use the source when you run the program. Usually the images are copied with the same relative path you used in your source and this relative path is what you use to find and load your images.

I can't be more specific, as there is not enough detail in the questions such as what you directory structure is and which IDE or build system you are using.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • err, you see.. the whole thing is it access a file image by this code: URL backgroundImgUrl = this.getClass().getResource("/moon_lander/resources/images/background.jpg"); backgroundImg = ImageIO.read(backgroundImgUrl); and that image is located inside a folder. What I was asking is how can make the program read that file. Sorry if I was rude. >.> – Louie Sep 22 '13 at 12:20
  • I would use `InputStream is = getClass().getClassLoader("/moon_lander/resources/images/background.jpg");` and you can get the actual contents from the InputStream. – Peter Lawrey Sep 22 '13 at 20:08
  • but sir, where should put the picture files then? o.o – Louie Sep 23 '13 at 02:01
  • @Louie I usually put them in the `resources` directory for a `maven` build. It doesn't matter where you put them as long as they end up in your class path e.g. the JAR you deploy. – Peter Lawrey Sep 23 '13 at 19:06