132 lines
5.0 KiB
Java
Raw Permalink Normal View History

import javax.swing.*;
import java.awt.*;
import java.util.Random;
public class DrawningLiner extends JPanel {
public EntityLiner EntityLiner;
public DrawningDeck _deck = null;
private Integer picture_width;
private Integer picture_height;
private Integer _StartPosX;
private Integer _StartPosY;
2024-05-15 05:36:07 +04:00
private int drawningLinerWidth = 160;
private int drawningLinerHeight = 120;
public void Init(int speed, double weight, Color bodycolor, Color additionalcolor, boolean pipe, boolean anchor) {
EntityLiner = new EntityLiner();
EntityLiner.Init(speed, weight, bodycolor, additionalcolor, pipe, anchor);
picture_width = null;
picture_height = null;
_StartPosX = null;
_StartPosY = null;
_deck = new DrawningDeck();
Random rand = new Random();
int randomNum = rand.nextInt(3);
_deck.SetCount(randomNum);
}
public boolean SetPictureSize(int width, int height) {
2024-05-15 05:36:07 +04:00
if (width < drawningLinerWidth || height < drawningLinerHeight)
return false;
picture_width = width;
picture_height = height;
if (_StartPosX != null || _StartPosY != null) {
2024-05-15 05:36:07 +04:00
if (_StartPosX + drawningLinerWidth > picture_width) {
_StartPosX = _StartPosX - (_StartPosX + drawningLinerWidth - picture_width);
} else if (_StartPosX < 0)
_StartPosX = 0;
2024-05-15 05:36:07 +04:00
if (_StartPosY + drawningLinerHeight > picture_height) {
_StartPosY = _StartPosY - (_StartPosY + drawningLinerHeight - picture_height);
} else if (_StartPosY < 0)
_StartPosY = 0;
}
return true;
}
public void SetPosition(int x, int y) {
if (!(picture_width != null && picture_height != null))
return;
2024-05-15 05:36:07 +04:00
if (x + drawningLinerWidth > picture_width) {
_StartPosX = x - (x + drawningLinerWidth - picture_width);
} else if (x < 0)
_StartPosX = 0;
else
_StartPosX = x;
2024-05-15 05:36:07 +04:00
if (y + drawningLinerHeight > picture_height) {
_StartPosY = y - (y + drawningLinerHeight - picture_height);
} else if (y < 0)
_StartPosY = 0;
else
_StartPosY = y;
}
public boolean MoveTransport(DirectionType direction) {
if (EntityLiner == null || _StartPosX == null || _StartPosY == null)
return false;
switch (direction) {
case Left:
2024-05-15 05:36:07 +04:00
if (_StartPosX - EntityLiner.Step > -40) {
_StartPosX -= (int) EntityLiner.Step;
}
return true;
case Up:
2024-05-15 05:36:07 +04:00
if (_StartPosY - EntityLiner.Step > -40) {
_StartPosY -= (int) EntityLiner.Step;
}
return true;
case Right:
2024-05-15 05:36:07 +04:00
if (_StartPosX + drawningLinerWidth + (int) EntityLiner.Step < picture_width
- EntityLiner.Step) {
_StartPosX += (int) EntityLiner.Step;
}
return true;
case Down:
2024-05-15 05:36:07 +04:00
if (_StartPosY + drawningLinerHeight + (int) EntityLiner.Step < picture_height
- EntityLiner.Step) {
_StartPosY += (int) EntityLiner.Step;
}
return true;
default:
return false;
}
}
public void DrawTransport(Graphics2D g) {
if (EntityLiner == null) {
return;
}
;
g.setColor(EntityLiner.getAdditionalColor());
_deck.Draw(g, _StartPosX, _StartPosY);
g.setColor(EntityLiner.getBodyColor());
int[] xPoints = { _StartPosX + 40, _StartPosX + 60, _StartPosX + 60 };
int[] yPoints = { _StartPosY + 80, _StartPosY + 80, _StartPosY + 120 };
g.drawPolygon(xPoints, yPoints, 3);
g.fillPolygon(xPoints, yPoints, 3);
xPoints = new int[] { _StartPosX + 40 + 100, _StartPosX + 60 + 100, _StartPosX + 40 + 100 };
yPoints = new int[] { _StartPosY + 80, _StartPosY + 80, _StartPosY + 120 };
g.drawPolygon(xPoints, yPoints, 3);
g.fillPolygon(xPoints, yPoints, 3);
g.drawRect(_StartPosX + 60, _StartPosY + 80, 80, 40);
g.fillRect(_StartPosX + 60, _StartPosY + 80, 80, 40);
if (EntityLiner.getAnchor()) {
g.setColor(EntityLiner.getAdditionalColor());
g.drawLine(_StartPosX + 70, _StartPosY + 85, _StartPosX + 70, _StartPosY + 110);
g.drawLine(_StartPosX + 60, _StartPosY + 90, _StartPosX + 80, _StartPosY + 90);
g.drawLine(_StartPosX + 60, _StartPosY + 100, _StartPosX + 70, _StartPosY + 110);
g.drawLine(_StartPosX + 70, _StartPosY + 110, _StartPosX + 80, _StartPosY + 100);
}
// отрисовка шлюпок
if (EntityLiner.getBoats()) {
g.setColor(EntityLiner.getAdditionalColor());
g.fillOval(_StartPosX + 80, _StartPosY + 80, 30, 20);
g.fillOval(_StartPosX + 120, _StartPosY + 80, 30, 20);
}
}
}