import animals.Animal;
import animals.Background;
import bear.Bear;
import bird.Sparrow;
import modules.AnimalModule;
import modules.Createobject;

import javax.swing.*;
import javax.swing.plaf.basic.BasicComboBoxUI;
import javax.swing.plaf.basic.BasicComboBoxUI.KeyHandler;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;

public class DrawPanel extends JPanel implements ActionListener, MouseMotionListener {
    private final int PANEL_WIDTH;
    private final int PANEL_HEIGHT;
    private final int TIMER_DELAY;
    private Timer timer;
    private BufferedImage backgroundImage;
    private ArrayList<Animal> animals; // List of animals
    private ArrayList<Background> backgrounds; // List of animals
    private int ticksFromStart = 0;
    private KeyHandler keyHandler;

    public DrawPanel(final int width, final int height, final int timerDelay, List<AnimalModule> modules) {
        this.PANEL_WIDTH = width;
        this.PANEL_HEIGHT = height;
        this.TIMER_DELAY = timerDelay;

        timer = new Timer(timerDelay, this);
        timer.start();
        this.keyHandler = new KeyHandler();

        setBackground(Color.green);

        animals = new ArrayList<>(); // Initialize the list of animals
        backgrounds = new ArrayList<>();

        Createobject.createAnimals(animals, getBackground(), modules);
        Createobject.createBackground(backgrounds, getBackground(), modules);

        // Add mouse motion listener
        addMouseMotionListener(this);
    }

    @Override
    protected void paintComponent(final Graphics gr) {
        super.paintComponent(gr);
        Graphics2D g2d = (Graphics2D) gr;
        if (backgroundImage != null) {
            g2d.drawImage(backgroundImage, 0, 0, getWidth(), getHeight(), null);
        } else {
            // Fill the background with the panel's color if the image is unavailable
            g2d.setColor(getBackground());
            g2d.fillRect(0, 0, getWidth(), getHeight());
        }

        for (Background background : backgrounds) {
            background.draw(g2d);
        }

        for (Animal animal : animals) {
            animal.draw(g2d);
        }
    }

    @Override
    public void actionPerformed(final ActionEvent e) {
        if (e.getSource() == timer) {
            ticksFromStart += 2;
            repaint();
        }
        if (keyHandler.isLeftPressed()) {
            cloud.leftMove();
            repaint();
        }
        if (keyHandler.isRightPressed()) {
            cloud.rightMove();
            repaint();
        }
    }

    @Override
    public void mouseDragged(MouseEvent e) {
        // Optional: Implement if needed
    }

    @Override
    public void mouseMoved(MouseEvent e) {
        for (Animal animal : animals) {
            if (animal instanceof Sparrow) {
                Sparrow sparrow = (Sparrow) animal;
                sparrow.setPosition(e.getX(), e.getY());

                updateAnimalsLookingAt(sparrow);
            }
        }

        repaint();
    }

    private void updateAnimalsLookingAt(Sparrow sparrow) {

        // Update the direction animals are looking at the sparrow
        for (Animal animal : animals) {
            if (animal instanceof Bear) {
                Bear bear = (Bear) animal;
                bear.lookAt(sparrow.getXCenter(), sparrow.getYCenter());
            }
        }
    }


}
У меня ошимбки при создании keyHandler