테트리스 만드는 문제
Rectangle 클래스
import java.awt.Point;
class Rectangle {
public static final int WIDTH = 50;
public static final int HEIGHT = 50;
private Point upperLeft;
private Point lowerRight;
public Rectangle(Point ul) {
upperLeft = ul;
lowerRight = new Point((int) ul.getX() + WIDTH, (int) ul.getY() + HEIGHT);
}
public Rectangle(Point ul, Point lr) {
upperLeft = ul;
lowerRight = lr;
}
public void translate(int x, int y) {
upperLeft.translate(x, y);
lowerRight.translate(x, y);
}
public String toString() {
return "UL: " + upperLeft.toString() + ", LR: " + lowerRight.toString();
}
}
Block 클래스
class Block {
private Rectangle[] rectangles;
public Block(int x, int y) {
rectangles = new Rectangle[4];
/* rectangles[1] = new Rectangle(x + Rectangle.WIDTH, y);
rectangles[2] = new Rectangle(x, y + Rectangle.HEIGHT);
rectangles[3] = new Rectangle(x + Rectangle.WIDTH, y + Rectangle.HEIGHT);
*/ }
public void translate(int x, int y) {
for (int i = 0; i < rectangles.length; i++) {
rectangles[i].translate(x, y);
}
/* for (Rectangle r : rectangles) {
r.translate(x, y);
}*/
}
public String toString() {
return rectangles[0].toString() + "\n" + rectangles[1].toString() + "\n"
+ rectangles[2].toString() + "\n" + rectangles[3].toString();
// return "UL: " + upperLeft.toString() + ", LR: " + lowerRight.toString();
}
public void setRectangle(int idx, Rectangle r) {
rectangles[idx] = r;
}
}
Block 1
ㅁㅁ
ㅁㅁ 형태
import java.awt.Point;
class Block1 extends Block {
public Block1(int x, int y) {
super(x, y);
setRectangle(0, new Rectangle(new Point(x, y)));
setRectangle(1, new Rectangle(new Point(x + Rectangle.WIDTH, y)));
setRectangle(2, new Rectangle(new Point(x, y + Rectangle.HEIGHT)));
setRectangle(3, new Rectangle(new Point(x + Rectangle.WIDTH, y + Rectangle.HEIGHT)));
}
}
class Block1 extends Block
class 자식 extends 부모
- extends 키워드는 클래스를 다른 클래스의 자식으로 만듬.
super(x,y)
- 부모 클래스로부터 상속받은 필드나 메소드를 자식 클래스에서 참조하는 데 사용하는 참조 변수
main 문
import java.awt.Point;
public class Main {
public static void main(String[] args) {
Block1 b1=new Block1(10,20);
System.out.println(b1.toString());
}
}
결과:
setRectangle 클래스 생각을 어캐하지..아아니 그리고 나는 up만 구하는게 아니고 사각형 네가지 꼭짓점 다
생성해야 하는줄 알고 어렵게 생각하고 있었던 거 같음
아니..진짜 왜케 꼬여서 생각하지? 항상 생각햇던 건데 내가 애초에 코드를 어렵게 짜려고 하는거같음
항상 코드를 보면 헐..하고 이해가는데 애초에 내가 어렵게 생각해서 못짬
뭐노 진짜 하 개열받네 개멍청
'SCHOOL > 객체지향 프로그래밍' 카테고리의 다른 글
자바의 메모리 영역 (0) | 2023.11.07 |
---|---|
자바의 배열 (0) | 2023.11.07 |
Main 함수와 Scanner (0) | 2023.11.07 |
자바 접근 제어자 (0) | 2022.11.16 |
객프 4주차 과제 (2) | 2022.11.13 |