6 p.1 6 Java GUI GUI paintcomponent GUI mouseclicked, keypressed, actionperformed mouseclicked paintcomponent thread, 1 GUI 6.0.2, mutlithread C

Similar documents
I. java.awt.rectangle java.lang.math random Java TM API java.awt Rectangle Rectangle (x,y)... public int x Rectangle X public int y Rectangle Y public

:30 12:00 I. I V II. III. IV. ( a d) V. VI : A ActionListener aa addactionlistener AE ActionEvent K KeyListener ak addkeyliste

I. (i) Java? (A). 2Apples (B). Vitamin-C (C). Peach21 (D). Pine_Apple (ii) Java? (A). Java (B). Java (C). Java (D). JavaScript Java JavaScript Java (i

I HTML HashMap (i) (ii) :.java import java.net.*; import java.io.*; import java.util.hashmap; public class SimpleStopWatch { public static voi

:30 12:00 I. I VII II. III. IV. ( a d) V. VI : this==null, T == N A ActionListener A addactionlistener C class D actionperforme

:30 12:00 I. I VII II. III. IV. ( a d) V. VI : this==null, T == N A ActionListener A addactionlistener C class D actionperformed

:30 12:00 I. I VII II. III. IV. ( a d) V. VI : this==null, T == N A ActionListener A addactionlistener C class D actionperforme

II Java :30 12:00 I. I IV II. III. IV. ( a d) V. : this==null, T == N A ActionListener C class D actionperformed G getsource I implements K

:30 12:00 I. I VII II. III. IV. ( a d) V. VI : this==null, T == N A ActionListener A addactionlistener C class D actionperformed

Thread

I. (i) Foo public (A). javac Foo.java java Foo.class (C). javac Foo java Foo (ii)? (B). javac Foo.java java Foo (D). javac Foo java Foo.class (A). Jav

I. (i) Java? (A). Foo_Bar (B). G day (C). 999 (D). Golgo13 (ii)? (A). Java public (B). Java (C). Java JavaScript (D). Java C Java C (iii)? (A). Java (

アプレットの作成

I 4 p.2 4 GUI java.awt.event.* import /* 1 */ import mouseclicked MouseListener implement /* 2 */ init addmouselistener(this) this /* 3 */ this mousec

Java 3 p.2 3 Java : boolean Graphics draw3drect fill3drect C int C OK while (1) int boolean switch case C Calendar java.util.calendar A

ガイダンス

5 p Point int Java p Point Point p; p = new Point(); Point instance, p Point int 2 Point Point p = new Point(); p.x = 1; p.y = 2;

ガイダンス

55 7 Java C Java TCP/IP TCP/IP TCP TCP_RO.java import java.net.*; import java.io.*; public class TCP_RO { public static void main(string[] a

r3.dvi

text_13.dvi

r4.dvi

226

text_10.dvi

vol.30.}...`.X...b.h

2 p.2 2 Java Hello0.class JVM Hello0 java > java Hello0.class Hello World! javac Java JVM java JVM : Java > javac 2> Q Foo.java Java : Q B

r14.dvi

2 static final int DO NOTHING ON CLOSE static final int HIDE ON CLOSE static final int DISPOSE ON CLOSE static final int EXIT ON CLOSE void setvisible

Microsoft PowerPoint prog1_doc2x.pptx

やさしいJavaプログラミング -Great Ideas for Java Programming サンプルPDF

Java演習(4) -- 変数と型 --

ÿþ˜u#u·0¹0Æ0à0

Local variable x y i paint public class Sample extends Applet { public void paint( Graphics gc ) { int x, y;... int i=10 ; while ( i < 100 ) {... i +=

目 次 Java GUI 3 1 概要 クラス構成 ソースコード例 課題...7 i

問題1 以下に示すプログラムは、次の処理をするプログラムである

4 p.2 4 GUI return; public void mousepressed(mouseevent e) { /* 5 */ public void mousereleased(mouseevent e) { /* 5 */ public void mouseentered(mousee


2

untitled

r2.dvi

Microsoft PowerPoint prog1_doc2.pptx

r8.dvi

< F2D834F838C A815B A CC>

< F2D B838A835882CC8CF68EAE2E6A7464>

r2.dvi

新・明解Java入門

ガイダンス

ただし 無作為にスレッドを複数実行すると 結果不正やデッドロックが起きる可能性がある 複数のスレッド ( マルチスレッド ) を安全に実行する ( スレッドセーフにする ) ためには 同期処理を用いるこ とが必要になる 同期処理は 予約語 synchronized で行うことができる ここでは sy

Safari AppletViewer Web HTML Netscape Web Web 15-1 Applet Web Applet init Web paint Web start Web HTML stop destroy update init Web paint start Web up

r14.dvi

< F2D82518CC282CC D2E6A7464>

ガイダンス

アプレットⅤ

< F2D F B834E2E6A7464>

Chapter 20. [ ] ; [ ] = new [ ] ; Color colors [ ] = new Color[ 20 ]; // 20 Button operations [ ] = new Button[ 10 ]; // 10 colors[ 3 ] = new Color( 1

try catch Exception Java try catch try { } catch ( Exception e ) { } e 16-1 try catch 0 try { int x = 0; int y = 10 / x; } catch ( Exception e ) { Sys

ガイダンス

Java演習(2) -- 簡単なプログラム --

class IntCell { private int value ; int getvalue() {return value; private IntCell next; IntCell next() {return next; IntCell(int value) {this.value =

GUIプログラムⅤ

IE6 2 BMI chapter1 Java 6 chapter2 Java 7 chapter3 for if 8 chapter4 : BMI 9 chapter5 Java GUI 10 chapter6 11 chapter7 BMI 12 chap

< F2D BCA82CC978E89BA82CC8EC08CB12E6A7464>

Programming-C-9.key

2 p.2 2 Java Hello0.class JVM Hello0 java > java Hello0.class Hello World! javac Java JVM java JVM : Java > javac 2> Q Foo.java Java : Q B

< F2D825282CC947B909482CC A815B83682E6A>

< F2D92DE82E8914B82CC977088D32E6A7464>

1 Java Java GUI , 2 2 jlabel1 jlabel2 jlabel3 jtextfield1 jtextfield2 jtextfield3 jbutton1 jtextfield1 jtextfield2 jtextfield3

シミュレーションの簡単な例 GUI 無しのシミュレーションを作る GUI を作る パラメタを設定するデモンストレーションをする 2 オブジェクト指向プログラミング特論

次の演習課題(1),(2)のプログラムを完成させよ

K227 Java 2

text_12.dvi

< F2D82B682E182F182AF82F12E6A7464>

r6.dvi

3. Java 2 3 Java C ( ) 4 java main 5 byte short int long float 4 7 double 8 15 char 2 ASCII Unic

解きながら学ぶJava入門編

class IntCell { private int value ; int getvalue() {return value; private IntCell next; IntCell next() {return next; IntCell(int value) {this.value =

r3.dvi

< F2D8EA CE909482CC92EA82852E6A7464>

Java言語 第1回

ALG ppt

Object MenuComponent MenuBar MenuItem Menu CheckboxMenuItem

Java 2 p.2 2 Java Hello0.class JVM Hello0 java > java Hello0.class Hello World! javac Java JVM java JVM : Java > javac 2> Q Foo.java Java : Q 2.

PowerPoint Presentation

untitled

目的 泡立ち法を例に Comparableインターフェイスの実装 抽象クラスの利用 型パラメタの利用 比較 入替 の回数を計測

例外(exception)とは何か

表示の更新もそういた作業のひとつに当たる スレッドの使用アニメーション アニメーションやシミュレーションなどは画面の更新が一定のタイミングで行われていく この連続した画面の更新をスレッドを利用して行う しかし paint() メソッドを直接呼び出して表示を更新することはできない その理由

2 p.2 2 Java Hello0.class JVM Hello0 java > java Hello0.class Hello World! javac Java JVM java JVM : Java > javac 2> Q Foo.java Java : Q B

2

Java (9) 1 Lesson Java System.out.println() 1 Java API 1 Java Java 1

10/ / /30 3. ( ) 11/ 6 4. UNIX + C socket 11/13 5. ( ) C 11/20 6. http, CGI Perl 11/27 7. ( ) Perl 12/ 4 8. Windows Winsock 12/11 9. JAV

B 10 : N ip2003f10.tex B : 9/12/ :02 p.1/71

アルゴリズムとデータ構造1

GUIプログラムⅣ

KeyListener init addkeylistener addactionlistener addkeylistener addkeylistener( this ); this.addkeylistener( this ); KeyListener public void keytyped

IT プロジェクト

Java プログラミング Ⅰ 3 回目変 数 今日の講義講義で学ぶ内容 変数とは 変数の使い方 キーボード入力の仕方 変 数 変 数 一時的に値を記憶させておく機能 変数は 型 ( データ型 ) と識別子をもちます 2 型 ( データ型 ) 変数に記憶する値の種類変数の型は 記憶できる値の種類と範囲

Java演習(9) -- クラスとメソッド --

LMNtal LMNtal LMNtal JAVA JAVA JAVA LMNtal LMNtal LMNtal

** 平成 16 年度 FE 午後問題 Java** 示現塾プロジェクトマネージャ テクニカルエンジニア ( ネットワーク ) など各種セミナーを開催中!! 開催日 受講料 カリキュラム等 詳しくは 今すぐアクセス!! 平成 16

Transcription:

6 p.1 6 Java GUI GUI paintcomponent GUI mouseclicked, keypressed, actionperformed mouseclicked paintcomponent 6.0.1 thread, 1 GUI 6.0.2, mutlithread CPU 1 CPU CPU +----+ +----+ +----+ Java 1 CPU

6 p.2 6 6.1 Java run run Runnable MyRunnable Runnable run run Thread.sleep 10 ThreadTest.java class MyRunnable implements Runnable { String name; MyRunnable(String n) { name = n; public void run() { int i; for (i = 0; i < 10; i++) { try { Thread.sleep(10); // 10 catch (InterruptedException e) { System.out.printf("%s: %d, ", name, i); public class ThreadTest { public static void main(string[] args) { Thread ta = new Thread(new MyRunnable("A")); Thread tb = new Thread(new MyRunnable("B")); Thread tc = new Thread(new MyRunnable("C")); ta.start(); tb.start(); tc.start(); // ThreadTest main Thread Runnable new Thread Thread start A: 0, A: 1, B: 0, A: 2, A: 3, B: 1, A: 4, C: 0, A: 5, B: 2, A: 6, A: 7, B: 3, A: 8, C: 1, A: 9, B: 4, B: 5, B: 6, C: 2, B: 7, C: 3, B: 8, C: 4, B: 9, C: 5, C: 6, C: 7, C: 8, C: 9,

6.2. GUI 6 p.3 6.2 GUI 6.2.1 GUI Guruguru.java import java.awt.*; import javax.swing.*; public class Guruguru extends JPanel implements Runnable { private int r = 50, x = 110, y = 70; private double theta = 0; // private volatile Thread thread = null; public Guruguru() { setpreferredsize(new Dimension(200, 180)); JButton startbtn = new JButton("start"); startbtn.addactionlistener(e -> startthread()); JButton stopbtn = new JButton("stop"); stopbtn.addactionlistener(e -> stopthread()); setlayout(new FlowLayout()); add(startbtn); add(stopbtn); startthread(); private void startthread() { if (thread == null) { thread = new Thread(this); thread.start(); private void stopthread() { thread = null; @Override public void paintcomponent(graphics g) { super.paintcomponent(g); // paintcomponent // g.drawstring("hello, World!", x, y); 1 implements Runnable Guruguru run paintcomponent (x, y) Hello, World!

6 p.4 6 thread Thread thread null null C NULL thread volatile startthread Thread this Guruguru run thread thread start stopthread run public void run() { Thread thisthread = Thread.currentThread(); for (; thread == thisthread; theta += 0.02) { x = 60 + (int)(r * Math.cos(theta)); y = 100 - (int)(r * Math.sin(theta)); repaint(); // paintcomponent try { Thread.sleep(30); // 30 catch (InterruptedException e) {... // main Thread currentthread thread run thread == thisthread stopthread thread null x y Thread.sleep 30 Thread.sleep InterruptedException try catch GUI run Thread null

6.2. GUI 6 p.5 implements Runnable Thread null Q 6.2.2 TextAnimation.java TextAnimation.java TextAnimation.java import javax.swing.*; import java.awt.*; public class TextAnimation extends JPanel private int x = 0; private volatile Thread thread = null; { public TextAnimation() { setpreferredsize(new Dimension(200, 150)); JButton startbtn = new JButton("start"); startbtn.addactionlistener(e -> startthread()); JButton stopbtn = new JButton("stop"); stopbtn.addactionlistener(e -> stopthread()); setlayout(new FlowLayout()); add(startbtn); add(stopbtn); startthread(); // startthread, stopthread Guruguru @Override public void paintcomponent(graphics g) { super.paintcomponent(g); g.drawstring("hello WORLD!", x, 100); public void run() { Thread mythread = Thread.currentThread(); while ( ) { x += 5; if (x > 200) { x = 0; try { Thread.sleep(100); // 100

6 p.6 6 catch (InterruptedException e) {... // main 6.2.3 run Guruguru.java Guruguru.java import java.awt.*; import javax.swing.*; public class Guruguru { private int r = 50, x = 110, y = 70; private double theta = 0; // private volatile Thread thread = null;... // private void startthread() { if (thread == null) { thread = new Thread( { /* run */ ); thread.start();... // stopthread, paintcomponent, main 6.2.4 6.3 SwingUtilities.invokeLater GUI Swing paintcomponent actionperformed GUI Swing SwingUtilities.invokeLater Swing

6.3. SwingUtilities.invokeLater 6 p.7 SwingUtilities.invokeLater Runnable Denko.java 1 import javax.swing.*; 2 import java.awt.*; 3 4 public class Denko extends JPanel implements Runnable { 5 private JLabel label; 6 private volatile Thread thread = null; 7 private String msg = "0123456789ABCDEF "; 8 9 public Denko() { 10 label = new JLabel(msg); 11 add(label); 12 JButton startbtn = new JButton("start"); 13 startbtn.addactionlistener(e -> startthread()); 14 JButton stopbtn = new JButton("stop"); 15 stopbtn.addactionlistener(e -> stopthread()); 16 setlayout(new FlowLayout()); 17 add(startbtn); add(stopbtn); 18 startthread(); 19 20 21 // startthread, StopThread Guruguru 22 23 public void run() { 24 Thread thisthread = Thread.currentThread(); 25 for (int i = 0; thread == thisthread; i = (i + 1) % msg.length()) { 26 String str = msg.substring(i) + msg.substring(0, i); 27 SwingUtilities.invokeLater(new Runnable() { 28 public void run() { 29 label.settext(str); 30 31 ); 32 try { 33 Thread.sleep(100); // 100 34 catch (InterruptedException e) { 35 36 37 38... // main 39 Runnable 27 31 SwingUtilities.invokeLater(() -> label.settext(str)); repaint SwingUtilities.invokeLater

6 p.8 6 repaint() SwingUtilities.invokeLater Guruguru SwingUtilities.invokeLater 6.4 6.4.1 BubbleSort1.java import javax.swing.*; import java.awt.*; public class BubbleSort1 extends JPanel implements Runnable { private int[] init = {10, 52, 23, 34, 8, 12, 4, 46, 7, 45, 44, 3; private int[] args = new int[init.length]; private final Color[] cs = { Color.RED, Color.ORANGE, Color.GREEN, Color.BLUE ; private volatile Thread thread = null; private int i, j; public BubbleSort1() { setpreferredsize(new Dimension(300, 200)); startthread(); private void startthread() { if (thread == null) { thread = new Thread(this); thread.start();... bubble sort @Override public void paintcomponent(graphics g) { int k; super.paintcomponent(g); g.setcolor(color.yellow); g.filloval(5, 50 + j * 10, 10, 10); g.setcolor(color.cyan); g.filloval(5, 50 + i * 10, 10, 10); for(k = 0; k < args.length; k++) { g.setcolor(cs[k % cs.length]); g.fillrect(20, 50 + k * 10, args[k] * 5, 10);

6.4. 6 p.9 paintcomponent 3 Graph.java run public void run() { while (true) { for (int k = 0; k < init.length; k++) { args[k] = init[k]; // for (i = 0; i < args.length - 1; i++) { for (j = args.length - 1; j > i; j--) { if (args[j - 1] > args[j]) { // int tmp = args[j - 1]; args[j - 1] = args[j]; args[j] = tmp; repaint(); try { // repaint Thread.sleep(500); catch (InterruptedException e) {... // main 6.4.2 quick sort : static void swap(int[] v, int i, int j) { int tmp = v[i]; v[i] = v[j]; v[j] = tmp; static void qsort(int[] v, int left, int right) { if (left >= right) return; int i = left, j = right; int pivot = v[i + (j - i) / 2]; while (true) { while (v[i] < pivot) i++; while (pivot < v[j]) j--; if (i >= j) break; swap(v, i, j); i++; j--;

6 p.10 6 qsort(v, left, i - 1); qsort(v, j + 1, right); 6.4.3 2 BubbleSort1.java BubbleSort2.java public class BubbleSort2 extends JPanel implements Runnable, ActionListener {... private volatile boolean threadsuspended = true; public BubbleSort2() { setpreferredsize(new Dimension(300, 200)); JButton step = new JButton("Step"); step.addactionlistener(this); setlayout(new FlowLayout()); add(step); startthread(); // startthread BubbleSort1... Runnable ActionListener 2 implements 2, sleep 6.4.1 public void run() { while(true) { for (int k = 0; k < init.length; k++) { args[k] = init[k]; // for (i = 0; i < args.length - 1; i++) { for (j = args.length - 1; j > i; j--) { if (args[j - 1] > args[j]) { // int tmp = args[j - 1]; args[j - 1] = args[j]; args[j] = tmp; repaint();

6.4. 6 p.11 /* repaint */ try { synchronized(this) { while (threadsuspended) { wait(); threadsuspended = true; catch (InterruptedException e) { synchronized synchronized 6.4.2 public synchronized void actionperformed(actionevent e) { // threadsuspended = false; notify(); synchronized synchronized(this) {... public void actionperformed(actionevent e) { synchronized (this) { threadsuspended = false; notify();... // main actionperformed notify threadsuspended actionperformed notify

6 p.12 6 BubbleSort2.java 6.4.4 1 6.5 synchronized synchronized synchronized ( ) synchronized synchronized wait synchronized synchronized x : x = x + 1; synchronized synchronized : 1. x = 0 2. A x + 1 (1) 3. 4. B x + 1 (1) 5. B x 1 6. 7. A x 1

6.6. : 6 p.13 x = x + 1; 2 x 1 x + 1 x synchronized synchronized 6.6 : 6.6.1 : 3 1, 2,..., n n 1 1. 1 2. n 1 n A B : 1. n 1 A C 2. 1 A B 3. n 1 C B output TextArea void hanoi(int n, String a, String b, String c) { if (n > 0) { hanoi(n - 1, a, c, b); output.append(" " + n + " " + a + " " + b + " \n"); hanoi(n - 1, c, b, a);

6 p.14 6 Runnable volatile null Thread.sleep wait notify synchronized