V
V
vvafree2016-10-02 08:46:03
Java
vvafree, 2016-10-02 08:46:03

Figure (image) generator for tests in Java?

Hello.
I am studying Java, there is a need for this generator, but I don’t know how to approach this issue. Google doesn't help. Advise on algorithms, methods and tools.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
exenza, 2016-10-02
@vvafree

Check out this Oracle tutorial on how to work with images - Drawing an Image and Writing/Saving an Image sections.
as an example:
600d09bdd65c4240b856d72e2a8c793a.jpeg

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 question

Ask a Question

731 491 924 answers to any question