在 PS 文件中使用渐变 | Java

Contents
[ Hide Show ]

在 PS 文档中添加渐变

本文将探讨如何在 PS 文档中使用渐变。

渐变是指颜色从一种颜色平滑过渡到另一种颜色,用于使绘制的图像更加逼真。 由于渐变是一种绘画,因此在 Java 中,它被实现为 java.awt.Paint 的子类。实际上,Java 平台有两种这样的绘画:

此外还有 java.awt.GradientPaint,但它只是 java.awt.LinearGradientPaint 的一个特例。

为了在 PsDocument 中设置绘画或描边,我们必须将 java.awt.Paint 类的对象(用于绘画)和 java.awt.Stroke 类的对象(用于描边)分别传入相应的方法。 Aspose.Page for Java 库处理 Java 平台提供的所有重要的 java.awt.Paint 实现类。这些类包括 java.awt.Colorjava.awt.TexturePaintjava.awt.LinearGradientPaintjava.awt.RadialGradientPaint。在 Java 中,描边颜色与 java.awt.Stroke 对象中的描边属性分开指定,并再次使用 java.awt.Paint。因此,Aspose.Page for Java 库也可以使用一整套绘画实现来绘制线条以及勾勒形状和文本的轮廓。

为了在 Aspose.Page for Java 库中使用渐变色绘制图形对象,需要创建 java.awt.LinearGradientPaintjava.awt.RadialGradientPaint 并将其传递给 setPaint()fillText()fillAndStrokeText() 方法(这些方法接受 java.awt.Paint 作为参数)。

为了在 Aspose.Page for Java 库中使用渐变色勾勒图形对象轮廓,还应将 java.awt.LinearGradientPaintjava.awt.RadialGradientPaint 传递给 setPaint()outlineText()fillAndStrokeText() 方法(这些方法接受描边绘制作为参数)。

在下面的示例中,我们演示了如何填充形状和文本,并使用渐变色勾勒文本轮廓。

在新的 PS 文档中使用渐变绘制图形对象的算法包括以下步骤:

  1. 为生成的 PS 文件创建输出流。
  2. 创建 PsSaveOptions
  3. 使用已创建的输出流和保存选项创建 PsDocument
  4. 根据要填充或勾勒轮廓的对象,创建必要的图形路径或字体。
  5. 根据所需的渐变形式,创建 java.awt.LinearGradientPaintjava.awt.RadialGradientPaint 对象。
  6. 为该画笔设置必要的变换。
  7. 将渐变画笔设置为 PsDocument 中的当前画笔。
  8. 使用当前画笔填充图形路径或填充文本。如果我们使用接受 java.awt.Paint 作为参数的文本填充方法,则可以忽略上一个点。
  9. 关闭页面。
  10. 保存文档。

如果我们需要使用渐变来描边(勾勒轮廓)图形对象,而不是最后四个点,则如下所示:

  1. 在 PsDocument 中将渐变设置为当前绘制。
  2. 创建 java.awt.Stroke 对象。
  3. 在 PsDocument 中将此描边设置为当前描边。
  4. 使用当前描边勾勒图形路径或勾勒文本轮廓。如果我们使用接受 java.awt.Stroke 作为参数的勾勒文本轮廓的方法,则可以忽略上一步。
  5. 关闭页面。
  6. 保存文档。

我们提供了 5 个独立的代码片段,演示了不同渐变的用法。

在此代码片段中,我们用两种颜色创建水平线性渐变,填充矩形,填充文本,并使用此渐变勾勒文本轮廓。

 1// Paint rectangle and text and draw text with horizontal gradient fill in PS document.
 2
 3String outputFileName = "HorizontalGradient_outPS.ps";
 4
 5//Create save options with A4 size
 6PsSaveOptions options = new PsSaveOptions();
 7
 8// Create new 1-paged PS Document
 9PsDocument document = new PsDocument(getOutputDir() + outputFileName, options, false);
