From 460a7750b149c0af94f616aca4925a3835d93156 Mon Sep 17 00:00:00 2001 From: Hells Hound Date: Sun, 9 Oct 2022 14:28:00 +0400 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=BE=20=D1=83=D1=81=D0=BB=D0=BE=D0=B6=D0=BD=D0=B5=D0=BD?= =?UTF-8?q?=D0=BD=D0=BE=D0=B5=20=D0=B7=D0=B0=D0=B4=D0=B0=D0=BD=D0=B8=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/BlockDirection.java | 5 +++ src/DrawingBlocks.java | 45 +++++++++++++++++++ src/DrawingComponents.java | 22 ++++++++++ src/DrawingWarship.java | 19 ++++++-- src/EntityWarship.java | 2 +- src/FormWarship.form | 87 +++++++++++++++++++++++-------------- src/FormWarship.java | 80 ++++++++++++++++++++++++++++++++-- src/Images/ArrowDown.jpg | Bin 0 -> 1179 bytes src/Images/ArrowLeft.jpg | Bin 0 -> 1194 bytes src/Images/ArrowRight.jpg | Bin 0 -> 1196 bytes src/Images/ArrowUp.jpg | Bin 0 -> 1191 bytes 11 files changed, 221 insertions(+), 39 deletions(-) create mode 100644 src/BlockDirection.java create mode 100644 src/DrawingBlocks.java create mode 100644 src/DrawingComponents.java create mode 100644 src/Images/ArrowDown.jpg create mode 100644 src/Images/ArrowLeft.jpg create mode 100644 src/Images/ArrowRight.jpg create mode 100644 src/Images/ArrowUp.jpg diff --git a/src/BlockDirection.java b/src/BlockDirection.java new file mode 100644 index 0000000..8d7250c --- /dev/null +++ b/src/BlockDirection.java @@ -0,0 +1,5 @@ +public enum BlockDirection { + TwoBlocks, + FourBlocks, + SixBlocks; +} diff --git a/src/DrawingBlocks.java b/src/DrawingBlocks.java new file mode 100644 index 0000000..12e68d0 --- /dev/null +++ b/src/DrawingBlocks.java @@ -0,0 +1,45 @@ +import java.awt.*; + +public class DrawingBlocks { + BlockDirection blockDirection; + public void DrawBlocks(Graphics2D g2, int _startPosX, int _startPosY){ + switch(blockDirection){ + case TwoBlocks: + g2.setColor(Color.GRAY); + g2.fillRect(_startPosX + 15, _startPosY + 10, 10, 10); + g2.fillRect(_startPosX + 15, _startPosY + 20, 10, 10); + g2.setColor(Color.BLACK); + g2.drawRect(_startPosX + 15, _startPosY + 10, 10, 10); + g2.drawRect(_startPosX + 15, _startPosY + 20, 10, 10); + break; + case FourBlocks: + g2.setColor(Color.GRAY); + g2.fillRect(_startPosX + 15, _startPosY + 10, 10, 10); + g2.fillRect(_startPosX + 15, _startPosY + 20, 10, 10); + g2.fillRect(_startPosX + 25, _startPosY + 10, 10, 10); + g2.fillRect(_startPosX + 25, _startPosY + 20, 10, 10); + g2.setColor(Color.BLACK); + g2.drawRect(_startPosX + 15, _startPosY + 10, 10, 10); + g2.drawRect(_startPosX + 15, _startPosY + 20, 10, 10); + g2.drawRect(_startPosX + 25, _startPosY + 10, 10, 10); + g2.drawRect(_startPosX + 25, _startPosY + 20, 10, 10); + break; + case SixBlocks: + g2.setColor(Color.GRAY); + g2.fillRect(_startPosX + 15, _startPosY + 10, 10, 10); + g2.fillRect(_startPosX + 15, _startPosY + 20, 10, 10); + g2.fillRect(_startPosX + 25, _startPosY + 10, 10, 10); + g2.fillRect(_startPosX + 25, _startPosY + 20, 10, 10); + g2.fillRect(_startPosX + 35, _startPosY + 10, 10, 10); + g2.fillRect(_startPosX + 35, _startPosY + 20, 10, 10); + g2.setColor(Color.BLACK); + g2.drawRect(_startPosX + 15, _startPosY + 10, 10, 10); + g2.drawRect(_startPosX + 15, _startPosY + 20, 10, 10); + g2.drawRect(_startPosX + 25, _startPosY + 10, 10, 10); + g2.drawRect(_startPosX + 25, _startPosY + 20, 10, 10); + g2.drawRect(_startPosX + 35, _startPosY + 10, 10, 10); + g2.drawRect(_startPosX + 35, _startPosY + 20, 10, 10); + break; + } + } +} diff --git a/src/DrawingComponents.java b/src/DrawingComponents.java new file mode 100644 index 0000000..bd909a7 --- /dev/null +++ b/src/DrawingComponents.java @@ -0,0 +1,22 @@ +import javax.swing.*; +import java.awt.*; + +public class DrawingComponents extends JComponent { + public DrawingWarship warship; + public DrawingComponents(){ + super(); + } + public void SetDrawingWarship(DrawingWarship warship){ + this.warship = warship; + } + + @Override + public void paintComponent(Graphics g){ + super.paintComponent(g); + Graphics2D g2 = (Graphics2D) g; + if(warship != null){ + warship.DrawTransport(g2); + } + repaint(); + } +} diff --git a/src/DrawingWarship.java b/src/DrawingWarship.java index 6899b28..04c5463 100644 --- a/src/DrawingWarship.java +++ b/src/DrawingWarship.java @@ -2,8 +2,8 @@ import java.awt.*; import java.util.Random; public class DrawingWarship { private EntityWarship Warship; - public EntityWarship GetWarship(){return Warship;} + public DrawingBlocks Blocks; private int _startPosX; private int _startPosY; @@ -18,6 +18,8 @@ public class DrawingWarship { { Warship = new EntityWarship(); Warship.Init(speed, weight, bodyColor); + Blocks = new DrawingBlocks(); + Blocks.blockDirection = BlockRandom(); } public void SetPosition(int x, int y, int width, int height) @@ -68,12 +70,11 @@ public class DrawingWarship { break; } } - public void DrawTransport(Graphics g){ + public void DrawTransport(Graphics2D g2){ if (_startPosX < 0 || _startPosY < 0 || _pictureHeight == null || _pictureWidth == null) { return; } - Graphics2D g2 = (Graphics2D) g; //главная палуба int[] pointXWarship = {_startPosX + 4, _startPosX + 94, _startPosX + 114, _startPosX + 94, _startPosX + 4}; int[] pointYWarship = {_startPosY, _startPosY, _startPosY + 20, _startPosY + 40, _startPosY + 40}; @@ -102,7 +103,19 @@ public class DrawingWarship { g2.fillRect(_startPosX, _startPosY + 5, 4, 10); g2.fillRect(_startPosX, _startPosY + 23, 4, 10); + //блоки + Blocks.DrawBlocks(g2,_startPosX, _startPosY); } + + public BlockDirection BlockRandom(){ + Random rand = new Random(); + int resRand = rand.nextInt(3); + if(resRand == 0) return BlockDirection.TwoBlocks; + if(resRand == 1) return BlockDirection.FourBlocks; + if(resRand == 2) return BlockDirection.SixBlocks; + return null; + } + public void ChangeBorders(int width, int height) { _pictureWidth = width; diff --git a/src/EntityWarship.java b/src/EntityWarship.java index 22b22c2..092a65b 100644 --- a/src/EntityWarship.java +++ b/src/EntityWarship.java @@ -17,7 +17,7 @@ public class EntityWarship { Speed = speed <= 0 ? rnd.nextInt(100) + 50 : speed; Weight = weight <= 0 ? rnd.nextInt(30)+40 : weight; BodyColor= bodyColor; - Step = Speed * 2000 / Weight; + Step = Speed * 100 / Weight; } } diff --git a/src/FormWarship.form b/src/FormWarship.form index ab0eb6c..12eb718 100644 --- a/src/FormWarship.form +++ b/src/FormWarship.form @@ -3,7 +3,7 @@ - + @@ -21,24 +21,19 @@ - - - - - - - - + + + - + - + @@ -46,44 +41,72 @@ - + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - + + + - + - - - + + + - + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/FormWarship.java b/src/FormWarship.java index dbd6b87..404303a 100644 --- a/src/FormWarship.java +++ b/src/FormWarship.java @@ -1,32 +1,106 @@ 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 FormWarship extends JFrame { - + private DrawingWarship _warship; + public DrawingComponents drawingComponents; private JPanel mainPanel; private JButton buttonCreate; private JButton buttonLeft; private JButton buttonUp; private JButton buttonDown; private JButton buttonRight; - private JToolBar statusStrip; + private JToolBar toolBar; + private JLabel toolBarLabelSpeed; + private JLabel toolBarLabelWieght; + private JLabel toolBarLabelColor; + private JPanel drawPanel; public FormWarship(){ + InitializeComponent(); + } + private void Draw(){ + drawingComponents.repaint(); + } + + private void InitializeComponent(){ setContentPane(mainPanel); setTitle("Warship"); setSize(900,700); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); setVisible(true); + Icon iconUp = new ImageIcon("src\\Images\\ArrowUp.jpg"); + buttonUp.setIcon(iconUp); + Icon iconDown = new ImageIcon("src\\Images\\ArrowDown.jpg"); + buttonDown.setIcon(iconDown); + Icon iconLeft = new ImageIcon("src\\Images\\ArrowLeft.jpg"); + buttonLeft.setIcon(iconLeft); + Icon iconRight = new ImageIcon("src\\Images\\ArrowRight.jpg"); + buttonRight.setIcon(iconRight); + + drawingComponents = new DrawingComponents(); + drawPanel.add(drawingComponents); buttonCreate.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { Random rnd = new Random(); + _warship = new DrawingWarship(); + _warship.Init(rnd.nextInt(200) + 100, rnd.nextInt(1000) + 1000, + new Color(rnd.nextInt(256),rnd.nextInt(256),rnd.nextInt(256))); + _warship.SetPosition(rnd.nextInt(90) + 10, rnd.nextInt(90) + 10, drawPanel.getWidth(), drawPanel.getHeight()); + toolBarLabelSpeed.setText("Color: " + _warship.GetWarship().GetSpeed() + " "); + toolBarLabelWieght.setText("Weight: " + _warship.GetWarship().GetWeight() + " "); + toolBarLabelColor.setText("Color: " + _warship.GetWarship().GetBodyColor().getRed() + " " + + _warship.GetWarship().GetBodyColor().getGreen() + " " + _warship.GetWarship().GetBodyColor().getBlue()); + drawingComponents.SetDrawingWarship(_warship); + Draw(); + } + }); + buttonLeft.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if(_warship != null) _warship.MoveTransport(Direction.Left); + Draw(); + } + }); + + buttonRight.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if(_warship != null) _warship.MoveTransport(Direction.Right); + Draw(); + } + }); + + buttonUp.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if(_warship != null) _warship.MoveTransport(Direction.Up); + Draw(); + } + }); + + buttonDown.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if(_warship != null) _warship.MoveTransport(Direction.Down); + Draw(); + } + }); + + drawPanel.addComponentListener(new ComponentAdapter() { + @Override + public void componentResized(ComponentEvent e) { + _warship.ChangeBorders(drawPanel.getWidth(),drawPanel.getHeight()); + Draw(); } }); } - } diff --git a/src/Images/ArrowDown.jpg b/src/Images/ArrowDown.jpg new file mode 100644 index 0000000000000000000000000000000000000000..4813a5b13f10de1f7f3963ed49824b411c263211 GIT binary patch literal 1179 zcmex=``2_j6xdp@o1cgOJMMZh|#U;coyG6VInuyV4pa*FVB^NNrR z{vTivwh= zDOELf4NWZ*Q!{f5ODks=S2uSLPp{yR(6I1`$f)F$)U@=B%&g*)(z5c3%Btp;*0%PJ z&aO$5r%atTea6gLixw|gx@`H1m8&*w-m-Pu_8mKS9XfpE=&|D`PM*4S`O4L6*Kgds z_3+W-Cr_U}fAR9w$4{TXeEs(Q$Io9Ne=#yJL%ap|8JfQYf&OA*VPR%r2lxYE z#}NLy#lXYN2#h>tK?ZwDTKlp1SfA{4t*%rfN^TSKi^N;T5dS#!x)~Po8aqGEh zNB13Gc;nX1sAF}nR(7T)v9HYJw>z|#{hRL({S|-ge{6j;*ZQcG3;**9x9ruCDLvji z7x!#Pe4J$YU_zDP36nS0bG9F}|Do}|uf}*?)Zvd@8a07Styi616?NM^f5m~l=Ddx` zQ}}krRvy16`fK8Y{Spxsw-0}-+BUmX^zGa$nl|PF=_wm`oZ4|-LGZ>W4uvo4kK|@Q zwA=acJjaVTl`CuNPA+@xZ*=4598axd!o_djv{xK_-+Y_p?|+8>3@lGR#=icey6@8S zv}v|_|1xr??$Ys7o}B;St>S-%#=t~5_uqLn%2!I(i@QF$m~-sbD~-4BXNLccIsIw1 z{I|%m6?Uua_ZV;dZT0VZ**?(^KQGi&Kf7~u`E8@jTT=>tZklmG{>Aye%3BM}{TT8Z zuD_7)+M~F%;RIhm>REpHMVLo`d{0Erd$9p=mlwDX7sB~P4chxEbhA#^n|K9`vnM~@P literal 0 HcmV?d00001 diff --git a/src/Images/ArrowLeft.jpg b/src/Images/ArrowLeft.jpg new file mode 100644 index 0000000000000000000000000000000000000000..0f9441d286a26ec777290220c9005bc1bb7d728f GIT binary patch literal 1194 zcmex=``2_j6xdp@o1cgOJMMZh|#U;coyG6VInuyV4pa*FVB^NNrR z{vTivTDoi-j64Z8S2#W<;`iIYoATtZSx zRZU$(Q_IBE%-q7#%Gt%$&E3P(D>x)HEIcAIDmf)JEj=SMtGJ}Jth}PKs=1}Lt-YhO zYtrN?Q>RUzF>}_U#Y>hhTfSoDs!f}>Y~8kf$Ie}c4j(ys?D&b3r!HN-a`oEv8#iw~ zeDwIq(`V0LynOZX)8{W=zkUDl^B2fpj10^WZ^3%h$X_cGze8aDXdi$F2 z-)VoG_U)PGEBENU!!{i+XVy(#H{R}C5qnC`DJRdcWvN(CNWcCd*=+T zcawRqe6ahp&OcT3y`1FVsqgkl{?XqVEA4*akN-#RfPhK&UU#j}PM%PIRa-gl_B*!6 zCm-al7Ck?2kNoelY_nNY%>FY7Y^k;vddcT>vNB!hcD#de(~EG98omYr7s7v)3M}*)76&@a5%8d*A=#>|S~HBl{sc^ABo^wNIulzI!`sn{{8! zddpPLz*>}w&r_!+*deMUps%O{)gIshPExfLA?G)%`g2}=Mm#6yJq6r yz4P4dt=Q5gZ)a_q*CO>h*z;1t#kWEnp`oEpy{kkR*q9nD82A`uH4W$gzX<@aI^Ht? literal 0 HcmV?d00001 diff --git a/src/Images/ArrowRight.jpg b/src/Images/ArrowRight.jpg new file mode 100644 index 0000000000000000000000000000000000000000..cea4be560b455dbfe13920c06e392505edc77148 GIT binary patch literal 1196 zcmex=``2_j6xdp@o1cgOJMMZh|#U;coyG6VInuyV4pa*FVB^NNrR z{vTivTDoi-j64Z8S2#W<;`iIYoATtZSx zRZU$(Q_IBE%-q7#%Gt%$&E3P(D>x)HEIcAIDmf)JEj=SMtGJ}Jth}PKs=1}Lt-YhO zYtrN?Q>RUzF>}_U#Y>hhTfSoDs!f}>Y~8kf$Ie}c4j(ys?D&b3r!HN-a`oEv8#iw~ zeDwIq(`V0LynOZX)8{W=zkUDl^B2fpj10^WZ^3yO7DZy(;z{71QP-RtY~)3^1uXiRMQ9OL8W5~yfzoIH6B zgQe*Q`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