package DoubleDeckerBus.DrawningObjects; import DoubleDeckerBus.DirectionType; import DoubleDeckerBus.Entities.EntityBus; import javax.swing.*; import java.awt.*; import java.util.Random; public class DrawningBus { protected JPanel BusPanel; protected EntityBus EntityBus; private int _pictureWidth; private int _pictureHeight; protected int _startPosX; protected int _startPosY; protected int _busWidth = 160; protected int _busHeight = 100; private IDraw DrawningDoor; public EntityBus EntityBus() { return EntityBus; } public DrawningBus(int speed, double weight, Color bodyColor, int width, int height, JPanel busPanel) { if (width <= _busWidth || height <= _busHeight) { return; } _startPosX = 0; _startPosY = 0; busPanel.setSize(width, height); BusPanel = busPanel; busPanel.paint(BusPanel.getGraphics()); _pictureWidth = width; _pictureHeight = height; EntityBus = new EntityBus(speed, weight, bodyColor); Random random = new Random(); int option = random.nextInt(1,4); if (option == 1) { DrawningDoor = new DrawningDoor(_busWidth, _busHeight, _startPosX, _startPosY, busPanel); } else if (option == 2) { DrawningDoor = new DrawningDoorOval(_busWidth, _busHeight, _startPosX, _startPosY, busPanel); } else if (option == 3) { DrawningDoor = new DrawningDoorTriangle(_busWidth, _busHeight, _startPosX, _startPosY, busPanel); } DrawningDoor.ChangeDoorsNumber(random.nextInt(2, 6)); } protected DrawningBus(int speed, double weight, Color bodyColor, int width, int height, int busWidth, int busHeight, JPanel busPanel) { if (width <= _busWidth || height <= _busHeight) { return; } _startPosX = 0; _startPosY = 0; busPanel.setSize(width, height); BusPanel = busPanel; busPanel.paint(BusPanel.getGraphics()); _pictureWidth = width; _pictureHeight = height; _busWidth = busWidth; _busHeight = busHeight; EntityBus = new EntityBus(speed, weight, bodyColor); Random random = new Random(); int option = random.nextInt(4) + 1; if (option == 1) { DrawningDoor = new DrawningDoor(_busWidth, _busHeight, _startPosX, _startPosY, busPanel); } else if (option == 2) { DrawningDoor = new DrawningDoorOval(_busWidth, _busHeight, _startPosX, _startPosY, busPanel); } else if (option == 3) { DrawningDoor = new DrawningDoorTriangle(_busWidth, _busHeight, _startPosX, _startPosY, busPanel); } DrawningDoor.ChangeDoorsNumber(random.nextInt(2, 6)); } public void SetPosition(int x, int y){ if (x < 0 || x + _busWidth > _pictureWidth) { x = Math.max(0, _pictureWidth - _busWidth); } if (y < 0 || y + _busHeight > _pictureHeight) { y = Math.max(0, _pictureHeight - _busHeight); } _startPosX = x; _startPosY = y; } public int GetPosX() { return _startPosX; } public int GetPosY() { return _startPosY; } public int GetWidth() { return _busWidth; } public int GetHeight() { return _busHeight; } public boolean CanMove(DirectionType direction) { if (EntityBus == null) return false; boolean can = false; switch (direction) { case Left: can = _startPosX - EntityBus.Step() >= 0; break; case Right: can = _startPosX + EntityBus.Step() + _busWidth < _pictureWidth; break; case Down: can = _startPosY + EntityBus.Step() + _busHeight < _pictureHeight; break; case Up: can = _startPosY - EntityBus.Step() >= 0; break; }; return can; } public void MoveTransport(DirectionType direction){ if (!CanMove(direction) || EntityBus == null) { return; } BusPanel.paint(BusPanel.getGraphics()); switch (direction) { case Left: if (_startPosX - EntityBus.Step() >= 0) { _startPosX -= (int) EntityBus.Step(); } break; case Up: if (_startPosY - EntityBus.Step() >= 0) { _startPosY -= (int) EntityBus.Step(); } break; case Right: if (_startPosX + EntityBus.Step() + _busWidth <= _pictureWidth) { _startPosX += (int) EntityBus.Step(); } break; case Down: if (_startPosY + EntityBus.Step() + _busHeight <= _pictureHeight) { _startPosY += (int) EntityBus.Step(); } break; } DrawningDoor.ChangeX(_startPosX); DrawningDoor.ChangeY(_startPosY); } public void DrawTransport() { Graphics2D g2d = (Graphics2D)BusPanel.getGraphics(); if (EntityBus == null) { return; } // Границы первого этажа автобуса g2d.setColor(EntityBus.BodyColor()); g2d.fillRect(_startPosX + 40, _startPosY + 40, 120, 40); // Колеса g2d.setColor(Color.black); g2d.fillOval(_startPosX + 45, _startPosY + 75, 20, 20); g2d.fillOval(_startPosX + 127, _startPosY + 75, 20, 20); // Окна g2d.setColor(Color.blue); g2d.fillOval(_startPosX + 50, _startPosY + 45, 20, 20); g2d.fillOval(_startPosX + 75, _startPosY + 45, 20, 20); g2d.fillOval(_startPosX + 100, _startPosY + 45, 20, 20); g2d.fillOval(_startPosX + 125, _startPosY + 45, 20, 20); // Двери DrawningDoor.DrawDoors(); } }