From 6d90609913f3f7010b583cc9fc5ae38150a9f319 Mon Sep 17 00:00:00 2001 From: andrewlalis Date: Sat, 1 Jul 2017 09:52:21 +0200 Subject: [PATCH] Added Image Generator and added class descriptions. --- pom.xml | 2 +- src/net/agspace/TengwarImageGenerator.java | 73 ++++++++++++++++++ .../java => }/net/agspace/Translator.java | 3 +- src/{main => }/resources/tngan.ttf | Bin src/{main => }/resources/tnganb.ttf | Bin src/{main => }/resources/tnganbi.ttf | Bin src/{main => }/resources/tngani.ttf | Bin 7 files changed, 76 insertions(+), 2 deletions(-) create mode 100644 src/net/agspace/TengwarImageGenerator.java rename src/{main/java => }/net/agspace/Translator.java (99%) rename src/{main => }/resources/tngan.ttf (100%) rename src/{main => }/resources/tnganb.ttf (100%) rename src/{main => }/resources/tnganbi.ttf (100%) rename src/{main => }/resources/tngani.ttf (100%) diff --git a/pom.xml b/pom.xml index 4a8a29b..cf2b7ae 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.github.andrewlalis TengwarTranslatorLibrary - 1.0 + 1.1 diff --git a/src/net/agspace/TengwarImageGenerator.java b/src/net/agspace/TengwarImageGenerator.java new file mode 100644 index 0000000..c7a398b --- /dev/null +++ b/src/net/agspace/TengwarImageGenerator.java @@ -0,0 +1,73 @@ +package net.agspace; + +import javax.imageio.ImageIO; +import java.awt.*; +import java.awt.geom.Rectangle2D; +import java.awt.image.BufferedImage; +import java.io.File; +import java.io.IOException; + +/** + * @author Andrew Lalis + * Class to generate images of tengwar text to fit certain criteria. + */ +public class TengwarImageGenerator { + + /** + * Generates an image of some tengwar text. The text is wrapped so that each line may not take up more than + * {@code maxWidth} pixels. + * @param tengwarText The string of tengwar text to transform into an image. + * @param maxWidth The maximum width, in pixels, that the generated image can be. + * @param fontSize The size of the font to use. + * @param bold Whether or not the font should be bold. + * @param italic Whether or not the font should be italic. + * @return An Image Object containing the drawn text. + */ + public static Image generateImage(String tengwarText, int maxWidth, int fontSize, boolean bold, boolean italic) { + BufferedImage bufferedImage = new BufferedImage(maxWidth, 1, BufferedImage.TYPE_INT_RGB); + Graphics2D graphics = (Graphics2D)bufferedImage.getGraphics(); + Color backgroundColor = new Color(54, 57, 62); + Font tengwarFont = getTengwarFont(bold, italic); + tengwarFont = tengwarFont.deriveFont(fontSize); + graphics.setFont(tengwarFont); + FontMetrics fm = graphics.getFontMetrics(); + Rectangle2D rect = fm.getStringBounds(tengwarText, graphics); + graphics.setColor(backgroundColor); + graphics.fillRect((int)rect.getX(), (int)rect.getY(), (int)rect.getWidth(), (int)rect.getHeight()); + graphics.setColor(Color.white); + graphics.drawString(tengwarText, 0, fm.getHeight()); + try { + ImageIO.write(bufferedImage, "png", new File("C:\\Users\\AndrewComputer\\Documents\\Programming\\IntelliJ_Projects")); + } catch (IOException e) { + e.printStackTrace(); + } + return (Image)bufferedImage; + } + + /** + * Returns the tngan.ttf font loaded into a Java Font. + * @param bold Whether or not font should be bold. + * @param italic Whether or not font should be italic. + * @return The Font loaded, or null. + */ + public static Font getTengwarFont(boolean bold, boolean italic){ + Font font = null; + try { + String filename = null; + if (bold && italic){ + filename = "resources/tnganbi.ttf"; + } else if (bold){ + filename = "resources/tnganb.ttf"; + } else if (italic){ + filename = "resources/tngani.ttf"; + } else { + filename = "resources/tngan.tff"; + } + font = Font.createFont(Font.TRUETYPE_FONT, TengwarImageGenerator.class.getClassLoader().getResourceAsStream(filename)); + } catch (FontFormatException | IOException e) { + e.printStackTrace(); + } + return font; + } + +} diff --git a/src/main/java/net/agspace/Translator.java b/src/net/agspace/Translator.java similarity index 99% rename from src/main/java/net/agspace/Translator.java rename to src/net/agspace/Translator.java index c69dc98..ef5189f 100644 --- a/src/main/java/net/agspace/Translator.java +++ b/src/net/agspace/Translator.java @@ -3,7 +3,8 @@ package net.agspace; import java.util.*; /** -* Created by Andrew's Computer on 23-Apr-17. +* @author Andrew Lalis + * Main class that holds static translation methods. */ public class Translator { diff --git a/src/main/resources/tngan.ttf b/src/resources/tngan.ttf similarity index 100% rename from src/main/resources/tngan.ttf rename to src/resources/tngan.ttf diff --git a/src/main/resources/tnganb.ttf b/src/resources/tnganb.ttf similarity index 100% rename from src/main/resources/tnganb.ttf rename to src/resources/tnganb.ttf diff --git a/src/main/resources/tnganbi.ttf b/src/resources/tnganbi.ttf similarity index 100% rename from src/main/resources/tnganbi.ttf rename to src/resources/tnganbi.ttf diff --git a/src/main/resources/tngani.ttf b/src/resources/tngani.ttf similarity index 100% rename from src/main/resources/tngani.ttf rename to src/resources/tngani.ttf