interface Drawable void drawint x int y class
インターフェースを使ったプログラム インターフェースの宣言 interface Drawable { void draw(int x, int y); } class Draw. House extends House implements Drawable{ int s = 10; public void draw(int x, int y) { up(); move. To(x, y, 0); down(); クラスがインターフ ェースを実装してい house(s); ェースを実装して ることの宣言 s+= 10; いることの宣言 } } class Draw. Text implements Drawable{ int s = 1; インターフェースの使用 public void draw(int x, int y) { public class T 81 { for(int i = 0; i < x / 10; i++){ public static void main(String args[]){ System. out. print("*"); } Turtle. Frame f = new Turtle. Frame(); for(int i = 0; i < y / 10; i++){ Drawable[] hm = new Drawable[3]; hm[0] = new Draw. House(); … System. out. print("+"); } … System. out. println(""); while(true){ … } hm[n]. draw(x, y); } 5
インターフェースの働き 宣言: drawという名前の メソッドがある interface Drawable { void draw(int x, int y); } class Draw. House extends House implements Drawable{ int s = 10; public void draw(int x, int y) { 実装: クラスに up(); move. To(x, y, 0); down(); クラスがインターフ drawという名前の ェースを実装してい house(s); ることの宣言 メソッドが定義 s+= 10; } } class Draw. Text implements Drawable{ 使用: 型として「drawと int s = 1; public void draw(int x, int y) { いう名前のメソッドがあ public class T 81 { for(int i = 0; i < x / 10; i++){ る」オブジェクトの配列 public static void main(String args[]){ System. out. print("*"); } Turtle. Frame f = new Turtle. Frame(); for(int i = 0; i < y / 10; i++){ Drawable[] hm = new Drawable[3]; hm[0] = new Draw. House(); … System. out. print("+"); } … System. out. println(""); while(true){ … } hm[n]. draw(x, y); } 6
スレッドが1つの プログラムの実行 public class List. Demo { public static void main(String[] args) { f. add. Button("追加"); f. add. Button("削除"); f. add. Button("前進"); f. add. Button("回転"); Turtle. List l = null; while (true) { String command = f. get. Pressed. Button(); if (command. equals(" 追加")) { int x = f. get. Clicked. X("クリックして下さい"); int y = f. get. Clicked. Y(); Turtle m = new Turtle(x, y, 0); f. add(m); public class Turtle. List { m. fd(0); public Turtle first. Turtle; l = new Turtle. List(m, l); public Turtle. List next. Turtles; } else if ( command. equals("前進")) { public Turtle. List(Turtle f, Turtle. List n) { l. forward. All(10); first. Turtle = f; next. Turtles = n; } else if ( command. equals("回転")) { } Turtle. speed. All(Turtle. speed. Fast); public void forward. All(int s) { l. turn. All(); first. Turtle. fd(s); Turtle. speed. All(Turtle. speed. Slow); if (next. Turtles != null) { } else if ( command. equals("削除")){ next. Turtles. forward. All(s); Turtle m = l. first. Turtle; } m. kame. Color = java. awt. Color. red; } m. fd(0); public void turn. All() { l = l. next. Turtles; first. Turtle. lt((int)(Math. random()*360)); } if (next. Turtles != null) { } next. Turtles. turn. All(); } } List. Demo Turtle. List public class Turtle{ boolean with. Kame = true; public static boolean with. Kame. All = true; public Color kame. Color = Color. green; Turtle. Panel f; // set by Turtle. Panel double angle; // turtle current angle double x, y; // turtle current position double dx, dy; // dx = sin(angle), dy = -cos(angle) boolean pen. Down; // pen status (up or down) Color c = Color. black; // pen color int kame. Type = 0; int rx, ry; boolean rubber = false; int move. Wait = 20; int rotate. Wait = 20; public Turtle(int x, int y, int ia) { this. x = ((double)x + 0. 5); this. y = ((double)y + 0. 5); setangle((double)ia *Math. PI/180. 0); pen. Down = true; } public double kame. Scale = 0. 4; public int kame[][] = kame. Fig; public int kame. R[][] = kame. RFig; public int kame. L[][] = kame. LFig; void kame. Draw(Graphics g, int data[][]) { int ix = (int)x, iy = (int)y; g. set. Color(kame. Color); for (int i = 0; i < data. length; i++) { int px = 0, py = 0; for (int j = 0; j < data[i]. length; j += 2) { int kx = data[i][j], ky = data[i][j+1]; int nx = (int)((kx*(-dy) + ky*(-dx)) * kame. Scale); int ny = (int)((kx*dx+ ky*(-dy)) * kame. Scale); if (j > 0) g. draw. Line(ix + px, iy + py, ix + nx, iy + ny); px = nx; py = ny; } } g. set. Color(c); g. fill. Oval(ix - 1, iy - 1, 2, 2); } void show(Graphics g) { Turtle if (rubber) { g. set. Color(c); g. draw. Line(rx, ry, (int)x, (int)y); } if (with. Kame && with. Kame. All) { switch ((kame. Type/2) % 4) { case 0: case 2: kame. Draw(g, kame); break; case 1: 10
スレッドを使ったプログラム import java. awt. Color; public class Multi 91 extends House public class Multi 9 implements Runnable{ im public static void main(String[] args){ Turtle. Frame f = new Turtle. Frame(); int n, s; Multi 91(int x, int y, int a, int n, int s){ super(x, y, a); this. n = n; this. s = s; } public void run(){ Multi 91 m = new Multi 91(200, 0, 10, 50); polygon(n, s); f. add(m); } Multi 91 m 1 = new Multi 91(100, 200, 0, 5, 50); } f. add(m 1); m 1. set. Color(new Color(255, 0, 0)); m 1. speed(5); Thread t = new Thread(m); Thread t 1 = new Thread(m 1); t. start(); t 1. start(); } System. out. println("Main メソッドは終了する. "); int n, s; Multi 91(int x, int y, int a, int super(x, y, a); this. n = n; this. s = s; } } public void run( polygon(n, s); } mのrunから処理を始める スレッドをそれぞれ作る スレッドの処理を 開始させる 11
スレッドを使ったプログラム import java. awt. Color; public class Multi 91 extends House public class Multi 9 implements Runnable{ im 最初のスレッドは mainから始まる public static void main(String[] args){ Turtle. Frame f = new Turtle. Frame(); int n, s; Multi 91(int x, int y, int a, int n, int s){ super(x, y, a); this. n = n; this. s = s; } public void run(){ Multi 91 m = new Multi 91(200, 0, 10, 50); polygon(n, s); f. add(m); } Multi 91 m 1 = new Multi 91(100, 200, 0, 5, 50); } f. add(m 1); m 1. set. Color(new Color(255, 0, 0)); m 1. speed(5); int n, s; Multi 91(int x, int y, int a, int super(x, y, a); this. n = n; this. s = s; } } public void run( polygon(n, s); } Thread t = new Thread(m); Thread t 1 = new Thread(m 1); t. start(); t 1. start(); } System. out. println("Main メソッドは終了する. "); 12
スレッドを使ったプログラム import java. awt. Color; public class Multi 91 extends House public class Multi 9 implements Runnable{ im 同じメソッドを public static void main(String[] args){ Turtle. Frame f = new Turtle. Frame(); int n, s; 新しいスレッドは Multi 91(int x, int y, int a, int n, int s){ super(x, y, a); 引数のオブジェクトの this. n = n; this. s = s; runメソッドから } public void run(){ Multi 91 m = new Multi 91(200, 0, 10, 50); polygon(n, s); f. add(m); } Multi 91 m 1 = new Multi 91(100, 200, 0, 5, 50); } f. add(m 1); m 1. set. Color(new Color(255, 0, 0)); m 1. speed(5); Thread t = new Thread(m); Thread t 1 = new Thread(m 1); t. start(); t 1. start(); } System. out. println("Main メソッドは終了する. "); int n, s; 複数のスレッドが Multi 91(int x, int y, int a, int super(x, y, a); 実行することも this. n = n; this. s = s; できる } } public void run( polygon(n, s); } startは スレッドを動かし始める 13
スレッドを使ったプログラム import java. awt. Color; 1. 2. スレッドが作られる 引数のオブジェクトの runメソッドが出発点になる public static void main(String[] args){ Turtle. Frame f = new Turtle. Frame(); public class Multi 91 extends House implements Runnable{ int n, s; Multi 91(int x, int y, int a, int n, int s){ super(x, y, a); this. n = n; this. s = s; } public void run(){ Multi 91 m = new Multi 91(200, 0, 10, 50); polygon(n, s); f. add(m); } Multi 91 m 1 = new Multi 91(100, 200, 0, 5, 50); } f. add(m 1); m 1. set. Color(new Color(255, 0, 0)); m 1. speed(5); Thread t = new Thread(m); Thread t 1 = new Thread(m 1); t. start(); t 1. start(); } System. out. println("Main メソッドは終了する. "); interface Runnable { void run(); } runメソッドがあるクラスの オブジェクトであれば何でもよい → Runnableインターフェースを 実装したクラスの オブジェクトであればよい 14
- Slides: 20