Merge pull request #5 from andrewlalis/development
Added image generator to create file.
This commit is contained in:
commit
af8fe4e70e
|
@ -2,7 +2,6 @@ package net.agspace;
|
||||||
|
|
||||||
import javax.imageio.ImageIO;
|
import javax.imageio.ImageIO;
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
import java.awt.geom.Rectangle2D;
|
|
||||||
import java.awt.image.BufferedImage;
|
import java.awt.image.BufferedImage;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
@ -23,47 +22,58 @@ public class TengwarImageGenerator {
|
||||||
* @param italic Whether or not the font should be italic.
|
* @param italic Whether or not the font should be italic.
|
||||||
* @return An Image Object containing the drawn text.
|
* @return An Image Object containing the drawn text.
|
||||||
*/
|
*/
|
||||||
public static Image generateImage(String tengwarText, int maxWidth, int fontSize, boolean bold, boolean italic) {
|
public static File generateImage(String tengwarText, int maxWidth, float fontSize, boolean bold, boolean italic, String filepath) {
|
||||||
BufferedImage bufferedImage = new BufferedImage(maxWidth, 1, BufferedImage.TYPE_INT_RGB);
|
BufferedImage bufferedImage = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);
|
||||||
Graphics2D graphics = (Graphics2D)bufferedImage.getGraphics();
|
Graphics2D graphics = bufferedImage.createGraphics();
|
||||||
Color backgroundColor = new Color(54, 57, 62);
|
Font tengwarFont = getTengwarFont(bold, italic, fontSize);
|
||||||
Font tengwarFont = getTengwarFont(bold, italic);
|
|
||||||
tengwarFont = tengwarFont.deriveFont(fontSize);
|
|
||||||
graphics.setFont(tengwarFont);
|
graphics.setFont(tengwarFont);
|
||||||
FontMetrics fm = graphics.getFontMetrics();
|
FontMetrics fm = graphics.getFontMetrics();
|
||||||
Rectangle2D rect = fm.getStringBounds(tengwarText, graphics);
|
int width = fm.stringWidth(tengwarText);
|
||||||
graphics.setColor(backgroundColor);
|
int height = fm.getHeight();
|
||||||
graphics.fillRect((int)rect.getX(), (int)rect.getY(), (int)rect.getWidth(), (int)rect.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.setColor(Color.white);
|
||||||
graphics.drawString(tengwarText, 0, fm.getHeight());
|
graphics.setFont(tengwarFont);
|
||||||
|
graphics.drawString(tengwarText, 0, fm.getAscent());
|
||||||
|
graphics.dispose();
|
||||||
|
|
||||||
try {
|
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) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
return (Image)bufferedImage;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the tngan.ttf font loaded into a Java Font.
|
* Returns the tngan.ttf font loaded into a Java Font.
|
||||||
* @param bold Whether or not font should be bold.
|
* @param bold Whether or not font should be bold.
|
||||||
* @param italic Whether or not font should be italic.
|
* @param italic Whether or not font should be italic.
|
||||||
|
* @param size The font size, in pt.
|
||||||
* @return The Font loaded, or null.
|
* @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;
|
Font font = null;
|
||||||
try {
|
try {
|
||||||
String filename = null;
|
String filename;
|
||||||
if (bold && italic){
|
if (bold && italic){
|
||||||
filename = "resources/tnganbi.ttf";
|
filename = "tnganbi.ttf";
|
||||||
} else if (bold){
|
} else if (bold){
|
||||||
filename = "resources/tnganb.ttf";
|
filename = "tnganb.ttf";
|
||||||
} else if (italic){
|
} else if (italic){
|
||||||
filename = "resources/tngani.ttf";
|
filename = "tngani.ttf";
|
||||||
} else {
|
} 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) {
|
} catch (FontFormatException | IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue