Java图形处理Paint方法、Update方法和Repaint方法.ppt

上传人:仙人指路1688 文档编号:2902222 上传时间:2023-03-01 格式:PPT 页数:49 大小:274.50KB
返回 下载 相关 举报
Java图形处理Paint方法、Update方法和Repaint方法.ppt_第1页
第1页 / 共49页
Java图形处理Paint方法、Update方法和Repaint方法.ppt_第2页
第2页 / 共49页
Java图形处理Paint方法、Update方法和Repaint方法.ppt_第3页
第3页 / 共49页
Java图形处理Paint方法、Update方法和Repaint方法.ppt_第4页
第4页 / 共49页
Java图形处理Paint方法、Update方法和Repaint方法.ppt_第5页
第5页 / 共49页
点击查看更多>>
资源描述

《Java图形处理Paint方法、Update方法和Repaint方法.ppt》由会员分享,可在线阅读,更多相关《Java图形处理Paint方法、Update方法和Repaint方法.ppt(49页珍藏版)》请在三一办公上搜索。

1、Java图形处理,1 Java图形2 Paint方法、Update方法和Repaint方法3 Graphics类4 Color类5 Graphics2D类,1 Java图形,抽象窗口化工具集(AWT)为图形用户界面编程提供接口,使得 Java可以提供较好的图形用户界面AWT把图形处理分为两个层次:1.原始图形处理,这一层较原始,图形直接以点、线和面的形 式画到界面上 2.提供大量组件,实现可定制的图形用户界面 Java编程语言中的图形坐标系统不同于数学中的坐标系,屏幕 左上角为(0,0),右下角为屏幕水平向右和垂直向下增长的像 素数,2 Paint、Repaint和Update方法,1.Pai

2、nt方法 public void paint(Graphics g)以画布为参数,在画布上执行画图方法 2.Repaint方法 Applet重画时系统自动调用paint方法 3.Update方法 public void update(Graphics g)更新容器,向Repaint发出刷新小应用程序的信号,缺省的Update方法清除Applet画图区并调用Paint方法,3 Graphics类,Graphics类是所有图形的抽象基类,允许应用程序在各种设备上实现组件的画图。Graphics类提供画各种图形的方法,其中包括线、圆和椭圆、矩形和多边形、图像以及各种字体的文本等。这些方法具体如下:p

3、ublic abstract void clipRect(int x,int y,int width,int height)指定的区域切分,public abstract void drawLine(int x1,int y1,int x2,int y2)使用当前颜色,在点(x1,y1)和(x2,y2)之间画线public abstract void drawOval(int x,int y,int width,int height)画椭圆public abstract void fillOval(int x,int y,int width,int height)画实心椭圆public abs

4、tract void drawPolygon(int xPoints,int yPoints,int nPoints)画x和y坐标定义的多边形,public void drawRect(int x,int y,int width,int height)画矩形public void fillRect(int x,int y,int width,int height)画实心矩形public abstract void drawRoundRect(int x,int y,int width,int height,int arcWidth,int arcHeight)画圆角矩形public abstr

5、act void drawString(String str,int x,int y)使用当前字体和颜色画字符串str,public abstract void setColor(Color c)设置图形上下文的当前颜色public abstract void setPaintMode()设置画模式public abstract boolean drawImage(Image img,int x,int y,ImageObserver observer)画特定图public abstract void setFont(Font font)设置特定的font字体。使用时首先得到font对象的一个

6、实例,Font类常用构造函数为:public Font(String name,int style,int size),通过指定的name、style和size创建字体实例。name指字体名,像“隶书”、“TimesRoman”等,字体风格为粗体、斜体,size指字号大小。例如:Font f=new Font(TimesRoman,Font.BOLD+Font.ITALIC,12);创建了具有粗斜体风格的12磅的TimesRoman字体。,【例1】设置Graphics对象画图,显示结果如图1所示。源程序代码如下:/程序文件名SimpleGui.javaimport java.awt.*;imp

7、ort java.applet.*;public class SimpleGUI extends AppletImage samImage;public void init()samImage=getImage(getDocumentBase(),sample.gif);,public void paint(Graphics g)/g.clipRect(50,50,180,180);/画线g.drawLine(0,0,20,30);/输出字符串g.drawString(图形显示,100,30);/设置颜色Color c=new Color(255,200,0);g.setColor(c);,/

