132 lines
4.5 KiB
Java
Raw Normal View History

2023-11-18 19:17:07 +04:00
import javax.swing.*;
import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.awt.geom.RoundRectangle2D;
public class DrawningTank {
JPanel TankPanel;
private EntityTank EntityTank;
private int _pictureWidth;
private int _pictureHeight;
private int _startPosX = 0;
private int _startPosY = 0;
private int _tankWidth = 150;
private int _tankHeight = 100;
private DrawningWheels DrawningWheels;
public EntityTank EntityTank(){
return EntityTank;
}
public boolean Init(int speed, double weight, Color bodyColor, Color additionalColor, int wheelQuantity,
int width, int height, boolean isAntiAircraftGun, boolean isTankTower, JPanel tankPanel){
if(width <= _tankWidth || height <= _tankHeight)
return false;
tankPanel.setSize(width, height);
TankPanel = tankPanel;
tankPanel.paint(TankPanel.getGraphics());
_pictureWidth = width;
_pictureHeight = height;
EntityTank = new EntityTank();
EntityTank.Init(speed, weight, bodyColor, additionalColor, isAntiAircraftGun, isTankTower);
DrawningWheels = new DrawningWheels();
DrawningWheels.Init(_tankWidth, _tankHeight, _startPosX, _startPosY, additionalColor, tankPanel);
DrawningWheels.ChangeWheelsQuantity(wheelQuantity);
return true;
}
public void SetPosition(int x, int y){
if (EntityTank == null)
return;
_startPosX = x;
_startPosY = y;
if (x + _tankWidth >= _pictureWidth || y + _tankHeight >= _pictureHeight){
_startPosX = 0;
_startPosY = 0;
}
DrawningWheels.currentX = _startPosX;
DrawningWheels.currentY = _startPosY;
}
public void MoveTransport(DirectionType direction){
if (EntityTank == null)
return;
TankPanel.paint(TankPanel.getGraphics());
switch (direction)
{
case Left:
if (_startPosX - EntityTank.Step() >= 0)
_startPosX -= (int)EntityTank.Step();
else
_startPosX = 0;
break;
case Up:
if (_startPosY - EntityTank.Step() >= 0)
_startPosY -= (int)EntityTank.Step();
else
_startPosY = 0;
break;
case Right:
if (_startPosX + EntityTank.Step() + _tankWidth < _pictureWidth)
_startPosX += (int)EntityTank.Step();
else
_startPosX = _pictureWidth - _tankWidth;
break;
case Down:
if (_startPosY + EntityTank.Step() + _tankHeight < _pictureHeight)
_startPosY += (int)EntityTank.Step();
else
_startPosY = _pictureHeight - _tankHeight;
break;
}
DrawningWheels.currentX = _startPosX;
DrawningWheels.currentY = _startPosY;
}
public void DrawTank() {
Graphics2D g2d = (Graphics2D) TankPanel.getGraphics();
if (EntityTank == null) return;
g2d.setColor(EntityTank.BodyColor());
// гусеница
RoundRectangle2D caterpillars = new RoundRectangle2D.Double(_startPosX, _startPosY + 65, _tankWidth, 35, 20, 20);
g2d.draw(caterpillars);
// колеса
DrawningWheels.DrawWheels();
// дуло
if (EntityTank.IsTankTower()) {
g2d.fillRect(_startPosX + 15, _startPosY + 37, 30, 7);
}
// башня
g2d.setColor(EntityTank.AdditionalColor());
g2d.fillRect(_startPosX + 45, _startPosY + 30, 60, 25);
g2d.fillRect(_startPosX + 5, _startPosY + 55 + 1, _tankWidth - 10, 9);
if (EntityTank.IsAntiAirforceGun()) {
// зенитное орудие
g2d.drawRect(_startPosX + 65, _startPosY + 18, 8, 12);
g2d.drawRect(_startPosX + 65 + 8, _startPosY + 16, 8, 14);
int[] xLeft = {_startPosX + 52, _startPosX + 67, _startPosX + 67 + 5, _startPosX + 57};
int[] yLeft = {_startPosY + 5, _startPosY + 18, _startPosY + 18, _startPosY + 5};
g2d.drawPolygon(xLeft, yLeft, xLeft.length);
int[] xRight = {_startPosX + 59, _startPosX + 74, _startPosX + 74 + 5, _startPosX + 66};
int[] yRight = {_startPosY, _startPosY + 16, _startPosY + 16, _startPosY};
g2d.drawPolygon(xRight, yRight, xRight.length);
}
}
}