From 86f553ca836e849f0b28b1f152f94042d4a021b2 Mon Sep 17 00:00:00 2001 From: MaxKarme <91691525+MaxKarme@users.noreply.github.com> Date: Fri, 28 Oct 2022 14:45:35 +0300 Subject: [PATCH 1/3] main commit --- Canvas.java | 15 +++++ Direction.java | 6 ++ DrawingAircraft.java | 146 +++++++++++++++++++++++++++++++++++++++++++ EntityAircraft.java | 22 +++++++ FormAircraft.form | 115 ++++++++++++++++++++++++++++++++++ FormAircraft.java | 95 ++++++++++++++++++++++++++++ Main.java | 5 ++ 7 files changed, 404 insertions(+) create mode 100644 Canvas.java create mode 100644 Direction.java create mode 100644 DrawingAircraft.java create mode 100644 EntityAircraft.java create mode 100644 FormAircraft.form create mode 100644 FormAircraft.java create mode 100644 Main.java diff --git a/Canvas.java b/Canvas.java new file mode 100644 index 0000000..be5f0df --- /dev/null +++ b/Canvas.java @@ -0,0 +1,15 @@ +import javax.swing.*; +import java.awt.*; + +public class Canvas extends JComponent { + FormAircraft form; + public Canvas(FormAircraft form) { + this.form = form; + } + + @Override + public void paintComponent(Graphics g) { + Graphics2D g2 = (Graphics2D) g; + form.draw(g2); + } +} diff --git a/Direction.java b/Direction.java new file mode 100644 index 0000000..870719c --- /dev/null +++ b/Direction.java @@ -0,0 +1,6 @@ +public enum Direction { + Up, + Right, + Left, + Down +} diff --git a/DrawingAircraft.java b/DrawingAircraft.java new file mode 100644 index 0000000..c7011d0 --- /dev/null +++ b/DrawingAircraft.java @@ -0,0 +1,146 @@ +import java.awt.*; + +class DrawingAircraft +{ + public EntityAircraft AirFighter; + + private float _startPosX; + private float _startPosY; + + private int _pictureWidth = -1; + private int _pictureHeight = -1; + + private int _airFighterWidth = 195; + private int _airFighterHeight = 166; + + public void Init(int speed, float weight, Color bodyColor) + { + AirFighter = new EntityAircraft(); + AirFighter.Init(speed, weight, bodyColor); + } + + public void SetPosition(int x, int y, int width, int height) + { + if (width < _airFighterWidth || height < _airFighterHeight) return; + + if (x + _airFighterWidth > width || x < 0) return; + if (y + _airFighterHeight > height || y < 0) return; + + _startPosX = x; + _startPosY = y; + + _pictureWidth = width; + _pictureHeight = height; + } + + public void MoveTransport(Direction direction) + { + if (_pictureWidth == -1 || _pictureHeight == -1) + { + return; + } + + + switch (direction) + { + case Right: + if (_startPosX + _airFighterWidth + AirFighter.Step < _pictureWidth) + { + _startPosX += AirFighter.Step; + } + break; + case Left: + if (_startPosX - AirFighter.Step > 0) + { + _startPosX -= AirFighter.Step; + } + break; + case Up: + if (_startPosY - AirFighter.Step > 0) + { + _startPosY -= AirFighter.Step; + } + break; + case Down: + if (_startPosY + _airFighterHeight + AirFighter.Step < _pictureHeight) + { + _startPosY += AirFighter.Step; + } + break; + } + + } + + public void DrawTransport(Graphics2D g) + { + if (_pictureWidth == -1 || _pictureHeight == -1) + { + return; + } + + g.setPaint(AirFighter.BodyColor); + + Polygon front = new Polygon(); + front.addPoint((int)(_startPosX + 160), (int)(_startPosY + 70)); + front.addPoint((int)(_startPosX + 195), (int)(_startPosY + 83)); + front.addPoint((int)(_startPosX + 160), (int)(_startPosY + 96)); + + + Polygon tailTop = new Polygon(); + tailTop.addPoint((int)(_startPosX), (int)(_startPosY + 30)); + tailTop.addPoint((int)(_startPosX), (int)(_startPosY + 70)); + tailTop.addPoint((int)(_startPosX + 25), (int)(_startPosY + 70)); + tailTop.addPoint((int)(_startPosX + 25), (int)(_startPosY + 55)); + + + Polygon tailBottom = new Polygon(); + tailBottom.addPoint((int)(_startPosX), (int)(_startPosY + 96)); + tailBottom.addPoint((int)(_startPosX), (int)(_startPosY + 136)); + tailBottom.addPoint((int)(_startPosX + 25), (int)(_startPosY + 111)); + tailBottom.addPoint((int)(_startPosX + 25), (int)(_startPosY + 96)); + + + Polygon wingTop = new Polygon(); + wingTop.addPoint((int)(_startPosX + 100), (int)(_startPosY)); + wingTop.addPoint((int)(_startPosX + 100), (int)(_startPosY + 70)); + wingTop.addPoint((int)(_startPosX + 75), (int)(_startPosY + 70)); + wingTop.addPoint((int)(_startPosX + 90), (int)(_startPosY)); + + + + Polygon wingBottom = new Polygon(); + wingBottom.addPoint((int)(_startPosX + 100), (int)(_startPosY + 96)); + wingBottom.addPoint((int)(_startPosX + 100), (int)(_startPosY + 166)); + wingBottom.addPoint((int)(_startPosX + 90), (int)(_startPosY + 166)); + wingBottom.addPoint((int)(_startPosX + 75), (int)(_startPosY + 96)); + + g.fillPolygon(front); + g.drawPolygon(tailTop); + g.drawPolygon(tailBottom); + g.drawPolygon(wingTop); + g.drawPolygon(wingBottom); + g.drawRect((int)_startPosX, (int)_startPosY + 70, 160, 26); + } + + public void ChangeBorders(int width, int height) + { + _pictureWidth = width; + _pictureHeight = height; + if (_pictureWidth <= _airFighterWidth || _pictureHeight <= _airFighterHeight) + { + _pictureWidth = -1; + _pictureHeight = -1; + return; + } + if (_startPosX + _airFighterWidth > _pictureWidth) + { + _startPosX = _pictureWidth - _airFighterWidth; + } + if (_startPosY + _airFighterHeight > _pictureHeight) + { + _startPosY = _pictureHeight - _airFighterHeight; + } + } + +} + diff --git a/EntityAircraft.java b/EntityAircraft.java new file mode 100644 index 0000000..2988375 --- /dev/null +++ b/EntityAircraft.java @@ -0,0 +1,22 @@ +import java.awt.*; +import java.util.Random; + +class EntityAircraft +{ + public int Speed; + public float Weight; + public Color BodyColor; + + public 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(50, 70) : weight; + BodyColor = bodyColor; + + Step = Speed * 100 / Weight; + } +} \ No newline at end of file diff --git a/FormAircraft.form b/FormAircraft.form new file mode 100644 index 0000000..fedb5cb --- /dev/null +++ b/FormAircraft.form @@ -0,0 +1,115 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/FormAircraft.java b/FormAircraft.java new file mode 100644 index 0000000..9441cf7 --- /dev/null +++ b/FormAircraft.java @@ -0,0 +1,95 @@ +import javax.swing.*; +import javax.swing.border.LineBorder; +import java.awt.*; +import java.awt.event.ComponentAdapter; +import java.awt.event.ComponentEvent; +import java.util.Random; + +public class FormAircraft { + private JButton createButton; + private JButton upButton; + private JButton rightButton; + private JButton downButton; + private JButton leftButton; + private JPanel mainPanel; + private JPanel DrawPlace; + private JLabel speedLabel; + private JLabel weightLabel; + private JLabel colorLabel; + + DrawingAircraft _airFighter; + + + private JFrame jframe = getFrame(); + + private JFrame getFrame() { + JFrame frame = new JFrame(); + frame.setVisible(true); + frame.setBounds(300, 100, 800, 600); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + return frame; + } + + public void run() { + jframe.add(mainPanel); + Canvas canv = new Canvas(this); + DrawPlace.add(canv); + + + createButton.addActionListener(e -> { + Dimension canvSize = canv.getSize(); + Random rnd = new Random(); + + _airFighter = new DrawingAircraft(); + + _airFighter.Init(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000), + new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256))); + + _airFighter.SetPosition((int)rnd.nextInt(10, 100), (int)rnd.nextInt(10, 100), canvSize.width, canvSize.height); + + Color bodyColor = _airFighter.AirFighter.BodyColor; + String colorString = "(" + bodyColor.getRed() + ", " + bodyColor.getGreen() + ", " + bodyColor.getBlue() + ")"; + + speedLabel.setText("Скорость: " + _airFighter.AirFighter.Speed + " "); + weightLabel.setText("Вес: " + _airFighter.AirFighter.Weight + " "); + colorLabel.setText("Цвет: " + colorString); + + canv.repaint(); + }); + + jframe.addComponentListener(new ComponentAdapter() { + @Override + public void componentResized(ComponentEvent e) { + if(_airFighter == null) return; + _airFighter.ChangeBorders(canv.getSize().width, canv.getSize().height); + jframe.revalidate(); + } + }); + + upButton.addActionListener(e -> { + if(_airFighter == null) return; + _airFighter.MoveTransport(Direction.Up); + canv.repaint(); + }); + rightButton.addActionListener(e -> { + if(_airFighter == null) return; + _airFighter.MoveTransport(Direction.Right); + canv.repaint(); + }); + downButton.addActionListener(e -> { + if(_airFighter == null) return; + _airFighter.MoveTransport(Direction.Down); + canv.repaint(); + }); + leftButton.addActionListener(e -> { + if(_airFighter == null) return; + _airFighter.MoveTransport(Direction.Left); + canv.repaint(); + }); + } + + public void draw(Graphics2D g) { + if(_airFighter == null) return; + _airFighter.DrawTransport(g); + } +} diff --git a/Main.java b/Main.java new file mode 100644 index 0000000..420ccca --- /dev/null +++ b/Main.java @@ -0,0 +1,5 @@ +public class Main { + public static void main(String[] args) { + new FormAircraft().run(); + } +} -- 2.25.1 From 685b97e188eb07530363f48dda4e2eb1d70fcdc9 Mon Sep 17 00:00:00 2001 From: MaxKarme <91691525+MaxKarme@users.noreply.github.com> Date: Fri, 28 Oct 2022 20:18:49 +0300 Subject: [PATCH 2/3] refactoring form and make hard part --- DrawingAircraft.java | 7 ++ DrawingEngines.java | 30 ++++++++ EnginesCount.java | 5 ++ FormAircraft.form | 161 ++++++++++++++++++++++++------------------- Resources/down.png | Bin 0 -> 451 bytes Resources/left.png | Bin 0 -> 367 bytes Resources/right.png | Bin 0 -> 366 bytes Resources/up.png | Bin 0 -> 360 bytes 8 files changed, 131 insertions(+), 72 deletions(-) create mode 100644 DrawingEngines.java create mode 100644 EnginesCount.java create mode 100644 Resources/down.png create mode 100644 Resources/left.png create mode 100644 Resources/right.png create mode 100644 Resources/up.png diff --git a/DrawingAircraft.java b/DrawingAircraft.java index c7011d0..48b301a 100644 --- a/DrawingAircraft.java +++ b/DrawingAircraft.java @@ -1,8 +1,10 @@ import java.awt.*; +import java.util.Random; class DrawingAircraft { public EntityAircraft AirFighter; + public DrawingEngines drawingEngines = new DrawingEngines(); private float _startPosX; private float _startPosY; @@ -15,8 +17,11 @@ class DrawingAircraft public void Init(int speed, float weight, Color bodyColor) { + Random rnd = new Random(); + AirFighter = new EntityAircraft(); AirFighter.Init(speed, weight, bodyColor); + drawingEngines.Init(rnd.nextInt(1, 8), bodyColor); } public void SetPosition(int x, int y, int width, int height) @@ -120,6 +125,8 @@ class DrawingAircraft g.drawPolygon(wingTop); g.drawPolygon(wingBottom); g.drawRect((int)_startPosX, (int)_startPosY + 70, 160, 26); + + drawingEngines.draw(g, (int)_startPosX, (int)_startPosY); } public void ChangeBorders(int width, int height) diff --git a/DrawingEngines.java b/DrawingEngines.java new file mode 100644 index 0000000..9878094 --- /dev/null +++ b/DrawingEngines.java @@ -0,0 +1,30 @@ +import java.awt.*; + +public class DrawingEngines { + private EnginesCount enginesCount; + private Color color; + + public void Init(int count, Color bodyColor) { + if(count <= 2) enginesCount = EnginesCount.Two; + else if(count >= 6) enginesCount = EnginesCount.Six; + else enginesCount = EnginesCount.Four; + + color = bodyColor; + } + + public void draw(Graphics2D g, int startPosX, int startPosY) { + g.setPaint(color); + g.fillOval(startPosX + 90, startPosY + 10, 30, 15); + g.fillOval(startPosX + 90, startPosY + 141, 30, 15); + + if(enginesCount == EnginesCount.Two) return; + + g.fillOval(startPosX + 90, startPosY + 30, 30, 15); + g.fillOval(startPosX + 90, startPosY + 121, 30, 15); + + if(enginesCount == EnginesCount.Four) return; + + g.fillOval(startPosX + 90, startPosY + 50, 30, 15); + g.fillOval(startPosX + 90, startPosY + 101, 30, 15); + } +} diff --git a/EnginesCount.java b/EnginesCount.java new file mode 100644 index 0000000..dbea801 --- /dev/null +++ b/EnginesCount.java @@ -0,0 +1,5 @@ +public enum EnginesCount { + Two, + Four, + Six +} diff --git a/FormAircraft.form b/FormAircraft.form index fedb5cb..ee165ee 100644 --- a/FormAircraft.form +++ b/FormAircraft.form @@ -1,6 +1,7 @@
- + + @@ -10,106 +11,122 @@ - - + + - - - - - - - - - - - + + + - + - - - - - - - - - - - - - - + - + - + - - + - + - + - - + - - - - - - - - - - + - + + - - + - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/Resources/down.png b/Resources/down.png new file mode 100644 index 0000000000000000000000000000000000000000..7500783f52570f15ba7b977f6fa53f97342ce4f0 GIT binary patch literal 451 zcmV;!0X+VRP)Px#1ZP1_K>z@;j|==^1poj532;bRa{vGmbN~PnbOGLGA9w%&0a!^yK~zXf?UqYU z!!Qtr$EBPA5Z%+V$bo<}P|+Kd+hiRl5F}(qk0m1 ze$V*Ps7eDJ(4*hhIS98#x57#(==Y9|{TFrovMi0Sv{gr{Z=9ObvlCdot;lP};Cuid z@1Nvt_^#HVv?zOy*FRs>YKBxragCdneR4PP$H0tzHoO?&<(34F;bPfluFOz&sb!Ce zXG$FkjDg0tWAbFMUu+Uve-e1CbQ#FwktP`u#GPzOZEr67g+zT0Z5HzdwRl`n17_@k zkKN5++jM$QJuCLTZGyyRbD0@9#FRY*n<*ilV`)GvK2byzMsOot-&~Ow4~_{=Tz3eP z=Q+E6oE1euqx?jFbH%Lr^UD)tSw^0pPx#1ZP1_K>z@;j|==^1poj532;bRa{vGmbN~PnbOGLGA9w%&0R%}zK~zXfwU zf-nq4Tf|eTd&a@ja}FK%qG%Yl#PQ)0)CbskE@o}5463uco z(f6m&Hruz9p!F~e%jy@G#3V^^pT;14y*aTYC3pkhn8ZlBKkVR~tE)y*;xC&kC#Biu zC>A4WckJe*ni7kuw${s0C-$h9qY`V9mhRE4*t1>^6z8NCig)!2C9O?Mh+_Juk+29? zsuNog#b}q(?5*=RCAP?+*khIw>PM#c{p_~6&|Q#1EzTuEr;QX<2LV(&I%jd*s$QYw zn%b+x9<`f~x2SgX1Kq3UpvukS+J}rwVv=93=VkQ)P0>7Kj{c=BgeSfHd+%gS;E(_S N002ovPDHLkV1i%gpo9Pb literal 0 HcmV?d00001 diff --git a/Resources/right.png b/Resources/right.png new file mode 100644 index 0000000000000000000000000000000000000000..7851a8e71e9a8a331779bd9c11ec654e9b584a27 GIT binary patch literal 366 zcmeAS@N?(olHy`uVBq!ia0vp^av;pX1|+Qw)-3{3jKx9jP7LeL$-D$|SkfJR9T^xl z_H+M9WCij$3p^r=85o30K$!7fntTON1GA@#V~B_M)@i5tnhXS7qmS?!em(x-_D`W+ zZL>u$5)@61`u&!x8;Y2>S6iF>_}T2tv3#@i@xn;|^~a00F6z8)-GAjD zAK9la=-m7L-Gkc|-@9F_ObT-Y3^#s_eycj6bN*+M@ZVETcp5Hw_9W`WbDC30 z9&TC}tFvugY)omF>)S_1EIqQqJ9Yi^La%CuSiNFg9kQ3{xvg8Mq3gsMt0d!Wv&63^ zUIzh@s)+py)>5p7@?F?hQA KxvXwQ%2 zsq?3|W^9m2UexgZmOx5k-a~;UD|RlLa-+HLxW&>hAGyxU<_nv=nvfx@{&k-oP*6;o zY4ug%XU9&YS8Ez_{p!=IwBy~4NAu2y ziuYcV3VE|lqEnTj9As!U4}?mOmn$f<^L!;Bl3j%`j{4-5zfPgg&ebxsLQ E0Eq;c<^TWy literal 0 HcmV?d00001 -- 2.25.1 From f25feff68b902db07789f244dbe06f83f7cb81c3 Mon Sep 17 00:00:00 2001 From: MaxKarme <91691525+MaxKarme@users.noreply.github.com> Date: Wed, 2 Nov 2022 08:51:14 +0300 Subject: [PATCH 3/3] add setCount in DrawingEngines --- Canvas.java | 6 +++--- DrawingEngines.java | 7 +++++-- Form.java | 5 +++++ FormAircraft.java | 5 +++-- 4 files changed, 16 insertions(+), 7 deletions(-) create mode 100644 Form.java diff --git a/Canvas.java b/Canvas.java index be5f0df..6421b3a 100644 --- a/Canvas.java +++ b/Canvas.java @@ -2,14 +2,14 @@ import javax.swing.*; import java.awt.*; public class Canvas extends JComponent { - FormAircraft form; - public Canvas(FormAircraft form) { + Form form; + public Canvas(Form form) { this.form = form; } @Override public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; - form.draw(g2); + form.Draw(g2); } } diff --git a/DrawingEngines.java b/DrawingEngines.java index 9878094..baa2d39 100644 --- a/DrawingEngines.java +++ b/DrawingEngines.java @@ -5,11 +5,14 @@ public class DrawingEngines { private Color color; public void Init(int count, Color bodyColor) { + setCount(count); + color = bodyColor; + } + + public void setCount(int count) { if(count <= 2) enginesCount = EnginesCount.Two; else if(count >= 6) enginesCount = EnginesCount.Six; else enginesCount = EnginesCount.Four; - - color = bodyColor; } public void draw(Graphics2D g, int startPosX, int startPosY) { diff --git a/Form.java b/Form.java new file mode 100644 index 0000000..0347fbb --- /dev/null +++ b/Form.java @@ -0,0 +1,5 @@ +import java.awt.*; + +public interface Form { + void Draw(Graphics2D g); +} diff --git a/FormAircraft.java b/FormAircraft.java index 9441cf7..e53b6af 100644 --- a/FormAircraft.java +++ b/FormAircraft.java @@ -5,7 +5,7 @@ import java.awt.event.ComponentAdapter; import java.awt.event.ComponentEvent; import java.util.Random; -public class FormAircraft { +public class FormAircraft implements Form { private JButton createButton; private JButton upButton; private JButton rightButton; @@ -88,7 +88,8 @@ public class FormAircraft { }); } - public void draw(Graphics2D g) { + @Override + public void Draw(Graphics2D g) { if(_airFighter == null) return; _airFighter.DrawTransport(g); } -- 2.25.1