OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libjava/] [classpath/] [javax/] [swing/] [plaf/] [metal/] [MetalUtils.java] - Blame information for rev 772

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 772 jeremybenn
/* MetalUtils.java
2
Copyright (C) 2005 Free Software Foundation, Inc.
3
 
4
This file is part of GNU Classpath.
5
 
6
GNU Classpath is free software; you can redistribute it and/or modify
7
it under the terms of the GNU General Public License as published by
8
the Free Software Foundation; either version 2, or (at your option)
9
any later version.
10
 
11
GNU Classpath is distributed in the hope that it will be useful, but
12
WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
General Public License for more details.
15
 
16
You should have received a copy of the GNU General Public License
17
along with GNU Classpath; see the file COPYING.  If not, write to the
18
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19
02110-1301 USA.
20
 
21
Linking this library statically or dynamically with other modules is
22
making a combined work based on this library.  Thus, the terms and
23
conditions of the GNU General Public License cover the whole
24
combination.
25
 
26
As a special exception, the copyright holders of this library give you
27
permission to link this library with independent modules to produce an
28
executable, regardless of the license terms of these independent
29
modules, and to copy and distribute the resulting executable under
30
terms of your choice, provided that you also meet, for each linked
31
independent module, the terms and conditions of the license of that
32
module.  An independent module is a module which is not derived from
33
or based on this library.  If you modify this library, you may extend
34
this exception to your version of the library, but you are not
35
obligated to do so.  If you do not wish to do so, delete this
36
exception statement from your version. */
37
 
38
package javax.swing.plaf.metal;
39
 
40
import gnu.classpath.SystemProperties;
41
 
42
import java.awt.Color;
43
import java.awt.Component;
44
import java.awt.GradientPaint;
45
import java.awt.Graphics;
46
import java.awt.Graphics2D;
47
import java.awt.TexturePaint;
48
import java.awt.geom.Rectangle2D;
49
import java.awt.image.BufferedImage;
50
import java.util.List;
51
 
52
import javax.swing.SwingConstants;
53
import javax.swing.UIManager;
54
 
55
/**
56
 * Some utility and helper methods for the Metal Look & Feel.
57
 *
58
 * @author Roman Kennke (roman@kennke.org)
59
 */
