1 『クラスとインスタンス』

1.1 クラスとインスタンスの関係を理解するためのサンプルプログラム

1.1.1 OOTraceSampleクラス(main()のあるクラス)

リスト 1.1.1.1 OOTraceSample.java
  1: /**
  2:  * OOTraceSample.java
  3:  * オブジェクト指向(クラス/インスタンス)の動作を理解するためのサンプルプログラム
  4:  * 
  5:  * @author macchan
  6:  * @date 2005/06/02 オブプロ第7回
  7:  * @version 1.0
  8:  */
  9: public class OOTraceSample {
 10: 
 11: 	public static void main(String[] args) {
 12: 		OOTraceSample sample = new OOTraceSample();
 13: 		sample.main();
 14: 	}
 15: 
 16: 	void main() {
 17: 		Point p1;
 18: 		Point p2;
 19: 		Point p3 = null;
 20: 
 21: 		p1 = new Point();
 22: 		p1.x = 100;
 23: 		p1.y = 100;
 24: 
 25: 		p2 = new Point();
 26: 		p2.x = 200;
 27: 		p2.y = 200;
 28: 
 29: 		System.out.println("p1.x=" + p1.x + ", p1.y=" + p1.y);
 30: 		System.out.println("p2.x=" + p2.x + ", p2.y=" + p2.y);
 31: 
 32: 		p1.addX(50);
 33: 		p2.setX(50);
 34: 
 35: 		System.out.println("p1.x=" + p1.x + ", p1.y=" + p1.y);
 36: 		System.out.println("p2.x=" + p2.x + ", p2.y=" + p2.y);
 37: 
 38: 		p1 = p2;
 39: 
 40: 		p1.x = 800;
 41: 
 42: 		System.out.println("p1.x=" + p1.x + ", p1.y=" + p1.y);
 43: 		System.out.println("p2.x=" + p2.x + ", p2.y=" + p2.y);
 44: 
 45:     p3.x = 100;
 46: 	}
 47: 
 48: }

1.1.2 Pointクラス

リスト 1.1.2.1 Point.java
  1: /**
  2:  * Point.java
  3:  * オブジェクト指向(クラス/インスタンス)の動作を理解するためのサンプルプログラム
  4:  * 
  5:  * @author macchan
  6:  * @date 2005/06/02 オブプロ第7回
  7:  * @version 1.0
  8:  */
  9: public class Point {
 10: 
 11: 	int x;
 12: 	int y;
 13: 
 14: 	/**
 15: 	 * xを設定する
 16: 	 */
 17: 	void setX(int x) {
 18: 		this.x = x;
 19: 	}
 20: 
 21: 	/**
 22: 	 * xを増やす
 23: 	 */
 24: 	void addX(int x) {
 25: 		this.x = this.x + x;
 26: 	}
 27: 
 28: }

1.2 初心者用GUI(BWindowライブラリ)の使い方

1.2.1 GUI例題(1)(ウインドウの出し方と描画の方法)

1.2.1.1 プログラム

