下面列出了怎么用java.awt.image.BaseMultiResolutionImage的API类实例代码及写法,或者点击链接到github查看源代码。
public void start() {
//Get things going. Request focus, set size, et cetera
setSize(200, 200);
setVisible(true);
validate();
final Image image = new BaseMultiResolutionImage(
createResolutionVariant(0),
createResolutionVariant(1),
createResolutionVariant(2),
createResolutionVariant(3)
);
int center = sizes[0] / 2;
Cursor cursor = Toolkit.getDefaultToolkit().createCustomCursor(
image, new Point(center, center), "multi-resolution cursor");
Frame frame = new Frame("Test Frame");
frame.setSize(300, 300);
frame.setLocation(300, 50);
frame.add(new Label("Move cursor here"));
frame.setCursor(cursor);
frame.setVisible(true);
}
private static void createAndShowGUI() {
frame = new JFrame();
frame.setSize(IMAGE_SIZE, IMAGE_SIZE);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new BorderLayout());
BufferedImage img1x = generateImage(1, COLOR_1X);
BufferedImage img2x = generateImage(2, COLOR_2X);
BaseMultiResolutionImage mri = new BaseMultiResolutionImage(
new BufferedImage[]{img1x, img2x});
Icon mrIcon = new ImageIcon(mri);
JToggleButton button = new JToggleButton();
button.setIcon(mrIcon);
panel.add(button, BorderLayout.CENTER);
frame.getContentPane().add(panel);
frame.setVisible(true);
}
@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]);
}
}
public static void main(String[] args) throws Exception {
int length = COLORS.length;
BufferedImage[] resolutionVariants = new BufferedImage[length];
for (int i = 0; i < length; i++) {
resolutionVariants[i] = createRVImage(getSize(i), COLORS[i]);
}
BaseMultiResolutionImage mrImage = new BaseMultiResolutionImage(
resolutionVariants);
// scale 1, transform 1, resolution variant 1
Color color = getImageColor(mrImage, 1, 1);
if (!getColorForScale(1).equals(color)) {
throw new RuntimeException("Wrong resolution variant!");
}
// scale 1, transform 2, resolution variant 2
color = getImageColor(mrImage, 1, 2);
if (!getColorForScale(2).equals(color)) {
throw new RuntimeException("Wrong resolution variant!");
}
// scale 2, transform 1, resolution variant 2
color = getImageColor(mrImage, 2, 1);
if (!getColorForScale(2).equals(color)) {
throw new RuntimeException("Wrong resolution variant!");
}
// scale 2, transform 2, resolution variant 4
color = getImageColor(mrImage, 2, 2);
if (!getColorForScale(4).equals(color)) {
throw new RuntimeException("Wrong resolution variant!");
}
}
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);
}
}
static void testZeroRVIMages() {
try {
new BaseMultiResolutionImage();
} catch (IllegalArgumentException ignored) {
return;
}
throw new RuntimeException("IllegalArgumentException is not thrown!");
}
static void testNullRVIMages() {
try {
new BaseMultiResolutionImage(null);
} catch (IllegalArgumentException ignored) {
return;
}
throw new RuntimeException("IllegalArgumentException is not thrown!");
}
static void testNullRVIMage() {
Image baseImage = new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB);
try {
new BaseMultiResolutionImage(baseImage, null);
} catch (NullPointerException ignored) {
return;
}
throw new RuntimeException("NullPointerException is not thrown!");
}
static void testIOOBException() {
for (int baseImageIndex : new int[]{-3, 2, 4}) {
try {
new BaseMultiResolutionImage(baseImageIndex,
createRVImage(0), createRVImage(1));
} catch (IndexOutOfBoundsException ignored) {
continue;
}
throw new RuntimeException("IndexOutOfBoundsException is not thrown!");
}
}
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 Exception {
int length = COLORS.length;
BufferedImage[] resolutionVariants = new BufferedImage[length];
for (int i = 0; i < length; i++) {
resolutionVariants[i] = createRVImage(getSize(i), COLORS[i]);
}
BaseMultiResolutionImage mrImage = new BaseMultiResolutionImage(
resolutionVariants);
// base
Color color = getImageColor(VALUE_RESOLUTION_VARIANT_BASE, mrImage, 2, 3);
if (!getColorForScale(1).equals(color)) {
throw new RuntimeException("Wrong base resolution variant!");
}
// dpi fit
color = getImageColor(VALUE_RESOLUTION_VARIANT_DPI_FIT, mrImage, 2, 3);
if (!getColorForScale(2).equals(color)) {
throw new RuntimeException("Resolution variant is not based on dpi!");
}
// size fit
color = getImageColor(VALUE_RESOLUTION_VARIANT_SIZE_FIT, mrImage, 2, 3);
if (!getColorForScale(6).equals(color)) {
throw new RuntimeException("Resolution variant is not based on"
+ " rendered size!");
}
// default
// depends on the policies of the platform
// just check that exception is not thrown
getImageColor(VALUE_RESOLUTION_VARIANT_DEFAULT, mrImage, 2, 3);
}
private static Image createTestImage(int width, int height, int colorindex) {
Color color = COLORS[colorindex % COLORS.length];
AffineTransform tx = GraphicsEnvironment
.getLocalGraphicsEnvironment()
.getDefaultScreenDevice()
.getDefaultConfiguration()
.getDefaultTransform();
Image baseImage = createTestImage(width, height, 1, 1, color);
Image rvImage = createTestImage(width, height, tx.getScaleX(), tx.getScaleY(), color);
return new BaseMultiResolutionImage(baseImage, rvImage);
}
/**
* Returns a custom colored highlight image.
* @param player determines the color of the highlight.
* @param size is the edge length in pixels of the image.
* @return the highlighted tile.
*/
public static ImageIcon getColoredHighlight(Player player, int size) {
ImageIcon coloredImage = colorMaskBased(highlightBaseImage, highlightImage, player.getColor());
Image small = coloredImage.getImage().getScaledInstance(size, size, Image.SCALE_SMOOTH);
int largeSize = Math.min(size * 2, GameSettings.TILE_RESOLUTION);
Image large = coloredImage.getImage().getScaledInstance(largeSize, largeSize, Image.SCALE_SMOOTH);
return new ImageIcon(new BaseMultiResolutionImage(small, large));
}
public static void main(String[] args) throws Exception {
int length = COLORS.length;
BufferedImage[] resolutionVariants = new BufferedImage[length];
for (int i = 0; i < length; i++) {
resolutionVariants[i] = createRVImage(getSize(i), COLORS[i]);
}
BaseMultiResolutionImage mrImage = new BaseMultiResolutionImage(
resolutionVariants);
// scale 1, transform 1, resolution variant 1
Color color = getImageColor(mrImage, 1, 1);
if (!getColorForScale(1).equals(color)) {
throw new RuntimeException("Wrong resolution variant!");
}
// scale 1, transform 2, resolution variant 2
color = getImageColor(mrImage, 1, 2);
if (!getColorForScale(2).equals(color)) {
throw new RuntimeException("Wrong resolution variant!");
}
// scale 2, transform 1, resolution variant 2
color = getImageColor(mrImage, 2, 1);
if (!getColorForScale(2).equals(color)) {
throw new RuntimeException("Wrong resolution variant!");
}
// scale 2, transform 2, resolution variant 4
color = getImageColor(mrImage, 2, 2);
if (!getColorForScale(4).equals(color)) {
throw new RuntimeException("Wrong resolution variant!");
}
}
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);
}
}
static void testZeroRVIMages() {
try {
new BaseMultiResolutionImage();
} catch (IllegalArgumentException ignored) {
return;
}
throw new RuntimeException("IllegalArgumentException is not thrown!");
}
static void testNullRVIMages() {
try {
new BaseMultiResolutionImage(null);
} catch (IllegalArgumentException ignored) {
return;
}
throw new RuntimeException("IllegalArgumentException is not thrown!");
}
static void testNullRVIMage() {
Image baseImage = new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB);
try {
new BaseMultiResolutionImage(baseImage, null);
} catch (NullPointerException ignored) {
return;
}
throw new RuntimeException("NullPointerException is not thrown!");
}
static void testIOOBException() {
for (int baseImageIndex : new int[]{-3, 2, 4}) {
try {
new BaseMultiResolutionImage(baseImageIndex,
createRVImage(0), createRVImage(1));
} catch (IndexOutOfBoundsException ignored) {
continue;
}
throw new RuntimeException("IndexOutOfBoundsException is not thrown!");
}
}
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 Exception {
int length = COLORS.length;
BufferedImage[] resolutionVariants = new BufferedImage[length];
for (int i = 0; i < length; i++) {
resolutionVariants[i] = createRVImage(getSize(i), COLORS[i]);
}
BaseMultiResolutionImage mrImage = new BaseMultiResolutionImage(
resolutionVariants);
// base
Color color = getImageColor(VALUE_RESOLUTION_VARIANT_BASE, mrImage, 2, 3);
if (!getColorForScale(1).equals(color)) {
throw new RuntimeException("Wrong base resolution variant!");
}
// dpi fit
color = getImageColor(VALUE_RESOLUTION_VARIANT_DPI_FIT, mrImage, 2, 3);
if (!getColorForScale(2).equals(color)) {
throw new RuntimeException("Resolution variant is not based on dpi!");
}
// size fit
color = getImageColor(VALUE_RESOLUTION_VARIANT_SIZE_FIT, mrImage, 2, 3);
if (!getColorForScale(6).equals(color)) {
throw new RuntimeException("Resolution variant is not based on"
+ " rendered size!");
}
// default
// depends on the policies of the platform
// just check that exception is not thrown
getImageColor(VALUE_RESOLUTION_VARIANT_DEFAULT, mrImage, 2, 3);
}
private static Image createTestImage(int width, int height, int colorindex) {
Color color = COLORS[colorindex % COLORS.length];
AffineTransform tx = GraphicsEnvironment
.getLocalGraphicsEnvironment()
.getDefaultScreenDevice()
.getDefaultConfiguration()
.getDefaultTransform();
Image baseImage = createTestImage(width, height, 1, 1, color);
Image rvImage = createTestImage(width, height, tx.getScaleX(), tx.getScaleY(), color);
return new BaseMultiResolutionImage(baseImage, rvImage);
}
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 Image create( int baseImageIndex, Image... resolutionVariants ) {
return new BaseMultiResolutionImage( baseImageIndex, resolutionVariants );
}
private void UI() {
setUndecorated(true);
BufferedImage img1x = generateImage(SZ / 2, C1X);
BufferedImage img2x = generateImage(SZ, C2X);
BaseMultiResolutionImage mri = new BaseMultiResolutionImage(
new BufferedImage[]{img1x, img2x});
Icon icon = new ImageIcon(mri);
// hardcoded icon size for OS X (Mac OS X L&F) - see JDK-8151060
BufferedImage tab1x = generateImage(16, C1X);
BufferedImage tab2x = generateImage(32, C2X);
BaseMultiResolutionImage tabMRI = new BaseMultiResolutionImage(
new BufferedImage[]{tab1x, tab2x});
Icon tabIcon = new ImageIcon(tabMRI);
setSize((N + 1) * SZ, SZ);
setLocation(50, 50);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(new GridLayout(1, 1));
JPanel p = new JPanel();
p.setLayout(new GridLayout(1, N));
JButton btn = new JButton(icon);
p.add(btn);
JToggleButton tbn = new JToggleButton(icon);
p.add(tbn);
JRadioButton rbn = new JRadioButton(icon);
rbn.setHorizontalAlignment(SwingConstants.CENTER);
p.add(rbn);
JCheckBox cbx = new JCheckBox(icon);
cbx.setHorizontalAlignment(SwingConstants.CENTER);
p.add(cbx);
lbl = new JLabel(icon);
p.add(lbl);
tabbedPane = new JTabbedPane(JTabbedPane.LEFT);
tabbedPane.addTab("", tabIcon, p);
getContentPane().add(tabbedPane);
setResizable(false);
setVisible(true);
}
private static BaseMultiResolutionImage createIcon(int w, int h) {
return new BaseMultiResolutionImage(
new BufferedImage[]{generateImage(w, h, 1, Color.RED),
generateImage(w, h, 2, Color.GREEN)});
}
private void UI() {
setUndecorated(true);
BufferedImage img1x = generateImage(SZ / 2, C1X);
BufferedImage img2x = generateImage(SZ, C2X);
BaseMultiResolutionImage mri = new BaseMultiResolutionImage(
new BufferedImage[]{img1x, img2x});
Icon icon = new ImageIcon(mri);
// hardcoded icon size for OS X (Mac OS X L&F) - see JDK-8151060
BufferedImage tab1x = generateImage(16, C1X);
BufferedImage tab2x = generateImage(32, C2X);
BaseMultiResolutionImage tabMRI = new BaseMultiResolutionImage(
new BufferedImage[]{tab1x, tab2x});
Icon tabIcon = new ImageIcon(tabMRI);
setSize((N + 1) * SZ, SZ);
setLocation(50, 50);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(new GridLayout(1, 1));
JPanel p = new JPanel();
p.setLayout(new GridLayout(1, N));
JButton btn = new JButton(icon);
p.add(btn);
JToggleButton tbn = new JToggleButton(icon);
p.add(tbn);
JRadioButton rbn = new JRadioButton(icon);
rbn.setHorizontalAlignment(SwingConstants.CENTER);
p.add(rbn);
JCheckBox cbx = new JCheckBox(icon);
cbx.setHorizontalAlignment(SwingConstants.CENTER);
p.add(cbx);
lbl = new JLabel(icon);
p.add(lbl);
tabbedPane = new JTabbedPane(JTabbedPane.LEFT);
tabbedPane.addTab("", tabIcon, p);
getContentPane().add(tabbedPane);
setResizable(false);
setVisible(true);
}
private static BaseMultiResolutionImage createIcon(int w, int h) {
return new BaseMultiResolutionImage(
new BufferedImage[]{generateImage(w, h, 1, Color.RED),
generateImage(w, h, 2, Color.GREEN)});
}
/**
* Creates an image containing pixels read from the screen.
* This image does not include the mouse cursor.
* This method can be used in case there is a scaling transform
* from user space to screen (device) space.
* Typically this means that the display is a high resolution screen,
* although strictly it means any case in which there is such a transform.
* Returns a {@link java.awt.image.MultiResolutionImage}.
* <p>
* For a non-scaled display, the {@code MultiResolutionImage}
* will have one image variant:
* <ul>
* <li> Base Image with user specified size.
* </ul>
* <p>
* For a high resolution display where there is a scaling transform,
* the {@code MultiResolutionImage} will have two image variants:
* <ul>
* <li> Base Image with user specified size. This is scaled from the screen.
* <li> Native device resolution image with device size pixels.
* </ul>
* <p>
* Example:
* <pre>{@code
* Image nativeResImage;
* MultiResolutionImage mrImage = robot.createMultiResolutionScreenCapture(frame.getBounds());
* List<Image> resolutionVariants = mrImage.getResolutionVariants();
* if (resolutionVariants.size() > 1) {
* nativeResImage = resolutionVariants.get(1);
* } else {
* nativeResImage = resolutionVariants.get(0);
* }
* }</pre>
* @param screenRect Rect to capture in screen coordinates
* @return The captured image
* @throws IllegalArgumentException if {@code screenRect} width and height are not greater than zero
* @throws SecurityException if {@code readDisplayPixels} permission is not granted
* @see SecurityManager#checkPermission
* @see AWTPermission
*
* @since 9
*/
public synchronized MultiResolutionImage
createMultiResolutionScreenCapture(Rectangle screenRect) {
return new BaseMultiResolutionImage(
createCompatibleImage(screenRect, true));
}
/**
* Creates an image containing pixels read from the screen.
* This image does not include the mouse cursor.
* This method can be used in case there is a scaling transform
* from user space to screen (device) space.
* Typically this means that the display is a high resolution screen,
* although strictly it means any case in which there is such a transform.
* Returns a {@link java.awt.image.MultiResolutionImage}.
* <p>
* For a non-scaled display, the {@code MultiResolutionImage}
* will have one image variant:
* <ul>
* <li> Base Image with user specified size.
* </ul>
* <p>
* For a high resolution display where there is a scaling transform,
* the {@code MultiResolutionImage} will have two image variants:
* <ul>
* <li> Base Image with user specified size. This is scaled from the screen.
* <li> Native device resolution image with device size pixels.
* </ul>
* <p>
* Example:
* <pre>{@code
* Image nativeResImage;
* MultiResolutionImage mrImage = robot.createMultiResolutionScreenCapture(frame.getBounds());
* List<Image> resolutionVariants = mrImage.getResolutionVariants();
* if (resolutionVariants.size() > 1) {
* nativeResImage = resolutionVariants.get(1);
* } else {
* nativeResImage = resolutionVariants.get(0);
* }
* }</pre>
* @param screenRect Rect to capture in screen coordinates
* @return The captured image
* @throws IllegalArgumentException if {@code screenRect} width and height are not greater than zero
* @throws SecurityException if {@code readDisplayPixels} permission is not granted
* @see SecurityManager#checkPermission
* @see AWTPermission
*
* @since 9
*/
public synchronized MultiResolutionImage
createMultiResolutionScreenCapture(Rectangle screenRect) {
return new BaseMultiResolutionImage(
createCompatibleImage(screenRect, true));
}