use of java.awt.MediaTracker in project JSettlers2 by jdmonin.
the class SOCBoardPanel method loadImages.
/**
* Load the images for the board: {@link #hexes}, {@link #rotatHexes}, and {@link #dice}.
* Loads all hex types, up through {@link SOCBoardLarge#FOG_HEX},
* because {@link #hexes} is static for all boards and all game options.
* @param c Our component, to load image resource files with getToolkit and getResource
* @param wantsRotated True for the 6-player non-sea board
* (v2 encoding {@link SOCBoard#BOARD_ENCODING_6PLAYER}), false otherwise.
* The large board (v3 encoding)'s fog-hex and gold-hex images have no rotated version,
* because that board layout is never rotated.
*/
private static synchronized void loadImages(Component c, final boolean wantsRotated) {
if ((hexes != null) && ((rotatHexes != null) || !wantsRotated))
return;
Toolkit tk = c.getToolkit();
Class<?> clazz = c.getClass();
if (hexes == null) {
MediaTracker tracker = new MediaTracker(c);
// water, desert, 5 resources, gold, fog, hex border mask, 3:1 port
hexes = new Image[11];
dice = new Image[14];
loadHexesAndImages(hexes, IMAGEDIR, tracker, tk, clazz, false);
for (int i = 2; i < 13; i++) {
dice[i] = tk.getImage(clazz.getResource(IMAGEDIR + "/dice" + i + ".gif"));
tracker.addImage(dice[i], 0);
}
try {
tracker.waitForID(0);
} catch (InterruptedException e) {
}
if (tracker.isErrorID(0)) {
System.out.println("Error loading board images");
}
}
if (wantsRotated && (rotatHexes == null)) {
MediaTracker tracker = new MediaTracker(c);
// only 9, not 11: large board (gold,fog) is not rotated
rotatHexes = new Image[9];
loadHexesAndImages(rotatHexes, IMAGEDIR + "/rotat", tracker, tk, clazz, true);
try {
tracker.waitForID(0);
} catch (InterruptedException e) {
}
if (tracker.isErrorID(0)) {
System.out.println("Error loading rotated board images");
}
}
}
use of java.awt.MediaTracker in project ma-core-public by infiniteautomation.
the class ImageUtils method waitForImage.
/**
* Waits for the image to be loaded.
*
* @param image
* the image to wait for
* @return the image after loading
* @throws InterruptedException
* if the media tracker was interrupted
*/
private static Image waitForImage(Image image) throws InterruptedException {
MediaTracker mediaTracker = new MediaTracker(new Container());
mediaTracker.addImage(image, 0);
mediaTracker.waitForID(0);
return image;
}
use of java.awt.MediaTracker in project openblocks by mikaelhg.
the class ImageManager method createImage.
public BufferedImage createImage(String file) {
Toolkit toolkit = Toolkit.getDefaultToolkit();
URL u = ImageManager.class.getResource(file);
if (u == null) {
System.out.println("Could not find resource " + file);
return null;
}
Image img = toolkit.createImage(u);
if (img == null) {
System.out.println("Couldn't load image " + file);
return null;
}
MediaTracker mt = new MediaTracker(comp);
try {
mt.addImage(img, 0);
mt.waitForAll();
} catch (Exception e) {
System.out.println("Couldn't load image " + file);
System.out.println(e);
return null;
}
if (mt.isErrorAny()) {
System.out.println("Couldn't load image " + file);
return null;
}
// ImageObserver observer = new ImageObserver() {
// public boolean imageUpdate(Image img,int flags,int x,int y,int w,int h) {
// if ((flags & (ALLBITS | FRAMEBITS | ABORT)) != 0) {
// synchronized (this) { notify(); }
// return false;
// }
// return true;
// }
// };
// try {
// synchronized (observer) {
// while (!toolkit.prepareImage(img,-1,-1,observer)) { observer.wait(); }
// }
// }
// catch (InterruptedException e) {
// System.out.println("Couldn't load image "+file);
// return null;
// }
// System.out.println("image width "+ img.getWidth(comp) +
// " height " + img.getHeight(comp));
BufferedImage bimg = comp.getGraphicsConfiguration().createCompatibleImage(img.getWidth(comp), img.getHeight(comp), Transparency.TRANSLUCENT);
bimg.getGraphics().drawImage(img, 0, 0, comp);
return bimg;
}
use of java.awt.MediaTracker in project scriptographer by scriptographer.
the class NetUtils method loadImage.
/**
* Load an image from a given URL. This blocks until the image is
* loaded or an error occured.
* @param url the URL of the image to load.
* @return the loaded image
* @throws InterruptedException
*/
public static Image loadImage(URL url) throws InterruptedException {
MediaTracker tracker = new MediaTracker(new java.awt.Container());
Image img = java.awt.Toolkit.getDefaultToolkit().createImage(url);
tracker.addImage(img, 0);
tracker.waitForAll();
return img;
}
use of java.awt.MediaTracker in project cxf by apache.
the class ImageDataContentHandler method convertToBufferedImage.
private static BufferedImage convertToBufferedImage(Image image) throws IOException {
if (image instanceof BufferedImage) {
return (BufferedImage) image;
}
// Wait until the image is completely loaded
MediaTracker tracker = new MediaTracker(new Component() {
private static final long serialVersionUID = 977142547536262901L;
});
tracker.addImage(image, 0);
try {
tracker.waitForAll();
} catch (InterruptedException e) {
throw new IOException(e.getMessage(), e);
}
// Create a BufferedImage so we can write it out later
BufferedImage bufImage = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_ARGB);
Graphics g = bufImage.createGraphics();
g.drawImage(image, 0, 0, null);
g.dispose();
return bufImage;
}
Aggregations