リスト 1.2.1.1.1 GUISample1.java
  1: import java.awt.Color;
  2: 
  3: import obpro.gui.BCanvas;
  4: import obpro.gui.BWindow;
  5: 
  6: /**
  7:  * GUI例題プログラム1
  8:  * アニメーションの方法1(ウインドウの出し方と描画の方法)
  9:  * 
 10:  * @author macchan
 11:  * @date 2005/06/02 オブプロ第7回 2006/05/21改訂
 12:  * @version 1.0
 13:  */
 14: public class GUISample1 {
 15: 
 16: 	public static void main(String[] args) {
 17: 		GUISample1 sample = new GUISample1();
 18: 		sample.main();
 19: 	}
 20: 
 21: 	//色の定数(色クラスを使うためにはimport文が必要)
 22: 	final Color BLACK = new Color(0, 0, 0);
 23: 	final Color RED = new Color(255, 0, 0);
 24: 	final Color GREEN = new Color(0, 255, 0);
 25: 	final Color BLUE = new Color(0, 0, 255);
 26: 
 27: 	void main() {
 28: 		BWindow window;
 29: 
 30: 		{// ウインドウを開く
 31: 			// ウインドウを生成する
 32: 			window = new BWindow();
 33: 
 34: 			// 位置と大きさを決める
 35: 			window.setLocation(100, 100);
 36: 			window.setSize(640, 480);
 37: 
 38: 			// ウインドウを開く
 39: 			window.show();
 40: 		}
 41: 
 42: 		{// アニメーションをする
 43: 			// キャンバスを取得する
 44: 			BCanvas canvas = window.getCanvas();
 45: 
 46: 			// アニメーションをする
 47: 			while (true) {
 48: 				// 図形を描く
 49: 				{
 50: 					// キャンバスをクリアする
 51: 					canvas.clear();
 52: 
 53: 					// 図形を描く
 54: 					canvas.drawLine(BLACK, 100, 100, 200, 200);// 線
 55: 					canvas.drawFillTriangle(BLUE, 300, 300, 300, 350, 350, 300);// 塗りつぶし三角形
 56: 					canvas.drawArc(RED, 300, 100, 200, 200, 0, 180);// 円弧
 57: 					canvas.drawFillArc(GREEN, 300, 100, 200, 200, 0, 360);// 塗りつぶし円弧
 58: 
 59: 					// キャンバスを更新する
 60: 					canvas.update();
 61: 				}
 62: 
 63: 				// 眠る
 64: 				canvas.sleep(0.1);
 65: 			}
 66: 		}
 67: 	}
 68: 
 69: }

1.2.2 GUI例題(2)(アニメーションの方法)

1.2.2.1 プログラム

リスト 1.2.2.1.1 GUISample2.java
  1: import java.awt.Color;
  2: 
  3: import obpro.gui.BCanvas;
  4: import obpro.gui.BWindow;
  5: 
  6: /**
  7:  * GUI例題プログラム2
  8:  * アニメーションの方法2(動かす)
  9:  * 
 10:  * @author macchan
 11:  * @date 2005/06/02 オブプロ第7回 2006/05/21改訂
 12:  * @version 1.0
 13:  */
 14: public class GUISample2 {
 15: 
 16: 	public static void main(String[] args) {
 17: 		GUISample2 sample = new GUISample2();
 18: 		sample.main();
 19: 	}
 20: 
 21: 	//色の定数(色クラスを使うためにはimport文が必要)
 22: 	final Color BLACK = new Color(0, 0, 0);
 23: 	final Color RED = new Color(255, 0, 0);
 24: 	final Color GREEN = new Color(0, 255, 0);
 25: 	final Color BLUE = new Color(0, 0, 255);
 26: 
 27: 	void main() {
 28: 		BWindow window;
 29: 
 30: 		{// ウインドウを開く
 31: 			// ウインドウを生成する
 32: 			window = new BWindow();
 33: 
 34: 			// 位置と大きさを決める
 35: 			window.setLocation(100, 100);
 36: 			window.setSize(640, 480);
 37: 
 38: 			// ウインドウを開く
 39: 			window.show();
 40: 		}
 41: 
 42: 		{// アニメーションをする
 43: 			// キャンバスを取得する
 44: 			BCanvas canvas = window.getCanvas();
 45: 
 46: 			// アニメーションをする
 47: 			int arcX = 0;
 48: 			while (true) {
 49: 				// 図形を動かす
 50: 				arcX = arcX + 10;
 51: 
 52: 				{// 図形を描く
 53: 					// キャンバスをクリアする
 54: 					canvas.clear();
 55: 
 56: 					// 図形を描く
 57: 					canvas.drawLine(BLACK, 100, 100, 200, 200);// 線
 58: 					canvas
 59: 							.drawFillTriangle(BLUE, arcX, 300, 300, 350, 350,
 60: 									300);// 塗りつぶし三角形
 61: 					canvas.drawArc(RED, arcX, 100, 200, 200, 0, 180);// 円弧
 62: 					canvas.drawFillArc(GREEN, arcX, 100, 200, 200, 0, 360);// 塗りつぶし円弧
 63: 
 64: 					// キャンバスを更新する
 65: 					canvas.update();
 66: 				}
 67: 
 68: 				// 眠る
 69: 				canvas.sleep(0.1);
 70: 			}
 71: 		}
 72: 	}
 73: 
 74: }

1.3 四角形のアニメーション

1.3.1 設計

1.3.2 四角形のアニメーション(1)(べた書き)

リスト 1.3.2.1 RectangleAnimation1.java
  1: import java.awt.Color;
  2: 
  3: import obpro.gui.BCanvas;
  4: import obpro.gui.BWindow;
  5: 
  6: /**
  7:  * 四角形のアニメーション1(べた書き)
  8:  * 
  9:  * @author macchan
 10:  * @date 2005/06/02 オブプロ第7回
 11:  * @version 1.0
 12:  */
 13: public class RectangleAnimation1 {
 14: 
 15: 	public static void main(String[] args) {
 16: 		RectangleAnimation1 rectangleAnimation = new RectangleAnimation1();
 17: 		rectangleAnimation.main();
 18: 	}
 19: 
 20: 	final Color BLACK = new Color(0, 0, 0);
 21: 	final Color RED = new Color(255, 0, 0);
 22: 
 23: 	BWindow window;
 24: 
 25: 	void main() {
 26: 		openWindow();
 27: 		doAnimation();
 28: 	}
 29: 
 30: 	//ウインドウを開く
 31: 	void openWindow() {
 32: 		window = new BWindow();
 33: 		window.setLocation(100, 100);
 34: 		window.setSize(640, 480);
 35: 		window.show();
 36: 	}
 37: 
 38: 	//アニメーションする
 39: 	void doAnimation() {
 40: 		//キャンバスを取得する
 41: 		BCanvas canvas = window.getCanvas();
 42: 
 43: 		//四角形1のデータを初期化する
 44: 		int x1 = 100;
 45: 		int y1 = 100;
 46: 		int w1 = 100;
 47: 		int h1 = 100;
 48: 
 49: 		//四角形2のデータを初期化する
 50: 		int x2 = 300;
 51: 		int y2 = 200;
 52: 		int w2 = 50;
 53: 		int h2 = 80;
 54: 
 55: 		//アニメーションする
 56: 		while (true) {
 57: 			{//図形を動かす
 58: 				//四角形1
 59: 				x1 = x1 + 10;
 60: 				y1 = y1 + 10;
 61: 
 62: 				//四角形2
 63: 				x2 = x2 + 5;
 64: 			}
 65: 
 66: 			{//図形を描く
 67: 				//キャンバスをクリアする
 68: 				canvas.clear();
 69: 
 70: 				{//図形を描く
 71: 					//四角形1
 72: 					canvas.drawLine(BLACK, x1, y1, x1 + w1, y1);
 73: 					canvas.drawLine(BLACK, x1 + w1, y1, x1 + w1, y1 + h1);
 74: 					canvas.drawLine(BLACK, x1 + w1, y1 + h1, x1, y1 + h1);
 75: 					canvas.drawLine(BLACK, x1, y1 + h1, x1, y1);
 76: 
 77: 					//四角形2
 78: 					canvas.drawLine(RED, x2, y2, x2 + w2, y2);
 79: 					canvas.drawLine(RED, x2 + w2, y2, x2 + w2, y2 + h2);
 80: 					canvas.drawLine(RED, x2 + w2, y2 + h2, x2, y2 + h2);
 81: 					canvas.drawLine(RED, x2, y2 + h2, x2, y2);
 82: 				}
 83: 
 84: 				//キャンバスを更新する
 85: 				canvas.update();
 86: 			}
 87: 
 88: 			//眠る
 89: 			canvas.sleep(0.1);
 90: 		}
 91: 	}
 92: 
 93: }

