下面列出了java.awt.image.BaseMultiResolutionImage#java.awt.image.MultiResolutionImage 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
static void testToolkitMultiResolutionImageLoad(Image image)
throws Exception {
MediaTracker tracker = new MediaTracker(new JPanel());
tracker.addImage(image, 0);
tracker.waitForID(0);
if (tracker.isErrorAny()) {
throw new RuntimeException("Error during image loading");
}
tracker.removeImage(image, 0);
testImageLoaded(image);
int w = image.getWidth(null);
int h = image.getHeight(null);
Image resolutionVariant = ((MultiResolutionImage) image)
.getResolutionVariant(2 * w, 2 * h);
if (image == resolutionVariant) {
throw new RuntimeException("Resolution variant is not loaded");
}
testImageLoaded(resolutionVariant);
}
static void testToolkitMultiResolutionImageChache(String fileName,
URL url) {
Image img1 = Toolkit.getDefaultToolkit().getImage(fileName);
if (!(img1 instanceof MultiResolutionImage)) {
throw new RuntimeException("Not a MultiResolutionImage");
}
Image img2 = Toolkit.getDefaultToolkit().getImage(fileName);
if (img1 != img2) {
throw new RuntimeException("Image is not cached");
}
img1 = Toolkit.getDefaultToolkit().getImage(url);
if (!(img1 instanceof MultiResolutionImage)) {
throw new RuntimeException("Not a MultiResolutionImage");
}
img2 = Toolkit.getDefaultToolkit().getImage(url);
if (img1 != img2) {
throw new RuntimeException("Image is not cached");
}
}
static void testToolkitMultiResolutionImageLoad(Image image)
throws Exception {
MediaTracker tracker = new MediaTracker(new JPanel());
tracker.addImage(image, 0);
tracker.waitForID(0);
if (tracker.isErrorAny()) {
throw new RuntimeException("Error during image loading");
}
tracker.removeImage(image, 0);
testImageLoaded(image);
int w = image.getWidth(null);
int h = image.getHeight(null);
Image resolutionVariant = ((MultiResolutionImage) image)
.getResolutionVariant(2 * w, 2 * h);
if (image == resolutionVariant) {
throw new RuntimeException("Resolution variant is not loaded");
}
testImageLoaded(resolutionVariant);
}
static void testToolkitMultiResolutionImageChache(String fileName,
URL url) {
Image img1 = Toolkit.getDefaultToolkit().getImage(fileName);
if (!(img1 instanceof MultiResolutionImage)) {
throw new RuntimeException("Not a MultiResolutionImage");
}
Image img2 = Toolkit.getDefaultToolkit().getImage(fileName);
if (img1 != img2) {
throw new RuntimeException("Image is not cached");
}
img1 = Toolkit.getDefaultToolkit().getImage(url);
if (!(img1 instanceof MultiResolutionImage)) {
throw new RuntimeException("Not a MultiResolutionImage");
}
img2 = Toolkit.getDefaultToolkit().getImage(url);
if (img1 != img2) {
throw new RuntimeException("Image is not cached");
}
}
@Test
public void baseMultiResImageTest() {
int baseIndex = 1;
int length = 4;
BufferedImage[] resolutionVariants = new BufferedImage[length];
for (int i = 0; i < length; i++) {
resolutionVariants[i] = createImage(i);
}
MultiResolutionImage bmrImage = new BaseMultiResolutionImage(baseIndex, resolutionVariants);
List<Image> rvImageList = bmrImage.getResolutionVariants();
assertEquals("MultiResoltion Image shoudl contain the same number of resolution variants!", rvImageList.size(), length);
for (int i = 0; i < length; i++) {
int imageSize = getSize(i);
Image testRVImage = bmrImage.getResolutionVariant(imageSize, imageSize);
assertSame("Images should be the same", testRVImage, resolutionVariants[i]);
}
}
@Override
public List<Image> getResolutionVariants() {
List<Image> variants = ((MultiResolutionImage)mrImage).getResolutionVariants();
List<Image> mappedVariants = new ArrayList<>();
for( Image image : variants )
mappedVariants.add( mapAndCacheImage( image ) );
return mappedVariants;
}
protected Image getImageWithResolutionVariant(String fileName,
String resolutionVariantName) {
synchronized (fileImgCache) {
Image image = getImageFromHash(this, fileName);
if (image instanceof MultiResolutionImage) {
return image;
}
Image resolutionVariant = getImageFromHash(this, resolutionVariantName);
image = createImageWithResolutionVariant(image, resolutionVariant);
fileImgCache.put(fileName, image);
return image;
}
}
protected Image getImageWithResolutionVariant(URL url,
URL resolutionVariantURL) {
synchronized (urlImgCache) {
Image image = getImageFromHash(this, url);
if (image instanceof MultiResolutionImage) {
return image;
}
Image resolutionVariant = getImageFromHash(this, resolutionVariantURL);
image = createImageWithResolutionVariant(image, resolutionVariant);
String key = url.toString();
urlImgCache.put(key, image);
return image;
}
}
public static Image map(MultiResolutionImage mrImage,
Function<Image, Image> mapper) {
if (mrImage instanceof MultiResolutionToolkitImage) {
MultiResolutionToolkitImage mrtImage =
(MultiResolutionToolkitImage) mrImage;
return MultiResolutionToolkitImage.map(mrtImage, mapper);
}
BiFunction<Integer, Integer, Image> sizeMapper
= (w, h) -> mapper.apply(mrImage.getResolutionVariant(w, h));
if (mrImage instanceof MultiResolutionCachedImage) {
MultiResolutionCachedImage mrcImage
= (MultiResolutionCachedImage) mrImage;
return new MultiResolutionCachedImage(mrcImage.baseImageWidth,
mrcImage.baseImageHeight,
mrcImage.sizes,
sizeMapper,
false);
}
Image image = (Image) mrImage;
int width = image.getWidth(null);
int height = image.getHeight(null);
return new MultiResolutionCachedImage(width, height, sizeMapper);
}
protected Image getImageWithResolutionVariant(String fileName,
String resolutionVariantName) {
synchronized (fileImgCache) {
Image image = getImageFromHash(this, fileName);
if (image instanceof MultiResolutionImage) {
return image;
}
Image resolutionVariant = getImageFromHash(this, resolutionVariantName);
image = createImageWithResolutionVariant(image, resolutionVariant);
fileImgCache.put(fileName, image);
return image;
}
}
protected Image getImageWithResolutionVariant(URL url,
URL resolutionVariantURL) {
synchronized (urlImgCache) {
Image image = getImageFromHash(this, url);
if (image instanceof MultiResolutionImage) {
return image;
}
Image resolutionVariant = getImageFromHash(this, resolutionVariantURL);
image = createImageWithResolutionVariant(image, resolutionVariant);
String key = url.toString();
urlImgCache.put(key, image);
return image;
}
}
public static Image map(MultiResolutionImage mrImage,
Function<Image, Image> mapper) {
if (mrImage instanceof MultiResolutionToolkitImage) {
MultiResolutionToolkitImage mrtImage =
(MultiResolutionToolkitImage) mrImage;
return MultiResolutionToolkitImage.map(mrtImage, mapper);
}
BiFunction<Integer, Integer, Image> sizeMapper
= (w, h) -> mapper.apply(mrImage.getResolutionVariant(w, h));
if (mrImage instanceof MultiResolutionCachedImage) {
MultiResolutionCachedImage mrcImage
= (MultiResolutionCachedImage) mrImage;
return new MultiResolutionCachedImage(mrcImage.baseImageWidth,
mrcImage.baseImageHeight,
mrcImage.sizes,
sizeMapper,
false);
}
Image image = (Image) mrImage;
int width = image.getWidth(null);
int height = image.getHeight(null);
return new MultiResolutionCachedImage(width, height, sizeMapper);
}
private CImage createFromImage(final Image image, final boolean prepareImage) {
if (image instanceof MultiResolutionImage) {
List<Image> resolutionVariants
= ((MultiResolutionImage) image).getResolutionVariants();
return createFromImages(resolutionVariants, prepareImage);
}
int[] buffer = imageToArray(image, prepareImage);
if (buffer == null) {
return null;
}
return new CImage(nativeCreateNSImageFromArray(buffer, image.getWidth(null), image.getHeight(null)));
}
public static void main(String[] args) throws Exception {
Image baseMRImage = new BaseMultiResolutionImage(createImage(1),
createImage(2));
testMRDisabledImage(baseMRImage);
saveImages();
Image toolkitMRImage = Toolkit.getDefaultToolkit().getImage(IMAGE_NAME_1X);
if (toolkitMRImage instanceof MultiResolutionImage) {
testMRDisabledImage(toolkitMRImage);
}
}
public static void main(String[] args) throws Exception {
if (OSInfo.getOSType() != OSInfo.OSType.MACOSX) {
return;
}
String icon = "NSImage://NSApplicationIcon";
final Image image = Toolkit.getDefaultToolkit().getImage(icon);
if (!(image instanceof MultiResolutionImage)) {
throw new RuntimeException("Icon does not have resolution variants!");
}
MultiResolutionImage multiResolutionImage = (MultiResolutionImage) image;
int width = 0;
int height = 0;
for (Image resolutionVariant : multiResolutionImage.getResolutionVariants()) {
int rvWidth = resolutionVariant.getWidth(null);
int rvHeight = resolutionVariant.getHeight(null);
if (rvWidth < width || rvHeight < height) {
throw new RuntimeException("Resolution variants are not sorted!");
}
width = rvWidth;
height = rvHeight;
}
}
static void testRVSizes() {
int imageSize = getSize(1);
double[][] sizeArray = {
{-imageSize, imageSize},
{2 * imageSize, -2 * imageSize},
{Double.POSITIVE_INFINITY, imageSize},
{Double.POSITIVE_INFINITY, -imageSize},
{imageSize, Double.NEGATIVE_INFINITY},
{-imageSize, Double.NEGATIVE_INFINITY},
{Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY},
{Double.NaN, imageSize},
{imageSize, Double.NaN},
{Double.NaN, Double.NaN},
{Double.POSITIVE_INFINITY, Double.NaN}
};
for (double[] sizes : sizeArray) {
try {
MultiResolutionImage mrImage = new BaseMultiResolutionImage(
0, createRVImage(0), createRVImage(1));
mrImage.getResolutionVariant(sizes[0], sizes[1]);
} catch (IllegalArgumentException ignored) {
continue;
}
throw new RuntimeException("IllegalArgumentException is not thrown!");
}
}
private static BufferedImage getBufferedImage(Image image) {
if (image instanceof MultiResolutionImage) {
MultiResolutionImage mrImage = (MultiResolutionImage) image;
return (BufferedImage) mrImage.getResolutionVariant(32, 32);
}
return (BufferedImage) image;
}
protected Image getImageWithResolutionVariant(String fileName,
String resolutionVariantName) {
synchronized (fileImgCache) {
Image image = getImageFromHash(this, fileName);
if (image instanceof MultiResolutionImage) {
return image;
}
Image resolutionVariant = getImageFromHash(this, resolutionVariantName);
image = createImageWithResolutionVariant(image, resolutionVariant);
fileImgCache.put(fileName, image);
return image;
}
}
protected Image getImageWithResolutionVariant(URL url,
URL resolutionVariantURL) {
synchronized (urlImgCache) {
Image image = getImageFromHash(this, url);
if (image instanceof MultiResolutionImage) {
return image;
}
Image resolutionVariant = getImageFromHash(this, resolutionVariantURL);
image = createImageWithResolutionVariant(image, resolutionVariant);
String key = url.toString();
urlImgCache.put(key, image);
return image;
}
}
private CImage createFromImage(final Image image, final boolean prepareImage) {
if (image instanceof MultiResolutionImage) {
List<Image> resolutionVariants
= ((MultiResolutionImage) image).getResolutionVariants();
return createFromImages(resolutionVariants, prepareImage);
}
int[] buffer = imageToArray(image, prepareImage);
if (buffer == null) {
return null;
}
return new CImage(nativeCreateNSImageFromArray(buffer, image.getWidth(null), image.getHeight(null)));
}
public static void main(String[] args) throws Exception {
Image baseMRImage = new BaseMultiResolutionImage(createImage(1),
createImage(2));
testMRDisabledImage(baseMRImage);
saveImages();
Image toolkitMRImage = Toolkit.getDefaultToolkit().getImage(IMAGE_NAME_1X);
if (toolkitMRImage instanceof MultiResolutionImage) {
testMRDisabledImage(toolkitMRImage);
}
}
public static void main(String[] args) throws Exception {
if (OSInfo.getOSType() != OSInfo.OSType.MACOSX) {
return;
}
String icon = "NSImage://NSApplicationIcon";
final Image image = Toolkit.getDefaultToolkit().getImage(icon);
if (!(image instanceof MultiResolutionImage)) {
throw new RuntimeException("Icon does not have resolution variants!");
}
MultiResolutionImage multiResolutionImage = (MultiResolutionImage) image;
int width = 0;
int height = 0;
for (Image resolutionVariant : multiResolutionImage.getResolutionVariants()) {
int rvWidth = resolutionVariant.getWidth(null);
int rvHeight = resolutionVariant.getHeight(null);
if (rvWidth < width || rvHeight < height) {
throw new RuntimeException("Resolution variants are not sorted!");
}
width = rvWidth;
height = rvHeight;
}
}
static void testRVSizes() {
int imageSize = getSize(1);
double[][] sizeArray = {
{-imageSize, imageSize},
{2 * imageSize, -2 * imageSize},
{Double.POSITIVE_INFINITY, imageSize},
{Double.POSITIVE_INFINITY, -imageSize},
{imageSize, Double.NEGATIVE_INFINITY},
{-imageSize, Double.NEGATIVE_INFINITY},
{Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY},
{Double.NaN, imageSize},
{imageSize, Double.NaN},
{Double.NaN, Double.NaN},
{Double.POSITIVE_INFINITY, Double.NaN}
};
for (double[] sizes : sizeArray) {
try {
MultiResolutionImage mrImage = new BaseMultiResolutionImage(
0, createRVImage(0), createRVImage(1));
mrImage.getResolutionVariant(sizes[0], sizes[1]);
} catch (IllegalArgumentException ignored) {
continue;
}
throw new RuntimeException("IllegalArgumentException is not thrown!");
}
}
public static void main(String[] args) throws IOException {
MultiResolutionImage tokio = loadTokio();
int desiredImageWidth = ThreadLocalRandom.current().nextInt(1500);
Image variant = tokio.getResolutionVariant(desiredImageWidth, 1);
System.out.printf("Width of image for %d: %d%n", desiredImageWidth, variant.getWidth(null));
}
private static MultiResolutionImage loadTokio() throws IOException {
List<Image> tokios = new ArrayList<>();
for (String url : IMAGE_URLS) {
tokios.add(ImageIO.read(new URL(url)));
}
return new BaseMultiResolutionImage(tokios.toArray(new Image[0]));
}
public static boolean isMultiResolutionImage( Image image ) {
return image instanceof MultiResolutionImage;
}
public static Image map( Image image, Function<Image, Image> mapper ) {
return image instanceof MultiResolutionImage
? new MappedMultiResolutionImage( image, mapper )
: mapper.apply( image );
}
public static Image getResolutionVariant( Image image, int destImageWidth, int destImageHeight ) {
return (image instanceof MultiResolutionImage)
? ((MultiResolutionImage)image).getResolutionVariant( destImageWidth, destImageHeight )
: image;
}
public static List<Image> getResolutionVariants( Image image ) {
return (image instanceof MultiResolutionImage)
? ((MultiResolutionImage)image).getResolutionVariants()
: Collections.singletonList( image );
}
@Override
public Image getResolutionVariant( double destImageWidth, double destImageHeight ) {
Image variant = ((MultiResolutionImage)mrImage).getResolutionVariant( destImageWidth, destImageHeight );
return mapAndCacheImage( variant );
}