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

結果はこちら

垂直グラデーション

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

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

結果はこちら

対角線グラデーション

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

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

結果

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

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