diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..e112a70 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,7 @@ +{ + "java.project.sourcePaths": ["src"], + "java.project.outputPath": "bin", + "java.project.referencedLibraries": [ + "lib/**/*.jar" + ] +} diff --git a/bin/CountPorthole.class b/bin/CountPorthole.class new file mode 100644 index 0000000..9865a3c Binary files /dev/null and b/bin/CountPorthole.class differ diff --git a/bin/Direction.class b/bin/Direction.class new file mode 100644 index 0000000..605bf53 Binary files /dev/null and b/bin/Direction.class differ diff --git a/bin/DrawingAirbus.class b/bin/DrawingAirbus.class new file mode 100644 index 0000000..f26ad62 Binary files /dev/null and b/bin/DrawingAirbus.class differ diff --git a/bin/DrawingField.class b/bin/DrawingField.class new file mode 100644 index 0000000..b51099e Binary files /dev/null and b/bin/DrawingField.class differ diff --git a/bin/DrawingPorthole.class b/bin/DrawingPorthole.class new file mode 100644 index 0000000..78426b3 Binary files /dev/null and b/bin/DrawingPorthole.class differ diff --git a/bin/EntityAirbus.class b/bin/EntityAirbus.class new file mode 100644 index 0000000..27f6c72 Binary files /dev/null and b/bin/EntityAirbus.class differ diff --git a/bin/FormAirbus$1.class b/bin/FormAirbus$1.class new file mode 100644 index 0000000..7265a04 Binary files /dev/null and b/bin/FormAirbus$1.class differ diff --git a/bin/FormAirbus$ButtonsListener.class b/bin/FormAirbus$ButtonsListener.class new file mode 100644 index 0000000..329302a Binary files /dev/null and b/bin/FormAirbus$ButtonsListener.class differ diff --git a/bin/FormAirbus.class b/bin/FormAirbus.class new file mode 100644 index 0000000..b60c311 Binary files /dev/null and b/bin/FormAirbus.class differ diff --git a/bin/Main.class b/bin/Main.class new file mode 100644 index 0000000..7982723 Binary files /dev/null and b/bin/Main.class differ diff --git a/images/KeyDown.jpg b/images/KeyDown.jpg new file mode 100644 index 0000000..d636d38 Binary files /dev/null and b/images/KeyDown.jpg differ diff --git a/images/KeyLeft.jpg b/images/KeyLeft.jpg new file mode 100644 index 0000000..dd31cce Binary files /dev/null and b/images/KeyLeft.jpg differ diff --git a/images/KeyRight.jpg b/images/KeyRight.jpg new file mode 100644 index 0000000..e24b05b Binary files /dev/null and b/images/KeyRight.jpg differ diff --git a/images/KeyUp.jpg b/images/KeyUp.jpg new file mode 100644 index 0000000..046f5d5 Binary files /dev/null and b/images/KeyUp.jpg differ diff --git a/src/CountPorthole.java b/src/CountPorthole.java new file mode 100644 index 0000000..95f8a66 --- /dev/null +++ b/src/CountPorthole.java @@ -0,0 +1,16 @@ +public enum CountPorthole { + Ten(10), + Twenty(20), + Thirty(30); + + // значение + private final int Count; + + CountPorthole(int count) { + Count = count; + } + + public int getCountPorthole() { + return Count; + } +} \ No newline at end of file diff --git a/src/Direction.java b/src/Direction.java new file mode 100644 index 0000000..f6c0152 --- /dev/null +++ b/src/Direction.java @@ -0,0 +1,6 @@ +public enum Direction { + Up, + Down, + Left, + Right; +} \ No newline at end of file diff --git a/src/DrawingAirbus.java b/src/DrawingAirbus.java new file mode 100644 index 0000000..337429b --- /dev/null +++ b/src/DrawingAirbus.java @@ -0,0 +1,153 @@ +import java.awt.*; +import java.util.Random; + +public class DrawingAirbus { + + public EntityAirbus Airbus; + public EntityAirbus getAirbus() { + return Airbus; + } + + public DrawingPorthole _porthole; + private int _startPosX; + private int _startPosY; + private Integer _pictureWidth = null; + private Integer _pictureHeight = null; + private final int _airbusWidth = 150; + private final int _airbusHeight = 50; + + // инициализация + public void Init(int speed, float weight, Color bodyColor, Color additionalColor, boolean isCompartment, boolean isadditionalEngine) { + EntityAirbus Airbus = new EntityAirbus(); + Airbus.Init(speed, weight, bodyColor, additionalColor, isCompartment, isadditionalEngine); + Random rand = new Random(); + _porthole = new DrawingPorthole(); + _porthole.SetCountPorthole(rand.nextInt(3)*10); + } + + public void SetPosition (int x, int y, int width, int height) { + if ( x>= 0 && x + _airbusWidth <= width && y>= 0 && _airbusHeight <= height) { + _startPosX = x; + _startPosY = y; + _pictureWidth = width; + _pictureHeight = height; + } + else return; + } + + public void MoveTransport(Direction direction){ + if (_pictureWidth == null || _pictureHeight == null) { + return; + } + + switch (direction) { + case Left: + if (_startPosX - Airbus.Step > 0) + { + _startPosX -= Airbus.Step; + } + break; + case Right: + if (_startPosX + Airbus.Step <= _pictureWidth) + { + _startPosX += Airbus.Step; + } + break; + case Up: + if (_startPosY - Airbus.Step > 0) + { + _startPosY -= Airbus.Step; + } + break; + case Down: + if (_startPosY + Airbus.Step <= _pictureHeight) + { + _startPosY += Airbus.Step; + } + break; + } + } + + public void DrawTransport(Graphics2D g) { + + if (_startPosX < 0 || _startPosY < 0 || _pictureHeight == null || _pictureWidth == null) { + return; + } + + // пассажирский отсек + if (Airbus.IsCompartment()) + { + g.setColor(Color.BLACK); + g.drawRect(_startPosX + 51, _startPosY + 10, 24, 10); + g.setColor(Airbus.getBodyColor()); + g.fillRect(_startPosX + 51, _startPosY + 10, 24, 10); + } + + // доп двигатель + if (Airbus.IsAdditionalEngine()) + { + g.setColor(Color.BLACK); + g.drawOval(_startPosX, _startPosY + 20, 11, 5); + g.setColor(Airbus.getAdditionalColor()); + g.fillOval(_startPosX, _startPosY + 20, 11, 5); + } + + //// ЛАЙН + g.setColor(Color.BLACK); + // нос + int[] xPolygonNoise = {_startPosX + 74, _startPosX + 88, _startPosX + 74}; + int[] yPolygonNoise = { _startPosY + 22, _startPosY + 15, _startPosY + 27}; + g.drawPolygon(xPolygonNoise, yPolygonNoise, xPolygonNoise.length); + // хвост + int[] xPolygonTale = { _startPosX + 4, _startPosX + 3, _startPosX + 21 }; + int[] yPolygonTale = { _startPosY + 17, _startPosY, _startPosY + 17 }; + g.drawPolygon(xPolygonTale, yPolygonTale, xPolygonTale.length); + // тело + g.drawRect(_startPosX + 2, _startPosY + 14, 72, 14); + // шасси + g.drawOval(_startPosX + 21, _startPosY + 30, 3, 3); + g.drawOval(_startPosX + 25, _startPosY + 30, 3, 3); + g.drawOval(_startPosX + 70, _startPosY + 30, 3, 3); + // крыло + g.drawOval(_startPosX + 24, _startPosY + 20, 31, 4); + // двигатель + g.drawOval(_startPosX, _startPosY + 14, 14, 5); + + //// ПОКРАСКА + // Основной цвет + g.setColor(Airbus.getBodyColor()); + // тело + g.fillRect(_startPosX + 2, _startPosY + 14, 72, 14); + // нос + g.fillPolygon(xPolygonNoise, yPolygonNoise, xPolygonNoise.length); + // хвост + g.fillPolygon(xPolygonTale, yPolygonTale, xPolygonTale.length); + // двигатель + g.fillOval(_startPosX, _startPosY + 14, 14, 5); + + // Доп цвет + g.setColor(Airbus.getAdditionalColor()); + // шасси + g.fillOval(_startPosX + 21, _startPosY + 30, 3, 3); + g.fillOval(_startPosX + 25, _startPosY + 30, 3, 3); + g.fillOval(_startPosX + 70, _startPosY + 30, 3, 3); + // крыло + g.fillOval(_startPosX + 24, _startPosY + 20, 31, 4); + } + + public void ChangeBorders(int width, int height) { + _pictureWidth = width; + _pictureHeight = height; + if (_pictureWidth <= _airbusWidth || _pictureHeight <= _airbusHeight) + { + _pictureHeight = null; + _pictureWidth = null; + } + if (_startPosX + _airbusWidth > _pictureWidth) { + _startPosX = _pictureWidth - _airbusWidth; + } + if (_startPosY + _airbusHeight > _pictureHeight) { + _startPosY = _pictureHeight - _airbusHeight; + } + } +} diff --git a/src/DrawingField.java b/src/DrawingField.java new file mode 100644 index 0000000..619be3f --- /dev/null +++ b/src/DrawingField.java @@ -0,0 +1,68 @@ +import java.awt.*; +import javax.swing.*; +import java.util.Random; + +public class DrawingField extends JPanel { + + private final FormAirbus field; + DrawingAirbus _airbus; + + public DrawingField(FormAirbus field) { + this.field = field; + } + + @Override + protected void paintComponent(Graphics g) { + super.paintComponent(g); + Graphics2D g2 =(Graphics2D)g; + if (_airbus != null) + _airbus.DrawTransport(g2); + else + return; + } + + public void UpButtonAction() { + if (_airbus != null) + _airbus.MoveTransport(Direction.Up); + else + return; + } + + public void DownButtonAction() { + if (_airbus != null) + _airbus.MoveTransport(Direction.Down); + else + return; + } + + public void RightButtonAction() { + if (_airbus != null) + _airbus.MoveTransport(Direction.Right); + else + return; + } + + public void LeftButtonAction() { + if (_airbus != null) + _airbus.MoveTransport(Direction.Left); + else + return; + } + + public void CreateButtonAction() { + Random rand = new Random(); + _airbus = new DrawingAirbus(); + _airbus.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)), + false, false); + + _airbus.SetPosition(rand.nextInt(100) + 10, rand.nextInt(100) + 10, getWidth(), getHeight()); + } + + public void ResizeField(){ + if (_airbus != null) + _airbus.ChangeBorders(getWidth(),getHeight()); + else return; + } +} diff --git a/src/DrawingPorthole.java b/src/DrawingPorthole.java new file mode 100644 index 0000000..97fa11b --- /dev/null +++ b/src/DrawingPorthole.java @@ -0,0 +1,36 @@ +import java.awt.*; + +public class DrawingPorthole { + private CountPorthole _porthole; + + public void SetCountPorthole (int Count) { + // проход по значениям + for (CountPorthole c: CountPorthole.values()) { + if (c.getCountPorthole() == Count) { + _porthole = c; + return; + } + return; + } + } + + public void Draw (Graphics2D g, Color color, int _startPosx, int _startPoxY) { + switch (_porthole.getCountPorthole()) { + case 10: + g.setColor(Color.BLACK); + // цикл + g.setColor(color); + break; + case 20: + g.setColor(Color.BLACK); + + g.setColor(color); + break; + case 30: + g.setColor(Color.BLACK); + + g.setColor(color); + break; + } + } +} \ No newline at end of file diff --git a/src/EntityAirbus.java b/src/EntityAirbus.java new file mode 100644 index 0000000..3f5329a --- /dev/null +++ b/src/EntityAirbus.java @@ -0,0 +1,54 @@ +import java.awt.*; +import java.util.Random; + +public class EntityAirbus { + private int Speed; + private float Weight; + private Color BodyColor; + private Color AdditionalColor; + private boolean IsCompartment; + private boolean IsAdditionalEngine; + + public float 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) { + Random rand = new Random(); + + if (weight <= 0) + Weight = rand.nextInt(100) + 500; + else + Weight = weight; + + if (speed <= 0) + Speed = rand.nextInt(50) + 10; + else + 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..0d7117e --- /dev/null +++ b/src/FormAirbus.java @@ -0,0 +1,147 @@ +import java.awt.*; +import javax.swing.*; +import java.awt.event.*; + + +public class FormAirbus extends JFrame { + + private int Width; + private int Height; + + JPanel BottomAndCreatePanel = new JPanel(); + JPanel CreatePanel = new JPanel(); + JPanel DimentionPanel = new JPanel(); + JPanel UpPanel = new JPanel(); + JPanel DownPanel = new JPanel(); + JPanel RightLeftPanel = new JPanel(); + + DrawingField field = new DrawingField(this); + + JButton ButtonCreate = new JButton("Create"); + + Icon iconUp = new ImageIcon("images\\KeyUp.jpg"); + JButton ButtonUp=new JButton(iconUp); + + Icon iconDown = new ImageIcon("images\\KeyDown.jpg"); + JButton ButtonDown=new JButton(iconDown); + + Icon iconRight = new ImageIcon("images\\KeyRight.jpg"); + JButton ButtonRight=new JButton(iconRight); + + Icon iconLeft = new ImageIcon("images\\KeyLeft.jpg"); + JButton ButtonLeft=new JButton(iconLeft); + + public FormAirbus() { + super("Airbus Hard"); + + setSize(800,600); + Width = getWidth(); + Height = getHeight(); + ShowWindow(); + RefreshWindow(); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setVisible(true); + + } + + ActionListener myButtonsListener = new ButtonsListener(); + + public class ButtonsListener implements ActionListener { + // реакция на события + public void actionPerformed(ActionEvent e) { + String command = e.getActionCommand(); + switch (command) { + case "Вверх": + field.UpButtonAction(); + break; + case "Вниз": + field.DownButtonAction(); + break; + case "Вправо": + field.RightButtonAction(); + break; + case "Влево": + field.LeftButtonAction(); + break; + case "Создать": + field.CreateButtonAction(); + break; + } + repaint(); + } + } + + public void ShowWindow() { + Dimension dimen = new Dimension(30,30); + + ButtonUp.setPreferredSize(dimen); + ButtonUp.setActionCommand("Вверх"); + ButtonUp.addActionListener(myButtonsListener); + + ButtonDown.setPreferredSize(dimen); + ButtonDown.setActionCommand("Вниз"); + ButtonDown.addActionListener(myButtonsListener); + + ButtonRight.setPreferredSize(dimen); + ButtonRight.setActionCommand("Вправо"); + ButtonRight.addActionListener(myButtonsListener); + + ButtonLeft.setPreferredSize(dimen); + ButtonLeft.setActionCommand("Влево"); + ButtonLeft.addActionListener(myButtonsListener); + + ButtonCreate.setPreferredSize(dimen); + ButtonCreate.setActionCommand("Создать"); + ButtonCreate.addActionListener(myButtonsListener); + + RightLeftPanel.setLayout(new FlowLayout(FlowLayout.CENTER,50,0)); + RightLeftPanel.setBackground(new Color(80, 80, 255, 0)); + RightLeftPanel.add(ButtonLeft); + RightLeftPanel.add(ButtonRight); + + UpPanel.setLayout(new FlowLayout()); + UpPanel.setBackground(new Color(255, 255, 255, 0)); + UpPanel.add(ButtonUp); + + DownPanel.setLayout(new FlowLayout()); + DownPanel.setBackground(new Color(255, 255, 255, 0)); + DownPanel.add(ButtonDown); + + DimentionPanel.setLayout(new BoxLayout(DimentionPanel,BoxLayout.Y_AXIS)); + DimentionPanel.setBackground(new Color(0,0,0,0)); + DimentionPanel.add(UpPanel); + DimentionPanel.add(RightLeftPanel); + DimentionPanel.add(DownPanel); + add(DimentionPanel); + + CreatePanel.setLayout(new FlowLayout()); + CreatePanel.setBackground(new Color(0,0,0,0)); + CreatePanel.add(ButtonCreate); + + BottomAndCreatePanel.setLayout(new BoxLayout(BottomAndCreatePanel,BoxLayout.Y_AXIS)); + BottomAndCreatePanel.setBackground(new Color(0,0,0,0)); + BottomAndCreatePanel.add(CreatePanel); + + add(BottomAndCreatePanel); + add(field); + + addComponentListener(new ComponentAdapter() { + @Override + public void componentResized(ComponentEvent e) { + super.componentResized(e); + Width = getWidth(); + Height = getHeight(); + + field.ResizeField(); + repaint(); + RefreshWindow(); + } + }); + } + + public void RefreshWindow() { + field.setBounds(0,0,Width,Height); + BottomAndCreatePanel.setBounds(-220,Height-110,Width,80); + DimentionPanel.setBounds(Width-170,Height-170,190,140); + } +} 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