Search in sources :

Example 21 with MediaTracker

use of java.awt.MediaTracker in project processdash by dtuma.

the class DashboardIconFactory method selectImageClosestToSize.

/** 
     * From a list of images, select the one that is closest to the preferred
     * size.
     * 
     *
     * 
     * @param images a list of images to choose from.
     * @param preferredSize
     * @return
     */
public static Image selectImageClosestToSize(Collection<? extends Image> images, int preferredSize, boolean preferSmaller) {
    // if no images were provided, return null.
    if (images == null || images.isEmpty())
        return null;
    // for our purposes, the images will almost always be loaded in full
    // by the time this method is called.  But it doesn't hurt to make
    // certain that they are ready to go.
    JLabel bogusImageObserver = new JLabel();
    MediaTracker t = new MediaTracker(bogusImageObserver);
    int id = 0;
    for (Image i : images) t.addImage(i, id++);
    try {
        t.waitForAll();
    } catch (InterruptedException e) {
    }
    // keep track of the "too big" image closest to the preferred size
    Image bigResult = null;
    int bigDelta = 1000;
    // keep track of the "too small" image closest to the preferred size
    Image smallResult = null;
    int smallDelta = 1000;
    // iterate over the images looking for the best match.
    for (Image image : images) {
        int size = Math.max(image.getWidth(bogusImageObserver), image.getHeight(bogusImageObserver));
        if (size < 0)
            // -1 even after the MediaTracker said it was completely loaded.
            continue;
        else if (size == preferredSize)
            // we've found a perfect match!  Return it.
            return image;
        else if (size < preferredSize) {
            // this image is too small.  But see if it is closer to the
            // preferred size than the best small image we've seen so far.
            int delta = preferredSize - size;
            if (delta < smallDelta) {
                smallResult = image;
                smallDelta = delta;
            }
        } else {
            // this image is too big.  But see if it is closer to the
            // preferred size than the best big image we've seen so far.
            int delta = size - preferredSize;
            if (delta < bigDelta) {
                bigResult = image;
                bigDelta = delta;
            }
        }
    }
    if (preferSmaller) {
        return (smallResult != null ? smallResult : bigResult);
    } else {
        return (bigResult != null ? bigResult : smallResult);
    }
}
Also used : MediaTracker(java.awt.MediaTracker) JLabel(javax.swing.JLabel) Image(java.awt.Image)

Example 22 with MediaTracker

use of java.awt.MediaTracker in project beast2 by CompEvol.

the class Utils6 method startSplashScreen.

/*
        This could live in the desktop script.
        However we'd like to get it on the screen as quickly as possible.
    */
public static void startSplashScreen() {
    Image img = getIcon("beast/app/draw/icons/beauti.png").getImage();
    int width = 2 * img.getWidth(null), height = img.getHeight(null);
    Window win = new Window(new Frame());
    win.pack();
    can = new Canvas();
    // why is this necessary?
    can.setSize(width, height);
    Toolkit tk = Toolkit.getDefaultToolkit();
    Dimension dim = tk.getScreenSize();
    win.setBounds(dim.width / 2 - width / 2, dim.height / 2 - height / 2, width, height);
    win.add("Center", can);
    // Image img=tk.getImage(
    // Utils.class.getResource("beast.png") ); //what
    MediaTracker mt = new MediaTracker(can);
    mt.addImage(img, 0);
    try {
        mt.waitForAll();
    } catch (Exception e) {
    }
    Graphics gr = can.getBufferedGraphics();
    gr.drawImage(img, width / 4, 0, can);
    win.setVisible(true);
    win.toFront();
    splashScreen = win;
}
Also used : Window(java.awt.Window) Graphics(java.awt.Graphics) Frame(java.awt.Frame) MediaTracker(java.awt.MediaTracker) Toolkit(java.awt.Toolkit) Dimension(java.awt.Dimension) Image(java.awt.Image) IOException(java.io.IOException)

