Answer the question
In order to leave comments, you need to log in
Answer the question
In order to leave comments, you need to log in
Check out this Oracle tutorial on how to work with images - Drawing an Image and Writing/Saving an Image sections.
as an example:
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;
public class Test {
private final static int MAX = 999_999;
private final static int MIN = 100_000;
private final static int IMAGE_WIDTH = 50;
private final static int IMAGE_HEIGHT = 10;
private final static int FONT_SIZE = 12;
private final static int TEXT_COORD_X = 0;
private final static int TEXT_COORD_Y = 10;
private final static String FILEPATH_TO_SAVE = "/home/max/Pictures/image.jpeg";
public static void main(String[] args) {
BufferedImage image = new BufferedImage(IMAGE_WIDTH, IMAGE_HEIGHT, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = image.createGraphics();
g2.setFont(new Font("SansSerif", Font.PLAIN, FONT_SIZE));
g2.drawString(getRandomNumber().toString(), TEXT_COORD_X, TEXT_COORD_Y);
try {
ImageIO.write(image, "jpeg", new File(FILEPATH_TO_SAVE));
} catch(IOException ex) {
ex.printStackTrace();
}
}
private static Integer getRandomNumber() {
Random random = new Random();
return random.nextInt((MAX - MIN) + 1) + MIN;
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question