PSファイルでのグラデーション操作 | Java

PSドキュメントにグラデーションを追加する

この記事では、PSドキュメントでグラデーションを使用する方法について説明します。

グラデーションとは、ある色から別の色への滑らかな変化です。描画した画像をよりリアルに見せるために使用されます。 グラデーションはペイントの一種であるため、Javaではjava.awt.Paintのサブクラスとして実装されるのが一般的です。実際には、Javaプラットフォームには2つのペイントがあります。

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.LinearGradientPaint、および java.awt.RadialGradientPaint です。 Javaのストロークカラーは、java.awt.Strokeオブジェクトのストロークプロパティとは別に割り当てられ、java.awt.Paintが再び使用されます。そのため、Aspose.Page for Javaライブラリは、線の描画や図形やテキストのアウトラインにもペイントの実装をすべて使用できます。

Aspose.Page for Java ライブラリでグラフィック オブジェクトをグラデーションで ペイント するには、java.awt.LinearGradientPaint または java.awt.RadialGradientPaint を作成し、それを setPaint() または、java.awt.Paint をパラメーターとして受け取る fillText()fillAndStrokeText() メソッドのいずれかに渡す必要があります。

Aspose.Page for Java ライブラリでグラフィック オブジェクトをグラデーションで アウトライン するには、java.awt.LinearGradientPaint または java.awt.RadialGradientPaint を、ストローク ペイントをパラメーターとして受け取る setPaint() または outlineText()fillAndStrokeText() メソッドのいずれかに渡す必要があります。

以下の例では、図形とテキストを塗りつぶし、テキストにグラデーションのアウトラインを設定する方法を示します。

新しいPSドキュメントでグラフィックオブジェクトにグラデーションを適用するアルゴリズムは、以下の手順で構成されます。

  1. 結果のPSファイル用の出力ストリームを作成します。
  2. PsSaveOptions を作成します。
  3. 既に作成した出力ストリームと保存オプションを使用して、 PsDocument を作成します。
  4. 塗りつぶしまたはアウトラインを設定するオブジェクトに応じて、必要なグラフィックパスまたはフォントを作成します。
  5. 希望するグラデーションの形状に応じて、java.awt.LinearGradientPaint または java.awt.RadialGradientPaint オブジェクトを作成します。
  6. このブラシに必要な変形を設定します。
  7. グラデーションブラシをPsDocumentの現在のペイントとして設定します。
  8. グラフィックパスを現在のペイントで塗りつぶすか、テキストで塗りつぶします。java.awt.Paint をパラメータとして受け入れるテキスト塗りつぶしメソッドのいずれかを使用する場合は、前の手順は無視できます。
  9. ページを閉じます。
  10. ドキュメントを保存します。

最後の4点の代わりにグラデーションでグラフィックオブジェクトを*ストローク(アウトライン)*する必要がある場合は、以下の手順に従います。

  1. PsDocument でグラデーションを現在のペイントとして設定します。
  2. java.awt.Stroke オブジェクトを作成します。
  3. このストロークを PsDocument で現在のストロークとして設定します。
  4. 現在のストロークでグラフィックパスをアウトラインするか、テキストをアウトラインします。java.awt.Stroke をパラメータとして受け入れるテキストアウトライン化メソッドのいずれかを使用する場合、前の点は無視されます。
  5. ページを閉じます。
  6. ドキュメントを保存します。

異なるグラデーションの使い方を示す5つのコードスニペットをご用意しました。

このコードスニペットでは、2色で水平方向の線形グラデーションを作成し、四角形を塗りつぶし、テキストを塗りつぶし、このグラデーションでテキストのアウトラインを作成します。

 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();
結果はこちら

垂直グラデーション

このコードスニペットでは、2色で斜めの線形グラデーションを作成し、このグラデーションで四角形を塗りつぶします。

 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();
結果はこちら

対角線グラデーション

このコードスニペットでは、2色から放射状グラデーションを作成し、このグラデーションで円を塗りつぶします。

 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();

結果

放射状グラデーション画像1

このコードスニペットでは、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.