zoukankan      html  css  js  c++  java
  • Java—图形处理

    抽象窗口化工具(AWT)为图形用户界面编程提供API编程接口,使得Java可以提供较好的图形用户界面。

    AWT把图形处理分为两个层次:一是处理原始图形,这一层较原始,图形直接以点、线和面的形式画到界面上;二是提供大量组件,实现可定制的图形用户界面。

    1  Paint方法、Update方法和Repaint方法

    1) Paint方法

           public void paint(Graphics g)

           以画布为参数,在画布上执行画图方法。在Applet中,不显式地调用paint方法。

    2) Repaint方法

           Applet重画时系统自动调用paint方法。

    3) Update方法

           public void update(Graphics g)

           更新容器,向Repaint发出刷新小应用程序的信号,缺省的Update方法清除Applet画图区并调用Paint方法。

    2 Graphics类

    Graphics类是所有图形上下文的抽象基类,允许应用程序在各种设备上实现组件的画图。图形对象封装了Java支持的基本渲染操作的状态信息,包括画图的组件对象、渲染区域的坐标(coordinates)、区域(clip)、颜色(color)、字体(font)、画图模式等。Graphics类提供画各种图形的方法,其中包括线、圆和椭圆、矩形和多边形、图像以及各种字体的文本等。这些方法具体如下:

            public abstract void clipRect(int x, int y, int width, int height)                 指定的区域切分。

    Paint颜料属性决定线条绘制的颜色。 Font字体。Stroke 画笔属性确定线型,如实线、虚线或点划线。 Transform转换属性定义渲染过程中应用的转换方法,使绘制的图形平移、旋转和缩放。

    Composite 合成属性决定如何在组件上绘制叠放图形。Clip剪切属性定义组件上的一个区域边界。

    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 abstract void drawPolygon(int[] xPoints, int[] yPoints, int nPoints)          画x和y坐标定义的多边形。

    public void drawRect(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 abstract 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对象的一个实例,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 import java.awt.*;
     2 import java.applet.*;
     3 import java.awt.geom.*;
     4 public class UseColor extends Applet
     5 {
     6     public void paint(Graphics oldg) 
     7     {
     8     Graphics2D g = (Graphics2D)oldg;
     9         g.setColor(Color.blue);
    10         g.fill(new Ellipse2D.Float(50,50,150,150));
    11         g.setColor(new Color(255,0,0,0));
    12         g.fill(new Ellipse2D.Float(50,50,140,140));
    13         g.setColor(new Color(255,0,0,64));
    14         g.fill(new Ellipse2D.Float(50,50,130,130));
    15         g.setColor(new Color(255,0,0,128));
    16         g.fill(new Ellipse2D.Float(50,50,110,110)); 
    17         g.setColor(new Color(255,0,0,255));
    18         g.fill(new Ellipse2D.Float(50,50,90,90));
    19         g.setColor(new Color(255,200,0));
    20         g.fill(new Ellipse2D.Float(50,50,70,70));
    21     }
    22 }
    View Code

    输出结果:

    3 Graphics2D类

    Graphics2D类继承于Graphics类,提供几何学、坐标变换、颜色管理以及文本排列等的更高级控制。

    Graphics2D类是Java平台上渲染二维图形、文字、以及图片的基础类,提供较好的对绘制形状、填充形状、旋转形状、绘制文本、绘制图像以及定义颜色的支持。

    在AWT编程接口中,用户通过Paint方法接收Graphics对象作为参数,若是使用Graphics2D类,就需要在Paint方法中进行强制转换。

    Public void paint(Graphics old)

    {

      Graphics2D new = (Graphics2D)old;

    }

    3.1  绘制形状

      Graphics2D提供以下两个方法进行形状的绘制:

      public abstract void draw(Shape s)

            根据Graphics2D的环境设置画出形状s,其中Shape接口包含的类如表所示。

    其中GeneralPath是一般的几何路径,它的构造函数为:

    public GeneralPath()                构造一个空的对象。

    常用的方法有四个,分别如下:

    public void lineTo(float x, float y)                                从当前坐标点到(x,y)坐标点画一条直线,将此点添加到路径上。

    public void moveTo(float x, float y)                             移动到坐标点(x,y),在路径上添加此点。

    public abstract void fill(Shape s)                                 画实心形状s。

    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的环境设置。所谓的环境设置是指设置画图的笔画和填充属性等,设置方法分别如下:

    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,渐变填充类常用构造函数为

    public GradientPaint(float x1, float y1, Color color1, float x2, float y2, Color color2, boolean cyclic)

    构建一个渐变GradientPaint对象,在起始坐标点到目标坐标点之间从颜色color1到color2渐变,cyclic为真,循环渐变。

     1 package test;
     2 
     3 import java.awt.*;
     4 import java.applet.*;
     5 import java.awt.geom.*;
     6 public class GUI2D extends Applet
     7 {
     8     /**
     9      * 
    10      */
    11     private static final long serialVersionUID = 5012904640684084416L;
    12 
    13     public void paint(Graphics oldg)
    14     {
    15         Graphics2D g = (Graphics2D)oldg;
    16         //设置笔画宽度
    17         BasicStroke stroke = new BasicStroke(10);
    18         g.setStroke(stroke);
    19         //画线
    20         Line2D line = new Line2D.Float(0,0,20,30);
    21         g.draw(line);
    22         line = new Line2D.Float(50,50,100,50);
    23         g.draw(line);
    24         line = new Line2D.Float(50,50,50,100);
    25         g.draw(line);
    26         stroke = new BasicStroke(5);
    27         g.setStroke(stroke);
    28         //设置渐变填充
    29         GradientPaint gt = new GradientPaint(0,0,Color.green,50,30,Color.blue,true);
    30         g.setPaint((Paint)gt);
    31         //画矩形
    32         Rectangle2D rect = new Rectangle2D.Float(80,80,40,40);
    33     g.draw(rect);
    34     rect = new Rectangle2D.Float(100,100,40,40);
    35     g.fill(rect);
    36     //画椭圆
    37     Ellipse2D ellipse = new Ellipse2D.Float(120,120,30,40);
    38     g.draw(ellipse);
    39     gt = new GradientPaint(0,0,Color.red,30,30,Color.yellow,true);
    40     g.setPaint((Paint)gt);
    41     ellipse = new Ellipse2D.Float(140,140,20,20);
    42     g.fill(ellipse);
    43     //画圆角矩形
    44     RoundRectangle2D roundRect = new RoundRectangle2D.Float(160,160,40,40,20,20);
    45     g.draw(roundRect);
    46     roundRect = new RoundRectangle2D.Float(180,180,40,40,20,20);
    47     g.fill(roundRect);
    48     //画几何图形
    49     GeneralPath path = new GeneralPath();
    50     path.moveTo(150,0);
    51     path.lineTo(160,50);
    52     path.curveTo(190,200,240,140,200,100);
    53     g.fill(path);
    54     }
    55 }
    View Code

    输出结果:

    3.2  绘制文本

    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对象的区域。

    例:

     1 import java.awt.*;
     2 import java.applet.*;
     3 import java.awt.geom.*;
     4 import java.awt.font.*;
     5 public class GUIText extends Applet
     6 {
     7     public void paint(Graphics oldg)
     8     {
     9     Graphics2D g = (Graphics2D)oldg;
    10     //设置字体
    11     Font f1 = new Font("Courier",Font.PLAIN,24);
    12     Font f2 = new Font("helvetica",Font.BOLD,24);
    13     FontRenderContext frc = g.getFontRenderContext();
    14     String str = new String("这是一个文本布局类的实现");
    15     String str2 = new String("扩充绘制文本的功能");
    16     //构造文本布局对象
    17     TextLayout layout = new TextLayout(str, f1, frc);    
    18     Point2D loc = new Point2D.Float(20,50);
    19     //绘制文本
    20     layout.draw(g, (float)loc.getX(), (float)loc.getY());
    21     //设置边框
    22     Rectangle2D bounds = layout.getBounds();
    23     bounds.setRect(bounds.getX()+loc.getX(),
    24     bounds.getY()+loc.getY(),
    25     bounds.getWidth(),
    26     bounds.getHeight());
    27     g.draw(bounds);
    28     layout = new TextLayout(str2,f2,frc);
    29     g.setColor(Color.red);
    30     layout.draw(g,20,80);
    31     }
    32 }
    View Code

    3.3  绘制图像

    绘制图像用到BufferedImage类,BufferedImage类是指存放图像数据的可访问的缓冲。其构造函数为:

    public BufferedImage(int width, int height, int imageType)                使用宽度(width)、高度(height)和imageType类型构造BufferedImage对象。

    public Graphics2D createGraphics()                                  用图片填充椭圆的具体过程如下:

    (1) 创建一个Graphics2D,可以画到BufferedImage中。

    例如构建一个BufferedImage对象buf。

    BufferedImage buf = new BufferedImage (img.getWidth(this),

    img.getHeight(this),BufferedImage.TYPE_INT_ARGB);

    创建一个临时Graphics2D对象:

    Graphics 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) 然后设置填充模式,并进行填充:

    g.setPaint(t);

    g.fill(new Ellipse2D.Float(100,50,60,60));

    例:完成图像显示,并将区域蓝色透明显示,然后进行图片填充

     1 import java.awt.*;
     2 import java.applet.*;
     3 import java.awt.geom.*;
     4 import java.awt.font.*;
     5 import java.awt.image.*;
     6 import java.net.*;
     7 public class GUIImage extends Applet 
     8 {
     9      public void paint(Graphics oldg) 
    10      {
    11     Graphics2D g = (Graphics2D)oldg;
    12     try
    13     {
    14         URL imgURL = new URL(getDocumentBase(),"sample.gif");
    15         Image img = getImage(imgURL);
    16         int h = img.getHeight(this);
    17         int w = img.getWidth(this);
    18     //构造缓冲图像对象
    19     BufferedImage buf = new BufferedImage(w,h,BufferedImage.TYPE_INT_ARGB);
    20     //放入临时图形类
    21     Graphics tmpG = buf.createGraphics();
    22     tmpG.drawImage(img,10,10,this);
    23     g.drawImage(buf,10,20,this);
    24     //设置透明颜色对象              
    25     Color transBlue = new Color(0,0,255,100);
    26     g.setColor(transBlue);
    27     GeneralPath path = new GeneralPath();
    28     path.moveTo(60,0);
    29     path.lineTo(50,100);
    30     path.curveTo(160,230,240,140,200,100);
    31     g.fill(path);
    32     transBlue = new Color(0,0,255,240);
    33     g.fill(new Ellipse2D.Float(100,100,50,50));
    34     Rectangle2D rect = new Rectangle2D.Float(0,0,h,w);
    35     //图片填充
    36     TexturePaint t = new TexturePaint(buf,rect);
    37     g.setPaint(t);
    38     g.fill(new Ellipse2D.Float(100,50,60,60));
    39     }
    40     catch(Exception e)
    41     {
    42         System.out.println("Error:" + e.getMessage());
    43     }
    44        }
    45 }
    View Code
  • 相关阅读:
    STM32Cube Uart_DMA测试工程
    STM32CubeMX安装指南
    基于STM32Cube的ADC模数采样设计
    C++ this指针的用法
    用七段数码管显示26个字母的方案
    FPGA的引脚VCCINT 、VCCIO VCCA
    Keil环境中建立带FreeRTOS的STM32L项目
    STM32L时钟
    Mysql explain
    nginx屏蔽IP
  • 原文地址:https://www.cnblogs.com/feitiannengdou/p/5447363.html
Copyright © 2011-2022 走看看