Java 2D 渲染——Java 2D Oracle官方教程翻译(双语对照)

本文翻译自oracle官方教程
<<Previous • Trail • Next>>

Java 2D Rendering

The Java 2D API provides a uniform rendering model across different types of devices.

Java 2D 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方法会随着适当的Graphics环境一起被自动的调用。

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. These features include:

Java 2D API 包括了java.awt.Graphics2D类,该类继承了Graphics类,用以提供对Java 2D API增强的制图和渲染特性的访问。这些特性包括:

  • Rendering the outline of any geometric primitive, using the stroke and paint attributes (draw method).
  • 使用笔划和颜料属性来渲染几何图元的轮廓(draw方法)。
  • Rendering any geometric primitive by filling its interior with the color or pattern specified by the paint attributes (fill method).
  • 用颜料属性所指定的颜色或图案填充几何图元的内部以实现对其渲染 (fill 方法)。
  • Rendering any text string (the drawString method). The font attribute is used to convert the string to glyphs, which are then filled with the color or pattern specified by the paint attributes.
  • 渲染任意文本串(drawString 方法)。字体属性是用来把字符串转换到字形的,其后字形又会被颜料属性指定的颜色或图案所填充。
  • Rendering the specified image (the drawImage method).
  • 渲染指定图像(drawImage 方法)。

In addition, the Graphics2D class supports the Graphics rendering methods for particular shapes, such as drawOval and fillRect.

此外,为了一些特殊图形,Graphics2D类还支持了Graphics渲染方法,比如drawOval和fillRect。

All methods that are represented above can be divided into two groups:

  1. Methods to draw a shape
  2. Methods that affect rendering

以上列举的所有方法可以分为两组:

  1. 画图形的方法
  2. 影响渲染的方法

The second group of the methods uses the state attributes that form the Graphics2D context for following purposes:

其中第二组方法会从Graphics2D环境获取状态属性,这样做的目的如下:

  • To vary the stroke width
  • 为调整笔划宽度
  • To change how strokes are joined together
  • 为改变笔划的接合方式
  • To set a clipping path to limit the area that is rendered
  • 为设置剪切路径来限制渲染区域
  • To translate, rotate, scale, or shear objects when they are rendered
  • 为在对象被渲染时,可以对它们进行平移、旋转、缩放或 剪错

5种基本的仿射变换

  • To define colors and patterns to fill shapes with
  • 为规定用于填充图形的颜色和图案
  • To specify how to compose multiple graphics objects
  • 为指定多个制图对象的组合方式

To employ Java 2D API features in the application, cast the Graphics object passed into a component’s rendering method to a Graphics2D object. For example:
为了应用Java 2D API的特性,可以将传进组件渲染方法的Graphics对象转为一个Graphics2D对象。例如:

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

As the following figure shows, the Graphics2D class rendering context contains several attributes.
如下列示意图所示,Graphics2D类渲染环境包含了一些属性。

StrokeThe 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.

pen attribute应用于图形轮廓。这个笔划属性使你能够以任意大小的点和迷人的图案来画直线,并能够对直线应用端帽和加入装饰。

在这里插入图片描述The fill attribute is applied to a shape’s interior. This paint attribute enables you to fill shapes with solid colors, gradients, and patterns.

fill attribute应用于图形内部。这个颜料属性使你能够用纯色、渐变和图案来填充图形。
在这里插入图片描述

在这里插入图片描述The compositing attribute is used when rendered objects overlap existing objects.

compositing attribute应用于被渲染对象与已存在对象重叠的情况下。

在这里插入图片描述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.

transform attribute用于在渲染过程中将被渲染对象的坐标从用户空间变换到设备空间。平移、旋转、缩放、剪错,这些变换也可以通过这个属性来实现。

在这里插入图片描述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.

clip,当使用一个Shape对象来定义一条剪切路径时,它会相应的定义出一个clip。而这个clip就约束着对这个Shape对象轮廓内部区域的渲染。

在这里插入图片描述The font attribute is used to convert text strings to glyphs.

font attribute用来将文本串转换到字形。

在这里插入图片描述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.

rendering hints指定了一些参数设置用来权衡速度与质量。例如,在抗锯齿特性可用的条件下,你可以指定是否使用它。另请参阅 渲染质量控制

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.

传递适当的属性对象即可对属性进行设置。比如想要将颜料属性改为蓝-绿渐变填充,你可以像下面的例子中那样构造一个GradientPaint对象并访问setPaint方法。

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

<<Previous • Trail • Next>>

发布了9 篇原创文章 · 获赞 0 · 访问量 645

猜你喜欢

转载自blog.csdn.net/realcfuture/article/details/103939201