Example 23 with MediaTracker

use of java.awt.MediaTracker in project cytoscape-impl by cytoscape.

the class ImageUtil method toBufferedImage.

public static BufferedImage toBufferedImage(final Image image) throws InterruptedException {
    if (image instanceof BufferedImage)
        return (BufferedImage) image;
    MediaTracker tracker = new MediaTracker(new Component() {
    });
    tracker.addImage(image, 0);
    tracker.waitForAll();
    PixelGrabber pixelGrabber = new PixelGrabber(image, 0, 0, -1, -1, false);
    pixelGrabber.grabPixels();
    ColorModel cm = pixelGrabber.getColorModel();
    final int w = pixelGrabber.getWidth();
    final int h = pixelGrabber.getHeight();
    WritableRaster raster = cm.createCompatibleWritableRaster(w, h);
    final BufferedImage renderedImage = new BufferedImage(cm, raster, cm.isAlphaPremultiplied(), new Hashtable());
    renderedImage.getRaster().setDataElements(0, 0, w, h, pixelGrabber.getPixels());
    return renderedImage;
}
Also used : MediaTracker(java.awt.MediaTracker) ColorModel(java.awt.image.ColorModel) WritableRaster(java.awt.image.WritableRaster) Hashtable(java.util.Hashtable) PixelGrabber(java.awt.image.PixelGrabber) Component(java.awt.Component) BufferedImage(java.awt.image.BufferedImage)

Example 24 with MediaTracker

use of java.awt.MediaTracker in project triplea by triplea-game.

the class TileImageBreaker method loadImage.

/**
 * Asks the user to select an image and then it loads it up into an Image
 * object and returns it to the calling class.
 *
 * @return The loaded image.
 */
private Image loadImage() {
    ToolLogger.info("Select the map");
    final String mapName = new FileOpen("Select The Map", mapFolderLocation, ".gif", ".png").getPathString();
    if (mapName != null) {
        final Image img = Toolkit.getDefaultToolkit().createImage(mapName);
        final MediaTracker tracker = new MediaTracker(new Panel());
        tracker.addImage(img, 1);
        try {
            tracker.waitForAll();
            return img;
        } catch (final InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }
    return null;
}
Also used : Panel(java.awt.Panel) MediaTracker(java.awt.MediaTracker) Image(java.awt.Image) BufferedImage(java.awt.image.BufferedImage)

Example 25 with MediaTracker

use of java.awt.MediaTracker in project triplea by triplea-game.

the class Util method ensureImageLoaded.

public static void ensureImageLoaded(final Image anImage) {
    final MediaTracker tracker = new MediaTracker(component);
    tracker.addImage(anImage, 1);
    try {
        tracker.waitForAll();
        tracker.removeImage(anImage);
    } catch (final InterruptedException ignored) {
        Thread.currentThread().interrupt();
    }
}
Also used : MediaTracker(java.awt.MediaTracker)

Aggregations

MediaTracker (java.awt.MediaTracker)30 Image (java.awt.Image)17 BufferedImage (java.awt.image.BufferedImage)16 Graphics (java.awt.Graphics)7 IOException (java.io.IOException)7 Toolkit (java.awt.Toolkit)5 Container (java.awt.Container)4 JLabel (javax.swing.JLabel)4 Component (java.awt.Component)3 MultiResolutionImage (java.awt.image.MultiResolutionImage)3 File (java.io.File)3 URL (java.net.URL)3 JPanel (javax.swing.JPanel)3 SimpleImage (org.jwildfire.image.SimpleImage)3 Dimension (java.awt.Dimension)2 Graphics2D (java.awt.Graphics2D)2 Panel (java.awt.Panel)2 PixelGrabber (java.awt.image.PixelGrabber)2 BarCodeBuilder (com.aspose.barcode.BarCodeBuilder)1 Frame (java.awt.Frame)1