diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..cdcb9fa --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..930ca43 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000..d2e0a73 --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,69 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + 1700296521973 + + + + + + + + \ No newline at end of file diff --git a/PIbd-22_Isaeva_A.I._Airbus_Hard.iml b/PIbd-22_Isaeva_A.I._Airbus_Hard.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/PIbd-22_Isaeva_A.I._Airbus_Hard.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/images/KeyDown.png b/images/KeyDown.png new file mode 100644 index 0000000..db4019a Binary files /dev/null and b/images/KeyDown.png differ diff --git a/images/KeyLeft.png b/images/KeyLeft.png new file mode 100644 index 0000000..a5ede96 Binary files /dev/null and b/images/KeyLeft.png differ diff --git a/images/KeyRight.png b/images/KeyRight.png new file mode 100644 index 0000000..8fd306d Binary files /dev/null and b/images/KeyRight.png differ diff --git a/images/KeyUp.png b/images/KeyUp.png new file mode 100644 index 0000000..4655d45 Binary files /dev/null and b/images/KeyUp.png differ diff --git a/src/Drawnings/DrawingAirbus.java b/src/Drawnings/DrawingAirbus.java new file mode 100644 index 0000000..efd3310 --- /dev/null +++ b/src/Drawnings/DrawingAirbus.java @@ -0,0 +1,144 @@ +package Drawnings; + +import java.awt.*; +import Entities.*; +import MovementStrategy.*; + +public class DrawingAirbus { + + public EntityAirbus entityAirbus; + public DrawingPortholes _portholes; + private int _pictureWidth; + private int _pictureHeight; + private int _startPosX; + private int _startPosY; + private int _airbusWidth = 123; + private int _airbusHeight = 44; + + public void Init(int speed, float weight, Color bodyColor, Color additionalColor, boolean isCompartment, boolean isadditionalEngine, int countPortholes, int width, int height) { + if (width < _airbusHeight || height < _airbusWidth) + return; + _pictureWidth = width; + _pictureHeight = height; + entityAirbus = new EntityAirbus(); + entityAirbus.Init(speed, weight, bodyColor, additionalColor, isCompartment, isadditionalEngine); + + _portholes = new DrawingPortholes(); + _portholes.SetCount(countPortholes); + } + + public void SetPosition (int x, int y) { + if (x + _airbusWidth > _pictureWidth || y + _airbusHeight > _pictureHeight) { + _startPosX = _pictureWidth - _airbusWidth; + _startPosY = _pictureHeight - _airbusHeight; + } + else + { + _startPosX = x; + _startPosY = y; + } + } + + public void MoveTransport(Direction direction){ + if (entityAirbus == null) { + return; + } + + switch (direction) { + case Left: + if (_startPosX - entityAirbus.Step > 0) + { + _startPosX -= entityAirbus.Step; + } + break; + case Right: + if (_startPosX + _airbusWidth + entityAirbus.Step < _pictureWidth) + { + _startPosX += entityAirbus.Step; + } + break; + case Up: + if (_startPosY - entityAirbus.Step > 0) + { + _startPosY -= entityAirbus.Step; + } + break; + case Down: + if (_startPosY + _airbusHeight + entityAirbus.Step < _pictureHeight) + { + _startPosY += entityAirbus.Step; + } + break; + } + } + + public void DrawTransport(Graphics2D g) { + + if (entityAirbus == null) { + return; + } + // тело + g.setColor(entityAirbus.getBodyColor()); + g.fillRect(_startPosX+3, _startPosY+17, 103, 20); + g.setColor(Color.BLACK); + g.drawRect(_startPosX+3, _startPosY+17, 103, 20); + + // иллюминаторы + _portholes.Draw(g, _startPosX, _startPosY); + + // нос + int[] xPolygonNoise = {_startPosX+106, _startPosX + 120, _startPosX+106,}; + int[] yPolygonNoise = {_startPosY+17, _startPosY+27, _startPosY+37}; + + g.setColor(entityAirbus.getBodyColor()); + g.fillPolygon(xPolygonNoise, yPolygonNoise, xPolygonNoise.length); + g.setColor(Color.BLACK); + g.drawPolygon(xPolygonNoise, yPolygonNoise, xPolygonNoise.length); + + // хвост + int[] xPolygonTale = { _startPosX+2, _startPosX+27, _startPosX+2}; + int[] yPolygonTale = {_startPosY, _startPosY + 18,_startPosY+18}; + g.setColor(entityAirbus.getBodyColor()); + g.fillPolygon(xPolygonTale, yPolygonTale, xPolygonTale.length); + g.setColor(Color.BLACK); + g.drawPolygon(xPolygonTale, yPolygonTale, xPolygonTale.length); + + // крыло + g.setColor(Color.BLACK); + g.fillOval(_startPosX+43, _startPosY+25, 22, 5); + g.drawOval(_startPosX+43, _startPosY+25, 22, 5); + + // двигатель + g.setColor(Color.BLACK); + g.fillOval(_startPosX+1, _startPosY+15, 19, 5); + g.drawOval(_startPosX+1, _startPosY+15, 19, 5); + + // шасси + g.setColor(entityAirbus.getBodyColor()); + g.fillOval(_startPosX+25,_startPosY+38, 6, 6); + g.fillOval(_startPosX+30,_startPosY+38, 6, 6); + g.fillOval(_startPosX+100,_startPosY+38, 6, 6); + g.setColor(Color.BLACK); + g.drawOval(_startPosX+25,_startPosY+38, 6, 6); + g.drawOval(_startPosX+30,_startPosY+38, 6, 6); + g.drawOval(_startPosX+100,_startPosY+38, 6, 6); + + // пассажирский отсек + if (entityAirbus.IsCompartment()) + { + g.setColor(entityAirbus.getAdditionalColor()); + g.fillOval(_startPosX+57, _startPosY+11, 39, 9); + g.setColor(Color.BLACK); + g.drawOval(_startPosX+57, _startPosY+11, 39, 9); + } + + // доп двигатель + if (entityAirbus.IsAdditionalEngine()) + { + g.setColor(entityAirbus.getAdditionalColor()); + g.fillOval(_startPosX, _startPosY + 25, 11, 5); + g.setColor(Color.BLACK); + g.drawOval(_startPosX, _startPosY + 25, 11, 5); + } + } +} diff --git a/src/Drawnings/DrawingPortholes.java b/src/Drawnings/DrawingPortholes.java new file mode 100644 index 0000000..11faa56 --- /dev/null +++ b/src/Drawnings/DrawingPortholes.java @@ -0,0 +1,98 @@ +package Drawnings; + +import java.awt.*; +import Entities.*; + +public class DrawingPortholes { + private CountPortholes _porthole; + public int Count; + + public CountPortholes getCount() + { + return _porthole; + } + public void SetCount (int count) { + Count = count; + switch (Count) { + case 10: + _porthole = CountPortholes.Ten; + break; + case 20: + _porthole = CountPortholes.Twenty; + break; + case 30: + _porthole = CountPortholes.Thirty; + break; + default: + _porthole = CountPortholes.Ten; + break; + } + } + + public void Draw (Graphics2D g, int _startPosx, int _startPoxY) { + g.setColor(Color.BLACK); + if (_porthole == CountPortholes.Ten) { + // цикл + for (int i = 0; i < 10; ++i) + { + g.setColor(Color.cyan); + g.fillOval(_startPosx + 19 + i*8, _startPoxY+21, 3, 3); + g.setColor(Color.black); + g.drawOval(_startPosx + 19 + i*8, _startPoxY+21, 3, 3); + } + } + if (_porthole == CountPortholes.Twenty) { + for (int i = 0; i < 10; ++i) + { + g.setColor(Color.cyan); + g.fillOval(_startPosx + 19 + i*8, _startPoxY+21, 3, 3); + g.setColor(Color.black); + g.drawOval(_startPosx + 19 + i*8, _startPoxY+21, 3, 3); + } + for (int i=0; i < 5; ++i) + { + g.setColor(Color.cyan); + g.fillOval(_startPosx + 15 + i*5, _startPoxY+26, 3, 3); + g.setColor(Color.black); + g.drawOval(_startPosx + 15 + i*5, _startPoxY+26, 3, 3); + } + for (int i=0; i < 5; ++i) + { + g.setColor(Color.cyan); + g.fillOval(_startPosx + 70 + i*5, _startPoxY+26, 3, 3); + g.setColor(Color.black); + g.drawOval(_startPosx + 70 + i*5, _startPoxY+26, 3, 3); + } + } + if (_porthole == CountPortholes.Thirty){ + for (int i = 0; i < 10; ++i) + { + g.setColor(Color.cyan); + g.fillOval(_startPosx + 19 + i*8, _startPoxY+21, 3, 3); + g.setColor(Color.black); + g.drawOval(_startPosx + 19 + i*8, _startPoxY+21, 3, 3); + } + for (int i=0; i < 5; ++i) + { + g.setColor(Color.cyan); + g.fillOval(_startPosx + 15 + i*5, _startPoxY+26, 3, 3); + g.setColor(Color.black); + g.drawOval(_startPosx + 15 + i*5, _startPoxY+26, 3, 3); + } + for (int i=0; i < 5; ++i) + { + g.setColor(Color.cyan); + g.fillOval(_startPosx + 70 + i*5, _startPoxY+26, 3, 3); + g.setColor(Color.black); + g.drawOval(_startPosx + 70 + i*5, _startPoxY+26, 3, 3); + } + for (int i = 0; i < 10; ++i) + { + g.setColor(Color.cyan); + g.fillOval(_startPosx + 19 + i*8, _startPoxY+31, 3, 3); + g.setColor(Color.black); + g.drawOval(_startPosx + 19 + i*8, _startPoxY+31, 3, 3); + } + } + } +} \ No newline at end of file diff --git a/src/Entities/CountPortholes.java b/src/Entities/CountPortholes.java new file mode 100644 index 0000000..6a2b4bd --- /dev/null +++ b/src/Entities/CountPortholes.java @@ -0,0 +1,6 @@ +package Entities; +public enum CountPortholes { + Ten, + Twenty, + Thirty; +} \ No newline at end of file diff --git a/src/Entities/EntityAirbus.java b/src/Entities/EntityAirbus.java new file mode 100644 index 0000000..1f4b8a7 --- /dev/null +++ b/src/Entities/EntityAirbus.java @@ -0,0 +1,47 @@ +package Entities; + +import java.awt.*; + +public class EntityAirbus { + private int Speed; + private float Weight; + private Color BodyColor; + private Color AdditionalColor; + private boolean IsCompartment; + private boolean IsAdditionalEngine; + + public int Step; + + public int getSpeed() { + return Speed; + } + public float getWeight() { + return Weight; + } + public Color getBodyColor() { + return BodyColor; + } + public Color getAdditionalColor() { + return AdditionalColor; + } + public boolean IsCompartment() { + return IsCompartment; + } + public boolean IsAdditionalEngine() { + return IsAdditionalEngine; + } + + + public void Init(int speed, float weight, Color bodyColor, Color additionalColor, boolean isCompartment, boolean isAdditionalEngine) + { + + Weight = weight; + Speed = speed; + BodyColor = bodyColor; + AdditionalColor = additionalColor; + IsCompartment = isCompartment; + IsAdditionalEngine = isAdditionalEngine; + + Step = Speed * 100 / (int) Weight; + } +} diff --git a/src/FormAirbus.java b/src/FormAirbus.java new file mode 100644 index 0000000..05d6c5e --- /dev/null +++ b/src/FormAirbus.java @@ -0,0 +1,151 @@ +import java.awt.*; +import javax.swing.*; +import java.awt.event.*; +import java.util.Random; +import Drawnings.*; +import MovementStrategy.*; + +public class FormAirbus extends JFrame { + + private DrawingAirbus _drawningAirbus; + private Canvas canvas = new Canvas(); + + JLabel labelCount = new JLabel("Введите число иллюминаторов:"); + private JTextField fieldCount = new JTextField(); + private JButton buttonCreate; + private JButton buttonUp; + private JButton buttonDown; + private JButton buttonRight; + private JButton buttonLeft; + + public FormAirbus() { + super("Создание самолёта"); + InitializeComponent(); + setVisible(true); + } + + private void InitializeComponent() + { + buttonCreate = new JButton("Создать самолёт"); + + buttonUp = new JButton(); + buttonUp.setName("up"); + buttonUp.setIcon(new ImageIcon("images\\KeyUp.png")); + buttonUp.setSize(48, 44); + + buttonRight = new JButton(); + buttonRight.setName("right"); + buttonRight.setIcon(new ImageIcon("images\\KeyRight.png")); + buttonRight.setSize(48, 44); + + buttonLeft = new JButton(); + + buttonLeft.setName("left"); + buttonLeft.setIcon(new ImageIcon("images\\KeyLeft.png")); + buttonLeft.setSize(48, 44); + + buttonDown = new JButton(); + buttonDown.setName("down"); + buttonDown.setIcon(new ImageIcon("images\\KeyDown.png")); + buttonDown.setSize(48, 44); + + setSize(800,500); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setLayout(null); + + buttonCreate.setBounds(12, 355, 170, 44); + buttonUp.setBounds(679, 313, 48, 44); + buttonRight.setBounds( 728, 358, 48, 44); + buttonLeft.setBounds(630, 358, 48, 44); + buttonDown.setBounds( 679, 358, 48, 44); + labelCount.setBounds(12, 405, 240, 20); + fieldCount.setBounds(210, 407, 48, 20); + canvas.setBounds(0,0,800, 460); + + add(buttonCreate); + add(buttonUp); + add(buttonRight); + add(buttonDown); + add(buttonLeft); + add(labelCount); + add(fieldCount); + add(canvas); + + buttonCreate.addActionListener(buttonCreateListener); + buttonUp.addActionListener(buttonsMoveListener); + buttonRight.addActionListener(buttonsMoveListener); + buttonDown.addActionListener(buttonsMoveListener); + buttonLeft.addActionListener(buttonsMoveListener); + } + + ActionListener buttonCreateListener = new ActionListener() { + public void actionPerformed(ActionEvent e) { + int countPortholes; + try + { + countPortholes = Integer.parseInt(fieldCount.getText()); + } + catch (Exception ex) + { + countPortholes = 0; + } + if (countPortholes != 10 && countPortholes != 20 && countPortholes != 30) + { + JOptionPane.showMessageDialog(null, "Число должно быть равно 10, 20 или 30.\nКол-во иллюминаторов приравнено к 10"); + countPortholes = 10; + } + + Random rand = new Random(); + _drawningAirbus = new DrawingAirbus(); + _drawningAirbus.Init(rand.nextInt(200) + 100, rand.nextInt(2000) + 1000, + new Color(rand.nextInt(256),rand.nextInt(256),rand.nextInt(256)), + new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)), + rand.nextBoolean(), rand.nextBoolean(), + countPortholes, + canvas.getWidth(), canvas.getHeight()); + + _drawningAirbus.SetPosition(rand.nextInt(100) + 10, rand.nextInt(100) + 10); + canvas.repaint(); + } + }; + + ActionListener buttonsMoveListener = new ActionListener() { + // реакция на нажатие + public void actionPerformed(ActionEvent e) { + if (_drawningAirbus == null) + { + return; + } + String command = ((JButton)(e.getSource())).getName(); + switch (command) { + case "up": + _drawningAirbus.MoveTransport(Direction.Up); + break; + case "down": + _drawningAirbus.MoveTransport(Direction.Down); + break; + case "right": + _drawningAirbus.MoveTransport(Direction.Right); + break; + case "left": + _drawningAirbus.MoveTransport(Direction.Left); + break; + } + canvas.repaint(); + } + }; + + class Canvas extends JComponent{ + public Canvas(){ + } + public void paintComponent (Graphics g){ + if (_drawningAirbus == null){ + return; + } + super.paintComponents (g) ; + Graphics2D g2d = (Graphics2D)g; + _drawningAirbus.DrawTransport(g2d); + super.repaint(); + } + } +} diff --git a/src/Main.java b/src/Main.java new file mode 100644 index 0000000..03af442 --- /dev/null +++ b/src/Main.java @@ -0,0 +1,5 @@ +public class Main { + public static void main(String[] args) { + new FormAirbus(); + } +} \ No newline at end of file diff --git a/src/MovementStrategy/Direction.java b/src/MovementStrategy/Direction.java new file mode 100644 index 0000000..3441376 --- /dev/null +++ b/src/MovementStrategy/Direction.java @@ -0,0 +1,7 @@ +package MovementStrategy; +public enum Direction { + Up, + Down, + Left, + Right; +} \ No newline at end of file