This commit is contained in:
kirin 2023-12-12 12:31:03 +04:00
parent e7ba53ebcd
commit 5c07ace122
6 changed files with 397 additions and 0 deletions

6
DirectionType.java Normal file
View File

@ -0,0 +1,6 @@
public enum DirectionType {
Up,
Down,
Left,
Right
}

154
DrawningLocomotive.java Normal file
View File

@ -0,0 +1,154 @@
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();
}
}

96
DrawningWheels.java Normal file
View File

@ -0,0 +1,96 @@
import javax.swing.*;
import java.awt.*;
public class DrawningWheels {
private int WheelSz;
JPanel MonorailPanel;
private NumberType WheelsNum;
private Color WheelColor, TireColor;
private int VehicleHeight, VehicleWidth;
public int StartPosX, StartPosY;
public int WheelSz(){
return WheelSz;
}
boolean Init(int width, int height, int startPosX, int startPosY, Color wheelColor, Color tireColor, JPanel monorailPanel){
VehicleWidth = width;
VehicleHeight = height;
StartPosX = startPosX;
WheelColor = wheelColor;
StartPosY = startPosY;
TireColor = tireColor;
MonorailPanel = monorailPanel;
return true;
}
public void ChangeWheelsNumb(int x){
if(x <= 2)
WheelsNum = NumberType.Two;
if(x == 3)
WheelsNum = NumberType.Three;
if(x >= 4)
WheelsNum = NumberType.Four;
}
public NumberType WheelsNumb(){
return WheelsNum;
}
public void DrawWheels(){
Graphics2D g2d = (Graphics2D)MonorailPanel.getGraphics();
int xWheel = StartPosX + 5;
int yWheel = StartPosY + VehicleHeight - 20;
if (WheelsNum == NumberType.Two){
g2d.setColor(WheelColor);
g2d.drawOval(xWheel, yWheel, 20, 20);
g2d.setColor(TireColor);
g2d.fillOval(xWheel, yWheel, 20, 20);
xWheel += 130;
g2d.setColor(WheelColor);
g2d.drawOval(xWheel, yWheel, 20, 20);
g2d.setColor(TireColor);
g2d.fillOval(xWheel, yWheel, 20, 20);
}
else if (WheelsNum == NumberType.Three){
g2d.setColor(WheelColor);
g2d.drawOval(xWheel, yWheel, 20, 20);
g2d.setColor(TireColor);
g2d.fillOval(xWheel, yWheel, 20, 20);
xWheel += 65;
g2d.setColor(WheelColor);
g2d.drawOval(xWheel, yWheel, 20, 20);
g2d.setColor(TireColor);
g2d.fillOval(xWheel, yWheel, 20, 20);
xWheel += 65;
g2d.setColor(WheelColor);
g2d.drawOval(xWheel, yWheel, 20, 20);
g2d.setColor(TireColor);
g2d.fillOval(xWheel, yWheel, 20, 20);
}
else if (WheelsNum == NumberType.Four){
g2d.setColor(WheelColor);
g2d.drawOval(xWheel, yWheel, 20, 20);
g2d.setColor(TireColor);
g2d.fillOval(xWheel, yWheel, 20, 20);
xWheel += 45;
g2d.setColor(WheelColor);
g2d.drawOval(xWheel, yWheel, 20, 20);
g2d.setColor(TireColor);
g2d.fillOval(xWheel, yWheel, 20, 20);
xWheel += 45;
g2d.setColor(WheelColor);
g2d.drawOval(xWheel, yWheel, 20, 20);
g2d.setColor(TireColor);
g2d.fillOval(xWheel, yWheel, 20, 20);
xWheel += 45;
g2d.setColor(WheelColor);
g2d.drawOval(xWheel, yWheel, 20, 20);
g2d.setColor(TireColor);
g2d.fillOval(xWheel, yWheel, 20, 20);
xWheel += 45;
}
}
}

41
EntityLocomotive.java Normal file
View File

