diff --git a/.idea/vcs.xml b/.idea/vcs.xml index a745119..94a25f7 100644 --- a/.idea/vcs.xml +++ b/.idea/vcs.xml @@ -1,6 +1,6 @@ - + \ 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..415ea9a --- /dev/null +++ b/src/Drawnings/DrawingAirbus.java @@ -0,0 +1,129 @@ +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 = 210; + private int _airbusHeight = 100; + + 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 > _pictureWidth || y > _pictureHeight || x < 0 || y < 0) + { + _startPosX = 0; + _startPosY = 0; + } + else + { + _startPosX = x; + _startPosY = y; + } + } + + public void MoveTransport(Direction direction){ + if (entityAirbus == null) { + return; + } + + switch (direction) { + case Left: + if (_startPosX - entityAirbus.Step > 5) + { + _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; + } + // иллюминаторы + _portholes.Draw(g, _startPosX+30, _startPosY+34); + + g.setColor(Color.BLACK); +//Тело + g.drawRect(_startPosX + 5, _startPosY + 50, 170, 30); + g.drawArc(_startPosX - 5, _startPosY + 50, 20, 30, 90, 180); +//Заднее крыло + g.drawLine( _startPosX, _startPosY, _startPosX + 50, _startPosY + 50); + g.drawLine( _startPosX, _startPosY, _startPosX, _startPosY + 52); +//Заднее боковые крылья + g.drawOval(_startPosX - 7, _startPosY + 45, 30, 8); + g.fillOval(_startPosX - 7, _startPosY + 45, 30, 8); +//Нос + g.drawLine( _startPosX + 175, _startPosY + 50, _startPosX + 200, _startPosY + 65); + g.drawLine( _startPosX + 200, _startPosY + 65, _startPosX + 175, _startPosY + 80); + g.drawLine( _startPosX + 175, _startPosY + 50, _startPosX + 175, _startPosY + 80); + g.drawLine( _startPosX + 175, _startPosY + 65, _startPosX + 200, _startPosY + 65); +//Крылья + g.drawArc(_startPosX + 49, _startPosY + 62, 5, 5, 90, 180); + g.drawLine( _startPosX + 51, _startPosY + 62, _startPosX + 140, _startPosY + 62); + g.drawArc( _startPosX + 137, _startPosY + 62, 5, 5, 2790, 180); + g.drawLine( _startPosX + 51, _startPosY + 67, _startPosX + 140, _startPosY + 67); +//Задние шасси + g.drawLine(_startPosX + 55, _startPosY + 80, _startPosX + 55, _startPosY + 90); + g.drawOval(_startPosX + 47, _startPosY + 90, 5, 5); + g.drawOval( _startPosX + 57, _startPosY + 90, 5, 5); +//Передние шасси + g.drawLine( _startPosX + 165, _startPosY + 80, _startPosX + 165, _startPosY + 90); + g.drawOval( _startPosX + 163, _startPosY + 91, 5, 5); +//Пассажирсакий доп. отсек + if (entityAirbus.IsCompartment()) + { + g.setColor(entityAirbus.getAdditionalColor()); + g.drawArc(_startPosX + 60, _startPosY + 28, 115, 45, 0, 180); + g.fillArc(_startPosX + 60, _startPosY + 28, 115, 45, 0, 180); + } +// Доп. двигатели + if (entityAirbus.IsAdditionalEngine()) + { + g.drawLine(_startPosX + 95, _startPosY + 68, _startPosX + 95, _startPosY + 75); + g.setColor(entityAirbus.getAdditionalColor()); + int[] xPolygon = { _startPosX + 83, _startPosX + 103, _startPosX + 103, _startPosX + 83, _startPosX + 83}; + int[] yPolygon = {_startPosY + 78, _startPosY + 73, _startPosY + 93, _startPosY + 88, _startPosY + 78}; + g.drawPolygon(xPolygon, yPolygon, xPolygon.length); + g.fillPolygon(xPolygon, yPolygon, xPolygon.length); + } + } +} diff --git a/src/Drawnings/DrawingPortholes.java b/src/Drawnings/DrawingPortholes.java new file mode 100644 index 0000000..9810120 --- /dev/null +++ b/src/Drawnings/DrawingPortholes.java @@ -0,0 +1,67 @@ +package Drawnings; + +import java.awt.*; +import Entities.*; + +public class DrawingPortholes { + private CountPortholes _porthole; + public CountPortholes getCount() + { + return _porthole; + } + public void SetCount (int 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 == null) {return;} + + 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.Ten) { + for (int i=0; i < 5; ++i) + { + g.setColor(Color.cyan); + g.fillOval(_startPosx - 15 + i*5, _startPoxY+28, 3, 3); + g.setColor(Color.black); + g.drawOval(_startPosx - 15 + i*5, _startPoxY+28, 3, 3); + } + for (int i=0; i < 5; ++i) + { + g.setColor(Color.cyan); + g.fillOval(_startPosx + 115 + i*5, _startPoxY+28, 3, 3); + g.setColor(Color.black); + g.drawOval(_startPosx + 115 + i*5, _startPoxY+28, 3, 3); + } + } + if (_porthole == CountPortholes.Thirty){ + for (int i = 0; i < 10; ++i) + { + g.setColor(Color.cyan); + g.fillOval(_startPosx + 19 + i*8, _startPoxY+37, 3, 3); + g.setColor(Color.black); + g.drawOval(_startPosx + 19 + i*8, _startPoxY+37, 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..33a4dcf --- /dev/null +++ b/src/FormAirbus.java @@ -0,0 +1,150 @@ +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 index aedd9cd..03af442 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,17 +1,5 @@ -// Press Shift twice to open the Search Everywhere dialog and type `show whitespaces`, -// then press Enter. You can now see whitespace characters in your code. public class Main { public static void main(String[] args) { - // Press Alt+Enter with your caret at the highlighted text to see how - // IntelliJ IDEA suggests fixing it. - System.out.printf("Hello and welcome!"); - - // Press Shift+F10 or click the green arrow button in the gutter to run the code. - for (int i = 1; i <= 5; i++) { - - // Press Shift+F9 to start debugging your code. We have set one breakpoint - // for you, but you can always add more by pressing Ctrl+F8. - System.out.println("i = " + i); - } + 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