day14_DrawShape

육희영·2021년 10월 28일
0
package com.java1.day14;

public class DrawShape {

	public static void main(String[] args) {
		PointEx[] p = {	new PointEx(100, 100),
						new PointEx(140, 150),
						new PointEx(200, 100)
		};
		
		Triangle t = new Triangle(p);
		Circle c = new Circle(new PointEx(150, 150), 50);
		
		t.draw();	//삼각형을 그린다.
		c.draw();	//원을 그린다.
	}
}

class Shape {
	String color = "black";
	void draw() {
		System.out.printf("[color=%s]%n", color);
	}
}

class PointEx {
	int x;
	int y;
	
	PointEx(int x, int y){
		this.x = x;
		this.y =y;
	}
	
	PointEx(){
		this(0, 0);
	}
	
	String getXY() {
		return "(" + x + "," + y + ")";	//x와 y의 값을 문자열로 반환
	}
}

class Circle extends Shape {
	PointEx center; //원의 원점좌표
	int r ;		  //반지름
	
	Circle() {		
		this(new PointEx(0, 0), 100);	
		// Circle(Point center, int r)를 호출..
	}
	
	Circle(PointEx center, int r){
		this.center = center;
		this.r = r;
	}
	
	void draw() {	//원을 그리는 대신 원의 정보를 출력 하도록 했다.
		System.out.printf("[center=(%d, %d), r=%d, color=%s]%n", center.x, center.y, r, color);
	}
}

class Triangle extends Shape {
	PointEx[] p = new PointEx[3];	//3개의 PointEx 인스턴스를 담을 배열을 생성한다.
	
	Triangle(PointEx[] p) {
		this.p = p;
	}
	
	void draw() {
		System.out.printf("[p1=%s, p2=%s, p3=%s, color=%s]%n", p[0].getXY(), p[1].getXY(), p[2].getXY(), color );
	}
}

출력결과

[p1=(100,100), p2=(140,150), p3=(200,100), color=black]
[center=(150, 150), r=50, color=black]

0개의 댓글

관련 채용 정보