Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 10042d6897 |
@@ -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();
|
||||||
@@ -167,31 +147,77 @@ class AnimationPanel extends JPanel implements MouseListener, java.awt.event.Mou
|
|||||||
|
|
||||||
// создание и запуск потока анимации
|
// создание и запуск потока анимации
|
||||||
animationThread = new Thread(new Runnable() {
|
animationThread = new Thread(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
while (true) {
|
while (true) {
|
||||||
try {
|
try {
|
||||||
// условие только если мышь внутри окна
|
if (carX > getWidth()) {
|
||||||
if (isMouseInside) {
|
carX = -200; // Сбрасываем на ширину новой машинки, чтобы она выезжала плавно
|
||||||
currentFrameIndex = (currentFrameIndex + 1) % frames.length;
|
|
||||||
repaint();
|
|
||||||
Thread.sleep(speedDelay);
|
|
||||||
} else {
|
|
||||||
Thread.sleep(50);
|
|
||||||
}
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
break; // выход
|
|
||||||
}
|
}
|
||||||
|
if (isMouseInside) {
|
||||||
|
// Двигаем машинку вправо
|
||||||
|
carX += 15;
|
||||||
|
// Если машинка уехала за правый край, возвращаем её за левый край
|
||||||
|
if (carX > getWidth()) {
|
||||||
|
carX = -100;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Если нужны еще и смены кадров (анимация колес и т.д.)
|
||||||
|
currentFrameIndex = (currentFrameIndex + 1) % frames.length;
|
||||||
|
|
||||||
|
repaint();
|
||||||
|
Thread.sleep(speedDelay);
|
||||||
|
} else {
|
||||||
|
Thread.sleep(50);
|
||||||
|
}
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
animationThread.start();
|
animationThread.start();
|
||||||
addMouseListener(this);
|
addMouseListener(this);
|
||||||
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);
|
|
||||||
g.fillRect(0, 0, getWidth(), getHeight());
|
|
||||||
|
|
||||||
// отрисовка в центре экрана
|
|
||||||
if (frames.length > 0) {
|
if (frames.length > 0) {
|
||||||
int x = (getWidth() - frames[0].getWidth()) / 2;
|
// 1. Параметры машинки
|
||||||
int y = (getHeight() - frames[0].getHeight()) / 2;
|
int originalWidth = frames[0].getWidth();
|
||||||
g.drawImage(currentFrames[currentFrameIndex], x, y, this);
|
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());
|
||||||
|
|
||||||
|
// Слой 2: Трава (темно-зеленая)
|
||||||
|
// Рисуем от нижней границы дороги до самого низа экрана
|
||||||
|
g.setColor(new Color(34, 139, 34)); // Цвет ForestGreen
|
||||||
|
int grassStartY = roadY + roadHeight;
|
||||||
|
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) {
|
||||||
|
|||||||
|
After Width: | Height: | Size: 321 KiB |
|
Before Width: | Height: | Size: 19 KiB |
|
Before Width: | Height: | Size: 19 KiB |
|
Before Width: | Height: | Size: 19 KiB |
|
Before Width: | Height: | Size: 19 KiB |
|
Before Width: | Height: | Size: 19 KiB |
|
After Width: | Height: | Size: 935 KiB |