Results 1 to 11 of 11

Thread: JCreator image

  1. #1
    Tb0h is offline Member
    Join Date
    Jul 2009
    Posts
    21
    Rep Power
    0

    Question JCreator image

    Hello,
    I have started using Java very recently, and I want to display an image in an applet. I'm pretty sure theres nothing wrong with the coding, but when I run the applet the applet window appears blank. I am using JCreator, and I think that the image isn't showing because it can't find the image. (The image is a 32 by 32 GIF) I have heard that you need to place the needed images in a certain folder or you have to include the image path, or something. And I am also wondering can you display an image in an applet before JARing it?
    Any code snippets, full codes or instructions on where to place the image to use it in an applet are appreciated.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    have you tried converting the GIF to a JPG?

  3. #3
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    I don't know about JCreator but I might be able to help you show an image in an applet.
    Since the applet security sandbox doesn't allow us to make a new File or URL it is usually easier to use the Class method getResource. This method delegates to the class loader which will look for the resource, your image, on the classpath. That's the first thing to be aware of: the resource must be on your classpath to be found. Also, if there are multiple resources only the first one found will be returned. This method will look for resources in jar files.
    To help with debugging along the way you can write out the returned URL to see where java thinks the file is.
    Another possibility is to load the image in another class (ie, outside the applet).
    Generally you can put your images in a folder named "images" and put this folder in the folder you do your developing in. So your current directory, the one you are working in will have a folder named "images" and it will contain your image(s).
    Then you fetch the image with:
    Java Code:
    String path = "images/myImage.ext";
    URL url = getClass().getResource(path);
    System.out.println("url = " + url);
    You can hand this url to any of:
    1  the newest way to load images: ImageIO.[i]read[/i],
    2  the older ImageIcon constructor or
    3  any of the antique Applet/Toolkit.[i]getImage[/i] ot Toolkit.[i]createImage[/i] methods that have a URL parameter.
    The extension, "ext" above, depends on what methods and/or java version you are using.
    ImageIcon and the antique methods(3) will load gif, jpg and png image formats.
    ImageIO will load all of these plus, in j2se 1.5+, bmp and wbmp images.
    For feedback about loading errors:
    1 ImageIO throws (IllegalArgument) exceptions
    2 ImageIcon has a getImageLoadStatus method you can use
    3 the antique methods require a MediaTracker to actually load the image data and this class has methods you can use to track/query the loading success/status. Without this extra work you get no feedback about what went wrong: couldn't find the file, corrupted/unreadable image data, unrecognizable/unsupported format, etc.

    can you display an image in an applet before JARing it
    Yes, as above.

  4. #4
    Tb0h is offline Member
    Join Date
    Jul 2009
    Posts
    21
    Rep Power
    0

    Default

    Would this code be able to fetch the image if I did as you suggested with the "Images" Folder?
    Java Code:
    import java.applet.Applet;
    import java.awt.*;
    import java.awt.image.*;
    import java.net.*;
    
    public class Test extends Applet {
      Image img = null;
      Image rot = null;
    
      int buffer[] = new int[32 * 32];
      int rotate[] = new int[32 * 32];
    
      public void init() {
       try {
          MediaTracker tracker = new MediaTracker (this);
          img = getImage(new URL(getDocumentBase(), "gumby.gif"));
          tracker.addImage (img, 0);
          tracker.waitForAll();
          PixelGrabber grabber =
            new PixelGrabber(img, 0, 0, 32, 32, buffer, 0, 32);
            try {
            grabber.grabPixels();
              }
          catch(InterruptedException e) {
             e.printStackTrace();
             }
          for(int y = 0; y < 32; y++) {
            for(int x = 0; x < 32; x++) {
              rotate[((32-x-1)*32)+y] = buffer[(y*32)+x];
              }
            }
          rot = createImage(new MemoryImageSource(32, 32, rotate, 0, 32));
          }
       catch (Exception e) {
          e.printStackTrace();
          }
       }
    
      public void update( Graphics g) {
       paint(g);
       }
    
      public void paint(Graphics g) {
         g.drawImage(img, 0, 0,this);
         g.drawImage(rot,0, 40, this);
        }
    }
    Last edited by Tb0h; 07-15-2009 at 03:08 AM.

  5. #5
    Tb0h is offline Member
    Join Date
    Jul 2009
    Posts
    21
    Rep Power
    0

    Default

    With most IDEs, when you make a new applet you have two main folders; "src" and "classes". In my IDE, (JCreator) "classes" has the HTML file and "src" has all of the coding. I created an "Images" Folder in src and added the picture. I used the code I displayed above and the applet appeared blank. What am I doing wrong?

  6. #6
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Would this code be able to fetch the image if I did as you suggested with the "Images" Folder?
    No. This
    Java Code:
                URL url = new URL(getDocumentBase(), "gumby.gif");
    will have java look for the image "gumby.gif" in the same folder as your document/html file is in and not inside another folder.
    If the "Images" folder is in the same folder as your document/html file then you would look for it with:
    Java Code:
                URL url = new URL(getDocumentBase(), "Images/gumby.gif");
    I loaded a gif contained in an "images" folder with this:
    Java Code:
                MediaTracker tracker = new MediaTracker (this);
                // your original url construction
                URL url = new URL(getDocumentBase(), "gumby.gif");
                // where will it point?
                System.out.println("url = " + url);
                // is there such a file?
                System.out.println("exists: " +
                    new java.io.File(url.getPath()).exists());
                // try duke in the "images" folder
                url = getClass().getResource("images/dukeWaveRed.gif");
                // where...
                System.out.println("url = " + url);
                // exists?
                System.out.println("exists: " +
                    new java.io.File(url.getPath()).exists());
                // this loaded the image okay
                img = getImage(new URL(getDocumentBase(), "images/dukeWaveRed.gif"));
    and got this in the console:
    Java Code:
    C:\jexp>javac testapplet.java
    
    C:\jexp>appletviewer TestApplet.java
    url = file:/C:/jexp/gumby.gif
    exists: false
    url = file:/C:/jexp/images/dukeWaveRed.gif
    exists: true
    The img loading was successful and both images showed up.

  7. #7
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    "classes" has the HTML file and "src" has all of the coding.
    I would have thought it would be the other way around: the source (.java) files in the "src" folder and the compiled byte code (.class files) in the "classes" folder. But that's not important.
    The documentBase should point to the folder where your html file is located and the codeBase should be the folder that contains the compiled (.class) file(s).
    You can write these out with System.out.println to see where it takes you.
    I added an html comment at the top of your source file and compile/run the applet with appletviewer (html files are too chaotic for development because of caching problems):
    Java Code:
    //  <applet code="TestApplet" width="200" height="200"></applet>
    Then adding these statements in init
    Java Code:
                System.out.println("documentBase: " + getDocumentBase());
                System.out.println("codeBase: " + getCodeBase());
    gives this in the console:
    Java Code:
    C:\jexp>appletviewer TestApplet.java
    documentBase: file:/C:/jexp/TestApplet.java
    codeBase: file:/C:/jexp/

  8. #8
    Tb0h is offline Member
    Join Date
    Jul 2009
    Posts
    21
    Rep Power
    0

    Default

    How should I incorporate your code with my Test applet code, displaying only the gumby.gif (rotating)

  9. #9
    Tb0h is offline Member
    Join Date
    Jul 2009
    Posts
    21
    Rep Power
    0

    Default

    This is the revised version of my code. The applet still appeared blank. I had the image folder in and everything, could you revise my code?
    Java Code:
    import java.applet.Applet;
    import java.awt.*;
    import java.awt.image.*;
    import java.net.*;
    
    public class TowerGame extends Applet {
      Image img = null;
      Image rot = null;
    
      int buffer[] = new int[32 * 32];
      int rotate[] = new int[32 * 32];
    
      public void init() {
       try {
          MediaTracker tracker = new MediaTracker (this);
          img = getImage(new URL(getDocumentBase(), "gumby.gif"));
          tracker.addImage (img, 0);
          tracker.waitForAll();
    	  System.out.println("documentBase: " + getDocumentBase());
    	  System.out.println("codeBase: " + getCodeBase());
          PixelGrabber grabber =
            new PixelGrabber(img, 0, 0, 32, 32, buffer, 0, 32);
            try {
            grabber.grabPixels();
              }
          catch(InterruptedException e) {
             e.printStackTrace();
             }
          for(int y = 0; y < 32; y++) {
            for(int x = 0; x < 32; x++) {
              rotate[((32-x-1)*32)+y] = buffer[(y*32)+x];
              }
            }
          rot = createImage(new MemoryImageSource(32, 32, rotate, 0, 32));
          }
       catch (Exception e) {
          e.printStackTrace();
          }
       }
    
      public void update( Graphics g) {
       paint(g);
       }
    
      public void paint(Graphics g) {
         g.drawImage(img, 0, 0,this);
         g.drawImage(rot,0, 40, this);
        }
    }
    Last edited by Tb0h; 07-15-2009 at 11:04 PM. Reason: code brackets

  10. #10
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    You're still looking for the image in the same folder as your html file.
    You can copy the image to that folder if you want to load it from there. If you want to load it from an "images" type folder you should include the folder in your relative path. So if your folder name is "images" your revised img assignment statement would look like this:
    Java Code:
          img = getImage(new URL(getDocumentBase(), "images/gumby.gif"));
    This tells java: start in the folder in which the html file is located, ie, the documentBase, and find the "images" folder. Go inside this folder and find the "gumby.gif" file.
    Since you are focusing on image loading let's try this:
    Java Code:
    //  <applet code="TestApplet" width="400" height="200"></applet>
    import java.applet.Applet;
    import java.awt.*;
    import java.net.*;
    
    public class TestApplet extends Applet {
        Image[] images = new Image[4];
    
        public void init() {
            try {
                System.out.println("documentBase: " + getDocumentBase());
                System.out.println("codeBase: " + getCodeBase());
                String path = "images/dukeWaveRed.gif";
    
                URL docBaseUrl = new URL(getDocumentBase(), path);
                System.out.println("docBaseUrl = " + docBaseUrl);
                images[0] = getImage(docBaseUrl);
    
                URL codeBaseUrl = new URL(getCodeBase(), path);
                System.out.println("codeBaseUrl = " + codeBaseUrl);
                images[1] = getImage(codeBaseUrl);
    
                URL resourceUrl = getClass().getResource(path);
                System.out.println("resourceUrl = " + resourceUrl);
                images[2] = getImage(resourceUrl);
    
                URL directUrl = new URL("file:/C:/jexp/images/dukeWaveRed.gif");
                System.out.println("directUrl = " + directUrl);
                images[3] = getImage(directUrl);
    
                MediaTracker tracker = new MediaTracker(this);
                for(int i = 0; i < images.length; i++) {
                    tracker.addImage (images[i], 0);
                }
                tracker.waitForAll();
            } catch(Exception e) {
                System.out.println(e.getClass().getName() + ": " +
                                   e.getMessage());
            }
        }
    
        public void update( Graphics g) {
            paint(g);
        }
    
        public void paint(Graphics g) {
            int w = getWidth();
            int h = getHeight();
            int iw = images[0].getWidth(this);
            int ih = images[0].getHeight(this);
            int pad = 5;
            int x0 = (w - images.length*iw - (images.length-1)*pad)/2;
            int y = (h - ih)/2;
            int x = x0;
            for(int i = 0; i < images.length; i++) {
                g.drawImage(images[i], x, y, this);
                x += iw + pad;
            }
        }
    }
    I'm not having any trouble with sandbox security issues while running this in the appletviewer.
    Here's the console output:
    Java Code:
    C:\jexp>javac testapplet.java
    
    C:\jexp>appletviewer TestApplet.java
    documentBase: file:/C:/jexp/TestApplet.java
    codeBase: file:/C:/jexp/
    docBaseUrl = file:/C:/jexp/images/dukeWaveRed.gif
    codeBaseUrl = file:/C:/jexp/images/dukeWaveRed.gif
    resourceUrl = file:/C:/jexp/images/dukeWaveRed.gif
    directUrl = file:/C:/jexp/images/dukeWaveRed.gif
    Make sure you change the path statement in the code above to suit your folder/imagefile:
    String path = "images/dukeWaveRed.gif";

  11. #11
    Tb0h is offline Member
    Join Date
    Jul 2009
    Posts
    21
    Rep Power
    0

    Default

    Oh my gosh, hardwired you are a LIFE SAVER. Thank you so much!!! I can FINALLY display an image, you have no idea how frustrating it has been (Well, if you are a programmer, frustration is probably one thing you understand very well)
    Thank You!
    (How do you mark a forum string [SOLVED]?)

Similar Threads

  1. JCreator error:
    By guru_boy in forum New To Java
    Replies: 4
    Last Post: 05-06-2009, 05:53 AM
  2. Applet in JCreator
    By mgm2010 in forum JCreator
    Replies: 0
    Last Post: 03-02-2009, 02:30 PM
  3. Replies: 8
    Last Post: 02-26-2009, 03:21 PM
  4. About using jcreator
    By yuchuang in forum JCreator
    Replies: 4
    Last Post: 11-29-2008, 08:29 PM
  5. jcreator
    By nikhil_solanki015 in forum JCreator
    Replies: 2
    Last Post: 10-28-2008, 07:04 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •