プログラムの 1 行目に自分の名前を入れること // vm12345 杉崎えり子 情報科学 B 第 14 回課題作成 3 情報科学 B Info2/3 info14 今日のフォルダー作成 情報科学 B 第 14 回課題作成 3 Report14_1.java 1
教科書 資料 過去のプログラムを見ながらで OK 課題 3( 提出 ) ボタンとアニメーション ( 第 13 回 ) を組み合わせて オリ ジナルのウィンドウを作成する アニメーションはオリジナルのものを作成すること Report14.java MyPanel14.java 第 15 回授業終了時間までにアップロードを完了させておくこと 情報科学 B 第 14 回課題作成 3 2
教科書 資料 過去のプログラムを見ながらで OK 課題 3( 提出 ) 例 : 1 乱数を使用して占いのプログラム ボタンをクリッ クするとアニメーションの後に結果を表示 アニメーションはオリジナルのものを作成すること 情報科学 B 第 14 回課題作成 3 Report14.java MyPanel14.java 3
教科書 資料 過去のプログラムを見ながらで OK 課題 3( 提出 ) 例 : 2 ボタン1をクリックするとアニメーション1が ボタン2をクリックするとアニメーション2が表示される プログラム アニメーションはオリジナルのものを作成すること 情報科学 B 第 14 回課題作成 3 Report14.java MyPanel14_1.java MyPanel14_2.java 4
6 ボタンとアニメ パネル フレーム anime public class Button extends JFrame implements ActionListener { MyPanel mypnl; 2 3 部品 public Button(){ addactionlistener(this); イベント public void actionperformed(actionevent e) { 1 public static void main(string[] args){ フレーム作成 ボタンクリック時の処理 ヒント mypnl = new MyPanel(); main を含むファイル MyPanel クラスで mypnl オブジェクトを作成するため Button myframe = new Button();
ヒント 6 ボタンとアニメ main を含まない残りの部分 おまじない 1 おまじない 2 class MyPanel extends JPanel implements Runnable { public MyPanel() { 9 ペイント public void paintcomponent(graphics myg) { 書き換え 図形描画 public void run() { Thread refresh = new Thread(this); refresh.start(); While(true){ repaint(); try { Thread.sleep(10); Catch(Exception e) { 9 10msec 待つ 5 4 Runnable インタフェースを実装 Thread クラスのオブジェクトを生成 7 6 スレッドの run メソッドを呼び出す runメソッドの本体を定義 8 paintcomponent() メソッドを呼び出す
グラフィック その他の描画方法 情報科学 B 第 11 回グラフィック 7
復習 JFrame: フレームウィンドウを作成 JButton: ボタンを作成 JLabel: 文字列を表示 JPanel: パネルを作成 パネルにコンポーネント ( ボタン ラベルなど ) を貼り付けられる ImageIcon: 画像ファイルをアイコンとして描画する
1 ウィンドウの作成 1 復習 JFrame クラスを拡張して Window クラスを作成 public class Window extends JFrame{ public Window() { public static void main(string[] args){ フレーム作成 2 JFrameクラスを継承してWindowクラスを作成 ( 更に機能を追加するため ) 3 Window myframe = new Window(); myframe オブジェクトを作成したら (Window) を実施する
2 ボタンの作成 public class Button extends JFrame implements ActionListener { public Button(){ addactionlistener(this); イベント public void actionperformed(actionevent e) { public static void main(string[] args){ フレーム作成 ボタンクリック時の処理 3 復習 4 2 5 1 ボタンクリック処理を行うために リスナーインタフェースを実装 クリックされたときの処理 Button myframe = new Button(); myframe オブジェクトを作成したら (Button) を実施する
復習 3 ボタンクリックでグラフィックス表示 パネル フレーム draw public class Button extends JFrame implements ActionListener { JPanel mypnl; 1 部品 3 public Button(){ mypnl=new JPanel(); フレーム作成 addactionlistener(this); イベント 5 public void actionperformed(actionevent e) { public static void main(string[] args){ ボタンクリック時の処理 Graphics myg=mypnl.getgraphics(); 図形の描画 4 2 JPanel クラスで mypnl オブジェクトを作成 6 myg を使って描画が可能になる Button myframe = new Button();
グラフィックス表示 パネル フレーム public class Gra extends JFrame{ MyPanel mypnl; public Gra() { 図形 フレーム作成 mypnl=new MyPanel(); setvisible(true); 復習 ペイント public class MyPanel extends JPanel { public void paintcomponent(graphics myg){ super.paintcomponent(myg); 2 自作 MyPanelクラスで mypnlオブジェクトを作成 4 paintcomponent() を呼び出し JPanel クラスに手を加えて MyPanel クラスを作成 が書いてないので JPanel のを実施 図形描画 5 JPanel( 親 ) のpaintComponent を実施後 以下の自作部分を 実施 public static void main(string[] args){ 1 Gra myframe = new Gra(); 3
復習 ファイルを分割 5 アニメーションの作成 public class Anime extends JFrame{ MyPanel mypnl; 2 public Anime() { パネル作成 mypnl = new MyPanel(); JFrame クラスを拡張して Anime クラスを作成 main を含むファイル 1 public static void main(string[] args){ Anime myframe = new Anime(); myframe オブジェクトを作成したら (Anime) を実施する
5 アニメーションの作成 おまじない 1 おまじない 2 class MyPanel extends JPanel implements Runnable { public MyPanel() { 8 ペイント public void paintcomponent(graphics myg) { 書き換え 図形描画 復習 Thread refresh = new Thread(this); refresh.start(); public void run() { while(true) { repaint(); try { Thread.sleep(10); 8 catch(exception e) { 10msec 待つ main を含まない残りの部分 4 3 Runnable インタフェースを実装 Thread クラスのオブジェクトを生成 6 5 スレッドの run メソッドを呼び出す run メソッドの本体を定義 7 paintcomponent() メソッドを呼び出す