Presentation is loading. Please wait.

Presentation is loading. Please wait.

객체지향프로그래밍강원대학교 객체지향프로그래밍 제 4 주 – 그래픽 프로그래밍 기초 1. 객체지향프로그래밍강원대학교 제 5 장 그래픽 프로그래밍 제 4 주 강의 목표 간단한 그래픽 프로그램을 작성하는 법을 배운다. 도형을 그리고 색을 선택하는 법을 배운다. 2.

Similar presentations


Presentation on theme: "객체지향프로그래밍강원대학교 객체지향프로그래밍 제 4 주 – 그래픽 프로그래밍 기초 1. 객체지향프로그래밍강원대학교 제 5 장 그래픽 프로그래밍 제 4 주 강의 목표 간단한 그래픽 프로그램을 작성하는 법을 배운다. 도형을 그리고 색을 선택하는 법을 배운다. 2."— Presentation transcript:

1 객체지향프로그래밍강원대학교 객체지향프로그래밍 제 4 주 – 그래픽 프로그래밍 기초 1

2 객체지향프로그래밍강원대학교 제 5 장 그래픽 프로그래밍 제 4 주 강의 목표 간단한 그래픽 프로그램을 작성하는 법을 배운다. 도형을 그리고 색을 선택하는 법을 배운다. 2

3 객체지향프로그래밍강원대학교 Frame 3

