diff --git a/.idea/PIbd-22_Aleikin_A.M._AirBomber._Hard.iml b/.idea/PIbd-22_Aleikin_A.M._AirBomber._Hard.iml index d6ebd48..b107a2d 100644 --- a/.idea/PIbd-22_Aleikin_A.M._AirBomber._Hard.iml +++ b/.idea/PIbd-22_Aleikin_A.M._AirBomber._Hard.iml @@ -2,7 +2,9 @@ - + + + diff --git a/.idea/misc.xml b/.idea/misc.xml index 639900d..432b646 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/Direction.java b/Direction.java new file mode 100644 index 0000000..dc58268 --- /dev/null +++ b/Direction.java @@ -0,0 +1,12 @@ +public enum Direction +{ + Up(1), + Down(2), + Left(3), + Right(4); + + Direction(int i ) + { + + } +} diff --git a/DrawEngines.java b/DrawEngines.java new file mode 100644 index 0000000..ba311df --- /dev/null +++ b/DrawEngines.java @@ -0,0 +1,36 @@ +import java.awt.*; + +public class DrawEngines +{ + private Engines numEngines; + + public void SetNumEngines(int i) { numEngines = Engines.GetNumEngines(i); } + + public void DrawningEngines(float _startPosX, float _startPosY, Graphics2D g2d, int _airBomberWidth, int _airBomberHeight) + { + switch(numEngines) + { + case TwoEngines: + g2d.fillRect((int)_startPosX + _airBomberWidth / 2 - 20, (int)_startPosY + _airBomberHeight / 2 - 20, 20, 10); + g2d.fillRect((int)_startPosX + _airBomberWidth / 2 - 20, (int)_startPosY + _airBomberHeight / 2 + 10, 20, 10); + break; + case FourEngines: + g2d.fillRect((int)_startPosX + _airBomberWidth / 2 - 20, (int)_startPosY + _airBomberHeight / 2 - 20, 20, 10); + g2d.fillRect((int)_startPosX + _airBomberWidth / 2 - 20, (int)_startPosY + _airBomberHeight / 2 + 10, 20, 10); + + g2d.fillRect((int)_startPosX + _airBomberWidth / 2 - 15, (int)_startPosY + _airBomberHeight / 2 - 35, 15, 10); + g2d.fillRect((int)_startPosX + _airBomberWidth / 2 - 15, (int)_startPosY + _airBomberHeight / 2 + 25, 15, 10); + break; + case SixEngines: + g2d.fillRect((int)_startPosX + _airBomberWidth / 2 - 20, (int)_startPosY + _airBomberHeight / 2 - 20, 20, 10); + g2d.fillRect((int)_startPosX + _airBomberWidth / 2 - 20, (int)_startPosY + _airBomberHeight / 2 + 10, 20, 10); + + g2d.fillRect((int)_startPosX + _airBomberWidth / 2 - 15, (int)_startPosY + _airBomberHeight / 2 - 35, 15, 10); + g2d.fillRect((int)_startPosX + _airBomberWidth / 2 - 15, (int)_startPosY + _airBomberHeight / 2 + 25, 15, 10); + + g2d.fillRect((int)_startPosX + _airBomberWidth / 2 - 10, (int)_startPosY + _airBomberHeight / 2 - 50, 10, 10); + g2d.fillRect((int)_startPosX + _airBomberWidth / 2 - 10, (int)_startPosY + _airBomberHeight / 2 + 40, 10, 10); + break; + } + } +} diff --git a/DrawningBomber.java b/DrawningBomber.java new file mode 100644 index 0000000..395a0aa --- /dev/null +++ b/DrawningBomber.java @@ -0,0 +1,170 @@ +import javax.swing.*; +import java.awt.*; +import java.util.*; + +public class DrawningBomber extends JPanel +{ + private EntityAirBomber AirBomber; + public EntityAirBomber getAirBomber() { return AirBomber; } + private float _startPosX; + private float _startPosY; + private int _pictureWidth = 0; + private int _pictureHeight = 0; + private final int _airBomberWidth = 100; + private final int _airBomberHeight = 100; + + private int NumEngines = 1; + private DrawEngines drawEngines = new DrawEngines(); + + public void Init(int speed, float weight, Color bodyColor) + { + AirBomber = new EntityAirBomber(); + AirBomber.Init(speed, weight, bodyColor); + Random random = new Random(); + drawEngines.SetNumEngines(random.nextInt(1, 4)); + } + + public void SetPosition(int x, int y, int width, int height) + { + if (x + _airBomberWidth > width || x < 0) return; + else _startPosX = x; + if (y + _airBomberHeight> height || y < 0) return; + else _startPosY = y; + _pictureWidth = width; + _pictureHeight = height; + } + + public void MoveTransport(Direction direction) + { + if (!(_pictureWidth > 0 || _pictureHeight > 0)) + { + return; + } + switch (direction) + { + case Right: + if (_startPosX + _airBomberWidth + AirBomber.GetStep() < _pictureWidth) + { + _startPosX += AirBomber.GetStep(); + } + break; + case Left: + if (_startPosX - AirBomber.GetStep() > 0) + { + _startPosX -= AirBomber.GetStep(); + } + break; + case Up: + if (_startPosY - AirBomber.GetStep() > 0) + { + _startPosY -= AirBomber.GetStep(); + } + break; + case Down: + if (_startPosY + _airBomberHeight + AirBomber.GetStep() < _pictureHeight) + { + _startPosY += AirBomber.GetStep(); + } + break; + } + } + + public void DrawTransport() + { + if (_startPosX < 0 || _startPosY < 0 || _pictureHeight == 0 || _pictureWidth == 0) + { + return; + } + repaint(); + } + + @Override + public void paintComponent(Graphics g) + { + if (getAirBomber() == null || _startPosX < 0 || _startPosY < 0 || _pictureHeight == 0 || _pictureWidth == 0) + { + return; + } + super.paintComponent(g); + + Graphics2D g2d = (Graphics2D) g; + + g2d.setColor(AirBomber.GetColor()); + + int[] xPos = { + (int)(_startPosX + _airBomberWidth), + (int)(_startPosX + _airBomberWidth), + (int)(_startPosX + _airBomberWidth - 10), + (int)(_startPosX + _airBomberWidth - 10)}; + int[] yPos = { + (int)(_startPosY + _airBomberHeight / 2 - 20), + (int)(_startPosY + _airBomberHeight / 2 + 20), + (int)(_startPosY + _airBomberHeight / 2 + 10), + (int)(_startPosY + _airBomberHeight / 2 - 10)}; + + g2d.fillPolygon(xPos, yPos, xPos.length); + + xPos = new int[] { + (int)(_startPosX + _airBomberWidth / 2), + (int)(_startPosX + _airBomberWidth / 2 + 10), + (int)(_startPosX + _airBomberWidth / 2 + 20), + (int)(_startPosX + _airBomberWidth / 2 + 10), + (int)(_startPosX + _airBomberWidth / 2)}; + + yPos = new int[] { + (int)(_startPosY), + (int)(_startPosY), + (int)(_startPosY + _airBomberHeight / 2), + (int)(_startPosY + _airBomberHeight), + (int)(_startPosY + _airBomberHeight)}; + + g2d.fillPolygon(xPos, yPos, xPos.length); + + xPos = new int[] { + (int)(_startPosX + 20), + (int)(_startPosX + _airBomberWidth), + (int)(_startPosX + _airBomberWidth), + (int)(_startPosX + 20)}; + + yPos = new int[] { + (int)(_startPosY + _airBomberHeight / 2 - 5), + (int)(_startPosY + _airBomberHeight / 2 - 5), + (int)(_startPosY + _airBomberHeight / 2 + 5), + (int)(_startPosY + _airBomberHeight / 2 + 5)}; + + g2d.fillPolygon(xPos, yPos, xPos.length); + + xPos = new int[] { + (int)(_startPosX), + (int)(_startPosX + 20), + (int)(_startPosX + 20)}; + + yPos = new int[] { + (int)(_startPosY + _airBomberHeight / 2), + (int)(_startPosY + _airBomberHeight / 2 - 5), + (int)(_startPosY + _airBomberHeight / 2 + 5)}; + + g2d.fillPolygon(xPos, yPos, xPos.length); + drawEngines.DrawningEngines(_startPosX, _startPosY, g2d, _airBomberWidth, _airBomberHeight); + } + + public void ChangeBorders(int width, int height) + { + _pictureWidth = width; + _pictureHeight = height; + if (_pictureWidth <= _airBomberWidth || _pictureHeight <= _airBomberHeight) + { + _pictureWidth = 0; + _pictureHeight = 0; + return; + } + if (_startPosX + _airBomberWidth > _pictureWidth) + { + _startPosX = _pictureWidth - _airBomberWidth; + } + if (_startPosY + _airBomberHeight > _pictureHeight) + { + _startPosY = _pictureHeight - _airBomberHeight; + } + } +} diff --git a/Engines.java b/Engines.java new file mode 100644 index 0000000..2d2e3a4 --- /dev/null +++ b/Engines.java @@ -0,0 +1,24 @@ +public enum Engines +{ + TwoEngines(1), + FourEngines(2), + SixEngines(6); + + Engines(int i) { + + } + + public static Engines GetNumEngines(int i) + { + switch(i) + { + case 1: + return TwoEngines; + case 2: + return FourEngines; + case 3: + return SixEngines; + } + return null; + } +} diff --git a/EntityAirBomber.java b/EntityAirBomber.java new file mode 100644 index 0000000..235946a --- /dev/null +++ b/EntityAirBomber.java @@ -0,0 +1,29 @@ +import java.awt.*; +import java.util.*; + +public class EntityAirBomber +{ + private int Speed; + private float Weight; + private Color BodyColor; + private float Step; + + public void Init(int speed, float weight, Color bodyColor) + { + Random rnd = new Random(); + Speed = speed <= 0 ? rnd.nextInt(50, 150) : speed; + Weight = weight <= 0 ? rnd.nextInt(40, 70) : weight; + BodyColor = bodyColor; + Step = Speed * 100 / Weight; + } + + public int GetSpeed() { return Speed; } + + public float GetWeight() { + return Weight; + } + + public Color GetColor() { return BodyColor; } + + public float GetStep() { return Step; } +} diff --git a/FormAirBomber.form b/FormAirBomber.form new file mode 100644 index 0000000..4c4e334 --- /dev/null +++ b/FormAirBomber.form @@ -0,0 +1,107 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/FormAirBomber.java b/FormAirBomber.java new file mode 100644 index 0000000..9db376d --- /dev/null +++ b/FormAirBomber.java @@ -0,0 +1,115 @@ +import javax.imageio.*; +import javax.swing.*; +import java.awt.*; +import java.awt.event.*; +import java.util.*; + +public class FormAirBomber { + private JToolBar statusStrip; + public JPanel Mainpanel; + + private DrawningBomber pictureBomber; + + private JButton buttonRight; + private JButton buttonCreate; + private JButton buttonLeft; + private JButton buttonUp; + private JButton buttonDown; + private JLabel JLabelSpeed = new JLabel(); + private JLabel JLabelWeight = new JLabel(); + private JLabel JLabelColor = new JLabel(); + + private void Draw() + { + if (pictureBomber.getAirBomber() == null) return; + pictureBomber.DrawTransport(); + } + + private void ButtonMove_Click(String name) + { + if (pictureBomber == null) return; + switch (name) + { + case "buttonLeft": + pictureBomber.MoveTransport(Direction.Left); + break; + case "buttonUp": + pictureBomber.MoveTransport(Direction.Up); + break; + case "buttonRight": + pictureBomber.MoveTransport(Direction.Right); + break; + case "buttonDown": + pictureBomber.MoveTransport(Direction.Down); + break; + } + Draw(); + } + + public FormAirBomber() { + Box LabelBox = Box.createHorizontalBox(); + LabelBox.setMinimumSize(new Dimension(1, 20)); + LabelBox.add(JLabelSpeed); + LabelBox.add(JLabelWeight); + LabelBox.add(JLabelColor); + statusStrip.add(LabelBox); + + try { + Image img = ImageIO.read(FormAirBomber.class.getResource("/Images/ArrowUp.jpg")); + buttonUp.setIcon(new ImageIcon(img)); + img = ImageIO.read(FormAirBomber.class.getResource("/Images/ArrowLeft.jpg")); + buttonLeft.setIcon(new ImageIcon(img)); + img = ImageIO.read(FormAirBomber.class.getResource("/Images/ArrowDown.jpg")); + buttonDown.setIcon(new ImageIcon(img)); + img = ImageIO.read(FormAirBomber.class.getResource("/Images/ArrowRight.jpg")); + buttonRight.setIcon(new ImageIcon(img)); + } catch (Exception ex) { + System.out.println(ex); + } + + buttonCreate.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + Random random = new Random(); + pictureBomber.Init(random.nextInt(100, 300), random.nextInt(1000, 2000), new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256))); + pictureBomber.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100), pictureBomber.getWidth(), pictureBomber.getHeight()); + JLabelSpeed.setText("Cкорость: " + pictureBomber.getAirBomber().GetSpeed() + " "); + JLabelWeight.setText("Вес: " + pictureBomber.getAirBomber().GetWeight() + " "); + JLabelColor.setText(("Цвет: " + pictureBomber.getAirBomber().GetColor() + " ")); + Draw(); + } + }); + buttonUp.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + ButtonMove_Click("buttonUp"); + } + }); + buttonLeft.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + ButtonMove_Click("buttonLeft"); + } + }); + buttonDown.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + ButtonMove_Click("buttonDown"); + } + }); + buttonRight.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + ButtonMove_Click("buttonRight"); + } + }); + pictureBomber.addComponentListener(new ComponentAdapter() { + @Override + public void componentResized(ComponentEvent e) { + super.componentResized(e); + pictureBomber.ChangeBorders(pictureBomber.getWidth(), pictureBomber.getHeight()); + Draw(); + } + }); + } +} diff --git a/Images/ArrowDown.jpg b/Images/ArrowDown.jpg new file mode 100644 index 0000000..2fdc8eb Binary files /dev/null and b/Images/ArrowDown.jpg differ diff --git a/Images/ArrowLeft.jpg b/Images/ArrowLeft.jpg new file mode 100644 index 0000000..f537b98 Binary files /dev/null and b/Images/ArrowLeft.jpg differ diff --git a/Images/ArrowRight.jpg b/Images/ArrowRight.jpg new file mode 100644 index 0000000..08cfda3 Binary files /dev/null and b/Images/ArrowRight.jpg differ diff --git a/Images/ArrowUp.jpg b/Images/ArrowUp.jpg new file mode 100644 index 0000000..012bc58 Binary files /dev/null and b/Images/ArrowUp.jpg differ diff --git a/Main.java b/Main.java new file mode 100644 index 0000000..1dead79 --- /dev/null +++ b/Main.java @@ -0,0 +1,13 @@ +import javax.swing.*; + +public class Main { + public static void main(String[] args) { + JFrame frame = new JFrame("Hard №1"); + frame.setContentPane(new FormAirBomber().Mainpanel); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + frame.setLocation(500, 200); + frame.pack(); + frame.setSize(800, 600); + frame.setVisible(true); + } +} \ No newline at end of file