Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8026189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T23:24:32+00:00 2026-06-04T23:24:32+00:00

I am learning to create game in Java and requires loading several images in

  • 0

I am learning to create game in Java and requires loading several images in before animating them. This is what I am doing now:

bg = new ImageIcon(System.getProperty("user.dir")+"/src/img/bg.jpg").getImage();

But clearly it has many disadvantages. For a start, it is long and ugly looking and I suspect, slow. Additionally, the forward slashes “/” would only work on Mac or Linux, where as on windows, it would use back slashes.

EDIT: As corrected by Samuel Rossille below, forward slash “/” in fact do work on windows.

I was wondering are there anyway of loading images, that are in the application directory, and won’t be changed, in a more elegant fashion, as well as being cross-platform?

What about loading text files? Are they any different?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T23:24:34+00:00Added an answer on June 4, 2026 at 11:24 pm

    See the info. on embedded resources. Note that most methods & constructors that accept a File will also accept an InputStream or URL. Embedded resources are read only.


    On the matter of files (in the rare instance you might be using them and have to form the path from a String).

    String fileSep = System.petProperty("file.separator");
    bg = new ImageIcon(System.getProperty("user.dir")+
        fileSep+
        "src"+
        fileSep+
        "img"+
        fileSep+
        "bg.jpg").getImage();
    

    This is one way to get a directory path that uses the correct separator for the file-system. The File class also offers handy constructors like new File(parent,child) that will insert the correct separator.

    As an aside, I strongly feel that all the methods & constructors in the J2SE that accept a String intended to represent a File path should be deprecated. If it needs a File, give it a File and be done with it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How does Udacity.com (new learning website) create presentation where the text is in front
Just wondering if anyone has some good resources for learning how to create new
Hi I have been learning Java Swing for creating a chess game to practice
I'm new to js and game development but i want to learn... For learning
Currently I am learning how to create shaders in GLSL for a game engine
Im just new to Java and I found this good tutorial for creating a
Im learning Java and Im creating a memory type game where you have to
So I'm just messing around learning to create a Space invaders type game. I
I start learning Silverlight and would like to create some simple game. I am
I have just began learning Java programming, I have created a simple game using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.