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

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);

Related

Add Translucent Image to JLayeredPane

So I'm making a stopmotion application - the live feed from my webcam is inside a JPanel (with setOpaque(false)) that is inside the top (10th) layer in a JLayeredPane - and basically, when I take a picture, I want to add it to one of the lower layers so that a trace of the previous pictures you've taken shows up on screen. Here's how I'm trying to do that now:
EDIT: this is my new code based off the answer below - this now does nothing, as opposed to just adding the opaque image as before - if I add this to a JPanel, though, and add the JPanel to the JLayeredPane, then all I get is gray
BufferedImage img2 = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g = img2.createGraphics();
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .5f));
g.drawImage(img2, 0, 0, null);
g.dispose();
ImageIcon imgIcon = new ImageIcon(new ImageIcon(img2).getImage().getScaledInstance(img2.getWidth(), img2.getHeight(), Image.SCALE_SMOOTH));
JLabel showPic = new JLabel(imgIcon);
showPic.setSize(layers.getSize());
showPic.setBounds(layers.getX() + 18, layers.getY(), img2.getWidth(), img2.getHeight());
layers.add(showPic, new Integer(1)); //layers is my JLayeredPane
layers.repaint();
layers.revalidate();
img is the picture I've just captured from my webcam, and I'm trying to make it semi transparent, then add it to a JLabel. How can I make this work? Or is there a better way to do this?
I don't know if this will solve the problem but the image you are looking for is
BufferedImage img2 = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);
java.awt.Transparency.TRANSLUCENT has the value of 3 and corresponds to BufferedImage.TYPE_INT_ARGB_PRE which only god knows what it's doing.
With BufferedImage.TYPE_INT_ARGB you can create a transparent image and apply Composite etc
You also need to correct this
g.drawImage(img2, null, 0, 0);
to
g.drawImage(img2, 0, 0, null);
--
Heres how transparency works for me:
I have two images bim and bim2 and I draw one on top of the other:
BufferedImage bim=null, bim2=null;
try {
bim=ImageIO.read(new File("...."));
bim2=ImageIO.read(new File("...."));
}
catch (Exception ex) { System.err.println("error in bim "+ex); }
int wc=bim.getWidth(), hc=bim.getHeight();
BufferedImage img2 = new BufferedImage(bim.getWidth(), bim.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g = img2.createGraphics();
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .5f));
g.drawImage(bim, 0, 0, null);
g.drawImage(bim2, 0, 0, wc, hc, null);
Then I can display it on a JPanel Jframe or whatever.
I can even create the Label:
JLabel showPic = new JLabel(new ImageIcon(img2));
JFrame f=new JFrame();
f.setSize(500, 500);
f.add(showPic);
f.setVisible(true);

Set Font type to Futura in Java

Every thing works fine except that it does not print Futura Font on the picture just a default looking font
BufferedImage image = ImageIO.read(new File("image.jpeg"));
Graphics g =image.getGraphics();
g.setFont(new Font("Futura", Font.PLAIN, 45));
g.drawString("Hello", 100, 440);
ImageIO.write(image, "png", new File("test.png"));
Maybe this can help :)
new font type in java
and this one: How to use Open Type Fonts in Java?
Follow links on 1st link

Printing the line separator character in an image [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Problems with newline in Graphics2D.drawString
String eol =System.lineSeparator();
String sampleText =epcURNText +eol+studentName+eol+DelayComments+eol+ArrivalMethodComments+eol;
System.out.println(sampleText);
Font font = new Font("Tahoma", Font.PLAIN, 11);
FontRenderContext frc = new FontRenderContext(null, true, true);
//get the height and width of the text
Rectangle2D bounds = font.getStringBounds(sampleText, frc);
int w = (int) bounds.getWidth();
int h = (int) bounds.getHeight();
//create a BufferedImage object
BufferedImage image = new BufferedImage(w, h,
BufferedImage.TYPE_INT_RGB);
//calling createGraphics() to get the Graphics2D
Graphics2D g = image.createGraphics();
//set color and other parameters
g.setColor(Color.WHITE);
g.fillRect(0, 0, w, h);
g.setColor(Color.BLACK);
g.setFont(font);
g.drawString(sampleText, (float) bounds.getX(),
(float) -bounds.getY());
//releasing resources
g.dispose();
// define the format of print document
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(image, "gif", os);
File f = new File("MyFile.jpg");
ImageIO.write(image, "JPEG", f);
Within the aforementioned code I am trying to print a string with newline characters within an image. However the newline character is omitted thus my text is presented within a single line? Does anybody have any idea on how to fix this?
Render the text in a JLabel with HTML formatting as shown the LabelRenderTest source in this answer.
It provides something better than line-breaks - the style for body width. Better in the sense that we do not need to manually calculate where to put line breaks for text (which also might be rendered in fonts that are not fixed width).

Improve "drawString" methods text quality

I'm in the process of making a captcha in Java but I'm having trouble improving the text quality the "drawString" method generates on top of my image.
Example of the text quality:
You can actually see the horrible edges on the text.
Java code:
File file = new File("C:\\captcha.png");
File file2 = new File("C:\\captcha2.png");
File fontfile = new File("C:\\xerox.ttf");
BufferedImage bfimage = ImageIO.read(file);
Graphics2D g = bfimage.createGraphics();
Font myfont = Font.createFont(Font.PLAIN, fontfile);
myfont = myfont.deriveFont(50f);
g.setFont(myfont);
g.setColor(Color.black);
AffineTransform att = new AffineTransform();
g.translate(100, 50);
att.rotate(Math.toRadians(15), 100, 50);
g.setTransform(att);
g.drawString("12345", 100, 50);
RenderedImage rimg = bfimage;
ImageIO.write(rimg, "PNG", file2);
Example of same font used in php, but here the quality is A LOT better with smooth edges:
How do I improve the text quality generated by the "drawString" method in Java?
Graphics and Graphics2D provide a rendering hint framework that allows you to configure some parts of the rendering of a component. Use an antialiasing rendering hint:
g2d.setRenderingHint( RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_DEFAULT);
and you should get antialiased text on your captcha.
http://docs.oracle.com/javase/6/docs/api/java/awt/RenderingHints.html for reference

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.

Categories