Using Graphics2D to overlay text on a BufferedImage and return a BufferedImage?

The method drawString() uses x and y for the leftmost character's baseline Numbers typically have no descenders; if the same is true of text a string drawn at position (0,0) will be rendered entirely outside the image. See this example.

The method drawString() uses x and y for the leftmost character's baseline. Numbers typically have no descenders; if the same is true of text, a string drawn at position (0,0) will be rendered entirely outside the image. See this example.

Addendum: You may be having trouble with an incompatible color model in your image. One simple expedient is to render the image and then modify it in situ. Import java.awt.

Color; import java.awt. Dimension; import java.awt. EventQueue; import java.awt.

Font; import java.awt. FontMetrics; import java.awt. Graphics; import java.awt.

Graphics2D; import java.awt.image. BufferedImage; import java.io. IOException; import java.net.

URL; import javax.imageio. ImageIO; import javax.swing. JFrame; import javax.swing.

JPanel; public class TextOverlay extends JPanel { private BufferedImage image; public TextOverlay() { try { image = ImageIO. Read(new URL( "

")); } catch (IOException e) { e.printStackTrace(); } this. SetPreferredSize(new Dimension( image.getWidth(), image.getHeight())); image = process(image); } private BufferedImage process(BufferedImage old) { int w = old.getWidth(); int h = old.getHeight(); BufferedImage img = new BufferedImage( w, h, BufferedImage.

TYPE_INT_ARGB); Graphics2D g2d = img.createGraphics(); g2d. DrawImage(old, 0, 0, null); g2d. SetPaint(Color.

Red); g2d. SetFont(new Font("Serif", Font. BOLD, 20)); String s = " world!"; FontMetrics fm = g2d.getFontMetrics(); int x = img.getWidth() - fm.

StringWidth(s) - 5; int y = fm.getHeight(); g2d. DrawString(s, x, y); g2d.dispose(); return img; } @Override protected void paintComponent(Graphics g) { super. PaintComponent(g); g.

DrawImage(image, 0, 0, null); } private static void create() { JFrame f = new JFrame(); f. SetDefaultCloseOperation(JFrame. EXIT_ON_CLOSE); f.

Add(new TextOverlay()); f.pack(); f. SetVisible(true); } public static void main(String args) { EventQueue. InvokeLater(new Runnable() { @Override public void run() { create(); } }); } }.

Well for my tests I'm using x = image.getHeight()/2 and likewise for y, so I don't think that that is the issue, but thanks! – Bolster Apr 17 '10 at 18:57 Any chance you're calling paintComponent() with the old image instead of the new? – trashgod Apr 17 '10 at 21:41 I'm not using any JComponents so no, no paintComponents; but if im missing something fundamental, please slap me with it, I am not a GUI coder and have very little knowledge of dealing with graphics in Java so this could be completely the wrong approach – Bolster Apr 17 '10 at 22:37 Pulled out the relevent bit of code, thank you very much for your help!

– Bolster Apr 18 '10 at 14:14 Works great, but needs some fix if running on Amazon Linux on EC2. See brandon.fuller. Name/archives/2011/09/12/00.

05.15 for details. – barryku 11/09/123 at 17:48.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions