Documentation

The Java™ Tutorials
Hide TOC
Java 2D RenderingJava 2D渲染
Trail: 2D Graphics二维图形
Lesson: Overview of the Java 2D API Concepts课程:Java 2D API概念概述

Java 2D RenderingJava 2D渲染

The Java 2D API provides a uniform rendering model across different types of devices.Java2D API提供了跨不同类型设备的一致性渲染模型。At the application level, the rendering process is the same whether the target rendering device is a screen or a printer.在应用程序级别,无论目标渲染设备是屏幕还是打印机,渲染过程都是相同的。When a component needs to be displayed, its paint or update method is automatically invoked with the appropriate Graphics context.当需要显示组件时,将使用适当的图形上下文自动调用其paint方法或update方法。

The Java 2D API includes the java.awt.Graphics2D class, which extends the Graphics class to provide access to the enhanced graphics and rendering features of the Java 2D API.Java 2D API包括java.awt.Graphics2D类,该类扩展了Graphics类,以提供对Java 2D API的增强图形和渲染功能的访问。These features include:

In addition, the Graphics2D class supports the Graphics rendering methods for particular shapes, such as drawOval and fillRect.此外,Graphics2D类支持特定形状的图形渲染方法,如drawOvalfillRectAll methods that are represented above can be divided into two groups:上述所有方法可分为两组:

  1. Methods to draw a shape绘制形状的方法
  2. Methods that affect rendering影响渲染的方法

The second group of the methods uses the state attributes that form the Graphics2D context for following purposes:第二组方法使用构成Graphics2D上下文的状态属性,用于以下目的:

To employ Java 2D API features in the application, cast the Graphics object passed into a component's rendering method to a Graphics2D object.要在应用程序中使用Java 2D API功能,请将传递到组件渲染方法中的Graphics对象强制转换为Graphics2D对象。For example:例如:

public void paint (Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    ...
}

As the following figure shows, the Graphics2D class rendering context contains several attributes.如下图所示,Graphics2D类呈现上下文包含几个属性。

This figure represents a stroke to outline the shape The pen attribute is applied to the outline of a shape.笔属性应用于形状的轮廓。This stroke attribute enables you to draw lines with any point size and dashing pattern and apply end-cap and join decorations to a line.此笔划属性使您能够绘制具有任意点大小和虚线图案的线,并将端点封口和连接装饰应用于线。
This figure shows how to fill a shape with solid color The fill attribute is applied to a shape's interior.填充属性应用于形状的内部。This paint attribute enables you to fill shapes with solid colors, gradients, and patterns.此绘制属性使您能够使用纯色、渐变和图案填充形状。
This figure shows how to composite an existing image and a graphic primitives The compositing attribute is used when rendered objects overlap existing objects.当渲染对象与现有对象重叠时,将使用合成属性
This figure represents shearing transform The transform attribute is applied during rendering to convert the rendered object from user space to device-space coordinates.在渲染期间应用变换属性,以将渲染对象从用户空间坐标转换为设备空间坐标。Optional translation, rotation, scaling, or shearing transforms can also be applied through this attribute.还可以通过该属性应用可选的平移、旋转、缩放或剪切变换。
This figure represents how to define the clipping path by using the Shape object The clip, type restricts rendering to the area within the outline of the Shape object used to define the clipping path.剪辑类型将渲染限制在用于定义剪辑路径的形状对象轮廓内的区域。Any Shape object that is used to define the clip.用于定义剪辑的任何Shape对象。
This figure demonstrates a sample of glyphs

The font attribute is used to convert text strings to glyphs.字体属性用于将文本字符串转换为字形。

This figure represents antialiasing Rendering hints specify preferences in the trade-offs between speed and quality.渲染提示在速度和质量之间的权衡中指定首选项。For example, you can specify whether antialiasing should be used, if this feature available.例如,如果此功能可用,可以指定是否应使用抗锯齿。See also Controlling Rendering Quality.请参见控制渲染质量

To learn more about transforming and compositing see the Advanced Topics in Java2D lesson.要了解有关转换和合成的更多信息,请参阅Java2D课程中的高级主题

When an attribute is set, the appropriate attribute object is passed.设置属性时,将传递相应的属性对象。As the following example shows, to change the paint attribute to a blue-green gradient fill, you construct a GradientPaint object and then call the setPaint method.如以下示例所示,要将paint属性更改为蓝绿色渐变填充,请构造GradientPaint对象,然后调用setPaint方法。

gp = new GradientPaint(0f,0f,blue,0f,30f,green);
g2.setPaint(gp);

Previous page: Coordinates
Next page: Geometric Primitives