在 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//Create an output stream for PostScript document
 2FileOutputStream outPsStream = new FileOutputStream(dataDir + "HorizontalGradient_outPS.ps");
 3//Create save options with A4 size
 4PsSaveOptions options = new PsSaveOptions();
 5
 6//Create new PS Document with the page opened
 7PsDocument document = new PsDocument(outPsStream, options, false);
 8
 9//Create a rectangle
10Rectangle2D.Float rectangle = new Rectangle2D.Float(200, 100, 200, 100);
11
12//Create horizontal linear gradient paint. Scale components in the transform must be equal to width and heigh of the rectangle.
13//Translation components are offsets of the rectangle.
14LinearGradientPaint paint = new LinearGradientPaint(new Point2D.Float(0, 0), new Point2D.Float(200, 100),
15		new float [] {0, 1}, new Color [] {new Color(0, 0, 0, 150), new Color(40, 128, 70, 50)},
16		MultipleGradientPaint.CycleMethod.NO_CYCLE, MultipleGradientPaint.ColorSpaceType.SRGB,
17		new AffineTransform(200, 0, 0, 100, 200, 100));
18
19//Set paint
20document.setPaint(paint);
21//Fill the rectangle
22document.fill(rectangle);
23
24//Fill a text with the gradient
25Font font = new Font("Arial", Font.BOLD, 96);
26document.fillAndStrokeText("ABC", font, 200, 300, paint, Color.BLACK, new BasicStroke(2));
27
28//Stroke a text with the gradient
29document.outlineText("ABC", font, 200, 400, paint, new BasicStroke(5));
30
31//Close current page
32document.closePage();
33//Save the document
34document.save();

运行此代码的结果如下:

水平渐变

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

 1//Create an output stream for PostScript document
 2FileOutputStream outPsStream = new FileOutputStream(dataDir + "VerticalGradient_outPS.ps");
 3//Create save options with A4 size
 4PsSaveOptions options = new PsSaveOptions();
 5
 6//Create new PS Document with the page opened
 7PsDocument document = new PsDocument(outPsStream, options, false);
 8
 9//Create a rectangle
10Rectangle2D.Float rectangle = new Rectangle2D.Float(200, 100, 200, 100);
11
12//Create arrays of colors and fractions for the gradient.
13Color[] colors = { Color.RED, Color.GREEN, Color.BLUE, Color.ORANGE, new Color(85,107,47) };
14float[] fractions = { 0.0f, 0.1873f, 0.492f, 0.734f, 1.0f };
15
16//Create the gradient transform. Scale components in the transform must be equal to width and heigh of the rectangle.
17//Translation components are offsets of the rectangle.
18AffineTransform transform = new AffineTransform(200, 0, 0, 100, 200, 100);
19//Rotate the gradient on 90 degrees around an origin
20transform.rotate(90 * (Math.PI / 180));
21
22//Create vertical linear gradient paint.
23LinearGradientPaint paint = new LinearGradientPaint(new Point2D.Float(0, 0), new Point2D.Float(200, 100),
24		fractions, colors, MultipleGradientPaint.CycleMethod.NO_CYCLE, MultipleGradientPaint.ColorSpaceType.SRGB,
25		transform);
26
27//Set paint
28document.setPaint(paint);
29//Fill the rectangle
30document.fill(rectangle);
31
32//Close current page
33document.closePage();
34//Save the document
35document.save();

结果如下

垂直渐变

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

 1//Create an output stream for PostScript document
 2FileOutputStream outPsStream = new FileOutputStream(dataDir + "DiagonalGradient_outPS.ps");
 3//Create save options with A4 size
 4PsSaveOptions options = new PsSaveOptions();
 5
 6// Create new PS Document with the page opened
 7PsDocument document = new PsDocument(outPsStream, options, false);
 8
 9//Create a rectangle
10Rectangle2D.Float rectangle = new Rectangle2D.Float(200, 100, 200, 100);
11
12//Create the gradient transform. Scale components in the transform must be equal to width and heigh of the rectangle.
13//Translation components are offsets of the rectangle.
14AffineTransform transform = new AffineTransform(200, 0, 0, 100, 200, 100);
15//Rotate gradient, than scale and translate to get visible color transition in required rectangle
16transform.rotate(-45 * (Math.PI / 180));
17float hypotenuse = (float) Math.sqrt(200 * 200 + 100 * 100);
18float ratio = hypotenuse / 200;
19transform.scale(-ratio, 1);
20transform.translate(100 / transform.getScaleX(), 0);
21
22//Create diagonal linear gradient paint.
23LinearGradientPaint paint = new LinearGradientPaint(new Point2D.Float(0, 0), new Point2D.Float(200, 100),
24		new float [] {0, 1}, new Color [] {Color.RED, Color.BLUE}, MultipleGradientPaint.CycleMethod.NO_CYCLE,
25		MultipleGradientPaint.ColorSpaceType.SRGB, transform);
26
27//Set paint
28document.setPaint(paint);
29//Fill the rectangle
30document.fill(rectangle);
31
32//Close current page
33document.closePage();
34//Save the document
35document.save();

结果如下

对角渐变

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

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

结果

径向渐变 imag1

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

 1//Create an output stream for PostScript document
 2FileOutputStream outPsStream = new FileOutputStream(dataDir + "RadialGradient2_outPS.ps");
 3//Create save options with A4 size
 4PsSaveOptions options = new PsSaveOptions();
 5
 6//Create new PS Document with the page opened
 7PsDocument document = new PsDocument(outPsStream, options, false);
 8
 9//Create a circle
10Ellipse2D.Float circle = new Ellipse2D.Float(200, 100, 200, 200);
11
12//Create arrays of colors and fractions for the gradient.
13Color[] colors = { Color.WHITE, Color.WHITE, Color.BLUE };
14float[] fractions = { 0.0f, 0.2f, 1.0f };
15
16//Create horizontal linear gradient paint. Scale components in the transform must be equal to width and heigh of the rectangle.
17//Translation components are offsets of the rectangle.
18AffineTransform transform = new AffineTransform(200, 0, 0, 200, 200, 100);
19
20//Create radial gradient paint.
21RadialGradientPaint paint = new RadialGradientPaint(new Point2D.Float(64, 64), 68, new Point2D.Float(24, 24),
22		fractions, colors, MultipleGradientPaint.CycleMethod.NO_CYCLE, MultipleGradientPaint.ColorSpaceType.SRGB,
23		transform);
24
25//Set paint
26document.setPaint(paint);
27//Fill the circle
28document.fill(circle);
29
30//Close current page
31document.closePage();
32//Save the document
33document.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.