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

Slides:



Advertisements
Similar presentations
Custom Painting Gestione della Grafica customizzata Vedi anche:
Advertisements

Chapter 15 Graphics. To paint, you need to specify where to paint. Each component has its own coordinate system with the origin (0, 0) at the upper-left.
Copyright 2006 by Pearson Education 1 Building Java Programs Supplement 3G: Graphics.
1 Graphical User Interfaces AWT and Swing packages Frames and Panels Components Nested Panels Images Reading for this Lecture: L&L, 3.9 – 3.11.
2D Graphics Drawing Things. Graphics In your GUI, you might want to draw graphics E.g. draw lines, circles, shapes, draw strings etc The Graphics class.
Fun with Swing Chapter 9. Overview Swing vs. AWT Creating windows and panels. Displaying formatted text in panels. Drawing graphics (lines, circles, etc.)
Graphical User Interfaces
Computer Science 209 Images and GUIs. Working with Java Colors The class java.awt.Color includes constants, such as Color.red, for some commonly used.
Web Design & Development Lecture 19. Java Graphics 2.
1 More on Applets Overview l Changing Colors l Changing Fonts & Styles l Applet Life-Cycle l Input using Dialog Window l Input using HTML parameters l.
Frame Windows A frame object is used to create a graphical frame window. This frame is used to show information in a graphical application. The JFrame.
CSE 1341 Honors Professor Mark Fontenot Southern Methodist University Note Set 21.
Graphics and event-driven programs Learning objectives By the end of this lecture you should be able to: identify and use some of the common components.
Chapter 2: Using Objects Part 2. Assume you wish to test the behaviour of some method. This is accomplished by providing a tester class: Supply a main.
Examples. // A simple Frame with Rectangle Inside import java.awt.*; import javax.swing.*; import java.awt.geom.*; // For Shapes class rectComponent extends.
Drawing in a frame – Java GUI
Laboratory Study II October, Java Programming Assignment  Write a program to calculate and output the distance traveled by a car on a tank of.
Graphics You draw on a Graphics object The Graphics object cannot directly be created by your code, instead one is generated when the method paintComponent.
Graphics Chapter 16.  If you want to draw shapes such as a bar chart, a clock, or a stop sign, how do you do it?
Graphics Programming. Introduction GOAL: Build the Indexer Client Event-driven vs. Sequential programs Terminology – Top-level windows are called “frame.
LAB SESSION 7 Graphical user interface Applet fundamentals Methods in applets Execution of an applet Graphics class.
Graphics CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
제 10 주 그래픽프로그래밍 2 제 10 주 강의 목표 Graphics 를 사용하여 임의의 Shape 을 그리는 법을 익힘 마우스이벤트 이용법을 익힘 타이머 이벤트 처리 및 애니메이션 효과를 구현하는 법을 익힘 객체지향프로그래밍강원대학교 1.
Problem Solving 6 GUIs and Event Handling ICS-201 Introduction to Computing II Semester 071.
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
Applets Graphical Java programs Run inside web browser Platform-neutral Easy deployment--loads when needed Secure.
Chapter 4 (Horstmann’s Book) Interface Types and Polymorphism: Graphics, Timer, Animation Hwajung Lee.
Graphical User Interfaces, 2D Graphics & Game Programming.
Chapter 5 Programming Graphics. Chapter Goals To be able to write simple applications To display graphical shapes such as lines and ellipses To use colors.
Multithreading : animation. slide 5.2 Animation Animation shows different objects moving or changing as time progresses. Thread programming is useful.
Chapter 5 Programming Graphics. Chapter Goals To be able to write applications with simple graphical user interfaces To display graphical shapes such.
Graphical User Interface Bonus slides Interaction Between Components & Drawing.
Bar Graph Design. Left-side/Right-side mechanical processing creative, abstract reasoning.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Intro to Graphics.
More Java Drawing in 2D Animations with Timer. Drawing Review A simple two-dimensional coordinate system exists for each graphics context or drawing surface.
CPSC 2100 University of Tennessee at Chattanooga – Fall 2013 Object-Oriented Design & Patterns 2 nd edition Cay S. Horstmann Chapter 4: Interface Types.
Chapter 5 Ch 1 – Introduction to Computers and Java Defining Classes and Methods 1.
CMSC 341 Building Java GUIs. 09/26/2007 CMSC 341 GUI 2 Why Java GUI Development? Course is about Data Structures, not GUIs. We are giving you the opportunity.
Introduction to Java Classes, events, GUI’s. Understand: How to use TextPad How to define a class or object How to create a GUI interface How event-driven.
Chapter 3: Implementing Classes Part 1. To become familiar with the process of implementing classes To be able to implement simple methods To understand.
Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.
Java Concepts Chapter 2 – Graphical Applications Mr. Smith AP Computer Science A.
1 Principles of Computer Science I Prof. Nadeem Abdul Hamid CSC 120 – Fall 2005 Lecture Unit 5 - Graphics.
CSE 219 Computer Science III Images. HW1 Has been posted on Blackboard Making a Game of Life with limited options.
tiled Map Case Study: Rendering with JPanel © Allan C. Milne v
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Chapter Three: Implementing Classes.
Chapter 3 – Implementing Classes Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Chapter 4: Applets and Graphics 1 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 4 Applets and Graphics.
Chapter 4 Fundamental Data Types. Chapter Goals To be able to write simple applets To display graphical shapes such as lines and ellipses To use colors.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. In order to display a drawing in a frame, define a class that extends.
ICOM 4015: Advanced Programming Lecture 3 Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Reading: Chapter Three:
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 21.1 Test-Driving the Painter Application.
(C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.
Introduction to Java Chapter 8 - Introduction to Java Graphics1 Chapter 8 Introduction to Java Graphics.
CIS Intro to JAVA Lecture Notes Set 8 9-June-05.
Fall 2006Adapted from Java Concepts Companion Slides1 Programming Graphics Advanced Programming ICOM 4015 Lecture 14 Reading: Java Concepts Chapter 5.
Chapter 5 Programming Graphics. Chapter Goals To be able to write simple applications To display graphical shapes such as lines and ellipses To use colors.
Basics of GUI Programming Chapter 11 and Chapter 22.
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
Object Oriented Programming Lecture 3: Things OO.
Chapter 3: Implementing Classes Part 1. To become familiar with the process of implementing classes To be able to implement simple methods To understand.
Basic Graphics 03/03/16 & 03/07/16 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
C Sc 335 Object-Oriented Programming and Design Rick Mercer
Break Time! Graphical User Interface (GUI) NO EXAM….ONLY PROJECT!
Chapter 5 Programming Graphics
Chapter Three: Implementing Classes
Chapter Three - Implementing Classes
4.14 GUI and Graphics Case Study: Creating Simple Drawings (Cont.)
Fundamental Data Types
Presentation transcript:

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

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

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

객체지향프로그래밍강원대학교 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

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

객체지향프로그래밍강원대학교 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

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

객체지향프로그래밍강원대학교 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

객체지향프로그래밍강원대학교 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

객체지향프로그래밍강원대학교 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

객체지향프로그래밍강원대학교 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

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

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

객체지향프로그래밍강원대학교 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

객체지향프로그래밍강원대학교 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

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

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

객체지향프로그래밍강원대학교 칼라 사각형 그리기 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

객체지향프로그래밍강원대학교 임의 칼라 사각형 그리기 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 는 Color(int r, int g, int b) - r, g, b 는

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

객체지향프로그래밍강원대학교 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

객체지향프로그래밍강원대학교 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

객체지향프로그래밍강원대학교 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

객체지향프로그래밍강원대학교 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

객체지향프로그래밍강원대학교 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 x the x coordinate of the top left y the y coordinate of the top left corner */ public Car(int x, int y) { xLeft = x; yTop = y; } 26

객체지향프로그래밍강원대학교 /** Draws the 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

객체지향프로그래밍강원대학교 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

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