4 객체지향프로그래밍강원대학교 import javax.swing.JFrame; public class EmptyFrameViewer { public static void main(String[] args) { JFrame frame = new JFrame(); final int FRAME_WIDTH = 300; final int FRAME_HEIGHT = 400; frame.setSize(FRAME_WIDTH, FRAME_HEIGHT); frame.setTitle("An Empty Frame"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } 4

5 객체지향프로그래밍강원대학교 도형 그리기 프레임 안에 도형을 그리기 위해서는 그림 그리는 기능을 가진 부품 (JComponent) 을 프레임에 넣어주어야 함 JComponent 객체는 paintComponent 메소드를 가짐 paintComponent 메소드는 필요할 때마다 시스템에 의해 자동으로 호 출됨 필요한 때 – 프로그램이 처음 실행될 때 – 프레임을 아이콘으로 최소화했다가 다시 창으로 복원시킬 때 – 프레임이 다른 윈도우에 의해 가려졌다가 나타날 때 – 마우스 드래그를 이용해 프레임을 이동시킬 때 등 5

6 객체지향프로그래밍강원대학교 import javax.swing.*; public class EmptyFrameViewer { public static void main(String[] args) { JFrame frame = new JFrame(); final int FRAME_WIDTH = 300; final int FRAME_HEIGHT = 400; frame.setSize(FRAME_WIDTH, FRAME_HEIGHT); frame.setTitle("An Empty Frame"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JComponent component = new JComponent(); // 부품을 구성 frame.add(component); // 부품을 프레임에 넣음 frame.setVisible(true); } 6

7 객체지향프로그래밍강원대학교 도형 그리기 JComponent 객체가 가지고 있는 paintComponent 메소드는 화면에 아무것 도 그리지 않음 화면에 도형을 그리려면 도형 그리는 기능을 갖도록 Jcomponent 를 수정하여 프레임에 넣어 줘야 함 Jcomponent 를 수정하여 새로운 부품을 만드는 방법 public class RectangleComponent extends JComponent { public void paintComponent(Graphics g) { // Recover Graphics2D Graphics2D g2 = (Graphics2D) g;... } } 7

8 객체지향프로그래밍강원대학교 RectangleComponent 는 JComponent 를 확장 (extend) 했다고 함 RectangleComponent 는 JComponent 의 서브클래스임 서브클래스는 수퍼클래스의 모든 속성을 상속하며 추가적인 속성을 가짐 학생은 사람의 서브클래스임 학생은 사람의 모든 속성을 다 가지며 추가로 학번, 학과, 학년 등의 속성을 가짐 학생은 사람이다. (O) 사람은 학생이다. (X) RectangleComponent 는 JComponent 이다 ! public class RectangleComponent extends JComponent { public void paintComponent(Graphics g) { // Recover Graphics2D Graphics2D g2 = (Graphics2D) g;... } } 사람 학생 JComponent RectangleComponent 사람 학생 8

9 객체지향프로그래밍강원대학교 public class RectangleComponent extends JComponent { public void paintComponent(Graphics g) { // Recover Graphics2D Graphics2D g2 = (Graphics2D) g; Rectangle box = new Rectangle(5, 10, 20, 30); g2.draw(box); } } RectanlgeComponent 클래스는 수퍼클래스인 JComponent 의 paintComponent 메소드를 상속하여 이를 재정의 (override) 함 JComponent 의 paintComponent 메소드는 아무것도 그리지 않음 재정의된 paintComponent 메소드는 사각형 인스턴스를 하나 구성하 고 이것을 화면에 그림 RectangleComponent.java 9

10 객체지향프로그래밍강원대학교 import javax.swing.*; public class RectangleViewer { public static void main(String[] args) { JFrame frame = new JFrame(); final int FRAME_WIDTH = 300; final int FRAME_HEIGHT = 400; frame.setSize(FRAME_WIDTH, FRAME_HEIGHT); frame.setTitle("A Rectangle Frame"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); RectangleComponent component = new RectangleComponent(); frame.add(component); // 부품을 프레임에 넣음 frame.setVisible(true); } RectangleViewer.java 10

11 객체지향프로그래밍강원대학교 import javax.swing.JComponent; import java.awt.Rectangle; import java.awt.Graphics; import java.awt.Graphics2D; public class RectangleComponent extends JComponent { public void paintComponent(Graphics g) { // Recover Graphics2D Graphics2D g2 = (Graphics2D) g; Rectangle box = new Rectangle(5, 10, 20, 30); g2.draw(box); box.translate(15, 25); g2.draw(box); } } RectangleComponent.java 11

12 객체지향프로그래밍강원대학교 컴파일할 때의 파일간 종속성 ( 같은 폴더에 ) RectanlgeComponent.java 파일 없이 RectanlgeViewer.java 를 컴파일하려고 하면 안됨 RectangleViewer 가 RectangleComponet 를 사용하기 때문 반면 RectanlgeComponent.java 는 RectanlgeViewer.java 파일 없이 도 컴파일 가능 12

13 객체지향프로그래밍강원대학교 Graphical Shapes Rectangle, Ellipse2D.Double, Line2D.Double 등 API 문서에서 java.awt.Shape 을 찾아보면 Shape 에 속하는 도형들을 더 볼 수 있음 13

14 객체지향프로그래밍강원대학교 Graphical Shapes Ellipse2D.Double 은 Ellipse2D 의 내부클래스 (innerclass) 임 Ellipse2D.Double 를 사용하기 위해서는 import java.awt.geom.Ellipse2D Ellipse2D.Double ellipse = new Ellipse2D.Double(x, y, width, height); g2.draw(ellipse); Ellipse 14

15 객체지향프로그래밍강원대학교 Drawing Lines To construct a line: or, Line2D.Double segment = new Line2D.Double(x1, y1, x2, y2); Point2D.Double from = new Point2D.Double(x1, y1); Point2D.Double to = new Point2D.Double(x2, y2); Line2D.Double segment = new Line2D.Double(from, to); To draw the line: g.draw(segment); 15

16 객체지향프로그래밍강원대학교 Drawing Strings g2.drawString("Message", 50, 100); 16

17 객체지향프로그래밍강원대학교 Colors Standard colors: Color 클래스에 static 필드로 정의되어 있 음 Color.BLUE, Color.RED, Color.PINK etc. static 필드 : 개별 인스턴스에 속하지 않고 클래스에 하나만 정의되는 필드 필드 : 클래스 내부이면서 메소드 외부에 선언된 변수, 상수, 객체 17

18 객체지향프로그래밍강원대학교 칼라 사각형 그리기 public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; g2.setColor(Color.RED); g2.fill(new Rectangle(0, 0, 200, 200)); g2.setColor(Color.YELLOW); g2.fill(new Rectangle(50, 50, 100, 100)); } 18

19 객체지향프로그래밍강원대학교 임의 칼라 사각형 그리기 public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; g2.setColor(new Color(0.5f, 0.6f, 0.3f)); g2.fill(new Rectangle(0, 0, 200, 200)); g2.setColor(new Color(25, 200, 224)); g2.fill(new Rectangle(50, 50, 100, 100)); } Color(float r, float g, float b) – r, g, b 는 0.0-1.0 Color(int r, int g, int b) - r, g, b 는 0-255 19

20 객체지향프로그래밍강원대학교 복잡한 모양 그리기 20

21 객체지향프로그래밍강원대학교 import javax.swing.JFrame; public class CarViewer { public static void main(String[] args) { JFrame frame = new JFrame(); final int FRAME_WIDTH = 300; final int FRAME_HEIGHT = 400; frame.setSize(FRAME_WIDTH, FRAME_HEIGHT); frame.setTitle("Two cars"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); CarComponent component = new CarComponent(); frame.add(component); frame.setVisible(true); } 21

22 객체지향프로그래밍강원대학교 import java.awt.Graphics; import java.awt.Graphics2D; import javax.swing.JComponent; public class CarComponent extends JComponent { public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; Car car1 = new Car(0, 0); int x = getWidth() - Car.WIDTH; int y = getHeight() - Car.HEIGHT; Car car2 = new Car(x, y); car1.render(g2); car2.render(g2); } 위치 WIDTH HEIGHT 22

23 객체지향프로그래밍강원대학교 import java.awt.Graphics; import java.awt.Graphics2D; import javax.swing.JComponent; public class CarComponent extends JComponent { public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; Car car1 = new Car(0, 0); int x = getWidth() - Car.WIDTH; int y = getHeight() - Car.HEIGHT; Car car2 = new Car(x, y); car1.render(g2); car2.render(g2); } getWidth, getHeight 는 paintComponent 를 실행하고 있는 CarComponent 객체에 호출됨 윈도우 크기를 조정하면 paintComponent 가 호출되고 자동차의 위치가 재계산됨 ( 프로그램을 실행한 후 마우스를 드래그하여 윈도우 크기를 바 꿔보세요 !) CarComponent 에 그림을 그리기 위한 도구 23

24 객체지향프로그래밍강원대학교 복잡한 모양 그리기 24

25 객체지향프로그래밍강원대학교 public class Car { public Car(int x, int y) { xLeft = x; yTop = y; } public void render(Graphics2D g2) { g2.draw(body); g2.draw(frontTire); g2.draw(rearTire); g2.draw(frontWindshield); g2.draw(roofTop); g2.draw(rearWindshield); } public static int WIDTH = 60;// static field public static int HEIGHT = 30; // static field private int xLeft; // 인스턴스 필드 private int yTop; // 인스턴스 필드 } 미완성 ! 25

26 객체지향프로그래밍강원대학교 import java.awt.Graphics2D; import java.awt.Rectangle; import java.awt.geom.Ellipse2D; import java.awt.geom.Line2D; import java.awt.geom.Point2D; /** A car shape that can be positioned anywhere on the screen. */ public class Car { /** Constructs a car with a given top left corner @param x the x coordinate of the top left corner @param y the y coordinate of the top left corner */ public Car(int x, int y) { xLeft = x; yTop = y; } 26

27 객체지향프로그래밍강원대학교 /** Draws the car. @param g2 the graphics context */ public void render(Graphics2D g2) { Rectangle body = new Rectangle(xLeft, yTop + 10, 60, 10); Ellipse2D.Double frontTire = new Ellipse2D.Double(xLeft + 10, yTop + 20, 10, 10); Ellipse2D.Double rearTire = new Ellipse2D.Double(xLeft + 40, yTop + 20, 10, 10); // The bottom of the front windshield Point2D.Double r1 = new Point2D.Double(xLeft + 10, yTop + 10); // The front of the roof Point2D.Double r2 = new Point2D.Double(xLeft + 20, yTop); // The rear of the roof Point2D.Double r3 = new Point2D.Double(xLeft + 40, yTop); // The bottom of the rear windshield Point2D.Double r4 27

28 객체지향프로그래밍강원대학교 Line2D.Double frontWindshield = new Line2D.Double(r1, r2); Line2D.Double roofTop = new Line2D.Double(r2, r3); Line2D.Double rearWindshield = new Line2D.Double(r3, r4); g2.draw(body); g2.draw(frontTire); g2.draw(rearTire); g2.draw(frontWindshield); g2.draw(roofTop); g2.draw(rearWindshield); } public static int WIDTH = 60; public static int HEIGHT = 30; private int xLeft; private int yTop; } 28

29 객체지향프로그래밍강원대학교 Reading Text Input String input = JOptionPane.showInputDialog("Enter x"); double x = Double.parseDouble(input); 29


Download ppt "객체지향프로그래밍강원대학교 객체지향프로그래밍 제 4 주 – 그래픽 프로그래밍 기초 1. 객체지향프로그래밍강원대학교 제 5 장 그래픽 프로그래밍 제 4 주 강의 목표 간단한 그래픽 프로그램을 작성하는 법을 배운다. 도형을 그리고 색을 선택하는 법을 배운다. 2."

Similar presentations


Ads by Google