10 장 AWT(2). 2/20 AWT(2)  Canvas 클래스  Graphics 클래스  Color 클래스  Font 클래스  내부클래스를 사용하는 이벤트 처리.

Slides:



Advertisements
Similar presentations
Applets and Graphics.
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.
AWT Components. 2 Using AWT Components 4 Component –Canvas –Scrollbar –Button –Checkbox –Label –List –Choice –TextComponent –TextArea –TextField 4 Component.
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?
Zhang & Liang, Computer Graphics Using Java 2D and 3D (c) 2007 Pearson Education, Inc. All rights reserved. 1 Chapter 2 2D Graphics: Basics.
CSC1401 Drawing in Java - 2. Reminder from last class How do you save your modified picture? String filename = …; Picture stevePicture = new Picture(filename);
Java Graphics Section 1 - Multi-File Graphics Programs Section 2 - The Coordinate System and Graphics Context g Section 3 - The Java Drawing and Painting.
Graphics in Java Part II. Lecture Objectives Learn how to use Graphics in Java.
1 A Simple Applet. 2 Applets and applications An application is an “ordinary” program Examples: Notepad, MS Word, Firefox, Halo, etc. An applet is a Java.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 28 - Java Graphics and Java2D Outline 28.1Introduction 28.2Graphics Contexts and Graphics Objects.
Enahnced Digital Clock Applet Setting applet parameters in the web page. The applet tag in HTML:
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Java Graphics Applets.
A Simple Applet. Applets and applications An applet is a Java program that runs on a web page –Applets can be run from: Internet Explorer Netscape Navigator.
A Simple Applet.
Drawing pictures with Java. JFrame: the basic Java window The swing package contains classes, objects and methods that can be used to create a consistent.
1 Features of Java CS 3331 Fall Outline  Abstract class  Interface  Application --- animation applets.
Graphics Applets By Mr. Dave Clausen. 2 Applets  A Java application is a stand-alone program with a main method (like the ones we've seen so far)  A.
Java Programs u 1 project file –with an extension of.mcp –contains information that CodeWarrior needs to run the program u >= 1 source files –have an extension.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Java Applets What is an Applet? How do you create.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Standard Graphics in Java,
Java On The Web Session 15. Memory Upload JAVA Applets Colors Fonts Drawing Methods Posting your Applet.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 2 Elementary Programming.
Abstract Window Toolkit (AWT) The Abstract Window Toolkit (AWT) supports Graphical User Interface (GUI) programming. AWT features include:  A rich set.
Programming and Problem Solving With Java Copyright 1999, James M. Slack Graphics in Java Applications The Graphics Class The Canvas Class The MouseListener.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved COS240 Object-Oriented Languages.
2D Graphics Basics Chapter 2. Bird’s Eye View  Overview of Computer Graphics  2D Graphics: Basics –Basic 2D graphics, rendering pipeline, geometry,
GUI Chapter 10 Graphics context and objects Creating a window based application JFrame, JTextField, JButton Containers and Controls Graphics commands Layout.
Applets & Graphics. Applets programs that run inside a browser Java platform-independence makes applets possible security restrictions: –cannot read or.
Chapter 7 Graphics. © Daly and Wrigley Objectives Use Graphic Components: ▫ Strings ▫ Lines ▫ Rectangles ▫ Ovals ▫ Arcs Change the color and font of elements.
Canvas and Graphics CS 21a. 9/26/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L17: Canvas.
Applets Applet is java program that can be embedded into HTML pages. Java applets runs on the java enabled web browsers such as mozilla and internet explorer.
1 A Simple Applet. 2 Applets and applications An application is an “ordinary” program Examples: Notepad, MS Word, Firefox, Halo, etc. An applet is a Java.
CSTP FS99CS423 (cotter)1 Java Graphics java.awt.*;
 2000 Prentice Hall, Inc. All rights reserved. Chapter 11 - Graphics and Java2D Outline 11.1Introduction 11.2Graphics Contexts and Graphics Objects 11.3Color.
Universidad Nacional de Colombia Facultad de Ingeniería Departamento de Sistemas ertificación en AVA.
CPS Today’s topics Java Applications Graphics Upcoming Review for Midterm Exam Reading Great Ideas, Chapters 5.
1 Windows program example import java.awt.*; import java.awt.event.*; public class wpexample extends Frame { public wpexample(String title) { super(title);
Painting (Chapter 12) Java Certification Study Group January 25, 1999 Mark Roth.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
עקרונות תכנות מונחה עצמים תרגול 6 - GUI. Outline  Game of Life  Painting.
Chapter 2: Graphics In Java Basics of classes –instance variables –methods –overriding methods Graphics class (drawing routines) Other classes –Color –Font.
Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 15 l Basic Figures l Colors l Fonts and Other Text Details.
1 A Simple Applet. 2 Applets and applications An applet is a Java program that runs on a web page Applets can be run within any modern browser To run.
CSI 3125, Preliminaries, page 1 Applet. CSI 3125, Preliminaries, page 2 Graphics Methods public abstract void drawString(String str, int x, int y): is.
GUI Components. The Swing package has numerous GUI components that can be added to a window. The Swing package has numerous GUI components that can be.
CSI 3125, Preliminaries, page 1 AWT. CSI 3125, Preliminaries, page 2 AWT Java AWT (Abstract Windowing Toolkit) is an API to develop GUI or window-based.
Lecture # 6 Graphical User Interface(GUI). Introduction A graphical user interface (GUI) presents a user- friendly mechanism for interacting with an application.
Institute of Ambient Intelligence 2009, Choi, Namseok, Dongseo Univ., Java 기초 ( 컴포넌트와 이벤트 처리 프로그래밍 ) Choi,
Getting Started with GUI Programming Chapter 10 CSCI 1302.
C13b, AWT cont.. Panel Container that acts like a Component Can be added into other components (like Frames, other Panels) private Panel makeScrollBars()
AWT Java AWT (Abstract Windowing Toolkit) is an API to develop GUI or window-based application in java. Java AWT components are platform-dependent i.e.
6.1 Coordinates The screen of a computer is a grid of little squares called pixels. The color of each pixel can be set individually, and drawing on the.
CPCS 391 Computer Graphics Lab One. Computer Graphics Using Java What is Computer Graphics: Computer graphics are graphics created using computers and,
Break Time! Graphical User Interface (GUI) NO EXAM….ONLY PROJECT!
CS202 Java Object Oriented Programming GUI Programming – Color and Drawing Methods Chengyu Sun California State University, Los Angeles.
Graphics Drawing Things With Java. Today's Topics Course Updates Java Graphics –Java 1.1 versus Java 1.2 (Java 2) Drawing Lines Drawing Shapes.
Java Graphics and Java2D
Graphics Applets By Mr. Dave Clausen
User Interface Programming In Java
Graphics Chapter 6 Copyright © 2000 W. W. Norton & Company.
Object Oriented Programming
Applets In Java Visit for more Learning Resources 1.
Graphics Applets By Mr. Dave Clausen
AWT Components.
Today’s topics Java Applications Upcoming Reading Graphics
Chapter 10 Graphics.
Java Applets.
עקרונות תכנות מונחה עצמים
Swing II.
APPLET.
Presentation transcript:

10 장 AWT(2)

2/20 AWT(2)  Canvas 클래스  Graphics 클래스  Color 클래스  Font 클래스  내부클래스를 사용하는 이벤트 처리

3/20 AWT(2)  Canvas 클래스 그래픽 처리를 할 수 있는 특정한 모양을 가지지 않는 컴포넌트 void paint(Graphics) 메서드를 오버라이딩하여 직접 그래픽 작업을 처리를 할 수 있고, getGraphics() 메서드로 Graphics 객체를 얻어와 그래픽 처리를 할 수도 있음

4/20 AWT(2)  Graphics 클래스 drawRect() 와 fillRect() 모두 사각형을 그리는 메서드지만 fillRect 메서드는 현재 설정되어있는 색으로 내부를 채운 형태로 그려짐. 시작점 x, y 좌표와 사각형의 폭과 높이를 인수로 받음 DrawPolygon() 과 fillPolygon() 다각형을 그려주는 메서드로 각 꼭지점의 x 좌표 배열, y 좌표 배열, 꼭지점의 숫자를 인수로 받음 drawRect(int x, int y, int width, int height) fillRect(int x, int y, int width, int height) drawPolygon(int[] xPoints, int[] yPoints, int nPoint) fillPolygon(int[] xPoints, int[] yPoints, int nPoint)

5/20 AWT(2) drawRoundRect() 와 fillRoundRect() 메서드는 모서리가 둥근 사각형을 그리는데 시작점의 좌표와 폭, 높이, 폭과 높이에 대한 둥근 정도를 인수로 받아 그림 drawRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight) fillRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight)

6/20 AWT(2) drawOval(i) 와 fillOval() 원을 그리는 메서드로 x 와 y 를 시작점으로 width 크기의 폭과 height 크기의 높이를 가지는 가상의 사각형 내부에 채워지는 원을 그림 drawOval(int x, int y, int width, int height) fillOval(int x, int y, int width, int height) drawOval() 메서드로 그려지는 원의 모양

7/20 AWT(2) drawArc() 와 fillArc() 메서드 시작점의 좌표와 폭과 높이 0 도를 기준으로 시작 각과 호의 내부각의 값을 입력받아 호를 그리는 메서드 drawArc(int x, int y, int width, int height, int startAngle, int arcAngle) fillArc(int x, int y, int width, int height, int startAngle, int arcAngle) drawArc() 메서드로 그려지는 호의 모양

8/20 AWT(2) drawLine() 두지점의 좌표를 연결하는 직선을 그리는 메서드이다. clearRect() 시작점, 폭과 높이를 입력받아 선택된 영역의 내용을 지우는 메서드이다. drawLine(int xp, int yp, int xr, int yr) clearRect(int x, int y, int width, int height)

9/20 AWT(2) import java.awt.*; class MyCanvas extends Canvas { // Canvas 클래스 상속 public void paint(Graphics g) { // Canvas 클래스의 paint(Graphics g) 메서드 오버라이딩 g.drawString("Test", 10, 20); g.drawRect(50, 10, 30, 20); g.fillRect(100, 10, 30, 20); int xPos[] = {150, 165, 180, 170, 160}; // 각 꼭지점의 X 좌표를 int 형 배열로 설정한다. int yPos[] = {20, 10, 20, 30, 30}; // 각 꼭지점의 Y 좌표를 int 형 배열로 설정한다. g.drawPolygon(xPos, yPos, 5); g.drawRoundRect(10, 50, 40, 40, 10, 10); g.drawOval(60, 50, 50, 20); g.fillArc(100, 50, 40, 40, 45, -75); g.drawLine(10, 110, 170, 130); } public class CanvasExam { public static void main(String[] args) { Frame frm = new Frame("Canva Exam"); Canvas canvas = new MyCanvas(); frm.setSize(200,180); frm.add(canvas, "Center"); frm.setVisible(true); }

10/20 AWT(2)  Color 클래스 RGB 값을 사용하여 색 객체를 생성할 수 있음 setColor(Color) 와 같은 색상 관련 메서드를 사용해 컴포넌트나 그래픽 객체의 색을 설정할 수 있음 public Color(int r, int g, int b)

11/20 AWT(2) 상수로 정의된 Color 객체 정정적 멤버 변수기기능 Color.black, Color.BLACK 검정색 Color.blue, Color.BLUE 파란색 Color.cyan Color.CYAN 하늘색 Color.darkGray, Color.DARK_GRAY 짙은 회색 Color.gray, Color.GRAY 회색 Color.green, Color.GREEN 녹색 Color.lightGray, Color.LIGHT_GRAY 옅은 회색 Color.magenta, Color.MAGENTA 진홍색 Color.orange, Color.ORANGE 주황색 Color.pink, Color.PINK 분홍색 Color.red, Color.RED 빨간색 Color.white, Color.WHITE 하얀색 Color.yellow, Color.YELLOW 노란색

12/20 AWT(2) import java.awt.*; class ColorCanvas extends Canvas { public void paint(Graphics g) { setBackground(Color.YELLOW); setForeground(Color.BLACK); g.drawString("Color 클래스의 정적멤버변수를 사용하여 ", 10, 20); g.drawString(" 색상을 설정 할 수 있다.", 10, 40); int color[] = {10, 50, 100, 150, 200, 250 }; for(int index=0; index < color.length; index++) { g.setColor(new Color(color[index], 0, color[5-index])); g.drawString("RGB 색상을 선택할 수 있다.", 10, 70+(20*index)); } } } public class ColorExam { public static void main(String args[]) { Frame frm = new Frame("Color 예제 "); ColorCanvas canvas = new ColorCanvas(); frm.setSize(300,220); frm.add(canvas, "Center"); frm.setVisible(true); }

13/20 AWT(2)  Font 클래스 Color 클래스와 마찬가지로 Font 클래스의 생성자를 호출해 폰트 객체를 만들고 setFont(Font) 메서드를 사용해 설정 public Font(String name, int style, int size) name 은 폰트의 이름, 스타일, 크기를 지정하며 플랫폼마다 지원되는 폰트가 다르기 때문에 GraphicsEnvironment 클래스의 getAvailableFontFamilyNames() 메서드를 사용해 지원되는 폰트를 먼저 확인해야 함 public Font(String name, int style, int size)

14/20 AWT(2) import java.awt.*; class FontCanvas extends Canvas { public void paint(Graphics g) { GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); String fonts[] = ge.getAvailableFontFamilyNames(); for(int index = 0; index < 50; index++) { g.setFont(new Font(fonts[index], Font.BOLD, 20)); g.drawString(fonts[index], 10, 15*index); } public class FontExam { public static void main(String args[]) { Frame frm = new Frame("Font "); frm.setSize(300,900); frm.add(new FontCanvas(), "Center"); frm.setVisible(true); }

15/20 setLayout(new GridLayout(4,4)); TextField(20); Frame.setBounds(10, 10, 200, 250); TextField Panel.setBounds(0, 30, 200, 40); Button Panel.setBounds(0, 70, 200, 180); public void setBounds(int x, int y, int width, int height) x - 이 컴퍼넌트의 새로운 x 좌표 y - 이 컴퍼넌트의 새로운 y 좌표 width - 이 컴퍼넌트의 새로운 width height - 이 컴퍼넌트의 새로운 height

16/20 import java.awt.*; class ButtonPane extends Panel { private Button[] butt = null; private final String[] buttName = {"7", "8", "9", "/", "4", "5", "6", "*", "1", "2", "3", "-", "0", "c", "=", "+" }; public ButtonPane() { setLayout(new GridLayout(4,4)); butt = new Button[buttName.length]; for(int i = 0; i < butt.length; i++) { butt[i] = new Button(buttName[i]); add(butt[i]); }

17/20 class Calculator { public Calculator() { Frame mainFrame = new Frame(" 전자계산기 "); mainFrame.setLayout(new BorderLayout()); mainFrame.setBounds(10, 10, 200, 250); TextField display = new TextField(20); display.setText("0"); display.setEditable(false); Panel displayPane = new Panel(); displayPane.setBounds(0, 30, 200, 40); displayPane.add(display); ButtonPane buttonPane = new ButtonPane(); buttonPane.setBounds(0, 70, 200, 180); mainFrame.add("North", displayPane); mainFrame.add("Center", buttonPane); mainFrame.setVisible(true); } public static void main(String args[]) { Calculator calculator = new Calculator(); } }

18/20 TextArea(); MenuBar mbar = new MenuBar(); Menu menu = new Menu(" 파일 "); MenuItem mitem = new MenuItem(" 새파일 "); menu.add(mitem); mbar.add(menu); Frame.setMenuBar(mbar);

19/20 import java.awt.*; public class Memojang { public Memojang() { Frame frame = new Frame(); TextArea ta = new TextArea(); frame.add(ta,"Center"); MenuBar mbar = new MenuBar(); Menu menu = new Menu(" 파일 "); MenuItem mitem1 = new MenuItem(" 새파일 "); MenuItem mitem2 = new MenuItem(" 열기 "); MenuItem mitem3 = new MenuItem(" 저장 "); MenuItem mitem4 = new MenuItem(" 종료 "); menu.add(mitem1); menu.add(mitem2); menu.add(mitem3); menu.add(mitem4); mbar.add(menu); frame.setMenuBar(mbar); frame.setSize(600,500); frame.setVisible(true); } public static void main(String[] args) { Memojang memo = new Memojang(); }