import java.awt.*; public class DrawingArtillery { protected EntityArtillery artillery; protected DrawingRollers drawingRollers; protected float _startPosX; protected float _startPosY; private Integer _pictureWidth = null; private Integer _pictureHeight = null; protected int _artilleryWidth = 80; protected int _artilleryHeight = 50; public EntityArtillery getArtillery() { return artillery; } public DrawingArtillery(int speed, float weight, Color bodyColor, int rollersCount) { artillery = new EntityArtillery(speed, weight, bodyColor); drawingRollers = new DrawingRollers(rollersCount, bodyColor); } protected DrawingArtillery(int speed, float weight, Color bodyColor, int rollersCount, int artilleryWidth, int artilleryHeight) { this(speed, weight, bodyColor, rollersCount); _artilleryWidth = artilleryWidth; _artilleryHeight = artilleryHeight; } public void SetPosition(int x, int y, int width, int height) { if (x < 0 || x + _artilleryWidth >= width) { return; } if (y < 0 || y + _artilleryHeight >= height) { return; } _startPosX = x; _startPosY = y; _pictureWidth = width; _pictureHeight = height; } public void moveTransport(Direction direction) { if (_pictureWidth == null || _pictureHeight == null) { return; } switch (direction) { case Right: if (_startPosX + _artilleryWidth + artillery.getStep() < _pictureWidth) { _startPosX += artillery.getStep(); } break; case Left: if (_startPosX - artillery.getStep() >= 0) { _startPosX -= artillery.getStep(); } break; case Up: if (_startPosY - artillery.getStep() >= 0) { _startPosY -= artillery.getStep(); } break; case Down: if (_startPosY + _artilleryHeight + artillery.getStep() < _pictureHeight) { _startPosY += artillery.getStep(); } break; } } public void drawTransport(Graphics2D g) { if (_startPosX < 0 || _startPosY < 0 || _pictureHeight == null || _pictureWidth == null) { return; } g.setColor(artillery.getBodyColor() != null ? artillery.getBodyColor() : Color.BLACK); g.fillRect((int) (_startPosX + _artilleryWidth / 8 * 2), (int) _startPosY, _artilleryWidth / 8 * 4, _artilleryHeight / 5); g.fillRect((int) _startPosX, (int) (_startPosY + _artilleryHeight / 5), _artilleryWidth, _artilleryHeight / 3); g.setColor(Color.GRAY); g.fillOval((int) _startPosX, (int) (_startPosY + _artilleryHeight * 2 / 5), _artilleryWidth, _artilleryHeight * 2 / 5); drawingRollers.draw(g, (int) _startPosX, (int) _startPosY, _artilleryWidth, _artilleryHeight); } public void changeBorders(int width, int height) { _pictureWidth = width; _pictureHeight = height; if (_pictureWidth <= _artilleryWidth || _pictureHeight <= _artilleryHeight) { _pictureWidth = null; _pictureHeight = null; return; } if (_startPosX + _artilleryWidth > _pictureWidth) { _startPosX = _pictureWidth - _artilleryWidth; } if (_startPosY + _artilleryHeight > _pictureHeight) { _startPosY = _pictureHeight - _artilleryHeight; } } public float[] getCurrentPosition() { return new float[] { _startPosX, _startPosX + _artilleryWidth - 1, _startPosY, _startPosY + _artilleryHeight -1 }; } }