Results 1 to 11 of 11
  1. #1
    kof20012 is offline Member
    Join Date
    Sep 2008
    Posts
    6
    Rep Power
    0

    Default problem getting the image(APPLET)

    hi there, i got a problem in getting the image from a applet, this is a Chess Game but without images, it doesnt work.

    this is the code

    Java Code:
    private static Image pieceImage[][] = new Image[2][6];
        private static String imageFilename[][] = {
            { "wp.gif", "wn.gif", "wb.gif", "wr.gif", "wq.gif", "wk.gif" },
            { "bp.gif", "bn.gif", "bb.gif", "br.gif", "bq.gif", "bk.gif" }};
            
        public static void bufferImages(Applet app) {
            for (int i = 0; i < 2; i++)
                for (int j = 0; j < 6; j++)
                    pieceImage[i][j] = app.getImage(app.getClass().getResource("/images/" + imageFilename[i][j]));
        }

    i have sent an image with the images folder(images inside)

    best regards,
    kof20012
    Attached Thumbnails Attached Thumbnails problem getting the image(APPLET)-xadrez.jpg  

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,611
    Rep Power
    25

    Default

    it doesnt work.
    Do you get error messages?
    Add a println() to show the URL returned by getResource before passing it to getImage().

    Where is the images folder relative to the current directory?

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

    Default

    The address on your folder looks something like:
    Java Code:
    ...\java programs\ChessApp\src\ChessApp
    and it has your images and an "images" folder in it. If you are compiling your source/.java files and running your bytecode/.class files in/from this folder the jvm will probably be looking in the "images" folder for your images. Try Norm's suggestion to check on the URLs returned by getResource:
    Java Code:
    String path = "/images/" + imageFilename[i][j];
    System.out.println(app.getClass().getResource(path).getPath());
    If the images are also in the "images" folder then
    Java Code:
    String path = "images/" + imageFilename[i][j];
    should fetch them. If you want to load them from the folder shown in your thumbnail then
    Java Code:
    String path = imageFilename[i][j];
    should do the trick.

    The other area of difficulty is using the antique getImage method to load images. This method is asynchronous: it returns immediately, ie, it doesn't block program execution to load the images. So you have to use a MediaTracker to load the images. The MediaTracker class api comments section has a code example of how to do this.

  4. #4
    kof20012 is offline Member
    Join Date
    Sep 2008
    Posts
    6
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Do you get error messages?
    Add a println() to show the URL returned by getResource before passing it to getImage().

    Where is the images folder relative to the current directory?
    they are inside the folder. inside the java files (Javafiles)/images/

  5. #5
    kof20012 is offline Member
    Join Date
    Sep 2008
    Posts
    6
    Rep Power
    0

    Default

    Quote Originally Posted by hardwired View Post
    The address on your folder looks something like:
    Java Code:
    ...\java programs\ChessApp\src\ChessApp
    and it has your images and an "images" folder in it. If you are compiling your source/.java files and running your bytecode/.class files in/from this folder the jvm will probably be looking in the "images" folder for your images. Try Norm's suggestion to check on the URLs returned by getResource:
    Java Code:
    String path = "/images/" + imageFilename[i][j];
    System.out.println(app.getClass().getResource(path).getPath());
    If the images are also in the "images" folder then
    Java Code:
    String path = "images/" + imageFilename[i][j];
    should fetch them. If you want to load them from the folder shown in your thumbnail then
    Java Code:
    String path = imageFilename[i][j];
    should do the trick.

    The other area of difficulty is using the antique getImage method to load images. This method is asynchronous: it returns immediately, ie, it doesn't block program execution to load the images. So you have to use a MediaTracker to load the images. The MediaTracker class api comments section has a code example of how to do this.

    it gave me an error:

    Java Code:
    BoardView.java:341: not a statement
                       String path="images/" + imageFilename[i][j]; 
                       ^
    BoardView.java:341: ';' expected
                       String path="images/" + imageFilename[i][j]; 
                             ^
    2 errors

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,611
    Rep Power
    25

    Default

    You have a syntax/coding error somewhere before line 341.
    Look at that area closely to see where it is. If you need help, copy the code from that area of your program here. Start with the nearest method definition.

  7. #7
    kof20012 is offline Member
    Join Date
    Sep 2008
    Posts
    6
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    You have a syntax/coding error somewhere before line 341.
    Look at that area closely to see where it is. If you need help, copy the code from that area of your program here. Start with the nearest method definition.
    im going to sent you the whole programme.
    the problem is on the boardview
    Attached Files Attached Files

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,611
    Rep Power
    25

    Default

    It be quicker if you'd post the code where the problem is.
    Your first post implied that the program compiled ok and that the problem was during execution. Then you must have made some changes to the code and now it does NOT compile with the changes and you get compile errors.
    LOOK AT THE CHANGES YOU MADE, the error is probably there.

  9. #9
    kof20012 is offline Member
    Join Date
    Sep 2008
    Posts
    6
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    It be quicker if you'd post the code where the problem is.
    Your first post implied that the program compiled ok and that the problem was during execution. Then you must have made some changes to the code and now it does NOT compile with the changes and you get compile errors.
    LOOK AT THE CHANGES YOU MADE, the error is probably there.
    Java Code:
      public static void bufferImages(Applet app) {
            for (int i = 0; i < 2; i++)
                for (int j = 0; j < 6; j++)
                    //pieceImage[i][j] = app.getImage(app.getClass().getResource("images/" + imageFilename[i][j]));
                    
                    String path = "/images/" + imageFilename[i][j];
    				System.out.println(app.getClass().getResource(path).getPath());
        }

  10. #10
    kof20012 is offline Member
    Join Date
    Sep 2008
    Posts
    6
    Rep Power
    0

    Default

    i have found the problem it was missing the {} after the "for" were 2 statements (2 code lines) i needed to put { }

    the problem is still the same

    at BoardView.bufferImages(BoardView.java:299)
    at ChessApp.init(ChessApp.java:27)
    at sun.applet.AppletPanel.run(AppletPanel.java:435)
    at java.lang.Thread.run(Thread.java:674)

  11. #11
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,611
    Rep Power
    25

    Default

    the problem is still the same
    You left off the begining of the error message.

Similar Threads

  1. Applet Image location
    By MarkWilson in forum NetBeans
    Replies: 3
    Last Post: 06-30-2008, 08:37 AM
  2. Problem with Image Convertion
    By aptivo in forum Java 2D
    Replies: 0
    Last Post: 06-10-2008, 12:15 AM
  3. Image upload applet 2.0
    By jfileupload in forum Java Software
    Replies: 0
    Last Post: 03-24-2008, 05:15 PM
  4. moving image - PROBLEM
    By Triss in forum New To Java
    Replies: 3
    Last Post: 01-17-2008, 07:52 PM
  5. how to place an image in an applet
    By paty in forum Java Applets
    Replies: 2
    Last Post: 08-04-2007, 06:15 PM

Posting Permissions

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