0

I am running into some issues while wanting to write some text on an image. As I looked, it could be done with following code:

package asd;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;

public class ImageAddingText {

    
    public static void main(String args[]) throws IOException {
        
        BufferedImage image = ImageIO.read(new File("C:\\Users\\Slobodan\\Desktop\\image2.png"));
        
        Font font = new Font("Arial", Font.BOLD, 20);
        
        Graphics g = image.getGraphics();
        g.setFont(font);
        g.setColor(Color.GREEN);
        g.drawString("Medium", 50, 50);
        System.out.println("Finished");
        System.out.println(image.getWidth());
    }
}

So image gets loaded into memory, image.getGraphics() create Graphics2D object, then the font, color and draw string are set. But in the image nothing happens, it still remains completely unchanged. The image is white and black, white occupying most of the space.. I tried to change colors, extension of the image, and seems nothing of that helps. I was expecting that i can see changes directly on the image, i thought it should work that way. After it runs and compiles, there are no error messages at all. I am using Java 8 together with Spring Boot. (though i run only Java)
Does anyone maybe have an idea what can be the issue there?

Thank you very much for reading.

slomil
  • 193
  • 1
  • 4
  • 12
  • Consider condensing your code and your problem into the smallest program that compiles and runs for us, that demonstrates your problem for us, but that does nothing else, a [mre]. Please read the link as it will tell you *exactly* what I am requesting, how to create one, and why it can help both you and us. – Hovercraft Full Of Eels Jan 31 '21 at 18:01
  • Can you explain me why this isn't good enough? It compiles and runs, though doesn't work. And it is minimum of the code necessary for running, found on this page https://www.baeldung.com/java-add-text-to-image in section 2.2. Path inside File refers to image location. Think I have read your comment.. – slomil Jan 31 '21 at 18:05
  • OK, how about this: 1) do you know where the error or problems lies? 2) How or why do you know that the problem exists in the code that you've posted? 3) If the code looks "OK" to me, how can I guess how or where to fix it? Maybe someone else can look at this and go, "aha, there's your problem", but for the life of me, I can't. But regardless, the link should already be telling you this information, and again, you probably really should read it. – Hovercraft Full Of Eels Jan 31 '21 at 18:27
  • 1
    Regarding, *"... And it is minimum of the code necessary for running"* -- no main method, no imports, no image file, maybe it will run for you, but it will most certainly *not* run for anyone else. Again, the [mre] link will tell you what we mean by the minimum code necessary for running. – Hovercraft Full Of Eels Jan 31 '21 at 18:28
  • @HovercraftFullOfEels There i meant that this is my whole main method, with no other code in it. There are no any imports of image. Image is located on Desktop, and path "C:\\Users\\Slobodan\\Desktop\\image2.png" is location of it on the desktop. Also, i think code reads image since it correctly displays its weight. Now, i don't know how else should i change code to replicate my problem since the main problem is how to write text to image, and found several examples of this exact code and how it should worked previously. – slomil Jan 31 '21 at 18:38
  • When I've done this myself, I have created a completely new small program that has the goal of being able to compile run and reproduce the problem so that I and others can directly observe it. I have used images from online sources, obtained via URL rather than local files. – Hovercraft Full Of Eels Jan 31 '21 at 18:44
  • Where do you display the image? And again, try to have in your example an image that is obtained via the internet, via URL, not locally, an image that we have no access to. And, best to display the pre and post text images. – Hovercraft Full Of Eels Jan 31 '21 at 18:56
  • I don't display it here in program, should i do that? (i check it on the local location if there are changes) i'll try to do that then.. – slomil Jan 31 '21 at 19:03
  • Yes, you should display it, if only to test it – Hovercraft Full Of Eels Jan 31 '21 at 19:05

1 Answers1

1

Again, your code works for me, using an online image:

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.*;

public class ImageAddingText {

    
    public static void main(String args[]) throws IOException {
        String imgUrl = "https://media.glamour.com/photos/5a425fd3b6bcee68da9f86f8/16:9/w_2560%2Cc_limit/best-face-oil.png";
        
        // BufferedImage image = ImageIO.read(new File("C:\\Users\\Slobodan\\Desktop\\image2.png"));
        
        URL url = new URL(imgUrl);
        BufferedImage image = ImageIO.read(url);
        
        // display the original image
        Icon icon = new ImageIcon(image);
        JOptionPane.showMessageDialog(null, icon);            

        Font font = new Font("Arial", Font.BOLD, 20);            
        Graphics g = image.getGraphics();
        g.setFont(font);
        g.setColor(Color.GREEN);
        g.drawString("Medium", 50, 50);
        
        g.dispose(); // you should always dispose resources *you* create yourself

        // display the changed image
        icon = new ImageIcon(image);            
        JOptionPane.showMessageDialog(null, icon);      
        
        System.out.println("Finished");
        System.out.println(image.getWidth());
    }
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Thank you man, you saved my life!:) I didn't know about that url thing..Though I don't know if I can make change of exact picture in the url, i will try now to see that, or could maybe create icon instead of image and that display.. Thanks very much for the help. – slomil Jan 31 '21 at 20:21