Ball World

Moom2n·2024년 3월 5일
0

Java

목록 보기
16/26

- Ball 이란?

ball world를 구성하는 기본 요소

  • 2차원 평면에서 ball을 설명할 수 있는 최소 정보
  • 위치
  • 크기
  • 생성 후 정보 변경 불가

변수

  • int x
  • int y
  • int radius

메소드

  • int getX()
  • int getY()
  • int getRadius()
  • String toString()
    출력은 형식 : (x,y,radius)

Reference. Accessor와 Mutator

  • Accessor
    변수값 반환
    private 변수에 대한 접근 지원
    외부에서 직접적인 접근이 필요한 경우에만 정의
    -- 형식
    get + field name
    getRadius, getColor, …
    is + field name
    isEnabled, isInterrupted, …
    -- 다른 용어
    Getter
  • Mutator
    변수값 변경
    private 변수에 대한 변경 지원
    외부에서 직접적인 변경이 필요한 경우에만 지원
    -- 형식
    set + field name
    setRadius, setColor, …
    -- 다른 용어
    Setter

- Ball 클래스 작성

package com.nhnacademy;

public class Ball {
    private int x;
    private int y;
    private int radius;

    public Ball(int x, int y, int radius) {
        if(radius <= 0) {
            throw new IllegalArgumentException("반지름은 0보다 커야합니다.");
        }

        if( (x + (long)radius > Integer.MAX_VALUE)
            || (x - (long)radius > Integer.MAX_VALUE)
            || (y + (long)radius > Integer.MAX_VALUE)
            || (y - (long)radius > Integer.MAX_VALUE)) {
                throw new IllegalArgumentException("공이 정수공간을 벗어납니다.");
            }
        this.x = x;
        this.y = y;
        this.radius = radius;
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public int getRadius() {
        return radius;
    }

    @Override
    public String toString() {
        return "(" + getX() + "," + getY() + "," + getRadius() + ")";
    }
}

PaintableBall


World

- 정의

  • world내에서 동작할 ball 관리
  • ball 추가
  • ball 삭제
  • 삭제할 ball을 주거나 순번으로 지정
  • ball 가져오기
  • 순번 지정
  • ball 갯수 얻기
  • graphics context가 주어지면, 이를 이용해 그리기
  • ball이 존재할 공간이면서 화면에 출력될 영역
  • Swing component JPanel 확장
  • 전체 프로그램의 틀이 될 JFrame 생성 후 world 추가하여 실행

Movable Ball World

- MovableBall


MovableWorld class

-

0개의 댓글

관련 채용 정보