1.3.3 四角形のアニメーション(2)(メソッド化)

リスト 1.3.3.1 RectangleAnimation2.java
  1: import java.awt.Color;
  2: 
  3: import obpro.gui.BCanvas;
  4: import obpro.gui.BWindow;
  5: 
  6: /**
  7:  * 四角形のアニメーション2(メソッド化)
  8:  * 
  9:  * @author macchan
 10:  * @date 2005/06/02 オブプロ第7回
 11:  * @version 1.0
 12:  */
 13: public class RectangleAnimation2 {
 14: 
 15: 	public static void main(String[] args) {
 16: 		RectangleAnimation2 rectangleAnimation = new RectangleAnimation2();
 17: 		rectangleAnimation.main();
 18: 	}
 19: 
 20: 	final Color BLACK = new Color(0, 0, 0);
 21: 	final Color RED = new Color(255, 0, 0);
 22: 
 23: 	BWindow window;
 24: 
 25: 	void main() {
 26: 		openWindow();
 27: 		doAnimation();
 28: 	}
 29: 
 30: 	//ウインドウを開く
 31: 	void openWindow() {
 32: 		window = new BWindow();
 33: 		window.setLocation(100, 100);
 34: 		window.setSize(640, 480);
 35: 		window.show();
 36: 	}
 37: 
 38: 	//アニメーションする
 39: 	void doAnimation() {
 40: 		//キャンバスを取得する
 41: 		BCanvas canvas = window.getCanvas();
 42: 
 43: 		//四角形1のデータを初期化する
 44: 		int x1 = 100;
 45: 		int y1 = 100;
 46: 		int w1 = 100;
 47: 		int h1 = 100;
 48: 
 49: 		//四角形2のデータを初期化する
 50: 		int x2 = 300;
 51: 		int y2 = 200;
 52: 		int w2 = 50;
 53: 		int h2 = 80;
 54: 
 55: 		//アニメーションする
 56: 		while (true) {
 57: 			{//図形を動かす
 58: 				//四角形1
 59: 				x1 = x1 + 10;
 60: 				y1 = y1 + 10;
 61: 
 62: 				//四角形2
 63: 				x2 = x2 + 5;
 64: 			}
 65: 
 66: 			{//図形を描く
 67: 				//キャンバスをクリアする
 68: 				canvas.clear();
 69: 
 70: 				//図形を描く
 71: 				drawRectangle(canvas, BLACK, x1, y1, w1, h1);
 72: 				drawRectangle(canvas, RED, x2, y2, w2, h2);
 73: 
 74: 				//キャンバスを更新する
 75: 				canvas.update();
 76: 			}
 77: 
 78: 			//眠る
 79: 			canvas.sleep(0.1);
 80: 		}
 81: 	}
 82: 
 83: 	//四角形を書く
 84: 	void drawRectangle(BCanvas canvas, Color color, int x1, int y1, int w1,
 85: 			int h1) {
 86: 		canvas.drawLine(color, x1, y1, x1 + w1, y1);
 87: 		canvas.drawLine(color, x1 + w1, y1, x1 + w1, y1 + h1);
 88: 		canvas.drawLine(color, x1 + w1, y1 + h1, x1, y1 + h1);
 89: 		canvas.drawLine(color, x1, y1 + h1, x1, y1);
 90: 	}
 91: 
 92: }

1.3.4 四角形のアニメーション(3)(データオブジェクト)