@ -0,0 +1,41 @@
import java.awt.*;
class EntityLocomotive {
private int Speed, WheelNum;
private double Weight, Step;
private Color ColorBody, ColorWindow, ColorFillBody, ColorRoga;
public int Speed(){
return Speed;
}
public int WheelNum(){
return WheelNum;
}
public double Weight(){
return Weight;
}
public double Step(){
return Step;
}
public Color ColorBody(){
return ColorBody;
}
public Color ColorWindow(){
return ColorWindow;
}
public Color ColorFillBody(){
return ColorFillBody;
}
public Color ColorRoga() {
return ColorRoga;
}
public void Init(int speed, double weight, int wheelNum, Color colorBody, Color colorWindow, Color colorRoga, Color colorFillBody)
{
Speed = speed;
Weight = weight;
Step = (double)Speed * 100 / Weight;
WheelNum = wheelNum;
ColorBody = colorBody;
ColorWindow = colorWindow;
ColorFillBody = colorFillBody;
ColorRoga = colorRoga;
}
}

95
Main.java Normal file
View File

@ -0,0 +1,95 @@
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.swing.*;
public class Main {
public static void main(String[] args) throws IOException {
JFrame LocomotiveFrame = new JFrame();
JPanel LocomotivePanel = new JPanel();
LocomotiveFrame.setLayout(new BorderLayout());
LocomotiveFrame.setSize(900, 500);
LocomotiveFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
LocomotiveFrame.setLayout(new BorderLayout(1,1));
DrawningLocomotive DrawningLocomotive = new DrawningLocomotive();
LocomotivePanel.setLayout(null);
JButton RightButton = new JButton();
JButton LeftButton = new JButton();
JButton UpButton = new JButton();
JButton DownButton = new JButton();
JButton CreateButton = new JButton();
RightButton.setText("Вправо");
LeftButton.setText("Влево");
UpButton.setText("Вверх");
DownButton.setText("Вниз");
CreateButton.setText("Создать");
CreateButton.setBounds(12, 401, 90, 40);
RightButton.setBounds(840,411,30,30);
LeftButton.setBounds(768,411,30,30);
UpButton.setBounds(804,375,30,30);
DownButton.setBounds(804,411,30,30);
LocomotivePanel.add(CreateButton);
LocomotivePanel.add(RightButton);
LocomotivePanel.add(LeftButton);
LocomotivePanel.add(UpButton);
LocomotivePanel.add(DownButton);
LocomotiveFrame.add(LocomotivePanel, BorderLayout.CENTER);
Random random = new Random();
CreateButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
DrawningLocomotive.Init(random.nextInt(300) + 100, random.nextDouble() * 3000.0 + 1000.0,
LocomotivePanel.getWidth(), LocomotivePanel.getHeight(), random.nextInt(5) + 2,
Color.BLACK,
Color.GREEN,
Color.BLACK,
LocomotivePanel);
DrawningLocomotive.DrawLoco();
}
});
RightButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(DrawningLocomotive.EntityLocomotive() == null)
return;
DrawningLocomotive.MoveTransport(DirectionType.Right);
DrawningLocomotive.DrawLoco();
}
});
LeftButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(DrawningLocomotive.EntityLocomotive() == null)
return;
DrawningLocomotive.MoveTransport(DirectionType.Left);
DrawningLocomotive.DrawLoco();
}
});
UpButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(DrawningLocomotive.EntityLocomotive() == null)
return;
DrawningLocomotive.MoveTransport(DirectionType.Up);
DrawningLocomotive.DrawLoco();
}
});
DownButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(DrawningLocomotive.EntityLocomotive() == null)
return;
DrawningLocomotive.MoveTransport(DirectionType.Down);
DrawningLocomotive.DrawLoco();
}
});
LocomotiveFrame.setVisible(true);
}
}

5
NumberType.java Normal file
View File

@ -0,0 +1,5 @@
public enum NumberType {
Two,
Three,
Four
}