60
class MetalUtils
61
{
62
 
63
  /**
64
   * The typical metal pattern for use with Graphics2D.
65
   */
66
  static BufferedImage pattern2D;
67
 
68
  /**
69
   * The light color to draw the pattern.
70
   */
71
  static Color lightColor;
72
 
73
  /**
74
   * The dark color to draw to draw the pattern.
75
   */
76
  static Color darkColor;
77
 
78
  /**
79
   * Fills a rectangle with the typical Metal pattern.
80
   *
81
   * @param g the <code>Graphics</code> context to use
82
   * @param x the X coordinate of the upper left corner of the rectangle to
83
   *     fill
84
   * @param y the Y coordinate of the upper left corner of the rectangle to
85
   *     fill
86
   * @param w the width of the rectangle to fill
87
   * @param h the height of the rectangle to fill
88
   * @param light the light color to use
89
   * @param dark the dark color to use
90
   */
91
  static void fillMetalPattern(Component c, Graphics g, int x, int y, int w, int h,
92
                                Color light, Color dark)
93
  {
94
    if (g instanceof Graphics2D
95
      && SystemProperties.getProperty("gnu.javax.swing.noGraphics2D") == null)
96
      fillMetalPattern2D((Graphics2D) g, x, y, w, h, light, dark);
97
    else
98
      {
99
        int xOff = 0;
100
        for (int mY = y; mY < (y + h); mY++)
101
          {
102
            // set color alternating with every line
103
            if (((mY - y) % 2) == 0)
104
              g.setColor(light);
105
            else
106
              g.setColor(dark);
107
 
108
            for (int mX = x + xOff; mX < (x + w); mX += 4)
109
              {
110
                g.fillRect(mX, mY, 1, 1);
111
              }
112
 
113
            // increase x offset
114
            xOff++;
115
            if (xOff > 3)
116
              xOff = 0;
117
          }
118
        }
119
  }
120
 
121
  /**
122
   * Fills a rectangle with the typical Metal pattern using Java2D.
123
   *
124
   * @param g2d the <code>Graphics2D</code> context to use
125
   * @param x the X coordinate of the upper left corner of the rectangle to
126
   *     fill
127
   * @param y the Y coordinate of the upper left corner of the rectangle to
128
   *     fill
129
   * @param w the width of the rectangle to fill
130
   * @param h the height of the rectangle to fill
131
   */
132
  static void fillMetalPattern2D(Graphics2D g2d,  int x, int y, int w, int h,
133
                                 Color light, Color dark)
134
  {
135
    if (pattern2D == null || !darkColor.equals(dark) || !lightColor.equals(light))
136
      initializePattern(light, dark);
137
 
138
    // Prepare the texture.
139
    TexturePaint texture =
140
      new TexturePaint(pattern2D, new Rectangle2D.Double(0., 0., 4., 4.));
141
    g2d.setPaint(texture);
142
    g2d.fillRect(x, y, w, h);
143
  }
144
 
145
  /**
146
   * Initializes the pattern image.
147
   */
148
  static void initializePattern(Color light, Color dark)
149
  {
150
    pattern2D = new BufferedImage(4, 4, BufferedImage.TYPE_INT_ARGB);
151
    lightColor = light;
152
    darkColor = dark;
153
    Graphics g = pattern2D.getGraphics();
154
    g.setColor(light);
155
    g.fillRect(0, 0, 1, 1);
156
    g.fillRect(2, 2, 1, 1);
157
    g.setColor(dark);
158
    g.fillRect(1, 1, 1, 1);
159
    g.fillRect(3, 3, 1, 1);
160
    g.dispose();
161
  }
162
 
163
  /**
164
   * Paints the typical Metal gradient. See {@link #paintGradient(Graphics,
165
   * int, int, int, int, float, float, Color, Color, Color, int, int[][])}
166
   * for more details.
167
   *
168
   * This variant paints a gradient without a mask.
169
   *
170
   * @param g the graphics context to use
171
   * @param x the X coordinate of the upper left corner of the rectangle
172
   * @param y the Y coordinate of the upper left corner of the rectangle
173
   * @param w the width of the rectangle
174
   * @param h the height of the rectangle
175
   * @param dir the direction of the gradient, either
176
   * @param uiProp the key of the UIManager property that has the parameters
177
   */
178
  static void paintGradient(Graphics g, int x, int y, int w, int h,
179
                            int dir, String uiProp)
180
  {
181
    paintGradient(g, x, y, w, h, dir, uiProp, null);
182
  }
183
 
184
  /**
185
   * Paints the typical Metal gradient. See {@link #paintGradient(Graphics,
186
   * int, int, int, int, float, float, Color, Color, Color, int, int[][])}
187
   * for more details.
188
   *
189
   * The parameters are fetched from the UIManager using the key
190
   * <code>uiProp</code>. The value is expected to be a {@link List} that
191
   * contains 4 values: two {@link Double}s and 3 {@link Color} object that
192
   * together make up the parameters passed to the painting method.
193
   *
194
   * @param g the graphics context to use
195
   * @param x the X coordinate of the upper left corner of the rectangle
196
   * @param y the Y coordinate of the upper left corner of the rectangle
197
   * @param w the width of the rectangle
198
   * @param h the height of the rectangle
199
   * @param dir the direction of the gradient, either
200
   * @param uiProp the key of the UIManager property that has the parameters
201
   * @param mask the mask that should be used when painting the gradient as
202
   *        described above
203
   */
204
  static void paintGradient(Graphics g, int x, int y, int w, int h,
205
                            int dir, String uiProp, int[][] mask)
206
  {
207
    List params = (List) UIManager.get(uiProp);
208
    float g1 = ((Float) params.get(0)).floatValue();
209
    float g2 = ((Float) params.get(1)).floatValue();
210
    Color c1 = (Color) params.get(2);
211
    Color c2 = (Color) params.get(3);
212
    Color c3 = (Color) params.get(4);
213
    paintGradient(g, x, y, w, h, g1, g2, c1, c2, c3, dir, mask);
214
  }
215
 
216
  /**
217
   * Paints the typical Metal gradient. The gradient is painted as follows:
218
   * <pre>
219
   *
220
   * +-------+--------+--------+-----------------------------+
221
   * |       |        |        |                             |
222
   * +-------+--------+--------+-----------------------------+
223
   * c1  ->  c2  --   c2  ->   c1         -------->          c3
224
   * < -g1- > < -g2- > < -g1- >
225
   * </pre>
226
   *
227
   * There are 4 distinct areas in this gradient:
228
   * <ol>
229
   * <li>A gradient from color 1 to color 2 with the relative width specified
230
   *   by <code>g1</code></li>
231
   * <li>A solid area with the color 2 and the relative width specified by
232
   *  <code>g2</code></li>
233
   * <li>A gradient from color 2 to color 1 with the relative width specified
234
   *   by <code>g1</code></li>
235
   *
236
   * The <code>mask</code> parameter is an array if int arrays, where the first
237
   * index specifies the row (in the gradient direction), and the second index
238
   * is the starting and end offset of that line. This way you can specify a
239
   * mask that should be laid over the gradient for paintint non-rectangular
240
   * gradients. The following example should demonstrate this for painting
241
   * a circular shaped gradient (note that the first and last line should not
242
   * be drawn at all, they are only here to show the circular shape more
243
   * clearly). Everything <em>inside</code> the surrounded area is filled by
244
   * the gradient:
245
   *
246
   * <pre>
247
   *     012345678
248
   *         xxx
249
   * 0      x   x         { {4, 7},
250
   * 1     x     x          {3, 8},
251
   * 2     x     x          {3, 8},
252
   * 3     x     x          {3, 8},
253
   * 4      x   x           {4, 7} }
254
   *         xxx
255
   * </pre>
256
   *
257
   * The <code>mask</code> array is expected to have <code>w</code> or
258
   * <code>h</code> array elements, depending on the direction.
259
   *
260
   * If the <code>mask</code> parameter is null, then the gradient is painted
261
   * without a mask.
262
   *
263
   * @param g the graphics context to use
264
   * @param x the X coordinate of the upper left corner of the rectangle
265
   * @param y the Y coordinate of the upper left corner of the rectangle
266
   * @param w the width of the rectangle
267
   * @param h the height of the rectangle
268
   * @param g1 the relative width of the c1->c2 gradients
269
   * @param g2 the relative width of the c2 solid area
270
   * @param c1 the color 1
271
   * @param c2 the color 2
272
   * @param c3 the color 3
273
   * @param dir the direction of the gradient, either
274
   *        {@link SwingConstants#HORIZONTAL} or {@link SwingConstants#VERTICAL}
275
   * @param mask the mask that should be used when painting the gradient as
276
   *        described above
277
   */
278
  static void paintGradient(Graphics g, int x, int y, int w, int h, float g1,
279
                            float g2, Color c1, Color c2, Color c3, int dir,
280
                            int[][] mask)
281
  {
282
    if (dir == SwingConstants.HORIZONTAL)
283
      paintHorizontalGradient(g, x, y, w, h, g1, g2, c1, c2, c3, mask);
284
    else
285
      paintVerticalGradient(g, x, y, w, h, g1, g2, c1, c2, c3, mask);
286
  }
287
 
288
  /**
289
   * Paints a horizontal gradient. See {@link #paintGradient(Graphics, int,
290
   * int, int, int, float, float, Color, Color, Color, int, int[][])}
291
   * for details.
292
   *
293
   * @param x the X coordinate of the upper left corner of the rectangle
294
   * @param y the Y coordinate of the upper left corner of the rectangle
295
   * @param w the width of the rectangle
296
   * @param h the height of the rectangle
297
   * @param g1 the relative width of the c1->c2 gradients
298
   * @param g2 the relative width of the c2 solid area
299
   * @param c1 the color 1
300
   * @param c2 the color 2
301
   * @param c3 the color 3
302
   * @param mask the mask that should be used when painting the gradient as
303
   *        described above
304
   */
305
  static void paintHorizontalGradient(Graphics g, int x, int y, int w, int h,
306
                                      float g1, float g2, Color c1, Color c2,
307
                                      Color c3, int[][] mask)
308
  {
309
 
310
    if (g instanceof Graphics2D
311
        && SystemProperties.getProperty("gnu.javax.swing.noGraphics2D") == null)
312
      {
313
        paintHorizontalGradient2D((Graphics2D) g, x, y, w, h, g1, g2, c1, c2,
314
                                  c3, mask);
315
        return;
316
      }
317
 
318
    // Calculate the coordinates.
319
    int y0 = y;
320
    int y1 = y + h;
321
    // The size of the first gradient area (c1->2).
322
    int w1 = (int) (w * g1);
323
    // The size of the solid c2 area.
324
    int w2 = (int) (w * g2);
325
    int x0 = x;
326
    int x1 = x0 + w1;
327
    int x2 = x1 + w2;
328
    int x3 = x2 + w1;
329
    int x4 = x + w;
330
 
331
    // Paint first gradient area (c1->c2).
332
    int xc; // The current y coordinate.
333
    for (xc = x0; xc < x1; xc++)
334
      {
335
        if (xc > x + w)
336
          break;
337
 
338
        // Perform color interpolation;
339
        double factor = (xc - x0) / (double) w1;
340
        int rInt = (int) ((c2.getRed() - c1.getRed()) * factor + c1.getRed());
341
        int gInt = (int) ((c2.getGreen() - c1.getGreen()) * factor
342
            + c1.getGreen());
343
        int bInt = (int) ((c2.getBlue() - c1.getBlue()) * factor
344
            + c1.getBlue());
345
        Color interpolated = new Color(rInt, gInt, bInt);
346
        g.setColor(interpolated);
347
        if (mask != null)
348
          {
349
            y0 = mask[xc - x0][0] + y;
350
            y1 = mask[xc - x0][1] + y;
351
          }
352
        g.fillRect(xc, y0, 1, y1 - y0);
353
      }
354
    // Paint solid c2 area.
355
    g.setColor(c2);
356
    if (mask == null)
357
      {
358
        g.fillRect(x1, y, x2 - x1, h);
359
      }
360
    else
361
      {
362
        for (xc = x1; xc < x2; xc++)
363
          {
364
            y0 = mask[xc - x0][0] + y;
365
            y1 = mask[xc - x0][1] + y;
366
            g.fillRect(xc, y0, 1, y1 - y0);
367
          }
368
      }
369
 
370
    // Paint second gradient area (c2->c1).
371
    for (xc = x2; xc < x3; xc++)
372
      {
373
        if (xc > x + w)
374
          break;
375
 
376
        // Perform color interpolation;
377
        double factor = (xc - x2) / (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
        if (mask != null)
386
          {
387
            y0 = mask[xc - x0][0] + y;
388
            y1 = mask[xc - x0][1] + y;
389
          }
390
        g.fillRect(xc, y0, 1, y1 - y0);
391
      }
392
 
393
    // Paint third gradient area (c1->c3).
394
    for (xc = x3; xc < x4; xc++)
395
      {
396
        if (xc > x + w)
397
          break;
398
 
399
        // Perform color interpolation;
400
        double factor = (xc - x3) / (double) (x4 - x3);
401
        int rInt = (int) ((c3.getRed() - c1.getRed()) * factor + c1.getRed());
402
        int gInt = (int) ((c3.getGreen() - c1.getGreen()) * factor
403
            + c1.getGreen());
404
        int bInt = (int) ((c3.getBlue() - c1.getBlue()) * factor
405
            + c1.getBlue());
406
        Color interpolated = new Color(rInt, gInt, bInt);
407
        g.setColor(interpolated);
408
        if (mask != null)
409
          {
410
            y0 = mask[xc - x0][0] + y;
411
            y1 = mask[xc - x0][1] + y;
412
          }
413
        g.drawLine(xc, y0, xc, y1);
414
      }
415
  }
416
 
417
  /**
418
   * Paints a vertical gradient. See {@link #paintGradient(Graphics, int, int,
419
   * int, int, float, float, Color, Color, Color, int, int[][])} for details.
420
   *
421
   * @param x the X coordinate of the upper left corner of the rectangle
422
   * @param y the Y coordinate of the upper left corner of the rectangle
423
   * @param w the width of the rectangle
424
   * @param h the height of the rectangle
425
   * @param g1 the relative width of the c1->c2 gradients
426
   * @param g2 the relative width of the c2 solid area
427
   * @param c1 the color 1
428
   * @param c2 the color 2
429
   * @param c3 the color 3
430
   * @param mask the mask that should be used when painting the gradient as
431
   *        described above
432
   */
433
  static void paintVerticalGradient(Graphics g, int x, int y, int w, int h,
434
                                    float g1, float g2, Color c1, Color c2,
435
                                    Color c3, int[][] mask)
436
  {
437
    if (g instanceof Graphics2D
438
        && SystemProperties.getProperty("gnu.javax.swing.noGraphics2D") == null)
439
       {
440
         paintVerticalGradient2D((Graphics2D) g, x, y, w, h, g1, g2, c1, c2,
441
                                   c3, mask);
442
         return;
443
       }
444
 
445
    // Calculate the coordinates.
446
    int x0 = x;
447
    int x1 = x + w;
448
    // The size of the first gradient area (c1->2).
449
    int w1 = (int) (h * g1);
450
    // The size of the solid c2 area.
451
    int w2 = (int) (h * g2);
452
    int y0 = y;
453
    int y1 = y0 + w1;
454
    int y2 = y1 + w2;
455
    int y3 = y2 + w1;
456
    int y4 = y + h;
457
 
458
    // Paint first gradient area (c1->c2).
459
    int yc; // The current y coordinate.
460
    for (yc = y0; yc < y1; yc++)
461
      {
462
        if (yc > y + h)
463
          break;
464
 
465
        // Perform color interpolation;
466
        double factor = (yc - y0) / (double) w1;
467
        int rInt = (int) ((c2.getRed() - c1.getRed()) * factor + c1.getRed());
468
        int gInt = (int) ((c2.getGreen() - c1.getGreen()) * factor
469
            + c1.getGreen());
470
        int bInt = (int) ((c2.getBlue() - c1.getBlue()) * factor
471
            + c1.getBlue());
472
        Color interpolated = new Color(rInt, gInt, bInt);
473
        g.setColor(interpolated);
474
        if (mask != null)
475
          {
476
            x0 = mask[yc - y0][0] + x;
477
            x1 = mask[yc - y0][1] + x;
478
          }
479
        g.fillRect(x0, yc, x1 - x0, 1);
480
      }
481
    // Paint solid c2 area.
482
    g.setColor(c2);
483
    if (mask == null)
484
      {
485
        g.fillRect(x, y1, w, y2 - y1);
486
      }
487
    else
488
      {
489
        for (yc = y1; yc < y2; yc++)
490
          {
491
            x0 = mask[yc - y0][0] + x;
492
            x1 = mask[yc - y0][1] + x;
493
            g.fillRect(x0, yc, x1 - x0, 1);
494
          }
495
      }
496
 
497
    // Paint second gradient area (c2->c1).
498
    for (yc = y2; yc < y3; yc++)
499
      {
500
        if (yc > y + h)
501
          break;
502
 
503
        // Perform color interpolation;
504
        double factor = (yc - y2) / (double) w1;
505
        int rInt = (int) ((c1.getRed() - c2.getRed()) * factor + c2.getRed());
506
        int gInt = (int) ((c1.getGreen() - c2.getGreen()) * factor
507
            + c2.getGreen());
508
        int bInt = (int) ((c1.getBlue() - c2.getBlue()) * factor
509
            + c2.getBlue());
510
        Color interpolated = new Color(rInt, gInt, bInt);
511
        g.setColor(interpolated);
512
        if (mask != null)
513
          {
514
            x0 = mask[yc - y0][0] + x;
515
            x1 = mask[yc - y0][1] + x;
516
          }
517
        g.fillRect(x0, yc, x1 - x0, 1);
518
      }
519
 
520
    // Paint third gradient area (c1->c3).
521
    for (yc = y3; yc < y4; yc++)
522
      {
523
        if (yc > y + h)
524
          break;
525
 
526
        // Perform color interpolation;
527
        double factor = (yc - y3) / (double) (y4 - y3);
528
        int rInt = (int) ((c3.getRed() - c1.getRed()) * factor + c1.getRed());
529
        int gInt = (int) ((c3.getGreen() - c1.getGreen()) * factor
530
            + c1.getGreen());
531
        int bInt = (int) ((c3.getBlue() - c1.getBlue()) * factor
532
            + c1.getBlue());
533
        Color interpolated = new Color(rInt, gInt, bInt);
534
        g.setColor(interpolated);
535
        if (mask != null)
536
          {
537
            x0 = mask[yc - y0][0] + x;
538
            x1 = mask[yc - y0][1] + x;
539
          }
540
        g.fillRect(x0, yc, x1 - x0, 1);
541
      }
542
  }
543
 
544
  /**
545
   * Paints a horizontal gradient using Graphics2D functionality.
546
   *
547
   * @param g the Graphics2D instance
548
   * @param x the X coordinate of the upper left corner of the rectangle
549
   * @param y the Y coordinate of the upper left corner of the rectangle
550
   * @param w the width of the rectangle
551
   * @param h the height of the rectangle
552
   * @param g1 the relative width of the c1->c2 gradients
553
   * @param g2 the relative width of the c2 solid area
554
   * @param c1 the color 1
555
   * @param c2 the color 2
556
   * @param c3 the color 3
557
   * @param mask the mask that should be used when painting the gradient as
558
   *        described above
559
   */
560
  private static void paintHorizontalGradient2D(Graphics2D g, int x, int y,
561
                                                int w, int h, float g1,
562
                                                float g2, Color c1,
563
                                                Color c2, Color c3,
564
                                                int[][] mask)
565
  {
566
    // FIXME: Handle the mask somehow, or do Graphics2D clipping instead.
567
    GradientPaint p1 = new GradientPaint(x, y, c1, x + w * g1, y, c2);
568
    g.setPaint(p1);
569
    // This fills the first gradient and the solid area in one go.
570
    g.fillRect(x, y, (int) (w * (g1 + g2)), h);
571
 
572
    GradientPaint p2 = new GradientPaint(x + (w * (g1 + g2)), y, c2, x + w, y,
573
                                         c3);
574
    g.setPaint(p2);
575
    g.fillRect((int) (x + (w * (g1 + g2))), y,
576
               (int) (w * (1. - (g1 + g2))), h);
577
  }
578
 
579
  private static void paintVerticalGradient2D(Graphics2D g, int x, int y,
580
                                              int w, int h, float g1,
581
                                              float g2, Color c1,
582
                                              Color c2, Color c3,
583
                                              int[][] mask)
584
  {
585
    // FIXME: Handle the mask somehow, or do Graphics2D clipping instead.
586
    GradientPaint p1 = new GradientPaint(x, y, c1, x, y + h * g1, c2);
587
    g.setPaint(p1);
588
    // This fills the first gradient and the solid area in one go.
589
    g.fillRect(x, y, w, (int) (h * (g1 + g2)));
590
 
591
    GradientPaint p2 = new GradientPaint(x, y + (h * (g1 + g2)), c2, x, y + h,
592
                                         c3);
593
    g.setPaint(p2);
594
    g.fillRect(x, (int) (y + (h * (g1 + g2))), w,
595
               (int) (h * (1. - (g1 + g2))));
596
  }
597
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.