Loading images?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 8 of 8

Thread: Loading images?

  1. #1
    Join Date
    Jun 2003
    Posts
    3

    Loading images?

    I am trying to make a little video game, but am still at the very beggining. The problem i have is with loading the images. I try it with:
    Image robinson;
    public void init () {
    robinson = getImage(getCodeBase(), "picture.jpg");
    }
    public void paint(Graphics g){
    g.drawImage(robinson, 10, 10, this);
    }

    and it doesn't draw anything. I have no errors or warnings. When i try to get the width of the image robinson, it comes out to be -1. Can anyone give me some ideas? Please?

    I am also not putting it on the web right away. The picture files are on my C drive. So i am just using the applet viewer to see what i am doing.

    hvala

    p.s. could it be the security issue in java? I am making it as an applet. Any insights?

  2. #2
    Join Date
    Nov 2002
    Posts
    18
    Hi there,
    I've got the same problem (see my post) Do you use Windows? Because drawImage also doesn't work with my Windows XP, but on my Linux box is does works, so maybe it's a Windows problem.

    -Wesley
    I like Java!
    Java is cool!

  3. #3
    Join Date
    Jun 2003
    Posts
    3

    Loading Images in XP

    Well i am glad that i am not the only one with this problem. I am using Windows XP and also am a rookie at Java. It probably is a problem with the Windows XP. Maybe there is a patch for Java or XP or something like that. I'm gonna have to search around the web. In the meantime if anybody has any suggestions or similar problems please do let me know. Thanks for a reply...

    p.s. i'm croatian and your english is just fine

  4. #4
    Join Date
    Mar 2003
    Posts
    834
    Have you guys tried an example which just uses g.drawString(...)? Are you sure you have the Java Runtime Environment installed? If not, get it from http://java.sun.com
    ArchAngel.
    O:-)

  5. #5
    Join Date
    Nov 2002
    Posts
    18
    Yes, all the other functions do work, I think it might has something to do with NTSF, do you guys all use NTSF? Else it's Windows Xp like said before.
    I like Java!
    Java is cool!

  6. #6
    Join Date
    Mar 2003
    Posts
    834
    Do you mean NTFS (NT File System)?

    I don't think it is XP. I use XP at home and it works fine. I'm on a 2000 machine at work. I'll check when I get home.
    ArchAngel.
    O:-)

  7. #7
    Join Date
    Nov 2002
    Posts
    18
    Ok, I've done some research, it isn't beacuse of the NTSF file system, I converted to FAT and still doesn't work.

    But I did found WHY it won't load an Image. It is because getDocumentBase() returns the name of the used HTML file. Example, you place your applet in a HTML file with the applet tags ("<applet></applet>") called test.html, the getDocumentBase() return "c:\java\test.html" (example) instead of "c:\java\". The same thing happens with getCodeBase().

    I'm going to try to find a solution, I guess you have to make an URL object yourself (the getDocumentBase() and getCodetBase() return an URL)

    Hope it helps
    -Wesley

    PS this is how I got to know it:

    PHP Code:
    import java.awt.*;
    import java.applet.*;
    import java.awt.Image;

    public class 
    test extends Applet 
      
    {
      private 
    Image image;
      
    int imgwidth;

      public 
    void init()
        {
        
    image=getImage(getCodeBase(),"lol.gif");
        }
      public 
    void paint(Graphics g)
        {
        
    imgwidth=image.getWidth(this);
        
    System.out.print("***Start Test.java***\nImage Path: (Document)' "getDocumentBase() +"'\n");

        
    System.out.print("Image Path: (Code) '"getCodeBase() +"'\n");

        
    g.drawImage(image,10,10,this);
        
    g.drawString("Width: "+imgwidth,100,100);
        }
      } 
    I like Java!
    Java is cool!

  8. #8
    Join Date
    Jun 2003
    Posts
    3

    Loading images

    This is what i have for now yet it tells me i have a security access exception. It seems as if it were reading the picture and that it would work if i could get around the security check?! What do you guys think?

    here it is:

    "java.security.AccessControlException: access denied (java.io.FilePermission \C:\JavaPrograms\Paint\a.gif read)"

    and here is the code:

    public class Test extends java.applet.Applet {

    private Image robinson;
    URL url;

    public void init () {
    try {
    url = new URL("file:///C:/JavaPrograms/Paint/a.gif");
    throw new MalformedURLException();
    }
    catch(MalformedURLException m){System.out.println("YOu suck!");}
    robinson = getImage(url);
    }

    public void paint(Graphics g){
    g.drawImage(robinson,10,10,this);
    }

    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center