AbstractRegionPainter

AbstractRegionPainter

Nimbus에서 영역(region) 또는 컴포넌트를 렌더링하기 위한 Painter 인스턴스를 정의할 때 사용하는 편리한 기본 클래스예요.

출처: Java API Reference

본문

AbstractRegionPainter는 Nimbus Look and Feel에서 영역을 그리는 Painter의 기본 구현을 제공해요. 페인팅 중 유용한 상태를 캡슐화하는 PaintContext와 함께, 좌표 디코딩, 색상 디코딩, 그라디언트 생성 등의 편의 메서드를 제공해요.

대표적인 시그니처는 다음과 같아요.

protected AbstractRegionPainter()
public final void paint(Graphics2D g, JComponent c, int w, int h)
protected Object[] getExtendedCacheKeys(JComponent c)
protected abstract AbstractRegionPainter.PaintContext getPaintContext()
protected void configureGraphics(Graphics2D g)
protected abstract void doPaint(Graphics2D g, JComponent c, int width, int height, Object[] extendedCacheKeys)
protected final float decodeX(float x)
protected final float decodeY(float y)
protected final float decodeAnchorX(float x, float dx)
protected final float decodeAnchorY(float y, float dy)
protected final Color decodeColor(String key, float hOffset, float sOffset, float bOffset, int aOffset)
protected final Color decodeColor(Color color1, Color color2, float midPoint)
protected final LinearGradientPaint decodeGradient(float x1, float y1, float x2, float y2, float[] midpoints, Color[] colors)
protected final RadialGradientPaint decodeRadialGradient(float x, float y, float r, float[] midpoints, Color[] colors)
protected final Color getComponentColor(JComponent c, String property, Color defaultColor, float saturationOffset, float brightnessOffset, int alphaOffset)

주요 메서드의 동작을 살펴볼게요.

  • paint(Graphics2D g, JComponent c, int w, int h) — 주어진 Graphics2D 객체에 렌더링해요. 구현은 Graphics2D의 상태를 수정할 수 있으며 완료 시 상태를 복원할 필요는 없어요. 대부분의 경우 호출자가 임시 그래픽스 객체를 전달하는 것이 권장돼요. Graphics2D는 절대 null이면 안 돼요. 그래픽스 객체의 상태는 paint 메서드가 존중할 수도 있고 아닐 수도 있어요. 예를 들어 안티앨리어싱 렌더링 힌트 설정이 Painter 구현에 존중될 수도 있고 아닐 수도 있죠. widthheight는 Painter가 그려야 할 너비와 높이를 지정해요.
  • getPaintContext() — 이 페인팅 작업의 PaintContext를 가져와요. 캐시 힌트와 런타임에 점을 디코딩하는 데 필요한 데이터(스트레칭 인셋, 인코딩 점이 정의된 캔버스 크기, 스트레칭 인셋의 반전 여부)를 포함해요. 하위 클래스가 서로 다른 캔버스 크기 등을 가진 여러 상태의 페인팅을 하나의 AbstractRegionPainter 구현으로 묶을 수 있게 해요.
  • configureGraphics(Graphics2D g) — 주어진 Graphics2D를 구성해요. 렌더링 전에 그래픽스 객체에 렌더링 힌트나 합성 규칙을 적용해야 하는 경우가 많은데, 이 메서드는 중간 버퍼든 직접 디스플레이든 렌더링 전에 그래픽스 객체를 구성할 편리한 훅을 제공해요.
  • doPaint(...) — 실제 페인팅 연산을 수행해요. 하위 클래스는 이 메서드를 구현해야 해요. 전달된 그래픽스 객체는 실제 렌더링되는 표면일 수도 있고 중간 버퍼일 수도 있으며, 미리 변환(translate)되어 있어요. 컴포넌트가 0, 0에 있고 너비가 width, 높이가 height인 것처럼 렌더링하면 돼요.
  • decodeX(float x) / decodeY(float y) — 주어진 인코딩된 X/Y 값에 대한 실제 픽셀 위치를 나타내는 float 값을 디코딩해 반환해요.
  • decodeAnchorX(float x, float dx) / decodeAnchorY(float y, float dy) — 컨트롤 포인트의 인코딩된 X/Y 값과 그 포인트에서 앵커까지의 오프셋 거리가 주어졌을 때, 앵커 포인트의 실제 픽셀 위치를 나타내는 float 값을 디코딩해 반환해요.
  • decodeColor(...) — 두 색상 사이의 오프셋 또는 UI defaults의 기본 색상에서 파생된 색상을 디코딩해 반환해요.
  • decodeGradient(...)LinearGradientPaint 생성 매개변수가 주어지면 선형 그라디언트 페인트를 생성해 반환해요. 시작점과 끝점이 같은 LinearGradientPaint를 만들지 않기 위한 것이 주 목적이에요. 그런 경우 끝 y 점을 약간 늘려 겹침을 피해요.
  • decodeRadialGradient(...)RadialGradientPaint 생성 매개변수가 주어지면 방사형 그라디언트 페인트를 생성해 반환해요. 반지름이 양수가 아닌 RadialGradientPaint를 만들지 않기 위한 것이 주 목적이에요. 그런 경우 반지름을 약간 늘려 0을 피해요.
  • getComponentColor(...) — 주어진 JComponent에서 색상 속성을 가져와요. 먼저 getXXX() 메서드를 확인하고, 없으면 property 키의 클라이언트 속성을 확인해요. 그래도 Color를 반환하지 못하면 defaultColor가 반환돼요.

캐시 관련 메서드

  • getExtendedCacheKeys(JComponent c) — 페인터 구현이 이미지 캐시 조회에 포함하고 싶은 추가 속성을 가져와요. paint(g, c, w, h) 메서드가 호출될 때마다 확인돼요.

더 알아보기