diff --git a/Project/.idea/uiDesigner.xml b/Project/.idea/uiDesigner.xml index e96534f..013f18e 100644 --- a/Project/.idea/uiDesigner.xml +++ b/Project/.idea/uiDesigner.xml @@ -14,18 +14,18 @@ - - - - - - + + + + + + diff --git a/Project/src/.idea/workspace.xml b/Project/src/.idea/workspace.xml new file mode 100644 index 0000000..f0b5579 --- /dev/null +++ b/Project/src/.idea/workspace.xml @@ -0,0 +1,49 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1667501112705 + + + + \ No newline at end of file diff --git a/Project/src/Additional_Enum.java b/Project/src/Additional_Enum.java new file mode 100644 index 0000000..7795957 --- /dev/null +++ b/Project/src/Additional_Enum.java @@ -0,0 +1,16 @@ +public enum Additional_Enum { + One(1), + Two(2), + Three(3); + + private final int addEnum; + + Additional_Enum(int i) + { + this.addEnum = i; + } + + public int GetAddEnum(){ + return addEnum; + } +} diff --git a/Project/src/Direction.java b/Project/src/Direction.java new file mode 100644 index 0000000..6716191 --- /dev/null +++ b/Project/src/Direction.java @@ -0,0 +1,17 @@ +public enum Direction { + Up(1), + Down(2), + Left(3), + Right(4); + + private final int DirectionCode; + + private Direction(int directionCode) + { + this.DirectionCode = directionCode; + } + + public int GetDirect(){ + return DirectionCode; + } +} diff --git a/Project/src/DrawingAirplaneWindow.java b/Project/src/DrawingAirplaneWindow.java new file mode 100644 index 0000000..ec6a8ab --- /dev/null +++ b/Project/src/DrawingAirplaneWindow.java @@ -0,0 +1,55 @@ +import javax.swing.*; +import java.awt.*; + +public class DrawingAirplaneWindow extends JComponent +{ + private Additional_Enum _airplaneWindowEnum; + + public void SetAddEnum(int decksAmount) { + for(Additional_Enum item : _airplaneWindowEnum.values()) + { + if(item.GetAddEnum() == decksAmount) + { + _airplaneWindowEnum = item; + return; + } + } + } + + public void DrawAirplaneWindow(Color colorWindow, Graphics g, float _startPosX, float _startPosY) + { + super.paintComponent(g); + Graphics2D g2d = (Graphics2D) g; + + switch(_airplaneWindowEnum.GetAddEnum()) + { + case 1: + g2d.setPaint(colorWindow); + g2d.fillOval((int)_startPosX + 9, (int)_startPosY + 11, 6, 4); + + g2d.setPaint(Color.BLACK); + g2d.drawOval((int)_startPosX + 9, (int)_startPosY + 11, 6, 4); + break; + case 2: + g2d.setPaint(colorWindow); + g2d.fillOval((int)_startPosX + 9, (int)_startPosY + 11, 6, 4); + g2d.fillOval((int)_startPosX + 18, (int)_startPosY + 11, 6, 4); + + g2d.setPaint(Color.BLACK); + g2d.drawOval((int)_startPosX + 9, (int)_startPosY + 11, 6, 4); + g2d.drawOval((int)_startPosX + 18, (int)_startPosY + 11, 6, 4); + break; + case 3: + g2d.setPaint(colorWindow); + g2d.fillOval((int)_startPosX + 9, (int)_startPosY + 11, 6, 4); + g2d.fillOval((int)_startPosX + 18, (int)_startPosY + 11, 6, 4); + g2d.fillOval((int)_startPosX + 27, (int)_startPosY + 11, 6, 4); + + g2d.setPaint(Color.BLACK); + g2d.drawOval((int)_startPosX + 9, (int)_startPosY + 11, 6, 4); + g2d.drawOval((int)_startPosX + 18, (int)_startPosY + 11, 6, 4); + g2d.drawOval((int)_startPosX + 27, (int)_startPosY + 11, 6, 4); + break; + } + } +} \ No newline at end of file diff --git a/Project/src/DrawingPlane.java b/Project/src/DrawingPlane.java new file mode 100644 index 0000000..30157bf --- /dev/null +++ b/Project/src/DrawingPlane.java @@ -0,0 +1,172 @@ +import java.awt.*; +import java.awt.Color; +import java.util.Random; + +public class DrawingPlane +{ + public EntityPlane Airbus; //класс-сущность + public DrawingAirplaneWindow _airplaneWindow; //для дополнительной отрисовки + + private float _startPosX; //левая координата отрисовки + private float _startPosY; //верхняя координата отрисовки + private Integer _pictureWidth = null; //ширина окна отрисовки + private Integer _pictureHeight = null; //высота окна отрисовки + protected final int _airbusWidth = 50; //ширина отрисовки самолёта + protected final int _airbusHeight = 16; //высота отрисовки самолёта + + //инициализаци свойств + public void Initial(int speed, float weight, Color corpusColor) + { + Airbus = new EntityPlane(); + Airbus.Initial(speed, weight, corpusColor); + _airplaneWindow = new DrawingAirplaneWindow(); + + SetAddEnum(); + } + + //установка кол-ва иллюминаторов + public void SetAddEnum() + { + Random rnd = new Random(); + int countWindow = rnd.nextInt(1, 4); + _airplaneWindow.SetAddEnum(countWindow); + } + + public EntityPlane GetPlane() + { + return Airbus; + } + + //установка координат позиции самолёта + public void SetPosition(int x, int y, int width, int height) + { + if (x < 0 || y < 0 || width <= x + _airbusWidth || height <= y + _airbusHeight) + { + _pictureHeight = null; + _pictureWidth = null; + + return; + } + + _startPosX = x; + _startPosY = y; + _pictureWidth = width; + _pictureHeight = height; + } + + //изменение направления движения + public void MoveTransport(Direction direction) + { + if (GetPlane() == null) + { + return; + } + + switch (direction) + { + case Right: //вправо + if (_startPosX + _airbusWidth + Airbus.GetStep() < _pictureWidth) + { + _startPosX += Airbus.GetStep(); + } + break; + + case Left: //влево + if (_startPosX - _airbusWidth - Airbus.GetStep() > 0) + { + _startPosX -= Airbus.GetStep(); + } + break; + + case Up: //вверх + if (_startPosY - _airbusHeight - Airbus.GetStep() > 0) + { + _startPosY -= Airbus.GetStep(); + } + break; + + case Down: //вниз + if (_startPosY + _airbusHeight + Airbus.GetStep() < _pictureHeight) + { + _startPosY += Airbus.GetStep(); + } + break; + } + } + + public void DrawTransport(Graphics g) + { + if (_startPosX < 0 || _startPosY < 0 || _pictureHeight == null || _pictureWidth == null) + { + return; + } + + if(GetPlane() == null){ + return; + } + + Graphics2D g2d = (Graphics2D)g; + + g2d.setColor(Color.WHITE); + g2d.fillRect(0, 0, _pictureWidth, _pictureHeight); + + //заливает фигуру + g2d.setPaint(Color.BLACK); + + //крыло + g2d.fillRect((int)_startPosX + 10, (int)_startPosY + 15, 20, 2); + g2d.fillRect((int)_startPosX + 12, (int)_startPosY + 17, 16, 2); + + //заднее поперечное крыло + g2d.fillOval((int)_startPosX - 3, (int)_startPosY + 7, 10, 5); + + //задние шасси + g2d.fillRect((int)_startPosX + 10, (int)_startPosY + 21, 2, 2); + g2d.fillRect((int)_startPosX + 12, (int)_startPosY + 23, 4, 4); + g2d.fillRect((int)_startPosX + 8, (int)_startPosY + 23, 2, 4); + + //переднее шасси + g2d.fillRect((int)_startPosX + 35, (int)_startPosY + 21, 2, 2); + g2d.fillRect((int)_startPosX + 35, (int)_startPosY + 23, 4, 4); + + g2d.setColor(Color.BLACK); //рисует контур + //корпус + g2d.drawRect((int)_startPosX, (int)_startPosY + 10, 40, 10); + + //хвостовое крыло + g2d.drawLine((int)_startPosX, (int)_startPosY + 10, (int)_startPosX, (int)_startPosY); + g2d.drawLine((int)_startPosX, (int)_startPosY, (int)_startPosX + 10, (int)_startPosY + 10); + + //нос самолёта + g2d.drawLine((int)_startPosX + 40, (int)_startPosY + 15, (int)_startPosX + 50, (int)_startPosY + 16); + g2d.drawLine((int)_startPosX + 40, (int)_startPosY + 10, (int)_startPosX + 47, (int)_startPosY + 15); + g2d.drawLine((int)_startPosX + 40, (int)_startPosY + 20, (int)_startPosX + 50, (int)_startPosY + 15); + + //отрисовка иллюминаторов + _airplaneWindow.DrawAirplaneWindow(Airbus.GetColor(), g, _startPosX, _startPosY); + } + + //смена границ формы отрисовки + public void ChangeBorders(int width, int height) + { + _pictureWidth = width; + _pictureHeight = height; + + if (_pictureWidth <= _airbusWidth || _pictureHeight <= _airbusHeight) + { + _pictureWidth = null; + _pictureHeight = null; + return; + } + + if (_startPosX + _airbusWidth > _pictureWidth) + { + _startPosX = _pictureWidth - _airbusWidth; + } + + if (_startPosY + _airbusHeight > _pictureHeight) + { + _startPosY = _pictureHeight - _airbusHeight; + } + } +} \ No newline at end of file diff --git a/Project/src/EntityPlane.java b/Project/src/EntityPlane.java new file mode 100644 index 0000000..38df649 --- /dev/null +++ b/Project/src/EntityPlane.java @@ -0,0 +1,31 @@ +import java.awt.*; + +public class EntityPlane +{ + public int Speed; //скорость + public int GetSpeed(){ + return Speed; + } + + public float Weight; //вес + public float GetWeight(){ + return Weight; + } + + public Color CorpusColor; //цвет корпуса + public Color GetColor(){ + return CorpusColor; + } + + //шаг перемещения самолёта + public float GetStep(){ + return Speed * 100 / Weight; + } + + public void Initial(int speed, float weight, Color corpusColor) + { + Speed = speed; + Weight = weight; + CorpusColor = corpusColor; + } +} \ No newline at end of file diff --git a/Project/src/FormPlane.form b/Project/src/FormPlane.form new file mode 100644 index 0000000..93521e4 --- /dev/null +++ b/Project/src/FormPlane.form @@ -0,0 +1,111 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/Project/src/FormPlane.java b/Project/src/FormPlane.java new file mode 100644 index 0000000..28e1fce --- /dev/null +++ b/Project/src/FormPlane.java @@ -0,0 +1,120 @@ +import javax.imageio.ImageIO; +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.ComponentAdapter; +import java.awt.event.ComponentEvent; +import java.util.Random; + +public class FormPlane +{ + protected DrawingPlane plane = new DrawingPlane(); + + public JPanel MainPanel; + private JButton ButtonCreate; + private JButton ButtonLeft; + private JButton ButtonDown; + private JButton ButtonRight; + private JButton ButtonUp; + private JToolBar StatusStrip; + private JPanel PictureBox; + private JLabel LabelSpeed = new JLabel(); + private JLabel LabelWeight = new JLabel(); + private JLabel LabelColor = new JLabel(); + + public FormPlane() + { + //создание строки отображения скорости, веса и цвета объекта + Box LableBox = Box.createHorizontalBox(); + LableBox.setMinimumSize(new Dimension(1, 20)); + LableBox.add(LabelSpeed); + LableBox.add(LabelWeight); + LableBox.add(LabelColor); + StatusStrip.add(LableBox); + + try + { + Image img = ImageIO.read(getClass().getResource("resourses/Up.png")); + ButtonUp.setIcon(new ImageIcon(img)); + img = ImageIO.read(getClass().getResource("resourses/Left.png")); + ButtonLeft.setIcon(new ImageIcon(img)); + img = ImageIO.read(getClass().getResource("resourses/Down.png")); + ButtonDown.setIcon(new ImageIcon(img)); + img = ImageIO.read(getClass().getResource("resourses/Right.png")); + ButtonRight.setIcon(new ImageIcon(img)); + } catch (Exception ex){ + System.out.println(ex.getMessage()); + } + + + ButtonCreate.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + plane = new DrawingPlane(); + Random rnd = new Random(); + plane.Initial(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000), + new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256))); + plane.SetPosition(rnd.nextInt(10, 100), rnd.nextInt(10, 100), + PictureBox.getWidth(), PictureBox.getHeight()); + LabelSpeed.setText("Скорость: " + plane.GetPlane().GetSpeed() + " "); + LabelWeight.setText("Вес: " + plane.GetPlane().GetWeight() + " "); + LabelColor.setText("Цвет: r = " + plane.GetPlane().GetColor().getRed() + " g = " + plane.GetPlane().GetColor().getGreen() + + " b = " + plane.GetPlane().GetColor().getBlue()); + Draw(); + } + }); + + //обновление размеров формы + MainPanel.addComponentListener(new ComponentAdapter() { + @Override + public void componentResized(ComponentEvent e) { + super.componentResized(e); + plane.ChangeBorders(PictureBox.getWidth(), PictureBox.getHeight()); + Draw(); + } + }); + + ButtonUp.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + plane.MoveTransport(Direction.Up); + Draw(); + } + }); + + ButtonLeft.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + plane.MoveTransport(Direction.Left); + Draw(); + } + }); + + ButtonDown.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + plane.MoveTransport(Direction.Down); + Draw(); + } + }); + + ButtonRight.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + plane.MoveTransport(Direction.Right); + Draw(); + } + }); + } + + public void Draw() + { + if(plane.GetPlane() == null) + { + return; + } + + plane.DrawTransport(PictureBox.getGraphics()); + } +} \ No newline at end of file diff --git a/Project/src/Main.java b/Project/src/Main.java new file mode 100644 index 0000000..9a6fc91 --- /dev/null +++ b/Project/src/Main.java @@ -0,0 +1,13 @@ +import javax.swing.*; + +public class Main { + public static void main(String[] args) { + JFrame frame = new JFrame("Airbus"); + frame.setContentPane(new FormPlane().MainPanel); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + frame.setLocation(500, 200); + frame.pack(); + frame.setSize(1000, 500); + frame.setVisible(true); + } +} \ No newline at end of file diff --git a/Project/src/resourses/Down.png b/Project/src/resourses/Down.png new file mode 100644 index 0000000..d80a2d8 Binary files /dev/null and b/Project/src/resourses/Down.png differ diff --git a/Project/src/resourses/Left.png b/Project/src/resourses/Left.png new file mode 100644 index 0000000..4823dd9 Binary files /dev/null and b/Project/src/resourses/Left.png differ diff --git a/Project/src/resourses/Right.png b/Project/src/resourses/Right.png new file mode 100644 index 0000000..b738c22 Binary files /dev/null and b/Project/src/resourses/Right.png differ diff --git a/Project/src/resourses/Up.png b/Project/src/resourses/Up.png new file mode 100644 index 0000000..6b987e6 Binary files /dev/null and b/Project/src/resourses/Up.png differ