Working with Hatch Patterns in PS file | Java

Add Hatch Pattern in PS Document

Hatch pattern is a texture tiling pattern usually represented by small 2-colors (usually black&white) simple image. The main content of these small images is various hatches.

The Java platform doesn’t have a separate class for painting shapes and text with hatches. However, Aspose.Page for Java library offers com.aspose.eps.HatchPaintLibrary class that gives a possibilty to create java.awt.TexturePaint filled with hatches defined by one of 55 styles offered by com.aspose.eps.HatchStyle.

In order to paint graphics objects with a hatch pattern in Aspose.Page for Java library it is necessary to create hatch java.awt.TexturePaint with assigned hatch style, pass it into setPaint() or one of the fillText() or fillAndStrokeText() methods that accept java.awt.Paint as a parameter.
In order to outline graphics objects with a hatch pattern in Aspose.Page for Java library someone should set hatch pattern as current paint in PsDocument, create new java.awt.BasicStroke and pass it to setStroke() or one of the outlineText() or fillAndStrokeText() methods that accept java.awt.Stroke as a parameter.

In the example below we demonstrate, firstly, how to fill a shape with a hatch pattern, then all variety of hatch styles in Java, and, finally, how to fill and outline a text with a hatch pattern.

An algorithm for painting graphics objects with a hatch pattern in a new PS document includes the following steps:

  1. Create an output stream for the resulting PS file.
  2. Create PsSaveOptions.
  3. Create PsDocument with the already created output stream and save options.
  4. Create the shape or font in dependence on what object we are going to fill or outline.
  5. Create with com.aspose.eps.HatchPaintLibrary an object of java.awt.TexturePaint with wishful style.
  6. Set the hatch paint as current paint in PsDocument
  7. Fill the shape with current paint or fill a text. If we use one of the methods for filling a text that accepts java.awt.Paint as a parameter, the previous point can be ignored.
  8. Close the page.
  9. Save the document.

If we need stroking (outlining) graphics objects with a hatch pattern instead of the last 4 points following will be:

     6. Set the hatch paint as a current paint in PsDocument.
     7. Create java.awt.BasicStroke object.
     8. Set this stroke as current stroke in PsDocument.
     9. Outline the shape with current paint and stroke or outline the text. If we use one of the methods for outlining the text that accepts java.awt.Stroke as a parameter, previous point can be ignored.
     10. Close the page.
     11. Save the document.

 1// The path to the documents directory.
 2String dataDir = Utils.getDataDir();
 3
 4//Create output stream for PostScript document
 5FileOutputStream outPsStream = new FileOutputStream(dataDir + "AddHatchPattern_outPS.ps");
 6//Create save options with A4 size
 7PsSaveOptions options = new PsSaveOptions();
 8
 9//Create new PS Document with the page opened
10PsDocument document = new PsDocument(outPsStream, options, false);
11
12int x0 = 20;
13int y0 = 100;
14int squareSide = 32;
15int width = 500;
16int sumX = 0;
17
18//Restore graphics state
19document.writeGraphicsSave();
20
21//Translate to initial point
22document.translate(x0, y0);
23
24//Create a square for every pattern
25Rectangle2D.Float square = new Rectangle2D.Float(0, 0, squareSide, squareSide);
26
27//Create pen for outlining pattern square
28BasicStroke stroke = new BasicStroke(2);
29
30HatchStyle [] hatchStyles = HatchStyle.values();
31
32//For every hatch pattern style 
33for (int i = 0; i < hatchStyles.length; i++) {            
34	//Create a hatch texture pattern by hatch style, foreground and background colors
35	TexturePaint paint = HatchPaintLibrary.getHatchTexturePaint(hatchStyles[i], Color.BLACK, Color.WHITE);
36	//Set paint with the current hatch pattern
37    document.setPaint(paint);
38
39    //Calculate a displacement in order to don't go beyond the page bounds
40    int x = squareSide;
41    int y = 0;
42    if (sumX >= width) {
43        x = -(sumX - squareSide);
44        y += squareSide;
45    }
46
47    //Translate current graphics state
48    document.translate(x, y);
49    //Fill pattern square
50    document.fill(square);
51
52    //Set current paint
53    document.setPaint(Color.BLACK);
54    //Set current stroke
55    document.setStroke(stroke);
56    //Draw square outline
57    document.draw(square);
58
59    //Calculate distance from X0
60    if (sumX >= width)
61        sumX = squareSide;
62    else
63        sumX += x;
64}
65
66//Restore graphics state
67document.writeGraphicsRestore();
68
69//Fill a text with the hatch pattern
70TexturePaint paint = HatchPaintLibrary.getHatchTexturePaint(HatchStyle.DiagonalCross, Color.RED, Color.YELLOW);
71Font font = new Font("Arial", Font.BOLD, 96);
72document.fillAndStrokeText("ABC", font, 200, 400, paint, Color.BLACK, stroke);
73
74//Outline the text with the hatch pattern
75paint = HatchPaintLibrary.getHatchTexturePaint(HatchStyle.Percent70, Color.BLUE, Color.WHITE);
76document.outlineText("ABC", font, 200, 600, paint, new BasicStroke(5));
77
78//Close current page
79document.closePage();
80//Save the document
81document.save();

See working with a hatch pattern in PS document in .NET and C++.


The result of running this code is appeared as

Add Hatch Pattern

You can download examples and data files from GitHub.

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.