본문 바로가기

자바공부

자바 그래픽 도형, 명품자바프로그래밍12장

728x90
반응형

* Graphics를 이용하여 선, 타원, 사격형, 둥근모서리사각형, 원호, 폐다각형 등을 그릴 수 있다.

void drawLine(int x1, int y1, int x2, int y2)
// (x1, y1)에서 (x2, y2)까지 선을 그린다.
void drawOval(int x, int y, int w, int h)
// (x, y)에서 w x h 크기의 사각형에 내접하는 타원을 그린다.
void drawRect(int x, int y, int w, int h)
// (x, y)에서 w x h 크기의 사각형을 그린다.
void drawRoundRect(int x, int y, int w, int h, int arcWidth, int arcHeight)
// arcWidth: 모서리 원의 수평 반지름
// arcHeight: 모서리 원의 수직 반지름
// (x, y)에서 w x h 크기의 사각형을 그리고, 4개의 모서리는 arcWidth와 arcHeight를 이용하여 원호로 그린다.

 

* Graphics의 drawLine()을 이용하여 선 그리기

package Ch12Ex;

import javax.swing.*;
import java.awt.*;

public class GraphicsDrawLineEx extends JFrame{
	Container contentPane;
	GraphicsDrawLineEx(){
		setTitle("drawLine 사용 예제");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		contentPane = getContentPane();
		
		MyPanel panel = new MyPanel();
		contentPane.add(panel, BorderLayout.CENTER);
		setSize(200, 150);
		setVisible(true);
	}
	
	class MyPanel extends JPanel{
		public void paintComponent(Graphics g) {
			super.paintComponent(g);
			g.setColor(Color.red);
			g.drawLine(20,20,100,100);
			
		}
	}
	public static void main(String[] args) {
		new GraphicsDrawLineEx();
	}

}

	...
    class MyPanel extends JPanel{
		public void paintComponent(Graphics g) {
			super.paintComponent(g);
			g.setColor(Color.red);
			g.drawOval(20,20,80,80);
			
		}
	}
    ...

원 그리기

	...
    class MyPanel extends JPanel{
		public void paintComponent(Graphics g) {
			super.paintComponent(g);
			g.setColor(Color.red);
			g.drawRect(20,20,80,80);
			
		}
	}
    ...

사각형 그리기

	...
    class MyPanel extends JPanel{
		public void paintComponent(Graphics g) {
			super.paintComponent(g);
			g.setColor(Color.red);
			g.drawRoundRect(20,20,120,80, 40, 60);
			
		}
	}
    ...

둥근 모서리 사각형

 

* Graphics 클래스를 이용하여 원호와 폐다각형을 그리는 메소드

void drawArc(int x, int y, int w, int h, int startAngle, int arcAngle)
// startAngle: 원호의 시작 각도
// arcAngle: 원호 각도
// (x, y)에서 w x h 크기의 사각형에 내접하는 원호를 그린다. 3시 방향이 0도의 기점이다.
// startAngle 지점에서 arcAngle 각도만큼 원호를 그린다.
// arcAngle이 양수이면 반시계 반향, 음수이면 시계 방향으로 그린다.

void drawPolygon(int []x, int []y, int n)
// x, y 배열에 저장된 점들 중 n개를 연결하는 폐다각형을 그린다.
// (x[0], y[0]), (x[1], y[1]), ... ,(x[n-1], y[n-1]), (x[0], y[0])의 점들을 모두 순서대로 연결한다.

 

	...
    class MyPanel extends JPanel{
		public void paintComponent(Graphics g) {
			super.paintComponent(g);
			g.setColor(Color.red);
			g.drawArc(20,100,80,80, 90, 270);
			
		}
	}
    ...

원호 그리기

	...
    class MyPanel extends JPanel{
		public void paintComponent(Graphics g) {
			super.paintComponent(g);
			g.setColor(Color.red);
			
            int []x={80,40,80,120};
            int []y={40,120,200,120};
            g.drawPolygon(x, y, 4);
			
		}
	}
    ...

폐다각형 Polygon 그리기

* Graphics의 모든 도형 그리기 메소드는 1픽셀 두께로만 그린다. 도형 외곽선의 두께를 선택할 수 없다.

도형 외곽선의 두께를 다양하게 그리고자 한다면 Graphics 2D를 사용하든지 크기를 달리하여 여러 번 도형을 그리는 방식을 사용하여야 한다.

 

* 도형 칠하기란 도형의 외각을 그리는 동시에 내부를 동일한 색으로 칠하는 기능이다.

도형의 외고가선과 내부를 분리하여 칠하는 기능은 없다.

칠하는 색과 외곽선의 색을 달리하고자 하는 경우, 도형 내부를 칠하고 다시 외곽선을 다른 색으로 그려야 한다.

그리기 메소드 명에서 draw 대신에 fill로 대치하면 된다.

drawRect() -> fillRect()
drawArc() -> fillArc()
// 그리기와 칠하기의 메소드 인자는 동일한다.
반응형