-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathImageManager.java
More file actions
86 lines (76 loc) · 2.63 KB
/
Copy pathImageManager.java
File metadata and controls
86 lines (76 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// -*- mode: java; c-basic-offset: 2; -*-
// Copyright 2009-2011 Google, All Rights reserved
// Copyright 2011-2012 MIT, All rights reserved
// Released under the MIT License https://raw.github.com/mit-cml/app-inventor/master/mitlicense.txt
package openblocks.codeblockutil;
import java.awt.Component;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Toolkit;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.net.URL;
public class ImageManager {
private Component comp;
public ImageManager(Component comp) {
assert(comp != null);
this.comp = comp;
}
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;
}
public static boolean hitTest(BufferedImage img,int x,int y) {
return ((img.getRGB(x,y) >> 24) & 0xFF) >= 0xFF/2;
}
}