下面列出了java.awt.image.MultiResolutionImage#getResolutionVariant() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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]);
}
}
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;
}
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));
}
static void testToolkitMultiResolutionImage(Image image,
boolean enableImageScaling) 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");
}
final BufferedImage bufferedImage1x = new BufferedImage(IMAGE_WIDTH,
IMAGE_HEIGHT, BufferedImage.TYPE_INT_RGB);
Graphics2D g1x = (Graphics2D) bufferedImage1x.getGraphics();
setImageScalingHint(g1x, false);
g1x.drawImage(image, 0, 0, null);
checkColor(bufferedImage1x.getRGB(3 * IMAGE_WIDTH / 4,
3 * IMAGE_HEIGHT / 4), false);
Image resolutionVariant = ((MultiResolutionImage) image).
getResolutionVariant(2 * IMAGE_WIDTH, 2 * IMAGE_HEIGHT);
if (resolutionVariant == null) {
throw new RuntimeException("Resolution variant is null");
}
MediaTracker tracker2x = new MediaTracker(new JPanel());
tracker2x.addImage(resolutionVariant, 0);
tracker2x.waitForID(0);
if (tracker2x.isErrorAny()) {
throw new RuntimeException("Error during scalable image loading");
}
final BufferedImage bufferedImage2x = new BufferedImage(2 * IMAGE_WIDTH,
2 * IMAGE_HEIGHT, BufferedImage.TYPE_INT_RGB);
Graphics2D g2x = (Graphics2D) bufferedImage2x.getGraphics();
setImageScalingHint(g2x, enableImageScaling);
g2x.drawImage(image, 0, 0, 2 * IMAGE_WIDTH,
2 * IMAGE_HEIGHT, 0, 0, IMAGE_WIDTH, IMAGE_HEIGHT, null);
checkColor(bufferedImage2x.getRGB(3 * IMAGE_WIDTH / 2,
3 * IMAGE_HEIGHT / 2), enableImageScaling);
if (!(image instanceof MultiResolutionImage)) {
throw new RuntimeException("Not a MultiResolutionImage");
}
MultiResolutionImage multiResolutionImage
= (MultiResolutionImage) image;
Image image1x = multiResolutionImage.getResolutionVariant(
IMAGE_WIDTH, IMAGE_HEIGHT);
Image image2x = multiResolutionImage.getResolutionVariant(
2 * IMAGE_WIDTH, 2 * IMAGE_HEIGHT);
if (image1x.getWidth(null) * 2 != image2x.getWidth(null)
|| image1x.getHeight(null) * 2 != image2x.getHeight(null)) {
throw new RuntimeException("Wrong resolution variant size");
}
}
static void testToolkitMultiResolutionImage(Image image,
boolean enableImageScaling) 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");
}
final BufferedImage bufferedImage1x = new BufferedImage(IMAGE_WIDTH,
IMAGE_HEIGHT, BufferedImage.TYPE_INT_RGB);
Graphics2D g1x = (Graphics2D) bufferedImage1x.getGraphics();
setImageScalingHint(g1x, false);
g1x.drawImage(image, 0, 0, null);
checkColor(bufferedImage1x.getRGB(3 * IMAGE_WIDTH / 4,
3 * IMAGE_HEIGHT / 4), false);
Image resolutionVariant = ((MultiResolutionImage) image).
getResolutionVariant(2 * IMAGE_WIDTH, 2 * IMAGE_HEIGHT);
if (resolutionVariant == null) {
throw new RuntimeException("Resolution variant is null");
}
MediaTracker tracker2x = new MediaTracker(new JPanel());
tracker2x.addImage(resolutionVariant, 0);
tracker2x.waitForID(0);
if (tracker2x.isErrorAny()) {
throw new RuntimeException("Error during scalable image loading");
}
final BufferedImage bufferedImage2x = new BufferedImage(2 * IMAGE_WIDTH,
2 * IMAGE_HEIGHT, BufferedImage.TYPE_INT_RGB);
Graphics2D g2x = (Graphics2D) bufferedImage2x.getGraphics();
setImageScalingHint(g2x, enableImageScaling);
g2x.drawImage(image, 0, 0, 2 * IMAGE_WIDTH,
2 * IMAGE_HEIGHT, 0, 0, IMAGE_WIDTH, IMAGE_HEIGHT, null);
checkColor(bufferedImage2x.getRGB(3 * IMAGE_WIDTH / 2,
3 * IMAGE_HEIGHT / 2), enableImageScaling);
if (!(image instanceof MultiResolutionImage)) {
throw new RuntimeException("Not a MultiResolutionImage");
}
MultiResolutionImage multiResolutionImage
= (MultiResolutionImage) image;
Image image1x = multiResolutionImage.getResolutionVariant(
IMAGE_WIDTH, IMAGE_HEIGHT);
Image image2x = multiResolutionImage.getResolutionVariant(
2 * IMAGE_WIDTH, 2 * IMAGE_HEIGHT);
if (image1x.getWidth(null) * 2 != image2x.getWidth(null)
|| image1x.getHeight(null) * 2 != image2x.getHeight(null)) {
throw new RuntimeException("Wrong resolution variant size");
}
}