10
11float offsetX = 200;
12float offsetY = 100;
13float width = 200;
14float height = 100;
15
16//Create graphics path from the first rectangle
17java.awt.geom.GeneralPath path = new java.awt.geom.GeneralPath();
18path.append(new Rectangle2D.Float(offsetX, offsetY, width, height), false);
19
20//Create linear gradient brush with rectangle as a bounds, start and end colors and transformation matrix
21LinearGradientPaint paint = new LinearGradientPaint(new Point2D.Float(0, 0), new Point2D.Float(200, 100),
22		new float [] {0, 1}, new Color [] {new Color(0, 0, 0, 150), new Color(40, 128, 70, 50)},
23		MultipleGradientPaint.CycleMethod.NO_CYCLE, MultipleGradientPaint.ColorSpaceType.SRGB,
24		new AffineTransform(width, 0, 0, height, offsetX, offsetY));
25
26//Set paint
27document.setPaint(paint);
28
29//Fill the rectangle
30document.fill(path);
31
32//Fill text with gradient
33java.awt.Font font = new java.awt.Font("Arial", java.awt.Font.BOLD, 96);
34document.fillAndStrokeText("ABC", font, 200, 300, paint, Color.BLACK, new java.awt.BasicStroke(2));
35
36//Set current stroke
37document.setStroke(new java.awt.BasicStroke(5));
38//Outline text with gradient
39document.outlineText("ABC", font, 200, 400);
40
41//Close current page
42document.closePage();
43
44//Save the document
45document.save();

运行此代码的结果如下:

水平渐变

在此代码片段中,我们用 5 种颜色创建了一个垂直线性渐变,并用该渐变填充了一个矩形。

 1// Paint rectangle with vertical gradient fill in PS document.
 2
 3String outputFileName = "VerticalGradient_outPS.ps";
 4
 5//Create save options with A4 size
 6PsSaveOptions options = new PsSaveOptions();
 7
 8// Create new 1-paged PS Document
 9PsDocument document = new PsDocument(getOutputDir() + outputFileName, options, false);
10
11float offsetX = 200;
12float offsetY = 100;
13float width = 200;
14float height = 100;
15
16//Create graphics path from the first rectangle
17java.awt.geom.GeneralPath path = new java.awt.geom.GeneralPath();
18path.append(new Rectangle2D.Float(offsetX, offsetY, width, height), false);
19
20//Create an array of interpolation colors
21Color[] colors = { Color.RED, Color.GREEN, Color.BLUE, Color.ORANGE, new Color(107, 142, 35) }; // DarkOliveGreen
22float[] positions = { 0.0f, 0.1873f, 0.492f, 0.734f, 1.0f };
23
24//Create the gradient transform. Scale components in the transform must be equal to width and heigh of the rectangle.
25//Translation components are offsets of the rectangle.
26AffineTransform transform = new AffineTransform(width, 0, 0, height, offsetX, offsetY);
27//Rotate the gradient on 90 degrees around an origin
28transform.rotate(Math.toRadians(90));
29
30//Create vertical linear gradient paint.
31LinearGradientPaint paint = new LinearGradientPaint(new Point2D.Float(0, 0), new Point2D.Float(200, 100),
32		positions, colors, MultipleGradientPaint.CycleMethod.NO_CYCLE, MultipleGradientPaint.ColorSpaceType.SRGB,
33		transform);
34
35//Set paint
36document.setPaint(paint);
37
38//Fill the rectangle
39document.fill(path);
40
41//Close current page
42document.closePage();
43
44//Save the document
45document.save();
结果如下

垂直渐变

在此代码片段中,我们用两种颜色创建了一个对角线性渐变,并用该渐变填充了一个矩形。

 1// Paint rectangle with diagonal gradient fill in PS document.
 2
 3String outputFileName = "DiagonalGradient_outPS.ps";
 4
 5//Create save options with A4 size
 6PsSaveOptions options = new PsSaveOptions();
 7
 8// Create new 1-paged PS Document
 9PsDocument document = new PsDocument(getOutputDir() + outputFileName, options, false);
10
11float offsetX = 200;
12float offsetY = 100;
13float width = 200;
14float height = 100;
15
16//Create graphics path from the first rectangle
17java.awt.geom.GeneralPath path = new java.awt.geom.GeneralPath();
18path.append(new Rectangle2D.Float(offsetX, offsetY, width, height), false);
19
20//Create the gradient transform. Scale components in the transform must be equal to width and heigh of the rectangle.
21//Translation components are offsets of the rectangle.
22AffineTransform transform = new AffineTransform(200, 0, 0, 100, 200, 100);
23//Rotate gradient, than scale and translate to get visible color transition in required rectangle
24transform.rotate(-45 * (Math.PI / 180));
25float hypotenuse = (float) Math.sqrt(200 * 200 + 100 * 100);
26float ratio = hypotenuse / 200;
27transform.scale(-ratio, 1);
28transform.translate(100 / transform.getScaleX(), 0);
29
30//Create diagonal linear gradient paint.
31LinearGradientPaint paint = new LinearGradientPaint(new Point2D.Float(0, 0), new Point2D.Float(200, 100),
32		new float [] {0, 1}, new Color [] {Color.RED, Color.BLUE}, MultipleGradientPaint.CycleMethod.NO_CYCLE,
33		MultipleGradientPaint.ColorSpaceType.SRGB, transform);
34
35//Set paint
36document.setPaint(paint);
37
38//Fill the rectangle
39document.fill(path);
40
41//Close current page
42document.closePage();
43
44//Save the document
45document.save();
结果如下

