r2
@@ -13,7 +13,7 @@ public class CarAnimation extends JFrame {
|
||||
|
||||
public CarAnimation() {
|
||||
setTitle("Анимация с машинкой");
|
||||
setSize(300, 200);
|
||||
setSize(1280, 720);
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
setLocationRelativeTo(null);
|
||||
|
||||
@@ -49,9 +49,9 @@ public class CarAnimation extends JFrame {
|
||||
JMenuItem normalSpeed = new JMenuItem("Нормально");
|
||||
JMenuItem fastSpeed = new JMenuItem("Быстро");
|
||||
|
||||
slowSpeed.addActionListener(e -> panel.setSpeed(300));
|
||||
normalSpeed.addActionListener(e -> panel.setSpeed(100));
|
||||
fastSpeed.addActionListener(e -> panel.setSpeed(30));
|
||||
slowSpeed.addActionListener(e -> panel.setSpeed(100));
|
||||
normalSpeed.addActionListener(e -> panel.setSpeed(50));
|
||||
fastSpeed.addActionListener(e -> panel.setSpeed(20));
|
||||
|
||||
speedMenu.add(slowSpeed);
|
||||
speedMenu.add(normalSpeed);
|
||||
@@ -139,27 +139,7 @@ class AnimationPanel extends JPanel implements MouseListener, java.awt.event.Mou
|
||||
}
|
||||
}
|
||||
|
||||
// private BufferedImage[] createDummyCarArray(Color color) {
|
||||
// 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;
|
||||
}
|
||||
|
||||
private int carX = 0;
|
||||
public AnimationPanel() {
|
||||
// подготовка изображений
|
||||
frames = prepareImages();
|
||||
@@ -171,16 +151,27 @@ class AnimationPanel extends JPanel implements MouseListener, java.awt.event.Mou
|
||||
public void run() {
|
||||
while (true) {
|
||||
try {
|
||||
// условие только если мышь внутри окна
|
||||
if (carX > getWidth()) {
|
||||
carX = -200; // Сбрасываем на ширину новой машинки, чтобы она выезжала плавно
|
||||
}
|
||||
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; // выход
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -190,8 +181,43 @@ class AnimationPanel extends JPanel implements MouseListener, java.awt.event.Mou
|
||||
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() {
|
||||
int frameCount = 5; // количество кадров
|
||||
int frameCount = 1; // количество кадров
|
||||
this.isFolderMode = false;
|
||||
BufferedImage[] imgArray = new BufferedImage[frameCount];
|
||||
|
||||
@@ -203,41 +229,53 @@ class AnimationPanel extends JPanel implements MouseListener, java.awt.event.Mou
|
||||
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
|
||||
protected void paintComponent(Graphics 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());
|
||||
|
||||
// отрисовка в центре экрана
|
||||
if (frames.length > 0) {
|
||||
int x = (getWidth() - frames[0].getWidth()) / 2;
|
||||
int y = (getHeight() - frames[0].getHeight()) / 2;
|
||||
g.drawImage(currentFrames[currentFrameIndex], x, y, this);
|
||||
// Слой 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) {
|
||||
|
||||
|
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 |