//=============================================C // Appletによる指定大きさの四角形描画 C // 描画ボタンと一辺の長さnをメニューに表示 C // 2009年5月4日 後 保範(東京工芸大学) C //=============================================C import java.applet.*; import java.awt.*; import java.awt.event.*; public class applet1 extends Applet { int n=200; Button btn; //表示ボタンの設定 TextField n_v; //長さnのメニュー設定 // 初期値の設定 public void init() { Label La=new Label("一辺の長さ="); //テキスト表示 n_v = new TextField("200",2); //nの入力画面 btn = new Button("表示"); //表示ボタン add(La); add(n_v); add(btn); //パネルへ追加 btn.addActionListener(new ActionAdp()); //表示動作 } // 表示動作のの実装クラス class ActionAdp implements ActionListener { public void actionPerformed(ActionEvent e) { if (e.getSource() == btn) //表示ボタンがON { n = Integer.valueOf(n_v.getText() ).intValue(); //nの値入力 repaint(); } //描画処理 } } // 四角形の描画処理 public void paint(Graphics g) { g.setColor(Color.white); g.fillRect(0,0,400,400); g.setColor(Color.black); int x = 200 - n/2; g.drawRect(x,x,n,n); } }