リスト 1.3.4.1 RectangleAnimation3.java
  1: import java.awt.Color;
  2: 
  3: import obpro.gui.BCanvas;
  4: import obpro.gui.BWindow;
  5: 
  6: /**
  7:  * 四角形のアニメーション3(データオブジェクト)
  8:  * 
  9:  * @author macchan
 10:  * @date 2005/06/02 オブプロ第7回
 11:  * @version 1.0
 12:  */
 13: public class RectangleAnimation3 {
 14: 
 15: 	public static void main(String[] args) {
 16: 		RectangleAnimation3 rectangleAnimation = new RectangleAnimation3();
 17: 		rectangleAnimation.main();
 18: 	}
 19: 
 20: 	final Color BLACK = new Color(0, 0, 0);
 21: 	final Color RED = new Color(255, 0, 0);
 22: 
 23: 	BWindow window;
 24: 
 25: 	void main() {
 26: 		openWindow();
 27: 		doAnimation();
 28: 	}
 29: 
 30: 	//ウインドウを開く
 31: 	void openWindow() {
 32: 		window = new BWindow();
 33: 		window.setLocation(100, 100);
 34: 		window.setSize(640, 480);
 35: 		window.show();
 36: 	}
 37: 
 38: 	//アニメーションする
 39: 	void doAnimation() {
 40: 		//キャンバスを取得する
 41: 		BCanvas canvas = window.getCanvas();
 42: 
 43: 		//四角形1のデータを初期化する
 44: 		RectangleData rectangle1 = new RectangleData();
 45: 		rectangle1.color = BLACK;
 46: 		rectangle1.x = 100;
 47: 		rectangle1.y = 100;
 48: 		rectangle1.width = 100;
 49: 		rectangle1.height = 100;
 50: 
 51: 		//四角形2のデータを初期化する
 52: 		RectangleData rectangle2 = new RectangleData();
 53: 		rectangle2.color = RED;
 54: 		rectangle2.x = 300;
 55: 		rectangle2.y = 200;
 56: 		rectangle2.width = 50;
 57: 		rectangle2.height = 80;
 58: 
 59: 		//アニメーションする
 60: 		while (true) {
 61: 			{//図形を動かす
 62: 				//四角形1
 63: 				rectangle1.x = rectangle1.x + 10;
 64: 				rectangle1.y = rectangle1.y + 10;
 65: 
 66: 				//四角形2
 67: 				rectangle2.x = rectangle2.x + 5;
 68: 			}
 69: 
 70: 			{//図形を描く
 71: 				//キャンバスをクリアする
 72: 				canvas.clear();
 73: 
 74: 				//図形を描く
 75: 				drawRectangle(canvas, rectangle1);
 76: 				drawRectangle(canvas, rectangle2);
 77: 
 78: 				//キャンバスを更新する
 79: 				canvas.update();
 80: 			}
 81: 
 82: 			//眠る
 83: 			canvas.sleep(0.1);
 84: 		}
 85: 	}
 86: 
 87: 	//四角形を書く
 88: 	void drawRectangle(BCanvas canvas, RectangleData r) {
 89: 		canvas.drawLine(r.color, r.x, r.y, r.x + r.width, r.y);
 90: 		canvas.drawLine(r.color, r.x + r.width, r.y, r.x + r.width, r.y
 91: 				+ r.height);
 92: 		canvas.drawLine(r.color, r.x + r.width, r.y + r.height, r.x, r.y
 93: 				+ r.height);
 94: 		canvas.drawLine(r.color, r.x, r.y + r.height, r.x, r.y);
 95: 	}
 96: }
リスト 1.3.4.2 RectangleData.java
  1: import java.awt.Color;
  2: 
  3: /**
  4:  * 四角形のデータを表現するクラス
  5:  */
  6: public class RectangleData {
  7: 
  8: 	Color color;
  9: 	int x = 0;
 10: 	int y = 0;
 11: 	int width = 0;
 12: 	int height = 0;
 13: 
 14: }

1.3.5 四角形のアニメーション(4)(カプセル化)

