PIbd-21_KozyrevSS_GasolineT.../DrawingTanker.java

102 lines
3.0 KiB
Java

import java.awt.*;
public class DrawingTanker {
protected BaseTanker GasolineTanker;
public BaseTanker GetGasolineTanker() {return GasolineTanker;}
private IWheelDraw wheelsDrawing;
protected int _pictureWidth;
protected int _pictureHeight;
protected int _startPosX;
protected int _startPosY;
protected final int _carWidth = 110;
protected final int _carHeight = 120;
public int GetPosX() {return _startPosX;}
public int GetPosY() {return _startPosY;}
public int GetWidth() {return _carWidth;}
public int GetHeight() {return _carHeight;}
public boolean CanMove(Direction direction)
{
if (GasolineTanker == null)
return false;
switch (direction)
{
case Left -> {
return _startPosX - GasolineTanker.Step > 0; }
case Up -> {
return _startPosY - GasolineTanker.Step > 0; }
case Right -> {
return _startPosX + _carWidth + GasolineTanker.Step < _pictureWidth; }
case Down -> {
return _startPosY + _carHeight + GasolineTanker.Step < _pictureHeight; }
default -> {
return false; }
}
}
public DrawingTanker(int speed, double weight, Color bodyColor, int width, int height, int wheelCount, int wheelMode)
{
_pictureHeight = height;
_pictureWidth = width;
GasolineTanker = new BaseTanker(speed, weight, bodyColor);
int mode = wheelMode % 3;
switch (mode)
{
case 0 -> {wheelsDrawing = new DrawWheelCircle();}
case 1 -> {wheelsDrawing = new DrawWheelSquare();}
case 2 -> {wheelsDrawing = new DrawWheelClassic();}
}
wheelsDrawing.setWheelCount(wheelCount);
}
public void SetPosition(int x, int y)
{
_startPosX = x;
_startPosY = y;
}
public void MoveTransport(Direction direction)
{
if (!CanMove(direction) || GasolineTanker == null)
return;
switch (direction)
{
case Left:
{
_startPosX -= (int)GasolineTanker.Step;
}
break;
case Up:
{
_startPosY -= (int)GasolineTanker.Step;
}
break;
case Right:
{
_startPosX += (int)GasolineTanker.Step;
}
break;
case Down:
{
_startPosY += (int)GasolineTanker.Step;
}
break;
}
}
public void DrawTransport(Graphics g)
{
if (GasolineTanker == null)
return;
var g2d = (Graphics2D) g;
g2d.setColor(GasolineTanker.getBodyColor());
// Отрисовка корпуса
g2d.fillRect(10 + _startPosX, 40 + _startPosY, 90, 20);
g2d.fillRect(80 + _startPosX, 10 + _startPosY, 20, 40);
// Отрисовка колесиков
wheelsDrawing.DrawWheels(_startPosX, _startPosY, GasolineTanker.getBodyColor(), g2d);
}
}