diff --git a/AirBomber/src/AirBomberPackage/CanvasMy.java b/AirBomber/src/AirBomberPackage/CanvasMy.java new file mode 100644 index 0000000..6557a30 --- /dev/null +++ b/AirBomber/src/AirBomberPackage/CanvasMy.java @@ -0,0 +1,31 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template + */ +package AirBomberPackage; + +import javax.swing.*; +import java.awt.*; + +/** + * + * @author Андрей + */ +public class CanvasMy extends JComponent { + public CanvasMy(){ + super(); + } + + private DrawingAirBomber _airBomber = null; + + public void setAirBomber(DrawingAirBomber _airBomber) { + this._airBomber = _airBomber; + } + + @Override + public void paintComponent(Graphics g){ + super.paintComponent(g); + Graphics2D g2d = (Graphics2D) g; + if (_airBomber != null) _airBomber.DrawTransport(g2d); + } +} diff --git a/AirBomber/src/AirBomberPackage/Direction.java b/AirBomber/src/AirBomberPackage/Direction.java new file mode 100644 index 0000000..7c7d402 --- /dev/null +++ b/AirBomber/src/AirBomberPackage/Direction.java @@ -0,0 +1,16 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template + */ +package AirBomberPackage; + +/** + * + * @author Андрей + */ +public enum Direction { + UP, + DOWN, + LEFT, + RIGHT +} diff --git a/AirBomber/src/AirBomberPackage/DrawingAirBomber.java b/AirBomber/src/AirBomberPackage/DrawingAirBomber.java new file mode 100644 index 0000000..7720f00 --- /dev/null +++ b/AirBomber/src/AirBomberPackage/DrawingAirBomber.java @@ -0,0 +1,224 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template + */ +package AirBomberPackage; +import java.awt.*; +import java.util.Random; +/** + * + * @author Андрей + */ +public class DrawingAirBomber { + private EntityAirBomber AirBomber; + public DrawingEngines drawingEngines; + public float _startPosX; + public float _startPosY; + private Integer _pictureWidth = null; + private Integer _pictureHeight = null; + private static final int _airBomberWidth = 110; + private static final int _airBomberHeight = 100; + + public void Init(int speed, float weight, Color bodyColor, int numberOfEngines){ + AirBomber = new EntityAirBomber(); + drawingEngines = new DrawingEngines(); + AirBomber.Init(speed, weight, bodyColor); + System.out.println(numberOfEngines + ""); + drawingEngines.setNumberOfEngines(numberOfEngines); + } + + public EntityAirBomber getAirBomber() { + return AirBomber; + } + + public void SetPosition(int x, int y, int width, int height){ + _pictureWidth = width; + _pictureHeight = height; + if (_pictureWidth == null || _pictureHeight == null) + { + return; + } + Random rd = new Random(); + _startPosX = x + _airBomberWidth >= _pictureWidth ? rd.nextInt(0, _pictureWidth - 1 - _airBomberWidth) : x; + _startPosY = y + _airBomberHeight >= _pictureHeight ? rd.nextInt(0, _pictureHeight - 1 - _airBomberHeight) : y; + } + + public void MoveTransport(Direction direction) + { + if (_pictureWidth == null || _pictureHeight == null) + { + return; + } + switch (direction) + { + // вправо + case RIGHT: + if (_startPosX + _airBomberWidth + AirBomber.Step < _pictureWidth) + { + _startPosX += AirBomber.Step; + } + break; + //влево + case LEFT: + if (_startPosX - AirBomber.Step > 0) + { + _startPosX -= AirBomber.Step; + } + break; + //вверх + case UP: + if (_startPosY - AirBomber.Step > 0) + { + _startPosY -= AirBomber.Step; + } + break; + //вниз + case DOWN: + if (_startPosY + _airBomberHeight + AirBomber.Step < _pictureHeight) + { + _startPosY += AirBomber.Step; + } + break; + } + } + + public void DrawTransport(Graphics2D g) + { + if (_startPosX < 0 || _startPosY < 0 + || _pictureHeight == null || _pictureWidth == null) + { + return; + } + + int _startPosXInt = (int)_startPosX; + int _startPosYInt = (int)_startPosY; + + //отрисовка двигателей + drawingEngines.drawEngines(g, _startPosXInt, _startPosYInt, AirBomber.getBodyColor()); + + //отрисовка кабины + int[] cabinX = + { + _startPosXInt + 5, + _startPosXInt + 20, + _startPosXInt + 20 + }; + + int[] cabinY = + { + _startPosYInt + 50, + _startPosYInt + 40, + _startPosYInt + 60 + }; + + g.fillPolygon(cabinX, cabinY, cabinX.length); + g.setColor(AirBomber.getBodyColor()); + //отрисовка корпуса + g.fillRect(_startPosXInt + 20, _startPosYInt + 40, 90, 20); + + //отрисовка правого крыла + int[] rightWingX = + { + _startPosXInt + 50, + _startPosXInt + 50, + _startPosXInt + 60, + _startPosXInt + 70 + }; + + int[] rightWingY = + { + _startPosYInt + 40, + _startPosYInt, + _startPosYInt, + _startPosYInt + 40 + }; + + g.fillPolygon(rightWingX, rightWingY, rightWingX.length); + + //отрисовка левого крыла + int[] leftWingX = + { + _startPosXInt + 50, + _startPosXInt + 50, + _startPosXInt + 60, + _startPosXInt + 70 + }; + + int[] leftWingY = + { + _startPosYInt + 60, + _startPosYInt + 100, + _startPosYInt + 100, + _startPosYInt + 60 + }; + + g.fillPolygon(leftWingX, leftWingY, leftWingX.length); + + //отрисовка хвоста (справа) + int[] rightTailX = + { + _startPosXInt + 95, + _startPosXInt + 95, + _startPosXInt + 110, + _startPosXInt + 110 + }; + + int[] rightTailY = + { + _startPosYInt + 40, + _startPosYInt + 30, + _startPosYInt + 15, + _startPosYInt + 40 + }; + + g.fillPolygon(rightTailX, rightTailY, rightTailX.length); + + //отрисовка хвоста (слева) + int[] leftTailX = + { + _startPosXInt + 95, + _startPosXInt + 95, + _startPosXInt + 110, + _startPosXInt + 110 + }; + + int[] leftTailY = + { + _startPosYInt + 60, + _startPosYInt + 70, + _startPosYInt + 85, + _startPosYInt + 60 + }; + + g.fillPolygon(leftTailX, leftTailY, leftTailX.length); + + g.setColor(Color.BLACK); + + g.drawRect(_startPosXInt + 20, _startPosYInt + 40, 90, 20); + g.drawPolygon(rightWingX, rightWingY, rightWingX.length); + g.drawPolygon(leftWingX, leftWingY, leftWingX.length); + g.drawPolygon(rightTailX, rightTailY, rightTailX.length); + g.drawPolygon(leftTailX, leftTailY, leftTailX.length); + g.drawLine(_startPosXInt, _startPosYInt + 50, _startPosXInt + 5, _startPosYInt + 50); + } + + public void ChangeBorders(int width, int height) + { + _pictureWidth = width; + _pictureHeight = height; + if (_pictureWidth <= _airBomberWidth || _pictureHeight <= _airBomberHeight) + { + _pictureWidth = null; + _pictureHeight = null; + return; + } + if (_startPosX + _airBomberWidth > _pictureWidth) + { + _startPosX = _pictureWidth - _airBomberWidth; + } + if (_startPosY + _airBomberHeight > _pictureHeight) + { + _startPosY = _pictureHeight - _airBomberHeight; + } + } +} diff --git a/AirBomber/src/AirBomberPackage/DrawingEngines.java b/AirBomber/src/AirBomberPackage/DrawingEngines.java new file mode 100644 index 0000000..53e529f --- /dev/null +++ b/AirBomber/src/AirBomberPackage/DrawingEngines.java @@ -0,0 +1,61 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template + */ +package AirBomberPackage; + +import java.awt.Graphics2D; +import java.awt.Color; + +/** + * + * @author Андрей + */ +public class DrawingEngines { + private Engines engines = null; + private int numberOfEngines; + + public int getNumberOfEngines() { + return numberOfEngines; + } + + public void setNumberOfEngines(int numberOfEngines) { + if (numberOfEngines != 2 && numberOfEngines != 4 && numberOfEngines != 6) return; + this.numberOfEngines = numberOfEngines; + switch (this.numberOfEngines) { + case 2: + engines = Engines.TWO; + break; + case 4: + engines = Engines.FOUR; + break; + case 6: + engines = Engines.SIX; + break; + } + } + + private void drawEngine(Graphics2D g, int startPosX, int startPosY, int yOffset, Color bodyColor){ + g.setColor(bodyColor); + g.fillRect(startPosX + 45, startPosY + 40 + yOffset, 5, 5); + g.setColor(Color.BLACK); + g.drawRect(startPosX + 45, startPosY + 40 + yOffset, 5, 5); + } + + public void drawEngines(Graphics2D g, int startPosX, int startPosY, Color bodyColor){ + if (engines == null) return; + + switch (engines) { + case SIX: + drawEngine(g, startPosX, startPosY, -5 - 5 - 20, bodyColor); + drawEngine(g, startPosX, startPosY, 20 + 5 + 20, bodyColor); + case FOUR: + drawEngine(g, startPosX, startPosY, -5 - 5 - 10, bodyColor); + drawEngine(g, startPosX, startPosY, 20 + 5 + 10, bodyColor); + case TWO: + drawEngine(g, startPosX, startPosY, -5 - 5, bodyColor); + drawEngine(g, startPosX, startPosY, 20 + 5, bodyColor); + break; + } + } +} diff --git a/AirBomber/src/AirBomberPackage/Engines.java b/AirBomber/src/AirBomberPackage/Engines.java new file mode 100644 index 0000000..4fa9b8b --- /dev/null +++ b/AirBomber/src/AirBomberPackage/Engines.java @@ -0,0 +1,15 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template + */ +package AirBomberPackage; + +/** + * + * @author Андрей + */ +public enum Engines { + TWO, + FOUR, + SIX +} diff --git a/AirBomber/src/AirBomberPackage/EntityAirBomber.java b/AirBomber/src/AirBomberPackage/EntityAirBomber.java new file mode 100644 index 0000000..d72ade4 --- /dev/null +++ b/AirBomber/src/AirBomberPackage/EntityAirBomber.java @@ -0,0 +1,43 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template + */ +package AirBomberPackage; +import java.awt.Color; +import java.util.Random; + +/** + * + * @author Андрей + */ +public class EntityAirBomber { + private int Speed; + + private float Weight; + + private Color BodyColor; + + public float Step; + + public int getSpeed() { + return Speed; + } + + public float getWeight() { + return Weight; + } + + public Color getBodyColor() { + return BodyColor; + } + + + + public void Init(int speed, float weight, Color bodyColor){ + Random rnd = new Random(); + Speed = speed <= 0 ? rnd.nextInt(50, 150) : speed; + Weight = weight <= 0 ? rnd.nextInt(40, 70) : weight; + Step = Speed * 100 / Weight; + BodyColor = bodyColor; + } +} diff --git a/AirBomber/src/AirBomberPackage/JFrameAirBomber.form b/AirBomber/src/AirBomberPackage/JFrameAirBomber.form index 769eecd..527555d 100644 --- a/AirBomber/src/AirBomberPackage/JFrameAirBomber.form +++ b/AirBomber/src/AirBomberPackage/JFrameAirBomber.form @@ -3,6 +3,16 @@
diff --git a/AirBomber/src/AirBomberPackage/JFrameAirBomber.java b/AirBomber/src/AirBomberPackage/JFrameAirBomber.java index 57162eb..e970014 100644 --- a/AirBomber/src/AirBomberPackage/JFrameAirBomber.java +++ b/AirBomber/src/AirBomberPackage/JFrameAirBomber.java @@ -3,6 +3,9 @@ * Click nbfs://nbhost/SystemFileSystem/Templates/GUIForms/JFrame.java to edit this template */ package AirBomberPackage; +import java.awt.*; +import javax.swing.*; +import java.util.Random; /** * @@ -16,6 +19,8 @@ public class JFrameAirBomber extends javax.swing.JFrame { public JFrameAirBomber() { initComponents(); } + + private DrawingAirBomber _airBomber; /** * This method is called from within the constructor to initialize the form. @@ -26,22 +31,180 @@ public class JFrameAirBomber extends javax.swing.JFrame { //