Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java drawstring directly image [duplicate]

Tags:

java

image

swing

Possible Duplicate:
Using Graphics2D to overlay text on a BufferedImage and return a BufferedImage

My Question is how to draw a String permanently to an image.

When I draw the String and then refresh the image the String is gone on the display.

like image 956
Tom Avatar asked Jun 03 '26 13:06

Tom


2 Answers

..how to draw a String permanently to an image.

If you mean a BufferedImage then it is simple.

  1. Get a Graphics or Graphics2D instance from the image.
  2. Draw the string to it.
  3. Dispose of the graphics object.

The job is done. To display the image, add it to a label.

If you have an Image instance as opposed to a BufferedImage instance, create a buffered image as large as the original image, then paint it to the graphics instance before step 2. (draw the string) above.

like image 103
Andrew Thompson Avatar answered Jun 06 '26 03:06

Andrew Thompson


In order to make the text always show on top of the image, place your drawString call inside paint(Graphics g)

It would be helpful if you posted some of your code, but I think you are probably calling drawString from the wrong place. Now if you also want to save the image with the text on top, that is another matter. I would probably use javax.imageio.ImageIO.write for that.

like image 35
Thorn Avatar answered Jun 06 '26 03:06

Thorn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!