Загрузка данных


package project; // Укажите ваш пакет

import java.awt.*;
import javax.swing.*;
import java.util.Scanner;

public class zad3 extends JFrame {
    
    // Переменная для хранения выбранного режима
    private int mode;
    
    // Конструктор принимает заголовок и режим работы
    zad3(String s, int mode) {
        super(s);
        this.mode = mode; // Сохраняем выбор пользователя
        setLayout(null);
        setSize(600, 600);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    // Статическая отрисовка
    public void paintS(Graphics g) {
        g.setColor(Color.YELLOW);
        g.fillRect(0, 0, 600, 600);
        g.setColor(Color.BLUE);
        
        int x1 = 0, y1 = 0;
        int x2 = 600, y2 = 600;
        g.drawLine(x1, y1, x2, y2);
        
        int c = 50;
        int z = x2 * x2 + y2 * y2;
        int s = c * c + c * c;
        
        int i = 0;
        g.fillRect(x1, y1, c, c);
        
        while (s * ++i < z) {
            g.fillRect(x1 + (i * c), y1 + (i * c), c, c);
        }
    }

    // Динамическая отрисовка
    public void paintD(Graphics g) {
        g.setColor(Color.YELLOW);
        g.fillRect(0, 0, 600, 600);
        g.setColor(Color.BLUE);
        
        int x1 = 0, y1 = 0;
        int x2 = 600, y2 = 600;
        g.drawLine(x1, y1, x2, y2);
        
        int c = 50;
        int z = x2 * x2 + y2 * y2;
        int s = c * c + c * c;
        
        int i = 0;
        
        while (s * i < z) {
            g.setColor(Color.YELLOW);
            g.fillRect(0, 0, 600, 600);
            g.setColor(Color.BLUE);
            g.drawLine(x1, y1, x2, y2);
            
            g.fillRect(x1 + (i * c), y1 + (i * c), c, c);
            
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            i++;
        }
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        
        // В зависимости от выбранного режима вызываем нужный метод
        if (mode == 1) {
            paintS(g);
        } else if (mode == 2) {
            paintD(g);
        } else {
            g.drawString("Неверный режим", 250, 300);
        }
    }

    public static void main(String[] args) {
        // Ввод данных ДО создания окна (в главном потоке)
        Scanner in = new Scanner(System.in);
        System.out.println("Выберите режим отрисовки:");
        System.out.println("1 - Статическая отрисовка");
        System.out.println("2 - Динамическая отрисовка");
        System.out.print("Введите 1 или 2: ");
        
        int choice = in.nextInt();
        in.close(); // Закрываем сканер после использования
        
        // Создаём окно, передавая выбранный режим в конструктор
        new zad3("Рисунок (Режим " + choice + ")", choice);
    }
}