Compare commits

1 Commits
rc1 ... main

Author SHA1 Message Date
10042d6897 r2 2026-04-26 23:49:53 +09:00
8 changed files with 108 additions and 70 deletions

View File

@@ -13,7 +13,7 @@ public class CarAnimation extends JFrame {
public CarAnimation() { public CarAnimation() {
setTitle("Анимация с машинкой"); setTitle("Анимация с машинкой");
setSize(300, 200); setSize(1280, 720);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null); setLocationRelativeTo(null);
@@ -49,9 +49,9 @@ public class CarAnimation extends JFrame {
JMenuItem normalSpeed = new JMenuItem("Нормально"); JMenuItem normalSpeed = new JMenuItem("Нормально");
JMenuItem fastSpeed = new JMenuItem("Быстро"); JMenuItem fastSpeed = new JMenuItem("Быстро");
slowSpeed.addActionListener(e -> panel.setSpeed(300)); slowSpeed.addActionListener(e -> panel.setSpeed(100));
normalSpeed.addActionListener(e -> panel.setSpeed(100)); normalSpeed.addActionListener(e -> panel.setSpeed(50));
fastSpeed.addActionListener(e -> panel.setSpeed(30)); fastSpeed.addActionListener(e -> panel.setSpeed(20));
speedMenu.add(slowSpeed); speedMenu.add(slowSpeed);
speedMenu.add(normalSpeed); speedMenu.add(normalSpeed);
@@ -139,27 +139,7 @@ class AnimationPanel extends JPanel implements MouseListener, java.awt.event.Mou
} }
} }
// private BufferedImage[] createDummyCarArray(Color color) { private int carX = 0;
// BufferedImage[] newFrames = new BufferedImage[5];
// for (int i = 0; i < 5; i++) {
// newFrames[i] = createDummyCar(i, 5, color); // цвет в метод отрисовки
// }
// return newFrames;
// }
private BufferedImage createDummyCar(int frame, int total, Color color) {
BufferedImage img = new BufferedImage(200, 100, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = img.createGraphics();
int offset = frame * 10;
g.setColor(color); // Используем переданный цвет
g.fillRect(20 + offset, 40, 100, 40);
g.setColor(Color.BLACK);
g.fillOval(30 + offset, 70, 20, 20);
g.fillOval(90 + offset, 70, 20, 20);
g.dispose();
return img;
}
public AnimationPanel() { public AnimationPanel() {
// подготовка изображений // подготовка изображений
frames = prepareImages(); frames = prepareImages();
@@ -171,16 +151,27 @@ class AnimationPanel extends JPanel implements MouseListener, java.awt.event.Mou
public void run() { public void run() {
while (true) { while (true) {
try { try {
// условие только если мышь внутри окна if (carX > getWidth()) {
carX = -200; // Сбрасываем на ширину новой машинки, чтобы она выезжала плавно
}
if (isMouseInside) { if (isMouseInside) {
// Двигаем машинку вправо
carX += 15;
// Если машинка уехала за правый край, возвращаем её за левый край
if (carX > getWidth()) {
carX = -100;
}
// Если нужны еще и смены кадров (анимация колес и т.д.)
currentFrameIndex = (currentFrameIndex + 1) % frames.length; currentFrameIndex = (currentFrameIndex + 1) % frames.length;
repaint(); repaint();
Thread.sleep(speedDelay); Thread.sleep(speedDelay);
} else { } else {
Thread.sleep(50); Thread.sleep(50);
} }
} catch (InterruptedException e) { } catch (InterruptedException e) {
break; // выход break;
} }
} }
} }
@@ -190,8 +181,43 @@ class AnimationPanel extends JPanel implements MouseListener, java.awt.event.Mou
addMouseMotionListener(this); addMouseMotionListener(this);
} }
private BufferedImage createDummyCar(int frame, int total) {
BufferedImage img = new BufferedImage(100, 100, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = img.createGraphics();
// 1. Рисуем корпус машинки
g.setColor(Color.RED);
g.fillRect(0, 40, 100, 40);
// крыша
g.setColor(Color.RED);
g.fillRect(20, 20, 60, 20);
// 2. Рисуем окна
// Цвет стекла (светло-голубой)
g.setColor(new Color(173, 216, 230));
// Переднее окно
g.fillRect(30, 25, 15, 15);
// Заднее окно
g.fillRect(55, 25, 15, 15);
// // Добавляем черную обводку для окон, чтобы они были четкими
// g.setColor(Color.BLACK);
// g.drawRect(20, 45, 25, 15);
// g.drawRect(50, 45, 25, 15);
// 3. Рисуем колеса
g.setColor(Color.BLACK);
g.fillOval(10, 70, 20, 20);
g.fillOval(70, 70, 20, 20);
g.dispose();
return img;
}
private BufferedImage[] prepareImages() { private BufferedImage[] prepareImages() {
int frameCount = 5; // количество кадров int frameCount = 1; // количество кадров
this.isFolderMode = false; this.isFolderMode = false;
BufferedImage[] imgArray = new BufferedImage[frameCount]; BufferedImage[] imgArray = new BufferedImage[frameCount];
@@ -203,41 +229,53 @@ class AnimationPanel extends JPanel implements MouseListener, java.awt.event.Mou
return imgArray; return imgArray;
} }
// рисование на BufferedImage
private BufferedImage createDummyCar(int frame, int total) {
BufferedImage img = new BufferedImage(200, 100, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = img.createGraphics();
// корпус
int offset = frame * 10;
g.setColor(Color.RED);
g.fillRect(20 + offset, 40, 100, 40);
// крыша
g.setColor(Color.RED);
g.fillRect(40 + offset, 20, 50, 25);
// колеса
g.setColor(Color.BLACK);
g.fillOval(30 + offset, 70, 20, 20);
g.fillOval(90 + offset, 70, 20, 20);
g.dispose();
return img;
}
@Override @Override
protected void paintComponent(Graphics g) { protected void paintComponent(Graphics g) {
super.paintComponent(g); super.paintComponent(g);
// очистка фона
g.setColor(Color.WHITE); if (frames.length > 0) {
// 1. Параметры машинки
int originalWidth = frames[0].getWidth();
int originalHeight = frames[0].getHeight();
int newWidth = originalWidth * 2;
int newHeight = originalHeight * 2;
int x = carX;
int y = (getHeight() - newHeight) / 2;
// 2. Параметры дороги
int roadHeight = 80;
int roadY = y + newHeight - 90; // Дорога под колесами
// --- ОТРИСОВКА ФОНА (СЛОИ) ---
// Слой 1: Небо (голубое)
// Рисуем на весь экран
g.setColor(new Color(135, 206, 235)); // Цвет SkyBlue
g.fillRect(0, 0, getWidth(), getHeight()); g.fillRect(0, 0, getWidth(), getHeight());
// отрисовка в центре экрана // Слой 2: Трава (темно-зеленая)
if (frames.length > 0) { // Рисуем от нижней границы дороги до самого низа экрана
int x = (getWidth() - frames[0].getWidth()) / 2; g.setColor(new Color(34, 139, 34)); // Цвет ForestGreen
int y = (getHeight() - frames[0].getHeight()) / 2; int grassStartY = roadY + roadHeight;
g.drawImage(currentFrames[currentFrameIndex], x, y, this); int grassHeight = getHeight() - grassStartY;
if (grassHeight > 0) {
g.fillRect(0, grassStartY, getWidth(), grassHeight);
}
// Слой 3: Дорога (темно-серая)
g.setColor(Color.DARK_GRAY);
g.fillRect(0, roadY, getWidth(), roadHeight);
// Слой 4: Разметка (белая)
g.setColor(Color.WHITE);
for (int i = 0; i < getWidth(); i += 60) {
g.fillRect(i, roadY + (roadHeight / 2) - 3, 30, 6);
}
// --- ОТРИСОВКА ОБЪЕКТОВ ---
// Слой 5: Машинка (поверх всего)
g.drawImage(currentFrames[currentFrameIndex], x, y, newWidth, newHeight, this);
} }
if (isFolderMode) { if (isFolderMode) {

Binary file not shown.

After

Width:  |  Height:  |  Size: 321 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 19 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 19 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 19 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 19 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 19 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 935 KiB