//The following code snippet illustrates how to use a MediaTracker to wait while an image is prepared:
//file: StatusImage.java
import java.awt.*;
import javax.swing.*;
public class StatusImage extends JComponent
{
boolean loaded = false;
String message = "Loading...";
Image image;
public StatusImage( Image image ) { this.image = image; }
public void paint(Graphics g) {
if (loaded)
g.drawImage(image, 0, 0, this);
else {
g.drawRect(0, 0, getSize().width - 1, getSize().height - 1);
g.drawString(message, 20, 20);
}
}
public void loaded() {
loaded = true;
repaint();
}
public void setMessage( String msg ) {
message = msg;
repaint();
}
public static void main( String [] args ) {
JFrame frame = new JFrame("TrackImage");
Image image = Toolkit.getDefaultToolkit().getImage( args[0] );
StatusImage statusImage = new StatusImage( image );
frame.add( statusImage );
frame.setSize(300,300);
frame.setVisible(true);
MediaTracker tracker = new MediaTracker( statusImage );
int MAIN_IMAGE = 0;
tracker.addImage( image, MAIN_IMAGE );
try {
tracker.waitForID( MAIN_IMAGE ); }
catch (InterruptedException e) {}
if ( tracker.isErrorID( MAIN_IMAGE ) )
statusImage.setMessage( "Error" );
else
statusImage.loaded();
}
}
Showing posts with label Image. Show all posts
Showing posts with label Image. Show all posts
ImageObserver Interface - Loading Image - Java Sample Program
//file: ObserveImageLoad.java
import java.awt.*;
import java.awt.image.*;
public class ObserveImageLoad {
public static void main( String [] args)
{
ImageObserver myObserver = new ImageObserver() {
public boolean imageUpdate( Image image, int flags, int x, int y, int width, int height)
{
if ( (flags & HEIGHT) !=0 )
System.out.println("Image height = " + height );
if ( (flags & WIDTH ) !=0 )
System.out.println("Image width = " + width );
if ( (flags & FRAMEBITS) != 0 )
System.out.println("Another frame finished.");
if ( (flags & SOMEBITS) != 0 )
System.out.println("Image section :" + new Rectangle( x, y, width, height ) );
if ( (flags & ALLBITS) != 0 )
System.out.println("Image finished!");
if ( (flags & ABORT) != 0 )
System.out.println("Image load aborted...");
return true;
}
};
Toolkit toolkit = Toolkit.getDefaultToolkit();
Image img = toolkit.getImage( args[0] );
toolkit.prepareImage( img, -1, -1, myObserver );
}
}
import java.awt.*;
import java.awt.image.*;
public class ObserveImageLoad {
public static void main( String [] args)
{
ImageObserver myObserver = new ImageObserver() {
public boolean imageUpdate( Image image, int flags, int x, int y, int width, int height)
{
if ( (flags & HEIGHT) !=0 )
System.out.println("Image height = " + height );
if ( (flags & WIDTH ) !=0 )
System.out.println("Image width = " + width );
if ( (flags & FRAMEBITS) != 0 )
System.out.println("Another frame finished.");
if ( (flags & SOMEBITS) != 0 )
System.out.println("Image section :" + new Rectangle( x, y, width, height ) );
if ( (flags & ALLBITS) != 0 )
System.out.println("Image finished!");
if ( (flags & ABORT) != 0 )
System.out.println("Image load aborted...");
return true;
}
};
Toolkit toolkit = Toolkit.getDefaultToolkit();
Image img = toolkit.getImage( args[0] );
toolkit.prepareImage( img, -1, -1, myObserver );
}
}
Subscribe to:
Posts (Atom)