KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > java > swing > plaf > nimbus > LoweredBorder


1 /*
2  * @(#)LoweredBorder.java 1.2 08/02/04
3  *
4  * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package com.sun.java.swing.plaf.nimbus;
9
10 import javax.swing.border.Border JavaDoc;
11 import javax.swing.JComponent JavaDoc;
12 import javax.swing.plaf.UIResource JavaDoc;
13 import java.awt.Insets JavaDoc;
14 import java.awt.Component JavaDoc;
15 import java.awt.Graphics JavaDoc;
16 import java.awt.Graphics2D JavaDoc;
17 import java.awt.Color JavaDoc;
18 import java.awt.Transparency JavaDoc;
19 import java.awt.RenderingHints JavaDoc;
20 import java.awt.Dimension JavaDoc;
21 import java.awt.image.BufferedImage JavaDoc;
22
23 /**
24  * LoweredBorder - A recessed rounded inner shadowed border. Used as the
25  * standard Nimbus TitledBorder. This class is both a painter and a swing
26  * border.
27  *
28  * @author Jasper Potts
29  */

30 public class LoweredBorder extends AbstractRegionPainter implements Border JavaDoc {
31     private static final int IMG_SIZE = 30;
32     private static final int RADIUS = 13;
33     private static final Insets JavaDoc INSETS = new Insets JavaDoc(10,10,10,10);
34     private static final PaintContext PAINT_CONTEXT = new PaintContext(INSETS,
35             new Dimension JavaDoc(IMG_SIZE,IMG_SIZE),false,
36             PaintContext.CacheMode.NINE_SQUARE_SCALE,
37             Integer.MAX_VALUE, Integer.MAX_VALUE);
38
39     // =========================================================================
40
// Painter Methods
41

42     @Override JavaDoc
43     protected Object JavaDoc[] getExtendedCacheKeys(JComponent JavaDoc c) {
44         return new Object JavaDoc[] {c.getBackground()};
45     }
46     
47     /**
48      * Actually performs the painting operation. Subclasses must implement this
49      * method. The graphics object passed may represent the actual surface being
50      * rendererd to, or it may be an intermediate buffer. It has also been
51      * pre-translated. Simply render the component as if it were located at 0, 0
52      * and had a width of <code>width</code> and a height of
53      * <code>height</code>. For performance reasons, you may want to read the
54      * clip from the Graphics2D object and only render within that space.
55      *
56      * @param g The Graphics2D surface to paint to
57      * @param c The JComponent related to the drawing event. For example,
58      * if the region being rendered is Button, then <code>c</code>
59      * will be a JButton. If the region being drawn is
60      * ScrollBarSlider, then the component will be JScrollBar.
61      * This value may be null.
62      * @param width The width of the region to paint. Note that in the case of
63      * painting the foreground, this value may differ from
64      * c.getWidth().
65      * @param height The height of the region to paint. Note that in the case of
66      * painting the foreground, this value may differ from
67      * c.getHeight().
68      */

69     protected void doPaint(Graphics2D JavaDoc g, JComponent JavaDoc c, int width, int height,
70             Object JavaDoc[] extendedCacheKeys) {
71         BufferedImage JavaDoc img1,img2;
72         if (g instanceof Graphics2D JavaDoc) {
73             img1 = ((Graphics2D JavaDoc)g).getDeviceConfiguration().
74                     createCompatibleImage(IMG_SIZE,IMG_SIZE,
75                             Transparency.TRANSLUCENT);
76             img2 = ((Graphics2D JavaDoc)g).getDeviceConfiguration().
77                     createCompatibleImage(IMG_SIZE,IMG_SIZE,
78                             Transparency.TRANSLUCENT);
79         } else {
80             img1 = new BufferedImage JavaDoc(IMG_SIZE,IMG_SIZE,
81                     BufferedImage.TYPE_INT_ARGB);
82             img2 = new BufferedImage JavaDoc(IMG_SIZE,IMG_SIZE,
83                     BufferedImage.TYPE_INT_ARGB);
84         }
85         // draw shadow shape
86
Graphics2D JavaDoc g2 = (Graphics2D JavaDoc)img1.getGraphics();
87         g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
88                 RenderingHints.VALUE_ANTIALIAS_ON);
89         g2.setColor(c.getBackground());
90         g2.fillRoundRect(2,0,26,26,RADIUS,RADIUS);
91         g2.dispose();
92         // draw shadow
93
InnerShadowEffect effect = new InnerShadowEffect();
94         effect.setDistance(1);
95         effect.setSize(3);
96         effect.setColor(getLighter(c.getBackground(),2.1f));
97         effect.setAngle(90);
98         effect.applyEffect(img1,img2,IMG_SIZE,IMG_SIZE);
99         // draw outline to img2
100
g2 = (Graphics2D JavaDoc)img2.getGraphics();
101         g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
102                 RenderingHints.VALUE_ANTIALIAS_ON);
103         g2.setClip(0,28,IMG_SIZE,1);
104         g2.setColor(getLighter(c.getBackground(),0.90f));
105         g2.drawRoundRect(2,1,25,25,RADIUS,RADIUS);
106         g2.dispose();
107         // draw final image
108
if (width != IMG_SIZE || height != IMG_SIZE){
109             ImageScalingHelper.paint(g,0,0,width,height,img2, INSETS, INSETS,
110                     ImageScalingHelper.PaintType.PAINT9_STRETCH,
111                     ImageScalingHelper.PAINT_ALL);
112         } else {
113             g.drawImage(img2,0,0,c);
114         }
115         img1 = null;
116         img2 = null;
117     }
118
119     /**
120      * <p>Gets the PaintContext for this painting operation. This method is
121      * called on every paint, and so should be fast and produce no garbage. The
122      * PaintContext contains information such as cache hints. It also contains
123      * data necessary for decoding points at runtime, such as the stretching
124      * insets, the canvas size at which the encoded points were defined, and
125      * whether the stretching insets are inverted.</p>
126      * <p/>
127      * <p> This method allows for subclasses to package the painting of
128      * different states with possibly different canvas sizes, etc, into one
129      * AbstractRegionPainter implementation.</p>
130      *
131      * @return a PaintContext associated with this paint operation.
132      */

