Загрузка данных
package lr4; // Убедитесь, что пакет называется так же, как у вас в проекте
import java.awt.*;
import javax.swing.*;
// Имя класса должно совпадать с именем файла: zad1.java
public class zad1 extends JFrame {
// Конструктор класса
zad1(String s) {
super(s);
setLayout(null);
setSize(600, 600);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Исправлено: 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);
}
}
@Override
public void paint(Graphics g) {
super.paint(g);
paintS(g);
}
public static void main(String[] args) {
// Создаем объект того класса, в котором находимся (zad1)
new zad1("Рисунок");
}
}
import java.awt.*;
import javax.swing.*;
public class PictureD extends JFrame {
PictureD(String s) {
super(s);
setLayout(null);
setSize(600, 600);
setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
// Метод для динамической отрисовки
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);
paintD(g);
}
public static void main(String[] args) {
new PictureD("Рисунок (Динамическая отрисовка)");
}
}
import java.awt.*;
import javax.swing.*;
import java.util.Scanner; // Импортируем Scanner для ввода с консоли
public class MyPicture extends JFrame {
MyPicture(String s) {
super(s);
setLayout(null);
setSize(600, 600);
setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
// Статический метод отрисовки (как в Задании 1)
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);
}
}
// Динамический метод отрисовки (как в Задании 2)
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);
// Ввод данных с консоли для выбора режима
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(); // Закрываем сканер
// Выбор метода в зависимости от ввода пользователя
if (choice == 1) {
paintS(g);
} else if (choice == 2) {
paintD(g);
} else {
System.out.println("Неверный ввод. Выполняется статическая отрисовка по умолчанию.");
paintS(g);
}
}
public static void main(String[] args) {
new MyPicture("Рисунок (Выбор режима)");
}
}