< prev index next >
src/java.desktop/share/classes/java/awt/Toolkit.java
Print this page
@@ -32,10 +32,11 @@
import java.awt.event.*;
import java.awt.im.InputMethodHighlight;
import java.awt.image.ColorModel;
import java.awt.image.ImageObserver;
import java.awt.image.ImageProducer;
+import java.awt.image.MultiResolutionImage;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.File;
import java.io.FileInputStream;
@@ -45,11 +46,10 @@
import java.util.HashMap;
import java.util.Map;
import java.util.MissingResourceException;
import java.util.Properties;
import java.util.ResourceBundle;
-import java.util.StringTokenizer;
import java.util.WeakHashMap;
import sun.awt.AWTAccessor;
import sun.awt.AWTPermissions;
import sun.awt.AppContext;
@@ -58,14 +58,17 @@
import sun.awt.SunToolkit;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Arrays;
+import java.util.LinkedList;
import java.util.ServiceLoader;
import java.util.Set;
import java.util.stream.Collectors;
import javax.accessibility.AccessibilityProvider;
+import sun.awt.image.MultiResolutionToolkitImage;
+import sun.awt.image.MultiResolutionToolkitImage.ResolutionVariantItem;
/**
* This class is the abstract superclass of all actual
* implementations of the Abstract Window Toolkit. Subclasses of
* the {@code Toolkit} class are used to bind the various components
@@ -636,10 +639,133 @@
* @see #createImage(java.lang.String)
*/
public abstract Image getImage(String filename);
/**
+ * The MediaResolutionNamingScheme class stores the resolution variant
+ * qualifier as well as scaled factors associated with it.
+ *
+ * @see #getImageUsingNamingSchemes
+ * @see MultiResolutionImage
+ *
+ * @since 9
+ */
+ public static class MediaResolutionNamingScheme {
+
+ private final String qualifier;
+ private final float scale;
+
+ /**
+ * Creates a new instance of MediaResolutionNamingScheme.
+ *
+ * @param qualifier the resolution-variant qualifier
+ * @param scale the scale associated with the resolution variant qualifier
+ *
+ * @since 9
+ */
+ public MediaResolutionNamingScheme(String qualifier, float scale) {
+ this.qualifier = qualifier;
+ this.scale = scale;
+ }
+
+ /**
+ * @return the resolution-variant qualifier
+ *
+ * @since 9
+ */
+
+ public String getQualifier() {
+ return qualifier;
+ }
+
+ /**
+ * @return scale factor
+ *
+ * @since 9
+ */
+ public float getScale() {
+ return scale;
+ }
+ }
+
+ /**
+ * Returns an image with resolution variants based on the provided media
+ * resolution naming scheme. This method is useful when it is necessary to
+ * provide an image with resolution variants for HiDPI displays.
+ * <p>
+ * For example, if HiDPI display supports scales 1.5 and 2 the following steps
+ * can be used to provide an image together with its high resolution variants.
+ * Put scaled versions of the original image in the same directory using
+ * naming convention {@literal @150pct} for scale 1.5 and {@literal @2x}
+ * for scale 2 The qualifier need to be placed between the image
+ * name and extension:
+ * <ul>
+ * <li>{@literal image_name.ext} - original image</li>
+ * <li>{@literal image_name@150pct.ext} - resolution variant for scale 1.5
+ * and qualifier {@literal @150pct}</li>
+ * <li>{@literal image_name@2x.ext} - resolution variant for scale 2 and
+ * qualifier {@literal @2x}</li>
+ * </ul>
+ * Call {@code getImageUsingNamingSchemes)} method providing only path to the
+ * original image and naming schemes which include defined quilifiers and scales.
+ * <pre> {@code
+ * Image image = toolkit.getImageUsingNamingSchemes(fileName,
+ * new Toolkit.MediaResolutionNamingScheme("@150pct", 1.5f),
+ * new Toolkit.MediaResolutionNamingScheme("@2x", 2f)
+ * );
+ * } </pre>
+ * <p>
+ * The resolution variant is not added if an image for the given naming scheme
+ * is missed. If all resolution variants are missed the returned result
+ * will be the same as call to the {@code getImage(fileName)} method.
+ *
+ * @param fileName file path to the original image
+ * @param namingSchemes array of naming scheme that contains resolution
+ * variant qualifier and associated with it scale factor
+ * @return an image with with resolution variants
+ *
+ * @see #getImage
+ * @see MediaResolutionNamingScheme
+ * @see MultiResolutionImage
+ */
+ public Image getImageUsingNamingSchemes(String fileName,
+ MediaResolutionNamingScheme... namingSchemes) {
+
+ Image baseimage = getImage(fileName);
+
+ if (namingSchemes.length == 0
+ || (baseimage instanceof MultiResolutionImage)) {
+ return baseimage;
+ }
+
+ int slash = fileName.lastIndexOf('/');
+ String name = (slash < 0) ? fileName : fileName.substring(slash + 1);
+
+ int dot = name.lastIndexOf('.');
+ String ext = (dot < 0) ? "" : name.substring(dot);
+ name = (dot < 0) ? name : name.substring(0, dot);
+ String prefix = (slash < 0) ? name : fileName.substring(0, slash + 1) + name;
+
+ java.util.List<ResolutionVariantItem> rvItems = new LinkedList<>();
+
+ for (MediaResolutionNamingScheme scheme : namingSchemes) {
+ String rvFileName = prefix + scheme.getQualifier() + ext;
+ if (SunToolkit.imageExists(rvFileName)) {
+ Image rvImage = toolkit.getImage(rvFileName);
+ float scale = scheme.getScale();
+ rvItems.add(new ResolutionVariantItem(rvImage, scale, scale));
+ }
+ }
+
+ if (rvItems.isEmpty()) {
+ return baseimage;
+ }
+
+ return new MultiResolutionToolkitImage(baseimage, rvItems);
+ }
+
+ /**
* Returns an image which gets pixel data from the specified URL.
* The pixel data referenced by the specified URL must be in one
* of the following formats: GIF, JPEG or PNG.
* The underlying toolkit attempts to resolve multiple requests
* with the same URL to the same returned Image.
< prev index next >