java @ch15. graphics 2011.04.01. outline graphical coodinate systems the graphics class drawing...

13
Java@Ch15. Graphics 2011.04.01

Post on 20-Dec-2015

238 views

Category:

Documents


2 download

TRANSCRIPT

Java@Ch15. Graphics

2011.04.01

Outline

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

[Sample code]

TestPaintComponent.java

DrawPolyLine.java

Graphical Coodinate Systems

(0,0) at the upper-left corner

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

Drawing Strings, Lines, Rectangles, and Ovals

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

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

程式範例 :

TestPaintComponent.java

課本 P.524 Liting 15.1

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

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

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

drawOval(x, y ,w, h) method

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)

範例練習 :

DrawArcs.java

課本 P.530 Liting 15.4

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

( 需 import java.awt.Color)

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

程式範例 :

DrawPolyLine.java