src/share/classes/sun/awt/SunToolkit.java

Print this page




 470         AWTAccessor.SequencedEventAccessor sea = AWTAccessor.getSequencedEventAccessor();
 471         if (sea != null && sea.isSequencedEvent(event)) {
 472             AWTEvent nested = sea.getNested(event);
 473             if (nested.getID() == WindowEvent.WINDOW_LOST_FOCUS &&
 474                 nested instanceof TimedWindowEvent)
 475             {
 476                 TimedWindowEvent twe = (TimedWindowEvent)nested;
 477                 ((SunToolkit)Toolkit.getDefaultToolkit()).
 478                     setWindowDeactivationTime((Window)twe.getSource(), twe.getWhen());
 479             }
 480         }
 481 
 482         // All events posted via this method are system-generated.
 483         // Placing the following call here reduces considerably the
 484         // number of places throughout the toolkit that would
 485         // otherwise have to be modified to precisely identify
 486         // system-generated events.
 487         setSystemGenerated(event);
 488         AppContext eventContext = targetToAppContext(event.getSource());
 489         if (eventContext != null && !eventContext.equals(appContext)) {
 490             if (log.isLoggable(PlatformLogger.FINE)) {
 491                 log.fine("Event posted on wrong app context : " + event);
 492             }
 493         }
 494         PostEventQueue postEventQueue =
 495             (PostEventQueue)appContext.get(POST_EVENT_QUEUE_KEY);
 496         if (postEventQueue != null) {
 497             postEventQueue.postEvent(event);
 498         }
 499     }
 500 
 501     /*
 502      * Post AWTEvent of high priority.
 503      */
 504     public static void postPriorityEvent(final AWTEvent e) {
 505         PeerEvent pe = new PeerEvent(Toolkit.getDefaultToolkit(), new Runnable() {
 506                 public void run() {
 507                     AWTAccessor.getAWTEventAccessor().setPosted(e);
 508                     ((Component)e.getSource()).dispatchEvent(e);
 509                 }
 510             }, PeerEvent.ULTIMATE_PRIORITY_EVENT);


 863      * Scans {@code imageList} for best-looking image of specified dimensions.
 864      * Image can be scaled and/or padded with transparency.
 865      */
 866     public static BufferedImage getScaledIconImage(java.util.List<Image> imageList, int width, int height) {
 867         if (width == 0 || height == 0) {
 868             return null;
 869         }
 870         Image bestImage = null;
 871         int bestWidth = 0;
 872         int bestHeight = 0;
 873         double bestSimilarity = 3; //Impossibly high value
 874         double bestScaleFactor = 0;
 875         for (Iterator<Image> i = imageList.iterator();i.hasNext();) {
 876             //Iterate imageList looking for best matching image.
 877             //'Similarity' measure is defined as good scale factor and small insets.
 878             //best possible similarity is 0 (no scale, no insets).
 879             //It's found while the experiments that good-looking result is achieved
 880             //with scale factors x1, x3/4, x2/3, xN, x1/N.
 881             Image im = i.next();
 882             if (im == null) {
 883                 if (log.isLoggable(PlatformLogger.FINER)) {
 884                     log.finer("SunToolkit.getScaledIconImage: " +
 885                               "Skipping the image passed into Java because it's null.");
 886                 }
 887                 continue;
 888             }
 889             if (im instanceof ToolkitImage) {
 890                 ImageRepresentation ir = ((ToolkitImage)im).getImageRep();
 891                 ir.reconstruct(ImageObserver.ALLBITS);
 892             }
 893             int iw;
 894             int ih;
 895             try {
 896                 iw = im.getWidth(null);
 897                 ih = im.getHeight(null);
 898             } catch (Exception e){
 899                 if (log.isLoggable(PlatformLogger.FINER)) {
 900                     log.finer("SunToolkit.getScaledIconImage: " +
 901                               "Perhaps the image passed into Java is broken. Skipping this icon.");
 902                 }
 903                 continue;
 904             }
 905             if (iw > 0 && ih > 0) {
 906                 //Calc scale factor
 907                 double scaleFactor = Math.min((double)width / (double)iw,
 908                                               (double)height / (double)ih);
 909                 //Calculate scaled image dimensions
 910                 //adjusting scale factor to nearest "good" value
 911                 int adjw = 0;
 912                 int adjh = 0;
 913                 double scaleMeasure = 1; //0 - best (no) scale, 1 - impossibly bad
 914                 if (scaleFactor >= 2) {
 915                     //Need to enlarge image more than twice
 916                     //Round down scale factor to multiply by integer value
 917                     scaleFactor = Math.floor(scaleFactor);
 918                     adjw = iw * (int)scaleFactor;
 919                     adjh = ih * (int)scaleFactor;


 954                     bestScaleFactor = scaleFactor;
 955                     bestImage = im;
 956                     bestWidth = adjw;
 957                     bestHeight = adjh;
 958                 }
 959                 if (similarity == 0) break;
 960             }
 961         }
 962         if (bestImage == null) {
 963             //No images were found, possibly all are broken
 964             return null;
 965         }
 966         BufferedImage bimage =
 967             new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
 968         Graphics2D g = bimage.createGraphics();
 969         g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
 970                            RenderingHints.VALUE_INTERPOLATION_BILINEAR);
 971         try {
 972             int x = (width - bestWidth) / 2;
 973             int y = (height - bestHeight) / 2;
 974             if (log.isLoggable(PlatformLogger.FINER)) {
 975                 log.finer("WWindowPeer.getScaledIconData() result : " +
 976                         "w : " + width + " h : " + height +
 977                         " iW : " + bestImage.getWidth(null) + " iH : " + bestImage.getHeight(null) +
 978                         " sim : " + bestSimilarity + " sf : " + bestScaleFactor +
 979                         " adjW : " + bestWidth + " adjH : " + bestHeight +
 980                         " x : " + x + " y : " + y);
 981             }
 982             g.drawImage(bestImage, x, y, bestWidth, bestHeight, null);
 983         } finally {
 984             g.dispose();
 985         }
 986         return bimage;
 987     }
 988 
 989     public static DataBufferInt getScaledIconData(java.util.List<Image> imageList, int width, int height) {
 990         BufferedImage bimage = getScaledIconImage(imageList, width, height);
 991         if (bimage == null) {
 992              if (log.isLoggable(PlatformLogger.FINER)) {
 993                  log.finer("SunToolkit.getScaledIconData: " +
 994                            "Perhaps the image passed into Java is broken. Skipping this icon.");
 995              }
 996             return null;
 997         }
 998         Raster raster = bimage.getRaster();
 999         DataBuffer buffer = raster.getDataBuffer();
1000         return (DataBufferInt)buffer;
1001     }
1002 
1003     protected EventQueue getSystemEventQueueImpl() {
1004         return getSystemEventQueueImplPP();
1005     }
1006 
1007     // Package private implementation
1008     static EventQueue getSystemEventQueueImplPP() {
1009         return getSystemEventQueueImplPP(AppContext.getAppContext());
1010     }
1011 
1012     public static EventQueue getSystemEventQueueImplPP(AppContext appContext) {




 470         AWTAccessor.SequencedEventAccessor sea = AWTAccessor.getSequencedEventAccessor();
 471         if (sea != null && sea.isSequencedEvent(event)) {
 472             AWTEvent nested = sea.getNested(event);
 473             if (nested.getID() == WindowEvent.WINDOW_LOST_FOCUS &&
 474                 nested instanceof TimedWindowEvent)
 475             {
 476                 TimedWindowEvent twe = (TimedWindowEvent)nested;
 477                 ((SunToolkit)Toolkit.getDefaultToolkit()).
 478                     setWindowDeactivationTime((Window)twe.getSource(), twe.getWhen());
 479             }
 480         }
 481 
 482         // All events posted via this method are system-generated.
 483         // Placing the following call here reduces considerably the
 484         // number of places throughout the toolkit that would
 485         // otherwise have to be modified to precisely identify
 486         // system-generated events.
 487         setSystemGenerated(event);
 488         AppContext eventContext = targetToAppContext(event.getSource());
 489         if (eventContext != null && !eventContext.equals(appContext)) {
 490             if (log.isLoggable(PlatformLogger.Level.FINE)) {
 491                 log.fine("Event posted on wrong app context : " + event);
 492             }
 493         }
 494         PostEventQueue postEventQueue =
 495             (PostEventQueue)appContext.get(POST_EVENT_QUEUE_KEY);
 496         if (postEventQueue != null) {
 497             postEventQueue.postEvent(event);
 498         }
 499     }
 500 
 501     /*
 502      * Post AWTEvent of high priority.
 503      */
 504     public static void postPriorityEvent(final AWTEvent e) {
 505         PeerEvent pe = new PeerEvent(Toolkit.getDefaultToolkit(), new Runnable() {
 506                 public void run() {
 507                     AWTAccessor.getAWTEventAccessor().setPosted(e);
 508                     ((Component)e.getSource()).dispatchEvent(e);
 509                 }
 510             }, PeerEvent.ULTIMATE_PRIORITY_EVENT);


 863      * Scans {@code imageList} for best-looking image of specified dimensions.
 864      * Image can be scaled and/or padded with transparency.
 865      */
 866     public static BufferedImage getScaledIconImage(java.util.List<Image> imageList, int width, int height) {
 867         if (width == 0 || height == 0) {
 868             return null;
 869         }
 870         Image bestImage = null;
 871         int bestWidth = 0;
 872         int bestHeight = 0;
 873         double bestSimilarity = 3; //Impossibly high value
 874         double bestScaleFactor = 0;
 875         for (Iterator<Image> i = imageList.iterator();i.hasNext();) {
 876             //Iterate imageList looking for best matching image.
 877             //'Similarity' measure is defined as good scale factor and small insets.
 878             //best possible similarity is 0 (no scale, no insets).
 879             //It's found while the experiments that good-looking result is achieved
 880             //with scale factors x1, x3/4, x2/3, xN, x1/N.
 881             Image im = i.next();
 882             if (im == null) {
 883                 if (log.isLoggable(PlatformLogger.Level.FINER)) {
 884                     log.finer("SunToolkit.getScaledIconImage: " +
 885                               "Skipping the image passed into Java because it's null.");
 886                 }
 887                 continue;
 888             }
 889             if (im instanceof ToolkitImage) {
 890                 ImageRepresentation ir = ((ToolkitImage)im).getImageRep();
 891                 ir.reconstruct(ImageObserver.ALLBITS);
 892             }
 893             int iw;
 894             int ih;
 895             try {
 896                 iw = im.getWidth(null);
 897                 ih = im.getHeight(null);
 898             } catch (Exception e){
 899                 if (log.isLoggable(PlatformLogger.Level.FINER)) {
 900                     log.finer("SunToolkit.getScaledIconImage: " +
 901                               "Perhaps the image passed into Java is broken. Skipping this icon.");
 902                 }
 903                 continue;
 904             }
 905             if (iw > 0 && ih > 0) {
 906                 //Calc scale factor
 907                 double scaleFactor = Math.min((double)width / (double)iw,
 908                                               (double)height / (double)ih);
 909                 //Calculate scaled image dimensions
 910                 //adjusting scale factor to nearest "good" value
 911                 int adjw = 0;
 912                 int adjh = 0;
 913                 double scaleMeasure = 1; //0 - best (no) scale, 1 - impossibly bad
 914                 if (scaleFactor >= 2) {
 915                     //Need to enlarge image more than twice
 916                     //Round down scale factor to multiply by integer value
 917                     scaleFactor = Math.floor(scaleFactor);
 918                     adjw = iw * (int)scaleFactor;
 919                     adjh = ih * (int)scaleFactor;


 954                     bestScaleFactor = scaleFactor;
 955                     bestImage = im;
 956                     bestWidth = adjw;
 957                     bestHeight = adjh;
 958                 }
 959                 if (similarity == 0) break;
 960             }
 961         }
 962         if (bestImage == null) {
 963             //No images were found, possibly all are broken
 964             return null;
 965         }
 966         BufferedImage bimage =
 967             new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
 968         Graphics2D g = bimage.createGraphics();
 969         g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
 970                            RenderingHints.VALUE_INTERPOLATION_BILINEAR);
 971         try {
 972             int x = (width - bestWidth) / 2;
 973             int y = (height - bestHeight) / 2;
 974             if (log.isLoggable(PlatformLogger.Level.FINER)) {
 975                 log.finer("WWindowPeer.getScaledIconData() result : " +
 976                         "w : " + width + " h : " + height +
 977                         " iW : " + bestImage.getWidth(null) + " iH : " + bestImage.getHeight(null) +
 978                         " sim : " + bestSimilarity + " sf : " + bestScaleFactor +
 979                         " adjW : " + bestWidth + " adjH : " + bestHeight +
 980                         " x : " + x + " y : " + y);
 981             }
 982             g.drawImage(bestImage, x, y, bestWidth, bestHeight, null);
 983         } finally {
 984             g.dispose();
 985         }
 986         return bimage;
 987     }
 988 
 989     public static DataBufferInt getScaledIconData(java.util.List<Image> imageList, int width, int height) {
 990         BufferedImage bimage = getScaledIconImage(imageList, width, height);
 991         if (bimage == null) {
 992              if (log.isLoggable(PlatformLogger.Level.FINER)) {
 993                  log.finer("SunToolkit.getScaledIconData: " +
 994                            "Perhaps the image passed into Java is broken. Skipping this icon.");
 995              }
 996             return null;
 997         }
 998         Raster raster = bimage.getRaster();
 999         DataBuffer buffer = raster.getDataBuffer();
1000         return (DataBufferInt)buffer;
1001     }
1002 
1003     protected EventQueue getSystemEventQueueImpl() {
1004         return getSystemEventQueueImplPP();
1005     }
1006 
1007     // Package private implementation
1008     static EventQueue getSystemEventQueueImplPP() {
1009         return getSystemEventQueueImplPP(AppContext.getAppContext());
1010     }
1011 
1012     public static EventQueue getSystemEventQueueImplPP(AppContext appContext) {