0

I have got a stupid problem.I want add a text with alpha 0.5 to my photo to make a watermark but setColor doesn't add a color. When I trying use setComposite I see the error "cannot find symbol".

buffered.getGraphics().drawImage(img, 0, 0, null);
            buffered.getGraphics().setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
             0.1f));
buffered.getGraphics().setColor(new Color(0.2f,0.3f,0.4f,0.5f));
buffered.getGraphics().setFont(new Font("Serif", Font.PLAIN, 32));
buffered.getGraphics().drawString("text", img.getWidth()/2, img.getHeight()/2);
buffered.getGraphics().dispose();
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Piotr
  • 175
  • 1
  • 2
  • 11
  • 1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) [This example](http://stackoverflow.com/a/13440543/418556) shows how to use the color to create translucent text. (I'm considering using it to close this as a duplicate - though it's about transforms mostly.) – Andrew Thompson Nov 18 '16 at 12:51
  • *"When I trying use `setComposite` I see the error "cannot find symbol"." .. `buffered.getGraphics()`* This is not entirely relevant to the stated aim, since it's not necessary to use composites in order to get translucent text. Having said that, it is easy to explain.[`setComposite(..)`](http://docs.oracle.com/javase/8/docs/api/java/awt/Graphics2D.html#setComposite-java.awt.Composite-) is a method of the **`Graphics2D`** API, whereas [`BufferedImage.getGraphics()`](http://docs.oracle.com/javase/8/docs/api/java/awt/image/BufferedImage.html#getGraphics--) returns a `Graphics` object .. – Andrew Thompson Nov 18 '16 at 13:02
  • .. To gain a `Graphics2D` instance, call [**`createGraphics()`**](http://docs.oracle.com/javase/8/docs/api/java/awt/image/BufferedImage.html#createGraphics--) – Andrew Thompson Nov 18 '16 at 13:03
  • Since you seem to have ignored my advice, I'll vote to close and close this question tab in my browser. All the best with it! – Andrew Thompson Nov 19 '16 at 18:42

1 Answers1

0

Simple: You're calling getGraphics() multiple times. You get a new Graphics object on every call.

Call getGraphics() only once and use the reference for all further calls.

Durandal
  • 19,919
  • 4
  • 36
  • 70