From 7b57f7dc426578452ceb98f9dfab550558bfc454 Mon Sep 17 00:00:00 2001 From: AnnaLioness Date: Sat, 7 Oct 2023 21:42:37 +0400 Subject: [PATCH 1/3] =?UTF-8?q?=D0=BF=D1=80=D0=BE=D0=BC=D0=B5=D0=B6=D1=83?= =?UTF-8?q?=D1=82=D0=BE=D1=87=D0=BD=D0=B0=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrawingContainerShip.java | 109 +++++++--------------------------- DrawingShip.java | 121 ++++++++++++++++++++++++++++++++++++++ EntityContainerShip.java | 18 ++---- EntityShip.java | 14 +++++ Form1.java | 8 +-- Status.java | 5 ++ 6 files changed, 171 insertions(+), 104 deletions(-) create mode 100644 DrawingShip.java create mode 100644 EntityShip.java create mode 100644 Status.java diff --git a/DrawingContainerShip.java b/DrawingContainerShip.java index 129c290..54d8cc2 100644 --- a/DrawingContainerShip.java +++ b/DrawingContainerShip.java @@ -1,103 +1,36 @@ import java.awt.*; -public class DrawingContainerShip { - public EntityContainerShip EntityContainerShip; - private int _pictureWidth; - private int _pictureHeight; - private int _startPosX; - private int _startPosY; - private int _shipWidth = 110; - private int _shipHeight = 65; +public class DrawingContainerShip extends DrawingShip{ + @Override + public EntityContainerShip EntityContainerShip; private DrawingDecks drawingDecks; - public boolean Init(int speed, double weight, Color bodyColor, Color - additionalColor, boolean crane, boolean container, int deck, int width, int height) + public DrawingContainerShip(int speed, double weight, Color bodyColor, Color +additionalColor, boolean crane, boolean containers,int deck, int width, int height) + { + super(speed, weight, bodyColor,width, height, 110, 65); + if (EntityShip != null) { - _pictureWidth = width; - _pictureHeight = height; - if (_pictureWidth < _shipWidth || _pictureHeight < _shipHeight) - { - return false; - } - EntityContainerShip = new EntityContainerShip(); - EntityContainerShip.Init(speed, weight, bodyColor, additionalColor, - crane, container, deck); - drawingDecks = new DrawingDecks(); - drawingDecks.setNumDecks(deck); - return true; - } - public void SetPosition(int x, int y) - { - _startPosX = Math.min(x, _pictureWidth - _shipWidth); - _startPosY = Math.min(y, _pictureHeight - _shipHeight); - } - public void MoveTransport(Direction direction) - { - if (EntityContainerShip == null) - { - return; - } - switch (direction) - { - case Left: - if (_startPosX - EntityContainerShip.Step > 0) - { - _startPosX -= (int)EntityContainerShip.Step; - } - break; - - case Up: - if (_startPosY - EntityContainerShip.Step > 0) - { - _startPosY -= (int)EntityContainerShip.Step; - } - break; - - case Right: - - if (_startPosX + EntityContainerShip.Step + _shipWidth < _pictureWidth) - { - _startPosX += (int)EntityContainerShip.Step; - } - break; - - case Down: - - if (_startPosY + EntityContainerShip.Step + _shipHeight< _pictureHeight) - { - _startPosY += (int)EntityContainerShip.Step; - } - break; - } + EntityShip = new EntityContainerShip(speed, weight, bodyColor, + additionalColor, crane, containers, deck); } + } + public void DrawShip(Graphics2D g) { - if (EntityContainerShip == null) - { - return; - } - //Pen pen = new Pen(Color.Black); - //Brush adbrush = new SolidBrush(EntityContainerShip.AdditionalColor); - //Brush brBlue = new SolidBrush(Color.Blue); - g.setPaint(Color.BLUE); - // заполнение борта - int x[] = {_startPosX+ 20, _startPosX+40, _startPosX+110, _startPosX+130, _startPosX+ 20}; - int y[] = {_startPosY+65,_startPosY+85, _startPosY+85, _startPosY+65, _startPosY+65}; - g.fillPolygon(x, y, 5); - //борт корабля контур - g.setPaint(Color.BLACK); - int _x[] = {_startPosX+ 20, _startPosX+40, _startPosX+110, _startPosX+130, _startPosX+ 20}; - int _y[] = {_startPosY+65,_startPosY+85, _startPosY+85, _startPosY+65, _startPosY+65}; - g.drawPolyline(_x, _y, 5); - //рисунок на борту - g.drawLine(_startPosX + 43, _startPosY + 80, _startPosX + 47, _startPosY + 80); - g.drawLine(_startPosX + 45, _startPosY + 70, _startPosX + 45, _startPosY + 80); - g.drawLine(_startPosX + 40, _startPosY + 75, _startPosX + 50, _startPosY + 75); + if(EntityShip == null) + { + return; + } + if(!(EntityShip is EntityContainerShip)) { + return; + } //контейнеры if (EntityContainerShip.Conteiners) { - g.setPaint(EntityContainerShip.AdditionalColor); + g.setPaint((EntityShip as EntityContainerShip).AdditionalColor); + super(DrawShip(g)); g.fillRect(_startPosX + 50, _startPosY + 55, 35, 10); g.fillRect(_startPosX + 85, _startPosY + 55, 20, 10); g.fillRect(_startPosX + 105, _startPosY + 50, 15, 15); diff --git a/DrawingShip.java b/DrawingShip.java new file mode 100644 index 0000000..27070e5 --- /dev/null +++ b/DrawingShip.java @@ -0,0 +1,121 @@ +import java.awt.*; +public class DrawingShip { + public EntityShip EntityShip; + private int _pictureWidth; + private int _pictureHeight; + private int _startPosX; + private int _startPosY; + private int _shipWidth = 110; + private int _shipHeight = 65; + private DrawingDecks drawingDecks; + public boolean CanMove(Direction direction) + { + if(EntityShip == null) return false; + switch(direction) + { + case Left: + return _startPosX - EntityShip.Step > 0; + case Right: + return _startPosX + _shipWidth + EntityShip.Step < _pictureWidth; + case Up: + return _startPosY - EntityShip.Step > 0; + case Down: + return _startPosY + _shipHeight+ EntityShip.Step < _pictureHeight; + default: + return false; + } + } + public DrawingShip(int speed, double weight, Color bodyColor, int width, int height) + { + if(width < _shipWidth || height < _shipHeight) + { + return; + } + _pictureWidth = width; + _pictureHeight = height; + EntityShip = new EntityShip(speed, weight, bodyColor); + } + protected DrawingShip(int speed, double weight, Color bodyColor, int width, int height, int shipWidth, int shipHeight) + { + if (width < _shipWidth || height < _shipHeight) + { + return; + } + _pictureWidth = width; + _pictureHeight = height; + _shipHeight = shipHeight; + _shipWidth = shipWidth; + EntityShip = new EntityShip(speed, weight, bodyColor); + } + public void SetPosition(int x, int y) + { + _startPosX = Math.min(x, _pictureWidth - _shipWidth); + _startPosY = Math.min(y, _pictureHeight - _shipHeight); + } + public void MoveTransport(Direction direction) + { + if (EntityShip == null) + { + return; + } + switch (direction) + { + case Left: + if (_startPosX - EntityShip.Step > 0) + { + _startPosX -= (int)EntityShip.Step; + } + break; + + case Up: + if (_startPosY - EntityShip.Step > 0) + { + _startPosY -= (int)EntityShip.Step; + } + break; + + case Right: + + if (_startPosX + EntityShip.Step + _shipWidth < _pictureWidth) + { + _startPosX += (int)EntityShip.Step; + } + break; + + case Down: + + if (_startPosY + EntityShip.Step + _shipHeight< _pictureHeight) + { + _startPosY += (int)EntityShip.Step; + } + break; + } + } + public void DrawShip(Graphics2D g) + { + if (EntityShip == null) + { + return; + } + //Pen pen = new Pen(Color.Black); + //Brush adbrush = new SolidBrush(EntityContainerShip.AdditionalColor); + //Brush brBlue = new SolidBrush(Color.Blue); + g.setPaint(EntityShip.BodyColor); + // заполнение борта + int x[] = {_startPosX+ 20, _startPosX+40, _startPosX+110, _startPosX+130, _startPosX+ 20}; + int y[] = {_startPosY+65,_startPosY+85, _startPosY+85, _startPosY+65, _startPosY+65}; + g.fillPolygon(x, y, 5); + + //борт корабля контур + g.setPaint(Color.BLACK); + int _x[] = {_startPosX+ 20, _startPosX+40, _startPosX+110, _startPosX+130, _startPosX+ 20}; + int _y[] = {_startPosY+65,_startPosY+85, _startPosY+85, _startPosY+65, _startPosY+65}; + g.drawPolyline(_x, _y, 5); + + //рисунок на борту + g.drawLine(_startPosX + 43, _startPosY + 80, _startPosX + 47, _startPosY + 80); + g.drawLine(_startPosX + 45, _startPosY + 70, _startPosX + 45, _startPosY + 80); + g.drawLine(_startPosX + 40, _startPosY + 75, _startPosX + 50, _startPosY + 75); + drawingDecks.DrawDeck(_startPosX, _startPosY, g); + } +} diff --git a/EntityContainerShip.java b/EntityContainerShip.java index fb2d513..dc7f63f 100644 --- a/EntityContainerShip.java +++ b/EntityContainerShip.java @@ -1,25 +1,19 @@ import java.awt.*; -public class EntityContainerShip { - public int Speed; - public double Weight; - public Color BodyColor; +public class EntityContainerShip extends EntityShip{ public Color AdditionalColor; public boolean Crane; public boolean Conteiners; public int Deck; - public double Step; - public void Init(int speed, double weight, Color bodyColor, Color -additionalColor, boolean crane, boolean containers, int deck) - { - Speed = speed; - Weight = weight; - BodyColor = bodyColor; + public EntityContainerShip(int speed, double weight, Color bodyColor, Color +additionalColor, boolean crane, boolean containers, int deck) + { + super(speed, weight,bodyColor); AdditionalColor = additionalColor; Crane = crane; Conteiners = containers; Deck = deck; - Step = (double)Speed * 100 / Weight; + } } diff --git a/EntityShip.java b/EntityShip.java new file mode 100644 index 0000000..f3e3756 --- /dev/null +++ b/EntityShip.java @@ -0,0 +1,14 @@ +import java.awt.*; +public class EntityShip { + public int Speed; + public double Weight; + public Color BodyColor; + public double Step; + public EntityShip(int speed, double weight, Color bodyColor) + { + Speed = speed; + Weight = weight; + BodyColor = bodyColor; + Step = (double)Speed * 100 / Weight; + } +} diff --git a/Form1.java b/Form1.java index 86ee3e8..8e832a9 100644 --- a/Form1.java +++ b/Form1.java @@ -17,25 +17,25 @@ public class Form1 { up.setFocusPainted(false); up.setContentAreaFilled(false); up.setName("up"); - up.setIcon(new ImageIcon("photo11.png")); + up.setIcon(new ImageIcon("D:\\рабочий стол\\рпп\\лаб1.репозит.сложная\\PIbd21.LyovushkinaA.A.Container_ship.Complicated\\photo11.png")); JButton down = new JButton(); down.setBorderPainted(false); down.setFocusPainted(false); down.setContentAreaFilled(false); down.setName("down"); - down.setIcon(new ImageIcon("photo33.png")); + down.setIcon(new ImageIcon("D:\\рабочий стол\\рпп\\лаб1.репозит.сложная\\PIbd21.LyovushkinaA.A.Container_ship.Complicated\\photo33.png")); JButton left = new JButton(); left.setBorderPainted(false); left.setFocusPainted(false); left.setContentAreaFilled(false); left.setName("left"); - left.setIcon(new ImageIcon("photo44.png")); + left.setIcon(new ImageIcon("D:\\рабочий стол\\рпп\\лаб1.репозит.сложная\\PIbd21.LyovushkinaA.A.Container_ship.Complicated\\photo44.png")); JButton right = new JButton(); right.setBorderPainted(false); right.setFocusPainted(false); right.setContentAreaFilled(false); right.setName("right"); - right.setIcon(new ImageIcon("photo22.png")); + right.setIcon(new ImageIcon("D:\\рабочий стол\\рпп\\лаб1.репозит.сложная\\PIbd21.LyovushkinaA.A.Container_ship.Complicated\\photo22.png")); buttonCreate.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e){ diff --git a/Status.java b/Status.java new file mode 100644 index 0000000..138be15 --- /dev/null +++ b/Status.java @@ -0,0 +1,5 @@ +public enum Status { + NotInit, + InProgress, + Finish +} -- 2.25.1 From 91f5aff2a249d1a4cc71faf8cfef89cf7306f2b9 Mon Sep 17 00:00:00 2001 From: AnnaLioness Date: Mon, 9 Oct 2023 23:11:13 +0400 Subject: [PATCH 2/3] =?UTF-8?q?=D0=B3=D0=BE=D1=82=D0=BE=D0=B2=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AbstractStrategy.java | 79 +++++++++++++++++++ DrawingContainerShip.java | 58 ++++++++------ DrawingDecks.java | 6 +- DrawingDecksRect.java | 73 ++++++++++++++++++ DrawingDecksTrapez.java | 73 ++++++++++++++++++ DrawingShip.java | 28 +++++-- DrawningObjectShip.java | 32 ++++++++ EntityContainerShip.java | 5 +- Form1.java => FormContainerShip.java | 110 ++++++++++++++++++++++----- IDecksDrawing.java | 6 ++ IMoveableObject.java | 18 +++++ Main.java | 2 +- MoveToBorder.java | 46 +++++++++++ MoveToCenter.java | 47 ++++++++++++ ObjectParameters.java | 32 ++++++++ 15 files changed, 559 insertions(+), 56 deletions(-) create mode 100644 AbstractStrategy.java create mode 100644 DrawingDecksRect.java create mode 100644 DrawingDecksTrapez.java create mode 100644 DrawningObjectShip.java rename Form1.java => FormContainerShip.java (55%) create mode 100644 IDecksDrawing.java create mode 100644 IMoveableObject.java create mode 100644 MoveToBorder.java create mode 100644 MoveToCenter.java create mode 100644 ObjectParameters.java diff --git a/AbstractStrategy.java b/AbstractStrategy.java new file mode 100644 index 0000000..10d32e4 --- /dev/null +++ b/AbstractStrategy.java @@ -0,0 +1,79 @@ +public abstract class AbstractStrategy { + private IMoveableObject _moveableObject; + + private Status _state = Status.NotInit; + + protected int FieldWidth; + + protected int FieldHeight; + + public Status GetStatus() { return _state; } + + public void SetData(IMoveableObject moveableObject, int width, int + height) + { + if (moveableObject == null) + { + _state = Status.NotInit; + return; + } + _state = Status.InProgress; + _moveableObject = moveableObject; + FieldWidth = width; + FieldHeight = height; + } + + public void MakeStep() + { + if (_state != Status.InProgress) + { + return; + } + if (IsTargetDestinaion()) + { + _state = Status.Finish; + return; + } + MoveToTarget(); + } + + protected boolean MoveLeft() { return MoveTo(Direction.Left);} + + protected boolean MoveRight() { return MoveTo(Direction.Right);} + + protected boolean MoveUp() {return MoveTo(Direction.Up);} + + protected boolean MoveDown(){return MoveTo(Direction.Down);} + + protected ObjectParameters GetObjectParameters(){ + return _moveableObject.GetObjectPosition(); + } + + protected int GetStep() + { + if (_state != Status.InProgress) + { + return 0; + } + return _moveableObject.GetStep(); + } + + protected abstract void MoveToTarget(); + + protected abstract boolean IsTargetDestinaion(); + + private boolean MoveTo(Direction direction) + { + if (_state != Status.InProgress) + { + return false; + } + if (_moveableObject.CheckCanMove(direction)) + { + _moveableObject.MoveObject(direction); + return true; + } + return false; + } + +} diff --git a/DrawingContainerShip.java b/DrawingContainerShip.java index 54d8cc2..b9b9f17 100644 --- a/DrawingContainerShip.java +++ b/DrawingContainerShip.java @@ -1,36 +1,40 @@ import java.awt.*; public class DrawingContainerShip extends DrawingShip{ - @Override - public EntityContainerShip EntityContainerShip; + + + private DrawingDecks drawingDecks; + private DrawingDecksTrapez drawingDecksTrapez; + private DrawingDecksRect drawingDecksRect; public DrawingContainerShip(int speed, double weight, Color bodyColor, Color -additionalColor, boolean crane, boolean containers,int deck, int width, int height) - { - super(speed, weight, bodyColor,width, height, 110, 65); +additionalColor, boolean crane, boolean containers,int deck, int deckType, int width, int height) + { + super(speed, weight, bodyColor,width, height, 110, 65); if (EntityShip != null) { EntityShip = new EntityContainerShip(speed, weight, bodyColor, - additionalColor, crane, containers, deck); + additionalColor, crane, containers,deck, deckType); + drawingDecks = new DrawingDecks(); + drawingDecksTrapez = new DrawingDecksTrapez(); + drawingDecksRect = new DrawingDecksRect(); + drawingDecksRect.setNumDecks(deck); + drawingDecksTrapez.setNumDecks(deck); + drawingDecks.setNumDecks(deck); } } - + @Override public void DrawShip(Graphics2D g) { - - if(EntityShip == null) - { - return; - } - if(!(EntityShip is EntityContainerShip)) { - return; - } - - //контейнеры - if (EntityContainerShip.Conteiners) + super.DrawShip(g); + if (EntityShip == null) { - g.setPaint((EntityShip as EntityContainerShip).AdditionalColor); - super(DrawShip(g)); + return; + } + //контейнеры + if (((EntityContainerShip)EntityShip).Conteiners) + { + g.setPaint(((EntityContainerShip)EntityShip).AdditionalColor); g.fillRect(_startPosX + 50, _startPosY + 55, 35, 10); g.fillRect(_startPosX + 85, _startPosY + 55, 20, 10); g.fillRect(_startPosX + 105, _startPosY + 50, 15, 15); @@ -46,9 +50,9 @@ additionalColor, boolean crane, boolean containers,int deck, int width, int heig g.drawRect(_startPosX + 65, _startPosY + 50, 40, 5); } //кран - if (EntityContainerShip.Crane) + if (((EntityContainerShip)EntityShip).Crane) { - g.setPaint(EntityContainerShip.AdditionalColor); + g.setPaint(((EntityContainerShip)EntityShip).AdditionalColor); g.fillRect(_startPosX + 43, _startPosY+20, 5, 45); g.fillRect(_startPosX + 47, _startPosY + 30, 20, 3); g.setPaint(Color.BLACK); @@ -57,7 +61,15 @@ additionalColor, boolean crane, boolean containers,int deck, int width, int heig g.drawLine(_startPosX + 47, _startPosY+20, _startPosX + 67, _startPosY + 30); g.drawLine(_startPosX + 67, _startPosY + 33, _startPosX + 67, _startPosY + 45); } - drawingDecks.DrawDeck(_startPosX, _startPosY, g); + if(((EntityContainerShip)EntityShip).DeckType == 1){ + drawingDecks.DrawDeck(_startPosX, _startPosY,EntityShip.BodyColor ,g); + } + if(((EntityContainerShip)EntityShip).DeckType == 2){ + drawingDecksTrapez.DrawDeck(_startPosX, _startPosY,EntityShip.BodyColor, g); + } + if(((EntityContainerShip)EntityShip).DeckType == 3){ + drawingDecksRect.DrawDeck(_startPosX, _startPosY, EntityShip.BodyColor, g); + } } } diff --git a/DrawingDecks.java b/DrawingDecks.java index 7d33080..000e1bc 100644 --- a/DrawingDecks.java +++ b/DrawingDecks.java @@ -1,7 +1,7 @@ import java.awt.*; -public class DrawingDecks { - private NumberOfDecks numDecks; +public class DrawingDecks implements IDecksDrawing{ + private NumberOfDecks numDecks; public NumberOfDecks getProperty(){ return numDecks; } @@ -22,7 +22,7 @@ public class DrawingDecks { break; } } - public void DrawDeck(int _startPosX, int _startPosY, Graphics2D g){ + public void DrawDeck(int _startPosX, int _startPosY,Color bodyColor, Graphics2D g){ if(numDecks == NumberOfDecks.Deck_1){ } diff --git a/DrawingDecksRect.java b/DrawingDecksRect.java new file mode 100644 index 0000000..e8f2ed6 --- /dev/null +++ b/DrawingDecksRect.java @@ -0,0 +1,73 @@ +import java.awt.*; +public class DrawingDecksRect implements IDecksDrawing { + private NumberOfDecks numDecks; + public NumberOfDecks getProperty(){ + return numDecks; + } + public void setNumDecks(int nDecks){ + switch(nDecks){ + case 1: + numDecks = NumberOfDecks.Deck_1; + break; + case 2: + numDecks = NumberOfDecks.Deck_2; + break; + case 3: + numDecks = NumberOfDecks.Deck_3; + break; + default: + numDecks = NumberOfDecks.Deck_1; + System.out.println("Что-то пошло не так, количество палуб неверное" + Integer.toString(nDecks) + "сделаем вид, будто она одна"); + break; + } + } + public void DrawDeck(int _startPosX, int _startPosY, Color bodyColor, Graphics2D g){ + if(numDecks == NumberOfDecks.Deck_1){ + g.setPaint(bodyColor); + int x[] = {_startPosX+ 20, _startPosX+20, _startPosX+130, _startPosX+130,_startPosX+ 20}; + int y[] = {_startPosY+65,_startPosY+85, _startPosY+85, _startPosY+65,_startPosY+65}; + g.fillPolygon(x, y, 5); + g.setPaint(Color.BLACK); + int _x[] = {_startPosX+ 20, _startPosX+20, _startPosX+130, _startPosX+130,_startPosX+ 20}; + int _y[] = {_startPosY+65,_startPosY+85, _startPosY+85, _startPosY+65,_startPosY+65}; + g.drawPolyline(_x, _y, 5); + g.drawLine(_startPosX + 43, _startPosY + 80, _startPosX + 47, _startPosY + 80); + g.drawLine(_startPosX + 45, _startPosY + 70, _startPosX + 45, _startPosY + 80); + g.drawLine(_startPosX + 40, _startPosY + 75, _startPosX + 50, _startPosY + 75); + } + if(numDecks == NumberOfDecks.Deck_2){ + g.setPaint(bodyColor); + int x[] = {_startPosX+ 20, _startPosX+20, _startPosX+130, _startPosX+130,_startPosX+ 20}; + int y[] = {_startPosY+65,_startPosY+85, _startPosY+85, _startPosY+65,_startPosY+65}; + g.fillPolygon(x, y, 5); + g.setPaint(Color.BLACK); + int _x[] = {_startPosX+ 20, _startPosX+20, _startPosX+130, _startPosX+130,_startPosX+ 20}; + int _y[] = {_startPosY+65,_startPosY+85, _startPosY+85, _startPosY+65,_startPosY+65}; + g.drawPolyline(_x, _y, 5); + g.drawLine(_startPosX + 43, _startPosY + 80, _startPosX + 47, _startPosY + 80); + g.drawLine(_startPosX + 45, _startPosY + 70, _startPosX + 45, _startPosY + 80); + g.drawLine(_startPosX + 40, _startPosY + 75, _startPosX + 50, _startPosY + 75); + + g.drawLine(_startPosX + 20, _startPosY + 75, _startPosX+ 35, _startPosY + 75); + g.drawLine(_startPosX + 55, _startPosY + 75, _startPosX+ 130, _startPosY + 75); + } + if(numDecks == NumberOfDecks.Deck_3){ + g.setPaint(bodyColor); + int x[] = {_startPosX+ 20, _startPosX+20, _startPosX+130, _startPosX+130,_startPosX+ 20}; + int y[] = {_startPosY+65,_startPosY+85, _startPosY+85, _startPosY+65,_startPosY+65}; + g.fillPolygon(x, y, 5); + g.setPaint(Color.BLACK); + int _x[] = {_startPosX+ 20, _startPosX+20, _startPosX+130, _startPosX+130,_startPosX+ 20}; + int _y[] = {_startPosY+65,_startPosY+85, _startPosY+85, _startPosY+65,_startPosY+65}; + g.drawPolyline(_x, _y, 5); + g.drawLine(_startPosX + 43, _startPosY + 80, _startPosX + 47, _startPosY + 80); + g.drawLine(_startPosX + 45, _startPosY + 70, _startPosX + 45, _startPosY + 80); + g.drawLine(_startPosX + 40, _startPosY + 75, _startPosX + 50, _startPosY + 75); + + g.drawLine(_startPosX + 20, _startPosY + 75, _startPosX+ 35, _startPosY + 75); + g.drawLine(_startPosX + 55, _startPosY + 75, _startPosX+ 130, _startPosY + 75); + g.drawLine(_startPosX + 20, _startPosY + 70, _startPosX+ 40, _startPosY + 70); + g.drawLine(_startPosX + 50, _startPosY + 70, _startPosX+ 130, _startPosY + 70); + } + } +} diff --git a/DrawingDecksTrapez.java b/DrawingDecksTrapez.java new file mode 100644 index 0000000..eaa871d --- /dev/null +++ b/DrawingDecksTrapez.java @@ -0,0 +1,73 @@ +import java.awt.*; +public class DrawingDecksTrapez implements IDecksDrawing{ + private NumberOfDecks numDecks; + public NumberOfDecks getProperty(){ + return numDecks; + } + public void setNumDecks(int nDecks){ + switch(nDecks){ + case 1: + numDecks = NumberOfDecks.Deck_1; + break; + case 2: + numDecks = NumberOfDecks.Deck_2; + break; + case 3: + numDecks = NumberOfDecks.Deck_3; + break; + default: + numDecks = NumberOfDecks.Deck_1; + System.out.println("Что-то пошло не так, количество палуб неверное" + Integer.toString(nDecks) + "сделаем вид, будто она одна"); + break; + } + } + public void DrawDeck(int _startPosX, int _startPosY, Color bodyColor, Graphics2D g){ + if(numDecks == NumberOfDecks.Deck_1){ + g.setPaint(bodyColor); + int x[] = {_startPosX+ 20, _startPosX+20, _startPosX+25, _startPosX+40, _startPosX+ 110,_startPosX+125, _startPosX+130, _startPosX+130, _startPosX+ 20}; + int y[] = {_startPosY+65,_startPosY+70, _startPosY+70, _startPosY+85, _startPosY+85,_startPosY+70, _startPosY+70, _startPosY+65, _startPosY+65}; + g.fillPolygon(x, y, 9); + g.setPaint(Color.BLACK); + int _x[] = {_startPosX+ 20, _startPosX+20, _startPosX+25, _startPosX+40, _startPosX+ 110,_startPosX+125, _startPosX+130, _startPosX+130, _startPosX+ 20}; + int _y[] = {_startPosY+65,_startPosY+70, _startPosY+70, _startPosY+85, _startPosY+85,_startPosY+70, _startPosY+70, _startPosY+65, _startPosY+65}; + g.drawPolyline(_x, _y, 9); + g.drawLine(_startPosX + 43, _startPosY + 80, _startPosX + 47, _startPosY + 80); + g.drawLine(_startPosX + 45, _startPosY + 70, _startPosX + 45, _startPosY + 80); + g.drawLine(_startPosX + 40, _startPosY + 75, _startPosX + 50, _startPosY + 75); + } + if(numDecks == NumberOfDecks.Deck_2){ + g.setPaint(bodyColor); + int x[] = {_startPosX+ 20, _startPosX+20, _startPosX+25,_startPosX+ 25, _startPosX+30, _startPosX+40, _startPosX+ 110, _startPosX+120, _startPosX+ 125,_startPosX+125, _startPosX+130, _startPosX+130, _startPosX+ 20}; + int y[] = {_startPosY+65,_startPosY+70, _startPosY+70,_startPosY+75, _startPosY+75, _startPosY+85, _startPosY+85,_startPosY+75, _startPosY+75,_startPosY+70, _startPosY+70, _startPosY+65, _startPosY+65}; + g.fillPolygon(x, y, 13); + g.setPaint(Color.BLACK); + int _x[] = {_startPosX+ 20, _startPosX+20, _startPosX+25,_startPosX+ 25, _startPosX+30, _startPosX+40, _startPosX+ 110, _startPosX+120, _startPosX+ 125,_startPosX+125, _startPosX+130, _startPosX+130, _startPosX+ 20}; + int _y[] = {_startPosY+65,_startPosY+70, _startPosY+70,_startPosY+75, _startPosY+75, _startPosY+85, _startPosY+85,_startPosY+75, _startPosY+75,_startPosY+70, _startPosY+70, _startPosY+65, _startPosY+65}; + g.drawPolyline(_x, _y, 13); + g.drawLine(_startPosX + 43, _startPosY + 80, _startPosX + 47, _startPosY + 80); + g.drawLine(_startPosX + 45, _startPosY + 70, _startPosX + 45, _startPosY + 80); + g.drawLine(_startPosX + 40, _startPosY + 75, _startPosX + 50, _startPosY + 75); + + g.drawLine(_startPosX + 120, _startPosY + 75, _startPosX+ 55, _startPosY + 75); + g.drawLine(_startPosX + 30, _startPosY + 75, _startPosX+ 35, _startPosY + 75); + } + if(numDecks == NumberOfDecks.Deck_3){ + g.setPaint(bodyColor); + int x[] = {_startPosX+ 20, _startPosX+20, _startPosX+25,_startPosX+ 25, _startPosX+30, _startPosX+30, _startPosX+ 120, _startPosX+120, _startPosX+ 125,_startPosX+125, _startPosX+130, _startPosX+130, _startPosX+ 20}; + int y[] = {_startPosY+65,_startPosY+70, _startPosY+70,_startPosY+75, _startPosY+75, _startPosY+85, _startPosY+85,_startPosY+75, _startPosY+75,_startPosY+70, _startPosY+70, _startPosY+65, _startPosY+65}; + g.fillPolygon(x, y, 13); + g.setPaint(Color.BLACK); + int _x[] = {_startPosX+ 20, _startPosX+20, _startPosX+25,_startPosX+ 25, _startPosX+30, _startPosX+30, _startPosX+ 120, _startPosX+120, _startPosX+ 125,_startPosX+125, _startPosX+130, _startPosX+130, _startPosX+ 20}; + int _y[] = {_startPosY+65,_startPosY+70, _startPosY+70,_startPosY+75, _startPosY+75, _startPosY+85, _startPosY+85,_startPosY+75, _startPosY+75,_startPosY+70, _startPosY+70, _startPosY+65, _startPosY+65}; + g.drawPolyline(_x, _y, 13); + g.drawLine(_startPosX + 43, _startPosY + 80, _startPosX + 47, _startPosY + 80); + g.drawLine(_startPosX + 45, _startPosY + 70, _startPosX + 45, _startPosY + 80); + g.drawLine(_startPosX + 40, _startPosY + 75, _startPosX + 50, _startPosY + 75); + + g.drawLine(_startPosX + 120, _startPosY + 75, _startPosX+ 55, _startPosY + 75); + g.drawLine(_startPosX + 125, _startPosY + 70, _startPosX+ 50, _startPosY + 70); + g.drawLine(_startPosX + 30, _startPosY + 75, _startPosX+ 35, _startPosY + 75); + g.drawLine(_startPosX + 25, _startPosY + 70, _startPosX+ 40, _startPosY + 70); + } + } +} diff --git a/DrawingShip.java b/DrawingShip.java index 27070e5..fb396f6 100644 --- a/DrawingShip.java +++ b/DrawingShip.java @@ -1,13 +1,25 @@ import java.awt.*; public class DrawingShip { public EntityShip EntityShip; - private int _pictureWidth; - private int _pictureHeight; - private int _startPosX; - private int _startPosY; - private int _shipWidth = 110; - private int _shipHeight = 65; - private DrawingDecks drawingDecks; + public int _pictureWidth; + public int _pictureHeight; + public int _startPosX; + public int _startPosY; + public int _shipWidth = 110; + public int _shipHeight = 65; + public int GetPosX(){ + return _startPosX; + } + public int GetPosY(){ + return _startPosY; + } + public int GetWidth(){ + return _shipWidth; + } + public int GetHeight(){ + return _shipHeight; + } + public boolean CanMove(Direction direction) { if(EntityShip == null) return false; @@ -116,6 +128,6 @@ public class DrawingShip { g.drawLine(_startPosX + 43, _startPosY + 80, _startPosX + 47, _startPosY + 80); g.drawLine(_startPosX + 45, _startPosY + 70, _startPosX + 45, _startPosY + 80); g.drawLine(_startPosX + 40, _startPosY + 75, _startPosX + 50, _startPosY + 75); - drawingDecks.DrawDeck(_startPosX, _startPosY, g); + } } diff --git a/DrawningObjectShip.java b/DrawningObjectShip.java new file mode 100644 index 0000000..f2a5126 --- /dev/null +++ b/DrawningObjectShip.java @@ -0,0 +1,32 @@ +public class DrawningObjectShip implements IMoveableObject{ + private DrawingShip _drawingShip = null; + public DrawningObjectShip(DrawingShip drawingShip) + { + _drawingShip = drawingShip; + } + public ObjectParameters GetObjectPosition() + { + + if (_drawingShip == null || _drawingShip.EntityShip == + null) + { + return null; + } + return new ObjectParameters(_drawingShip.GetPosX(), + _drawingShip.GetPosY(), _drawingShip.GetWidth(), _drawingShip.GetHeight()); + + } + + public int GetStep() + { + return (int)(_drawingShip.EntityShip.Step); + } + public boolean CheckCanMove(Direction direction) + { + return _drawingShip.CanMove(direction); + } + public void MoveObject(Direction direction) + { + _drawingShip.MoveTransport(direction); + } +} diff --git a/EntityContainerShip.java b/EntityContainerShip.java index dc7f63f..c54209b 100644 --- a/EntityContainerShip.java +++ b/EntityContainerShip.java @@ -6,14 +6,15 @@ public class EntityContainerShip extends EntityShip{ public boolean Crane; public boolean Conteiners; public int Deck; + public int DeckType; public EntityContainerShip(int speed, double weight, Color bodyColor, Color -additionalColor, boolean crane, boolean containers, int deck) +additionalColor, boolean crane, boolean containers, int deck, int deckType) { super(speed, weight,bodyColor); AdditionalColor = additionalColor; Crane = crane; Conteiners = containers; Deck = deck; - + DeckType = deckType; } } diff --git a/Form1.java b/FormContainerShip.java similarity index 55% rename from Form1.java rename to FormContainerShip.java index 8e832a9..0f0aa47 100644 --- a/Form1.java +++ b/FormContainerShip.java @@ -2,16 +2,24 @@ import java.awt.*; import java.util.*; import javax.swing.*; import java.awt.event.*; -public class Form1 { - private DrawingContainerShip _drawingShip; +public class FormContainerShip { + private DrawingShip _drawingShip; + private AbstractStrategy _abstractStrategy; Canvas canv; public void Draw(){ canv.repaint(); } - public Form1(){ + public FormContainerShip(){ JFrame w=new JFrame ("ContainerShip"); - JButton buttonCreate = new JButton("создать"); + JButton buttonCreateShip = new JButton("создать кораблик"); + JButton buttonCreateContainerShip = new JButton("создать контейнеровоз"); + JButton buttonStep = new JButton("шаг"); + String[] items = { + "MoveToCenter", "MoveToRight-Down" + }; + JComboBox comboBoxStrategy = new JComboBox(items); + comboBoxStrategy.setEditable(false); JButton up = new JButton(); up.setBorderPainted(false); up.setFocusPainted(false); @@ -36,28 +44,83 @@ public class Form1 { right.setContentAreaFilled(false); right.setName("right"); right.setIcon(new ImageIcon("D:\\рабочий стол\\рпп\\лаб1.репозит.сложная\\PIbd21.LyovushkinaA.A.Container_ship.Complicated\\photo22.png")); - buttonCreate.addActionListener( + buttonCreateShip.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e){ System.out.println(e.getActionCommand()); Random random = new Random(); - _drawingShip = new DrawingContainerShip(); - /*_drawingShip.Init(random.nextInt(100, 300), + _drawingShip = new DrawingShip(random.nextInt(100, 300), random.nextInt(1000, 3000), - new Color(random.nextInt(0, 256), random.nextInt(0, 256),random.nextInt(0, 256)), - new Color(random.nextInt(0, 256), random.nextInt(0, 256),random.nextInt(0, 256)), - random.nextBoolean(0, 2),random.nextBoolean(0, 2),1000, 560);*/ - _drawingShip.Init(random.nextInt(100,300), - random.nextInt(1000,3000), - new Color(random.nextInt(0,256), random.nextInt(0,256), random.nextInt(0,256)), - new Color(random.nextInt(0,256), random.nextInt(0,256), random.nextInt(0,256)), - random.nextBoolean(), random.nextBoolean(), random.nextInt(1,4), 960, 560); + new Color(random.nextInt(0,256), random.nextInt(0,256), random.nextInt(0,256)),960, 560); + _drawingShip.SetPosition(random.nextInt(10, 100),random.nextInt(10, 100)); canv._drawingShip = _drawingShip; Draw(); } } ); + buttonCreateContainerShip.addActionListener( + new ActionListener() { + public void actionPerformed(ActionEvent e){ + System.out.println(e.getActionCommand()); + Random random = new Random(); + _drawingShip = new DrawingContainerShip(random.nextInt(100, 300), + random.nextInt(1000, 3000), + new Color(random.nextInt(0,256), random.nextInt(0,256), random.nextInt(0,256)), + new Color(random.nextInt(0,256), random.nextInt(0,256),random.nextInt(0,256)), + random.nextBoolean(), random.nextBoolean(), random.nextInt(1,4),random.nextInt(1,4),960, 560); + + _drawingShip.SetPosition(random.nextInt(10, 100),random.nextInt(10, 100)); + canv._drawingShip = _drawingShip; + Draw(); + } + } + ); + buttonStep.addActionListener( + new ActionListener(){ + public void actionPerformed(ActionEvent e){ + if (_drawingShip == null) + { + return; + } + if (comboBoxStrategy.isEnabled()) + { + + switch (comboBoxStrategy.getSelectedIndex()) + { + case 0: + _abstractStrategy = new MoveToCenter(); + break; + case 1: + _abstractStrategy = new MoveToBorder(); + break; + default: + _abstractStrategy = null; + break; + } + if (_abstractStrategy == null) + { + return; + } + _abstractStrategy.SetData(new + DrawningObjectShip(_drawingShip), 960, + 560); + comboBoxStrategy.setEnabled(false); + } + if (_abstractStrategy == null) + { + return; + } + _abstractStrategy.MakeStep(); + Draw(); + if (_abstractStrategy.GetStatus() == Status.Finish) + { + comboBoxStrategy.setEnabled(true); + _abstractStrategy = null; + } + } + } + ); ActionListener actioListener = new ActionListener() { public void actionPerformed(ActionEvent e){ System.out.println(((JButton)(e.getSource())).getName()); @@ -86,18 +149,25 @@ public class Form1 { down.addActionListener(actioListener); left.addActionListener(actioListener); right.addActionListener(actioListener); + w.setSize (1000, 600); w.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); w.setLayout(null); canv = new Canvas(); canv.setBounds(0, 0, 1000, 600); - buttonCreate.setBounds(2, 540, 100, 20); + buttonCreateShip.setBounds(2, 540, 200, 20); + buttonCreateContainerShip.setBounds(215, 540, 200, 20); + comboBoxStrategy.setBounds(800, 10, 150, 30); + buttonStep.setBounds(800, 45, 100, 20); up.setBounds(900, 480, 45, 45); down.setBounds(900, 520, 45, 45); left.setBounds(860, 520, 45, 45); right.setBounds(940, 520, 45, 45); w.add(canv); - w.add(buttonCreate); + w.add(buttonCreateShip); + w.add(buttonCreateContainerShip); + w.add(comboBoxStrategy); + w.add(buttonStep); w.add(up); w.add(down); w.add(left); @@ -106,13 +176,15 @@ public class Form1 { } } class Canvas extends JComponent{ - public DrawingContainerShip _drawingShip; - public Canvas(){ + public DrawingShip _drawingShip; + + public Canvas(){ } public void paintComponent (Graphics g){ if (_drawingShip == null){ return; } + super.paintComponents (g) ; Graphics2D g2d = (Graphics2D)g; _drawingShip.DrawShip(g2d); diff --git a/IDecksDrawing.java b/IDecksDrawing.java new file mode 100644 index 0000000..2d11162 --- /dev/null +++ b/IDecksDrawing.java @@ -0,0 +1,6 @@ +import java.awt.*; +public interface IDecksDrawing { + public NumberOfDecks getProperty(); + public void setNumDecks(int nDecks); + public void DrawDeck(int _startPosX, int _startPosY,Color bodyColor, Graphics2D g); +} diff --git a/IMoveableObject.java b/IMoveableObject.java new file mode 100644 index 0000000..f1d76bf --- /dev/null +++ b/IMoveableObject.java @@ -0,0 +1,18 @@ +public interface IMoveableObject { + ObjectParameters GetObjectPosition(); + /// + /// Шаг объекта + /// + int GetStep(); + /// + /// Проверка, можно ли переместиться по нужному направлению + /// + /// + /// + boolean CheckCanMove(Direction direction); + /// + /// Изменение направления пермещения объекта + /// + /// Направление + void MoveObject(Direction direction); +} diff --git a/Main.java b/Main.java index 6402b78..8414fdc 100644 --- a/Main.java +++ b/Main.java @@ -1,5 +1,5 @@ public class Main { public static void main(String[] args){ - Form1 form1 = new Form1(); + FormContainerShip formContainerShip = new FormContainerShip(); } } diff --git a/MoveToBorder.java b/MoveToBorder.java new file mode 100644 index 0000000..3afb6e4 --- /dev/null +++ b/MoveToBorder.java @@ -0,0 +1,46 @@ +public class MoveToBorder extends AbstractStrategy{ + protected boolean IsTargetDestinaion() + { + var objParams = GetObjectParameters(); + if (objParams == null) + { + return false; + } + return objParams.RightBorder <= FieldWidth && + objParams.RightBorder + GetStep() >= FieldWidth && + objParams.DownBorder <= FieldHeight && + objParams.DownBorder + GetStep() >= FieldHeight; + } + protected void MoveToTarget() + { + var objParams = GetObjectParameters(); + if (objParams == null) + { + return; + } + var diffX = objParams.RightBorder - FieldWidth; + if (Math.abs(diffX) > GetStep()) + { + if (diffX > 0) + { + MoveLeft(); + } + else + { + MoveRight(); + } + } + var diffY = objParams.DownBorder - FieldHeight; + if (Math.abs(diffY) > GetStep()) + { + if (diffY > 0) + { + MoveUp(); + } + else + { + MoveDown(); + } + } + } +} diff --git a/MoveToCenter.java b/MoveToCenter.java new file mode 100644 index 0000000..844b8d6 --- /dev/null +++ b/MoveToCenter.java @@ -0,0 +1,47 @@ + +public class MoveToCenter extends AbstractStrategy{ + protected boolean IsTargetDestinaion() + { + var objParams = GetObjectParameters(); + if (objParams == null) + { + return false; + } + return objParams.ObjectMiddleHorizontal <= FieldWidth / 2 && + objParams.ObjectMiddleHorizontal + GetStep() >= FieldWidth / 2 && + objParams.ObjectMiddleVertical <= FieldHeight / 2 && + objParams.ObjectMiddleVertical + GetStep() >= FieldHeight / 2; + } + protected void MoveToTarget() + { + var objParams = GetObjectParameters(); + if (objParams == null) + { + return; + } + var diffX = objParams.ObjectMiddleHorizontal - FieldWidth / 2; + if (Math.abs(diffX) > GetStep()) + { + if (diffX > 0) + { + MoveLeft(); + } + else + { + MoveRight(); + } + } + var diffY = objParams.ObjectMiddleVertical - FieldHeight / 2; + if (Math.abs(diffY) > GetStep()) + { + if (diffY > 0) + { + MoveUp(); + } + else + { + MoveDown(); + } + } + } +} diff --git a/ObjectParameters.java b/ObjectParameters.java new file mode 100644 index 0000000..0d29535 --- /dev/null +++ b/ObjectParameters.java @@ -0,0 +1,32 @@ +public class ObjectParameters { + private int _x; + private int _y; + private int _width; + private int _height; + + public int LeftBorder; + + public int TopBorder; + + public int RightBorder; + + public int DownBorder; + + public int ObjectMiddleHorizontal; + + public int ObjectMiddleVertical; + + public ObjectParameters(int x, int y, int width, int height) + { + _x = x; + _y = y; + _width = width; + _height = height; + LeftBorder = _x; + TopBorder = _y; + RightBorder = _x + _width; + DownBorder = _y + _height; + ObjectMiddleHorizontal = _x + _width / 2; + ObjectMiddleVertical = _y + _height / 2; + } +} -- 2.25.1 From 2f936be74ab7d78bd464a25f6b3117c2be8de554 Mon Sep 17 00:00:00 2001 From: AnnaLioness Date: Fri, 20 Oct 2023 18:49:21 +0400 Subject: [PATCH 3/3] =?UTF-8?q?=D0=BA=D0=BE=D0=BC=D0=BC=D0=B8=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrawingContainerShip.java | 34 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/DrawingContainerShip.java b/DrawingContainerShip.java index b9b9f17..dd20df4 100644 --- a/DrawingContainerShip.java +++ b/DrawingContainerShip.java @@ -4,9 +4,9 @@ public class DrawingContainerShip extends DrawingShip{ - private DrawingDecks drawingDecks; - private DrawingDecksTrapez drawingDecksTrapez; - private DrawingDecksRect drawingDecksRect; + + private IDecksDrawing iDecksDrawing; + public DrawingContainerShip(int speed, double weight, Color bodyColor, Color additionalColor, boolean crane, boolean containers,int deck, int deckType, int width, int height) { @@ -15,12 +15,18 @@ additionalColor, boolean crane, boolean containers,int deck, int deckType, int w { EntityShip = new EntityContainerShip(speed, weight, bodyColor, additionalColor, crane, containers,deck, deckType); - drawingDecks = new DrawingDecks(); - drawingDecksTrapez = new DrawingDecksTrapez(); - drawingDecksRect = new DrawingDecksRect(); - drawingDecksRect.setNumDecks(deck); - drawingDecksTrapez.setNumDecks(deck); - drawingDecks.setNumDecks(deck); + + + if(deckType == 1){ + iDecksDrawing = new DrawingDecks(); + } + if(deckType == 2){ + iDecksDrawing = new DrawingDecksTrapez(); + } + if(deckType == 3){ + iDecksDrawing = new DrawingDecksRect(); + } + iDecksDrawing.setNumDecks(deck); } } @Override @@ -61,15 +67,7 @@ additionalColor, boolean crane, boolean containers,int deck, int deckType, int w g.drawLine(_startPosX + 47, _startPosY+20, _startPosX + 67, _startPosY + 30); g.drawLine(_startPosX + 67, _startPosY + 33, _startPosX + 67, _startPosY + 45); } - if(((EntityContainerShip)EntityShip).DeckType == 1){ - drawingDecks.DrawDeck(_startPosX, _startPosY,EntityShip.BodyColor ,g); - } - if(((EntityContainerShip)EntityShip).DeckType == 2){ - drawingDecksTrapez.DrawDeck(_startPosX, _startPosY,EntityShip.BodyColor, g); - } - if(((EntityContainerShip)EntityShip).DeckType == 3){ - drawingDecksRect.DrawDeck(_startPosX, _startPosY, EntityShip.BodyColor, g); - } + iDecksDrawing.DrawDeck(_startPosX, _startPosY, EntityShip.BodyColor, g); } } -- 2.25.1