From 0a03f8b6d0e79784ad4ed991b478005b54d1a87b Mon Sep 17 00:00:00 2001 From: gg12 darfren Date: Mon, 16 Oct 2023 16:08:28 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B5=D0=B4=D0=B5=D0=BB?= =?UTF-8?q?=D0=B0=D0=BB=20=D0=B1=D0=B0=D0=B7=D0=BE=D0=B2=D1=83=D1=8E=20?= =?UTF-8?q?=D0=BD=D0=B0=20Java?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DrawningMonorail.java | 137 +++++++++--------- .../{ => DrawningObjects}/DrawningWheels.java | 7 +- .../{ => Entities}/EntityMonorail.java | 17 +-- src/MonorailHard/Main.java | 96 ++++++++++-- .../MovementStrategy/AbstractStrategy.java | 19 ++- 5 files changed, 178 insertions(+), 98 deletions(-) rename src/MonorailHard/{ => DrawningObjects}/DrawningMonorail.java (72%) rename src/MonorailHard/{ => DrawningObjects}/DrawningWheels.java (91%) rename src/MonorailHard/{ => Entities}/EntityMonorail.java (54%) diff --git a/src/MonorailHard/DrawningMonorail.java b/src/MonorailHard/DrawningObjects/DrawningMonorail.java similarity index 72% rename from src/MonorailHard/DrawningMonorail.java rename to src/MonorailHard/DrawningObjects/DrawningMonorail.java index 03f4a90..d80d91f 100644 --- a/src/MonorailHard/DrawningMonorail.java +++ b/src/MonorailHard/DrawningObjects/DrawningMonorail.java @@ -1,32 +1,34 @@ -package MonorailHard; +package MonorailHard.DrawningObjects; + +import MonorailHard.DirectionType; +import MonorailHard.Entities.EntityMonorail; +import MonorailHard.MovementStrategy.IMoveableObject; +import com.sun.source.tree.ImportTree; import javax.swing.*; -import javax.swing.text.html.parser.Entity; import java.awt.*; import java.util.Random; public class DrawningMonorail { - JPanel MonorailPanel; - private EntityMonorail EntityMonorail; + protected JPanel MonorailPanel; + protected EntityMonorail EntityMonorail; private int _pictureWidth; private int _pictureHeight; - private int _startPosX = 0; - private int _startPosY = 0; - private int _monorailWidth = 133; - private int _monorailHeight = 50; - private DrawningWheels DrawningWheels; + protected int _startPosX; + protected int _startPosY; + protected int _monorailWidth = 133; + protected int _monorailHeight = 50; + public DrawningWheels DrawningWheels; protected int wheelSz; - - public EntityMonorail EntityMonorail(){ return EntityMonorail; } - public boolean Init(int speed, double weight, Color bodyColor, Color wheelColor, Color tireColor, int wheelNumb, - int width, int height, boolean secondCabine, boolean magniteRail, JPanel monorailPanel){ + public DrawningMonorail(int speed, double weight, Color bodyColor, Color wheelColor, Color tireColor, + int width, int height, JPanel monorailPanel){ if(width <= _monorailWidth || height <= _monorailHeight) - return false; + return; _startPosY=0; _startPosX = 0; monorailPanel.setSize(width, height); @@ -35,14 +37,31 @@ public class DrawningMonorail { wheelSz = _monorailHeight - _monorailHeight * 7 / 10; _pictureWidth = width; _pictureHeight = height; - EntityMonorail = new EntityMonorail(); - EntityMonorail.Init(speed, weight, bodyColor, wheelColor, tireColor, wheelNumb, secondCabine, magniteRail); + EntityMonorail = new EntityMonorail(speed, weight, bodyColor, wheelColor, tireColor); int dif = _monorailWidth / 10; - DrawningWheels = new DrawningWheels(); - DrawningWheels.Init(_monorailWidth - dif, _monorailHeight,_startPosX,_startPosY,wheelColor,tireColor,monorailPanel); + DrawningWheels = new DrawningWheels(_monorailWidth - dif, _monorailHeight,_startPosX,_startPosY,wheelColor,tireColor,monorailPanel); Random rand = new Random(); DrawningWheels.ChangeWheelsNumb(rand.nextInt(1, 6)); - return true; + } + + protected DrawningMonorail(int speed, double weight, Color bodyColor, Color wheelColor, Color tireColor, + int width, int height, int monorailWidth, int monorailHeight, JPanel monorailPanel){ + if(width <= _monorailWidth || height <= _monorailHeight) + return; + _startPosY=0; + _startPosX = 0; + monorailPanel.setSize(width, height); + MonorailPanel = monorailPanel; + monorailPanel.paint(MonorailPanel.getGraphics()); + wheelSz = _monorailHeight - _monorailHeight * 7 / 10; + _pictureWidth = width; + _pictureHeight = height; + _monorailWidth = monorailWidth; + _monorailHeight = monorailHeight; + EntityMonorail = new EntityMonorail(speed, weight, bodyColor, wheelColor, tireColor); + int dif = _monorailWidth / 10; + DrawningWheels = new DrawningWheels(_monorailWidth - dif, _monorailHeight,_startPosX,_startPosY,wheelColor,tireColor,monorailPanel); + DrawningWheels.ChangeWheelsNumb(2); } public void SetPosition(int x, int y){ @@ -50,14 +69,43 @@ public class DrawningMonorail { return; _startPosX = x; _startPosY = y; - if(x + _monorailWidth <= _pictureWidth|| y + _monorailHeight <= _pictureHeight){ + if(x + _monorailWidth >= _pictureWidth|| y + _monorailHeight >= _pictureHeight || x < 0 || y < 0){ _startPosX = 0; _startPosY = 0; } } - public void MoveTransport(DirectionType direction){ + public int GetPosX(){return _startPosX;} + public int GetPosY(){return _startPosY;} + public int GetWidth(){return _monorailWidth;} + public int GetHeight(){return _monorailHeight;} + public boolean CanMove(DirectionType direction) + { if (EntityMonorail == null) + return false; + boolean can = false; + switch (direction) + { + case Left: + can = _startPosX - EntityMonorail.Step() >= 0; + break; + case Right: + can = _startPosX + EntityMonorail.Step() < _pictureWidth; + break; + case Down: + can = _startPosX + EntityMonorail.Step() < _pictureHeight; + break; + case Up: + can = _startPosX - EntityMonorail.Step() >= 0; + break; + }; + return can; + } + + + + public void MoveTransport(DirectionType direction){ + if (!CanMove(direction) || EntityMonorail == null) return; MonorailPanel.paint(MonorailPanel.getGraphics()); switch (direction) @@ -65,26 +113,18 @@ public class DrawningMonorail { case Left: if (_startPosX - EntityMonorail.Step() >= 0) _startPosX -= (int)EntityMonorail.Step(); - else - _startPosX = 0; break; case Up: if (_startPosY - EntityMonorail.Step() >= 0) _startPosY -= (int)EntityMonorail.Step(); - else - _startPosY = 0; break; case Right: if (_startPosX + EntityMonorail.Step() + _monorailWidth < _pictureWidth) _startPosX += (int)EntityMonorail.Step(); - else - _startPosX = _pictureWidth - _monorailWidth; break; case Down: if (_startPosY + EntityMonorail.Step() + _monorailHeight < _pictureHeight) _startPosY += (int)EntityMonorail.Step(); - else - _startPosY = _pictureHeight - _monorailHeight; break; } DrawningWheels.CurX = _startPosX; @@ -193,43 +233,8 @@ public class DrawningMonorail { g2d.fillRect(rightRect.x, rightRect.y, rightRect.width, rightRect.height); g2d.setColor(Color.BLUE); g2d.drawRect(rightRect.x, rightRect.y, rightRect.width, rightRect.height); - - //вторая кабина - if (EntityMonorail.SecondCabine()) - { - int[] pointsSecondCabineX = { _startPosX + _monorailWidth / 20 * 19, - _startPosX + _monorailWidth + dif, - _startPosX + _monorailWidth + dif, - _startPosX + _monorailWidth / 20 * 19}; - int[] pointsSecondCabineY = { _startPosY + _monorailHeight / 10, - _startPosY + _monorailHeight / 5 * 2, - _startPosY + _monorailHeight / 10 * 7, - _startPosY + _monorailHeight / 10 * 7}; - g2d.setColor(EntityMonorail.BodyColor()); - g2d.fillPolygon(pointsSecondCabineX, pointsSecondCabineY, pointsSecondCabineX.length); - g2d.setColor(Color.BLACK); - g2d.drawPolygon(pointsSecondCabineX, pointsSecondCabineY, pointsSecondCabineX.length); - Rectangle Rect = new Rectangle(); - Rect.x = _startPosX + _monorailWidth / 20 * 19; - Rect.y = _startPosY + _monorailHeight / 25 * 4 + _monorailHeight / 50 * 3; - Rect.width = _monorailWidth / 120 * 6; - Rect.height = _monorailHeight / 50 * 7; - g2d.setColor(Color.WHITE); - g2d.fillRect(Rect.x, Rect.y, Rect.width, Rect.height); - g2d.setColor(Color.BLUE); - g2d.drawRect(Rect.x, Rect.y, Rect.width, Rect.height); - } +//колеса DrawningWheels.DrawWheels(); - - _monorailWidth+=dif; - - //магнитная линия - if (EntityMonorail.MagniteRail()) - { - g2d.setColor(Color.BLACK); - g2d.drawLine(_startPosX, _startPosY + _monorailHeight, _startPosX + _monorailWidth, _startPosY + _monorailHeight); - } + _monorailWidth += dif; } - - } diff --git a/src/MonorailHard/DrawningWheels.java b/src/MonorailHard/DrawningObjects/DrawningWheels.java similarity index 91% rename from src/MonorailHard/DrawningWheels.java rename to src/MonorailHard/DrawningObjects/DrawningWheels.java index 28a7e09..20938f4 100644 --- a/src/MonorailHard/DrawningWheels.java +++ b/src/MonorailHard/DrawningObjects/DrawningWheels.java @@ -1,4 +1,6 @@ -package MonorailHard; +package MonorailHard.DrawningObjects; + +import MonorailHard.NumberType; import javax.swing.*; import java.awt.*; @@ -14,7 +16,7 @@ public class DrawningWheels { return WheelSz; } - boolean Init(int width, int height, int curX, int curY, Color wheelColor, Color tireColor, JPanel monorailPanel){ + public DrawningWheels(int width, int height, int curX, int curY, Color wheelColor, Color tireColor, JPanel monorailPanel){ Width = width; Height = height; CurX = curX; @@ -23,7 +25,6 @@ public class DrawningWheels { TireColor = tireColor; WheelSz = Height - Height * 7 / 10; MonorailPanel = monorailPanel; - return true; } public void ChangeWheelsNumb(int x){ diff --git a/src/MonorailHard/EntityMonorail.java b/src/MonorailHard/Entities/EntityMonorail.java similarity index 54% rename from src/MonorailHard/EntityMonorail.java rename to src/MonorailHard/Entities/EntityMonorail.java index 9eb2264..fdea9f4 100644 --- a/src/MonorailHard/EntityMonorail.java +++ b/src/MonorailHard/Entities/EntityMonorail.java @@ -1,20 +1,15 @@ -package MonorailHard; +package MonorailHard.Entities; import java.awt.*; public class EntityMonorail { - private int Speed, WheelNumb; + private int Speed; private double Weight, Step; private Color BodyColor, WheelColor, TireColor; - private boolean MagniteRail; - private boolean SecondCabine; public int Speed(){ return Speed; } - public int WheelNumb(){ - return WheelNumb; - } public double Weight(){ return Weight; } @@ -30,18 +25,12 @@ public class EntityMonorail { public Color TireColor(){ return TireColor; } - public boolean SecondCabine(){return SecondCabine;} - public boolean MagniteRail(){return MagniteRail;} - public void Init(int speed, double weight, Color bodyColor, Color wheelColor, Color tireColor, int wheelNumb, - boolean secondCabine, boolean magniteRail){ + public EntityMonorail(int speed, double weight, Color bodyColor, Color wheelColor, Color tireColor){ Speed = speed; Weight = weight; Step = (double)Speed * 100 / Weight; BodyColor = bodyColor; WheelColor = wheelColor; TireColor = tireColor; - WheelNumb = wheelNumb; - SecondCabine = secondCabine; - MagniteRail = magniteRail; } } diff --git a/src/MonorailHard/Main.java b/src/MonorailHard/Main.java index 1314426..50dcfd0 100644 --- a/src/MonorailHard/Main.java +++ b/src/MonorailHard/Main.java @@ -1,4 +1,8 @@ package MonorailHard; +import MonorailHard.DrawningObjects.DrawningLocomotive; +import MonorailHard.DrawningObjects.DrawningMonorail; +import MonorailHard.MovementStrategy.*; + import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; @@ -10,14 +14,20 @@ import javax.imageio.ImageIO; import javax.swing.*; public class Main { + static DrawningMonorail DrawningMonorail; + static AbstractStrategy _abstractStrategy; public static void main(String[] args) throws IOException { + String[] items = {"Довести до центра", "Довести до края"}; + + JComboBox comboBoxStrategy = new JComboBox(items); + comboBoxStrategy.setBounds(562,12,151,28); JFrame MonorailFrame = new JFrame(); + MonorailFrame.setResizable(false); JPanel MonorailPanel = new JPanel(); MonorailFrame.setLayout(new BorderLayout()); - MonorailFrame.setSize(900, 500); + MonorailFrame.setSize(743, 576); MonorailFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); MonorailFrame.setLayout(new BorderLayout(1,1)); - DrawningMonorail DrawningMonorail = new DrawningMonorail(); MonorailPanel.setLayout(null); BufferedImage RightIcon = ImageIO.read(new File("RightButton.png")); BufferedImage LeftIcon = ImageIO.read(new File("LeftButton.png")); @@ -29,36 +39,100 @@ public class Main { JButton UpButton = new JButton(new ImageIcon(UpIcon)); JButton DownButton = new JButton(new ImageIcon(DownIcon)); JButton CreateButton = new JButton(); + JButton CreateLocomotiveButton = new JButton(); + JButton buttonStep = new JButton(); + + CreateLocomotiveButton.setBounds(198,477,180, 40); + CreateLocomotiveButton.setText("Создать локомотив"); CreateButton.setText("Создать"); - CreateButton.setBounds(12, 401, 90, 40); - RightButton.setBounds(840,411,30,30); - LeftButton.setBounds(768,411,30,30); - UpButton.setBounds(804,375,30,30); - DownButton.setBounds(804,411,30,30); + buttonStep.setBounds(619, 46, 94, 29); + buttonStep.setText("шаг"); + CreateButton.setBounds(12, 477, 180, 40); + RightButton.setBounds(683,487,30,30); + LeftButton.setBounds(611,487,30,30); + UpButton.setBounds(647,451,30,30); + DownButton.setBounds(647,487,30,30); MonorailPanel.add(CreateButton); + MonorailPanel.add(CreateLocomotiveButton); MonorailPanel.add(RightButton); MonorailPanel.add(LeftButton); MonorailPanel.add(UpButton); MonorailPanel.add(DownButton); + MonorailPanel.add(comboBoxStrategy); + MonorailPanel.add(buttonStep); + comboBoxStrategy.setSelectedIndex(-1); + + MonorailFrame.add(MonorailPanel, BorderLayout.CENTER); Random random = new Random(); CreateButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - DrawningMonorail.Init(random.nextInt(100, 300), random.nextDouble(1000, 3000), + DrawningMonorail = new DrawningMonorail(random.nextInt(100, 300), random.nextDouble(1000, 3000), Color.getHSBColor(random.nextInt(0, 301), random.nextInt(0, 301), random.nextInt(0, 301)), Color.getHSBColor(random.nextInt(0, 301), random.nextInt(0, 301), random.nextInt(0, 301)), Color.getHSBColor(random.nextInt(0, 301), random.nextInt(0, 301), random.nextInt(0, 301)), - random.nextInt(2, 5), - MonorailPanel.getWidth(), MonorailPanel.getHeight(), random.nextBoolean(), random.nextBoolean(), MonorailPanel); + MonorailPanel.getWidth(), MonorailPanel.getHeight(), MonorailPanel); DrawningMonorail.DrawMonorail(); + comboBoxStrategy.enable(true); } }); + + CreateLocomotiveButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + DrawningMonorail = new DrawningLocomotive(random.nextInt(100, 300), random.nextDouble(1000, 3000), + Color.getHSBColor(random.nextInt(0, 301), random.nextInt(0, 301), random.nextInt(0, 301)), + Color.getHSBColor(random.nextInt(0, 301), random.nextInt(0, 301), random.nextInt(0, 301)), + Color.getHSBColor(random.nextInt(0, 301), random.nextInt(0, 301), random.nextInt(0, 301)), + random.nextInt(1, 6), + MonorailPanel.getWidth(), MonorailPanel.getHeight(), random.nextBoolean(), random.nextBoolean(), + Color.getHSBColor(random.nextInt(0, 301), random.nextInt(0, 301), random.nextInt(0, 301)), + MonorailPanel); + DrawningMonorail.DrawMonorail(); + comboBoxStrategy.enable(true); + } + }); + + buttonStep.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if(DrawningMonorail == null) + return; + if(comboBoxStrategy.isEnabled()){ + if(comboBoxStrategy.getSelectedIndex() == 0) + _abstractStrategy = new MoveToCenter(); + else if(comboBoxStrategy.getSelectedIndex() == 1) + _abstractStrategy = new MoveToBorder(); + else + _abstractStrategy = null; + if(_abstractStrategy == null) + return; + _abstractStrategy.SetData(new DrawningObjectMonorail(DrawningMonorail), MonorailPanel.getWidth(), + MonorailPanel.getHeight()); + comboBoxStrategy.enable(false); + } + if (_abstractStrategy == null) + { + return; + } + _abstractStrategy.MakeStep(); + DrawningMonorail.DrawMonorail(); + if (_abstractStrategy.GetStatus() == Status.Finish) + { + comboBoxStrategy.enable(true); + _abstractStrategy = null; + } + + } + + }); RightButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - if(DrawningMonorail.EntityMonorail() == null) + if(DrawningMonorail.EntityMonorail() == null) { return; + } DrawningMonorail.MoveTransport(DirectionType.Right); DrawningMonorail.DrawMonorail(); } diff --git a/src/MonorailHard/MovementStrategy/AbstractStrategy.java b/src/MonorailHard/MovementStrategy/AbstractStrategy.java index 571522b..b5a76cd 100644 --- a/src/MonorailHard/MovementStrategy/AbstractStrategy.java +++ b/src/MonorailHard/MovementStrategy/AbstractStrategy.java @@ -8,7 +8,7 @@ public abstract class AbstractStrategy { private int FieldWidth; protected int FieldWidth(){return FieldWidth;} private int FieldHeight; - protected int FieldHeight(){return FieldWidth;} + protected int FieldHeight(){return FieldHeight;} public Status GetStatus() { return _state; } public void SetData(IMoveableObject moveableObject, int width, int height) @@ -29,7 +29,7 @@ public abstract class AbstractStrategy { { return; } - if (IsTargetDestinaion()) + if (IsTargetDestination()) { _state = Status.Finish; return; @@ -57,8 +57,19 @@ public abstract class AbstractStrategy { protected abstract void MoveToTarget(); - protected abstract boolean IsTargetDestinaion(); + protected abstract boolean IsTargetDestination(); - private + private boolean MoveTo(DirectionType directionType) { + if (_state != Status.InProgress) + { + return false; + } + if (_moveableObject.CheckCanMove(directionType)) + { + _moveableObject.MoveObject(directionType); + return true; + } + return false; + } }