Clean up some code, add comments. Tiny stuff only.
This commit is contained in:
		
							parent
							
								
									c17f73f9f6
								
							
						
					
					
						commit
						41330de4f4
					
				
					 5 changed files with 38 additions and 49 deletions
				
			
		|  | @ -36,16 +36,16 @@ class Generator { | ||||||
|         final PDDocumentInformation info = new PDDocumentInformation(); |         final PDDocumentInformation info = new PDDocumentInformation(); | ||||||
|         info.setTitle("Labyrinth " + this.labyrinth.getWidth() + "x" + this.labyrinth.getHeight() + ", ID " + this.labyrinth.getRandomSeed()); |         info.setTitle("Labyrinth " + this.labyrinth.getWidth() + "x" + this.labyrinth.getHeight() + ", ID " + this.labyrinth.getRandomSeed()); | ||||||
|         pdDocument.setDocumentInformation(info); |         pdDocument.setDocumentInformation(info); | ||||||
|         final PDPage page = new PDPage(new PDRectangle(pageWidth, pageHeight)); |         final PDPage puzzlePage = new PDPage(new PDRectangle(pageWidth, pageHeight)); | ||||||
|         final PDPage solution = new PDPage(new PDRectangle(pageWidth, pageHeight)); |         final PDPage solutionPage = new PDPage(new PDRectangle(pageWidth, pageHeight)); | ||||||
|         pdDocument.addPage(page); |         pdDocument.addPage(puzzlePage); | ||||||
|         pdDocument.addPage(solution); |         pdDocument.addPage(solutionPage); | ||||||
|         try (final PDPageContentStream labyrinthPageContentStream = new PDPageContentStream(pdDocument, page); |         try (final PDPageContentStream puzzlePageContentStream = new PDPageContentStream(pdDocument, puzzlePage); | ||||||
|              final PDPageContentStream solutionPageContentStream = new PDPageContentStream(pdDocument, solution)) { |              final PDPageContentStream solutionPageContentStream = new PDPageContentStream(pdDocument, solutionPage)) { | ||||||
|             setUpPageContentStream(labyrinthPageContentStream); |             setUpPageContentStream(puzzlePageContentStream); | ||||||
|             setUpPageContentStream(solutionPageContentStream); |             setUpPageContentStream(solutionPageContentStream); | ||||||
|             this.drawHorizonzalLines(labyrinthPageContentStream, solutionPageContentStream); |             this.drawHorizontalLines(puzzlePageContentStream, solutionPageContentStream); | ||||||
|             this.drawVerticalLines(labyrinthPageContentStream, solutionPageContentStream); |             this.drawVerticalLines(puzzlePageContentStream, solutionPageContentStream); | ||||||
|             this.drawSolution(solutionPageContentStream); |             this.drawSolution(solutionPageContentStream); | ||||||
|         } catch (IOException e) { |         } catch (IOException e) { | ||||||
|             e.printStackTrace(); |             e.printStackTrace(); | ||||||
|  | @ -68,9 +68,10 @@ class Generator { | ||||||
|         pageContentStream.setNonStrokingColor(Color.BLACK); |         pageContentStream.setNonStrokingColor(Color.BLACK); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     private void drawHorizonzalLines(@NonNull final PDPageContentStream... contentStreams) throws IOException { |     private void drawHorizontalLines(@NonNull final PDPageContentStream... contentStreams) throws IOException { | ||||||
|         // PDF has the origin in the lower left corner. We want it in the upper left corner, hence some magic is required. |         // PDF has the origin in the lower left corner. We want it in the upper left corner, hence some magic is required. | ||||||
|         Coordinate coordinate = new Coordinate(0f, 0f); |         Coordinate coordinate = new Coordinate(0f, 0f); | ||||||
|  |         // Draw the TOP borders of all tiles. | ||||||
|         for (int y = 0; y < this.labyrinth.getHeight(); y++) { |         for (int y = 0; y < this.labyrinth.getHeight(); y++) { | ||||||
|             boolean isPainting = false; |             boolean isPainting = false; | ||||||
|             coordinate = coordinate.withY(y); |             coordinate = coordinate.withY(y); | ||||||
|  | @ -102,6 +103,7 @@ class Generator { | ||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|  |         // Draw the BOTTOM border of the last row of tiles. | ||||||
|         boolean isPainting = false; |         boolean isPainting = false; | ||||||
|         int y = this.labyrinth.getHeight(); |         int y = this.labyrinth.getHeight(); | ||||||
|         coordinate = coordinate.withY(this.labyrinth.getHeight()); |         coordinate = coordinate.withY(this.labyrinth.getHeight()); | ||||||
|  | @ -137,6 +139,7 @@ class Generator { | ||||||
|     private void drawVerticalLines(@NonNull final PDPageContentStream... contentStreams) throws IOException { |     private void drawVerticalLines(@NonNull final PDPageContentStream... contentStreams) throws IOException { | ||||||
|         // PDF has the origin in the lower left corner. We want it in the upper left corner, hence some magic is required. |         // PDF has the origin in the lower left corner. We want it in the upper left corner, hence some magic is required. | ||||||
|         Coordinate coordinate = new Coordinate(0f, 0f); |         Coordinate coordinate = new Coordinate(0f, 0f); | ||||||
|  |         // Draw the LEFT borders of all tiles. | ||||||
|         for (int x = 0; x < this.labyrinth.getWidth(); x++) { |         for (int x = 0; x < this.labyrinth.getWidth(); x++) { | ||||||
|             boolean isPainting = false; |             boolean isPainting = false; | ||||||
|             coordinate = coordinate.withX(x); |             coordinate = coordinate.withX(x); | ||||||
|  | @ -168,6 +171,7 @@ class Generator { | ||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|  |         // Draw the RIGHT border of the last column of tiles. | ||||||
|         boolean isPainting = false; |         boolean isPainting = false; | ||||||
|         int x = this.labyrinth.getWidth(); |         int x = this.labyrinth.getWidth(); | ||||||
|         coordinate = coordinate.withX(this.labyrinth.getWidth()); |         coordinate = coordinate.withX(this.labyrinth.getWidth()); | ||||||
|  |  | ||||||
|  | @ -9,7 +9,6 @@ public class PDFRenderer implements Renderer<byte[]> { | ||||||
|     static final float SCALE = 10; |     static final float SCALE = 10; | ||||||
| 
 | 
 | ||||||
|     private PDFRenderer() { |     private PDFRenderer() { | ||||||
| 
 |  | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     @NonNull |     @NonNull | ||||||
|  | @ -23,6 +22,4 @@ public class PDFRenderer implements Renderer<byte[]> { | ||||||
|         final Generator generator = new Generator(labyrinth); |         final Generator generator = new Generator(labyrinth); | ||||||
|         return generator.generate(); |         return generator.generate(); | ||||||
|     } |     } | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -3,24 +3,30 @@ package ch.fritteli.labyrinth.generator.renderer.pdffile; | ||||||
| import ch.fritteli.labyrinth.generator.model.Labyrinth; | import ch.fritteli.labyrinth.generator.model.Labyrinth; | ||||||
| import ch.fritteli.labyrinth.generator.renderer.Renderer; | import ch.fritteli.labyrinth.generator.renderer.Renderer; | ||||||
| import ch.fritteli.labyrinth.generator.renderer.pdf.PDFRenderer; | import ch.fritteli.labyrinth.generator.renderer.pdf.PDFRenderer; | ||||||
|  | import io.vavr.control.Option; | ||||||
|  | import io.vavr.control.Try; | ||||||
| import lombok.NonNull; | import lombok.NonNull; | ||||||
| 
 | 
 | ||||||
|  | import java.io.File; | ||||||
| import java.io.IOException; | import java.io.IOException; | ||||||
| import java.nio.file.Files; | import java.nio.file.Files; | ||||||
| import java.nio.file.Path; | import java.nio.file.Path; | ||||||
|  | import java.util.NoSuchElementException; | ||||||
| 
 | 
 | ||||||
| public class PDFFileRenderer implements Renderer<Path> { | public class PDFFileRenderer implements Renderer<Path> { | ||||||
|     @NonNull |     @NonNull | ||||||
|     private static final PDFRenderer PDF_RENDERER = PDFRenderer.newInstance(); |     private static final PDFRenderer PDF_RENDERER = PDFRenderer.newInstance(); | ||||||
|     private Path targetFile; |     @NonNull | ||||||
|  |     private Option<Path> targetFile; | ||||||
| 
 | 
 | ||||||
|     private PDFFileRenderer() { |     private PDFFileRenderer() { | ||||||
|         try { |         this.targetFile = Try | ||||||
|             this.targetFile = Files.createTempFile("labyrinth_", ".pdf"); |                 .of(() -> Files.createTempFile("labyrinth_", ".pdf")) | ||||||
|         } catch (IOException e) { |                 .onFailure(ex -> { | ||||||
|                     System.err.println("Unable to set default target file."); |                     System.err.println("Unable to set default target file."); | ||||||
|             e.printStackTrace(); |                     ex.printStackTrace(); | ||||||
|         } |                 }) | ||||||
|  |                 .toOption(); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     @NonNull |     @NonNull | ||||||
|  | @ -29,12 +35,15 @@ public class PDFFileRenderer implements Renderer<Path> { | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     public boolean isTargetFileDefinedAndWritable() { |     public boolean isTargetFileDefinedAndWritable() { | ||||||
|         return this.targetFile != null && this.targetFile.toFile().canWrite(); |         return this.targetFile | ||||||
|  |                 .map(Path::toFile) | ||||||
|  |                 .map(File::canWrite) | ||||||
|  |                 .getOrElse(false); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     @NonNull |     @NonNull | ||||||
|     public PDFFileRenderer setTargetFile(@NonNull final Path targetFile) { |     public PDFFileRenderer setTargetFile(@NonNull final Path targetFile) { | ||||||
|         this.targetFile = targetFile; |         this.targetFile = Option.of(targetFile); | ||||||
|         return this; |         return this; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  | @ -42,18 +51,19 @@ public class PDFFileRenderer implements Renderer<Path> { | ||||||
|     public Path render(@NonNull final Labyrinth labyrinth) { |     public Path render(@NonNull final Labyrinth labyrinth) { | ||||||
|         if (!this.isTargetFileDefinedAndWritable()) { |         if (!this.isTargetFileDefinedAndWritable()) { | ||||||
|             try { |             try { | ||||||
|                 Files.createFile(this.targetFile); |                 Files.createFile(this.targetFile.get()); | ||||||
|             } catch (IOException e) { |             } catch (IOException | NoSuchElementException e) { | ||||||
|                 throw new IllegalArgumentException("Cannot write to target file.", e); |                 throw new IllegalArgumentException("Cannot write to target file.", e); | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|         final byte[] bytes = PDF_RENDERER.render(labyrinth); |         final byte[] bytes = PDF_RENDERER.render(labyrinth); | ||||||
|  |         final Path targetFile = this.targetFile.get(); | ||||||
|         try { |         try { | ||||||
|             Files.write(this.targetFile, bytes); |             Files.write(targetFile, bytes); | ||||||
|         } catch (IOException e) { |         } catch (IOException e) { | ||||||
|             System.err.println("Failed writing to file " + this.targetFile.normalize().toString()); |             System.err.println("Failed writing to file " + targetFile.normalize().toString()); | ||||||
|             e.printStackTrace(); |             e.printStackTrace(); | ||||||
|         } |         } | ||||||
|         return this.targetFile; |         return targetFile; | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -1,11 +0,0 @@ | ||||||
| <?xml version="1.0" encoding="UTF-8"?> |  | ||||||
| <module type="JAVA_MODULE" version="4"> |  | ||||||
|   <component name="NewModuleRootManager" inherit-compiler-output="true"> |  | ||||||
|     <exclude-output /> |  | ||||||
|     <content url="file://$MODULE_DIR$"> |  | ||||||
|       <sourceFolder url="file://$MODULE_DIR$/java" isTestSource="false" /> |  | ||||||
|     </content> |  | ||||||
|     <orderEntry type="inheritedJdk" /> |  | ||||||
|     <orderEntry type="sourceFolder" forTests="false" /> |  | ||||||
|   </component> |  | ||||||
| </module> |  | ||||||
|  | @ -1,11 +0,0 @@ | ||||||
| <?xml version="1.0" encoding="UTF-8"?> |  | ||||||
| <module type="JAVA_MODULE" version="4"> |  | ||||||
|   <component name="NewModuleRootManager" inherit-compiler-output="true"> |  | ||||||
|     <exclude-output /> |  | ||||||
|     <content url="file://$MODULE_DIR$"> |  | ||||||
|       <sourceFolder url="file://$MODULE_DIR$/java" isTestSource="true" /> |  | ||||||
|     </content> |  | ||||||
|     <orderEntry type="inheritedJdk" /> |  | ||||||
|     <orderEntry type="sourceFolder" forTests="false" /> |  | ||||||
|   </component> |  | ||||||
| </module> |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue