トップ 差分 一覧 Farm ソース 検索 ヘルプ PDF RSS ログイン

AWTなフォントパネル

[Java]
[プログラミング]

FontPanel.java(765)

java -encoding EUC-JP FrontPanel.java

import java.awt.*;
import java.awt.event.*;

public class FontPanel extends Frame implements ItemListener{

  private Frame frame;
  private Choice fontnamechoice;
  private Choice fontsizechoice;
  private String fontname = "SansSerif";
  private int fontsize = 18;

  FontPanel(){
    super("FontPanel");
    Font[] fonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();
    fontnamechoice = new Choice();
    for(int i = 0; i < fonts.length; i++){
      fontnamechoice.add(fonts[i].getName());
    }
    fontnamechoice.addItemListener(this);
    fontsizechoice = new Choice();
    fontsizechoice.add("10");
    fontsizechoice.add("12");
    fontsizechoice.add("14");
    fontsizechoice.add("16");
    fontsizechoice.add("18");
    fontsizechoice.add("20");
    fontsizechoice.select("14");
    fontsizechoice.addItemListener(this);
    Panel p = new Panel(new GridLayout());
    p.add(fontnamechoice);
    p.add(fontsizechoice);
    setLayout(new BorderLayout());
    add(p, BorderLayout.NORTH);
    setBounds( 0, 0, 350, 200);
    setVisible(true);
  }

  public void itemStateChanged(ItemEvent e) {
    Object o = e.getSource();
    if(o == null){
    }else if(o == fontnamechoice){
      fontname = ((Choice)o).getSelectedItem();
    }else if(o == fontsizechoice){
      fontsize = Integer.parseInt(((Choice)o).getSelectedItem());
    }
    repaint();
  }

  public static void main(String[] args){
    FontPanel test = new FontPanel();
  }

  public void paint(Graphics g){
    Graphics2D g2 = (Graphics2D)g;
    
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
			RenderingHints.VALUE_ANTIALIAS_ON);
    g2.setBackground(Color.white);
    g2.clearRect(0, 0, getWidth(), getHeight());
    
    g2.setColor(Color.black);

    Font f = new Font(fontname, Font.PLAIN, fontsize);
    g2.setFont(f);
    g2.drawString("あいうえお テスト 思い出", 40, 80);
    g2.drawString("abcedefghijklmn", 40, 110);
    g2.drawString("ABCEDEFGHIJKLMN", 40, 140);
  }
}