diff --git a/ProjectElectricLocomotive/DrawingElectricLocomotive.java b/ProjectElectricLocomotive/DrawingElectricLocomotive.java new file mode 100644 index 0000000..c0ddbd2 --- /dev/null +++ b/ProjectElectricLocomotive/DrawingElectricLocomotive.java @@ -0,0 +1,169 @@ +package ProjectElectricLocomotive; +import java.awt.*; +public class DrawingElectricLocomotive { + public EntityElectricLocomotive EntityElectricLocomotive; + + private int _pictureWidth; + private int _pictureHeight; + private int _startPosX; + private int _startPosY; + private final int _locoWidth = 150; + private final int _locoHeight = 50; + + public boolean Init(int speed, double weight, Color bodyColor, Color additionalColor, + boolean horns, boolean seifbatteries, int width, int height) + { + + if (width < _locoWidth || height < _locoHeight) + { + return false; + } + _pictureWidth = width; + _pictureHeight = height; + EntityElectricLocomotive = new EntityElectricLocomotive(); + EntityElectricLocomotive.Init(speed, weight, bodyColor, additionalColor, horns, seifbatteries); + return true; + } + + public void SetPosition(int x, int y) + { + if (x < 0 || x + _locoWidth > _pictureWidth) + { + x = 20; + } + if (y < 0 || y + _locoHeight > _pictureHeight) + { + y = 20; + } + _startPosX = x; + _startPosY = y; + } + + public void MoveTransport(DyrectionType direction){ + if(EntityElectricLocomotive == null) return; + switch(direction) + { + case Up -> { + if(_startPosY - EntityElectricLocomotive.Step() >= 0) + _startPosY -= (int) EntityElectricLocomotive.Step(); + } + case Down -> { + if(_startPosY + EntityElectricLocomotive.Step() + _locoHeight <= _pictureHeight) + _startPosY += (int) EntityElectricLocomotive.Step(); + } + case Left -> { + if(_startPosX - EntityElectricLocomotive.Step() >= 0) + _startPosX -= (int) EntityElectricLocomotive.Step(); + } + case Right -> { + if(_startPosX + EntityElectricLocomotive.Step() + _locoWidth <= _pictureWidth) + _startPosX += (int) EntityElectricLocomotive.Step(); + } + } + } + public void DrawTransport(Graphics g) { + if (EntityElectricLocomotive == null) { + return; + } + + Graphics2D g2d = (Graphics2D) g; + + Color bodyColor = EntityElectricLocomotive.BodyColor; + Color additionalColor = EntityElectricLocomotive.AdditionalColor; + Color blackBrush = Color.BLACK; + Color windowsColor = Color.BLUE; + + if(EntityElectricLocomotive.Horns) + { + g2d.fillRect(_startPosX + 30, _startPosY + 15, 20, 5); + g2d.drawLine(_startPosX + 40, _startPosY + 15, _startPosX + 50, _startPosY + 10); + g2d.drawLine(_startPosX + 50, _startPosY + 10, _startPosX + 45, _startPosY); + g2d.drawLine(_startPosX + 45, _startPosY + 15, _startPosX + 50, _startPosY + 10); + g2d.drawLine(_startPosX + 50, _startPosY + 10, _startPosX + 40, _startPosY); + g2d.setColor(blackBrush); + } + + if(EntityElectricLocomotive.SeifBatteries) + { + g2d.drawRect(_startPosX + 50, _startPosY + 25, 20, 10); + g2d.setColor(blackBrush); + } + + //locomotive + Polygon loco = new Polygon(); + + loco.addPoint(_startPosX, _startPosY + 40); + loco.addPoint(_startPosX, _startPosY + 30); + loco.addPoint(_startPosX + 20, _startPosY + 20); + loco.addPoint(_startPosX + 70, _startPosY + 20); + loco.addPoint(_startPosX +80, _startPosY + 30); + loco.addPoint(_startPosX +80, _startPosY + 40); + loco.addPoint(_startPosX +75, _startPosY + 45); + loco.addPoint(_startPosX +5, _startPosY + 45); + loco.addPoint(_startPosX, _startPosY + 40); + + g2d.setColor(blackBrush); + g2d.drawPolygon(loco); + g2d.setColor(bodyColor); + g2d.fillPolygon(loco); + + // windows + Polygon window = new Polygon(); + window.addPoint(_startPosX + 10, _startPosY + 30); + window.addPoint(_startPosX +15, _startPosY + 25); + window.addPoint(_startPosX + 20, _startPosY + 25); + window.addPoint(_startPosX + 20, _startPosY + 30); + window.addPoint(_startPosX +10, _startPosY + 30); + + g2d.setColor(blackBrush); + g2d.drawPolygon(window); + g2d.setColor(windowsColor); + g2d.fillPolygon(window); + + g2d.fillRect(_startPosX + 25, _startPosY + 25, 10, 5); + g2d.setColor(windowsColor); + g2d.drawRect(_startPosX + 25, _startPosY + 25, 10, 5); + g2d.setColor(blackBrush); + //locomotive + + //обязательные колеса + //loco + g2d.fillOval(_startPosX + 10, _startPosY + 45, 5, 5); + g2d.fillOval(_startPosX + 25, _startPosY + 45, 5, 5); + g2d.fillOval(_startPosX + 50, _startPosY + 45, 5, 5); + g2d.fillOval(_startPosX + 65, _startPosY + 45, 5, 5); + + g2d.setColor(EntityElectricLocomotive.BodyColor); + g2d.fillRect(_startPosX, _startPosY, _locoWidth, _locoHeight); + + + //telega + g2d.setColor(blackBrush); + g2d.fillOval(_startPosX + 95, _startPosY + 45, 5, 5); + g2d.fillOval(_startPosX + 140, _startPosY + 45, 5, 5); + + //telejka + Polygon telega = new Polygon(); + + telega.addPoint(_startPosX + 90, _startPosY + 25); + telega.addPoint(_startPosX + 95, _startPosY + 20); + telega.addPoint(_startPosX + 145, _startPosY + 20); + telega.addPoint(_startPosX + 150, _startPosY + 25); + telega.addPoint(_startPosX + 150, _startPosY + 45); + telega.addPoint(_startPosX + 90, _startPosY + 45); + telega.addPoint(_startPosX + 90, _startPosY + 25); + + g2d.setColor(additionalColor); + g2d.fillPolygon(telega); + g2d.setColor(blackBrush); + g2d.drawPolygon(telega); + //telejka + + //телега окна + g2d.setColor(blackBrush); + g.drawLine(_startPosX + 80, _startPosY + 40, _startPosX + 90, _startPosY + 40); + g2d.setColor(windowsColor); g.fillRect(_startPosX + 95, _startPosY + 30, 10, 5); + g.fillRect(_startPosX + 115, _startPosY + 30, 10, 5); + g.fillRect(_startPosX + 135, _startPosY + 30, 10, 5); + } +} diff --git a/ProjectElectricLocomotive/EntityElectricLocomotive.java b/ProjectElectricLocomotive/EntityElectricLocomotive.java index 962864f..492585f 100644 --- a/ProjectElectricLocomotive/EntityElectricLocomotive.java +++ b/ProjectElectricLocomotive/EntityElectricLocomotive.java @@ -4,7 +4,7 @@ import java.awt.*; public class EntityElectricLocomotive { public int Speed; - public int Weight; + public double Weight; public Color BodyColor; public Color AdditionalColor; public boolean Horns; @@ -13,7 +13,7 @@ public class EntityElectricLocomotive { { return (double) Speed * 100 / Weight; } - public void Init(int speed, int weight, Color bodyColor, Color additionalColor, + public void Init(int speed, double weight, Color bodyColor, Color additionalColor, boolean horns, boolean seifBatteries) { Speed = speed; diff --git a/ProjectElectricLocomotive/FormElectricLocomotive.form b/ProjectElectricLocomotive/FormElectricLocomotive.form new file mode 100644 index 0000000..f0e9886 --- /dev/null +++ b/ProjectElectricLocomotive/FormElectricLocomotive.form @@ -0,0 +1,90 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/ProjectElectricLocomotive/FormElectricLocomotive.java b/ProjectElectricLocomotive/FormElectricLocomotive.java new file mode 100644 index 0000000..a23ae48 --- /dev/null +++ b/ProjectElectricLocomotive/FormElectricLocomotive.java @@ -0,0 +1,79 @@ +package ProjectElectricLocomotive; +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionListener; +import java.util.Random; + +public class FormElectricLocomotive { + DrawingElectricLocomotive _drawingElectricLocomotive = new DrawingElectricLocomotive(); + private JButton buttonCreate; + private JPanel pictureBox; + private JButton buttonUp; + private JButton buttonDown; + private JButton buttonLeft; + private JButton buttonRight; + + public JPanel getPictureBox() { + return pictureBox; + } + public FormElectricLocomotive() + { + buttonUp.setName("buttonUp"); + buttonDown.setName("buttonDown"); + buttonLeft.setName("buttonLeft"); + buttonRight.setName("buttonRight"); + + buttonCreate.addActionListener(e -> { + _drawingElectricLocomotive = new DrawingElectricLocomotive(); + Random random = new Random(); + + _drawingElectricLocomotive.Init( + random.nextInt(100, 300), + random.nextInt(1000, 3000), + new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)), + new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)), + random.nextBoolean(), + random.nextBoolean(), + pictureBox.getWidth(), + pictureBox.getHeight() + ); + + //_drawingElectricLocomotive.SetEnginesCount(random.nextInt(2, 7)); + _drawingElectricLocomotive.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100)); + Draw(); + }); + + ActionListener buttonMoveClickedListener = e -> { + String buttonName = ((JButton) e.getSource()).getName(); + + switch (buttonName) { + case ("buttonUp") -> { + _drawingElectricLocomotive.MoveTransport(DyrectionType.Up); + } + case ("buttonDown") -> { + _drawingElectricLocomotive.MoveTransport(DyrectionType.Down); + } + case ("buttonLeft") -> { + _drawingElectricLocomotive.MoveTransport(DyrectionType.Left); + } + case ("buttonRight") -> { + _drawingElectricLocomotive.MoveTransport(DyrectionType.Right); + } + } + Draw(); + }; + buttonUp.addActionListener(buttonMoveClickedListener); + buttonDown.addActionListener(buttonMoveClickedListener); + buttonLeft.addActionListener(buttonMoveClickedListener); + buttonRight.addActionListener(buttonMoveClickedListener); + } + public void Draw() { + if (_drawingElectricLocomotive.EntityElectricLocomotive == null) { + return; + } + Graphics g = pictureBox.getGraphics(); + pictureBox.paint(g); + _drawingElectricLocomotive.DrawTransport(g); + } + +} diff --git a/ProjectElectricLocomotive/Main.java b/ProjectElectricLocomotive/Main.java index eda4640..5d307fe 100644 --- a/ProjectElectricLocomotive/Main.java +++ b/ProjectElectricLocomotive/Main.java @@ -3,6 +3,6 @@ package ProjectElectricLocomotive; public class Main { public static void main(String[] args) { - System.out.println("hello"); + } } diff --git a/ProjectElectricLocomotive/MainFrameElectricLocomotive.java b/ProjectElectricLocomotive/MainFrameElectricLocomotive.java new file mode 100644 index 0000000..4998474 --- /dev/null +++ b/ProjectElectricLocomotive/MainFrameElectricLocomotive.java @@ -0,0 +1,21 @@ +package ProjectElectricLocomotive; + +import javax.swing.*; + +public class MainFrameElectricLocomotive extends JFrame { + private FormElectricLocomotive _formElectricLocomotive; + public MainFrameElectricLocomotive() + { + super("Электролокомотив"); + setDefaultCloseOperation(EXIT_ON_CLOSE); + _formElectricLocomotive = new FormElectricLocomotive(); + setContentPane(_formElectricLocomotive.getPictureBox()); + setDefaultLookAndFeelDecorated(false); + setLocation(500, 50); + pack(); + setVisible(true); + } +} +// MainFrameStormtrooper mainFrameStormtrooper = new MainFrameStormtrooper(); +// } +//} diff --git a/ProjectElectricLocomotive/img/arrowDown.png b/ProjectElectricLocomotive/img/arrowDown.png new file mode 100644 index 0000000..17a85d2 Binary files /dev/null and b/ProjectElectricLocomotive/img/arrowDown.png differ diff --git a/ProjectElectricLocomotive/img/arrowLeft.png b/ProjectElectricLocomotive/img/arrowLeft.png new file mode 100644 index 0000000..3162784 Binary files /dev/null and b/ProjectElectricLocomotive/img/arrowLeft.png differ diff --git a/ProjectElectricLocomotive/img/arrowRight.png b/ProjectElectricLocomotive/img/arrowRight.png new file mode 100644 index 0000000..08e4399 Binary files /dev/null and b/ProjectElectricLocomotive/img/arrowRight.png differ diff --git a/ProjectElectricLocomotive/img/arrowUP.png b/ProjectElectricLocomotive/img/arrowUP.png new file mode 100644 index 0000000..3355b16 Binary files /dev/null and b/ProjectElectricLocomotive/img/arrowUP.png differ diff --git a/out/production/PIbd-21_Bakalskaya_E.D._ElectricLocomotive._HARD/.idea/misc.xml b/out/production/PIbd-21_Bakalskaya_E.D._ElectricLocomotive._HARD/.idea/misc.xml index 501ce09..4458232 100644 --- a/out/production/PIbd-21_Bakalskaya_E.D._ElectricLocomotive._HARD/.idea/misc.xml +++ b/out/production/PIbd-21_Bakalskaya_E.D._ElectricLocomotive._HARD/.idea/misc.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file