PIbd-23_Vrazhkin_S_A_Electr.../DrawningLocomotive.java
2023-12-12 12:31:03 +04:00

155 lines
5.7 KiB
Java

import java.awt.*;
import java.util.Random;
import javax.swing.*;
public class DrawningLocomotive {
JPanel LocomotivePanel;
private EntityLocomotive EntityLocomotive;
private int pictureWidth;
private int pictureHeight;
private int startPosX;
private int startPosY;
private final int vehicleWidth = 170;
private final int vehicleHeight = 110;
private DrawningWheels DrawningWheels;
protected int wheelSz;
public EntityLocomotive EntityLocomotive(){
return EntityLocomotive;
}
public boolean Init(int speed, double weight, int width, int height, int wheelNum,
Color colorBody, Color colorWindow,
Color colorRoga, JPanel locomotivePanel) {
if (width <= vehicleWidth || height <= vehicleHeight) {
return false;
}
startPosX = 0;
startPosY = 0;
locomotivePanel.setSize(width, height);
LocomotivePanel = locomotivePanel;
locomotivePanel.paint(LocomotivePanel.getGraphics());
pictureWidth = width;
pictureHeight = height;
EntityLocomotive = new EntityLocomotive();
EntityLocomotive.Init(speed, weight, wheelNum, colorBody, colorWindow, colorRoga, LocomotivePanel.getBackground());
DrawningWheels = new DrawningWheels();
DrawningWheels.Init(vehicleWidth, vehicleHeight,startPosX,startPosY,Color.BLACK,Color.WHITE,locomotivePanel);
Random rand = new Random();
DrawningWheels.ChangeWheelsNumb(rand.nextInt(8)+2);
return true;
}
public void SetPosition(int x, int y) {
if (EntityLocomotive == null)
return;
startPosX = x;
startPosY = y;
if (x + vehicleWidth >= pictureWidth || y + vehicleHeight >= pictureHeight) {
startPosX = 1;
startPosY = 1;
}
}
public void MoveTransport(DirectionType direction) {
if (EntityLocomotive == null)
return;
LocomotivePanel.paint(LocomotivePanel.getGraphics());
switch (direction) {
case Left:
if (startPosX - EntityLocomotive.Step() >= 0)
startPosX -= (int)EntityLocomotive.Step();
else
startPosX = 0;
break;
case Up:
if (startPosY - EntityLocomotive.Step() >= 0)
startPosY -= (int)EntityLocomotive.Step();
else
startPosY = 0;
break;
case Right:
if (startPosX + EntityLocomotive.Step() + vehicleWidth < pictureWidth)
startPosX += (int)EntityLocomotive.Step();
else
startPosX = pictureWidth - vehicleWidth;
break;
case Down:
if (startPosY + EntityLocomotive.Step() + vehicleHeight < pictureHeight)
startPosY += (int)EntityLocomotive.Step();
else
startPosY = pictureHeight - vehicleHeight;
break;
}
DrawningWheels.StartPosX = startPosX;
DrawningWheels.StartPosY = startPosY;
}
public void DrawLoco() {
if (EntityLocomotive == null)
return;
Graphics2D g = (Graphics2D)LocomotivePanel.getGraphics();
Color colorBody = EntityLocomotive.ColorBody();
Color colorWindow = EntityLocomotive.ColorWindow();
Color colorFillBody = EntityLocomotive.ColorFillBody();
Color colorRoga = EntityLocomotive.ColorRoga();
// Body
g.setColor(colorBody);
g.drawRect(startPosX, startPosY + vehicleHeight - 50, vehicleWidth - 10, vehicleHeight - 80);
int[] xUpBodyPoints = {
startPosX,
startPosX + 10,
startPosX + 150,
startPosX + 160
};
int[] yUpBodyPoints = {
startPosY + 60,
startPosY + 30,
startPosY + 30,
startPosY + 60
};
g.drawPolygon(xUpBodyPoints, yUpBodyPoints, xUpBodyPoints.length);
// Roga
g.setColor(colorRoga);
g.drawLine(startPosX + vehicleWidth / 2, startPosY + 30, startPosX + vehicleWidth / 2 + 10,
startPosY + 15);
g.drawLine(startPosX + vehicleWidth / 2 + 10, startPosY + 15, startPosX + vehicleWidth / 2,
startPosY);
// Door
g.setColor(colorBody);
g.drawRect(startPosX + vehicleWidth / 2 - 15, startPosY + 45, vehicleWidth / 10, vehicleHeight / 2 - 10);
g.setColor(colorFillBody);
g.fillRect(startPosX + vehicleWidth / 2 - 14, startPosY + 46, vehicleWidth / 10 - 1, vehicleHeight / 2 - 12);
// Windows
g.setColor(colorWindow);
int xWindow = startPosX + 15;
int yWindow = startPosY + 37;
for (int i = 0; i < 2; i++) {
g.drawRect(xWindow, yWindow, 15, 15);
xWindow += 25;
}
xWindow += 35;
for (int i = 0; i < 2; i++) {
g.drawRect(xWindow, yWindow, 15, 15);
xWindow += 25;
}
// Battery
g.setColor(colorBody);
int[] xbatteryPoints = {startPosX + vehicleWidth - 10,
startPosX + vehicleWidth,
startPosX + vehicleWidth,
startPosX + vehicleWidth - 10};
int[] yBatteryPoints = {
startPosY + vehicleHeight - 25,
startPosY + vehicleHeight - 20,
startPosY + vehicleHeight - 55,
startPosY + vehicleHeight - 50
};
g.fillPolygon(xbatteryPoints, yBatteryPoints, xbatteryPoints.length);
DrawningWheels.DrawWheels();
}
}