Java图形程序设计北京理工大学计算机系 金旭亮第十三讲
2009-7-29 2北京理工大学计算机系 (共 48页)
Java绘图功能简介
Java的绘图能力:
绘制 2维图形,3维图形需要附加的支持包
控制颜色
选择字体
Java 2D API
提供大量的 2维图形绘制和填充功能
2009-7-29 3北京理工大学计算机系 (共 48页)
绘图的第一步
显示一个,裸体,窗体,SimpleFrameTest.java
窗体是一个对象,从 JFrame类中派生出来。
2009-7-29 4北京理工大学计算机系 (共 48页)
设定窗体行为
实例,CenteredFrameTest
1,自定义图标
2,自动居中。
2009-7-29 5北京理工大学计算机系 (共 48页)
Java窗体和面板继承树
2009-7-29 6北京理工大学计算机系 (共 48页)
向窗体中加入面板
将一个 JPanel对象加入到窗体中,在其上绘图
实例,NotHelloWorld.java
2009-7-29 7北京理工大学计算机系 (共 48页)
Java坐标系统
2009-7-29 8北京理工大学计算机系 (共 48页)
图形上下文与图形对象
Graphics context(图形上下文)
用于在屏幕上绘图
Graphics对象管理图形上下文
Graphics类 是一个抽象基类,必须派生出具体子类以实现绘图功能。
类 Component 有一个 paint方法,采用一个
Graphics 对象作为参数
public void paint( Graphics g )
通过调用类 Component的 repaint方法来调用
paint
2009-7-29 9北京理工大学计算机系 (共 48页)
颜色控制
类 Color用于处理颜色
三原色原理:
R-Red,0~255
G-Green,0~255
B-Blue,0~255
2009-7-29 10北京理工大学计算机系 (共 48页)
颜色常数
Color C o n s t a n t C o l o r R G B v a l u e
p u b l i c f i n a l s t a t i c C o l o r o r a n g e o ra n g e 2 5 5,2 0 0,0
p u b l i c f i n a l s t a t i c C o l o r p i n k p in k 2 5 5,1 7 5,1 7 5
p u b l i c f i n a l s t a t i c C o l o r c y a n c y a n 0,2 5 5,2 5 5
p u b l i c f i n a l s t a t i c C o l o r m a g e n t a m a g e n ta 2 5 5,0,2 5 5
p u b l i c f i n a l s t a tic C o l o r y e l l o w y e ll o w 2 5 5,2 5 5,0
p u b l i c f i n a l s t a t i c C o l o r b l a c k b l a c k 0,0,0
p u b l i c f i n a l s t a t i c C o l o r w h i t e w h i te 2 5 5,2 5 5,2 5 5
p u b l i c f i n a l s t a t i c C o l o r g r a y g ra y 1 2 8,1 2 8,1 2 8
p u b l i c f i n a l s t a t i c C o l o r l i g h t G r a y li g h t g ra y 1 9 2,1 9 2,1 9 2
p u b l i c f i n a l s t a t i c C o l o r d a r k G r a y d a rk g ra y 6 4,6 4,6 4
p u b l i c f i n a l s t a t i c C o l o r red re d 2 5 5,0,0
p u b l i c f i n a l s t a t i c C o l o r g r e e n g re e n 0,2 5 5,0
p u b l i c f i n a l s t a t i c C o l o r b l u e b lu e 0,0,2 5 5
F i g,1 1,3 Color c l a s s static c o n s t a n t s a n d R G B v a l u e s
2009-7-29 11北京理工大学计算机系 (共 48页)
Color类的一些方法
M e t h o d D e s c r i p t i o n
p u b l i c C o l o r ( i n t r,i n t g,
int b )
Cre a t e s a c o l o r b a se d o n re d,g re e n a n d b l u e c o n t e n t s
e x p re ss e d a s i n t e g e rs fro m 0 t o 2 5 5,
p u b l i c C o l o r ( f l o a t r,
f l o a t g,f l o a t b )
Cre a t e s a c o l o r b a se d o n re d,g re e n a n d b l u e c o n t e n t s
e x p re ss e d a s fl o a t i n g - p o i n t v a l u e s fro m 0,0 t o 1,0,
p u b l i c i n t g e t R e d ( )
/ / C o l o r c l a s s
Re t u rn s a v a l u e b e t w e e n 0 a n d 2 5 5 re p re se n t i n g t h e re d
c o n t e n t,
p u b l i c i n t g e t G r e e n ( )
/ / C o l o r c l a s s
Re t u rn s a v a l u e b e t w e e n 0 a n d 2 5 5 re p re se n t i n g t h e g re e n
c o n t e n t,
p u b l i c i n t g e t B l u e ( )
/ / C o l o r c l a s s
Re t u rn s a v a l u e b e t w e e n 0 a n d 2 5 5 re p re se n t i n g t h e b l u e
c o n t e n t,
p u b l i c C o l o r g e t C o l o r ( )
/ / G r a p h i c s c l a s s
Re t u rn s a C o l o r o b j e c t re p r e se n t i n g t h e c u r re n t c o l o r fo r t h e
g ra p h i c s c o n t e x t,
p u b l i c v o i d s e t C o l o r ( C o l o r
c ) / / G r a p h i c s c l a s s
S e t s t h e c u rre n t c o l o r fo r d ra w i n g w i t h t h e g ra p h i c s c o n t e x t,
F i g,1 1,4 Color m e t h o d s a n d c o l o r - r e l a t e d Graphics m e t h o d s,
2009-7-29 12北京理工大学计算机系 (共 48页)
颜色实例
ShowColors.java
2009-7-29 13北京理工大学计算机系 (共 48页)
选择颜色对话框
ShowColors2.java,使用 JColorChooser类
2009-7-29 14北京理工大学计算机系 (共 48页)
JColorChooser对话框
2009-7-29 15北京理工大学计算机系 (共 48页)
选择字体
类 Font用于操纵字体
字体的基本属性:
名字 name:
Monospaced,SansSerif,Serif,等,
风格 style:
Font.PLAIN,Font.ITALIC 和 Font.BOLD
大小 size:
以点来度量 (1/72英寸 )
2009-7-29 16北京理工大学计算机系 (共 48页)
Font类常量和相关的绘图方法
M e t h o d o r c o n s t a n t D e s c r i p t i o n
p u b l i c f i n a l s t a t i c i n t P L A I N
/ / F o n t c l a s s
A c o n sta n t re p re s e n t i n g a p l a i n fo n t st y l e,
p u b l i c f i n a l s t a t i c i n t B O L D
/ / F o n t c l a s s
A c o n sta n t re p re s e n t i n g a b o l d fo n t sty l e,
p u b l i c f i n a l s t a t i c i n t I T A L I C
// F o n t c l a s s
A c o n sta n t re p re s e n t i n g a n i t a l i c fo n t st y l e,
p u b l i c F o n t ( S t r i n g n a m e,int
s t y l e,int s i z e )
Crea t e s a Font o b j e c t w i t h t h e s p e c i f i e d fo n t,sty l e a n d s i z e,
p u b l i c i n t g e t S t y l e ( )
/ / F o n t c l a s s
Re t u rn s a n i n t e g e r v a l u e i n d i c a t i n g t h e c u rre n t fo n t sty l e,
p u b l i c i n t g e t S i z e ( )
/ / F o n t c l a s s
Re t u rn s a n i n t e g e r v a l u e i n d i c a t i n g t h e c u rre n t fo n t si z e,
p u b l i c S t r i n g g e t N a m e ( )
/ / F o n t c l a s s
Re t u rn s t h e c u rre n t fo n t n a m e a s a str i n g,
p u b l i c S t r i n g g e t F a m i l y ( )
/ / F o n t c l a s s
Re t u rn s t h e fo n t ’ s fa m i l y n a m e a s a stri n g,
F i g,1 1,8 Font m e t h o d s,c o n s t a n t s a n d f o n t - r e l a t e d Graphics m e t h o d s ( P a r t 1 o f 2 ),
2009-7-29 17北京理工大学计算机系 (共 48页)
M e th o d o r c o n s t a n t De s c ri p ti o n
public boolean is Pl ai n( )
// Font class
T e sts a fo n t fo r a p l a i n fo n t st y l e,Re t u r n s t ru e i f t h e
fo n t i s p l a i n,
public boolean is Bo ld ()
// Font class
T e sts a fo n t fo r a b o l d fo n t sty l e,Re t u rn s tr ue i f t h e
fo n t i s b o l d,
public boolean is It al ic ()
// Font class
T e sts a fo n t fo r a n i t a l i c fo n t st y l e,Re t u rn s t ru e i f t h e
fo n t i s i t a l i c,
public Font getFo nt()
// Graphics class
Re t u rn s a Fo nt o b j e c t re fe re n c e re p re se n t i n g t h e c u rre n t
fo n t,
public void se tF o nt ( Fo nt f )
// Graphics class
S e t s t h e c u rre n t fo n t t o t h e fo n t,sty l e a n d s i z e sp e c i f i e d
b y t h e Font o b j e c t re fe re n c e f,
F i g,1 1,8 Font m e t h o d s,c o n s t a n t s a n d f o n t - r e l a t e d Graphics m e t h o d s ( P a r t 2 o f 2 ),
Font类常量和相关的绘图方法
2009-7-29 18北京理工大学计算机系 (共 48页)
字体示例
Fonts.java
2009-7-29 19北京理工大学计算机系 (共 48页)
绘制直线,矩形和椭圆
类 Graphics用于绘制基本图形
所有绘图方法都需要两个参数,width和
height
2009-7-29 20北京理工大学计算机系 (共 48页)
绘图方法列表
M e t h o d D e s c r i p t i o n
p u b l i c v o i d d r a w L i n e ( i n t x1,
int y1,i n t x 2,int y2 )
Dra w s a l i n e b e t w e e n t h e p o i n t ( x1,y1 ) a n d t h e p o i n t ( x2,y2 ),
p u b l i c v o i d d r a w R e c t ( i n t x,
int y,int w i d t h,i n t h e i g h t )
Dra w s a re c t a n g l e o f t h e s p e c i f i e d width a n d height,T h e t o p -
l e ft c o rn e r o f t h e re c t a n g l e h a s t h e c o o rd i n a t e s ( x,y ),
p u b l i c v o i d f i l l R e c t ( i n t x,
int y,int w i d t h,i n t h e i g h t )
Dra w s a so l i d re c t a n g l e w i t h t h e sp e c i f i e d w id th a n d height,
T h e t o p - l e ft c o rn e r o f t h e re c t a n g l e h a s t h e c o o r d i n a t e ( x,y ),
p u b l i c v o i d c l e a r R e c t ( int x,
int y,int w i d t h,i n t h e i g h t )
Dra w s a so l i d re c t a n g l e w i t h t h e sp e c i f i e d w id th a n d height i n
t h e c u rre n t b a c k g ro u n d c o l o r,T h e t o p - l e ft c o rn e r o f t h e re c t a n g l e
h a s t h e c o o rd i n a t e ( x,y ),
p u b l i c v o i d d r a w R o u n d R e c t ( i n t
x,i n t y,int w i d t h,i n t
h e i g h t,i n t a r c W i d t h,i n t
a r c H e i g h t )
Dra w s a re c t a n g l e w i t h ro u n d e d c o rn e rs i n t h e c u rre n t c o l o r w i t h t h e
sp e c i fi e d width a n d he ig h t,T h e ar cW i dt h a n d arcHeight
d e t e rm i n e t h e ro u n d i n g o f t h e c o rn e rs (se e F i g,1 1,1 5 ),
p u b l i c v o i d f i l l R o u n d R e c t ( i n t
x,i n t y,int w i d t h,i n t
h e i g h t,i n t a r c W i d t h,i n t
a r c H e i g h t )
Dra w s a so l i d re c t a n g l e w i t h ro u n d e d c o rn e rs i n t h e c u rre n t c o l o r
w i t h t h e sp e c i f i e d width a n d height,T h e arcWidth a n d
arcHeight d e t e r m i n e t h e ro u n d i n g o f t h e c o rn e rs (se e F i g,1 1,1 5 ),
F i g,1 1,1 3
Graphics m e t h o d s t h a t d r a w l i n e s,r e c t a n g l e s a n d o v a l s ( P a r t 1 o f 2 ),
2009-7-29 21北京理工大学计算机系 (共 48页)
绘图方法列表
M e t h o d D e s c r i p t i o n
p u b l i c v o i d f i l l 3 D R e c t (
int x,int y,i n t w i d t h,
int h e i g h t,b o o l e a n b )
D ra w s a f i l l e d t h re e - d i m e n s i o n a l re c t a n g l e i n t h e c u rre n t c o l o r w i t h t h e
sp e c i fie d width a n d h e i g h t,Th e t o p - l e ft c o rn e r o f t h e re c t a n g l e
h a s t h e c o o rd i n a t e s ( x,y ),Th e re c t a n g l e a p p e a rs ra i se d w h e n b i s t ru e
a n d i s l o w e re d w h e n b i s fa l s e,
p u b l i c v o i d d r a w O v a l ( i n t
x,i n t y,int w i d t h,i n t
h e i g h t )
D ra w s a n o v a l i n t h e c u rre n t c o l o r w i t h t h e s p e c i fi e d w i d t h a n d
height,Th e b o u n d i n g re c t a n g l e ’ s t o p - l e ft c o rn e r i s a t t h e
c o o r d i n a t e s ( x,y ),Th e o v a l t o u c h e s a l l fo u r si d e s o f t h e b o u n d i n g
re c t a n g l e a t t h e c e n t e r o f e a c h s i d e (s e e F i g,1 1,1 6 ),
p u b l i c v o i d f i l l O v a l ( i n t
x,i n t y,int w i d t h,i n t
h e i g h t )
D ra w s a f i l l e d o v a l i n t h e c u rr e n t c o l o r w i t h t h e sp e c i f i e d widt h a n d
height,Th e b o u n d i n g re c t a n g l e ’ s t o p - l e ft c o rn e r i s a t t h e
c o o r d i n a t e s ( x,y ),Th e o v a l t o u c h e s a l l fo u r si d e s o f t h e b o u n d i n g
re c t a n g l e a t t h e c e n t e r o f e a c h s i d e (s e e F i g,1 1,1 6 ),
F i g,1 1,1 3 Graphics m e t h o d s t h a t d r a w l i n e s,r e c t a n g l e s a n d o v a l s ( P a r t 2 o f 2 ),
2009-7-29 22北京理工大学计算机系 (共 48页)
LinesRectsOvals.java
基本图形绘制实例
2009-7-29 23北京理工大学计算机系 (共 48页)
关于圆角矩形
2009-7-29 24北京理工大学计算机系 (共 48页)
绘制圆弧
DrawArcs.java
2009-7-29 25北京理工大学计算机系 (共 48页)
绘制椭圆
2009-7-29 26北京理工大学计算机系 (共 48页)
绘制椭圆的方法
M e t h o d D e s c r i p t i o n
public void dr aw A rc ( int x,
int y,int width,int height,
int startAngle,i nt arcAngle )
Dra w s a n a r c re l a t i v e t o t h e b o u n d i n g re c t a n g l e ’ s t o p - l e ft
c o o r d i n a t e s ( x,y ) w i t h t h e s p e c i f i e d width a n d height,
T h e a rc se g m e n t i s d ra w n sta rt i n g a t sta rt An g l e a n d s w e e p s
a rc An g l e d e g re e s,
public void fi ll A rc ( int x,
int y,int width,int height,
int startAngle,i nt arcAngle )
Dra w s a so l i d a rc ( i,e,,a se c t o r) re l a t i v e t o t h e b o u n d i n g
re c t a n g l e ’ s t o p - l e ft c o o rd i n a t e s ( x,y ) w i t h t h e sp e c i fi e d
width a n d height,T h e a r c se g m e n t i s d r a w n sta rt i n g a t
sta rt A n g l e a n d s w e e p s a r c A n g l e d e g re e s,
F i g,1 1,1 8 Graphics m e t h o d s f o r d r a w i n g a r c s,
2009-7-29 27北京理工大学计算机系 (共 48页)
绘制多边形与折线
类 Polygon
实例,DrawPolygons.java
2009-7-29 28北京理工大学计算机系 (共 48页)
显示图像
使用 Image对象代表图像,使用 ImageIO类从图像文件中读入图像,使用 g.drawImage显示图像
实例,ImageTest.java
2009-7-29 29北京理工大学计算机系 (共 48页)
Java 2D API
Java 2D API提供了大量绘制图形的方法,分布在以下包中:
java.awt
java.awt.image
java.awt.color
java.awt.font.geom
java.awt.print
java.awt.image.renderable
类 java.awt.Graphics2D派生自
java.awt.Graphics,以前用到的
Graphics对象就是 Graphics2D对象。
2009-7-29 30北京理工大学计算机系 (共 48页)
Java 2D 图形
java.awt.geom包中包括以下图形
Ellipse2D.Double
Rectangle2D.Double
RoundRectangle2D.Double
Arc3D.Double
Lines2D.Double
上面的 Double表示以双精度数来表示坐标。
2009-7-29 31北京理工大学计算机系 (共 48页)
实例
Shapes.java
2009-7-29 32北京理工大学计算机系 (共 48页)
2D图形实例
Shapes2.java:使用路径( GeneralPath类 ),
排列已有图形
2009-7-29 33北京理工大学计算机系 (共 48页)
动画实例
Myframe.java,实现文本动画:
采用
Thread.sleep(25);
实现程序暂停。
调用 repaint()方法主动调用 paint(Graphics g)
方法进行绘图。
一些简单的通过循环实现的图形示例
2009-7-29 35北京理工大学计算机系 (共 48页)
2009-7-29 36北京理工大学计算机系 (共 48页)
2009-7-29 37北京理工大学计算机系 (共 48页)
欣赏几幅计算机图形阿基米德螺线
2009-7-29 38北京理工大学计算机系 (共 48页)
Circle的妙用
2009-7-29 39北京理工大学计算机系 (共 48页)
点的追逐
2009-7-29 40北京理工大学计算机系 (共 48页)
极坐标绘图
2009-7-29 41北京理工大学计算机系 (共 48页)
随机花朵
2009-7-29 42北京理工大学计算机系 (共 48页)
递归图形
2009-7-29 43北京理工大学计算机系 (共 48页)
三角形的递归分形
2009-7-29 44北京理工大学计算机系 (共 48页)
课后实践
通过灵活使用循环语句,配合简单的绘图函数,
可以绘出很漂亮的计算机图形。
基本原理:通过用户界面组件的 getGraphics()方法创建 Graphics对象进行绘图
计算机图形学中的分形算法可以造就出让人眼光一亮,令人惊叹的的数学艺术图案!
感兴趣的同学可以自行钻研,如果有了好作品,
请交到我这儿,上课时向同学们展示。
2009-7-29 45北京理工大学计算机系 (共 48页)
课后练习
通过学习范例程序,利用 Java的 2D API,结合数学知识,绘制各种函数的图形,并用路径进行图形变幻。
学习 Java 2D Demo,掌握 Java动画及图形处理技巧。
2009-7-29 46北京理工大学计算机系 (共 48页)
多线程图形实例
MovingShapes,一个复杂的 Java程序
2009-7-29 47北京理工大学计算机系 (共 48页)
程序分析
创建图形对象的继承体系(两个基类:
Shape和 BoundedShape)
用类 MovingShape 包装图形对象
public class MovingShape extends Thread
2009-7-29 48北京理工大学计算机系 (共 48页)
程序分析
使用自定义类 PaintPanel (派生自 JPanel)作为绘图表面,在其中处理鼠标事件,并启动 MovingShape对象多线程移动图形,调用
paintComponent()方法重绘整个屏幕其中用到了反射
类 PaintProgram继承自 JFrame,是主窗体,
也是程序入口