对角渐变

在此代码片段中,我们创建了两种颜色的径向渐变,并用该渐变填充一个圆圈。

 1// Paint rectangle with radial gradient fill in PS document.
 2
 3String outputFileName = "RadialGradient1_outPS.ps";
 4
 5//Create save options with A4 size
 6PsSaveOptions options = new PsSaveOptions();
 7
 8// Create new 1-paged PS Document
 9PsDocument document = new PsDocument(getOutputDir() + outputFileName, options, false);
10
11float offsetX = 200;
12float offsetY = 100;
13float width = 200;
14float height = 100;
15
16//Create a circle
17Ellipse2D.Float circle = new Ellipse2D.Float(offsetX, offsetY, width, height);
18
19//Create arrays of colors and fractions for the gradient.
20Color[] colors = { Color.WHITE, Color.WHITE, Color.BLUE };
21float[] fractions = { 0.0f, 0.2f, 1.0f };
22
23//Create horizontal linear gradient paint. Scale components in the transform must be equal to width and heigh of the rectangle.
24//Translation components are offsets of the rectangle.
25AffineTransform transform = new AffineTransform(width, 0, 0, height, offsetX, offsetY);
26
27//Create radial linear gradient paint.
28RadialGradientPaint paint = new RadialGradientPaint(new Point2D.Float(64, 64), 68, new Point2D.Float(24, 24),
29		fractions, colors, MultipleGradientPaint.CycleMethod.NO_CYCLE, MultipleGradientPaint.ColorSpaceType.SRGB,
30		transform);
31
32//Set paint
33document.setPaint(paint);
34
35//Fill the rectangle
36document.fill(circle);
37
38//Close current page
39document.closePage();
40
41//Save the document
42document.save();

结果

径向渐变 imag1

在此代码片段中,我们创建了一个由 6 种颜色组成的径向渐变,并用该渐变填充了一个矩形。

 1// Paint rectangle with radial gradient fill in PS document.
 2
 3String outputFileName = "RadialGradient2_outPS.ps";
 4
 5//Create save options with A4 size
 6PsSaveOptions options = new PsSaveOptions();
 7
 8// Create new 1-paged PS Document
 9PsDocument document = new PsDocument(getOutputDir() + outputFileName, options, false);
10
11float offsetX = 200;
12float offsetY = 100;
13float width = 200;
14float height = 100;
15
16//Create a rectangle
17Rectangle2D.Float rectangle = new Rectangle2D.Float(offsetX, offsetY, width, height);
18
19//Create arrays of colors and fractions for the gradient.
20Color[] colors = { Color.GREEN, Color.BLUE, Color.BLACK, Color.YELLOW, new Color(245, 245, 220), Color.RED };
21float[] fractions = { 0.0f, 0.2f, 0.3f, 0.4f, 0.9f, 1.0f };
22
23//Create horizontal linear gradient paint. Scale components in the transform must be equal to width and heigh of the rectangle.
24//Translation components are offsets of the rectangle.
25AffineTransform transform = new AffineTransform(width, 0, 0, height, offsetX, offsetY);
26
27//Create radial gradient paint.
28RadialGradientPaint paint = new RadialGradientPaint(new Point2D.Float(300, 200), 100, new Point2D.Float(300, 200),
29		fractions, colors, MultipleGradientPaint.CycleMethod.NO_CYCLE, MultipleGradientPaint.ColorSpaceType.SRGB,
30		transform);
31
32//Set paint
33document.setPaint(paint);
34
35//Fill the rectangle
36document.fill(rectangle);
37
38//Close current page
39document.closePage();
40
41//Save the document
42document.save();

结果

径向渐变图像 2

请参阅 .NET 中 PS 文档中的渐变操作。

您可以从 GitHub下载示例和数据文件。

Have any questions about Aspose.Page?



Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.