PIbd-22_Chernyshev_G.J._29_.../Trolleybus/DrawingBus.java
2023-11-24 20:37:08 +03:00

193 lines
6.1 KiB
Java

package Trolleybus;
import java.awt.*;
import java.util.Random;
public class DrawingBus {
public EntityBus EntityBus;
public EntityBus getEntityBus() {return EntityBus;}
private void setEntityBus(EntityBus entityBus) {EntityBus = entityBus;}
public IDrawingDoors Doors;
// Ширина окна
private int _pictureWidth;
// Высота окна
private int _pictureHeight;
// Левая координата прорисовки автобуса
protected int _startPosX;
// Верхняя координата прорисовки автобуса
protected int _startPosY;
// Ширина прорисовки автобуса
protected int _busWidth = 150;
// Высота прорисовки автобуса
protected int _busHeight = 95;
public int GetPosX() {
return _startPosX;
}
public int GetPosY() {
return _startPosY;
}
public int GetWidth() {
return _busWidth;
}
public int GetHeight() {
return _busHeight;
}
public DrawingBus(int speed, double weight, Color bodyColor, int width, int height) {
if (width < _busWidth || height < _busHeight) {
return;
}
_startPosX = 0;
_startPosY = 0;
_pictureWidth = width;
_pictureHeight = height;
EntityBus = new EntityBus(speed, weight, bodyColor);
Random random = new Random();
//Форма дверей
int shape = random.nextInt(1,4);
switch (shape) {
case 1:
Doors = new DrawingDoors();
break;
case 2:
Doors = new DrawingOvalDoors();
break;
case 3:
Doors = new DrawingTriangleDoors();
break;
}
//Количество дверей
Doors.SetCntOfDoors(random.nextInt(3, 6));
}
protected DrawingBus(int speed, double weight, Color bodyColor, int width, int height, int busWidth, int busHeight) {
if (width <= _busWidth || height <= _busHeight) {
return;
}
_startPosX = 0;
_startPosY = 0;
_pictureWidth = width;
_pictureHeight = height;
_busWidth = busWidth;
_busHeight = busHeight;
EntityBus = new EntityBus(speed, weight, bodyColor);
Random random = new Random();
//Форма дверей
int shape = random.nextInt(1,4);
switch (shape) {
case 1:
Doors = new DrawingDoors();
break;
case 2:
Doors = new DrawingOvalDoors();
break;
case 3:
Doors = new DrawingTriangleDoors();
break;
}
//Количество дверей
Doors.SetCntOfDoors(random.nextInt(3, 6));
}
//Конструктор для усложнённой 3
public DrawingBus(EntityBus bus, IDrawingDoors doors, int width, int height) {
if (width < _busWidth || height < _busHeight) {
return;
}
_startPosX = 0;
_startPosY = 0;
_pictureWidth = width;
_pictureHeight = height;
EntityBus = bus;
Doors = doors;
}
public void SetPosition(int x, int y){
_startPosX = Math.min(Math.max(x, 0), _pictureWidth - _busWidth);
_startPosY = Math.min(Math.max(y, 0), _pictureHeight - _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;
}
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;
}
}
public void DrawTransport(Graphics g) {
if (EntityBus == null) {
return;
}
Graphics2D g2d = (Graphics2D)g;
//Корпус
g2d.setColor(EntityBus.getBodyColor());
g2d.drawLine(_startPosX, _startPosY + 30, _startPosX, _startPosY + 80);
g2d.drawLine(_startPosX, _startPosY + 80, _startPosX + 20, _startPosY + 80);
g2d.drawLine(_startPosX + 45, _startPosY + 80, _startPosX + 105, _startPosY + 80);
g2d.drawLine(_startPosX + 130, _startPosY + 80, _startPosX + 150, _startPosY + 80);
g2d.drawLine(_startPosX + 150, _startPosY + 80, _startPosX + 150, _startPosY + 30);
g2d.drawLine(_startPosX + 150, _startPosY + 30, _startPosX, _startPosY + 30);
//Колёса
g2d.setColor(Color.BLACK);
g2d.drawOval(_startPosX + 20, _startPosY + 70, 25, 25);
g2d.drawOval(_startPosX + 105, _startPosY + 70, 25, 25);
//Двери
Doors.DrawDoors(g2d, EntityBus.getBodyColor(), _startPosX, _startPosY);
}
public IMoveableObject GetMoveableObject(){
return new DrawingObjectBus(this);
}
}