From daea9003f686fa7aef35742758e7c42ecf5ccb69 Mon Sep 17 00:00:00 2001 From: Yuee Shiness Date: Mon, 24 Oct 2022 19:41:28 +0400 Subject: [PATCH 1/6] Engines enum and draw engine class are added! --- App.java | 8 ++ Classes/Direction.java | 11 ++ Classes/DrawingAircraft.java | 225 +++++++++++++++++++++++++++++++++++ Classes/DrawingEngines.java | 69 +++++++++++ Classes/Engines.java | 32 +++++ Classes/EntityAircraft.java | 43 +++++++ Form/FormAirFighter.form | 129 ++++++++++++++++++++ Form/FormAirFighter.java | 109 +++++++++++++++++ Resources/bottomarrow.png | Bin 0 -> 489 bytes Resources/leftarrow.png | Bin 0 -> 494 bytes Resources/rightarrow.png | Bin 0 -> 493 bytes Resources/upbutton.png | Bin 0 -> 489 bytes 12 files changed, 626 insertions(+) create mode 100644 App.java create mode 100644 Classes/Direction.java create mode 100644 Classes/DrawingAircraft.java create mode 100644 Classes/DrawingEngines.java create mode 100644 Classes/Engines.java create mode 100644 Classes/EntityAircraft.java create mode 100644 Form/FormAirFighter.form create mode 100644 Form/FormAirFighter.java create mode 100644 Resources/bottomarrow.png create mode 100644 Resources/leftarrow.png create mode 100644 Resources/rightarrow.png create mode 100644 Resources/upbutton.png diff --git a/App.java b/App.java new file mode 100644 index 0000000..47fee84 --- /dev/null +++ b/App.java @@ -0,0 +1,8 @@ +import Form.FormAirFighter; + +public class App +{ + public static void main(String[] args) { + new FormAirFighter(); + } +} diff --git a/Classes/Direction.java b/Classes/Direction.java new file mode 100644 index 0000000..9eea621 --- /dev/null +++ b/Classes/Direction.java @@ -0,0 +1,11 @@ +package Classes; + +public enum Direction +{ + Up, + Down, + Left, + Right, +} + + diff --git a/Classes/DrawingAircraft.java b/Classes/DrawingAircraft.java new file mode 100644 index 0000000..d59be7b --- /dev/null +++ b/Classes/DrawingAircraft.java @@ -0,0 +1,225 @@ +package Classes; + +import java.awt.*; +import java.util.Random; + + +public class DrawingAircraft +{ + public EntityAircraft Plane; + private DrawingEngines _engines; + + private int _startPosX; + private int _startPosY; + + private Integer _pictureWidth = null; + private Integer _pictureHeight = null; + + protected final int _aircraftWidth = 100; + protected final int _aircraftHeight = 120; + + public void Init(int speed, float weight, Color bodyColor) + { + Random rnd = new Random(); + Plane = new EntityAircraft(); + Plane.Init(speed,weight,bodyColor); + _engines = new DrawingEngines(); + _engines.setEngines(rnd.nextInt(2,7)); + } + + public void SetPosition(int x,int y,int width,int height) + { + if (x + _aircraftWidth > width || x < 0 || y + _aircraftHeight > height || y < 0) + { + return; + } + else + { + _startPosX = x; + _startPosY = y; + _pictureWidth = width; + _pictureHeight = height; + } + } + + public void MoveTransport(Direction direction) + { + if (_pictureWidth == null || _pictureHeight == null) + { + return; + } + + switch (direction) + { + case Right: + { + if (_startPosX + _aircraftWidth + Plane.getStep() < _pictureWidth) + { + _startPosX += Plane.getStep(); + } + } + break; + case Left: + { + if (_startPosX - Plane.getStep() > 0) + { + _startPosX -= Plane.getStep(); + } + } + break; + case Up: + { + if (_startPosY - Plane.getStep() > 0) + { + _startPosY -= Plane.getStep(); + } + } + break; + case Down: + { + if (_startPosY + _aircraftHeight + Plane.getStep() < _pictureHeight) + { + _startPosY += Plane.getStep(); + } + } + break; + } + } + + public void DrawTransport(Graphics g) + { + + if (_startPosX < 0 || _startPosY < 0 || _pictureHeight == null || _pictureWidth == null) + { + return; + } + + //Aircraft Body + g.setColor(Plane.BodyColor); + g.fillRect((int)(_startPosX + 20), (int)(_startPosY + 45), 80, 14); + + g.setColor(Color.BLACK); + g.drawRect((int)(_startPosX + 20), (int)(_startPosY + 45), 80, 14); + + //Wings + Polygon pathWing1 = new Polygon(); + + Point point1B = new Point((int)(_startPosX + 50), (int)(_startPosY + 45)); + Point point2B = new Point(point1B.x, point1B.y - 40); + Point point3B = new Point(point2B.x + 6, point2B.y); + Point point4B = new Point(point3B.x + 27, point1B.y); + + pathWing1.addPoint(point1B.x,point1B.y); + pathWing1.addPoint(point2B.x,point2B.y); + pathWing1.addPoint(point3B.x,point3B.y); + pathWing1.addPoint(point4B.x,point4B.y); + + g.setColor(Color.BLACK); + g.drawPolygon(pathWing1); + + g.setColor(Plane.BodyColor); + g.fillPolygon(pathWing1); + + Polygon pathWing2 = new Polygon(); + + Point point1B2 = new Point((int)(_startPosX + 50), (int)(_startPosY + 60)); + Point point2B2 = new Point(point1B2.x, point1B2.y + 40); + Point point3B2 = new Point(point2B2.x + 6, point2B2.y); + Point point4B2 = new Point(point3B2.x + 27, point1B2.y); + + pathWing2.addPoint(point1B2.x,point1B2.y); + pathWing2.addPoint(point2B2.x,point2B2.y); + pathWing2.addPoint(point3B2.x,point3B2.y); + pathWing2.addPoint(point4B2.x,point4B2.y); + + g.setColor(Color.BLACK); + g.drawPolygon(pathWing2); + + g.setColor(Plane.BodyColor); + g.fillPolygon(pathWing2); + + + //BackWings + Polygon pathBackWing1 = new Polygon(); + + Point point1W = new Point((int)(_startPosX + 85), (int)(_startPosY + 45)); + Point point2W = new Point(point1W.x, point1W.y - 7); + Point point3W = new Point(point2W.x + 15, point2W.y - 16); + Point point4W = new Point(point3W.x, point1W.y); + + + pathBackWing1.addPoint(point1W.x, point1W.y); + pathBackWing1.addPoint(point2W.x, point2W.y); + pathBackWing1.addPoint(point3W.x, point3W.y); + pathBackWing1.addPoint(point4W.x, point4W.y); + + g.setColor(Color.BLACK); + g.drawPolygon(pathBackWing1); + + g.setColor(Plane.BodyColor); + g.fillPolygon(pathBackWing1); + + Polygon pathBackWing2 = new Polygon(); + + Point point1W2 = new Point((int)(_startPosX + 85), (int)(_startPosY + 60)); + Point point2W2 = new Point(point1W2.x, point1W2.y + 7); + Point point3W2 = new Point(point2W2.x + 15, point2W2.y +16); + Point point4W2 = new Point(point3W2.x, point1W2.y); + + pathBackWing2.addPoint(point1W2.x, point1W2.y); + pathBackWing2.addPoint(point2W2.x, point2W2.y); + pathBackWing2.addPoint(point3W2.x, point3W2.y); + pathBackWing2.addPoint(point4W2.x, point4W2.y); + + g.setColor(Color.BLACK); + g.drawPolygon(pathBackWing2); + + g.setColor(Plane.BodyColor); + g.fillPolygon(pathBackWing2); + + //Nose + Polygon pathBoseBody = new Polygon(); + + Point point1N = new Point((int)(_startPosX + 20), (int)(_startPosY + 45)); + Point point2N = new Point(point1N.x - 15 ,point1N.y + 7); + Point point3N = new Point(point1N.x, point1N.y + 15); + + pathBoseBody.addPoint(point1N.x, point1N.y); + pathBoseBody.addPoint(point2N.x, point2N.y); + pathBoseBody.addPoint(point3N.x, point3N.y); + + + g.setColor(Color.BLACK); + g.drawPolygon(pathBoseBody); + + g.setColor(Plane.BodyColor); + g.fillPolygon(pathBoseBody); + + //Engines + _engines.drawEngines(g,_startPosX,_startPosY, Plane.BodyColor); + + + + } + + public void ChangeBorders(int width, int height) + { + _pictureWidth = width; + _pictureHeight = height; + + if (_pictureWidth <= _aircraftWidth || _pictureHeight <= _aircraftHeight) + { + _pictureHeight = null; + _pictureWidth = null; + return; + } + if (_startPosX + _aircraftWidth > _pictureWidth) + { + _startPosX = _pictureWidth - _aircraftWidth; + } + if (_startPosY + _aircraftHeight > _pictureHeight) + { + _startPosY = _pictureHeight - _aircraftHeight; + } + } +} diff --git a/Classes/DrawingEngines.java b/Classes/DrawingEngines.java new file mode 100644 index 0000000..30d0314 --- /dev/null +++ b/Classes/DrawingEngines.java @@ -0,0 +1,69 @@ +package Classes; + + +import java.awt.*; + +public class DrawingEngines +{ + private Engines enginesCount; + + + public void setEngines(int count) + { + enginesCount = Engines.getEnginesEnum(count); + } + + public void drawEngines(Graphics g,int startPosX,int startPosY,Color bodyColor) + { + if(enginesCount != null) + { + switch(enginesCount) + { + case TwoEngines -> { + g.setColor((Color.BLACK)); + g.drawOval(startPosX + 40,startPosY + 5,30,10); + g.drawOval(startPosX + 40,startPosY + 90,30,10); + g.setColor((bodyColor)); + g.fillOval(startPosX + 40,startPosY + 5,30,10); + g.fillOval(startPosX + 40,startPosY + 90,30,10); + } + + case FourEngines -> { + + g.setColor((Color.BLACK)); + g.drawOval(startPosX + 40,startPosY + 5,30,10); + g.drawOval(startPosX + 40,startPosY + 90,30,10); + g.drawOval(startPosX + 40,startPosY + 20,30,10); + g.drawOval(startPosX + 40,startPosY + 75,30,10); + g.setColor(bodyColor); + g.fillOval(startPosX + 40,startPosY + 5,30,10); + g.fillOval(startPosX + 40,startPosY + 90,30,10); + g.fillOval(startPosX + 40,startPosY + 20,30,10); + g.fillOval(startPosX + 40,startPosY + 75,30,10); + } + + case SixEngines -> { + + g.setColor((Color.BLACK)); + g.drawOval(startPosX + 40,startPosY + 5,30,10); + g.drawOval(startPosX + 40,startPosY + 90,30,10); + g.drawOval(startPosX + 40,startPosY + 20,30,10); + g.drawOval(startPosX + 40,startPosY + 75,30,10); + g.drawOval(startPosX + 40,startPosY + 35,30,10); + g.drawOval(startPosX + 40,startPosY + 60,30,10); + g.setColor(bodyColor); + g.fillOval(startPosX + 40,startPosY + 5,30,10); + g.fillOval(startPosX + 40,startPosY + 90,30,10); + g.fillOval(startPosX + 40,startPosY + 20,30,10); + g.fillOval(startPosX + 40,startPosY + 75,30,10); + g.fillOval(startPosX + 40,startPosY + 35,30,10); + g.fillOval(startPosX + 40,startPosY + 60,30,10); + } + } + } + + + + } + +} diff --git a/Classes/Engines.java b/Classes/Engines.java new file mode 100644 index 0000000..d99b67f --- /dev/null +++ b/Classes/Engines.java @@ -0,0 +1,32 @@ +package Classes; + +public enum Engines +{ + TwoEngines(2), + FourEngines(4), + SixEngines(6); + private final int value; + Engines(int value) + { + this.value = value; + } + + public int getEngines() + { + return value; + } + + public static Engines getEnginesEnum(int count) + { + for(Engines eng : Engines.values()) + { + if(eng.getEngines() == count) + { + return eng; + } + } + return null; + } + + +} diff --git a/Classes/EntityAircraft.java b/Classes/EntityAircraft.java new file mode 100644 index 0000000..6f46e84 --- /dev/null +++ b/Classes/EntityAircraft.java @@ -0,0 +1,43 @@ +package Classes; + +import java.awt.*; + +public class EntityAircraft +{ + public int Speed; + public float Weight; + public Color BodyColor; + public float Step; + + + public void Init(int speed,float weight,Color bodyColor) + { + Speed = speed; + Weight = weight; + BodyColor = bodyColor; + Step = speed * 100 / weight; + + } + + public int getSpeed() + { + return Speed; + } + + public float getStep() + { + return Step; + } + + public float getWeight() + { + return Weight; + } + + public Color getBodyColor() + { + return BodyColor; + } + + +} \ No newline at end of file diff --git a/Form/FormAirFighter.form b/Form/FormAirFighter.form new file mode 100644 index 0000000..cac5761 --- /dev/null +++ b/Form/FormAirFighter.form @@ -0,0 +1,129 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/Form/FormAirFighter.java b/Form/FormAirFighter.java new file mode 100644 index 0000000..5df819a --- /dev/null +++ b/Form/FormAirFighter.java @@ -0,0 +1,109 @@ +package Form; +import Classes.Direction; +import Classes.DrawingAircraft; + +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 FormAirFighter extends JFrame +{ + private DrawingAircraft _aircraft; + private JPanel Form; + private JPanel label; + private JPanel pictureBox; + private JTextArea speedLabel; + private JTextArea weightLabel; + private JTextArea colorLabel; + + private JButton moveRight; + private JButton moveDown; + private JButton moveLeft; + private JButton moveUp; + private JButton btnCreate; + + private void moveButtonClick(ActionEvent event) { + if (_aircraft == null) return; + String name = ((JButton)event.getSource()).getName(); + switch (name) { + case "left" -> { + _aircraft.MoveTransport(Direction.Left); + } + case "right" -> { + _aircraft.MoveTransport(Direction.Right); + } + case "up" -> { + _aircraft.MoveTransport(Direction.Up); + } + case "down" -> { + _aircraft.MoveTransport(Direction.Down); + } + } + Draw(); + } + + private void CreateEntity() { + Random random = new Random(); + _aircraft = new DrawingAircraft(); + _aircraft.Init( + random.nextInt(10, 300), + random.nextFloat(1000, 2000), + new Color( + random.nextInt(0,256), + random.nextInt(0,256), + random.nextInt(0,256) + ) + ); + _aircraft.SetPosition( + random.nextInt(0, 100), + random.nextInt(0, 100), + Form.getWidth(), + Form.getHeight() + ); + speedLabel.setText("Speed: " + _aircraft.Plane.getSpeed()); + weightLabel.setText("Weight: " + _aircraft.Plane.getWeight()); + colorLabel.setText("Color: " + _aircraft.Plane.getBodyColor()); + + Draw(); + } + + private void resizeWindow() { + _aircraft.ChangeBorders(pictureBox.getWidth(), pictureBox.getHeight()); + Draw(); + } + + private void Draw() { + Graphics2D graphics = (Graphics2D) pictureBox.getGraphics(); + graphics.clearRect(0, 0, pictureBox.getWidth(), pictureBox.getHeight()); + pictureBox.paintComponents(graphics); + _aircraft.DrawTransport(graphics); + } + + public FormAirFighter() { + super("AirFighter"); + setContentPane(Form); + setSize(new Dimension(500, 500)); + + // action move // + ActionListener listener = this::moveButtonClick; + moveRight.addActionListener(listener); + moveDown.addActionListener(listener); + moveLeft.addActionListener(listener); + moveUp.addActionListener(listener); + + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setVisible(true); + btnCreate.addActionListener(e -> CreateEntity()); + Form.addComponentListener(new ComponentAdapter() { + @Override + public void componentResized(ComponentEvent e) { + super.componentResized(e); + resizeWindow(); + } + }); + } +} diff --git a/Resources/bottomarrow.png b/Resources/bottomarrow.png new file mode 100644 index 0000000000000000000000000000000000000000..9ac02daf00cddcdd241f8f3fadd452b2857df199 GIT binary patch literal 489 zcmex=*2V@Ay0FW*q;sELbD$siDIIlarm~UJq)9VD;NKq(`y1_g z@9c!1`CYp9ns3?myE3JJ(u>WfBtJem?{8K9`J(IPdRyx9CPg{;uKbxTK{2R-9zO0>Is_p-5qUEBLRr8mvRoYat Lc8%2w{{J@tSwe+c literal 0 HcmV?d00001 diff --git a/Resources/leftarrow.png b/Resources/leftarrow.png new file mode 100644 index 0000000000000000000000000000000000000000..b06055b42af5866f8315e260e61835bfb165b2e7 GIT binary patch literal 494 zcmex=*2V@Ay0FW*q;sELbD|s3A~=2)aI?UxA{6Xr=*0 zZ!z#NGXia77G$tz*id7CqunrVc7M)muG*z*9whIG+&AyB#->xRA0D1qcm0ptI)RQ5 zop!e@C7Bl;f>Teh?P!&0V~|@_l|B3Y+t(T|cJX`MjXgWbV#32CrxheVo%^mFp~$;5 z-1c4Z#1&UuLqlEs?=y(=f4zSuBJS~RYrFYjuPuMP%gEocUOy*1zQ6N7gZh@1 zAWm=DOHnJ9GikN7X34IV^i+J#zG`0TvtQg*r%&nCTr$~Su%s`>a-yLHqmA>%6VF6k WGA5p>j=54~DJm)|82tIb|C<0jbbm$w literal 0 HcmV?d00001 diff --git a/Resources/rightarrow.png b/Resources/rightarrow.png new file mode 100644 index 0000000000000000000000000000000000000000..fcac24b0a5689ab1a0d3d480245cb4bfbf029ca1 GIT binary patch literal 493 zcmex=*2V@Ay0FW*q;sELbD|s3A~=2)aI?UxA{6Xr=*0 zZ!z#NGXia77G$tz*dd)a>)9{vYAfq|&6iBJ7c7~ia-!(d2}T>|ji;`6EG`T@Qyp_P zYippWsHpho1FUCkJBr1`cYHP5u;W(LxjO4v(l1|r>bkRTUy8-|cl8Ifo^_-K9WR-- zY60Jhg^E!Nira$R6A!++J+pG(zkBCCeV=dAd-cvumFY$)d;A#tb>0{4e5xsKG)yzpSV4jN=(=!E4gA7 TgVqAKaLqfHL<;XQ{J#kRgqnfa literal 0 HcmV?d00001 diff --git a/Resources/upbutton.png b/Resources/upbutton.png new file mode 100644 index 0000000000000000000000000000000000000000..3672f5673b6c26b4482564addacf343d011ced5d GIT binary patch literal 489 zcmex=*2V@Ay0FW*q;sELbDynPhp<4$wG}M-VlzaTpF6GsodFS8GuDy6X{#e169s7AJXEHPYd&Ch*&*(cNQdmg^9v5`5;yLR^;`Ld-}r9ytS+vl=#XnIRb zGFH-%km#H_v!nn0fy&9s@$Bd1Y7XB!bK&Uir3XK(idmkkGW|+LfXA#^n>rK>6(>%d N(Fp|qYWV-(1OPhngGvAZ literal 0 HcmV?d00001 From 43596cc79d2d67a6a6280353db0594c83dcba3c7 Mon Sep 17 00:00:00 2001 From: 1yuee Date: Tue, 25 Oct 2022 16:34:45 +0400 Subject: [PATCH 2/6] Removed extra spaces. --- Classes/DrawingAircraft.java | 8 -------- 1 file changed, 8 deletions(-) diff --git a/Classes/DrawingAircraft.java b/Classes/DrawingAircraft.java index d59be7b..efbb755 100644 --- a/Classes/DrawingAircraft.java +++ b/Classes/DrawingAircraft.java @@ -3,7 +3,6 @@ package Classes; import java.awt.*; import java.util.Random; - public class DrawingAircraft { public EntityAircraft Plane; @@ -88,7 +87,6 @@ public class DrawingAircraft public void DrawTransport(Graphics g) { - if (_startPosX < 0 || _startPosY < 0 || _pictureHeight == null || _pictureWidth == null) { return; @@ -138,7 +136,6 @@ public class DrawingAircraft g.setColor(Plane.BodyColor); g.fillPolygon(pathWing2); - //BackWings Polygon pathBackWing1 = new Polygon(); @@ -147,7 +144,6 @@ public class DrawingAircraft Point point3W = new Point(point2W.x + 15, point2W.y - 16); Point point4W = new Point(point3W.x, point1W.y); - pathBackWing1.addPoint(point1W.x, point1W.y); pathBackWing1.addPoint(point2W.x, point2W.y); pathBackWing1.addPoint(point3W.x, point3W.y); @@ -188,7 +184,6 @@ public class DrawingAircraft pathBoseBody.addPoint(point2N.x, point2N.y); pathBoseBody.addPoint(point3N.x, point3N.y); - g.setColor(Color.BLACK); g.drawPolygon(pathBoseBody); @@ -197,9 +192,6 @@ public class DrawingAircraft //Engines _engines.drawEngines(g,_startPosX,_startPosY, Plane.BodyColor); - - - } public void ChangeBorders(int width, int height) From e3ff02cff0d8201d0f394c6541ed7f8ebbb1736e Mon Sep 17 00:00:00 2001 From: 1yuee Date: Tue, 25 Oct 2022 16:35:15 +0400 Subject: [PATCH 3/6] Removed extra spaces. --- Classes/DrawingEngines.java | 5 ----- 1 file changed, 5 deletions(-) diff --git a/Classes/DrawingEngines.java b/Classes/DrawingEngines.java index 30d0314..3c59880 100644 --- a/Classes/DrawingEngines.java +++ b/Classes/DrawingEngines.java @@ -7,7 +7,6 @@ public class DrawingEngines { private Engines enginesCount; - public void setEngines(int count) { enginesCount = Engines.getEnginesEnum(count); @@ -61,9 +60,5 @@ public class DrawingEngines } } } - - - } - } From 51c291cedbd3736d87366bc97184a8f561636ae1 Mon Sep 17 00:00:00 2001 From: 1yuee Date: Tue, 25 Oct 2022 16:35:50 +0400 Subject: [PATCH 4/6] Removed extra spaces. --- Classes/Engines.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/Classes/Engines.java b/Classes/Engines.java index d99b67f..be7bd5d 100644 --- a/Classes/Engines.java +++ b/Classes/Engines.java @@ -27,6 +27,4 @@ public enum Engines } return null; } - - } From c9fd4493f6eee05f740fa804a269ce5b8b1b39b5 Mon Sep 17 00:00:00 2001 From: 1yuee Date: Tue, 25 Oct 2022 16:36:04 +0400 Subject: [PATCH 5/6] Removed extra spaces. --- Classes/EntityAircraft.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/Classes/EntityAircraft.java b/Classes/EntityAircraft.java index 6f46e84..f426834 100644 --- a/Classes/EntityAircraft.java +++ b/Classes/EntityAircraft.java @@ -9,7 +9,6 @@ public class EntityAircraft public Color BodyColor; public float Step; - public void Init(int speed,float weight,Color bodyColor) { Speed = speed; @@ -39,5 +38,4 @@ public class EntityAircraft return BodyColor; } - } \ No newline at end of file From 1438f0a3103750d3d0103544bdf4b58ee8bd6471 Mon Sep 17 00:00:00 2001 From: 1yuee Date: Tue, 25 Oct 2022 16:36:14 +0400 Subject: [PATCH 6/6] Removed extra spaces. --- Classes/EntityAircraft.java | 1 - 1 file changed, 1 deletion(-) diff --git a/Classes/EntityAircraft.java b/Classes/EntityAircraft.java index f426834..68e46df 100644 --- a/Classes/EntityAircraft.java +++ b/Classes/EntityAircraft.java @@ -15,7 +15,6 @@ public class EntityAircraft Weight = weight; BodyColor = bodyColor; Step = speed * 100 / weight; - } public int getSpeed()