Top Banner
Java@Ch15. Graphics 2011.04.01
13

Java @Ch15. Graphics 2011.04.01. Outline Graphical Coodinate Systems The Graphics Class Drawing Strings, Lines, Rectangles, and Ovals Drawing Polygons.

Dec 20, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Java @Ch15. Graphics 2011.04.01. Outline Graphical Coodinate Systems The Graphics Class Drawing Strings, Lines, Rectangles, and Ovals Drawing Polygons.

Java@Ch15. Graphics

2011.04.01

Page 2: Java @Ch15. Graphics 2011.04.01. Outline Graphical Coodinate Systems The Graphics Class Drawing Strings, Lines, Rectangles, and Ovals Drawing Polygons.

Outline

• Graphical Coodinate Systems• The Graphics Class• Drawing Strings, Lines, Rectangles, and Ovals• Drawing Polygons and Polylines

[Sample code]

TestPaintComponent.java

DrawPolyLine.java

Page 3: Java @Ch15. Graphics 2011.04.01. Outline Graphical Coodinate Systems The Graphics Class Drawing Strings, Lines, Rectangles, and Ovals Drawing Polygons.

Graphical Coodinate Systems

(0,0) at the upper-left corner

Page 4: Java @Ch15. Graphics 2011.04.01. Outline Graphical Coodinate Systems The Graphics Class Drawing Strings, Lines, Rectangles, and Ovals Drawing Polygons.

The Graphics Class

Java.awt.Graphics

+setColor(color: Color): void

+setFont(font: Font): void

+drawString(s: String , x: int , y:int): void

+drawLine(x1,:int , y1:int , x2:int , y2:int): void

+drawRect(x:int , y:int , w:int , h:int): void

+fillRect(x:int , y:int , w:int , h:int): void

+drawRoundRect(x:int , y:int , w:int , h:int , aw:int , ah:int): void

+fillRoundRect(x:int , y:int , w:int , h:int , aw:int , ah:int): void

+draw3DRect(x:int , y:int , w:int , h:int , raised:boolean): void

+fill3DRect(x:int , y:int , w:int , h:int , raised:boolean): void

課本 P.523

Page 5: Java @Ch15. Graphics 2011.04.01. Outline Graphical Coodinate Systems The Graphics Class Drawing Strings, Lines, Rectangles, and Ovals Drawing Polygons.

Drawing Strings, Lines, Rectangles, and Ovals

drawString(String s, int x, int y) method

drawLine(int x1, int y1, int x2, int y2) method

Page 6: Java @Ch15. Graphics 2011.04.01. Outline Graphical Coodinate Systems The Graphics Class Drawing Strings, Lines, Rectangles, and Ovals Drawing Polygons.

程式範例 :

TestPaintComponent.java

課本 P.524 Liting 15.1

Page 7: Java @Ch15. Graphics 2011.04.01. Outline Graphical Coodinate Systems The Graphics Class Drawing Strings, Lines, Rectangles, and Ovals Drawing Polygons.

drawRec(intx, int y, int w, int h) method

fillRec(intx, int y, int w, int h) method

Page 8: Java @Ch15. Graphics 2011.04.01. Outline Graphical Coodinate Systems The Graphics Class Drawing Strings, Lines, Rectangles, and Ovals Drawing Polygons.

drawRoundRect(x, y, w, h, aw, ah) method

drawOval(x, y ,w, h) method

Page 9: Java @Ch15. Graphics 2011.04.01. Outline Graphical Coodinate Systems The Graphics Class Drawing Strings, Lines, Rectangles, and Ovals Drawing Polygons.

Drawing Arcs

drawArc(int x, int y, int w, int h, int startAngle, int arcAngle)

fillArc(int x, int y, int w, int h, int startAngle, int arcAngle)

Page 10: Java @Ch15. Graphics 2011.04.01. Outline Graphical Coodinate Systems The Graphics Class Drawing Strings, Lines, Rectangles, and Ovals Drawing Polygons.

範例練習 :

DrawArcs.java

課本 P.530 Liting 15.4

Page 11: Java @Ch15. Graphics 2011.04.01. Outline Graphical Coodinate Systems The Graphics Class Drawing Strings, Lines, Rectangles, and Ovals Drawing Polygons.

程式練習 :1. 用 g.drawOval() 畫臉2. fillOval() 畫眼睛3. setColor() 設定畫筆顏色4. drawArc() 畫嘴巴

( 需 import java.awt.Color)

Page 12: Java @Ch15. Graphics 2011.04.01. Outline Graphical Coodinate Systems The Graphics Class Drawing Strings, Lines, Rectangles, and Ovals Drawing Polygons.

Drawing Polygons and Polylines

Java.awt.Polygon

+xpoints: int []

+ypoints: int []

+nponits: int []

+Polygon()

+Polygon(xpoints: int [] , ypoints: int [] , npoints: int [])

+addPoints(x: int, y: int)

課本 P.532

Page 13: Java @Ch15. Graphics 2011.04.01. Outline Graphical Coodinate Systems The Graphics Class Drawing Strings, Lines, Rectangles, and Ovals Drawing Polygons.

程式範例 :

DrawPolyLine.java