From 091005867d35dfdbc57ca928115a6e59a38b0675 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=90=D0=BB=D0=B5=D0=B9?= =?UTF-8?q?=D0=BA=D0=B8=D0=BD?= Date: Mon, 28 Nov 2022 00:15:39 +0400 Subject: [PATCH] Without dopAsk --- .../PIbd-22_Aleikin_A.M._AirBomber._Hard.iml | 4 +- .idea/misc.xml | 2 +- Direction.java | 12 ++ DrawningBomber.java | 164 ++++++++++++++++++ EntityAirBomber.java | 29 ++++ FormAirBomber.form | 107 ++++++++++++ FormAirBomber.java | 115 ++++++++++++ Images/ArrowDown.jpg | Bin 0 -> 488 bytes Images/ArrowLeft.jpg | Bin 0 -> 493 bytes Images/ArrowRight.jpg | Bin 0 -> 491 bytes Images/ArrowUp.jpg | Bin 0 -> 484 bytes Main.java | 13 ++ 12 files changed, 444 insertions(+), 2 deletions(-) create mode 100644 Direction.java create mode 100644 DrawningBomber.java create mode 100644 EntityAirBomber.java create mode 100644 FormAirBomber.form create mode 100644 FormAirBomber.java create mode 100644 Images/ArrowDown.jpg create mode 100644 Images/ArrowLeft.jpg create mode 100644 Images/ArrowRight.jpg create mode 100644 Images/ArrowUp.jpg create mode 100644 Main.java 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/DrawningBomber.java b/DrawningBomber.java new file mode 100644 index 0000000..0e75f41 --- /dev/null +++ b/DrawningBomber.java @@ -0,0 +1,164 @@ +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; + + public void Init(int speed, float weight, Color bodyColor) + { + AirBomber = new EntityAirBomber(); + AirBomber.Init(speed, weight, bodyColor); + } + + 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)}; + + g.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)}; + + g.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)}; + + g.fillPolygon(xPos, yPos, xPos.length); + } + + 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/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 0000000000000000000000000000000000000000..2fdc8ebcbde743e07cc7eea33e9203b43c307292 GIT binary patch literal 488 zcmex=>ukC3pCfH06P@c#e}N z-o;g&w;DzE3YmVe{MgJd%q70|Kf{yRiEj$;D@y46^8Pn1xbCg#Pp^ahJ#yc@^jv;j z4mIj_3^Y|*^GNj_r%CnNrI}d@84h2W*dA~>by$0Cz1&m0y?$Nqw;f+sPl#+>nfBSM zYo^|*DQ8Y{<`$%CTi?DhWeI=y>ukC3pCfH06P@c#ea_p27&sV#X4x}bRdSG251A$P?N;^lpb(Q*<~XnAo2x>Pn3}gWm0yww)mC6yV6l2v z2&bolfaAfd_q`*I?~7Zp)%R*qYStR#i&vg1a6L?Z_CzfCS@WSu^E=!83;S!G&kIg@ zQFZg&r&n|5g_z2Go45GsRG};nv&1CH-r8e(B{Ne!gF<@#sWNf=-V*#f|DFFEU(>&9 z+%K=(v~GuJLr3^}9i6#31vV}!53j7B=RVJOVo&;^m-Hk{6 zU7Q=F8|SS4$?)d#@z3l@wGTfs3NL?lXi0h4;~1~tVzXaM8%>ukC3pCfH06P@c#eTdNvZ=F-`5V7Cl`_F9dyy*(Yw#V+TKCke0 zoxb>qymFO0E6=W5m}%ne6*Ps#@=}i~lmGpF3yyWJ7qpEwg#{{+kTVB@5MFijCHxbv3UoRW_5KQyRhML|CGN; zAqytV4BC^xeDh+=qxEcM#oaqrF5DKi@)OUry-Sw8t=kd2H1CRB|F&B`ZL?TBJl-78 SQhDw6GIN2?vH9`;Zvp`8=dq{& literal 0 HcmV?d00001 diff --git a/Images/ArrowUp.jpg b/Images/ArrowUp.jpg new file mode 100644 index 0000000000000000000000000000000000000000..012bc582aecb5f1a6083feb3f4c37a87810bbd84 GIT binary patch literal 484 zcmex=>ukC3pCfH06P@c#eY+d{1d9T7fuV*1wq(04RW#UqO`eV5OhZy(HSD%v()@AuleYN?vj9b}-t7@rF zZ37)HiB7(<Yny&e z71fdcn6saoXB&6w(xqBnlNQx_Dm4E-z9U20!LoaHP*ifzm1#wXF20)PKlQ`e%}kFk zIymoOZZCVXBu{$smOBEKpUjS~J@!#~$HUr>Q{yfh|2y;0`H=C4Q@-yf`@d~#GZxD3 z6IuK3!_$!0)wP{XGY`Gpd3SfU)_(7(`|m_z)RzVd?G?Z5U7NeY-Dg=~?i-bxTpa-n NcS=9ZG5GlZCIC8zuo(aV literal 0 HcmV?d00001 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