/[classpath]/classpath/javax/swing/plaf/metal/MetalUtils.java
ViewVC logotype

Diff of /classpath/javax/swing/plaf/metal/MetalUtils.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.2.2.3 by gnu_andrew, Wed Nov 2 00:44:01 2005 UTC revision 1.2.2.4 by gnu_andrew, Sun Nov 27 21:00:41 2005 UTC
# Line 44  import java.awt.Graphics2D; Line 44  import java.awt.Graphics2D;
44  import java.awt.TexturePaint;  import java.awt.TexturePaint;
45  import java.awt.geom.Rectangle2D;  import java.awt.geom.Rectangle2D;
46  import java.awt.image.BufferedImage;  import java.awt.image.BufferedImage;
47    import java.util.List;
48    
49    import javax.swing.SwingConstants;
50    import javax.swing.UIManager;
51    
52  /**  /**
53   * Some utility and helper methods for the Metal Look & Feel.   * Some utility and helper methods for the Metal Look & Feel.
# Line 129  class MetalUtils Line 133  class MetalUtils
133    
134      // Prepare the texture.      // Prepare the texture.
135      TexturePaint texture =      TexturePaint texture =
136        new TexturePaint(pattern2D, new Rectangle2D.Double(0., 0., 4., 2.));        new TexturePaint(pattern2D, new Rectangle2D.Double(0., 0., 4., 4.));
137      g2d.setPaint(texture);      g2d.setPaint(texture);
138      g2d.fillRect(x, y, w, h);      g2d.fillRect(x, y, w, h);
139    }    }
# Line 139  class MetalUtils Line 143  class MetalUtils
143     */     */
144    static void initializePattern(Color light, Color dark)    static void initializePattern(Color light, Color dark)
145    {    {
146      pattern2D = new BufferedImage(4, 4, BufferedImage.TYPE_INT_RGB);      pattern2D = new BufferedImage(4, 4, BufferedImage.TYPE_INT_ARGB);
147      lightColor = light;      lightColor = light;
148      darkColor = dark;      darkColor = dark;
149      Graphics g = pattern2D.getGraphics();      Graphics g = pattern2D.getGraphics();
# Line 151  class MetalUtils Line 155  class MetalUtils
155      g.fillRect(3, 3, 1, 1);      g.fillRect(3, 3, 1, 1);
156      g.dispose();      g.dispose();
157    }    }
158    
159      /**
160       * Paints the typical Metal gradient. See {@link #paintGradient(Graphics,
161       * int, int, int, int, double, double, Color, Color, Color, int)}
162       * for more details.
163       *
164       * The parameters are fetched from the UIManager using the key
165       * <code>uiProp</code>. The value is expected to be a {@link List} that
166       * contains 4 values: two {@link Double}s and 3 {@link Color} object that
167       * together make up the parameters passed to the painting method.
168       *
169       * @param g the graphics context to use
170       * @param x the X coordinate of the upper left corner of the rectangle
171       * @param y the Y coordinate of the upper left corner of the rectangle
172       * @param w the width of the rectangle
173       * @param h the height of the rectangle
174       * @param dir the direction of the gradient, either
175       * @param uiProp the key of the UIManager property that has the parameters
176       */
177      static void paintGradient(Graphics g, int x, int y, int w, int h,
178                                int dir, String uiProp)
179      {
180        List params = (List) UIManager.get(uiProp);
181        double g1 = ((Double) params.get(0)).doubleValue();
182        double g2 = ((Double) params.get(1)).doubleValue();
183        Color c1 = (Color) params.get(2);
184        Color c2 = (Color) params.get(3);
185        Color c3 = (Color) params.get(4);
186        paintGradient(g, x, y, w, h, g1, g2, c1, c2, c3, dir);
187      }
188    
189      /**
190       * Paints the typical Metal gradient. The gradient is painted as follows:
191       * <pre>
192       *
193       * +-------+--------+--------+-----------------------------+
194       * |       |        |        |                             |
195       * +-------+--------+--------+-----------------------------+
196       * c1  ->  c2  --   c2  ->   c1         -------->          c3
197       * < -g1- > < -g2- > < -g1- >
198       * </pre>
199       *
200       * There are 4 distinct areas in this gradient:
201       * <ol>
202       * <li>A gradient from color 1 to color 2 with the relative width specified
203       *   by <code>g1</code></li>
204       * <li>A solid area with the color 2 and the relative width specified by
205       *  <code>g2</code></li>
206       * <li>A gradient from color 2 to color 1 with the relative width specified
207       *   by <code>g1</code></li>
208       *
209       * @param g the graphics context to use
210       * @param x the X coordinate of the upper left corner of the rectangle
211       * @param y the Y coordinate of the upper left corner of the rectangle
212       * @param w the width of the rectangle
213       * @param h the height of the rectangle
214       * @param g1 the relative width of the c1->c2 gradients
215       * @param g2 the relative width of the c2 solid area
216       * @param c1 the color 1
217       * @param c2 the color 2
218       * @param c3 the color 3
219       * @param dir the direction of the gradient, either
220       *        {@link SwingConstants#HORIZONTAL} or {@link SwingConstants#VERTICAL}
221       */
222      static void paintGradient(Graphics g, int x, int y, int w, int h, double g1,
223                                double g2, Color c1, Color c2, Color c3, int dir)
224      {
225        if (dir == SwingConstants.HORIZONTAL)
226          paintHorizontalGradient(g, x, y, w, h, g1, g2, c1, c2, c3);
227        else
228          paintVerticalGradient(g, x, y, w, h, g1, g2, c1, c2, c3);
229      }
230    
231      /**
232       * Paints a horizontal gradient. See {@link #paintGradient(Graphics, int,
233       * int, int, int, double, double, Color, Color, Color, int)} for details.
234       *
235       * @param x the X coordinate of the upper left corner of the rectangle
236       * @param y the Y coordinate of the upper left corner of the rectangle
237       * @param w the width of the rectangle
238       * @param h the height of the rectangle
239       * @param g1 the relative width of the c1->c2 gradients
240       * @param g2 the relative width of the c2 solid area
241       * @param c1 the color 1
242       * @param c2 the color 2
243       * @param c3 the color 3
244       */
245      static void paintHorizontalGradient(Graphics g, int x, int y, int w, int h,
246                                          double g1, double g2, Color c1, Color c2,
247                                          Color c3)
248      {
249        // Calculate the coordinates.
250        // The size of the first gradient area (c1->2).
251        int w1 = (int) (w * g1);
252        // The size of the solid c2 area.
253        int w2 = (int) (w * g2);
254        int x0 = x;
255        int x1 = x0 + w1;
256        int x2 = x1 + w2;
257        int x3 = x2 + w1;
258        int x4 = x + w;
259    
260        // Paint first gradient area (c1->c2).
261        int xc; // The current y coordinate.
262        for (xc = x0; xc < x1; xc++)
263          {
264            if (xc > x + w)
265              break;
266    
267            // Perform color interpolation;
268            double factor = (xc - x0) / (double) w1;
269            int rInt = (int) ((c2.getRed() - c1.getRed()) * factor + c1.getRed());
270            int gInt = (int) ((c2.getGreen() - c1.getGreen()) * factor
271                + c1.getGreen());
272            int bInt = (int) ((c2.getBlue() - c1.getBlue()) * factor
273                + c1.getBlue());
274            Color interpolated = new Color(rInt, gInt, bInt);
275            g.setColor(interpolated);
276            g.drawLine(xc, y, xc, y + h);
277          }
278        // Paint solid c2 area.
279        g.setColor(c2);
280        g.fillRect(x1, y, x2 - x1, h);
281    
282        // Paint second gradient area (c2->c1).
283        for (xc = x2; xc < x3; xc++)
284          {
285            if (xc > x + w)
286              break;
287    
288            // Perform color interpolation;
289            double factor = (xc - x2) / (double) w1;
290            int rInt = (int) ((c1.getRed() - c2.getRed()) * factor + c2.getRed());
291            int gInt = (int) ((c1.getGreen() - c2.getGreen()) * factor
292                + c2.getGreen());
293            int bInt = (int) ((c1.getBlue() - c2.getBlue()) * factor
294                + c2.getBlue());
295            Color interpolated = new Color(rInt, gInt, bInt);
296            g.setColor(interpolated);
297            g.drawLine(xc, y, xc, y + h);
298          }
299    
300        // Paint third gradient area (c1->c3).
301        for (xc = x3; xc < x4; xc++)
302          {
303            if (xc > x + w)
304              break;
305    
306            // Perform color interpolation;
307            double factor = (xc - x3) / (double) (x4 - x3);
308            int rInt = (int) ((c3.getRed() - c1.getRed()) * factor + c1.getRed());
309            int gInt = (int) ((c3.getGreen() - c1.getGreen()) * factor
310                + c1.getGreen());
311            int bInt = (int) ((c3.getBlue() - c1.getBlue()) * factor
312                + c1.getBlue());
313            Color interpolated = new Color(rInt, gInt, bInt);
314            g.setColor(interpolated);
315            g.drawLine(xc, y, xc, y + h);
316          }
317      }
318    
319      /**
320       * Paints a vertical gradient. See {@link #paintGradient(Graphics, int, int,
321       * int, int, double, double, Color, Color, Color, int)} for details.
322       *
323       * @param x the X coordinate of the upper left corner of the rectangle
324       * @param y the Y coordinate of the upper left corner of the rectangle
325       * @param w the width of the rectangle
326       * @param h the height of the rectangle
327       * @param g1 the relative width of the c1->c2 gradients
328       * @param g2 the relative width of the c2 solid area
329       * @param c1 the color 1
330       * @param c2 the color 2
331       * @param c3 the color 3
332       */
333      static void paintVerticalGradient(Graphics g, int x, int y, int w, int h,
334                                        double g1, double g2, Color c1, Color c2,
335                                        Color c3)
336      {
337        // Calculate the coordinates.
338        // The size of the first gradient area (c1->2).
339        int w1 = (int) (h * g1);
340        // The size of the solid c2 area.
341        int w2 = (int) (h * g2);
342        int y0 = y;
343        int y1 = y0 + w1;
344        int y2 = y1 + w2;
345        int y3 = y2 + w1;
346        int y4 = y + h;
347    
348        // Paint first gradient area (c1->c2).
349        int yc; // The current y coordinate.
350        for (yc = y0; yc < y1; yc++)
351          {
352            if (yc > y + h)
353              break;
354    
355            // Perform color interpolation;
356            double factor = (yc - y0) / (double) w1;
357            int rInt = (int) ((c2.getRed() - c1.getRed()) * factor + c1.getRed());
358            int gInt = (int) ((c2.getGreen() - c1.getGreen()) * factor
359                + c1.getGreen());
360            int bInt = (int) ((c2.getBlue() - c1.getBlue()) * factor
361                + c1.getBlue());
362            Color interpolated = new Color(rInt, gInt, bInt);
363            g.setColor(interpolated);
364            g.drawLine(x, yc, x + w, yc);
365          }
366        // Paint solid c2 area.
367        g.setColor(c2);
368        g.fillRect(x, y1, w, y2 - y1);
369    
370        // Paint second gradient area (c2->c1).
371        for (yc = y2; yc < y3; yc++)
372          {
373            if (yc > y + h)
374              break;
375    
376            // Perform color interpolation;
377            double factor = (yc - y2) / (double) w1;
378            int rInt = (int) ((c1.getRed() - c2.getRed()) * factor + c2.getRed());
379            int gInt = (int) ((c1.getGreen() - c2.getGreen()) * factor
380                + c2.getGreen());
381            int bInt = (int) ((c1.getBlue() - c2.getBlue()) * factor
382                + c2.getBlue());
383            Color interpolated = new Color(rInt, gInt, bInt);
384            g.setColor(interpolated);
385            g.drawLine(x, yc, x + w, yc);
386          }
387    
388        // Paint third gradient area (c1->c3).
389        for (yc = y3; yc < y4; yc++)
390          {
391            if (yc > y + h)
392              break;
393    
394            // Perform color interpolation;
395            double factor = (yc - y3) / (double) (y4 - y3);
396            int rInt = (int) ((c3.getRed() - c1.getRed()) * factor + c1.getRed());
397            int gInt = (int) ((c3.getGreen() - c1.getGreen()) * factor
398                + c1.getGreen());
399            int bInt = (int) ((c3.getBlue() - c1.getBlue()) * factor
400                + c1.getBlue());
401            Color interpolated = new Color(rInt, gInt, bInt);
402            g.setColor(interpolated);
403            g.drawLine(x, yc, x + w, yc);
404          }
405      }
406  }  }

Legend:
Removed from v.1.2.2.3  
changed lines
  Added in v.1.2.2.4

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26