Results 1 to 13 of 13
  1. #1
    ea25 is offline Member
    Join Date
    Apr 2010
    Posts
    8
    Rep Power
    0

    Default Java Applet loading image; render as you get image?

    Hi all, I'm writing a very simple applet which fetches an image from an external URL (using java.net.URL) and paints the image in my applet. I succeeded in loading the image. However now I would like to enhance my applet so that it renders the image progressively as it it fetched from the URL (in a Firefox manner, rendering from top to bottom). I would greatly appreciate if anyone can help me with this issue. Thanks

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,941
    Rep Power
    8

    Default

    This is just a guess, but have you looked at ImageObserver yet? I wonder what happens if you override imageUpdate() and draw what you have of the image as that method is called?
    Last edited by KevinWorkman; 04-13-2011 at 02:22 PM.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    ea25 is offline Member
    Join Date
    Apr 2010
    Posts
    8
    Rep Power
    0

    Default

    Thanks for getting back to me Kevin, I looked into the ImageObserver and it might be a promising idea to use it in conjunction with repaint(). I'll also look into the ImageUpdate. Will report back. Thanks for the time being

  4. #4
    ea25 is offline Member
    Join Date
    Apr 2010
    Posts
    8
    Rep Power
    0

    Default

    I can't seem to figure exactly out how ImageObserver works and how can harness its functionality for my purpose. Can somebody kindly help me out? I'm at loss about this image loading issue, it seems rather impossible to get it done

  5. #5
    ea25 is offline Member
    Join Date
    Apr 2010
    Posts
    8
    Rep Power
    0

    Default

    I finally managed to figure out how to override the imageUpdate. I also learned that there is a system property, awt.image.incrementalDraw, which by default should be set to True and which enables the drawing of images in an incremental fashion. However mine was defaulted to false, so I had to manually enable it. However I still haven't seen the results I expected, drawImage seems to persist on drawing the image only when all picture bytes arrive. Here is my applet code so far:

    Java Code:
     
    import java.applet.*;
    import java.awt.*;
    import java.awt.image.ImageObserver;
    import java.io.IOException;
    import java.net.URL;
    
    public class imageApp extends Applet {
        Image image;
     
        public void init () {
        	System.setProperty("awt.image.incrementalDraw", "true"); 
        	incrementaldraw = Boolean.getBoolean("awt.image.incrementalDraw");
            System.out.println(incrementaldraw); //Verify incrementalDraw is set to true
    
        	try{
    			URL url = new URL("http://geoeyemediaportal.s3.amazonaws.com/gallery/Inauguration_2009.jpg");
    			image = getImage(url);
    	}
    	catch(IOException e){}
        }
        
    [INDENT][/INDENT]public void paint (Graphics g) {
                g.drawImage (image, 0, 0, this);
        }
        public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) {
                if ((infoflags & SOMEBITS ) != 0){
                    repaint();
                }
                return super.imageUpdate(img, infoflags, x, y, width, height);
        }
    }

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,941
    Rep Power
    8

    Default

    I don't think imageUpdate() is working how we (I) expected it to. I put a print statement in there, and it seems to never be called. Weird. I might be out of my depth on this one.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,941
    Rep Power
    8

    Default

    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  8. #8
    ea25 is offline Member
    Join Date
    Apr 2010
    Posts
    8
    Rep Power
    0

    Default

    Do you mean something like this below ? If I put a System.out.println in that section it actually prints out, therefore repaint is being called. I think the problem derives from drawImage or the java Graphics component.

    Java Code:
        public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) {
                if ((infoflags & SOMEBITS ) != 0){
                    System.out.println ("REPAINTING!");//<------------------------
                    repaint();
                }
                return super.imageUpdate(img, infoflags, x, y, width, height);
        }
    I also found this bug with older versions of jdk, but I assume it must have been fixed since then: Bug ID: 5034556 drawImage behavior

    I'm completely at a loss about this issue :confused:

  9. #9
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,941
    Rep Power
    8

    Default

    I meant something like this:

    Java Code:
    @Override
    	public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) {
    		
    		System.out.println("imageUpdate");
    		
    		//if ((infoflags & SOMEBITS ) != 0){
    			repaint();
    		//}
    		return super.imageUpdate(img, infoflags, x, y, width, height);
    	}
    Actually, I wouldn't be surprised if that bug is unfixed. Which means this might not be doable. But maybe somebody smarter than me can swoop in and tell us what we're missing.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  10. #10
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,941
    Rep Power
    8

    Default

    Yeah, here's the "main" bug report for this: Bug ID: 4098417 Component does not update image incrementally

    It doesn't look like it, or any of its related bugs, has been fixed yet. There might be a workaround, but I wouldn't know what it is. Sorry. Hopefully somebody else can help more.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  11. #11
    ea25 is offline Member
    Join Date
    Apr 2010
    Posts
    8
    Rep Power
    0

    Default

    If I place at that location, System.out.println("imageUpdate"); get called as well. Doesn't seem to make a difference though. How someone else can give us a helping hand on this one. Thanks very much for the time being Kevin

  12. #12
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,941
    Rep Power
    8

    Default

    Interesting. I threw together this application version, hopefully somebody can use it to figure out a workaround:
    Java Code:
    import java.awt.*;
    import java.io.File;
    import java.io.IOException;
    
    import javax.imageio.ImageIO;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    public class ImageApp extends JPanel {
    	Image image;
    
    	public ImageApp () {
    		System.setProperty("awt.image.incrementalDraw", "true"); 
    		boolean incrementaldraw = Boolean.getBoolean("awt.image.incrementalDraw");
    		System.out.println(incrementaldraw); //Verify incrementalDraw is set to true
    
    		new Thread(){
    			public void run(){
    				try{
    					image = ImageIO.read(new File("C:/Documents and Settings/kworkman/Desktop/Inauguration_2009.jpg"));
    					//image = ImageIO.read(new File("http://geoeyemediaportal.s3.amazonaws.com/gallery/Inauguration_2009.jpg"));
    				}
    				catch(IOException e){}
    			}
    		}.start();
    		
    	}
    	public void paintComponent(Graphics g) {
    		super.paintComponent(g);
    		//if(image != null){
    		g.drawImage (image, 0, 0, this);
    		//}
    	}
    	
    	@Override
    	public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) {
    		
    		System.out.println("imageUpdate");
    		
    		//if ((infoflags & SOMEBITS ) != 0){
    			repaint();
    		//}
    		return super.imageUpdate(img, infoflags, x, y, width, height);
    	}
    	
    	public static void main(String... args){
    		JFrame frame = new JFrame("Image Test");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.add(new ImageApp());
    		
    		frame.setSize(500,500);
    		frame.setVisible(true);
    	}
    }
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  13. #13
    ea25 is offline Member
    Join Date
    Apr 2010
    Posts
    8
    Rep Power
    0

    Default

    Cheers, better structured code than mine as I'm a beginner to Java. Seems I have immediately stumbled on a big one already.

Similar Threads

  1. Java Applet Strange Image Loading Problem
    By 1yuchen in forum Java Applets
    Replies: 36
    Last Post: 01-06-2012, 02:06 AM
  2. Image Loading
    By hobbles in forum New To Java
    Replies: 10
    Last Post: 09-06-2010, 08:30 PM
  3. Loading image in applet
    By syarizma in forum Advanced Java
    Replies: 0
    Last Post: 08-06-2009, 09:02 AM
  4. loading image into applet
    By balaram in forum Java Applets
    Replies: 1
    Last Post: 11-06-2008, 02:13 PM
  5. Loading An Image Help Please!
    By shaungoater in forum Java 2D
    Replies: 2
    Last Post: 01-09-2008, 08:14 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
  •