1 package helloagain.kanday; 2 3 import java.awt.Font; 4 import java.awt.FontMetrics; 5 import java.awt.Graphics; 6 import java.util.logging.Level; 7 import java.util.logging.Logger; 8 import javax.swing.JComponent; 9 10 /*** 11 * メッセージを表示するpane。 12 * 与えられた大きさに対して中央に1行の文字列を表示する。 13 * 14 * <p> 15 * Created: Wed May 26 12:04:57 2004 16 * 17 * @author <a href="mailto:torutk@02.246.ne.jp">TAKAHASHI,Toru</a> 18 * @version 1.0 19 */ 20 public class KandayPane extends JComponent { 21 22 /*** 23 * コンストラクタ。 24 * フォントを固定で生成する。Serif, 18point 25 */ 26 public KandayPane() { 27 font = new Font("Serif", Font.PLAIN, 18); 28 setOpaque(true); 29 } 30 31 /*** 32 * 描画するメッセージを設定する。 33 * メッセージ設定時に、文字列を描画した際の大よその幅を計算しておく。 34 * @param aMessage 描画する文字列 35 */ 36 public void setMessage(String aMessage) { 37 message = aMessage; 38 updateMessageWidth(); 39 } 40 41 /*** 42 * フォントを設定する。 43 * @param aFont フォント 44 */ 45 public void setFont(Font aFont) { 46 font = aFont; 47 updateMessageWidth(); 48 } 49 50 /*** 51 * コンポーネント描画時に、message文字列を表示する。 52 * 描画位置は、コンポネントのだいたい中央に位置するよう算出する。 53 * 54 * @param graphics a <code>Graphics</code> value 55 */ 56 public final void paintComponent(final Graphics graphics) { 57 if (logger.isLoggable(Level.FINER)) { 58 logger.entering("MYCLASS", "paintComponent", graphics); 59 logger.finer("background color = " + getBackground()); 60 logger.finer("foreground color = " + getForeground()); 61 } 62 super.paintComponent(graphics); 63 graphics.setColor(getBackground()); 64 graphics.fillRect(0, 0, getWidth(), getHeight()); 65 66 int x = (getWidth() / 2) - (messageWidth / 2); 67 int y = (getHeight() / 2); 68 graphics.setFont(font); 69 graphics.setColor(getForeground()); 70 graphics.drawString(message, x, y); 71 } 72 73 private void updateMessageWidth() { 74 FontMetrics metrics = getFontMetrics(font); 75 messageWidth = metrics.stringWidth(message); 76 } 77 78 private String message; 79 private Font font; 80 private int messageWidth; 81 82 private static final String MYCLASS = KandayPane.class.getName(); 83 private static Logger logger = Logger.getLogger(MYCLASS); 84 }