Results 1 to 14 of 14
Like Tree2Likes
  • 1 Post By Norm
  • 1 Post By DarrylBurke

Thread: Need Help with Loading couple images to a BufferedImage.

  1. #1
    Shikatsu is offline Member
    Join Date
    Nov 2011
    Posts
    49
    Rep Power
    0

    Default Need Help with Loading couple images to a BufferedImage.

    Greetings EveryOne

    I am making a board game, I want to load the board and the pieces with their current locations into a BufferedImage then paint the BufferedImage to the screen, is this a good aproch? if not can you show me a better one?

    Here is the code that I came up with:

    Java Code:
    import java.applet.*;
    import java.awt.*;
    import java.awt.image.*;
    
    public class ImageExampleWithBuffer extends Applet
    {
        private int WinWidth  = 200;
        private int WinHeight = 200;
        
        Image IMG;
        
        BufferedImage BuffImg = new BufferedImage(50, 50, 
                                                  BufferedImage.TYPE_INT_RGB);
        
        @Override
        public void init()
        {
            setSize(WinWidth, WinHeight);
            
            IMG = getImage(getDocumentBase(), "Smily.jpg");
            
            Graphics2D Graph = BuffImg.createGraphics();
            Graph.drawImage(IMG, 50, 50, this);
            
            repaint();
        }
        
        @Override
        public void paint(Graphics G)
        {
            G.drawImage(BuffImg, 50, 50, this);
        }
                
    }
    The output is a 50*50 black square, the image doesn't shows up, even it's in the same directory as the applet.

    Please SomeOne Help, Thank You.
    Knowledge Is Power! Don't give me a fish but teach me how to catch one
    I am not asking you to code it for me, what i need is guidance so i can learn

  2. #2
    SourCookie's Avatar
    SourCookie is offline Member
    Join Date
    Nov 2011
    Location
    Los Angeles, California
    Posts
    5
    Rep Power
    0

    Default Re: Need Help with Loading couple images to a BufferedImage.

    To load a image from a file enter in this code:

    BufferedImage img = null;
    try {
    img = ImageIO.read(new File("strawberry.jpg"));
    } catch (IOException e) {
    }

    Be sure to substitute strawberry.jpg for the actual file.

    If the code is running in an applet, then its just as easy to obtain the image from the applet codebase:

    try {
    URL url = new URL(getCodeBase(), "strawberry.jpg");
    img = ImageIO.read(url);
    } catch (IOException e) {
    }

    Image I/O which has support for GIF, PNG, JPEG, BMP, and WBMP, recognises the contents of the file as a JPEG format image, and decodes it into a BufferedImage which can be directly used by Java 2D.

    Hope this helps. :)
    Last edited by SourCookie; 11-26-2011 at 06:28 PM.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,307
    Rep Power
    25

    Default Re: Need Help with Loading couple images to a BufferedImage.

    How are you executing the program?
    Do you get any error messages?

  4. #4
    Shikatsu is offline Member
    Join Date
    Nov 2011
    Posts
    49
    Rep Power
    0

    Default Re: Need Help with Loading couple images to a BufferedImage.

    Quote Originally Posted by Norm View Post
    How are you executing the program?
    Do you get any error messages?
    The program executes fine and no error messages, the problem is the image doesn't show up in the applet only a 50*50 black square, that makes me wonder if the image is actually copied to BufferedImage (BuffImg).
    Knowledge Is Power! Don't give me a fish but teach me how to catch one
    I am not asking you to code it for me, what i need is guidance so i can learn

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,307
    Rep Power
    25

    Default Re: Need Help with Loading couple images to a BufferedImage.

    Read the API doc for the getImage method. It probably doesn't do what you want when you want it.

  6. #6
    Shikatsu is offline Member
    Join Date
    Nov 2011
    Posts
    49
    Rep Power
    0

    Default Re: Need Help with Loading couple images to a BufferedImage.

    Quote Originally Posted by Norm View Post
    Read the API doc for the getImage method. It probably doesn't do what you want when you want it.
    This is what the API doc say:

    Quote Originally Posted by API
    getImage

    public Image getImage(URL url,
    String name)

    Returns an Image object that can then be painted on the screen. The url argument must specify an absolute URL. The name argument is a specifier that is relative to the url argument.

    This method always returns immediately, whether or not the image exists. When this applet attempts to draw the image on the screen, the data will be loaded. The graphics primitives that draw the image will incrementally paint on the screen.

    Parameters:
    url - an absolute URL giving the base location of the image.
    name - the location of the image, relative to the url argument.
    Returns:
    the image at the specified URL.
    And about getDocumentBase():

    Quote Originally Posted by API
    public URL getDocumentBase()

    Gets the URL of the document in which this applet is embedded. For example, suppose an applet is contained within the document:

    Archive: J2SE End of Life (EOL) Versions


    The document base is:

    Archive: J2SE End of Life (EOL) Versions


    Returns:
    the URL of the document that contains this applet.
    From reading the doc I can't see there is a problem with getImage() or getDocumentBase().

    I have already tried what SourCookie suggested, but it's the same.
    Knowledge Is Power! Don't give me a fish but teach me how to catch one
    I am not asking you to code it for me, what i need is guidance so i can learn

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,307
    Rep Power
    25

    Default Re: Need Help with Loading couple images to a BufferedImage.

    When this applet attempts to draw the image on the screen, the data will be loaded.
    That says to me that the image data might not be loaded UNTIL you attempt to draw the image on the screen.
    Your code does NOT attempt to draw the image on the screen so it might not be loaded.

    Try adding this to your paint method:
    G.drawImage(IMG, 250, 250, this); // This causes the image to be loaded

    and if that works, add this following the above (all in paint):

    Graphics2D Graph = BuffImg.createGraphics();
    Graph.drawImage(IMG, 0, 0, this);
    G.drawImage(BuffImg, 100, 100, this);

    I got the image to draw on the screen with both of the above.
    Attached Thumbnails Attached Thumbnails Need Help with Loading couple images to a BufferedImage.-appletviewer.png  
    Last edited by Norm; 11-26-2011 at 09:14 PM.
    Shikatsu likes this.

  8. #8
    Shikatsu is offline Member
    Join Date
    Nov 2011
    Posts
    49
    Rep Power
    0

    Default Re: Need Help with Loading couple images to a BufferedImage.

    Thank you Sir Norm, You are awesome

    Here is the code in case someone curious:

    Java Code:
    import java.applet.*;
    import java.awt.*;
    import java.awt.image.*;
     
    public class ImageExampleWithBuffer extends Applet
    {
        private int WinWidth  = 200;
        private int WinHeight = 200;
         
        Image IMG;
         
        BufferedImage BuffImg = new BufferedImage(50, 50,
                                                  BufferedImage.TYPE_INT_RGB);
        Graphics2D Graph = BuffImg.createGraphics(); 
        @Override
        public void init()
        {
            setSize(WinWidth, WinHeight);
             
            IMG = getImage(getDocumentBase(), "WhiteSmily.jpg");
              
            repaint();
        }
         
        @Override
        public void paint(Graphics G)
        {
            Graph.drawImage(IMG, 0, 0, this);
            G.drawImage(BuffImg, 50, 50, this);
        }
                 
    }
    Any comment, suggestion I will be glad to heart it.
    Last edited by Shikatsu; 11-27-2011 at 03:32 AM.
    Knowledge Is Power! Don't give me a fish but teach me how to catch one
    I am not asking you to code it for me, what i need is guidance so i can learn

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Need Help with Loading couple images to a BufferedImage.

    Norm likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,307
    Rep Power
    25

    Default Re: Need Help with Loading couple images to a BufferedImage.

    DB Nice post on the ranch

  11. #11
    Shikatsu is offline Member
    Join Date
    Nov 2011
    Posts
    49
    Rep Power
    0

    Default Re: Need Help with Loading couple images to a BufferedImage.

    It's a against the rules to post the same question on a different forum or something?
    Knowledge Is Power! Don't give me a fish but teach me how to catch one
    I am not asking you to code it for me, what i need is guidance so i can learn

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

    Default Re: Need Help with Loading couple images to a BufferedImage.

    Quote Originally Posted by Shikatsu View Post
    It's a against the rules to post the same question on a different forum or something?
    Did you read the link that Darryl gave you on the ranch regarding this? Please read the information given to you before making us restate that which has been stated before.

  13. #13
    Shikatsu is offline Member
    Join Date
    Nov 2011
    Posts
    49
    Rep Power
    0

    Default Re: Need Help with Loading couple images to a BufferedImage.

    I have read that on coderanch, I asked here before I noticed that post, and I was wondering if it's the same here.
    imho, I won't be annoyed if someone came and asked me a question then he/she went to someone else to get a second opinion, it's like getting a second opinion from another doctor, should that be annoying? I am just wondering.

    Even though, I posted there because no one answered for a while and I don't have much free time and when I do I need to achieve the most I can in that time.

    Thank you for you understanding.
    Knowledge Is Power! Don't give me a fish but teach me how to catch one
    I am not asking you to code it for me, what i need is guidance so i can learn

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

    Default Re: Need Help with Loading couple images to a BufferedImage.

    Quote Originally Posted by Shikatsu View Post
    I have read that on coderanch, I asked here before I noticed that post, and I was wondering if it's the same here.
    The principles of etiquette are the same everywhere.

    imho, I won't be annoyed if someone came and asked me a question then he/she went to someone else to get a second opinion, it's like getting a second opinion from another doctor, should that be annoying? I am just wondering.
    Again read the link. We're not annoyed by multiposting. We're annoyed by your not being forthcoming about it.

    Even though, I posted there because no one answered for a while and I don't have much free time and when I do I need to achieve the most I can in that time.
    And by multiposting without notification, you show that you're not respectful of our time. As the link states, cross-posting can frustrate anyone who tries to help you only to find out later that the same answer was given hours ago in a cross-posted thread. No one likes wasting their time, especially a volunteer. The polite thing to do would be to not do this, but if you feel that you absolutely must, to at least provide links in both cross-posts to each other.



    Thank you for you understanding.
    Ditto.

Similar Threads

  1. Loading Images
    By TacoManStan in forum Java Gaming
    Replies: 9
    Last Post: 09-22-2011, 11:18 PM
  2. Loading files(namely images)
    By jammas615 in forum New To Java
    Replies: 1
    Last Post: 07-11-2011, 10:06 AM
  3. Loading images from an Array
    By pinkette in forum New To Java
    Replies: 5
    Last Post: 04-15-2011, 11:37 AM
  4. Loading Images - Imp
    By Thulasiraman in forum Advanced Java
    Replies: 0
    Last Post: 01-28-2008, 09:33 AM

Posting Permissions

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