Results 1 to 4 of 4
  1. #1
    xing217sg is offline Member
    Join Date
    Oct 2009
    Posts
    2
    Rep Power
    0

    Default Help: how to set imageObserver

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.io.*;


    public class AnimApp extends JComponent implements Runnable {
    Image images;
    private long oldtimeStamp;

    Toolkit toolkit = Toolkit.getDefaultToolkit();


    public void paint(Graphics g) {
    super.paintComponent (g);

    System.out.println("NA BEI HERER!!!!!!!!!");
    g.drawImage(images, 0, 0, this);

    }

    public void run() {
    // Load the array of images
    // Display each image for 1 second
    int delay = 400; // 0.040 second

    try {
    while (true) {
    if (oldtimeStamp != this.checktimeStamp()) {
    oldtimeStamp = checktimeStamp();
    images = toolkit.createImage("image1.jpg");
    images = toolkit.getImage("image1.jpg");

    Thread.sleep(delay);
    }
    } catch (Exception e) {
    }
    }



    public long checktimeStamp(){
    File file = new File("image1.jpg");

    System.out.println(""+file.lastModified());
    return file.lastModified();
    }

    public static void main(String[] args) {
    AnimApp app = new AnimApp();

    // Display the animation in a frame
    JFrame frame = new JFrame();
    frame.getContentPane().add(app);
    frame.setSize(800, 600);
    frame.setVisible(true);

    (new Thread(app)).start();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    }
    }

    I wanted to load an image when the original image is replaced, however by doing so the buffering of the image is cancelled. The updated image is meant to create an animation when it replaced the whole image. However for the animation to display smoothly, i heard that i need to implement imageobserver. Anyone knows how to do it?

  2. #2
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    6

    Default

    Googling "image animation java" gave numerous useful results. The first was:
    InformIT: Creating Animation with Java > Animating a Sequence of Images

    You might also want to look at examples of using java.awt.MediaTracker.

    Here is one way to load an image without an ImageObserver:
    Java Code:
        static public Image fetchImage(String filename) {
    	try {
    		File imf = new File(filename);
    		return ImageIO.read(imf);
    	} catch (IOException ex) {
    		Util.log.severe("failed to load " + filename + ": " + ex);
    		return null;
    	}
        }
    And here is another
    Java Code:
       /** Fetch an image from the /image/ subdirectory of the class files directory
         * NB: The class must be "built" so the images are
         * copied from the src/ directory to the  build/ directory
         * @param path  Path of resource, realtive to the .class file's directory
         * Typically,  images are in a subdirectory of the class directory
         * and the path is of the form "images/icon.ext"
         * where ext is for an icon (ico) or an image (gif, png, or jpg).
         * @return  An Image, or null if there is an error.
         */
        static public Image fetchImageResource(String path) {
    	java.net.URL imgURL = Tagger.class.getResource(path);
    	Image o = null;
    	if (imgURL != null)
    		try {o = ImageIO.read(imgURL); }
    		catch (IOException ex) { o = null; }
    	if (o != null) return o;
    	log.severe("Couldn't load image resource: " + path);
    	return null;
        }
    Last edited by zweibieren; 10-21-2009 at 05:28 PM. Reason: add code

  3. #3
    xing217sg is offline Member
    Join Date
    Oct 2009
    Posts
    2
    Rep Power
    0

    Default

    Hi, actually my code can load the image sucessfully. Its just that i want an imageObserver to be implemented so that flickering can be reduced.

  4. #4
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    6

    Default

    Egad. I've now read the code a bit more closely. It is loading images as they are replaced in a file.

    I wonder if the behavior could be improved by AnimApp extend JPanel.
    And instead of overriding paint(), override paintComponent().

    The flicker may occur because the images variable is being repainted at the same time as it is being read.

    Try this version of the while loop in the run() method:
    Java Code:
    while (true) {
        if ( ! oldtimeStamp.equals(checktimeStamp())) {
            oldtimeStamp = checktimeStamp();
            Image nextImage = ImageIO.read("image1.jpg");
            images = nextImage;   // change images only when content is complete
        }
        Thread.sleep(delay);
    }
    Note that the delay is now outside the if statement.

    If you were to use an ImageObserver, the getImage should again be an assignment to nextImage.
    AnimApp would implement ImageObserver and would have a method like

    Java Code:
    boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) {
        if ((infoflags&ALLBITS) != 0)
            images = nextImage;
    }
    However: The documentation of ImageObserver suggests this may not work.
    For ALLBITS it says:
    This flag in the infoflags argument to imageUpdate indicates that a static image which was previously drawn is now complete and can be drawn again in its final form.
    The "was previously drawn" part implies that imageUpdate will only be called after trying to draw the image. I recommend the ImageIO.read() solution instead.
    Last edited by zweibieren; 10-21-2009 at 06:38 PM. Reason: reread the initial post more carefully

Posting Permissions

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