객체지향프로그래밍 제 4 주 – 그래픽 프로그래밍 기초
DESCRIPTION
객체지향프로그래밍 제 4 주 – 그래픽 프로그래밍 기초. 제 5 장 그래픽 프로그래밍. 제 4 주 강의 목표 간단한 그래픽 프로그램을 작성하는 법을 배운다 . 도형을 그리고 색을 선택하는 법을 배운다. Frame. import javax.swing.JFrame; public class EmptyFrameViewer { public static void main(String[] args) { JFrame frame = new JFrame(); - PowerPoint PPT PresentationTRANSCRIPT
강원대학교 1객체지향프로그래밍
객체지향프로그래밍
제 4 주 – 그래픽 프로그래밍 기초
강원대학교 2객체지향프로그래밍
제 5 장
그래픽 프로그래밍
제 4 주 강의 목표• 간단한 그래픽 프로그램을 작성하는 법을 배운다 .• 도형을 그리고 색을 선택하는 법을 배운다 .
강원대학교 3객체지향프로그래밍
Frame
강원대학교 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); }}
강원대학교 5객체지향프로그래밍
도형 그리기
• 프레임 안에 도형을 그리기 위해서는 그림 그리는 기능을 가진 부품(JComponent) 을 프레임에 넣어주어야 함
• JComponent 객체는 paintComponent 메소드를 가짐• paintComponent 메소드는 필요할 때마다 시스템에 의해
자동으로 호출됨• 필요한 때
– 프로그램이 처음 실행될 때– 프레임을 아이콘으로 최소화했다가 다시 창으로 복원시킬 때– 프레임이 다른 윈도우에 의해 가려졌다가 나타날 때– 마우스 드래그를 이용해 프레임을 이동시킬 때 등
강원대학교 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); }}
강원대학교 7객체지향프로그래밍
도형 그리기• JComponent 객체가 가지고 있는 paintComponent 메소드는
화면에 아무것도 그리지 않음• 화면에 도형을 그리려면 도형 그리는 기능을 갖도록 Jcomponent 를
수정하여 프레임에 넣어 줘야 함
• Jcomponent 를 수정하여 새로운 부품을 만드는 방법
public class RectangleComponent extends JComponent{ public void paintComponent(Graphics g) { // Recover Graphics2D Graphics2D g2 = (Graphics2D) g; . . . }}
강원대학교 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
사람
학생
강원대학교 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
강원대학교 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
강원대학교 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
강원대학교 12객체지향프로그래밍
컴파일할 때의 파일간 종속성• ( 같은 폴더에 ) RectanlgeComponent.java 파일 없이
RectanlgeViewer.java 를 컴파일하려고 하면 안됨• RectangleViewer 가 RectangleComponet 를 사용하기
때문
• 반면 RectanlgeComponent.java 는 RectanlgeViewer.-java 파일 없이도 컴파일 가능
강원대학교 13객체지향프로그래밍
Graphical Shapes
• Rectangle, Ellipse2D.Double, Line2D.Double 등• API 문서에서 java.awt.Shape 을 찾아보면 Shape 에 속하는
도형들을 더 볼 수 있음
강원대학교 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
강원대학교 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);
강원대학교 16객체지향프로그래밍
Drawing Strings
g2.drawString("Message", 50, 100);
강원대학교 17객체지향프로그래밍
Colors
• Standard colors: Color 클래스에 static 필드로 정의되어 있음
• Color.BLUE, Color.RED, Color.PINK etc.
• static 필드 : 개별 인스턴스에 속하지 않고 클래스에 하나만 정의되는 필드
• 필드 : 클래스 내부이면서 메소드 외부에 선언된 변수 , 상수 , 객체
강원대학교 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));
}
강원대학교 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.0Color(int r, int g, int b) - r, g, b 는 0-255
강원대학교 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); }}
강원대학교 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
강원대학교 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 는 paint-Component 를 실행하고 있는 Car-Component 객체에 호출됨
• 윈도우 크기를 조정하면 paintCom-ponent 가 호출되고 자동차의 위치가 재계산됨 ( 프로그램을 실행한 후 마우스를 드래그하여 윈도우 크기를 바꿔보세요 !)
CarComponent 에 그림을 그리기 위한 도구
강원대학교 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; // 인스턴스 필드
}
미완성 !
강원대학교 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; }
강원대학교 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
강원대학교 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;}
강원대학교 29객체지향프로그래밍
Reading Text Input
String input = JOptionPane.showInputDialog("Enter x");double x = Double.parseDouble(input);