Merge pull request #5 from andrewlalis/development

Added image generator to create file.
This commit is contained in:
Andrew Lalis 2017-07-02 01:41:01 +02:00 committed by GitHub
commit af8fe4e70e
5 changed files with 30 additions and 20 deletions

View File

@ -2,7 +2,6 @@ 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;
@ -23,47 +22,58 @@ public class TengwarImageGenerator {
* @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);
public static File generateImage(String tengwarText, int maxWidth, float fontSize, boolean bold, boolean italic, String filepath) {
BufferedImage bufferedImage = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = bufferedImage.createGraphics();
Font tengwarFont = getTengwarFont(bold, italic, 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());
int width = fm.stringWidth(tengwarText);
int height = fm.getHeight();
graphics.dispose();
bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
graphics = bufferedImage.createGraphics();
graphics.setBackground(new Color(54, 57, 62));
graphics.clearRect(0, 0, width, height);
graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
graphics.setColor(Color.white);
graphics.drawString(tengwarText, 0, fm.getHeight());
graphics.setFont(tengwarFont);
graphics.drawString(tengwarText, 0, fm.getAscent());
graphics.dispose();
try {
ImageIO.write(bufferedImage, "png", new File("C:\\Users\\AndrewComputer\\Documents\\Programming\\IntelliJ_Projects"));
File file = new File(filepath);
ImageIO.write(bufferedImage, "png", file);
return file;
} catch (IOException e) {
e.printStackTrace();
}
return (Image)bufferedImage;
return null;
}
/**
* 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.
* @param size The font size, in pt.
* @return The Font loaded, or null.
*/
public static Font getTengwarFont(boolean bold, boolean italic){
public static Font getTengwarFont(boolean bold, boolean italic, float size){
Font font = null;
try {
String filename = null;
String filename;
if (bold && italic){
filename = "resources/tnganbi.ttf";
filename = "tnganbi.ttf";
} else if (bold){
filename = "resources/tnganb.ttf";
filename = "tnganb.ttf";
} else if (italic){
filename = "resources/tngani.ttf";
filename = "tngani.ttf";
} else {
filename = "resources/tngan.tff";
filename = "tngan.ttf";
}
font = Font.createFont(Font.TRUETYPE_FONT, TengwarImageGenerator.class.getClassLoader().getResourceAsStream(filename));
font = Font.createFont(Font.TRUETYPE_FONT, TengwarImageGenerator.class.getClassLoader().getResourceAsStream(filename)).deriveFont(size);
} catch (FontFormatException | IOException e) {
e.printStackTrace();
}