8、设置字体Font f=new Font(TimesRoman,Font.BOLD+Font.ITALIC,24);g.setFont(f);g.drawString(图形显示,180,30);g.drawLine(20,20,100,50);g.drawLine(20,20,50,100);g.drawRect(40,40,40,40);g.fillRect(60,60,40,40);g.setColor(Color.red);/3D矩形g.draw3DRect(80,80,40,40,true);,g.draw3DRect(100,100,40,40,false);g.fill3DRect(

9、120,120,40,40,true);/椭圆g.drawOval(150,150,30,40);g.fillOval(170,170,20,20);g.setColor(Color.blue);/圆角矩形g.drawRoundRect(180,180,40,40,20,20);g.fillRoundRect(200,200,40,40,20,20);,/多边形int xC=242,260,254,297,242;int yC=240,243,290,300,270;g.drawPolygon(xC,yC,5);/图片g.drawImage(samImage,250,50,this);,图1

10、简单的图形界面,将例1注释的程序语句/g.clipRect(50,50,180,180);的注释符号去掉,重新编译执行,可以看见如图2所示的结果。,图2 裁剪后的图形界面,4 Color类,Color类是用来封装颜色的使用Color对象较为简单的方法是直接使用Color类提供的预定义的颜色,像红色Color.red、橙色Color.orange等;也可以使用RGB颜色模式进行定义。所谓RGB颜色模式是指使用三种基色:红、绿、蓝,通过三种颜色的调整得出其它各种颜色,这三种基色的值范围为0255。例如Color c=new Color(255,200,0);定义橙色。表1给出常用颜色的RGB值以及

11、对应的类预定义参数。,表1 常用颜色的RGB值以及对应的类预定义参数,Color还有一个构造函数,它构造的Color对象用于是否透明显示颜色。public Color(int red,int green,int blue,int alpha)其中:前三个分量即RGB颜色模式中的参数,第四个alpha分量指透明的程度。当alpha分量为255时,表示完全不透明,正常显示;当alpha分量为0时,表示完全透明,前三个分量不起作用,而介于0255之间的值可以制造出颜色不同的层次效果。,【例2】测试Color对象,界面如图3所示。源程序代码如下:/程序文件名UseColor.javaimport ja

12、va.awt.*;import java.applet.*;import java.awt.geom.*;public class UseColor extends Appletpublic void paint(Graphics oldg)Graphics2D g=(Graphics2D)oldg;,g.setColor(Color.blue);g.fill(new Ellipse2D.Float(50,50,150,150);g.setColor(new Color(255,0,0,0);g.fill(new Ellipse2D.Float(50,50,140,140);g.setColo

13、r(new Color(255,0,0,64);g.fill(new Ellipse2D.Float(50,50,130,130);g.setColor(new Color(255,0,0,128);g.fill(new Ellipse2D.Float(50,50,110,110);g.setColor(new Color(255,0,0,255);g.fill(new Ellipse2D.Float(50,50,90,90);g.setColor(new Color(255,200,0);g.fill(new Ellipse2D.Float(50,50,70,70);,图3 颜色测试界面,5

14、 Graphics2D类,Graphics2D类继承于Graphics类,提供几何学、坐标变换、颜色管理以及文本排列等的更高级控制。Graphics2D类是Java平台上渲染二维图形、文字、以及图片的基础类,提供较好的对绘制形状、填充形状、旋转形状、绘制文本、绘制图像以及定义颜色的支持。在AWT编程接口中,用户通过Paint方法接收Graphics对象作为参数,若是使用Graphics2D类,就需要在Paint方法中进行强制转换。Public void paint(Graphics old)Graphics2D new=(Graphics2D)old;,5.1 绘制形状 Graphics2D提

15、供以下两个方法进行形状的绘制:public abstract void draw(Shape s)根据Graphics2D的环境设置画出形状s,其中Shape接口包含的类如表2所示。public abstract void fill(Shape s)画实心形状s。,表2 Graphics2D绘制的图形类,其中GeneralPath是一般的几何路径,它的构造函数为:public GeneralPath()构造一个空的对象。常用的方法有四个,分别如下:public void lineTo(float x,float y)从当前坐标点到(x,y)坐标点画一条直线,将此点添加到路径上。public v

16、oid moveTo(float x,float y)移动到坐标点(x,y),在路径上添加此点。,public void quadTo(float x1,float y1,float x2,float y2)以坐标点(x1,y1)为控制点,在当前坐标点和坐标点(x2,y2)之间插入二次曲线片断。public void curveTo(float x1,float y1,float x2,float y2,float x3,float y3)以(x1,y1)和(x2,y2)为控制点,在当前坐标点和(x3,y3)之间插入曲线片断。,在Draw方法中提到Graphics2D的环境设置。所谓的环境设置

17、是指设置画图的笔画和填充属性等,设置方法分别如下:public abstract void setStroke(Stroke s)设置笔画的粗细。其中Stroke接口中常用BasicStroke类来实现,一个较简单的构造函数为 public BasicStroke(float width)创建实线笔画宽度为width。public abstract void setPaint(Paint paint),设置Graphics2D环境的填充属性。其中,paint的值可以为渐变填充类java.awt.GradientPaint,也可以为图形填充类java.awt.TexturePaint,后者将在5

18、.3节绘制图像中讲到。渐变填充类常用构造函数为 public GradientPaint(float x1,float y1,Color color1,float x2,float y2,Color color2,boolean cyclic)构建一个渐变GradientPaint对象,在起始坐标点到目标坐标点之间从颜色color1到color2渐变,cyclic为真,循环渐变。,【例3】演示了几何形状、笔画变换以及颜色渐变显示。其中直线的笔画宽度为10,其它笔画宽度为5,中间三个图形实现绿色到蓝色的循环渐变,后三个图形实现红色到黄色的循环渐变,如图4所示。/程序文件名GUI2D.javaim

19、port java.awt.*;import java.applet.*;import java.awt.geom.*;public class GUI2D extends Applet,public void paint(Graphics oldg)Graphics2D g=(Graphics2D)oldg;/设置笔画宽度BasicStroke stroke=new BasicStroke(10);g.setStroke(stroke);/画线Line2D line=new Line2D.Float(0,0,20,30);g.draw(line);line=new Line2D.Float(

20、50,50,100,50);g.draw(line);,line=new Line2D.Float(50,50,50,100);g.draw(line);stroke=new BasicStroke(5);g.setStroke(stroke);/设置渐变填充GradientPaint gt=new GradientPaint(0,0,Color.green,50,30,Color.blue,true);g.setPaint(Paint)gt);/画矩形Rectangle2D rect=new Rectangle2D.Float(80,80,40,40);,g.draw(rect);rect=

21、new Rectangle2D.Float(100,100,40,40);g.fill(rect);/画椭圆Ellipse2D ellipse=new Ellipse2D.Float(120,120,30,40);g.draw(ellipse);gt=new GradientPaint(0,0,Color.red,30,30,Color.yellow,true);g.setPaint(Paint)gt);ellipse=new Ellipse2D.Float(140,140,20,20);g.fill(ellipse);,/画圆角矩形RoundRectangle2D roundRect=new

22、 RoundRectangle2D.Float(160,160,40,40,20,20);g.draw(roundRect);roundRect=new RoundRectangle2D.Float(180,180,40,40,20,20);g.fill(roundRect);/画几何图形GeneralPath path=new GeneralPath();path.moveTo(150,0);path.lineTo(160,50);path.curveTo(190,200,240,140,200,100);g.fill(path);,图4 通过Graphics2D对象绘制形状,5.2 绘制文

23、本 Graphics2D类提供一个文本布局(TextLayout)对象,用于实现各种字体或段落文本的绘制。其构造函数为:public TextLayout(String string,Font font,FontRenderContext frc)通过字符串string和字体font构造布局。public void draw(Graphics2D g2,float x,float y)将这个TextLayout对象画到Graphics2D对象g2上的x,y坐标处。public Rectangle2D getBounds()返回TextLayout对象的区域。,【例4】测试绘制文本功能,如图5所

24、示。源程序代码如下:/程序文件GUIText.javaimport java.awt.*;import java.applet.*;import java.awt.geom.*;import java.awt.font.*;public class GUIText extends Appletpublic void paint(Graphics oldg),Graphics2D g=(Graphics2D)oldg;/设置字体Font f1=new Font(Courier,Font.PLAIN,24);Font f2=new Font(helvetica,Font.BOLD,24);Font

25、RenderContext frc=g.getFontRenderContext();String str=new String(这是一个文本布局类的实现);String str2=new String(扩充绘制文本的功能);/构造文本布局对象TextLayout layout=new TextLayout(str,f1,frc);Point2D loc=new Point2D.Float(20,50);,/绘制文本layout.draw(g,(float)loc.getX(),(float)loc.getY();/设置边框Rectangle2D bounds=layout.getBounds

26、();bounds.setRect(bounds.getX()+loc.getX(),bounds.getY()+loc.getY(),bounds.getWidth(),bounds.getHeight();g.draw(bounds);layout=new TextLayout(str2,f2,frc);g.setColor(Color.red);layout.draw(g,20,80);,图5 Graphics2D对象绘制文本,5.3 绘制图像 绘制图像用到BufferedImage类,BufferedImage类是指存放图像数据的可访问的缓冲。其构造函数为:public Buffere

27、dImage(int width,int height,int imageType)使用宽度(width)、高度(height)和imageType类型构造BufferedImage对象。,用图片填充椭圆的具体过程如下:(1)创建一个Graphics2D,可以画到BufferedImage中。例如构建一个BufferedImage对象buf。BufferedImage buf=new BufferedImage(img.getWidth(this),img.getHeight(this),BufferedImage.TYPE_INT_ARGB);创建一个临时Graphics2D对象:Graph

28、ics tmpG=buf.createGraphics();将图像画入临时缓冲:tmpG.drawImage(img,10,10,this);,(2)用TexturePaint类进行填充:public TexturePaint(BufferedImage txtr,Rectangle2D anchor)构造TexturePaint对象,需要一个Rectangle2D对象来存放该对象:Rectangle2D rect=new Rectangle2D.Float(0,0,h,w);TexturePaint t=new TexturePaint(buf,rect);(3)然后设置填充模式,并进行填充

29、:g.setPaint(t);g.fill(new Ellipse2D.Float(100,50,60,60);,【例5】完成图像显示,并将区域蓝色透明显示,然后进行图片填充,如图6所示。源程序代码如下:/程序文件名GUIImage.javaimport java.awt.*;import java.applet.*;import java.awt.geom.*;import java.awt.font.*;import java.awt.image.*;import.*;,public class GUIImage extends Applet public void paint(Graph

30、ics oldg)Graphics2D g=(Graphics2D)oldg;tryURL imgURL=new URL(getDocumentBase(),sample.gif);Image img=getImage(imgURL);int h=img.getHeight(this);int w=img.getWidth(this);,/构造缓冲图像对象BufferedImage buf=new BufferedImage(w,h,BufferedImage.TYPE_INT_ARGB);/放入临时图形类Graphics tmpG=buf.createGraphics();tmpG.draw

31、Image(img,10,10,this);g.drawImage(buf,10,20,this);/设置透明颜色对象 Color transBlue=new Color(0,0,255,100);g.setColor(transBlue);GeneralPath path=new GeneralPath();,path.moveTo(60,0);path.lineTo(50,100);path.curveTo(160,230,240,140,200,100);g.fill(path);transBlue=new Color(0,0,255,240);g.fill(new Ellipse2D.

32、Float(100,100,50,50);Rectangle2D rect=new Rectangle2D.Float(0,0,h,w);/图片填充TexturePaint t=new TexturePaint(buf,rect);,g.setPaint(t);g.fill(new Ellipse2D.Float(100,50,60,60);catch(MalformedURLException e)System.out.println(Error:+e.getMessage();,图6 通过Graphics2D对象绘制图像,练习,1.使用Graphics类进行图形绘制,要求绘制深蓝色矩形和红色椭圆形,然后分别进行填充。2.使用Graphics2D类绘制粉红色到蓝色的渐变,填充到圆角矩形、多边形,笔画宽度为10。3.绘制文本“欢迎来到Java世界”,其中“欢迎来到”为蓝色显示,而“Java世界”为橙色显示,文本用矩形框起来,底色为黄色。4.画一个三角形区域,然后用图片进行填充。,

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 建筑/施工/环境 > 项目建议


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号