Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Images Failing to Load in Game

  1. #1
    Junior Member
    Join Date
    Dec 2020
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Images Failing to Load in Game

    Hey Guys,

    I'm new to programming in Java, and this is my first foray into loading images into a simple program. I have the game working, with exception of the images loading in game properly. Would you look it over and give me some feedback on what I may be missing? Thanks!

    Cheers!
    Last edited by Baiano42; December 6th, 2020 at 06:57 PM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Images Failing to Load in Game

    Please post here any code that shows the problem. Be sure to wrap the code in code tags.
    Where are the image files located?
    Also post the contents of the command prompt window where the java command is being executed so we can see the directory.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2020
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Images Failing to Load in Game

    I am using a MVC design pattern, and the images are located in the model.images subfolder.

    [code]
    public class GameBoard extends JPanel {
    ...
    private ImageIcon background = new ImageIcon("images/backgroundSkin.gif");

    @Override
    public void paint(Graphics g) {

    background.paintIcon(null, g, 0, -150);

    }
    }
    [code]

    Here is the content of the command prompt:
    PS C:\Users\baian\Desktop\Project2\Project2> cd 'c:\Users\baian\Desktop\Project2\Project2'; & 'c:\Users\baian\.vscode\extensions\vscjava.vscode-java-debug-0.29.0\scripts\launcher.bat' 'C:\jdk-14.0.2\bin\java.exe' '-XX:+ShowCodeDetailsInExceptionMessages' '-Dfile.encoding=UTF-8' '-cp' 'C:\Users\baian\AppData\Roaming\Code\User\workspac eStorage\fed9f8b7cc70fe61f5e2d704b371b559\redhat.j ava\jdt_ws\Project2_67b58bc5\bin' 'view.GameFrame'

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Images Failing to Load in Game

    I can not make any sense of the command prompt window that was posted.
    What directory is the current directory for the program when it is executed?
    Is the images folder in that folder?

    images are located in the model.images subfolder.
    According to that, the java command should be executed in the model folder (it contains the images folder)
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Dec 2020
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Images Failing to Load in Game

    I have the folder with all the source files located on my desktop.
    The file initiating the program is the GameFrame.java in the view folder, while the folder containing all the images are located in the model.images folder.
    When running the program, I am using the "Run" command from the main function in the GameFrame.java file, located in the view folder.

    --- Update ---

    I got it! I needed to add the model/images/*.gif... Man do I feel dumb... Thanks Norm!
    Last edited by Baiano42; December 6th, 2020 at 06:44 PM.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Images Failing to Load in Game

    folder containing all the images are located in the model.images folder.
    Is the java command issued in the model directory? The code looks to find the images in the the image folder.
    Can you post the layout for the folders: view and model?

    If the class is in the package: view, then the classpath needs to point to the folder containing the view folder.

    If the image files are in the images folder, and the images folder is in the model folder, the current directory when the java command is issued should be the model folder.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Load objects of a sample game from a LVL file
    By Cha in forum What's Wrong With My Code?
    Replies: 0
    Last Post: October 9th, 2018, 01:15 AM
  2. Help! Error with a save and load feature of a java chess game
    By davidchan in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 22nd, 2013, 08:27 AM
  3. Replies: 2
    Last Post: September 27th, 2012, 07:06 PM
  4. Simple game that requires me to load game settings from a file
    By 14fenix in forum Java Theory & Questions
    Replies: 5
    Last Post: December 1st, 2011, 09:21 PM
  5. How to add card images to Blackjack game??
    By Jayc44 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 15th, 2011, 12:43 PM