diff --git a/.idea/misc.xml b/.idea/misc.xml index 12f22af..8ed5f34 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml index daf7e72..0703f9c 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -2,7 +2,7 @@ - + \ No newline at end of file diff --git a/DrawingRoadTrain.java b/DrawingRoadTrain.java deleted file mode 100644 index 7b8830e..0000000 --- a/DrawingRoadTrain.java +++ /dev/null @@ -1,87 +0,0 @@ - -import java.awt.*; - -public class DrawingRoadTrain { - private WheelDrawing wheelDrawing; - public EntityRoadTrain EntityRoadTrain; - private int _pictureWidth; - private int _pictureHeight; - private int _startPosX; - private int _startPosY; - private int _roadTrainWidth = 200; - private int _roadTrainHeight = 100; - public boolean Init(int speed, double weight, Color bodyColor, Color - additionalColor, boolean wheel, boolean door, boolean light, int numWheel, int width, int height) - { - _pictureWidth = width; - _pictureHeight = height; - if (_pictureHeight < _roadTrainHeight || _pictureWidth < _roadTrainWidth) - return false; - EntityRoadTrain = new EntityRoadTrain(); - EntityRoadTrain.Init(speed, weight, bodyColor, additionalColor,wheel, door, light, numWheel); - - wheelDrawing = new WheelDrawing(); - wheelDrawing.setNumWheel(numWheel); - return true; - } - public void SetPosition(int x, int y) - { - _startPosX = Math.min(x, _pictureWidth-_roadTrainWidth); - _startPosY = Math.min(y, _pictureHeight- _roadTrainHeight); - } - public void MoveTransport(Direction direction) - { - if (EntityRoadTrain == null){ - return; - } - switch (direction) - { - case Left: - if (_startPosX - EntityRoadTrain.Step > 0) - { - _startPosX -= (int) EntityRoadTrain.Step; - } - break; - case Up: - if (_startPosY - EntityRoadTrain.Step > 0) - { - _startPosY -= (int) EntityRoadTrain.Step; - } - break; - case Right: - if (_startPosX + _roadTrainWidth + EntityRoadTrain.Step < _pictureWidth) - { - _startPosX += (int) EntityRoadTrain.Step; - } - break; - case Down: - if (_startPosY + _roadTrainHeight + EntityRoadTrain.Step < _pictureHeight) - { - _startPosY += (int) EntityRoadTrain.Step; - } - break; - } - } - public void DrawTransport(Graphics2D g) { - if (EntityRoadTrain == null) { - return; - } - // машина - g.drawRect(_startPosX, _startPosY + 50, 160, 20); // кузов - g.drawRect(_startPosX + 120, _startPosY + 10, 40, 40); // кабина - g.drawRect(_startPosX + 10, _startPosY, 90, 50); // бак с водой - g.drawRect(_startPosX + 130, _startPosY + 20, 30, 20); // окно - g.drawLine(_startPosX + 160, _startPosY + 70, _startPosX + 180, _startPosY + 80); // держатель для щетки - g.drawRect(_startPosX + 170, _startPosY + 80, 40, 10); // щетка - // обвесы - g.setColor(EntityRoadTrain.AdditionalColor); - wheelDrawing.Draw(_startPosX, _startPosY, EntityRoadTrain.AdditionalColor, g); - - if (EntityRoadTrain.Door) { - g.fillRect(_startPosX + 40, _startPosY + 20, 20, 30); // дверь - } - if (EntityRoadTrain.Light) { - g.fillRect(_startPosX + 135, _startPosY, 10, 10); // мигалка - } - } -} diff --git a/EntityRoadTrain.java b/EntityRoadTrain.java deleted file mode 100644 index e397359..0000000 --- a/EntityRoadTrain.java +++ /dev/null @@ -1,26 +0,0 @@ -import java.awt.*; - -public class EntityRoadTrain { - public int Speed; - public double Weight; - public Color BodyColor; - public Color AdditionalColor; - public boolean Wheel; - public boolean Door; - public boolean Light; - public double Step; - public int numWheel; - public void Init(int speed, double weight, Color bodyColor, Color - additionalColor, boolean wheel, boolean door, boolean light, int numwheel) - { - numWheel = numwheel; - Speed = speed; - Weight = weight; - BodyColor = bodyColor; - AdditionalColor = additionalColor; - Wheel = wheel; - Door = door; - Light = light; - Step = (double)Speed * 100 / Weight; - } -} diff --git a/FormRoadTrain.java b/FormRoadTrain.java deleted file mode 100644 index 1672cc2..0000000 --- a/FormRoadTrain.java +++ /dev/null @@ -1,128 +0,0 @@ -import javax.swing.*; -import java.awt.*; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; -import java.util.Random; - -public class FormRoadTrain { - private DrawingRoadTrain _drawingRoadTrain; - Canvas canv; - public void Draw(){ - canv.repaint(); - } - public FormRoadTrain(){ - JFrame w=new JFrame ("RoadTrain"); - JButton buttonCreate = new JButton("Create"); - buttonCreate.setBorderPainted(false); - buttonCreate.setContentAreaFilled(false); - buttonCreate.setFont(new Font("Arial", Font.PLAIN, 20)); - JButton up = new JButton(); - up.setBorderPainted(false); - up.setContentAreaFilled(false); - up.setName("up"); - Image img_up = new ImageIcon("images/up.png").getImage(); - Image newImg_up = img_up.getScaledInstance(20, 20, java.awt.Image.SCALE_SMOOTH); - up.setIcon(new ImageIcon(newImg_up)); - - JButton down = new JButton(); - down.setBorderPainted(false); - down.setContentAreaFilled(false); - down.setName("down"); - Image img_down = new ImageIcon("images/down.png").getImage(); - Image newImg_down = img_down.getScaledInstance(20, 20, java.awt.Image.SCALE_SMOOTH); - down.setIcon(new ImageIcon(newImg_down)); - - JButton left = new JButton(); - left.setBorderPainted(false); - left.setContentAreaFilled(false); - left.setName("left"); - Image img_left = new ImageIcon("images/left.png").getImage(); - Image newImg_left = img_left.getScaledInstance(20, 20, java.awt.Image.SCALE_SMOOTH); - left.setIcon(new ImageIcon(newImg_left)); - - JButton right = new JButton(); - right.setBorderPainted(false); - right.setContentAreaFilled(false); - right.setName("right"); - Image img_right = new ImageIcon("images/right.png").getImage(); - Image newImg_right = img_right.getScaledInstance(20, 20, java.awt.Image.SCALE_SMOOTH); - right.setIcon(new ImageIcon(newImg_right)); - - buttonCreate.addActionListener( - new ActionListener() { - public void actionPerformed(ActionEvent e){ - Random random = new Random(); - _drawingRoadTrain = new DrawingRoadTrain(); - _drawingRoadTrain.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.nextInt(0, 2) == 1, random.nextInt(0, 2) == 1,random.nextInt(0, 2) == 1, - random.nextInt(1, 4), 1000, 560); - _drawingRoadTrain.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100)); - canv._drawingRoadTrain = _drawingRoadTrain; - Draw(); - } - } - ); - ActionListener actioListener = new ActionListener() { - public void actionPerformed(ActionEvent e){ - if (_drawingRoadTrain == null) - { - return; - } - switch(((JButton)(e.getSource())).getName()){ - case "up": - _drawingRoadTrain.MoveTransport(Direction.Up); - break; - case "down": - _drawingRoadTrain.MoveTransport(Direction.Down); - break; - case "left": - _drawingRoadTrain.MoveTransport(Direction.Left); - break; - case "right": - _drawingRoadTrain.MoveTransport(Direction.Right); - break; - } - Draw(); - } - }; - up.addActionListener(actioListener); - 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(30, 500, 100, 30); - up.setBounds(880, 450, 20, 20); - down.setBounds(880, 510, 20, 20); - left.setBounds(850, 480, 20, 20); - right.setBounds(910, 480, 20, 20); - w.add(canv); - w.add(buttonCreate); - w.add(up); - w.add(down); - w.add(left); - w.add(right); - w.setVisible (true); - } -} - -class Canvas extends JComponent{ - public DrawingRoadTrain _drawingRoadTrain; - public Canvas(){ - } - public void paintComponent (Graphics g){ - if (_drawingRoadTrain == null){ - return; - } - super.paintComponents (g) ; - Graphics2D g2d = (Graphics2D)g; - _drawingRoadTrain.DrawTransport(g2d); - super.repaint(); - } -} diff --git a/src/AbstractStrategy.java b/src/AbstractStrategy.java new file mode 100644 index 0000000..6f8645a --- /dev/null +++ b/src/AbstractStrategy.java @@ -0,0 +1,63 @@ +package src; + +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/Direction.java b/src/Direction.java similarity index 82% rename from Direction.java rename to src/Direction.java index 5b70d63..9c9bce1 100644 --- a/Direction.java +++ b/src/Direction.java @@ -1,3 +1,5 @@ +package src; + public enum Direction { Up, Down, diff --git a/src/DrawingObjectTrain.java b/src/DrawingObjectTrain.java new file mode 100644 index 0000000..a8b78bc --- /dev/null +++ b/src/DrawingObjectTrain.java @@ -0,0 +1,27 @@ +package src; + +public class DrawingObjectTrain implements IMoveableObject { + + private DrawingRoadTrain _drawingRoadTrain = null; + + public DrawingObjectTrain(DrawingRoadTrain drawingRoadTrain) + { + _drawingRoadTrain = drawingRoadTrain; + } + + public ObjectParameters GetObjectPosition(){ + if (_drawingRoadTrain == null || _drawingRoadTrain.EntityRoadTrain == + null) + { + return null; + } + return new ObjectParameters(_drawingRoadTrain.GetPosX(), + _drawingRoadTrain.GetPosY(), _drawingRoadTrain.GetWidth(), _drawingRoadTrain.GetHeight()); + } + public int GetStep(){ return (int) _drawingRoadTrain.EntityRoadTrain.Step; } + + public boolean CheckCanMove(Direction direction) { return _drawingRoadTrain.CanMove(direction);} + + public void MoveObject(Direction direction) { _drawingRoadTrain.MoveTransport(direction); } + +} diff --git a/src/DrawingRoadTrain.java b/src/DrawingRoadTrain.java new file mode 100644 index 0000000..6f6a671 --- /dev/null +++ b/src/DrawingRoadTrain.java @@ -0,0 +1,101 @@ +package src; + +import java.awt.*; +import java.util.Random; + +public class DrawingRoadTrain { + protected IWheelDrawing wheelDrawing; + public EntityRoadTrain EntityRoadTrain; + protected int _pictureWidth; + protected int _pictureHeight; + protected int _startPosX; + protected int _startPosY; + protected int _roadTrainWidth = 200; + protected int _roadTrainHeight = 100; + public DrawingRoadTrain(int speed, double weight, Color bodyColor, int numWheel, int width, int height) + { + _pictureWidth = width; + _pictureHeight = height; + if (_pictureHeight < _roadTrainHeight || _pictureWidth < _roadTrainWidth) + return; + EntityRoadTrain = new EntityRoadTrain(speed, weight, bodyColor, numWheel); + + Random random = new Random(); + switch(random.nextInt(0, 3)){ + case 0: + wheelDrawing = new WheelDrawing(); + break; + case 1: + wheelDrawing = new WheelDrawingRect(); + break; + case 2: + wheelDrawing = new WheelDrawingTringle(); + break; + default: + wheelDrawing = new WheelDrawing(); + break; + } + wheelDrawing.setNumWheel(numWheel); + } + public void SetPosition(int x, int y) + { + _startPosX = Math.min(x, _pictureWidth-_roadTrainWidth); + _startPosY = Math.min(y, _pictureHeight- _roadTrainHeight); + } + public void MoveTransport(Direction direction) + { + if (!CanMove(direction) || EntityRoadTrain == null) + { + return; + } + switch (direction) + { + case Left: + _startPosX -= (int)EntityRoadTrain.Step; + break; + case Up: + _startPosY -= (int)EntityRoadTrain.Step; + break; + case Right: + _startPosX += (int)EntityRoadTrain.Step; + break; + case Down: + _startPosY += (int)EntityRoadTrain.Step; + break; + } + } + public void DrawTransport(Graphics2D g) { + if (EntityRoadTrain == null) { + return; + } + // машина + g.drawRect(_startPosX, _startPosY + 50, 160, 20); // кузов + g.drawRect(_startPosX + 120, _startPosY + 10, 40, 40); // кабина + g.drawRect(_startPosX + 130, _startPosY + 20, 30, 20); // окно + wheelDrawing.Draw(_startPosX, _startPosY, Color.BLACK, g); + } + public int GetPosX (){return _startPosX;} + public int GetPosY (){return _startPosY;} + public int GetWidth (){return _roadTrainWidth;} + public int GetHeight (){return _roadTrainHeight;} + public boolean CanMove(Direction direction) + { + if (EntityRoadTrain == null) + { + return false; + } + switch (direction) + { + case Left: + return _startPosX - EntityRoadTrain.Step > 0; + case Right: + return _startPosX + _roadTrainWidth + EntityRoadTrain.Step < _pictureWidth; + case Up: + return _startPosY - EntityRoadTrain.Step > 0; + case Down: + return _startPosY + _roadTrainHeight + EntityRoadTrain.Step < _pictureHeight; + default: + return false; + } + } +} diff --git a/src/DrawingRoadTrainWithTank.java b/src/DrawingRoadTrainWithTank.java new file mode 100644 index 0000000..267cbe1 --- /dev/null +++ b/src/DrawingRoadTrainWithTank.java @@ -0,0 +1,45 @@ +package src; + +import java.awt.*; + +public class DrawingRoadTrainWithTank extends DrawingRoadTrain{ + + public DrawingRoadTrainWithTank(int speed, double weight, Color bodyColor, Color + additionalColor, boolean tank, boolean brush, int _numWheel, int width, int height) { + super(speed, weight, bodyColor, _numWheel, width, height); + EntityRoadTrain = new EntityRoadTrainWithTank(speed, weight, bodyColor, additionalColor, tank, brush, _numWheel); + } + + + public void SetPosition(int x, int y) + { + _startPosX = Math.min(x, _pictureWidth-_roadTrainWidth); + _startPosY = Math.min(y, _pictureHeight-_roadTrainHeight); + } + + public void DrawTransport(Graphics2D g) + { + + if (EntityRoadTrain == null) + { + return; + } + + super.DrawTransport(g); + + g.setColor(((EntityRoadTrainWithTank)EntityRoadTrain).AdditionalColor); + + + wheelDrawing.Draw(_startPosX, _startPosY, ((EntityRoadTrainWithTank) EntityRoadTrain).AdditionalColor, g); + if (((EntityRoadTrainWithTank)EntityRoadTrain).Brush) { + g.drawLine(_startPosX + 160, _startPosY + 70, _startPosX + 180, _startPosY + 80); + g.drawRect(_startPosX + 170, _startPosY + 80, 40, 10); //щетка + g.fillRect(_startPosX + 170, _startPosY + 80, 40, 10); + } + + if (((EntityRoadTrainWithTank)EntityRoadTrain).Tank) { + g.drawRect(_startPosX + 10, _startPosY, 90, 50); //бак с водой + g.fillRect(_startPosX + 10, _startPosY, 90, 50); + } + } +} diff --git a/src/EntityRoadTrain.java b/src/EntityRoadTrain.java new file mode 100644 index 0000000..8e05bfb --- /dev/null +++ b/src/EntityRoadTrain.java @@ -0,0 +1,19 @@ +package src; + +import java.awt.*; + +public class EntityRoadTrain { + public int Speed; + public double Weight; + public Color BodyColor; + public double Step; + public int numWheel; + public EntityRoadTrain(int speed, double weight, Color bodyColor, int numwheel) + { + numWheel = numwheel; + Speed = speed; + Weight = weight; + BodyColor = bodyColor; + Step = (double)Speed * 100 / Weight; + } +} diff --git a/src/EntityRoadTrainWithTank.java b/src/EntityRoadTrainWithTank.java new file mode 100644 index 0000000..03666e0 --- /dev/null +++ b/src/EntityRoadTrainWithTank.java @@ -0,0 +1,21 @@ +package src; + +import java.awt.*; + +public class EntityRoadTrainWithTank extends EntityRoadTrain{ + + public Color AdditionalColor; + + public boolean Tank; + + public boolean Brush; + + public EntityRoadTrainWithTank(int speed, double weight, Color bodyColor, Color + additionalColor, boolean tank, boolean brush, int _numWheel) + { + super(speed, weight, bodyColor, _numWheel); + AdditionalColor = additionalColor; + Tank = tank; + Brush = brush; + } +} diff --git a/src/FormRoadTrain.java b/src/FormRoadTrain.java new file mode 100644 index 0000000..bfb3f95 --- /dev/null +++ b/src/FormRoadTrain.java @@ -0,0 +1,218 @@ +package src; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.Random; + +public class FormRoadTrain { + private DrawingRoadTrain _drawingRoadTrain; + private AbstractStrategy abstractStrategy; + Canvas canv; + static int pictureBoxWidth = 980; + static int pictureBoxHeight = 580; + public void Draw(){ + canv.repaint(); + } + public FormRoadTrain(){ + JFrame w=new JFrame ("RoadTrain"); + JButton buttonCreateRoadTrain = new JButton("Normal"); + buttonCreateRoadTrain.setBorderPainted(false); + buttonCreateRoadTrain.setContentAreaFilled(false); + buttonCreateRoadTrain.setFont(new Font("Arial", Font.PLAIN, 20)); + buttonCreateRoadTrain.setSize(100,100); + JButton buttonCreateRoadTrainWithTank = new JButton("With Tank"); + buttonCreateRoadTrainWithTank.setBorderPainted(false); + buttonCreateRoadTrainWithTank.setContentAreaFilled(false); + buttonCreateRoadTrainWithTank.setFont(new Font("Arial", Font.PLAIN, 20)); + JButton buttonStrategysStep = new JButton("strategys step"); + buttonStrategysStep.setBorderPainted(false); + buttonStrategysStep.setContentAreaFilled(false); + buttonStrategysStep.setFont(new Font("Arial", Font.PLAIN, 15)); + JComboBox comboBoxStrategy = new JComboBox( + new String[]{ + "к центру", + "к краю", + }); + comboBoxStrategy.setFont(new Font("Arial", Font.PLAIN, 15)); + JButton up = new JButton(); + up.setBorderPainted(false); + up.setFocusPainted(false); + up.setContentAreaFilled(false); + up.setName("up"); + Image img_up = new ImageIcon("D:\\study\\RPP_HARD\\PIbd-22_Smirnov_A_A_RoadTrain_HARD\\images\\up.png").getImage(); + Image newImg_up = img_up.getScaledInstance(20, 20, java.awt.Image.SCALE_SMOOTH); + up.setIcon(new ImageIcon(newImg_up)); + + JButton down = new JButton(); + down.setBorderPainted(false); + down.setContentAreaFilled(false); + down.setName("down"); + Image img_down = new ImageIcon("D:\\study\\RPP_HARD\\PIbd-22_Smirnov_A_A_RoadTrain_HARD\\images\\down.png").getImage(); + Image newImg_down = img_down.getScaledInstance(20, 20, java.awt.Image.SCALE_SMOOTH); + down.setIcon(new ImageIcon(newImg_down)); + + JButton left = new JButton(); + left.setBorderPainted(false); + left.setContentAreaFilled(false); + left.setName("left"); + Image img_left = new ImageIcon("D:\\study\\RPP_HARD\\PIbd-22_Smirnov_A_A_RoadTrain_HARD\\images\\left.png").getImage(); + Image newImg_left = img_left.getScaledInstance(20, 20, java.awt.Image.SCALE_SMOOTH); + left.setIcon(new ImageIcon(newImg_left)); + + JButton right = new JButton(); + right.setBorderPainted(false); + right.setContentAreaFilled(false); + right.setName("right"); + Image img_right = new ImageIcon("D:\\study\\RPP_HARD\\PIbd-22_Smirnov_A_A_RoadTrain_HARD\\images\\right.png").getImage(); + Image newImg_right = img_right.getScaledInstance(20, 20, java.awt.Image.SCALE_SMOOTH); + right.setIcon(new ImageIcon(newImg_right)); + + buttonCreateRoadTrain.addActionListener( + new ActionListener() { + public void actionPerformed(ActionEvent e){ + Random random = new Random(); + _drawingRoadTrain = new DrawingRoadTrain( + random.nextInt(100, 300), + random.nextInt(1000, 3000), + new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)), + random.nextInt(1, 4), + pictureBoxWidth, + pictureBoxHeight); + _drawingRoadTrain.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100)); + canv._drawingRoadTrain = _drawingRoadTrain; + Draw(); + } + } + ); + buttonCreateRoadTrainWithTank.addActionListener( + new ActionListener() { + public void actionPerformed(ActionEvent e){ + Random random = new Random(); + _drawingRoadTrain = new DrawingRoadTrainWithTank( + 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.nextInt(0, 2) == 1, + random.nextInt(0, 2) == 1, + random.nextInt(1, 4), + pictureBoxWidth, + pictureBoxHeight); + _drawingRoadTrain.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100)); + canv._drawingRoadTrain = _drawingRoadTrain; + Draw(); + } + } + ); + buttonStrategysStep.addActionListener( + new ActionListener() { + public void actionPerformed(ActionEvent e){ + if (_drawingRoadTrain == 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 DrawingObjectTrain(_drawingRoadTrain), pictureBoxWidth, pictureBoxHeight); + comboBoxStrategy.setEnabled(false); + } + if (abstractStrategy == null) + { + return; + } + abstractStrategy.MakeStep(); + Draw(); + if (abstractStrategy.GetStatus() == Status.Finish) + { + comboBoxStrategy.setEnabled(true); + abstractStrategy = null; + } + } + } + ); + ActionListener actionListener = new ActionListener() { + public void actionPerformed(ActionEvent e){ + if (_drawingRoadTrain == null) + { + return; + } + switch(((JButton)(e.getSource())).getName()){ + case "up": + _drawingRoadTrain.MoveTransport(Direction.Up); + break; + case "down": + _drawingRoadTrain.MoveTransport(Direction.Down); + break; + case "left": + _drawingRoadTrain.MoveTransport(Direction.Left); + break; + case "right": + _drawingRoadTrain.MoveTransport(Direction.Right); + break; + } + Draw(); + } + }; + up.addActionListener(actionListener); + down.addActionListener(actionListener); + left.addActionListener(actionListener); + right.addActionListener(actionListener); + + w.setSize (1000, 600); + w.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); + w.setLayout(null); + canv = new Canvas(); + canv.setBounds(0, 0, pictureBoxWidth, pictureBoxHeight); + buttonCreateRoadTrain.setBounds(2, 540, 150, 20); + buttonCreateRoadTrainWithTank.setBounds(160, 540, 200, 20); + up.setBounds(900, 480, 40, 40); + down.setBounds(900, 520, 40, 40); + left.setBounds(860, 520, 40, 40); + right.setBounds(940, 520, 40, 40); + comboBoxStrategy.setBounds(pictureBoxWidth - 150, 20, 150, 20); + buttonStrategysStep.setBounds(pictureBoxWidth - 150, 45, 150, 20); + w.add(canv); + w.add(buttonCreateRoadTrain); + w.add(buttonCreateRoadTrainWithTank); + w.add(up); + w.add(down); + w.add(left); + w.add(right); + w.add(comboBoxStrategy); + w.add(buttonStrategysStep); + w.setVisible (true); + } +} + +class Canvas extends JComponent{ + public DrawingRoadTrain _drawingRoadTrain; + public Canvas(){ + } + public void paintComponent (Graphics g){ + if (_drawingRoadTrain == null){ + return; + } + super.paintComponents (g) ; + Graphics2D g2d = (Graphics2D)g; + _drawingRoadTrain.DrawTransport(g2d); + super.repaint(); + } +} diff --git a/src/IMoveableObject.java b/src/IMoveableObject.java new file mode 100644 index 0000000..05def6b --- /dev/null +++ b/src/IMoveableObject.java @@ -0,0 +1,14 @@ +package src; + +public interface IMoveableObject { + + ObjectParameters GetObjectPosition(); + + int GetStep(); + + boolean CheckCanMove(Direction direction); + + void MoveObject(Direction direction); + +} + diff --git a/src/IWheelDrawing.java b/src/IWheelDrawing.java new file mode 100644 index 0000000..56949b2 --- /dev/null +++ b/src/IWheelDrawing.java @@ -0,0 +1,13 @@ +package src; + +import java.awt.*; + +public interface IWheelDrawing { + + public NumWheel getNumWheel(); + + public void setNumWheel(int kWheel); + + public void Draw(int _startPosX, int _startPosY, Color color, Graphics2D g2d); + +} diff --git a/Main.java b/src/Main.java similarity index 86% rename from Main.java rename to src/Main.java index ffafb77..08db27e 100644 --- a/Main.java +++ b/src/Main.java @@ -1,3 +1,5 @@ +package src; + public class Main { public static void main(String[] args) { new FormRoadTrain(); } diff --git a/src/MoveToBorder.java b/src/MoveToBorder.java new file mode 100644 index 0000000..f62ed06 --- /dev/null +++ b/src/MoveToBorder.java @@ -0,0 +1,51 @@ +package src; + +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/src/MoveToCenter.java b/src/MoveToCenter.java new file mode 100644 index 0000000..d32e97b --- /dev/null +++ b/src/MoveToCenter.java @@ -0,0 +1,51 @@ +package src; + +public class MoveToCenter extends AbstractStrategy { + + protected boolean IsTargetDestinaion() + { + var objParams = GetObjectParameters(); + if (objParams == null) + { + return false; + } + return + Math.abs(objParams.ObjectMiddleHorizontal() - FieldWidth / 2) <= GetStep() + && + Math.abs(objParams.ObjectMiddleVertical() - FieldHeight / 2) <= GetStep(); + } + + 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/NumWheel.java b/src/NumWheel.java similarity index 82% rename from NumWheel.java rename to src/NumWheel.java index c1c8900..b5d2fdf 100644 --- a/NumWheel.java +++ b/src/NumWheel.java @@ -1,3 +1,5 @@ +package src; + public enum NumWheel { oneWheel, diff --git a/src/ObjectParameters.java b/src/ObjectParameters.java new file mode 100644 index 0000000..71025ce --- /dev/null +++ b/src/ObjectParameters.java @@ -0,0 +1,25 @@ +package src; + +public class ObjectParameters { + + private int _x; + private int _y; + private int _width; + private int _height; + public int RightBorder () {return _x + _width;} + + public int DownBorder () {return _y + _height;} + + public int ObjectMiddleHorizontal () {return _x + _width / 2;} + + public int ObjectMiddleVertical () {return _y + _height / 2;} + + public ObjectParameters(int x, int y, int width, int height) + { + _x = x; + _y = y; + _width = width; + _height = height; + } +} + diff --git a/src/Status.java b/src/Status.java new file mode 100644 index 0000000..e323837 --- /dev/null +++ b/src/Status.java @@ -0,0 +1,8 @@ +package src; + +public enum Status { + NotInit, + InProgress, + Finish +} + diff --git a/WheelDrawing.java b/src/WheelDrawing.java similarity index 63% rename from WheelDrawing.java rename to src/WheelDrawing.java index 85389dd..385eda7 100644 --- a/WheelDrawing.java +++ b/src/WheelDrawing.java @@ -1,8 +1,10 @@ +package src; + import java.awt.*; -public class WheelDrawing { +public class WheelDrawing implements IWheelDrawing{ private NumWheel numWheel; - public NumWheel getSomeProperty() { + public NumWheel getNumWheel() { return numWheel; } public void setNumWheel(int kWheel){ @@ -22,20 +24,29 @@ public class WheelDrawing { break; } } - void Draw(int _startPosX, int _startPosY, Color color, Graphics2D g){ + public void Draw(int _startPosX, int _startPosY, Color color, Graphics2D g){ g.setColor(color); switch (numWheel) { case oneWheel -> { g.drawOval(_startPosX + 120, _startPosY + 70, 30, 30); + g.drawOval(_startPosX + 125, _startPosY + 75, 20, 20); } case twoWheel -> { g.drawOval(_startPosX + 120, _startPosY + 70, 30, 30); + g.drawOval(_startPosX + 125, _startPosY + 75, 20, 20); + g.drawOval(_startPosX + 50, _startPosY + 70, 30, 30); + g.drawOval(_startPosX + 55, _startPosY + 75, 20, 20); } case threeWheel -> { g.drawOval(_startPosX + 120, _startPosY + 70, 30, 30); - g.drawOval(_startPosX + 55, _startPosY + 70, 30, 30); + g.drawOval(_startPosX + 125, _startPosY + 75, 20, 20); + + g.drawOval(_startPosX + 50, _startPosY + 70, 30, 30); + g.drawOval(_startPosX + 55, _startPosY + 75, 20, 20); + g.drawOval(_startPosX + 15, _startPosY + 70, 30, 30); + g.drawOval(_startPosX + 20, _startPosY + 75, 20, 20); } }; } diff --git a/src/WheelDrawingRect.java b/src/WheelDrawingRect.java new file mode 100644 index 0000000..6719a7e --- /dev/null +++ b/src/WheelDrawingRect.java @@ -0,0 +1,57 @@ +package src; + +import java.awt.*; + +public class WheelDrawingRect implements IWheelDrawing{ + + private NumWheel numWheel; + + public NumWheel getNumWheel() { + return numWheel; + } + + public void setNumWheel(int kWheel){ + switch(kWheel){ + case 1: + numWheel = NumWheel.oneWheel; + break; + case 2: + numWheel = NumWheel.twoWheel; + break; + case 3: + numWheel = NumWheel.threeWheel; + break; + default: + numWheel = NumWheel.oneWheel; + System.out.println("Ошибка! Количество " + kWheel); + break; + } + } + + public void Draw(int _startPosX, int _startPosY, Color color, Graphics2D g){ + g.setColor(color); + switch (numWheel) { + case oneWheel -> { + g.drawOval(_startPosX + 120, _startPosY + 70, 30, 30); + g.drawRect(_startPosX + 125, _startPosY + 75, 20, 20); + } + case twoWheel -> { + g.drawOval(_startPosX + 120, _startPosY + 70, 30, 30); + g.drawRect(_startPosX + 125, _startPosY + 75, 20, 20); + + g.drawOval(_startPosX + 50, _startPosY + 70, 30, 30); + g.drawRect(_startPosX + 55, _startPosY + 75, 20, 20); + } + case threeWheel -> { + g.drawOval(_startPosX + 120, _startPosY + 70, 30, 30); + g.drawRect(_startPosX + 125, _startPosY + 75, 20, 20); + + g.drawOval(_startPosX + 50, _startPosY + 70, 30, 30); + g.drawRect(_startPosX + 55, _startPosY + 75, 20, 20); + + g.drawOval(_startPosX + 15, _startPosY + 70, 30, 30); + g.drawRect(_startPosX + 20, _startPosY + 75, 20, 20); + } + }; + } +} diff --git a/src/WheelDrawingTringle.java b/src/WheelDrawingTringle.java new file mode 100644 index 0000000..1623a29 --- /dev/null +++ b/src/WheelDrawingTringle.java @@ -0,0 +1,72 @@ +package src; + +import java.awt.*; + +public class WheelDrawingTringle implements IWheelDrawing{ + private NumWheel numWheel; + public NumWheel getNumWheel() { + return numWheel; + } + public void setNumWheel(int kWheel){ + switch(kWheel){ + case 1: + numWheel = NumWheel.oneWheel; + break; + case 2: + numWheel = NumWheel.twoWheel; + break; + case 3: + numWheel = NumWheel.threeWheel; + break; + default: + numWheel = NumWheel.oneWheel; + System.out.println("Ошибка! Количество " + kWheel); + break; + } + } + public void Draw(int _startPosX, int _startPosY, Color color, Graphics2D g){ + g.setColor(color); + switch (numWheel) { + case oneWheel -> { + g.drawOval(_startPosX + 120, _startPosY + 70, 30, 30); + g.drawPolygon( + new int[]{ _startPosX + 135, _startPosX + 125, _startPosX + 145 }, + new int[]{ _startPosY + 70, _startPosY + 95, _startPosY + 95 }, + 3); + } + case twoWheel -> { + g.drawOval(_startPosX + 120, _startPosY + 70, 30, 30); + g.drawPolygon( + new int[]{ _startPosX + 135, _startPosX + 125, _startPosX + 145 }, + new int[]{ _startPosY + 70, _startPosY + 95, _startPosY + 95 }, + 3); + + g.drawOval(_startPosX + 50, _startPosY + 70, 30, 30); + g.drawPolygon( + new int[]{_startPosX + 65, _startPosX + 55, _startPosX + 75}, + new int[]{_startPosY + 70, _startPosY + 95, _startPosY + 95}, + 3 + ); + } + case threeWheel -> { + g.drawOval(_startPosX + 120, _startPosY + 70, 30, 30); + g.drawPolygon( + new int[]{ _startPosX + 135, _startPosX + 125, _startPosX + 145 }, + new int[]{ _startPosY + 70, _startPosY + 95, _startPosY + 95 }, + 3); + + g.drawOval(_startPosX + 50, _startPosY + 70, 30, 30); + g.drawPolygon( + new int[]{_startPosX + 65, _startPosX + 55, _startPosX + 75}, + new int[]{_startPosY + 70, _startPosY + 95, _startPosY + 95}, + 3); + + g.drawOval(_startPosX + 15, _startPosY + 70, 30, 30); + g.drawPolygon( + new int[]{_startPosX + 30, _startPosX + 20, _startPosX + 40}, + new int[]{_startPosY + 70, _startPosY + 95, _startPosY + 95}, + 3); + } + }; + } +}