diff --git a/ProjectAirbus/.idea/uiDesigner.xml b/ProjectAirbus/.idea/uiDesigner.xml new file mode 100644 index 0000000..2b63946 --- /dev/null +++ b/ProjectAirbus/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/ProjectAirbus/src/DirectionType.java b/ProjectAirbus/src/DirectionType.java new file mode 100644 index 0000000..95f37e8 --- /dev/null +++ b/ProjectAirbus/src/DirectionType.java @@ -0,0 +1,6 @@ +public enum DirectionType { + Up, + Down, + Left, + Right, +} diff --git a/ProjectAirbus/src/DrawningAirbus.java b/ProjectAirbus/src/DrawningAirbus.java new file mode 100644 index 0000000..bb76321 --- /dev/null +++ b/ProjectAirbus/src/DrawningAirbus.java @@ -0,0 +1,162 @@ +import java.awt.*; +import java.util.Random; + +public class DrawningAirbus { + private EntityAirbus entityAirbus; + + public EntityAirbus getEntityAirbus() { + return entityAirbus; + } + + private Integer _pictureWidth; + private Integer _pictureHeight; + private Integer _startPosX; + private Integer _startPosY; + + private final int _drawningAirbusWidth = 145; + private final int _drawningAirbusHeight = 50; + private DrawningAirbusPortholes _drawningAirbusPortholes; + + public DrawningAirbus(int Speed, double Weight, Color BodyColor, Color AdditionalColor, + boolean AdditionalEngine, boolean PassengerCompartment) { + entityAirbus = new EntityAirbus(Speed, Weight, BodyColor, AdditionalColor, AdditionalEngine, PassengerCompartment); + _pictureWidth = null; + _pictureHeight = null; + _startPosX = null; + _startPosY = null; + _drawningAirbusPortholes = new DrawningAirbusPortholes(entityAirbus); + Random random = new Random(); + int portholesCount = random.nextInt(1, 4); + _drawningAirbusPortholes.setPortholesCount(portholesCount * 10); + } + + public boolean SetPictureSize(int width, int height) { + if (width > _drawningAirbusWidth && height > _drawningAirbusHeight) { + _pictureWidth = width; + _pictureHeight = height; + + if (_startPosX != null && _startPosY != null) { + if (_startPosX + _drawningAirbusWidth > _pictureWidth) { + _startPosX = _pictureWidth - _drawningAirbusWidth; + } + + if (_startPosY + _drawningAirbusHeight > _pictureHeight) { + _startPosY = _pictureHeight - _drawningAirbusHeight; + } + } + return true; + } + return false; + } + + public void SetPosition(int x, int y) { + if (_pictureHeight == null || _pictureWidth == null) { + return; + } + + _startPosX = x; + _startPosY = y; + + if (_startPosX + _drawningAirbusWidth > _pictureWidth) { + _startPosX = _pictureWidth - _drawningAirbusWidth; + } + if (_startPosY + _drawningAirbusHeight > _pictureHeight) { + _startPosY = _pictureHeight - _drawningAirbusHeight; + } + } + + public boolean MoveTransport(DirectionType direction) { + if (entityAirbus == null || _startPosX == null || _startPosY == null) { + return false; + } + + switch (direction) { + case DirectionType.Left: + if (_startPosX - entityAirbus.getStep() > 0) { + _startPosX -= (int) entityAirbus.getStep(); + } + return true; + case DirectionType.Up: + if (_startPosY - entityAirbus.getStep() > 0) { + _startPosY -= (int) entityAirbus.getStep(); + } + return true; + case DirectionType.Right: + if (_startPosX + entityAirbus.getStep() < _pictureWidth - _drawningAirbusWidth) { + _startPosX += (int) entityAirbus.getStep(); + } + return true; + case DirectionType.Down: + if (_startPosY + entityAirbus.getStep() < _pictureHeight - _drawningAirbusHeight) { + _startPosY += (int) entityAirbus.getStep(); + } + return true; + default: + return false; + } + } + + public void DrawTransport(Graphics g) { + if (entityAirbus == null || _startPosX == null || _startPosY == null) { + return; + } + + Graphics2D g2d = (Graphics2D) g; + + // дополнительный отсек для пассажиров + if (entityAirbus.withPassengerCompartment()) { + g2d.setColor(Color.BLACK); + g2d.drawRect(_startPosX + 60, _startPosY + 10, 40, 10); + g2d.drawOval(_startPosX + 50, _startPosY + 10, 20, 20); + g2d.drawOval(_startPosX + 90, _startPosY + 10, 20, 20); + + g2d.setColor(entityAirbus.getAdditionalColor()); + g2d.fillRect(_startPosX + 60, _startPosY + 11, 40, 10); + g2d.fillOval(_startPosX + 50, _startPosY + 10, 20, 20); + g2d.fillOval(_startPosX + 90, _startPosY + 10, 20, 20); + } + + //фюзеляж самолёта + int[] xPointsBorders = {_startPosX, _startPosX, _startPosX + 10, _startPosX + 120, _startPosX + 145, _startPosX + 120, _startPosX + 20}; + int[] yPointsBorders = {_startPosY, _startPosY + 30, _startPosY + 40, _startPosY + 40, _startPosY + 30, _startPosY + 20, _startPosY + 20}; + + g2d.setColor(Color.BLACK); + g2d.drawOval(_startPosX, _startPosY + 20, 20, 20); + g2d.drawPolygon(xPointsBorders, yPointsBorders, 7); + + g2d.setColor(entityAirbus.getBodyColor()); + g2d.fillOval(_startPosX, _startPosY + 20, 20, 20); + g2d.fillPolygon(xPointsBorders, yPointsBorders, 7); + g2d.setColor(Color.BLACK); + g2d.drawLine(_startPosX + 120, _startPosY + 30, _startPosX + 145, _startPosY + 30); + g2d.drawLine(_startPosX + 120, _startPosY + 20, _startPosX + 120, _startPosY + 40); + + //шасси + g2d.drawLine(_startPosX + 40, _startPosY + 40, _startPosX + 40, _startPosY + 44); + g2d.drawOval(_startPosX + 34, _startPosY + 44, 6, 6); + g2d.drawOval(_startPosX + 40, _startPosY + 44, 6, 6); + g2d.drawLine(_startPosX + 110, _startPosY + 40, _startPosX + 110, _startPosY + 44); + g2d.drawOval(_startPosX + 107, _startPosY + 44, 6, 6); + + //двигатель в хвосте + g2d.setColor(Color.BLACK); + g2d.fillOval(_startPosX, _startPosY + 15, 8, 8); + g2d.fillOval(_startPosX + 12, _startPosY + 15, 8, 8); + g2d.fillRect(_startPosX + 4, _startPosY + 15, 12, 8); + + //иллюминаторы + _drawningAirbusPortholes.drawAirbusPortholes(g, Color.BLUE, _startPosX + 2, _startPosY + 25); + + //дополнительный двигатель на крыле + if (entityAirbus.withAdditionalEngine()) { + g2d.fillOval(_startPosX + 48, _startPosY + 30, 8, 8); + g2d.fillOval(_startPosX + 60, _startPosY + 30, 8, 8); + g2d.fillRect(_startPosX + 52, _startPosY + 30, 12, 8); + } + + //крыло + g2d.fillRect(_startPosX + 40, _startPosY + 28, 40, 4); + g2d.fillOval(_startPosX + 38, _startPosY + 28, 4, 4); + g2d.fillOval(_startPosX + 78, _startPosY + 28, 4, 4); + } +} diff --git a/ProjectAirbus/src/DrawningAirbusPortholes.java b/ProjectAirbus/src/DrawningAirbusPortholes.java new file mode 100644 index 0000000..cbbc8ea --- /dev/null +++ b/ProjectAirbus/src/DrawningAirbusPortholes.java @@ -0,0 +1,37 @@ +import java.awt.*; + +public class DrawningAirbusPortholes { + private PortholesEnum _portholesEnum; + private EntityAirbus _entityAirbus; + + public void setPortholesCount(int portholesCount) { + for (PortholesEnum value : PortholesEnum.values()) { + if (value.portholesEnumNumber == portholesCount) { + _portholesEnum = value; + return; + } + } + } + + public DrawningAirbusPortholes(EntityAirbus entityAirbus) { + _entityAirbus = entityAirbus; + } + + public void drawAirbusPortholes(Graphics g, Color color, float portholesStartPosX, float portholesStartPosY) { + Graphics2D g2d = (Graphics2D) g; + int intervalBetweenPortholes = 120 / _portholesEnum.portholesEnumNumber; + + for (int i = 0; i < _portholesEnum.portholesEnumNumber; i++) { + g2d.setColor(color); + int posX = (int)(portholesStartPosX + i * intervalBetweenPortholes); + drawPortholeKit(g2d, posX, (int)portholesStartPosY); + } + } + + private void drawPortholeKit(Graphics2D g2d, int posX, int posY) { + g2d.setColor(Color.CYAN); + g2d.fillOval(posX, posY, 5, 5); + g2d.setColor(Color.BLACK); + g2d.drawOval(posX, posY, 5, 5); + } +} diff --git a/ProjectAirbus/src/EntityAirbus.java b/ProjectAirbus/src/EntityAirbus.java new file mode 100644 index 0000000..00d4fb0 --- /dev/null +++ b/ProjectAirbus/src/EntityAirbus.java @@ -0,0 +1,48 @@ +import java.awt.*; + +public class EntityAirbus { + private int Speed; + public int getSpeed() { + return Speed; + } + + private double Weight; + public double getWeight() { + return Weight; + } + + private Color BodyColor; + public Color getBodyColor() { + return BodyColor; + } + + private Color AdditionalColor; + public Color getAdditionalColor() { + return AdditionalColor; + } + + private boolean AdditionalEngine; + public boolean withAdditionalEngine() { + return AdditionalEngine; + } + + private boolean PassengerCompartment; + public boolean withPassengerCompartment() { + return PassengerCompartment; + } + + private double Step; + public double getStep() { + return (double)Speed * 100 / Weight; + } + + public EntityAirbus(int Speed, double Weight, Color BodyColor, Color AdditionalColor, + boolean AdditionalEngine, boolean PassengerCompartment) { + this.Speed = Speed; + this.Weight = Weight; + this.BodyColor = BodyColor; + this.AdditionalColor = AdditionalColor; + this.AdditionalEngine = AdditionalEngine; + this.PassengerCompartment = PassengerCompartment; + } +} diff --git a/ProjectAirbus/src/FormAirbus.form b/ProjectAirbus/src/FormAirbus.form new file mode 100644 index 0000000..1837926 --- /dev/null +++ b/ProjectAirbus/src/FormAirbus.form @@ -0,0 +1,89 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/ProjectAirbus/src/FormAirbus.java b/ProjectAirbus/src/FormAirbus.java new file mode 100644 index 0000000..3a66d1a --- /dev/null +++ b/ProjectAirbus/src/FormAirbus.java @@ -0,0 +1,113 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.ComponentAdapter; +import java.util.LinkedList; +import java.util.List; +import java.util.Random; + +public class FormAirbus { + DrawningAirbus _drawningAirbus; + private JPanel PanelWrapper; + public JPanel getPanelWrapper() { + return PanelWrapper; + } + private JButton buttonCreate; + private JButton buttonRight; + private JPanel PictureBox; + private JButton buttonDown; + private JButton buttonLeft; + private JButton buttonUp; + private List controls; + + public FormAirbus() { + buttonUp.setName("buttonUp"); + buttonDown.setName("buttonDown"); + buttonLeft.setName("buttonLeft"); + buttonRight.setName("buttonRight"); + + InitializeControlsRepaintList(); + + buttonCreate.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + Random random = new Random(); + _drawningAirbus = new DrawningAirbus(random.nextInt(100, 300), random.nextInt(1000, 3000), + new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)), + new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)), + random.nextBoolean(), random.nextBoolean()); + _drawningAirbus.SetPictureSize(PictureBox.getWidth(), PictureBox.getHeight()); + _drawningAirbus.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100)); + + Draw(); + } + }); + + ActionListener buttonMoveClickListener = new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + String buttonName = ((JButton) e.getSource()).getName(); + boolean result = false; + + switch (buttonName) { + case "buttonUp": { + result = _drawningAirbus.MoveTransport(DirectionType.Up); + } + break; + case "buttonDown": { + result = _drawningAirbus.MoveTransport(DirectionType.Down); + } + break; + case "buttonLeft": { + result = _drawningAirbus.MoveTransport(DirectionType.Left); + } + break; + case "buttonRight": { + result = _drawningAirbus.MoveTransport(DirectionType.Right); + } + break; + } + + if (result) { + Draw(); + } + } + + }; + + buttonRight.addActionListener(buttonMoveClickListener); + buttonDown.addActionListener(buttonMoveClickListener); + buttonLeft.addActionListener(buttonMoveClickListener); + buttonUp.addActionListener(buttonMoveClickListener); + } + + public void Draw() { + if (_drawningAirbus.getEntityAirbus() == null) { + return; + } + + Graphics g = PictureBox.getGraphics(); + g.setColor(PictureBox.getBackground()); + g.fillRect(0,0, PictureBox.getWidth(), PictureBox.getHeight()); + _drawningAirbus.DrawTransport(g); + + RepaintControls(); + } + + private void RepaintControls() { + for (JComponent control : controls) { + control.repaint(); + } + } + + private void InitializeControlsRepaintList() { + controls = new LinkedList<>(); + controls.add(buttonCreate); + controls.add(buttonUp); + controls.add(buttonDown); + controls.add(buttonLeft); + controls.add(buttonRight); + } +} + diff --git a/ProjectAirbus/src/Main.java b/ProjectAirbus/src/Main.java index f5858db..f95b8a2 100644 --- a/ProjectAirbus/src/Main.java +++ b/ProjectAirbus/src/Main.java @@ -1,5 +1,14 @@ +import javax.swing.*; + public class Main { public static void main(String[] args) { - + JFrame.setDefaultLookAndFeelDecorated(false); + JFrame frame = new JFrame("Аэробус"); + frame.setContentPane(new FormAirbus().getPanelWrapper()); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + frame.setLocation(200, 200); + frame.pack(); + frame.setSize(1000, 600); + frame.setVisible(true); } } \ No newline at end of file diff --git a/ProjectAirbus/src/PortholesEnum.java b/ProjectAirbus/src/PortholesEnum.java new file mode 100644 index 0000000..30bf279 --- /dev/null +++ b/ProjectAirbus/src/PortholesEnum.java @@ -0,0 +1,10 @@ +public enum PortholesEnum { + Ten(10), + Twenty(20), + Thirty(30); + + public final int portholesEnumNumber; + PortholesEnum (int i) { + this.portholesEnumNumber = i; + } +}