PIbd-14_Antonova_A.A.__Hard/Drawnings/DrawningTruck.java
2024-05-09 22:23:57 +04:00

224 lines
6.7 KiB
Java

package Drawnings;
import DifferenceOfWheels.*;
import Entities.EntityDumpTruck;
import Entities.EntityTruck;
import java.awt.*;
import java.util.Random;
public class DrawningTruck{
public Entities.EntityTruck EntityTruck;
protected Entities.EntityTruck get_EntityTruck() { return EntityTruck; }
public IOrnaments _ornament = new DrawningWheels();
int CountOfWheels = 0;
int ornament_type;
// Ширина окна
private Integer _pictureWidth;
// Высота окна
private Integer _pictureHeight;
// Левая координата прорисовки автомобиля
protected Integer _startPosX;
// Верхняя кооридната прорисовки автомобиля
protected Integer _startPosY;
// Ширина прорисовки самосвала
private int _drawningTruckWidth = 110; //100
// Высота прорисовки самосвала
private int _drawningTruckHeight = 110; //92
public Integer GetPosX() { return _startPosX;}
public Integer GetPosY() { return _startPosY;}
public Integer GetWidth() { return _drawningTruckWidth;}
public Integer GetHeight() { return _drawningTruckHeight;}
Random random = new Random();
private DrawningTruck()
{
_pictureWidth = null;
_pictureHeight = null;
_startPosX = null;
_startPosY = null;
}
public DrawningTruck(int speed, double weight, Color bodyColor)
{
EntityTruck = new EntityTruck(speed, weight, bodyColor);
CountOfWheels = random.nextInt(2, 5);
ornament_type = random.nextInt(0, 3);
}
protected DrawningTruck(int drawningTruckWidth, int drawningTruckHeight)
{
_drawningTruckWidth = drawningTruckWidth;
_drawningTruckHeight = drawningTruckHeight;
}
protected void SetOrnamentWheel(){
switch (ornament_type){
case 0:
_ornament = new DrawningOrnamentStar();
_ornament.SetCount(CountOfWheels);
break;
case 1:
_ornament = new DrawningOrnamentTriangle();
_ornament.SetCount(CountOfWheels);
break;
case 2:
_ornament = new DrawningOrnamentRectangle();
_ornament.SetCount(CountOfWheels);
break;
}
}
public Boolean SetPictureSize(int width, int height)
{
if (_drawningTruckWidth > width || _drawningTruckHeight > height)
{
return false;
}
if (_pictureWidth != null && width != _pictureWidth || _pictureHeight != null && height != _pictureHeight)
{
if (_startPosX + _drawningTruckWidth > width)
{
_startPosX -= _drawningTruckWidth;
}
if (_startPosY + _drawningTruckHeight > height)
{
_startPosY -= _drawningTruckHeight;
}
}
_pictureWidth = width;
_pictureHeight = height;
return true;
}
public void SetPosition(int x, int y)
{
if (_pictureHeight == null|| _pictureWidth == null)
{
return;
}
if (x < 0)
{
x = 0;
}
if (x + _drawningTruckWidth > _pictureWidth)
{
x = (int)_pictureWidth - _drawningTruckWidth;
}
if (y < 0)
{
y = 0;
}
if (y + _drawningTruckHeight > _pictureHeight)
{
y = (int)_pictureHeight - _drawningTruckHeight;
}
_startPosX = x;
_startPosY = y;
}
public Boolean MoveTransport(DirectionType direction)
{
if (EntityTruck == null || _startPosX == null || _startPosY == null)
{
return false;
}
switch (direction)
{
//влево
case DirectionType.Left:
if (_startPosX - _drawningTruckWidth > 0)
{
_startPosX -= (int)EntityTruck.Step;
}
return true;
//вверх
case DirectionType.Up:
if (_startPosY - _drawningTruckHeight > 0)
{
_startPosY -= (int)EntityTruck.Step;
}
return true;
// вправо
case DirectionType.Right:
if (_startPosX + _drawningTruckWidth + (int)EntityTruck.Step < _pictureWidth)
{
_startPosX += (int)EntityTruck.Step;
}
return true;
//вниз
case DirectionType.Down:
if (_startPosY + _drawningTruckHeight + (int)EntityTruck.Step < _pictureHeight)
{
_startPosY += (int)EntityTruck.Step;
}
return true;
default:
return false;
}
}
public void DrawTransport(Graphics2D g)
{
if (EntityTruck == null || _startPosX == null || _startPosY == null)
{
return;
}
g.setPaint(Color.BLACK);
// Колёса
g.setPaint(Color.BLACK);
SetOrnamentWheel();
if (_ornament != null){
switch (_ornament.get_count_weels()){
case 2:
_ornament.DrawOrnament(g, _startPosX + 5, _startPosY + 67);
_ornament.DrawOrnament(g, _startPosX + 80, _startPosY + 67);
break;
case 3:
_ornament.DrawOrnament(g, _startPosX + 5, _startPosY + 67);
_ornament.DrawOrnament(g, _startPosX + 33, _startPosY + 67);
_ornament.DrawOrnament(g, _startPosX + 80, _startPosY + 67);
break;
case 4:
_ornament.DrawOrnament(g, _startPosX , _startPosY + 67);
_ornament.DrawOrnament(g, _startPosX + 27, _startPosY + 67);
_ornament.DrawOrnament(g, _startPosX + 56, _startPosY + 67);
_ornament.DrawOrnament(g, _startPosX + 83, _startPosY + 67);
break;
}
}
//Границы самосвала
g.drawRect( _startPosX + 5, _startPosY + 53, 100, 13);
g.drawRect( _startPosX + 75, _startPosY + 15, 30, 36);
g.drawRect(_startPosX + 80, _startPosY + 20, 20, 20);
// Кузов
g.setPaint(EntityTruck.BodyColor);
g.fillRect(_startPosX + 5, _startPosY + 53, 100, 13);
g.fillRect(_startPosX + 75, _startPosY + 15, 30, 36);
//Окно
g.setPaint(Color.CYAN);
g.fillRect(_startPosX + 80, _startPosY + 20, 20, 20);
}
}