import animals.Animal; import bear.Bear; import bird.Sparrow; import…
- User
import animals.Animal; import bear.Bear; import bird.Sparrow;
import javax.imageio.ImageIO; import javax.swing.; 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.io.File; import java.io.IOException; import java.util.ArrayList;
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 animals; // Список животных
public DrawPanel(final int width, final int height, final int timerDelay) { this.PANEL_WIDTH = width; this.PANEL_HEIGHT = height; this.TIMER_DELAY = timerDelay; timer = new Timer(timerDelay, this); timer.start(); setBackground(Color.green); animals = new ArrayList<>(); // Инициализируем список животных // Создаем животных и добавляем их в список Createobject.createAnimals(animals, getBackground()); // Добавляем слушатель движения мыши 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 { // Заполняем фон цветом панели, если изображение недоступно g2d.setColor(getBackground()); g2d.fillRect(0, 0, getWidth(), getHeight()); } for (Animal animal : animals) { animal.draw(g2d); } } @Override public void actionPerformed(final ActionEvent e) { if (e.getSource() == timer) { repaint(); } } @Override public void mouseDragged(MouseEvent e) { } @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); } /*else if (animal instanceof Bear) { Bear bear = (Bear) animal; }*/ } repaint(); } private void updateAnimalsLookingAt(Sparrow sparrow) { // Обновляем направление взгляда животных на воробья for (Animal animal : animals) { if (animal instanceof Bear) { Bear bear = (Bear) animal; bear.lookAt(sparrow.getXCenter(), sparrow.getYCenter()); } } }} Мне необходимо создать конструктор, для того, чтобы пользователь имел возможность добавлять различные модули в мой код, не изменяя мой основной код. Класс Createobject имеет вид: public static void createAnimals(ArrayList animals, Color background) { // Пример создания нескольких животных и добавления их в список
Color bearPrimaryColor = new Color(0x96765d); Color bearSecondaryColor = new Color(0x7C3100); Bear bear = new Bear(260, 150, 150, 250, Color.PINK, bearPrimaryColor, background); animals.add(bear); Sparrow sparrow = new Sparrow(350, 150, 50, 40, bearSecondaryColor, Color.PINK); animals.add(sparrow); bear.lookAt(sparrow.getXCenter(), sparrow.getYCenter()); } - Assistant