133     protected PaintContext getPaintContext() {
134         return PAINT_CONTEXT;
135     }
136
137     // =========================================================================
138
// Border Methods
139

140     /**
141      * Returns the insets of the border.
142      *
143      * @param c the component for which this border insets value applies
144      */

145     public Insets JavaDoc getBorderInsets(Component JavaDoc c) {
146         return INSETS;
147     }
148
149     /**
150      * Returns whether or not the border is opaque. If the border is opaque, it
151      * is responsible for filling in it's own background when painting.
152      */

153     public boolean isBorderOpaque() {
154         return false;
155     }
156
157     /**
158      * Paints the border for the specified component with the specified position
159      * and size.
160      *
161      * @param c the component for which this border is being painted
162      * @param g the paint graphics
163      * @param x the x position of the painted border
164      * @param y the y position of the painted border
165      * @param width the width of the painted border
166      * @param height the height of the painted border
167      */

168     public void paintBorder(Component JavaDoc c, Graphics JavaDoc g, int x, int y, int width,
169                             int height) {
170         JComponent JavaDoc comp = (c instanceof JComponent JavaDoc)?(JComponent JavaDoc)c:null;
171         if (g instanceof Graphics2D JavaDoc){
172             Graphics2D JavaDoc g2 = (Graphics2D JavaDoc)g;
173             g2.translate(x,y);
174             paint(g2,comp, width, height);
175             g2.translate(-x,-y);
176         } else {
177             BufferedImage JavaDoc img = new BufferedImage JavaDoc(IMG_SIZE,IMG_SIZE,
178                     BufferedImage.TYPE_INT_ARGB);
179             Graphics2D JavaDoc g2 = (Graphics2D JavaDoc)img.getGraphics();
180             paint(g2,comp, width, height);
181             g2.dispose();
182             ImageScalingHelper.paint(g,x,y,width,height,img,INSETS, INSETS,
183                     ImageScalingHelper.PaintType.PAINT9_STRETCH,
184                     ImageScalingHelper.PAINT_ALL);
185         }
186     }
187
188     private Color JavaDoc getLighter(Color JavaDoc c, float factor){
189         return new Color JavaDoc(Math.min((int)(c.getRed()/factor), 255),
190                          Math.min((int)(c.getGreen()/factor), 255),
191                          Math.min((int)(c.getBlue()/factor), 255));
192     }
193 }
194
195
Popular Tags