リスト 1.3.5.1 RectangleAnimation4.java
  1: import java.awt.Color;
  2: 
  3: import obpro.gui.BCanvas;
  4: import obpro.gui.BWindow;
  5: 
  6: /**
  7:  * 四角形のアニメーション4(カプセル化)
  8:  * 
  9:  * @author macchan
 10:  * @date 2005/06/02 オブプロ第7回
 11:  * @version 1.0
 12:  */
 13: public class RectangleAnimation4 {
 14: 
 15: 	public static void main(String[] args) {
 16: 		RectangleAnimation4 rectangleAnimation = new RectangleAnimation4();
 17: 		rectangleAnimation.main();
 18: 	}
 19: 
 20: 	final Color BLACK = new Color(0, 0, 0);
 21: 	final Color RED = new Color(255, 0, 0);
 22: 
 23: 	BWindow window;
 24: 
 25: 	void main() {
 26: 		openWindow();
 27: 		doAnimation();
 28: 	}
 29: 
 30: 	//ウインドウを開く
 31: 	void openWindow() {
 32: 		window = new BWindow();
 33: 		window.setLocation(100, 100);
 34: 		window.setSize(640, 480);
 35: 		window.show();
 36: 	}
 37: 
 38: 	//アニメーションする
 39: 	void doAnimation() {
 40: 		//キャンバスを取得する
 41: 		BCanvas canvas = window.getCanvas();
 42: 
 43: 		//四角形1のデータを初期化する
 44: 		CapsuledRectangle rectangle1 = new CapsuledRectangle();
 45: 		rectangle1.initialize(BLACK, 100, 100, 100, 100);
 46: 
 47: 		//四角形2のデータを初期化する
 48: 		CapsuledRectangle rectangle2 = new CapsuledRectangle();
 49: 		rectangle2.initialize(RED, 300, 200, 50, 80);
 50: 
 51: 		//アニメーションする
 52: 		while (true) {
 53: 			//図形を動かす
 54: 			rectangle1.move(10, 10);
 55: 			rectangle2.move(5, 0);
 56: 
 57: 			{//図形を描く
 58: 				//キャンバスをクリアする
 59: 				canvas.clear();
 60: 
 61: 				//図形を描く
 62: 				rectangle1.draw(canvas);
 63: 				rectangle2.draw(canvas);
 64: 
 65: 				//キャンバスを更新する
 66: 				canvas.update();
 67: 			}
 68: 
 69: 			//眠る
 70: 			canvas.sleep(0.1);
 71: 		}
 72: 	}
 73: }
リスト 1.3.5.2 CapsuledRectangle.java
  1: import java.awt.Color;
  2: 
  3: import obpro.gui.BCanvas;
  4: 
  5: /**
  6:  * 四角形を表現するクラス
  7:  */
  8: public class CapsuledRectangle {
  9: 
 10: 	private Color color;
 11: 	private int x = 0;
 12: 	private int y = 0;
 13: 	private int width = 0;
 14: 	private int height = 0;
 15: 
 16: 	/**
 17: 	 * 初期化する
 18: 	 */
 19: 	public void initialize(Color color, int x, int y, int width, int height) {
 20: 		this.color = color;
 21: 		this.x = x;
 22: 		this.y = y;
 23: 		this.width = width;
 24: 		this.height = height;
 25: 	}
 26: 
 27: 	/**
 28: 	 * 動かす
 29: 	 */
 30: 	public void move(int moveX, int moveY) {
 31: 		x = x + moveX;
 32: 		y = y + moveY;
 33: 	}
 34: 
 35: 	/**
 36: 	 * 描く
 37: 	 */
 38: 	public void draw(BCanvas canvas) {
 39: 		canvas.drawLine(color, x, y, x + width, y);
 40: 		canvas.drawLine(color, x + width, y, x + width, y + height);
 41: 		canvas.drawLine(color, x + width, y + height, x, y + height);
 42: 		canvas.drawLine(color, x, y + height, x, y);
 43: 	}
 44: }

1.3.6 四角形のアニメーション(5)(コンストラクタ&フレームワークを意識したメソッド化)

