# **자바 2D 그래픽스 쉽게 배우기** ## **1. 자바 2D 그래픽스란?** 자바에서 2D 그래픽을 그리려면 `java.awt`와 `javax.swing` 패키지를 사용한다. 특히 **`Graphics`, `Graphics2D` 클래스**를 활용하면 **선을 긋고, 도형을 그리고, 색상을 채우고, 이미지를 출력할 수 있다.** --- ## **2. 주요 클래스 및 메서드 정리** ### **(1) `Graphics` 클래스 (기본 그래픽 처리)** | 메서드 | 설명 | |--------|------------------------------| | `drawLine(x1, y1, x2, y2)` | (x1, y1) → (x2, y2) 직선 그리기 | | `drawRect(x, y, width, height)` | 사각형 그리기 (테두리만) | | `fillRect(x, y, width, height)` | 사각형 그리기 (채우기) | | `drawOval(x, y, width, height)` | 타원 그리기 (테두리만) | | `fillOval(x, y, width, height)` | 타원 그리기 (채우기) | | `drawPolygon(xPoints, yPoints, nPoints)` | 다각형 그리기 | | `fillPolygon(xPoints, yPoints, nPoints)` | 다각형 채우기 | | `setColor(Color c)` | 그리기 색상 변경 | | `setFont(Font f)` | 글꼴 변경 | | `drawString(String str, x, y)` | 문자열 그리기 | **예제 코드 (사각형과 문자열 그리기)** ```java import javax.swing.*; import java.awt.*; public class MyPanel extends JPanel { @Override protected void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(Color.BLUE); g.fillRect(50, 50, 100, 100); // 파란색 사각형 g.setColor(Color.RED); g.drawString("Hello, Graphics!", 60, 180); // 문자열 출력 } public static void main(String[] args) { JFrame frame = new JFrame(); frame.setSize(300, 300); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(new MyPanel()); frame.setVisible(true); } } ``` ✅ **`paintComponent(Graphics g)`를 오버라이드하여 원하는 그래픽을 그릴 수 있다!** --- ### **(2) `Graphics2D` 클래스 (고급 그래픽 처리)** `Graphics2D`는 `Graphics`의 확장판으로, **더 정밀한 그래픽을 그릴 수 있다.** `Graphics2D` 객체는 `Graphics`에서 다운캐스팅하여 얻을 수 있다. ```java Graphics2D g2 = (Graphics2D) g; ``` | 메서드 | 설명 | |--------|------------------------------| | `setStroke(Stroke s)` | 선 두께 설정 | | `setRenderingHint(RenderingHints.Key, Object)` | 안티앨리어싱 적용 (부드러운 그래픽) | | `draw(Shape s)` | `Shape` 객체 그리기 | | `fill(Shape s)` | `Shape` 객체 채우기 | | `rotate(theta, x, y)` | (x, y)를 중심으로 회전 | | `translate(x, y)` | (x, y)만큼 이동 | | `scale(sx, sy)` | x, y 방향으로 확대/축소 | **예제 코드 (둥근 사각형과 선 두께 조절)** ```java import javax.swing.*; import java.awt.*; public class MyPanel extends JPanel { @Override protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D) g; g2.setStroke(new BasicStroke(5)); // 선 두께 설정 g2.setColor(Color.MAGENTA); g2.drawRoundRect(50, 50, 100, 100, 20, 20); // 둥근 사각형 g2.setColor(Color.GREEN); g2.drawLine(50, 200, 200, 200); // 굵은 선 } public static void main(String[] args) { JFrame frame = new JFrame(); frame.setSize(300, 300); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(new MyPanel()); frame.setVisible(true); } } ``` ✅ **`Graphics2D`를 사용하면 더 정밀한 그래픽 조작이 가능하다!** --- ### **(3) `Color`와 `Font` 클래스 (색상과 글꼴 설정)** | 메서드 | 설명 | |--------|------------------------------| | `new Color(r, g, b)` | RGB 값으로 색상 생성 | | `setColor(Color c)` | 현재 색상 변경 | | `setFont(Font f)` | 글꼴 설정 | | `new Font("FontName", style, size)` | 글꼴 생성 | **예제 코드 (다양한 색상과 글꼴 적용)** ```java g.setColor(new Color(255, 100, 100)); // 연한 빨간색 g.fillOval(50, 50, 100, 100); g.setColor(Color.BLUE); g.setFont(new Font("Arial", Font.BOLD, 20)); g.drawString("Hello!", 70, 180); ``` ✅ **`Color`와 `Font`를 조합하면 더 다양한 표현이 가능하다!** --- ### **(4) `Image` 클래스 (이미지 출력)** | 메서드 | 설명 | |--------|------------------------------| | `Image img = Toolkit.getDefaultToolkit().getImage("image.png")` | 이미지 로드 | | `drawImage(img, x, y, this)` | 이미지 그리기 | **예제 코드 (이미지 출력)** ```java Image img = Toolkit.getDefaultToolkit().getImage("image.png"); g.drawImage(img, 50, 50, this); ``` --- ## **3. 정리** ✅ **기본 도형을 그리려면 `Graphics` 클래스 사용! (`drawLine()`, `drawRect()` 등)** ✅ **더 정밀한 그래픽을 원하면 `Graphics2D` 사용! (`setStroke()`, `rotate()` 등)** ✅ **색상은 `Color`, 글꼴은 `Font` 클래스로 설정 가능!** ✅ **이미지 출력도 가능! (`drawImage()`)** 자바의 2D 그래픽을 활용하면 **간단한 그림부터 UI 요소, 차트, 애니메이션까지 다양한 그래픽을 만들 수 있다!**