How to change font style of text on image in java? - java

I am creating image dynamically and writing on image, there are limited set of font style.I want to change it to some other style in "Kunstler Script".
here is my code:
BufferedImage image = new BufferedImage(imageWidth,
imageHeight,BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = image.createGraphics();
g2.clearRect(0, 0, imageWidth, imageHeight);
g2.drawImage(image, 0, 0, null);
g2.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 65));
g2.setColor(Color.orange);
g2.drawString(" MY IMAGE", 350, 70);
g2.drawLine(350, 80, 1000, 80);
g2.dispose();
File out = new File("E:/" + IMAGE + "_1.png");
ImageIO.write(image, "png", out);
here i want to change the font style
g2.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 65));
at the place of "SANS_SERIF" i want some another font like "Kunstler Script"
please anybody help me to FIX it.
Thank you.

Assuming Kuntsler Script is an available font, it's as simple as:
g2.setFont(new Font("Kunstler Script", Font.BOLD, 65));

Related

Java AWT text produces artifacts

I'm trying to draw some text on some plain color background, and I'm getting some artifacts around the text. Is there a way to get clean writing?
final BufferedImage image = new BufferedImage(400, 400,
BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = (Graphics2D) image.getGraphics();
g2d.setColor(Color.BLUE);
g2d.fillRect(0,0, image.getWidth(), image.getHeight());
Font font = new Font ("Verdana", Font.PLAIN, 40);
AffineTransform affinetransform = new AffineTransform();
FontRenderContext frc = new FontRenderContext(affinetransform, true, true);
TextLayout layout = new TextLayout("Hello World!", font, frc);
g2d.setColor(Color.WHITE);
layout.draw(g2d, 100, 100);
ImageIO.write(image, "jpg", new File("testDirtyText.jpg"));
It's producing some artifacts as shown here:
Any advice around any other aspect of this would be appreciated as well.
Thanks!
I'm not quite sure whether you're referring to aliasing artifacts or JPEG compression artifacts. To fix the former, add:
g2d.setRenderingHint(
RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
See https://docs.oracle.com/javase/8/docs/api/java/awt/RenderingHints.html for more rendering options.
To fix the latter, save as a PNG instead:
ImageIO.write(image, "png", new File("test.png"));
Or if you really need a JPEG, see this Q&A about setting the JPEG quality level.

Is it possible to add multiple Font style at the same time at some string text?

I am trying to draw a string into an image.is it possible to add multiple Font style at the same text(bold and italic).?
my code is:
Graphics g = image.getGraphics();
Font font = new Font("Arial", Font.ITALIC, 16);
g.setFont(font);
g.drawString(text, 100, 100);
g.dispose();
but i want to add italic with bold attribute.
As per the documentation, you can OR together the styles you want.
Font font = new Font ("Arial", Font.BOLD | Font.ITALIC, 16);

How to draw char of Wingding.ttf font with Java Graphics.DrawString?

I am trying to draw characters from Wingding.ttf font with Java Graphics.DrawString. But resulting image contains only rectangle instead of char.
BufferedImage image = new BufferedImage(100, 100, BufferedImage.TYPE_4BYTE_ABGR);
Graphics graphics = image.getGraphics();
Font font = new Font("Wingdings", Font.PLAIN, 20);
graphics.setFont(font);
graphics.setColor(Color.BLACK);
graphics.drawString("\u00d8", 10, 10); // THREE-D TOP-LIGHTED RIGHTWARDS ARROWHEAD char
ImageIO.write(image, "PNG", new File(TEST_DATA_DIR + "bullet_char.png"));
How can I do this?
I dont think wingdings is one of the "standard" fonts
Font font = null;
try {
font=Font.createFont( Font.TRUETYPE_FONT,
new FileInputStream(new File("/pathto/WINGDINGS.TTF")) );
} catch(Exception e) {
System.out.println(e);
}
font=font.deriveFont(Font.PLAIN, 200);
graphics.setFont(font);
once you load the font (its always PLANE 1 pnt) you can the derive the style and size you want....
As similar questions has been answered here: Drawing exotic fonts in a java applet, you need to pass \uF0d8 instead of \u00d8.
graphics.drawString("\uF0d8", 10, 10);

Java 1.6 Graphics2D: Rendering text into a box

I am looking for an easy way to render a String into a rectangular box within a JPG whereas line breaks should happen automatically for that text box.
Is this possible with Graphics2D ?
Rendering a string on a single line is easy, the following code snippet uses Antialiasing as well as a good JPG output compression quality:
BufferedImage img = ImageIO.read(new File(".../input.jpg"));
int width = img.getWidth();
int height = img.getHeight();
Color zgColor = new Color(0xAB,0x3C,0x2E);
Color grey = new Color(0xCC,0xCC,0xCC);
BufferedImage bufferedImage = new BufferedImage(width, height, img.getType());
Graphics2D g = bufferedImage.createGraphics();
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// draw graphics
g.drawImage(img, 0, 0, null);
g.setColor(zgColor);
int y = 900;
int x = 50;
g.setFont(new Font("Arial", Font.BOLD, 80));
g.drawString("Demo Text", x, y);
y+=80;
g.setColor(Color.WHITE);
g.setFont(new Font("Arial", Font.BOLD, 60));
g.drawString("Some other text a bit below", x, y);
y+=400;
g.setFont(new Font("Arial", Font.BOLD, 30));
g.setColor(Color.WHITE);
g.drawString("AND THIS WOULD BE THE TEXT I'D LIKE TO FIT INTO A BOX WITH AUTOMATIC LINE BREAKS", x, y);
g.dispose();
// Save as high quality JPEG
File targetFile = new File(".......result.jpg");
//ImageIO.write(bufferedImage, "jpg", targetFile); // this would give bad quality!
Iterator iter = ImageIO.getImageWritersByFormatName("jpeg");
ImageWriter writer = (ImageWriter)iter.next();
ImageWriteParam iwp = writer.getDefaultWriteParam();
iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
iwp.setCompressionQuality(1); // best quality
FileImageOutputStream output = new FileImageOutputStream(targetFile);
writer.setOutput(output);
IIOImage image = new IIOImage(bufferedImage, null, null);
writer.write(null, image, iwp);
writer.dispose();
System.out.println("Done.");
Check out LineBreakMeasurer. The API has some example code to get you started.
Or another approach is to create a JLabel with your image. Then you can add a JTextArea to the label and set the wrapping property on. Then the text will automatically wrap when you add the text area to the label. You will manually need to set the bounds of the text area within the label to control the placement of the text.

Why different font rendering with graphics.drawString() and a default JLabel with ClearType?

Why has the displayed GUI different font style/rendering with graphics.drawString() and a default JLabel with activated cleartype? And how can i fix it?
Try this
Graphics2D g2d = (Graphics2D)g;
Font font = new Font("Arial", Font.PLAIN, 12);
g2d.setFont(font);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.drawString("Hello World", 25, 100);

Categories