KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jgoodies > looks > common > ExtPasswordView


1 /*
2  * Copyright (c) 2001-2005 JGoodies Karsten Lentzsch. All Rights Reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * o Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  *
10  * o Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * o Neither the name of JGoodies Karsten Lentzsch nor the names of its
15  * contributors may be used to endorse or promote products derived from this
16  * software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */

30  
31 package com.jgoodies.looks.common;
32
33 import java.awt.Container JavaDoc;
34 import java.awt.Graphics JavaDoc;
35 import java.awt.Graphics2D JavaDoc;
36 import java.awt.RenderingHints JavaDoc;
37
38 import javax.swing.JPasswordField JavaDoc;
39 import javax.swing.text.Element JavaDoc;
40 import javax.swing.text.PasswordView JavaDoc;
41
42 /**
43  * Differs from its superclass in that it renders a circle,
44  * not a star ("*") as echo character.
45  *
46  * @author Karsten Lentzsch
47  * @version $Revision: 1.1 $
48  */

49 public final class ExtPasswordView extends PasswordView JavaDoc {
50
51     public ExtPasswordView(Element JavaDoc element) {
52         super(element);
53     }
54
55     /*
56      * Overrides the superclass behavior to paint a filled circle,
57      * not the star ("*") character.
58      */

59     protected int drawEchoCharacter(Graphics JavaDoc g, int x, int y, char c) {
60         Container JavaDoc container = getContainer();
61         if (!(container instanceof JPasswordField JavaDoc))
62             return super.drawEchoCharacter(g, x, y, c);
63
64         JPasswordField JavaDoc field = (JPasswordField JavaDoc) container;
65         int charWidth = getFontMetrics().charWidth(field.getEchoChar());
66         int advance = 2;
67         int diameter = charWidth - advance;
68
69         // Painting the dot with anti-alias enabled.
70
Graphics2D JavaDoc g2 = (Graphics2D JavaDoc) g;
71         Object JavaDoc oldHints = g2.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
72         g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
73                 RenderingHints.VALUE_ANTIALIAS_ON);
74         
75         // Try to vertically align the circle with the font base line.
76
g.fillOval(x, y - diameter, diameter, diameter);
77         g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, oldHints);
78         // End of painting the dot
79

80         // The following line would paint a square, not a dot.
81
// g.fillRect(x, y - diameter + 1, diameter, diameter);
82

83         return x + diameter + advance;
84     }
85     
86     
87 }
Popular Tags