Documentation

The Java™ Tutorials
Hide TOC
Stroking and Filling Graphics Primitives描边和填充图形原语
Trail: 2D Graphics
Lesson: Working with Geometry

Stroking and Filling Graphics Primitives描边和填充图形原语

You already know how to create different geometric primitives and more complicated shapes. 您已经知道如何创建不同的几何图元和更复杂的形状。This lesson teaches how to add some color and fancy outlines to your graphics and represents filling and stroking:本课程介绍如何在图形中添加一些颜色和奇特的轮廓,并表示填充和描边:

To apply fancy line styles and fill patterns to geometric primitives change the stroke and paint attributes in the Graphics2D context before rendering. 要将花式线样式和填充图案应用于几何图元,请在渲染之前更改Graphics2D上下文中的笔划和绘制属性。For example, draw a dashed line by creating an appropriate Stroke object. 例如,通过创建适当的Stroke对象绘制虚线。To add this stroke to the Graphics2D context before you render the line call the setStroke method. 要在渲染线条之前将此描边添加到Graphics2D上下文,请调用setStroke方法。Similarly, you apply a gradient fill to a Shape object by creating a GradientPaint object and adding it to the Graphics2D context.类似地,通过创建GradientPaint对象并将其添加到Graphics2D上下文,可以将渐变填充应用于形状对象。

The following code lines enrich geometric primitives with filling and stroking context:以下代码行使用填充和笔划上下文来丰富几何图元:

// draw RoundRectangle2D.Double

final static float dash1[] = {10.0f};
    final static BasicStroke dashed =
        new BasicStroke(1.0f,
                        BasicStroke.CAP_BUTT,
                        BasicStroke.JOIN_MITER,
                        10.0f, dash1, 0.0f);
g2.setStroke(dashed);
g2.draw(new RoundRectangle2D.Double(x, y,
                                   rectWidth,
                                   rectHeight,
                                   10, 10));

Dashed rounded rectangle

// fill Ellipse2D.Double
redtowhite = new GradientPaint(0,0,color.RED,100, 0,color.WHITE);
g2.setPaint(redtowhite);
g2.fill (new Ellipse2D.Double(0, 0, 100, 50));

Polygon filled with gradient color

The ShapesDemo2D.java code example represents additional implementations of stoking and filling.ShapesDemo2D.java代码示例代表了描边和填充的其他实现。

Defining Fancy Line Styles and Fill Patterns定义花式线条样式和填充图案

Using the Java 2D Stroke and Paint classes, you can define fancy line styles and fill patterns.使用Java 2D StrokePaint类,可以定义奇特的线条样式和填充图案。

Line Styles线条样式

Line styles are defined by the stroke attribute in the Graphics2D rendering context. 线条样式由Graphics2D渲染上下文中的笔划属性定义。To set the stroke attribute, you create a BasicStroke object and pass it into the Graphics2D setStroke method.要设置笔划属性,请创建BasicStroke对象并将其传递给Graphics2D setStroke方法。

A BasicStroke object holds information about the line width, join style, end-cap style, and dash style. BasicStroke对象包含有关线宽、连接样式、端盖样式和短划线样式的信息。This information is used when a Shape is rendered with the draw method.使用draw方法渲染Shape时使用此信息。

The line width is the thickness of the line measured perpendicular to its trajectory. 线宽是垂直于其轨迹测量的线的厚度。The line width is specified as a float value in user coordinate units, which are roughly equivalent to 1/72 of an inch when the default transform is used.线宽以用户坐标单位指定为float值,在使用默认变换时,该单位大致相当于1/72英寸。

The join style is the decoration that is applied where two line segments meet. 连接样式是应用于两条线段相交处的装饰。BasicStroke supports the following three join styles:BasicStroke支持以下三种连接样式:

Join bevel stroke styleJOIN_BEVEL

Join miter stroke styleJOIN_MITER

Join round stroke style JOIN_ROUND

The end-cap style is the decoration that is applied where a line segment ends. 端盖样式是应用于线段结束处的装饰。BasicStroke supports the following three end-cap styles:BasicStroke支持以下三种端盖样式:

Butt end-cap style CAP_BUTT

Round end-cap style CAP_ROUND

Square end-cap style CAP_SQUARE

The dash style defines the pattern of opaque and transparent sections applied along the length of the line. 虚线样式定义沿直线长度应用的不透明和透明截面的图案。The dash style is defined by a dash array and a dash phase. 破折号样式由破折号阵列和破折号阶段定义。The dash array defines the dash pattern. 虚线数组定义破折号图案。Alternating elements in the array represent the dash length and the length of the space between dashes in user coordinate units. 数组中的交替元素以用户坐标单位表示虚线长度和虚线之间的空间长度。Element 0 represents the first dash, element 1 the first space, and so on. 元素0表示第一个破折号,元素1表示第一个空格,依此类推。The dash phase is an offset into the dash pattern, also specified in user coordinate units. 虚线相位是虚线图案的偏移,也以用户坐标单位指定。The dash phase indicates what part of the dash pattern is applied to the beginning of the line.破折号阶段指示破折号图案的哪一部分应用于线的开头。

Fill Patterns填充图案

Fill patterns are defined by the paint attribute in the Graphics2D rendering context. 填充图案由Graphics2D渲染上下文中的涂色属性定义。To set the paint attribute, you create an instance of an object that implements the Paint interface and pass it into the Graphics2D setPaint method.要设置绘制属性,请创建实现Paint接口的对象实例,并将其传递到Graphics2D setPaint方法中。

The following three classes implement the Paint interface: Color, GradientPaint, and TexturePaint.以下三个类实现了Paint接口:ColorGradientPaintTexturePaint

To create a GradientPaint, you specify a beginning position and color and an ending position and color. 要创建GradientPaint,请指定开始位置和颜色以及结束位置和颜色。The gradient changes proportionally from one color to the other color along the line connecting the two positions. 渐变沿连接两个位置的线从一种颜色成比例地变化到另一种颜色。For example:例如:

Gradient filling

The pattern for a TexturePaint class is defined by a BufferedImage class. TexturePaint类的图案由BuffereImage类定义。To create a TexturePaint object, you specify the image that contains the pattern and a rectangle that is used to replicate and anchor the pattern. 要创建TexturePaint对象,请指定包含图案的图像和用于复制和定位图案的矩形。The following image represents this feature:下图表示此功能:

Using a texture to fill a rectangle

Previous page: Drawing Arbitrary Shapes
Next page: Working with Text APIs