Q:

A random output images

Welcome) have a little trouble, help solve them, please. Here's the code:
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;

import javax.swing.JComponent;
import javax.swing.JFrame;

class MyCanvas extends JComponent {

  public void paint(Graphics g) {
    Image img1 = Toolkit.getDefaultToolkit().getImage("yourFile.gif");

    int width = img1.getWidth(this);
    int height = img1.getHeight(this);

    int scale = 2;
    int w = scale * width;
    int h = scale * height;
    // explicitly specify width (w) and height (h)
    g.drawImage(img1, 10, 10, (int) w, (int) h, this);

  }
}

public class Graphics2DDrawScaleImage {
  public static void main(String[] a) {
    JFrame window = new JFrame();
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setBounds(30, 30, 300, 300);
    window.getContentPane().add(new MyCanvas());
    window.setVisible(true);
  }
}

If we run the code, we have displayed a picture.But my task is to bring many of the same image but in different places. How to do it?)
If you do so:
for(int i = 0; i < 500 ; i+=100){
g.drawImage(img1, 10+i, 10, (int) w, (int) h, this);
}

Then the images are displayed in a row. And I would like more randoml

A:

And I would like more random

Use the Random class to generate an integer within your specified range. You would want to use getWidth() and getHeight() to get the bounds of your component so you can specify a realistic range. Then you can randomize the x, y values.
Random random = new Random();
int x = random(getWidth());
int y = random(getHeight();

You might also want to consider the width/height of your images to make sure they are fully contained in the panel.
Also, custom painting is done by overriding the paintComponent(...) method not the paint() method.

