This discussion is archived
4 Replies Latest reply: Feb 28, 2013 9:56 PM by UdayaShankaraGandhi RSS

How do i import images into Eclipse on a mac?

992767 Newbie
Currently Being Moderated
I am working on a game right now, but I am using a Mac. I have searched everywhere for how to import an image, but all of the tutorials are for Windows. Can anyone help me, or tell me what I'm doing wrong? Here is my code:

Here is my main class: http://pastebin.com/GUkqgLiJ

Here is my character class: http://pastebin.com/WpbUThaZ

Thanks!
  • 1. Re: How do i import images into Eclipse on a mac?
    UdayaShankaraGandhi Newbie
    Currently Being Moderated
    I guess your doubt is reading the image from a given file path and you can use that image in your application development.

    If that is the case, you can use the below method to import the image
    public Image importImage(File filePath){
            Image img = null;
            try
            {
                img = ImageIO.read(filePath);
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return img;
        }
    Use this method and get the Image object and draw it on your respective canvas (a JFrame).
  • 2. Re: How do i import images into Eclipse on a mac?
    gimbal2 Guru
    Currently Being Moderated
    Post code here, not on some external site. Otherwise you instantly exclude people who are behind corporate firewalls, which are plenty.

    Use \
     tags. And don't post everything, just post what is relevant. The "problem" isn't really clear to me anyway - the code is exactly the same on any platform, only the path you should use is different because Windows and MacOS have different filesystems. If you have hardcoded paths in your application code you're doing it wrong.
    
    An alternative to loading images from the filesystem is to load images from the application's jar / classpath. A google for "java jar resource loading" should give plenty of examples on how to do that, I'm out of time myself. The benefit of this approach is that you don't have filesystem mess in your application, it functions the same wherever you run it.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
  • 3. Re: How do i import images into Eclipse on a mac?
    992767 Newbie
    Currently Being Moderated
    Sorry :S.

    Do you know what those file paths might be for Mac? I just need an example and then I can figure it out.

    Edited by: 989764 on Feb 28, 2013 3:43 PM
  • 4. Re: How do i import images into Eclipse on a mac?
    UdayaShankaraGandhi Newbie
    Currently Being Moderated
    A path string is used to locate a file or a directory. Unfortunately, path strings are system dependent, e.g., "c:\myproject\java\Hello.java" in Windows or "/myproject/java/Hello.java" in Unix/Mac.

    Windows use back-slash '\' as the directory separator; while Unixes/Mac use forward-slash '/'.

    Assume that you have stored my image in the path /home/myproject/java/Hello.java in my Mac, then i can call like
    importImage("/home/myproject/java/Hello.java");

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points