651.md 4.7 KB
Newer Older
W
init  
wizardforcel 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
# 抚摸和填充图形基元

> 原文: [https://docs.oracle.com/javase/tutorial/2d/geometry/strokeandfill.html](https://docs.oracle.com/javase/tutorial/2d/geometry/strokeandfill.html)

您已经知道如何创建不同的几何图元和更复杂的形状。本课程教授如何为图形添加一些颜色和花哨的轮廓,并表示填充和描边:

*   _ 填充 _ - 是用纯色或颜色渐变或纹理图案绘制形状内部的过程
*   _ 抚摸 _ - 是一个绘制形状轮廓的过程,应用笔触宽度,线条样式和颜色属性

要将奇特线条样式和填充图案应用于几何图元,请在渲染之前更改 [`Graphics2D`](https://docs.oracle.com/javase/8/docs/api/java/awt/Graphics2D.html) 上下文中的笔触和绘制属性。例如,通过创建适当的 [`Stroke`](https://docs.oracle.com/javase/8/docs/api/java/awt/Stroke.html) 对象绘制虚线。要在渲染行之前将此笔划添加到`Graphics2D`上下文,请调用`setStroke`方法。类似地,通过创建`GradientPaint`对象并将其添加到`Graphics2D`上下文,将渐变填充应用于`Shape`对象。

以下代码行使用填充和描边上下文来丰富几何图元:

W
wizardforcel 已提交
14
```java
W
init  
wizardforcel 已提交
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
// 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](img/4ad6995d4cd9a952f1607f9eead72907.jpg)

W
wizardforcel 已提交
33
```java
W
init  
wizardforcel 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
// 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](img/48798a19dd87ecb122bfdf7e494e65d1.jpg)

[`ShapesDemo2D.java`](examples/ShapesDemo2D.java) 代码示例表示 stoking 和 fill 的其他实现。

## 定义花式线条样式和填充图案

使用 Java 2D `Stroke``Paint`类,您可以定义花式线条样式和填充图案。

### 线条样式

线条样式由`Graphics2D`渲染上下文中的笔触属性定义。要设置笔触属性,请创建`BasicStroke`对象并将其传递给`Graphics2D` `setStroke`方法。

`BasicStroke`对象包含有关线宽,连接样式,端盖样式和破折号样式的信息。使用`draw`方法渲染`Shape`时使用此信息。

W
wizardforcel 已提交
55
*线宽*是垂直于其轨迹测量的线的粗细。线宽指定为用户坐标单位的`float`值,当使用默认变换时,大致相当于 1/72 英寸。
W
init  
wizardforcel 已提交
56

W
wizardforcel 已提交
57
*连接样式*是在两个线段相交的地方应用的装饰。 `BasicStroke`支持以下三种连接样式:
W
init  
wizardforcel 已提交
58 59 60 61 62 63 64

![Join bevel stroke style](img/00820c62bd27f5624c9cae9551ac10e8.jpg) `JOIN_BEVEL`

![Join miter stroke style](img/7088cb288a873ab5a0aa575fc13f9f47.jpg) `JOIN_MITER`

![Join round stroke style](img/2b3970c32cd6fc4a898699e974832927.jpg) `JOIN_ROUND`

W
wizardforcel 已提交
65
*端盖样式*是在线段结束时应用的装饰。 `BasicStroke`支持以下三种端盖样式:
W
init  
wizardforcel 已提交
66 67 68 69 70 71 72

![Butt end-cap style](img/eee5947182e6d5b02b6f449fcd61ae01.jpg) `CAP_BUTT`

![Round end-cap style](img/88c0e509cfe1c912bfd1bb6380e5df06.jpg) `CAP_ROUND`

![Square end-cap style](img/6277f8f7f82c72f4eb2e5e2ae4eca22b.jpg) `CAP_SQUARE`

W
wizardforcel 已提交
73
*破折号样式*定义沿线长度应用的不透明和透明部分的图案。破折号样式由破折号数组和破折号阶段定义。*破折号数组*定义破折号模式。数组中的交替元素表示短划线长度和用户坐标单位中虚线之间的空格长度。元素 0 表示第一个破折号,元素 1 表示第一个破折号,依此类推。*破折号阶段*是破折号模式的偏移量,也以用户坐标单位指定。破折号阶段指示破折号模式的哪个部分应用于行的开头。
W
init  
wizardforcel 已提交
74 75 76 77 78 79 80 81 82

### 填充图案

填充图案由`Graphics2D`渲染上下文中的 paint 属性定义。要设置 paint 属性,可以创建实现`Paint`接口的对象实例,并将其传递给`Graphics2D` `setPaint`方法。

以下三个类实现`Paint`接口:`Color``GradientPaint``TexturePaint`

要创建`GradientPaint`,请指定起始位置和颜色以及结束位置和颜色。梯度沿着连接两个位置的线从一种颜色到另一种颜色成比例地变化。例如:

W
wizardforcel 已提交
83
![Gradient filling](img/156e27ca8b2f2d9ed9730e41e548101c.jpg)
W
init  
wizardforcel 已提交
84 85 86

`TexturePaint`类的模式由`BufferedImage`类定义。要创建`TexturePaint`对象,请指定包含图案的图像以及用于复制和锚定图案的矩形。以下图像代表此功能:

W
wizardforcel 已提交
87
![Using a texture to fill a rectangle](img/ff586462372ec8b435991f61edf9b928.jpg)