src/share/classes/javax/swing/plaf/metal/MetalUtils.java

Print this page


   1 /*
   2  * Copyright (c) 1998, 2005, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 193      * in order from the origin.
 194      * <ol>
 195      * <li>Gradient r1 % of the size from c1 to c2
 196      * <li>Rectangle r2 % of the size in c2.
 197      * <li>Gradient r1 % of the size from c2 to c1
 198      * <li>The remaining size will be filled with a gradient from c1 to c3.
 199      * </ol>
 200      *
 201      * @param c Component rendering to
 202      * @param g Graphics to draw to.
 203      * @param key UIManager key used to look up gradient values.
 204      * @param x X coordinate to draw from
 205      * @param y Y coordinate to draw from
 206      * @param w Width to draw to
 207      * @param h Height to draw to
 208      * @param vertical Direction of the gradient
 209      * @return true if <code>key</code> exists, otherwise false.
 210      */
 211     static boolean drawGradient(Component c, Graphics g, String key,
 212                                 int x, int y, int w, int h, boolean vertical) {
 213         java.util.List gradient = (java.util.List)UIManager.get(key);

 214         if (gradient == null || !(g instanceof Graphics2D)) {
 215             return false;
 216         }
 217 
 218         if (w <= 0 || h <= 0) {
 219             return true;
 220         }
 221 
 222         GradientPainter.INSTANCE.paint(
 223                 c, (Graphics2D)g, gradient, x, y, w, h, vertical);
 224         return true;
 225     }
 226 
 227 
 228     private static class GradientPainter extends CachedPainter {
 229         /**
 230          * Instance used for painting.  This is the only instance that is
 231          * ever created.
 232          */
 233         public static final GradientPainter INSTANCE = new GradientPainter(8);
 234 
 235         // Size of images to create. For vertical gradients this is the width,
 236         // otherwise it's the height.
 237         private static final int IMAGE_SIZE = 64;
 238 
 239         /**
 240          * This is the actual width we're painting in, or last painted to.
 241          */
 242         private int w;
 243         /**
 244          * This is the actual height we're painting in, or last painted to
 245          */
 246         private int h;
 247 
 248 
 249         GradientPainter(int count) {
 250             super(count);
 251         }
 252 
 253         public void paint(Component c, Graphics2D g,
 254                           java.util.List gradient, int x, int y, int w,
 255                           int h, boolean isVertical) {
 256             int imageWidth;
 257             int imageHeight;
 258             if (isVertical) {
 259                 imageWidth = IMAGE_SIZE;
 260                 imageHeight = h;
 261             }
 262             else {
 263                 imageWidth = w;
 264                 imageHeight = IMAGE_SIZE;
 265             }
 266             synchronized(c.getTreeLock()) {
 267                 this.w = w;
 268                 this.h = h;
 269                 paint(c, g, x, y, imageWidth, imageHeight,
 270                       gradient, isVertical);
 271             }
 272         }
 273 
 274         protected void paintToImage(Component c, Image image, Graphics g,
 275                                     int w, int h, Object[] args) {
 276             Graphics2D g2 = (Graphics2D)g;
 277             java.util.List gradient = (java.util.List)args[0];

 278             boolean isVertical = ((Boolean)args[1]).booleanValue();
 279             // Render to the VolatileImage
 280             if (isVertical) {
 281                 drawVerticalGradient(g2,
 282                                      ((Number)gradient.get(0)).floatValue(),
 283                                      ((Number)gradient.get(1)).floatValue(),
 284                                      (Color)gradient.get(2),
 285                                      (Color)gradient.get(3),
 286                                      (Color)gradient.get(4), w, h);
 287             }
 288             else {
 289                 drawHorizontalGradient(g2,
 290                                       ((Number)gradient.get(0)).floatValue(),
 291                                       ((Number)gradient.get(1)).floatValue(),
 292                                       (Color)gradient.get(2),
 293                                       (Color)gradient.get(3),
 294                                       (Color)gradient.get(4), w, h);
 295             }
 296         }
 297 


   1 /*
   2  * Copyright (c) 1998, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 193      * in order from the origin.
 194      * <ol>
 195      * <li>Gradient r1 % of the size from c1 to c2
 196      * <li>Rectangle r2 % of the size in c2.
 197      * <li>Gradient r1 % of the size from c2 to c1
 198      * <li>The remaining size will be filled with a gradient from c1 to c3.
 199      * </ol>
 200      *
 201      * @param c Component rendering to
 202      * @param g Graphics to draw to.
 203      * @param key UIManager key used to look up gradient values.
 204      * @param x X coordinate to draw from
 205      * @param y Y coordinate to draw from
 206      * @param w Width to draw to
 207      * @param h Height to draw to
 208      * @param vertical Direction of the gradient
 209      * @return true if <code>key</code> exists, otherwise false.
 210      */
 211     static boolean drawGradient(Component c, Graphics g, String key,
 212                                 int x, int y, int w, int h, boolean vertical) {
 213         @SuppressWarnings("unchecked")
 214         java.util.List<?> gradient = (java.util.List)UIManager.get(key);
 215         if (gradient == null || !(g instanceof Graphics2D)) {
 216             return false;
 217         }
 218 
 219         if (w <= 0 || h <= 0) {
 220             return true;
 221         }
 222 
 223         GradientPainter.INSTANCE.paint(
 224                 c, (Graphics2D)g, gradient, x, y, w, h, vertical);
 225         return true;
 226     }
 227 
 228 
 229     private static class GradientPainter extends CachedPainter {
 230         /**
 231          * Instance used for painting.  This is the only instance that is
 232          * ever created.
 233          */
 234         public static final GradientPainter INSTANCE = new GradientPainter(8);
 235 
 236         // Size of images to create. For vertical gradients this is the width,
 237         // otherwise it's the height.
 238         private static final int IMAGE_SIZE = 64;
 239 
 240         /**
 241          * This is the actual width we're painting in, or last painted to.
 242          */
 243         private int w;
 244         /**
 245          * This is the actual height we're painting in, or last painted to
 246          */
 247         private int h;
 248 
 249 
 250         GradientPainter(int count) {
 251             super(count);
 252         }
 253 
 254         public void paint(Component c, Graphics2D g,
 255                           java.util.List<?> gradient, int x, int y, int w,
 256                           int h, boolean isVertical) {
 257             int imageWidth;
 258             int imageHeight;
 259             if (isVertical) {
 260                 imageWidth = IMAGE_SIZE;
 261                 imageHeight = h;
 262             }
 263             else {
 264                 imageWidth = w;
 265                 imageHeight = IMAGE_SIZE;
 266             }
 267             synchronized(c.getTreeLock()) {
 268                 this.w = w;
 269                 this.h = h;
 270                 paint(c, g, x, y, imageWidth, imageHeight,
 271                       gradient, isVertical);
 272             }
 273         }
 274 
 275         protected void paintToImage(Component c, Image image, Graphics g,
 276                                     int w, int h, Object[] args) {
 277             Graphics2D g2 = (Graphics2D)g;
 278             @SuppressWarnings("unchecked")
 279             java.util.List<?> gradient = (java.util.List)args[0];
 280             boolean isVertical = ((Boolean)args[1]).booleanValue();
 281             // Render to the VolatileImage
 282             if (isVertical) {
 283                 drawVerticalGradient(g2,
 284                                      ((Number)gradient.get(0)).floatValue(),
 285                                      ((Number)gradient.get(1)).floatValue(),
 286                                      (Color)gradient.get(2),
 287                                      (Color)gradient.get(3),
 288                                      (Color)gradient.get(4), w, h);
 289             }
 290             else {
 291                 drawHorizontalGradient(g2,
 292                                       ((Number)gradient.get(0)).floatValue(),
 293                                       ((Number)gradient.get(1)).floatValue(),
 294                                       (Color)gradient.get(2),
 295                                       (Color)gradient.get(3),
 296                                       (Color)gradient.get(4), w, h);
 297             }
 298         }
 299