diff --git a/ProjectStormtrooper/DrawingEngines.java b/ProjectStormtrooper/DrawingEngines.java new file mode 100644 index 0000000..6e1d4fc --- /dev/null +++ b/ProjectStormtrooper/DrawingEngines.java @@ -0,0 +1,85 @@ +package ProjectStormtrooper; + +import java.awt.*; + +public class DrawingEngines { + private EnumEnginesCount _enumEnginesCount; + + public void SetEnumEnginesCount(int enginesCount) { + for (EnumEnginesCount val : EnumEnginesCount.values()) { + if (val.count == enginesCount) { + this._enumEnginesCount = val; + return; + } + } + this._enumEnginesCount = EnumEnginesCount.Two; + } + + private void DrawEngine(Graphics2D g2d, Color color, int x, int y, int w, int h) { + g2d.setColor(Color.WHITE); + g2d.fillRect(x, y, w, h); + g2d.setColor(Color.BLACK); + g2d.drawRect(x, y, w, h); + g2d.setColor(color); + g2d.fillRect(x, y, 6, h); + } + + public void DrawEngines(Graphics g, Color color, int startPosX, int startPosY, int drawingWidth, int drawingHeight) { + if (_enumEnginesCount == null) { + return; + } + Graphics2D g2d = (Graphics2D) g; + + int engineWidth = 20; + int engineHeight = 6; + + if (_enumEnginesCount.count >= EnumEnginesCount.Two.count) { + DrawEngine( + g2d, color, + startPosX + drawingWidth - drawingWidth / 5, + startPosY + drawingHeight / 2 - drawingHeight / 6, + engineWidth, + engineHeight + ); + DrawEngine( + g2d, color, + startPosX + drawingWidth - drawingWidth / 5, + startPosY + drawingHeight / 2 + drawingHeight / 6 - engineHeight, + engineWidth, + engineHeight + ); + } + if (_enumEnginesCount.count >= EnumEnginesCount.Four.count) { + DrawEngine( + g2d, color, + startPosX + drawingWidth - drawingWidth / 5 + 5, + startPosY + drawingHeight / 2 - drawingHeight / 6 - 10, + engineWidth - 5, + engineHeight + ); + DrawEngine( + g2d, color, + startPosX + drawingWidth - drawingWidth / 5 + 5, + startPosY + drawingHeight / 2 + drawingHeight / 6 - engineHeight + 10, + engineWidth - 5, + engineHeight + ); + } + if (_enumEnginesCount.count >= EnumEnginesCount.Six.count) { + DrawEngine( + g2d, color, + startPosX + drawingWidth / 2 - 10, + startPosY + drawingHeight / 2 - drawingHeight / 6 - 10, + engineWidth, + engineHeight + ); + DrawEngine( + g2d, color, + startPosX + drawingWidth / 2 - 10, + startPosY + drawingHeight / 2 + drawingHeight / 6 + 3, + engineWidth, + engineHeight + ); + } + } +} diff --git a/ProjectStormtrooper/DrawingStormtrooper.java b/ProjectStormtrooper/DrawingStormtrooper.java new file mode 100644 index 0000000..38f91db --- /dev/null +++ b/ProjectStormtrooper/DrawingStormtrooper.java @@ -0,0 +1,262 @@ +package ProjectStormtrooper; + +import java.awt.*; + +public class DrawingStormtrooper { + public EntityStormtrooper EntityStormtrooper; + private DrawingEngines _drawingEngines; + private int _pictureWidth; + private int _pictureHeight; + private int _startPosX; + private int _startPosY; + private final int _stormtrooperWidth = 110; + private final int _stormtrooperHeight = 110; + + public boolean Init(int speed, double weight, Color bodyColor, + Color additionalColor, boolean rockets, boolean bombs, + int width, int height) { + if (width < _stormtrooperWidth && height < _stormtrooperHeight) { + return false; + } + _pictureWidth = width; + _pictureHeight = height; + _drawingEngines = new DrawingEngines(); + EntityStormtrooper = new EntityStormtrooper(); + EntityStormtrooper.Init(speed, weight, bodyColor, additionalColor, rockets, bombs); + return true; + } + + public void SetEnginesCount(int enginesCount) { + _drawingEngines.SetEnumEnginesCount(enginesCount); + } + + public void SetPosition(int x, int y) { + if (x < 0) { + x = 0; + } else if (x > _pictureWidth - _stormtrooperWidth) { + x = _pictureWidth - _stormtrooperWidth; + } + _startPosX = x; + + if (y < 0) { + y = 0; + } else if (y > _pictureHeight - _stormtrooperHeight) { + y = _pictureHeight - _stormtrooperHeight; + } + _startPosY = y; + } + + public void MoveTransport(EnumDirectionType direction) { + if (EntityStormtrooper == null) { + return; + } + switch (direction) { + case Up -> { + if (_startPosY - EntityStormtrooper.Step() >= 0) { + _startPosY -= (int) EntityStormtrooper.Step(); + } + } + case Down -> { + if (_startPosY + _stormtrooperHeight + EntityStormtrooper.Step() <= _pictureHeight) { + _startPosY += (int) EntityStormtrooper.Step(); + } + } + case Left -> { + if (_startPosX - EntityStormtrooper.Step() >= 0) { + _startPosX -= (int) EntityStormtrooper.Step(); + } + } + case Right -> { + if (_startPosX + _stormtrooperWidth + EntityStormtrooper.Step() <= _pictureWidth) { + _startPosX += (int) EntityStormtrooper.Step(); + } + } + } + } + + public void DrawTransport(Graphics g) { + if (EntityStormtrooper == null) { + return; + } + Graphics2D g2d = (Graphics2D) g; + + Color bodyColor = EntityStormtrooper.BodyColor; + Color additionalColor = EntityStormtrooper.AdditionalColor; + Color blackColor = Color.BLACK; + Color redColor = Color.RED; + + _drawingEngines.DrawEngines(g, additionalColor, _startPosX, _startPosY, _stormtrooperWidth, _stormtrooperHeight); + + // Длина фюзеляжа + int bodyHeight = _stormtrooperHeight / 9; + int bodyWidth = _stormtrooperWidth - _stormtrooperWidth / 8; + + // Рисуем бомбы + if (EntityStormtrooper.Bombs) { + Polygon bombTailPolygon = new Polygon(); + bombTailPolygon.addPoint(_startPosX + _stormtrooperWidth / 2 - _stormtrooperWidth / 8 + bodyHeight * 3 - 5, + _startPosY + _stormtrooperHeight / 2 - bodyHeight / 2 - _stormtrooperHeight / 3 + bodyHeight / 2); + bombTailPolygon.addPoint(_startPosX + _stormtrooperWidth / 2 - _stormtrooperWidth / 8 + bodyHeight * 3 + 5, + _startPosY + _stormtrooperHeight / 2 - bodyHeight / 2 - _stormtrooperHeight / 3 + bodyHeight / 2 - 5); + bombTailPolygon.addPoint(_startPosX + _stormtrooperWidth / 2 - _stormtrooperWidth / 8 + bodyHeight * 3 + 5, + _startPosY + _stormtrooperHeight / 2 - bodyHeight / 2 - _stormtrooperHeight / 3 + bodyHeight / 2 + 5); + bombTailPolygon.addPoint(_startPosX + _stormtrooperWidth / 2 - _stormtrooperWidth / 8 + bodyHeight * 3 - 5, + _startPosY + _stormtrooperHeight / 2 - bodyHeight / 2 - _stormtrooperHeight / 3 + bodyHeight / 2); + + g2d.setColor(additionalColor); + g2d.fillPolygon(bombTailPolygon); + g2d.setColor(blackColor); + g2d.drawPolygon(bombTailPolygon); + + bombTailPolygon.translate(0, (int) (_stormtrooperHeight - 2 * (_stormtrooperHeight / 2 - bodyHeight / 2 - _stormtrooperHeight / 3 + bodyHeight / 2 - 5) - bombTailPolygon.getBounds2D().getHeight())); + + g2d.setColor(additionalColor); + g2d.fillPolygon(bombTailPolygon); + g2d.setColor(blackColor); + g2d.drawPolygon(bombTailPolygon); + + g2d.setColor(additionalColor); + g2d.fillOval( + _startPosX + _stormtrooperWidth / 2 - _stormtrooperWidth / 8, + _startPosY + _stormtrooperHeight / 2 - bodyHeight / 2 - _stormtrooperHeight / 3, + bodyHeight * 3, + bodyHeight); + + g2d.fillOval( + _startPosX + _stormtrooperWidth / 2 - _stormtrooperWidth / 8, + _startPosY + _stormtrooperHeight / 2 - bodyHeight / 2 + _stormtrooperHeight / 3, + bodyHeight * 3, + bodyHeight); + + g2d.setColor(blackColor); + g2d.drawOval( + _startPosX + _stormtrooperWidth / 2 - _stormtrooperWidth / 8, + _startPosY + _stormtrooperHeight / 2 - bodyHeight / 2 - _stormtrooperHeight / 3, + bodyHeight * 3, + bodyHeight); + g2d.drawOval( + _startPosX + _stormtrooperWidth / 2 - _stormtrooperWidth / 8, + _startPosY + _stormtrooperHeight / 2 - bodyHeight / 2 + _stormtrooperHeight / 3, + bodyHeight * 3, + bodyHeight); + } + + // Рисуем ракеты + if (EntityStormtrooper.Rockets) { + int rocketWidth = bodyHeight * 4; + int rocketHeight = bodyHeight / 2; + + Polygon rocketCockPitPolygon = new Polygon(); + rocketCockPitPolygon.addPoint(_startPosX + _stormtrooperWidth / 2 - _stormtrooperWidth / 5 - rocketHeight, + _startPosY + _stormtrooperHeight / 2 - bodyHeight - bodyHeight / 2 + rocketHeight / 2); + rocketCockPitPolygon.addPoint(_startPosX + _stormtrooperWidth / 2 - _stormtrooperWidth / 5, + _startPosY + _stormtrooperHeight / 2 - bodyHeight - bodyHeight / 2); + rocketCockPitPolygon.addPoint(_startPosX + _stormtrooperWidth / 2 - _stormtrooperWidth / 5, + _startPosY + _stormtrooperHeight / 2 - bodyHeight - bodyHeight / 2 + rocketHeight); + + Polygon rocketTailPolygon = new Polygon(); + rocketTailPolygon.addPoint(_startPosX + _stormtrooperWidth / 2 - _stormtrooperWidth / 5 - rocketHeight + rocketWidth - 10, + _startPosY + _stormtrooperHeight / 2 - bodyHeight - bodyHeight / 2 + rocketHeight / 2); + rocketTailPolygon.addPoint(_startPosX + _stormtrooperWidth / 2 - _stormtrooperWidth / 5 + rocketWidth, + _startPosY + _stormtrooperHeight / 2 - bodyHeight * 2 + rocketHeight / 2); + rocketTailPolygon.addPoint(_startPosX + _stormtrooperWidth / 2 - _stormtrooperWidth / 5 + rocketWidth, + _startPosY + _stormtrooperHeight / 2 - bodyHeight - bodyHeight / 2 + rocketHeight + bodyHeight / 2 - rocketHeight / 2); + + g2d.setColor(redColor); + g2d.fillPolygon(rocketCockPitPolygon); + g2d.setColor(blackColor); + g2d.drawPolygon(rocketCockPitPolygon); + + g2d.setColor(blackColor); + g2d.fillPolygon(rocketTailPolygon); + + rocketCockPitPolygon.translate(0, (int) (_stormtrooperHeight - 2 * (_stormtrooperHeight / 2 - bodyHeight - bodyHeight / 2) - rocketCockPitPolygon.getBounds2D().getHeight())); + rocketTailPolygon.translate(0, (int) (_stormtrooperHeight - 2 * (_stormtrooperHeight / 2 - bodyHeight * 2 + rocketHeight / 2) - rocketTailPolygon.getBounds2D().getHeight())); + + g2d.setColor(redColor); + g2d.fillPolygon(rocketCockPitPolygon); + g2d.setColor(blackColor); + g2d.drawPolygon(rocketCockPitPolygon); + + g2d.setColor(blackColor); + g2d.fillPolygon(rocketTailPolygon); + + g2d.setColor(additionalColor); + g2d.fillRect( + _startPosX + _stormtrooperWidth / 2 - _stormtrooperWidth / 5, + _startPosY + _stormtrooperHeight / 2 - bodyHeight - bodyHeight / 2, + rocketWidth, + rocketHeight); + + + g2d.fillRect( + _startPosX + _stormtrooperWidth / 2 - _stormtrooperWidth / 5, + _startPosY + _stormtrooperHeight / 2 + bodyHeight / 2 + bodyHeight / 2, + rocketWidth, + rocketHeight); + + g2d.setColor(blackColor); + g2d.drawRect( + _startPosX + _stormtrooperWidth / 2 - _stormtrooperWidth / 5, + _startPosY + _stormtrooperHeight / 2 - bodyHeight - bodyHeight / 2, + rocketWidth, + rocketHeight); + + g2d.drawRect( + _startPosX + _stormtrooperWidth / 2 - _stormtrooperWidth / 5, + _startPosY + _stormtrooperHeight / 2 + bodyHeight / 2 + bodyHeight / 2, + rocketWidth, + rocketHeight); + } + + // Рисуем нос + Polygon cockPitPolygon = new Polygon(); + cockPitPolygon.addPoint(_startPosX, _startPosY + _stormtrooperHeight / 2); + cockPitPolygon.addPoint(_startPosX + _stormtrooperWidth / 8, _startPosY + _stormtrooperHeight / 2 - bodyHeight / 2); + cockPitPolygon.addPoint(_startPosX + _stormtrooperWidth / 8, _startPosY + _stormtrooperHeight / 2 + bodyHeight / 2); + + g2d.setColor(blackColor); + g2d.fillPolygon(cockPitPolygon); + + // Рисуем крылья + Polygon wingsPolygon = new Polygon(); + wingsPolygon.addPoint(_startPosX + _stormtrooperWidth / 2, _startPosY); + wingsPolygon.addPoint(_startPosX + _stormtrooperWidth / 2 + _stormtrooperWidth / 15, _startPosY); + wingsPolygon.addPoint(_startPosX + _stormtrooperWidth / 2 + _stormtrooperWidth / 6, _startPosY + _stormtrooperHeight / 2); + wingsPolygon.addPoint(_startPosX + _stormtrooperWidth / 2 + _stormtrooperWidth / 15, _startPosY + _stormtrooperHeight); + wingsPolygon.addPoint(_startPosX + _stormtrooperWidth / 2, _startPosY + _stormtrooperHeight); + + g2d.setColor(bodyColor); + g.fillPolygon(wingsPolygon); + g2d.setColor(blackColor); + g.drawPolygon(wingsPolygon); + + // Рисуем хвостовое оперение + Polygon tailPolygon = new Polygon(); + tailPolygon.addPoint(_startPosX + _stormtrooperWidth, _startPosY + _stormtrooperHeight / 2 - _stormtrooperHeight / 3); + tailPolygon.addPoint(_startPosX + bodyWidth, _startPosY + _stormtrooperHeight / 2 - _stormtrooperHeight / 8); + tailPolygon.addPoint(_startPosX + bodyWidth, _startPosY + _stormtrooperHeight / 2 + _stormtrooperHeight / 8); + tailPolygon.addPoint(_startPosX + _stormtrooperWidth, _startPosY + _stormtrooperHeight / 2 + _stormtrooperHeight / 3); + + g2d.setColor(bodyColor); + g.fillPolygon(tailPolygon); + g2d.setColor(blackColor); + g.drawPolygon(tailPolygon); + + // Рисуем фюзеляж + g2d.setColor(bodyColor); + g2d.fillRect( + _startPosX + _stormtrooperWidth / 8, + _startPosY + _stormtrooperHeight / 2 - bodyHeight / 2, + bodyWidth, + bodyHeight + ); + g2d.setColor(blackColor); + g2d.drawRect( + _startPosX + _stormtrooperWidth / 8, + _startPosY + _stormtrooperHeight / 2 - bodyHeight / 2, + bodyWidth, + bodyHeight + ); + } +} diff --git a/ProjectStormtrooper/EntityStormtrooper.java b/ProjectStormtrooper/EntityStormtrooper.java new file mode 100644 index 0000000..2570ba4 --- /dev/null +++ b/ProjectStormtrooper/EntityStormtrooper.java @@ -0,0 +1,25 @@ +package ProjectStormtrooper; + +import java.awt.*; + +public class EntityStormtrooper { + public int Speed; + public double Weight; + public Color BodyColor; + public Color AdditionalColor; + public boolean Rockets; + public boolean Bombs; + + public double Step() { + return (double) Speed * 250 / Weight; + } + + public void Init(int speed, double weight, Color bodyColor, Color additionalColor, boolean rockets, boolean bombs) { + Speed = speed; + Weight = weight; + BodyColor = bodyColor; + AdditionalColor = additionalColor; + Rockets = rockets; + Bombs = bombs; + } +} diff --git a/ProjectStormtrooper/EnumDirectionType.java b/ProjectStormtrooper/EnumDirectionType.java new file mode 100644 index 0000000..c7c3cd6 --- /dev/null +++ b/ProjectStormtrooper/EnumDirectionType.java @@ -0,0 +1,5 @@ +package ProjectStormtrooper; + +public enum EnumDirectionType { + Up, Down, Left, Right +} diff --git a/ProjectStormtrooper/EnumEnginesCount.java b/ProjectStormtrooper/EnumEnginesCount.java new file mode 100644 index 0000000..475628b --- /dev/null +++ b/ProjectStormtrooper/EnumEnginesCount.java @@ -0,0 +1,12 @@ +package ProjectStormtrooper; + +public enum EnumEnginesCount { + Two(2), + Four(4), + Six(6); + public final int count; + + EnumEnginesCount(int count) { + this.count = count; + } +} diff --git a/ProjectStormtrooper/FormStormtrooper.form b/ProjectStormtrooper/FormStormtrooper.form new file mode 100644 index 0000000..5aa85d1 --- /dev/null +++ b/ProjectStormtrooper/FormStormtrooper.form @@ -0,0 +1,94 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/ProjectStormtrooper/FormStormtrooper.java b/ProjectStormtrooper/FormStormtrooper.java new file mode 100644 index 0000000..bc8ab6b --- /dev/null +++ b/ProjectStormtrooper/FormStormtrooper.java @@ -0,0 +1,84 @@ +package ProjectStormtrooper; + +import javax.swing.*; +import java.awt.*; +import java.util.Random; +import java.awt.event.ActionListener; + +public class FormStormtrooper { + DrawingStormtrooper _drawingStormtrooper = new DrawingStormtrooper(); + private JButton buttonCreate; + private JPanel pictureBox; + private JButton buttonDown; + private JButton buttonUp; + private JButton buttonLeft; + private JButton buttonRight; + + public JPanel getPictureBox() { + return pictureBox; + } + + public FormStormtrooper() { + buttonUp.setName("buttonUp"); + buttonDown.setName("buttonDown"); + buttonLeft.setName("buttonLeft"); + buttonRight.setName("buttonRight"); + + buttonCreate.addActionListener(e -> { + _drawingStormtrooper = new DrawingStormtrooper(); + Random random = new Random(); + + _drawingStormtrooper.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() + ); + + _drawingStormtrooper.SetEnginesCount(random.nextInt(2, 7)); + _drawingStormtrooper.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100)); + + Draw(); + }); + + ActionListener buttonMoveClickedListener = e -> { + String buttonName = ((JButton) e.getSource()).getName(); + + switch (buttonName) { + case ("buttonUp") -> { + _drawingStormtrooper.MoveTransport(EnumDirectionType.Up); + } + case ("buttonDown") -> { + _drawingStormtrooper.MoveTransport(EnumDirectionType.Down); + } + case ("buttonLeft") -> { + _drawingStormtrooper.MoveTransport(EnumDirectionType.Left); + } + case ("buttonRight") -> { + _drawingStormtrooper.MoveTransport(EnumDirectionType.Right); + } + } + + Draw(); + }; + + buttonUp.addActionListener(buttonMoveClickedListener); + buttonDown.addActionListener(buttonMoveClickedListener); + buttonLeft.addActionListener(buttonMoveClickedListener); + buttonRight.addActionListener(buttonMoveClickedListener); + } + + public void Draw() { + if (_drawingStormtrooper.EntityStormtrooper == null) { + return; + } + + Graphics g = pictureBox.getGraphics(); + pictureBox.paint(g); + _drawingStormtrooper.DrawTransport(g); + } +} diff --git a/ProjectStormtrooper/Main.java b/ProjectStormtrooper/Main.java new file mode 100644 index 0000000..3649ce1 --- /dev/null +++ b/ProjectStormtrooper/Main.java @@ -0,0 +1,7 @@ +package ProjectStormtrooper; + +public class Main { + public static void main(String[] args) { + MainFrameStormtrooper mainFrameStormtrooper = new MainFrameStormtrooper(); + } +} diff --git a/ProjectStormtrooper/MainFrameStormtrooper.java b/ProjectStormtrooper/MainFrameStormtrooper.java new file mode 100644 index 0000000..670d59e --- /dev/null +++ b/ProjectStormtrooper/MainFrameStormtrooper.java @@ -0,0 +1,19 @@ +package ProjectStormtrooper; + +import javax.swing.*; + +public class MainFrameStormtrooper extends JFrame { + private FormStormtrooper _formStormtrooper; + + public MainFrameStormtrooper() { + super(); + setTitle("Штурмовик"); + setDefaultCloseOperation(EXIT_ON_CLOSE); + _formStormtrooper = new FormStormtrooper(); + setContentPane(_formStormtrooper.getPictureBox()); + setDefaultLookAndFeelDecorated(false); + setLocation(300, 100); + pack(); + setVisible(true); + } +} diff --git a/ProjectStormtrooper/img/arrowDOWN.png b/ProjectStormtrooper/img/arrowDOWN.png new file mode 100644 index 0000000..7ef7c08 Binary files /dev/null and b/ProjectStormtrooper/img/arrowDOWN.png differ diff --git a/ProjectStormtrooper/img/arrowLEFT.png b/ProjectStormtrooper/img/arrowLEFT.png new file mode 100644 index 0000000..92ca017 Binary files /dev/null and b/ProjectStormtrooper/img/arrowLEFT.png differ diff --git a/ProjectStormtrooper/img/arrowRIGHT.png b/ProjectStormtrooper/img/arrowRIGHT.png new file mode 100644 index 0000000..563b1b9 Binary files /dev/null and b/ProjectStormtrooper/img/arrowRIGHT.png differ diff --git a/ProjectStormtrooper/img/arrowUP.png b/ProjectStormtrooper/img/arrowUP.png new file mode 100644 index 0000000..c5a432c Binary files /dev/null and b/ProjectStormtrooper/img/arrowUP.png differ