From 4144ff067a5d39780de018141bd3534e96e837f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D0=BA=D1=81=D0=B8=D0=BC=20=D0=AF=D0=BA=D0=BE?= =?UTF-8?q?=D0=B2=D0=BB=D0=B5=D0=B2?= Date: Fri, 8 Dec 2023 10:35:37 +0400 Subject: [PATCH 1/3] =?UTF-8?q?=D0=A1=D0=B4=D0=B5=D0=BB=D0=B0=D0=BD=D0=BD?= =?UTF-8?q?=D0=B0=D1=8F=201=20=D0=BB=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82?= =?UTF-8?q?=D0=BE=D1=80=D0=BD=D0=B0=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Resources/down.png | Bin 0 -> 588 bytes Resources/left.png | Bin 0 -> 645 bytes Resources/right.png | Bin 0 -> 571 bytes Resources/up.png | Bin 0 -> 601 bytes src/CountPaddles.java | 13 +++++ src/Direction.java | 6 ++ src/DrawingBoat.java | 95 +++++++++++++++++++++++++++++++ src/DrawingField.java | 52 +++++++++++++++++ src/DrawingPaddles.java | 43 ++++++++++++++ src/EntityBoat.java | 17 ++++++ src/FormBoat.java | 123 ++++++++++++++++++++++++++++++++++++++++ src/Main.java | 5 ++ 12 files changed, 354 insertions(+) create mode 100644 Resources/down.png create mode 100644 Resources/left.png create mode 100644 Resources/right.png create mode 100644 Resources/up.png create mode 100644 src/CountPaddles.java create mode 100644 src/Direction.java create mode 100644 src/DrawingBoat.java create mode 100644 src/DrawingField.java create mode 100644 src/DrawingPaddles.java create mode 100644 src/EntityBoat.java create mode 100644 src/FormBoat.java create mode 100644 src/Main.java diff --git a/Resources/down.png b/Resources/down.png new file mode 100644 index 0000000000000000000000000000000000000000..0933073bb00bf1697240aaf9b0d20d76b0e29eb4 GIT binary patch literal 588 zcmeAS@N?(olHy`uVBq!ia0vp^${@@EBp9qWy=etf3dtTpz6=aiY77hwEes65fI)^mS!_&ch7H<6nMa`fnR*{X{IKq^oBw|ay+jhS>ekQL;{KWnlNzcp= z8!)Q|rf)uB%`tP`iNpVW&tCCa#l_)Zm8Ln{U`1DSLc%b zRK{>7c?OkZdwV~MuZ%yogr&1*{j_5fcRrplGqGY*fSPf2zKW`&(8>P4pOjh7D@~P{ zD8=G>r9wbgc~XD0UgU01mn@e}+w&$LJ(usLBV3wg`SJFB9jS?btY-Opram`SpDB3q z`(3Y3u4mP|E!>))oyfU&QfcQzk8jcp8)|(043w_MvSvNE=hKvM6EFS$;j(I<;K{`7 z->!NW<=0CVU0qvz1{h2XswJ)wB`Jv|saDBFsX&Us$iT>0*U(7Uz$C=b(#piZ%D_n5 z07x2~y=A-|MMG|WN@iLmZVh|dySafHG~hOrWag$8mn7yE;MVhc(yA*!Jq(_%elF{r G5}E)z2Ffx3 literal 0 HcmV?d00001 diff --git a/Resources/left.png b/Resources/left.png new file mode 100644 index 0000000000000000000000000000000000000000..c4f6ad5cc934b430ca6a68ef3d6a8eea0a31a828 GIT binary patch literal 645 zcmeAS@N?(olHy`uVBq!ia0vp^${@@EBp9qWy=etf3dtTpz6=aiY77hwEes65fI)^mS!_&ch!z2a<#t=^z#}JFtt&=u-u^0-p{rB9oXfMaKCXF9VU333- zF3{k(c8goA?0Q5;f5PhCr3sZ0ce!H{lb<)X7A$?tP86eZG+6(vyIQl)Yj1tJs-eOUZE6Uc32k10!R3^PL{+1wrk5idMvjYB(R{ zo)IYeD2LaeLhdk^Qibfh!-`z@&4Re(!X|Y|+9>Z#tBLVp6RnB#zV_zl)~OSw*nLBtqvsS6XVzjYrfS~k61i7TOQ;fm8KmRD~}N?6P(I8n&Muy9Ax=Q=-K$AII8i`h3P zZQU(m#IeL}rzulf-NxPXm=Cb9E0rW}+su92Wc_`%+!?!W{?2)~_f?Z2Dxg9YeY#(Vo9o1a#1RfVlXl=GS)RT(lsy% zF|@QYF$5xQ0|P4qgXz0`-l1s7%}>cptHiD0+4C=;M56(>p(HamwYVfPw*a@Eji0Xe Q0`)L>y85}Sb4q9e0FfB;X8-^I literal 0 HcmV?d00001 diff --git a/Resources/right.png b/Resources/right.png new file mode 100644 index 0000000000000000000000000000000000000000..f75903e7f9f5eea3ba17ffe4ec0e0ba0e030c58a GIT binary patch literal 571 zcmeAS@N?(olHy`uVBq!ia0vp^${@@EBp9qWy=etf3dtTpz6=aiY77hwEes65fI)^mS!_&ch^>=C?P>&=?PjG%(NqJ&X lDnogBxn5>oc5!lIL8@MUQTpt6Hc~)E44$rjF6*2UngHOp&eH$@ literal 0 HcmV?d00001 diff --git a/Resources/up.png b/Resources/up.png new file mode 100644 index 0000000000000000000000000000000000000000..b0384df1546ae75031b5be7e0768e88e7d4d58de GIT binary patch literal 601 zcmeAS@N?(olHy`uVBq!ia0vp^${@@EBp9qWy=etf3dtTpz6=aiY77hwEes65fI)^mS!_&ch4F~s8Z+lkg*E{+1O{`G%cv>vhfu(%e8H#Igj zdJ8xn%(>I7aDqk6Ev>DoW5H?xmV>4p9cqX6yxYb0+0M3kbMwx`?+aJ?iE1Sotv2He znK|`*k3rZ=0X@?Rz6n0ebA@HTukz{;T3OP5eB+l@Ar2xQmrgVB*}R^AO4g-4rFP9j zMkecNYg;n}Pgl-nV3>H>Xk%>XIc$zX}3h?O?hSIDgL;3 zmW{?tC4ro;W?K5+=I=2LeUtJ2c>DcInUd`iFLvB{xOLSt?WQ?C6VgM<422#v$q35m zHJI$1FVTK_j*0f-A5&%;{eRByy{p*$gR685Fc?)!Tq8+NTTD!{F)a=d#Wzp$PzJuGf(O literal 0 HcmV?d00001 diff --git a/src/CountPaddles.java b/src/CountPaddles.java new file mode 100644 index 0000000..e7d0704 --- /dev/null +++ b/src/CountPaddles.java @@ -0,0 +1,13 @@ +public enum CountPaddles { + One(1), + Two(2), + Three(3); + + private final int Value; + CountPaddles(int Count) { + Value = Count; + } + public int getCountPaddles(){ + return Value; + } +} diff --git a/src/Direction.java b/src/Direction.java new file mode 100644 index 0000000..a641b80 --- /dev/null +++ b/src/Direction.java @@ -0,0 +1,6 @@ +public enum Direction { + Up, + Down, + Left, + Right +} diff --git a/src/DrawingBoat.java b/src/DrawingBoat.java new file mode 100644 index 0000000..6575909 --- /dev/null +++ b/src/DrawingBoat.java @@ -0,0 +1,95 @@ +import java.awt.*; +public class DrawingBoat { + public EntityBoat Boat; + + public DrawingPaddles Paddles; + private int _startPosX; + private int _startPosY; + private Integer _pictureWidth = null; + private Integer _pictureHeight = null; + private final int _BoatWidth = 180; + private final int _BoatHeight = 160; + + public void Init(int speed, float weight, Color bodyColor){ + Boat = new EntityBoat(); + Boat.Init(speed, weight, bodyColor); + Paddles = new DrawingPaddles(); + Paddles.SetCountPaddles((int)(1+Math.random()+Math.random()*2)); + } + + public void SetPosition(int x, int y, int width, int height){ + if(x >= 0 && x+_BoatWidth <= width && y+_BoatHeight <= height){ + _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 + _BoatWidth + Boat.Step < _pictureWidth){ + _startPosX += Boat.Step; + } + break; + case Left: + if(_startPosX - Boat.Step >= 0){ + _startPosX -= Boat.Step; + } + break; + case Up: + if (_startPosY - Boat.Step >= 0) + { + _startPosY -= Boat.Step; + } + break; + + case Down: + if (_startPosY + _BoatHeight + Boat.Step < _pictureHeight) + { + _startPosY += Boat.Step; + } + break; + } + } + + public void DrawTransport(Graphics g){ + if (_startPosX < 0 || _startPosY < 0 || _pictureHeight== null || _pictureWidth== null) + { + return; + } + + Graphics2D g2 = (Graphics2D)g; + + g2.setColor(Color.BLACK); + + g2.drawLine(_startPosX + 10, _startPosY + 60, _startPosX + 110, _startPosY + 60); + g2.drawLine( _startPosX + 110, _startPosY + 60, _startPosX + 180, _startPosY + 90); + g2.drawLine( _startPosX + 180, _startPosY + 90, _startPosX + 110, _startPosY + 120); + g2.drawLine( _startPosX + 110, _startPosY + 120, _startPosX + 10, _startPosY + 120); + g2.drawLine(_startPosX + 10, _startPosY + 120, _startPosX + 10, _startPosY + 60); + g2.drawOval( _startPosX + 15, _startPosY + 65, 95, 50); + + Paddles.DrawPaddles(g2, _startPosX, _startPosY); + + g2.setColor(Color.GRAY); + + g2.fillRect( _startPosX + 2, _startPosY + 65, 10, 50); + g2.fillRect( _startPosX + 15, _startPosY + 53, 90, 10); + g2.fillRect( _startPosX + 15, _startPosY + 118, 90, 10); + + g2.setColor(Color.BLACK); + + g2.drawRect( _startPosX + 60, _startPosY , 4, 90); + g2.drawRect( _startPosX + 60, _startPosY , 4, 90); + g2.drawLine( _startPosX + 25, _startPosY + 20, _startPosX + 100, _startPosY); + g2.drawLine( _startPosX + 100, _startPosY, _startPosX + 100, _startPosY + 50); + g2.drawLine( _startPosX + 25, _startPosY + 70, _startPosX + 100, _startPosY + 50); + g2.drawLine( _startPosX + 25, _startPosY + 70, _startPosX + 25, _startPosY + 20); + } +} diff --git a/src/DrawingField.java b/src/DrawingField.java new file mode 100644 index 0000000..6e40815 --- /dev/null +++ b/src/DrawingField.java @@ -0,0 +1,52 @@ +import javax.swing.*; +import java.awt.*; +import java.util.Random; +public class DrawingField extends JPanel{ + private final FormBoat field; + DrawingBoat _Boat; + public DrawingField(FormBoat field){ + this.field = field; + } + @Override + protected void paintComponent(Graphics g){ + super.paintComponent(g); + Graphics2D g2 = (Graphics2D)g; + if(_Boat != null){ + _Boat.DrawTransport(g2); + } + else return; + } + public void UpButtonAction(){ + if(_Boat != null){ + _Boat.MoveTransport(Direction.Up); + } else return; + } + public void DownButtonAction(){ + if (_Boat!=null){ + _Boat.MoveTransport(Direction.Down); + } + else + return; + } + public void RightButtonAction(){ + if (_Boat!=null){ + _Boat.MoveTransport(Direction.Right); + } + else + return; + } + public void LeftButtonAction(){ + if (_Boat!=null){ + _Boat.MoveTransport(Direction.Left); + } + else + return; + } + + public void CreateButtonAction(){ + Random r = new Random(); + _Boat = new DrawingBoat(); + _Boat.Init(r.nextInt(50)+10,r.nextInt(100)+500,new Color(r.nextInt(256),r.nextInt(256),r.nextInt(256))); + _Boat.SetPosition(r.nextInt(100)+10,r.nextInt(100)+10,getWidth(),getHeight()); + } +} diff --git a/src/DrawingPaddles.java b/src/DrawingPaddles.java new file mode 100644 index 0000000..9787a3c --- /dev/null +++ b/src/DrawingPaddles.java @@ -0,0 +1,43 @@ +import java.awt.*; +public class DrawingPaddles { + private CountPaddles _paddles; + + private int step = 0; + + public void SetCountPaddles(int Count){ + for(CountPaddles temp : CountPaddles.values()) + if(temp.getCountPaddles() == Count){ + _paddles = temp; + return; + } + } + + public void DrawPaddles(Graphics2D g, int _startPosX, int _startPosY){ + step = 0; + switch(_paddles.getCountPaddles()){ + case 1: + drawPaddle(g,_startPosX,_startPosY); + break; + case 2: + for(int i = 0;i<2;i++){ + drawPaddle(g,_startPosX,_startPosY); + step+=25; + } + break; + case 3: + for(int i = 0;i<3;i++){ + drawPaddle(g,_startPosX,_startPosY); + step+=25; + } + break; + } + } + + private void drawPaddle(Graphics2D g, int _startPosX, int _startPosY){ + g.setColor(Color.BLACK); + g.drawLine(_startPosX + 20+step, _startPosY + 60, _startPosX + 35+step, _startPosY + 27); + g.drawOval(_startPosX+32+step, _startPosY+13, 10, 15); + g.drawLine(_startPosX + 20+step, _startPosY + 120, _startPosX + 35+step, _startPosY + 150); + g.drawOval(_startPosX+32+step, _startPosY+150, 10, 15); + } +} diff --git a/src/EntityBoat.java b/src/EntityBoat.java new file mode 100644 index 0000000..653e3f9 --- /dev/null +++ b/src/EntityBoat.java @@ -0,0 +1,17 @@ +import java.awt.*; +import java.util.Random; +public class EntityBoat { + private int Speed; + + private float Weight; + private Color BodyColor; + public float Step; + + public void Init(int speed, float weight, Color bodyColor){ + Random r = new Random(); + Speed = speed <= 0 ? r.nextInt(50)+10 : speed; + Weight = weight <= 0 ? r.nextInt(100)+500:weight; + BodyColor = bodyColor; + Step = Speed * 600 / (int)Weight; + } +} diff --git a/src/FormBoat.java b/src/FormBoat.java new file mode 100644 index 0000000..bdb2ed5 --- /dev/null +++ b/src/FormBoat.java @@ -0,0 +1,123 @@ +import javax.swing.*; +import java.awt.*; + +public class FormBoat extends JFrame{ + private final int Width; + private final int Height; + + JPanel BottomPanel = new JPanel(); + JPanel CreatePanel = new JPanel(); + JPanel BottomAndCreatePanel = new JPanel(); + JPanel DimentionPanel = new JPanel(); + JPanel UpPanel = new JPanel(); + JPanel DownPanel = new JPanel(); + JPanel LRPanel = new JPanel(); + + DrawingField field = new DrawingField(this); + + JButton ButtonCreate = new JButton("Создать"); + + Icon arrowUp = new ImageIcon("Resources/up.png"); + JButton ButtonUp = new JButton(arrowUp); + + Icon arrowLeft = new ImageIcon("Resources/left.png"); + JButton ButtonLeft = new JButton(arrowLeft); + + Icon arrowDown = new ImageIcon("Resources/down.png"); + JButton ButtonDown = new JButton(arrowDown); + + Icon arrowRight = new ImageIcon("Resources/right.png"); + JButton ButtonRight = new JButton(arrowRight); + + public FormBoat(){ + super("Boat"); + setSize(800, 600); + Width = getWidth(); + Height = getHeight(); + ShowWindow(); + RefreshWindow(); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setVisible(true); + } + + private void ShowWindow() { + Dimension dimen = new Dimension(35,35); + + // Обработка нажатия кнопки + ButtonUp.setPreferredSize(dimen); + ButtonUp.addActionListener(e->{ + field.UpButtonAction(); + repaint(); + }); + + // Обработка нажатия кнопки + ButtonDown.setPreferredSize(dimen); + ButtonDown.addActionListener(e->{ + field.DownButtonAction(); + repaint(); + }); + + // Обработка нажатия кнопки + ButtonRight.setPreferredSize(dimen); + ButtonRight.addActionListener(e->{ + field.RightButtonAction(); + repaint(); + }); + + // Обработка нажатия кнопки + ButtonLeft.setPreferredSize(dimen); + ButtonLeft.addActionListener(e->{ + field.LeftButtonAction(); + repaint(); + }); + + // Добавление кнопок на панель (Левая и правая стрелки) + LRPanel.setLayout(new FlowLayout(FlowLayout.CENTER,50,0)); + LRPanel.setBackground(new Color(0,0,0,0)); + LRPanel.add(ButtonLeft); + LRPanel.add(ButtonRight); + + // Добавление кнопки (Стрелка вверх) + UpPanel.setLayout(new FlowLayout()); + UpPanel.setBackground(new Color(0,0,0,0)); + UpPanel.add(ButtonUp); + + // Добавление кнопки (Стрелка вниз) + DownPanel.setLayout(new FlowLayout()); + DownPanel.setBackground(new Color(0,0,0,0)); + DownPanel.add(ButtonDown); + + DimentionPanel.setLayout(new BoxLayout(DimentionPanel,BoxLayout.Y_AXIS)); + DimentionPanel.setBackground(new Color(0,0,0,0)); + DimentionPanel.add(UpPanel); + DimentionPanel.add(LRPanel); + DimentionPanel.add(DownPanel); + add(DimentionPanel); + + //нажатие кнопки + CreatePanel.setLayout(new FlowLayout()); + CreatePanel.setBackground(new Color(0,0,0,0)); + CreatePanel.add(ButtonCreate); + ButtonCreate.addActionListener(e->{ + field.CreateButtonAction(); + repaint(); + }); + + BottomPanel.setLayout(new FlowLayout()); + BottomPanel.setBackground(new Color(0,0,0,0)); + + BottomAndCreatePanel.setLayout(new BoxLayout(BottomAndCreatePanel,BoxLayout.Y_AXIS)); + BottomAndCreatePanel.setBackground(new Color(0,0,0,0)); + BottomAndCreatePanel.add(CreatePanel); + BottomAndCreatePanel.add(BottomPanel); + + add(BottomAndCreatePanel); + add(field); + + } + public void RefreshWindow(){ + field.setBounds(0,0,Width,Height); + BottomAndCreatePanel.setBounds(-320,Height-110,Width,80); + DimentionPanel.setBounds(Width-170,Height-170,190,140); + } +} diff --git a/src/Main.java b/src/Main.java new file mode 100644 index 0000000..506164d --- /dev/null +++ b/src/Main.java @@ -0,0 +1,5 @@ +public class Main { + public static void main(String[] args) { + new FormBoat(); + } +} \ No newline at end of file -- 2.25.1 From 1873882d103497f4a81bf9a95b8ef1b3ba4cfbce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D0=BA=D1=81=D0=B8=D0=BC=20=D0=AF=D0=BA=D0=BE?= =?UTF-8?q?=D0=B2=D0=BB=D0=B5=D0=B2?= Date: Tue, 19 Dec 2023 12:58:45 +0400 Subject: [PATCH 2/3] =?UTF-8?q?=D0=A1=D0=B4=D0=B5=D0=BB=D0=B0=D0=BD=D0=BD?= =?UTF-8?q?=D0=B0=D1=8F=202=20=D0=BB=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82?= =?UTF-8?q?=D0=BE=D1=80=D0=BD=D0=B0=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/CountPaddles.java | 13 ----- src/Direction.java | 6 -- src/DrawingBoat.java | 95 ------------------------------- src/DrawingField.java | 52 ----------------- src/DrawingPaddles.java | 43 -------------- src/EntityBoat.java | 17 ------ src/FormBoat.java | 123 ---------------------------------------- src/Main.java | 5 -- 8 files changed, 354 deletions(-) delete mode 100644 src/CountPaddles.java delete mode 100644 src/Direction.java delete mode 100644 src/DrawingBoat.java delete mode 100644 src/DrawingField.java delete mode 100644 src/DrawingPaddles.java delete mode 100644 src/EntityBoat.java delete mode 100644 src/FormBoat.java delete mode 100644 src/Main.java diff --git a/src/CountPaddles.java b/src/CountPaddles.java deleted file mode 100644 index e7d0704..0000000 --- a/src/CountPaddles.java +++ /dev/null @@ -1,13 +0,0 @@ -public enum CountPaddles { - One(1), - Two(2), - Three(3); - - private final int Value; - CountPaddles(int Count) { - Value = Count; - } - public int getCountPaddles(){ - return Value; - } -} diff --git a/src/Direction.java b/src/Direction.java deleted file mode 100644 index a641b80..0000000 --- a/src/Direction.java +++ /dev/null @@ -1,6 +0,0 @@ -public enum Direction { - Up, - Down, - Left, - Right -} diff --git a/src/DrawingBoat.java b/src/DrawingBoat.java deleted file mode 100644 index 6575909..0000000 --- a/src/DrawingBoat.java +++ /dev/null @@ -1,95 +0,0 @@ -import java.awt.*; -public class DrawingBoat { - public EntityBoat Boat; - - public DrawingPaddles Paddles; - private int _startPosX; - private int _startPosY; - private Integer _pictureWidth = null; - private Integer _pictureHeight = null; - private final int _BoatWidth = 180; - private final int _BoatHeight = 160; - - public void Init(int speed, float weight, Color bodyColor){ - Boat = new EntityBoat(); - Boat.Init(speed, weight, bodyColor); - Paddles = new DrawingPaddles(); - Paddles.SetCountPaddles((int)(1+Math.random()+Math.random()*2)); - } - - public void SetPosition(int x, int y, int width, int height){ - if(x >= 0 && x+_BoatWidth <= width && y+_BoatHeight <= height){ - _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 + _BoatWidth + Boat.Step < _pictureWidth){ - _startPosX += Boat.Step; - } - break; - case Left: - if(_startPosX - Boat.Step >= 0){ - _startPosX -= Boat.Step; - } - break; - case Up: - if (_startPosY - Boat.Step >= 0) - { - _startPosY -= Boat.Step; - } - break; - - case Down: - if (_startPosY + _BoatHeight + Boat.Step < _pictureHeight) - { - _startPosY += Boat.Step; - } - break; - } - } - - public void DrawTransport(Graphics g){ - if (_startPosX < 0 || _startPosY < 0 || _pictureHeight== null || _pictureWidth== null) - { - return; - } - - Graphics2D g2 = (Graphics2D)g; - - g2.setColor(Color.BLACK); - - g2.drawLine(_startPosX + 10, _startPosY + 60, _startPosX + 110, _startPosY + 60); - g2.drawLine( _startPosX + 110, _startPosY + 60, _startPosX + 180, _startPosY + 90); - g2.drawLine( _startPosX + 180, _startPosY + 90, _startPosX + 110, _startPosY + 120); - g2.drawLine( _startPosX + 110, _startPosY + 120, _startPosX + 10, _startPosY + 120); - g2.drawLine(_startPosX + 10, _startPosY + 120, _startPosX + 10, _startPosY + 60); - g2.drawOval( _startPosX + 15, _startPosY + 65, 95, 50); - - Paddles.DrawPaddles(g2, _startPosX, _startPosY); - - g2.setColor(Color.GRAY); - - g2.fillRect( _startPosX + 2, _startPosY + 65, 10, 50); - g2.fillRect( _startPosX + 15, _startPosY + 53, 90, 10); - g2.fillRect( _startPosX + 15, _startPosY + 118, 90, 10); - - g2.setColor(Color.BLACK); - - g2.drawRect( _startPosX + 60, _startPosY , 4, 90); - g2.drawRect( _startPosX + 60, _startPosY , 4, 90); - g2.drawLine( _startPosX + 25, _startPosY + 20, _startPosX + 100, _startPosY); - g2.drawLine( _startPosX + 100, _startPosY, _startPosX + 100, _startPosY + 50); - g2.drawLine( _startPosX + 25, _startPosY + 70, _startPosX + 100, _startPosY + 50); - g2.drawLine( _startPosX + 25, _startPosY + 70, _startPosX + 25, _startPosY + 20); - } -} diff --git a/src/DrawingField.java b/src/DrawingField.java deleted file mode 100644 index 6e40815..0000000 --- a/src/DrawingField.java +++ /dev/null @@ -1,52 +0,0 @@ -import javax.swing.*; -import java.awt.*; -import java.util.Random; -public class DrawingField extends JPanel{ - private final FormBoat field; - DrawingBoat _Boat; - public DrawingField(FormBoat field){ - this.field = field; - } - @Override - protected void paintComponent(Graphics g){ - super.paintComponent(g); - Graphics2D g2 = (Graphics2D)g; - if(_Boat != null){ - _Boat.DrawTransport(g2); - } - else return; - } - public void UpButtonAction(){ - if(_Boat != null){ - _Boat.MoveTransport(Direction.Up); - } else return; - } - public void DownButtonAction(){ - if (_Boat!=null){ - _Boat.MoveTransport(Direction.Down); - } - else - return; - } - public void RightButtonAction(){ - if (_Boat!=null){ - _Boat.MoveTransport(Direction.Right); - } - else - return; - } - public void LeftButtonAction(){ - if (_Boat!=null){ - _Boat.MoveTransport(Direction.Left); - } - else - return; - } - - public void CreateButtonAction(){ - Random r = new Random(); - _Boat = new DrawingBoat(); - _Boat.Init(r.nextInt(50)+10,r.nextInt(100)+500,new Color(r.nextInt(256),r.nextInt(256),r.nextInt(256))); - _Boat.SetPosition(r.nextInt(100)+10,r.nextInt(100)+10,getWidth(),getHeight()); - } -} diff --git a/src/DrawingPaddles.java b/src/DrawingPaddles.java deleted file mode 100644 index 9787a3c..0000000 --- a/src/DrawingPaddles.java +++ /dev/null @@ -1,43 +0,0 @@ -import java.awt.*; -public class DrawingPaddles { - private CountPaddles _paddles; - - private int step = 0; - - public void SetCountPaddles(int Count){ - for(CountPaddles temp : CountPaddles.values()) - if(temp.getCountPaddles() == Count){ - _paddles = temp; - return; - } - } - - public void DrawPaddles(Graphics2D g, int _startPosX, int _startPosY){ - step = 0; - switch(_paddles.getCountPaddles()){ - case 1: - drawPaddle(g,_startPosX,_startPosY); - break; - case 2: - for(int i = 0;i<2;i++){ - drawPaddle(g,_startPosX,_startPosY); - step+=25; - } - break; - case 3: - for(int i = 0;i<3;i++){ - drawPaddle(g,_startPosX,_startPosY); - step+=25; - } - break; - } - } - - private void drawPaddle(Graphics2D g, int _startPosX, int _startPosY){ - g.setColor(Color.BLACK); - g.drawLine(_startPosX + 20+step, _startPosY + 60, _startPosX + 35+step, _startPosY + 27); - g.drawOval(_startPosX+32+step, _startPosY+13, 10, 15); - g.drawLine(_startPosX + 20+step, _startPosY + 120, _startPosX + 35+step, _startPosY + 150); - g.drawOval(_startPosX+32+step, _startPosY+150, 10, 15); - } -} diff --git a/src/EntityBoat.java b/src/EntityBoat.java deleted file mode 100644 index 653e3f9..0000000 --- a/src/EntityBoat.java +++ /dev/null @@ -1,17 +0,0 @@ -import java.awt.*; -import java.util.Random; -public class EntityBoat { - private int Speed; - - private float Weight; - private Color BodyColor; - public float Step; - - public void Init(int speed, float weight, Color bodyColor){ - Random r = new Random(); - Speed = speed <= 0 ? r.nextInt(50)+10 : speed; - Weight = weight <= 0 ? r.nextInt(100)+500:weight; - BodyColor = bodyColor; - Step = Speed * 600 / (int)Weight; - } -} diff --git a/src/FormBoat.java b/src/FormBoat.java deleted file mode 100644 index bdb2ed5..0000000 --- a/src/FormBoat.java +++ /dev/null @@ -1,123 +0,0 @@ -import javax.swing.*; -import java.awt.*; - -public class FormBoat extends JFrame{ - private final int Width; - private final int Height; - - JPanel BottomPanel = new JPanel(); - JPanel CreatePanel = new JPanel(); - JPanel BottomAndCreatePanel = new JPanel(); - JPanel DimentionPanel = new JPanel(); - JPanel UpPanel = new JPanel(); - JPanel DownPanel = new JPanel(); - JPanel LRPanel = new JPanel(); - - DrawingField field = new DrawingField(this); - - JButton ButtonCreate = new JButton("Создать"); - - Icon arrowUp = new ImageIcon("Resources/up.png"); - JButton ButtonUp = new JButton(arrowUp); - - Icon arrowLeft = new ImageIcon("Resources/left.png"); - JButton ButtonLeft = new JButton(arrowLeft); - - Icon arrowDown = new ImageIcon("Resources/down.png"); - JButton ButtonDown = new JButton(arrowDown); - - Icon arrowRight = new ImageIcon("Resources/right.png"); - JButton ButtonRight = new JButton(arrowRight); - - public FormBoat(){ - super("Boat"); - setSize(800, 600); - Width = getWidth(); - Height = getHeight(); - ShowWindow(); - RefreshWindow(); - setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - setVisible(true); - } - - private void ShowWindow() { - Dimension dimen = new Dimension(35,35); - - // Обработка нажатия кнопки - ButtonUp.setPreferredSize(dimen); - ButtonUp.addActionListener(e->{ - field.UpButtonAction(); - repaint(); - }); - - // Обработка нажатия кнопки - ButtonDown.setPreferredSize(dimen); - ButtonDown.addActionListener(e->{ - field.DownButtonAction(); - repaint(); - }); - - // Обработка нажатия кнопки - ButtonRight.setPreferredSize(dimen); - ButtonRight.addActionListener(e->{ - field.RightButtonAction(); - repaint(); - }); - - // Обработка нажатия кнопки - ButtonLeft.setPreferredSize(dimen); - ButtonLeft.addActionListener(e->{ - field.LeftButtonAction(); - repaint(); - }); - - // Добавление кнопок на панель (Левая и правая стрелки) - LRPanel.setLayout(new FlowLayout(FlowLayout.CENTER,50,0)); - LRPanel.setBackground(new Color(0,0,0,0)); - LRPanel.add(ButtonLeft); - LRPanel.add(ButtonRight); - - // Добавление кнопки (Стрелка вверх) - UpPanel.setLayout(new FlowLayout()); - UpPanel.setBackground(new Color(0,0,0,0)); - UpPanel.add(ButtonUp); - - // Добавление кнопки (Стрелка вниз) - DownPanel.setLayout(new FlowLayout()); - DownPanel.setBackground(new Color(0,0,0,0)); - DownPanel.add(ButtonDown); - - DimentionPanel.setLayout(new BoxLayout(DimentionPanel,BoxLayout.Y_AXIS)); - DimentionPanel.setBackground(new Color(0,0,0,0)); - DimentionPanel.add(UpPanel); - DimentionPanel.add(LRPanel); - DimentionPanel.add(DownPanel); - add(DimentionPanel); - - //нажатие кнопки - CreatePanel.setLayout(new FlowLayout()); - CreatePanel.setBackground(new Color(0,0,0,0)); - CreatePanel.add(ButtonCreate); - ButtonCreate.addActionListener(e->{ - field.CreateButtonAction(); - repaint(); - }); - - BottomPanel.setLayout(new FlowLayout()); - BottomPanel.setBackground(new Color(0,0,0,0)); - - BottomAndCreatePanel.setLayout(new BoxLayout(BottomAndCreatePanel,BoxLayout.Y_AXIS)); - BottomAndCreatePanel.setBackground(new Color(0,0,0,0)); - BottomAndCreatePanel.add(CreatePanel); - BottomAndCreatePanel.add(BottomPanel); - - add(BottomAndCreatePanel); - add(field); - - } - public void RefreshWindow(){ - field.setBounds(0,0,Width,Height); - BottomAndCreatePanel.setBounds(-320,Height-110,Width,80); - DimentionPanel.setBounds(Width-170,Height-170,190,140); - } -} diff --git a/src/Main.java b/src/Main.java deleted file mode 100644 index 506164d..0000000 --- a/src/Main.java +++ /dev/null @@ -1,5 +0,0 @@ -public class Main { - public static void main(String[] args) { - new FormBoat(); - } -} \ No newline at end of file -- 2.25.1 From 202fddbcffb60ec7895a96d911f92840acd78982 Mon Sep 17 00:00:00 2001 From: YakovlevMaxim Date: Tue, 19 Dec 2023 13:01:48 +0400 Subject: [PATCH 3/3] revert 1873882d103497f4a81bf9a95b8ef1b3ba4cfbce MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit revert Сделанная 2 лабораторная --- src/CountPaddles.java | 13 +++++ src/Direction.java | 6 ++ src/DrawingBoat.java | 95 +++++++++++++++++++++++++++++++ src/DrawingField.java | 52 +++++++++++++++++ src/DrawingPaddles.java | 43 ++++++++++++++ src/EntityBoat.java | 17 ++++++ src/FormBoat.java | 123 ++++++++++++++++++++++++++++++++++++++++ src/Main.java | 5 ++ 8 files changed, 354 insertions(+) create mode 100644 src/CountPaddles.java create mode 100644 src/Direction.java create mode 100644 src/DrawingBoat.java create mode 100644 src/DrawingField.java create mode 100644 src/DrawingPaddles.java create mode 100644 src/EntityBoat.java create mode 100644 src/FormBoat.java create mode 100644 src/Main.java diff --git a/src/CountPaddles.java b/src/CountPaddles.java new file mode 100644 index 0000000..e7d0704 --- /dev/null +++ b/src/CountPaddles.java @@ -0,0 +1,13 @@ +public enum CountPaddles { + One(1), + Two(2), + Three(3); + + private final int Value; + CountPaddles(int Count) { + Value = Count; + } + public int getCountPaddles(){ + return Value; + } +} diff --git a/src/Direction.java b/src/Direction.java new file mode 100644 index 0000000..a641b80 --- /dev/null +++ b/src/Direction.java @@ -0,0 +1,6 @@ +public enum Direction { + Up, + Down, + Left, + Right +} diff --git a/src/DrawingBoat.java b/src/DrawingBoat.java new file mode 100644 index 0000000..6575909 --- /dev/null +++ b/src/DrawingBoat.java @@ -0,0 +1,95 @@ +import java.awt.*; +public class DrawingBoat { + public EntityBoat Boat; + + public DrawingPaddles Paddles; + private int _startPosX; + private int _startPosY; + private Integer _pictureWidth = null; + private Integer _pictureHeight = null; + private final int _BoatWidth = 180; + private final int _BoatHeight = 160; + + public void Init(int speed, float weight, Color bodyColor){ + Boat = new EntityBoat(); + Boat.Init(speed, weight, bodyColor); + Paddles = new DrawingPaddles(); + Paddles.SetCountPaddles((int)(1+Math.random()+Math.random()*2)); + } + + public void SetPosition(int x, int y, int width, int height){ + if(x >= 0 && x+_BoatWidth <= width && y+_BoatHeight <= height){ + _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 + _BoatWidth + Boat.Step < _pictureWidth){ + _startPosX += Boat.Step; + } + break; + case Left: + if(_startPosX - Boat.Step >= 0){ + _startPosX -= Boat.Step; + } + break; + case Up: + if (_startPosY - Boat.Step >= 0) + { + _startPosY -= Boat.Step; + } + break; + + case Down: + if (_startPosY + _BoatHeight + Boat.Step < _pictureHeight) + { + _startPosY += Boat.Step; + } + break; + } + } + + public void DrawTransport(Graphics g){ + if (_startPosX < 0 || _startPosY < 0 || _pictureHeight== null || _pictureWidth== null) + { + return; + } + + Graphics2D g2 = (Graphics2D)g; + + g2.setColor(Color.BLACK); + + g2.drawLine(_startPosX + 10, _startPosY + 60, _startPosX + 110, _startPosY + 60); + g2.drawLine( _startPosX + 110, _startPosY + 60, _startPosX + 180, _startPosY + 90); + g2.drawLine( _startPosX + 180, _startPosY + 90, _startPosX + 110, _startPosY + 120); + g2.drawLine( _startPosX + 110, _startPosY + 120, _startPosX + 10, _startPosY + 120); + g2.drawLine(_startPosX + 10, _startPosY + 120, _startPosX + 10, _startPosY + 60); + g2.drawOval( _startPosX + 15, _startPosY + 65, 95, 50); + + Paddles.DrawPaddles(g2, _startPosX, _startPosY); + + g2.setColor(Color.GRAY); + + g2.fillRect( _startPosX + 2, _startPosY + 65, 10, 50); + g2.fillRect( _startPosX + 15, _startPosY + 53, 90, 10); + g2.fillRect( _startPosX + 15, _startPosY + 118, 90, 10); + + g2.setColor(Color.BLACK); + + g2.drawRect( _startPosX + 60, _startPosY , 4, 90); + g2.drawRect( _startPosX + 60, _startPosY , 4, 90); + g2.drawLine( _startPosX + 25, _startPosY + 20, _startPosX + 100, _startPosY); + g2.drawLine( _startPosX + 100, _startPosY, _startPosX + 100, _startPosY + 50); + g2.drawLine( _startPosX + 25, _startPosY + 70, _startPosX + 100, _startPosY + 50); + g2.drawLine( _startPosX + 25, _startPosY + 70, _startPosX + 25, _startPosY + 20); + } +} diff --git a/src/DrawingField.java b/src/DrawingField.java new file mode 100644 index 0000000..6e40815 --- /dev/null +++ b/src/DrawingField.java @@ -0,0 +1,52 @@ +import javax.swing.*; +import java.awt.*; +import java.util.Random; +public class DrawingField extends JPanel{ + private final FormBoat field; + DrawingBoat _Boat; + public DrawingField(FormBoat field){ + this.field = field; + } + @Override + protected void paintComponent(Graphics g){ + super.paintComponent(g); + Graphics2D g2 = (Graphics2D)g; + if(_Boat != null){ + _Boat.DrawTransport(g2); + } + else return; + } + public void UpButtonAction(){ + if(_Boat != null){ + _Boat.MoveTransport(Direction.Up); + } else return; + } + public void DownButtonAction(){ + if (_Boat!=null){ + _Boat.MoveTransport(Direction.Down); + } + else + return; + } + public void RightButtonAction(){ + if (_Boat!=null){ + _Boat.MoveTransport(Direction.Right); + } + else + return; + } + public void LeftButtonAction(){ + if (_Boat!=null){ + _Boat.MoveTransport(Direction.Left); + } + else + return; + } + + public void CreateButtonAction(){ + Random r = new Random(); + _Boat = new DrawingBoat(); + _Boat.Init(r.nextInt(50)+10,r.nextInt(100)+500,new Color(r.nextInt(256),r.nextInt(256),r.nextInt(256))); + _Boat.SetPosition(r.nextInt(100)+10,r.nextInt(100)+10,getWidth(),getHeight()); + } +} diff --git a/src/DrawingPaddles.java b/src/DrawingPaddles.java new file mode 100644 index 0000000..9787a3c --- /dev/null +++ b/src/DrawingPaddles.java @@ -0,0 +1,43 @@ +import java.awt.*; +public class DrawingPaddles { + private CountPaddles _paddles; + + private int step = 0; + + public void SetCountPaddles(int Count){ + for(CountPaddles temp : CountPaddles.values()) + if(temp.getCountPaddles() == Count){ + _paddles = temp; + return; + } + } + + public void DrawPaddles(Graphics2D g, int _startPosX, int _startPosY){ + step = 0; + switch(_paddles.getCountPaddles()){ + case 1: + drawPaddle(g,_startPosX,_startPosY); + break; + case 2: + for(int i = 0;i<2;i++){ + drawPaddle(g,_startPosX,_startPosY); + step+=25; + } + break; + case 3: + for(int i = 0;i<3;i++){ + drawPaddle(g,_startPosX,_startPosY); + step+=25; + } + break; + } + } + + private void drawPaddle(Graphics2D g, int _startPosX, int _startPosY){ + g.setColor(Color.BLACK); + g.drawLine(_startPosX + 20+step, _startPosY + 60, _startPosX + 35+step, _startPosY + 27); + g.drawOval(_startPosX+32+step, _startPosY+13, 10, 15); + g.drawLine(_startPosX + 20+step, _startPosY + 120, _startPosX + 35+step, _startPosY + 150); + g.drawOval(_startPosX+32+step, _startPosY+150, 10, 15); + } +} diff --git a/src/EntityBoat.java b/src/EntityBoat.java new file mode 100644 index 0000000..653e3f9 --- /dev/null +++ b/src/EntityBoat.java @@ -0,0 +1,17 @@ +import java.awt.*; +import java.util.Random; +public class EntityBoat { + private int Speed; + + private float Weight; + private Color BodyColor; + public float Step; + + public void Init(int speed, float weight, Color bodyColor){ + Random r = new Random(); + Speed = speed <= 0 ? r.nextInt(50)+10 : speed; + Weight = weight <= 0 ? r.nextInt(100)+500:weight; + BodyColor = bodyColor; + Step = Speed * 600 / (int)Weight; + } +} diff --git a/src/FormBoat.java b/src/FormBoat.java new file mode 100644 index 0000000..bdb2ed5 --- /dev/null +++ b/src/FormBoat.java @@ -0,0 +1,123 @@ +import javax.swing.*; +import java.awt.*; + +public class FormBoat extends JFrame{ + private final int Width; + private final int Height; + + JPanel BottomPanel = new JPanel(); + JPanel CreatePanel = new JPanel(); + JPanel BottomAndCreatePanel = new JPanel(); + JPanel DimentionPanel = new JPanel(); + JPanel UpPanel = new JPanel(); + JPanel DownPanel = new JPanel(); + JPanel LRPanel = new JPanel(); + + DrawingField field = new DrawingField(this); + + JButton ButtonCreate = new JButton("Создать"); + + Icon arrowUp = new ImageIcon("Resources/up.png"); + JButton ButtonUp = new JButton(arrowUp); + + Icon arrowLeft = new ImageIcon("Resources/left.png"); + JButton ButtonLeft = new JButton(arrowLeft); + + Icon arrowDown = new ImageIcon("Resources/down.png"); + JButton ButtonDown = new JButton(arrowDown); + + Icon arrowRight = new ImageIcon("Resources/right.png"); + JButton ButtonRight = new JButton(arrowRight); + + public FormBoat(){ + super("Boat"); + setSize(800, 600); + Width = getWidth(); + Height = getHeight(); + ShowWindow(); + RefreshWindow(); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setVisible(true); + } + + private void ShowWindow() { + Dimension dimen = new Dimension(35,35); + + // Обработка нажатия кнопки + ButtonUp.setPreferredSize(dimen); + ButtonUp.addActionListener(e->{ + field.UpButtonAction(); + repaint(); + }); + + // Обработка нажатия кнопки + ButtonDown.setPreferredSize(dimen); + ButtonDown.addActionListener(e->{ + field.DownButtonAction(); + repaint(); + }); + + // Обработка нажатия кнопки + ButtonRight.setPreferredSize(dimen); + ButtonRight.addActionListener(e->{ + field.RightButtonAction(); + repaint(); + }); + + // Обработка нажатия кнопки + ButtonLeft.setPreferredSize(dimen); + ButtonLeft.addActionListener(e->{ + field.LeftButtonAction(); + repaint(); + }); + + // Добавление кнопок на панель (Левая и правая стрелки) + LRPanel.setLayout(new FlowLayout(FlowLayout.CENTER,50,0)); + LRPanel.setBackground(new Color(0,0,0,0)); + LRPanel.add(ButtonLeft); + LRPanel.add(ButtonRight); + + // Добавление кнопки (Стрелка вверх) + UpPanel.setLayout(new FlowLayout()); + UpPanel.setBackground(new Color(0,0,0,0)); + UpPanel.add(ButtonUp); + + // Добавление кнопки (Стрелка вниз) + DownPanel.setLayout(new FlowLayout()); + DownPanel.setBackground(new Color(0,0,0,0)); + DownPanel.add(ButtonDown); + + DimentionPanel.setLayout(new BoxLayout(DimentionPanel,BoxLayout.Y_AXIS)); + DimentionPanel.setBackground(new Color(0,0,0,0)); + DimentionPanel.add(UpPanel); + DimentionPanel.add(LRPanel); + DimentionPanel.add(DownPanel); + add(DimentionPanel); + + //нажатие кнопки + CreatePanel.setLayout(new FlowLayout()); + CreatePanel.setBackground(new Color(0,0,0,0)); + CreatePanel.add(ButtonCreate); + ButtonCreate.addActionListener(e->{ + field.CreateButtonAction(); + repaint(); + }); + + BottomPanel.setLayout(new FlowLayout()); + BottomPanel.setBackground(new Color(0,0,0,0)); + + BottomAndCreatePanel.setLayout(new BoxLayout(BottomAndCreatePanel,BoxLayout.Y_AXIS)); + BottomAndCreatePanel.setBackground(new Color(0,0,0,0)); + BottomAndCreatePanel.add(CreatePanel); + BottomAndCreatePanel.add(BottomPanel); + + add(BottomAndCreatePanel); + add(field); + + } + public void RefreshWindow(){ + field.setBounds(0,0,Width,Height); + BottomAndCreatePanel.setBounds(-320,Height-110,Width,80); + DimentionPanel.setBounds(Width-170,Height-170,190,140); + } +} diff --git a/src/Main.java b/src/Main.java new file mode 100644 index 0000000..506164d --- /dev/null +++ b/src/Main.java @@ -0,0 +1,5 @@ +public class Main { + public static void main(String[] args) { + new FormBoat(); + } +} \ No newline at end of file -- 2.25.1