Pibd-14_Boyko_M.S._ElectroT.../ProjectElectroTrans/Drawnings/DrawningTrans.java
2024-04-25 10:39:26 +04:00

149 lines
5.2 KiB
Java

package Drawnings;
import Entities.*;
import MovementStrategy.*;
import java.awt.*;
import java.util.Random;
public class DrawningTrans {
private final EntityTrans entityTran;
public EntityTrans getEntityTrans() {
return entityTran;
}
private Integer _pictureWidth;
private Integer _pictureHeight;
protected Integer _startPosX;
protected Integer _startPosY;
private int _drawingTransWidth = 110;
private int _drawingTransHeight = 60;
public int GetPosX(){return _startPosX;}
public int GetPosY(){return _startPosY;}
public int GetWidth(){return _drawingTransWidth;}
public int GetHeight(){return _drawingTransHeight;}
private IDrawWheels drawWheels;
public DrawningTrans(int speed, float weight, Color bodyColor, int wheelsType) {
entityTran = new EntityTrans(speed, weight, bodyColor);
_startPosY = null;
_startPosX = null;
_pictureWidth = null;
_pictureHeight = null;
switch (wheelsType) {
case 0:
drawWheels = new DrawningWheels();
break;
case 1:
drawWheels = new DrawningTriangleWheels();
break;
case 2:
drawWheels = new DrawningRectWheels();
break;
}
Random random = new Random();
int wheelsCount = random.nextInt(1, 4);
System.out.print(wheelsCount);
drawWheels.setNumber(wheelsCount);
}
protected DrawningTrans(int speed, float weight, Color bodyColor, int wheelsType, int transWidth, int transHeight) {
this(speed, weight, bodyColor, wheelsType);
_drawingTransHeight = transHeight;
_drawingTransWidth = transWidth;
}
public void setPosition(int x, int y) {
if (_pictureHeight == null || _pictureWidth == null)
return;
_startPosX = x;
_startPosY = y;
if (_drawingTransWidth + x > _pictureWidth || x < 0) {
_startPosX = 0;
}
if (_drawingTransHeight + y > _pictureHeight || y < 0) {
_startPosY = 0;
}
}
public boolean setPictureSize(int width, int height) {
if (_drawingTransHeight > height || _drawingTransWidth > width)
return false;
_pictureHeight = height;
_pictureWidth = width;
if (_startPosX != null && _startPosY != null)
{
if (_startPosX + _drawingTransWidth > width)
_startPosX = width - _drawingTransWidth;
if (_startPosY + _drawingTransHeight > height)
_startPosY = height - _drawingTransHeight;
}
return true;
}
public boolean moveTransport(MovementDirection direction) {
if (entityTran == null || _pictureWidth == null || _pictureHeight == null)
return false;
switch (direction) {
case MovementDirection.Left:
if (_startPosX - entityTran.getStep() > 0)
_startPosX -= (int) entityTran.getStep();
return true;
case MovementDirection.Up:
if (_startPosY - entityTran.getStep() > 0)
_startPosY -= (int) entityTran.getStep();
return true;
case MovementDirection.Right:
if (_startPosX + entityTran.getStep() < _pictureWidth - _drawingTransWidth)
_startPosX += (int) entityTran.getStep();
return true;
case MovementDirection.Down:
if (_startPosY + entityTran.getStep() < _pictureHeight - _drawingTransHeight)
_startPosY += (int) entityTran.getStep();
return true;
default:
return false;
}
}
public void drawTrans(Graphics g) {
if (entityTran == null || _startPosX == null || _startPosY == null) {
return;
}
Graphics2D g2d = (Graphics2D) g;
Point[] electroTransBorders = new Point[]{
new Point(_startPosX, _startPosY + 30),
new Point(_startPosX + 10, _startPosY + 10),
new Point(_startPosX + 70, _startPosY + 10),
new Point(_startPosX + 80, _startPosY + 30),
new Point(_startPosX + 80, _startPosY + 50),
new Point(_startPosX, _startPosY + 50),
};
Polygon electroTransPolygon = new Polygon();
for (Point point : electroTransBorders)
electroTransPolygon.addPoint(point.x, point.y);
g2d.setColor(entityTran.getBodyColor());
g2d.drawPolygon(electroTransPolygon);
Point[] electroTransGlass = new Point[]{
new Point(_startPosX + 2, _startPosY + 30),
new Point(_startPosX + 10, _startPosY + 13),
new Point(_startPosX + 70, _startPosY + 13),
new Point(_startPosX + 78, _startPosY + 30),
};
Polygon electroTransGlassPolygon = new Polygon();
for (Point point : electroTransGlass)
electroTransGlassPolygon.addPoint(point.x, point.y);
g2d.setColor(entityTran.getBodyColor());
g2d.drawPolygon(electroTransGlassPolygon);
drawWheels.drawWheels(g2d, entityTran.getBodyColor(), _startPosX, _startPosY);
}
}