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);
}
}
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;
}
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;
}
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;
}
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();
}
}
Aggregations