リスト 1.3.6.1 RectangleAnimation5.java
  1: import java.awt.Color;
  2: 
  3: import obpro.gui.BCanvas;
  4: import obpro.gui.BWindow;
  5: 
  6: /**
  7:  * 四角形のアニメーション5(コンストラクタ & フレームワークを意識したメソッド化)
  8:  * 
  9:  * @author macchan
 10:  * @date 2005/06/02 オブプロ第7回
 11:  * @version 1.0
 12:  */
 13: public class RectangleAnimation5 {
 14: 
 15: 	public static void main(String[] args) {
 16: 		RectangleAnimation5 rectangleAnimation = new RectangleAnimation5();
 17: 		rectangleAnimation.main();
 18: 	}
 19: 
 20: 	final Color BLACK = new Color(0, 0, 0);
 21: 	final Color RED = new Color(255, 0, 0);
 22: 
 23: 	BWindow window;
 24: 
 25: 	Rectangle rectangle1;
 26: 	Rectangle rectangle2;
 27: 
 28: 	void main() {
 29: 		openWindow();
 30: 		doAnimation();
 31: 	}
 32: 
 33: 	//ウインドウを開く
 34: 	void openWindow() {
 35: 		window = new BWindow();
 36: 		window.setLocation(100, 100);
 37: 		window.setSize(640, 480);
 38: 		window.show();
 39: 	}
 40: 
 41: 	//アニメーションする
 42: 	void doAnimation() {
 43: 		//前処理
 44: 		BCanvas canvas = window.getCanvas();//キャンバスを取得する
 45: 		initializeObjects();//オブジェクトの初期化をする
 46: 
 47: 		//アニメーションする
 48: 		while (true) {
 49: 			//図形を動かす
 50: 			move();
 51: 
 52: 			//図形を描く
 53: 			canvas.clear();
 54: 			draw(canvas);
 55: 			canvas.update();
 56: 
 57: 			//眠る
 58: 			canvas.sleep(0.1);
 59: 		}
 60: 	}
 61: 
 62: 	//オブジェクトを初期化する
 63: 	void initializeObjects() {
 64: 		rectangle1 = new Rectangle(BLACK, 100, 100, 100, 100);
 65: 		rectangle2 = new Rectangle(RED, 300, 200, 50, 80);
 66: 	}
 67: 
 68: 	//オブジェクトを動かす
 69: 	void move() {
 70: 		rectangle1.move(10, 10);
 71: 		rectangle2.move(5, 0);
 72: 	}
 73: 
 74: 	//オブジェクトを描画する
 75: 	void draw(BCanvas canvas) {
 76: 		rectangle1.draw(canvas);
 77: 		rectangle2.draw(canvas);
 78: 	}
 79: 
 80: }
リスト 1.3.6.2 Rectangle.java
  1: import java.awt.Color;
  2: 
  3: import obpro.gui.BCanvas;
  4: 
  5: /**
  6:  * 四角形を表現するクラス
  7:  */
  8: public class Rectangle {
  9: 
 10: 	private Color color;
 11: 	private int x = 0;
 12: 	private int y = 0;
 13: 	private int width = 0;
 14: 	private int height = 0;
 15: 
 16: 	/**
 17: 	 * コンストラクタ
 18: 	 */
 19: 	public Rectangle(Color color, int x, int y, int width, int height) {
 20: 		this.color = color;
 21: 		this.x = x;
 22: 		this.y = y;
 23: 		this.width = width;
 24: 		this.height = height;
 25: 	}
 26: 
 27: 	/**
 28: 	 * 動かす
 29: 	 */
 30: 	public void move(int moveX, int moveY) {
 31: 		x = x + moveX;
 32: 		y = y + moveY;
 33: 	}
 34: 
 35: 	/**
 36: 	 * 描く
 37: 	 */
 38: 	public void draw(BCanvas canvas) {
 39: 		canvas.drawLine(color, x, y, x + width, y);
 40: 		canvas.drawLine(color, x + width, y, x + width, y + height);
 41: 		canvas.drawLine(color, x + width, y + height, x, y + height);
 42: 		canvas.drawLine(color, x, y + height, x, y);
 43: 	}
 44: }