diff --git a/AbstractStrategy.java b/AbstractStrategy.java new file mode 100644 index 0000000..af7eff4 --- /dev/null +++ b/AbstractStrategy.java @@ -0,0 +1,106 @@ +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(); + } + /// + /// Перемещение влево + /// + /// Результат перемещения (true - удалось переместиться, false - неудача) + 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 Integer.parseInt(null); + } + return _moveableObject.GetStep(); + } + /// + /// Перемещение к цели + /// + protected abstract void MoveToTarget(); + /// + /// Достигнута ли цель + /// + /// + protected abstract boolean IsTargetDestinaion(); + /// + /// Попытка перемещения в требуемом направлении + /// + /// Направление + /// Результат попытки (true - удалось переместиться, false - неудача) + private boolean MoveTo(Direction directionType) + { + if (_state != Status.InProgress) + { + return false; + } + if (_moveableObject.CheckCanMove(directionType)) + { + _moveableObject.MoveObject(directionType); + return true; + } + else { + return false; + } + + } +} + diff --git a/DopClassRect.java b/DopClassRect.java new file mode 100644 index 0000000..555159f --- /dev/null +++ b/DopClassRect.java @@ -0,0 +1,42 @@ +import java.awt.*; + +public class DopClassRect implements IDop{ + + public int numOfWheels; + public DopClassRect(int num){ + numOfWheels = num; + } + public void setNumOfWheels(String num){ + switch (Integer.valueOf(num)){ + case 2: + numOfWheels = NumberOfWheelsEnum.wheel_2.value; + break; + case 3: + numOfWheels = NumberOfWheelsEnum.wheel_3.value; + break; + case 4: + numOfWheels = NumberOfWheelsEnum.wheel_4.value; + break; + default: + numOfWheels = NumberOfWheelsEnum.wheel_2.value; + } + + } + + public void drawWheels(Graphics g, int _startPosX, int _startPosY, Color c) { + g.setColor(c); + System.out.println("WHITE"); + if (numOfWheels >= 2) { + g.fillRect(_startPosX, _startPosY + 35 + 10, 15, 15); + g.fillRect(_startPosX + 95, _startPosY + 35 + 10, 15, 15); + + } if (numOfWheels >= 3) { + g.fillRect(_startPosX, _startPosY + 35 + 10, 15, 15); + g.fillRect(_startPosX + 15, _startPosY + 35 + 10, 15, 15); + + } if (numOfWheels >= 4) { + g.fillRect(_startPosX + 95 - 20, _startPosY + 35 + 10, 15, 15); + } + } +} + diff --git a/DopClassTriangle.java b/DopClassTriangle.java new file mode 100644 index 0000000..119202e --- /dev/null +++ b/DopClassTriangle.java @@ -0,0 +1,39 @@ +import java.awt.*; + +public class DopClassTriangle implements IDop{ + public int numOfWheels; + + public DopClassTriangle(int num){ + numOfWheels = num; + } + public void setNumOfWheels(String num){ + switch (Integer.valueOf(num)){ + case 2: + numOfWheels = NumberOfWheelsEnum.wheel_2.value; + break; + case 3: + numOfWheels = NumberOfWheelsEnum.wheel_3.value; + break; + case 4: + numOfWheels = NumberOfWheelsEnum.wheel_4.value; + break; + default: + numOfWheels = NumberOfWheelsEnum.wheel_2.value; + } + + } + + public void drawWheels(Graphics g, int _startPosX, int _startPosY, Color c) { + g.setColor(c); + + if (numOfWheels >= 2) { + g.fillPolygon(new int[]{_startPosX, _startPosX + 15/2, _startPosX + 15}, new int[]{_startPosY + 35 + 10, _startPosY + 35 + 10 + 15/2, _startPosY + 35 + 10}, 3); + g.fillPolygon( new int[]{_startPosX + 95, _startPosX + 95 + 15/2, _startPosX + 95 + 15}, new int[]{_startPosY + 35 + 10, _startPosY + 35 + 10 + 15/2, _startPosY + 35 + 10}, 3); + } if (numOfWheels >= 3) { + g.fillPolygon(new int[]{_startPosX + 15, _startPosX + 15 + 15/2, _startPosX + 15 + 15}, new int[]{_startPosY + 35 + 10, _startPosY + 35 + 10 + 15/2, _startPosY + 35 + 10}, 3); + } if (numOfWheels >= 4) { + g.fillPolygon(new int[]{_startPosX + 95 - 20, _startPosX + 95 - 20 + 15/2, _startPosX + 95 - 20 + 15}, new int[]{_startPosY + 35 + 10, _startPosY + 35 + 10 + 15/2, _startPosY + 35 + 10}, 3); + } + } +} + diff --git a/DrawingAdvancedCruiser.java b/DrawingAdvancedCruiser.java new file mode 100644 index 0000000..18a53f9 --- /dev/null +++ b/DrawingAdvancedCruiser.java @@ -0,0 +1,77 @@ + +import java.awt.*; + +public class DrawingAdvancedCruiser extends DrawingCruiser{ + EntityAdvancedCruiser EntityCar; + + + public DrawingAdvancedCruiser(int speed, double weight, Color bodyColor, Color additionalColor, boolean bodyKit, boolean tent, boolean ornamentWheels, int width, int height) { + super(speed, weight, bodyColor, width, height, 110, 60); + { + EntityCar = new EntityAdvancedCruiser(speed, weight, bodyColor, additionalColor, bodyKit, tent); + } + } + public DrawingAdvancedCruiser(EntityAdvancedCruiser entityDumpTruck, IDop wheels) + { + super(entityDumpTruck, wheels); + { + EntityCar = entityDumpTruck; + this.wheels = wheels; + } + } + + + public EntityAdvancedCruiser getEntityDumpTruck(){ + return EntityCar; + } + + public void DrawTransport(Graphics g) + { + EntityAdvancedCruiser dumpTruck; + if (EntityCar == null ) + { + return; + } + + + Color addBrush = EntityCar.AdditionalColor; + Color brush = EntityCar.BodyColor; + + super.DrawTransport(g); + if (EntityCar.HelicopterPad) + { + g.setColor(addBrush); + int[] pointsX = new int[3]; + int[] pointsY = new int[3]; + pointsX[0] = _startPosX; pointsY[0] = _startPosY + 35; + pointsX[1] = _startPosX + 85; pointsY[1] = _startPosY; + pointsX[2] = _startPosX + 85; pointsY[2] = _startPosY + 35; + g.fillPolygon(pointsX, pointsY, 3); + + } + + if (EntityCar.Coating) + { + g.setColor(addBrush); + int[] pointsX = new int[4]; + int[] pointsY = new int[4]; + pointsX[0] = _startPosX; pointsY[0] = _startPosY + 35; + pointsX[1] = _startPosX; pointsY[1] = _startPosY; + pointsX[2] = _startPosX + 85; pointsY[2] = _startPosY; + pointsX[3] = _startPosX + 85; pointsY[3] = _startPosY + 35; + g.fillPolygon(pointsX, pointsY, 4); + } + + if (EntityCar.HelicopterPad && EntityCar.Coating) + { + g.setColor(brush); + int x = _startPosX; + int y = _startPosY - 8; + g.fillRect(_startPosX, _startPosY, 95, 3); + + } + + if (wheels == null){return;} + wheels.drawWheels(g, _startPosX, _startPosY, brush); + } +} diff --git a/DrawingCruiser.java b/DrawingCruiser.java new file mode 100644 index 0000000..aa6fc81 --- /dev/null +++ b/DrawingCruiser.java @@ -0,0 +1,170 @@ +import java.awt.*; + +public class DrawingCruiser { + public EntityCruiser EntityCruiser; + + private int _pictureWidth; + + private int _pictureHeight; + + protected int _startPosX; + + protected int _startPosY; + + private int _carWidth = 110; + + private int _carHeight = 60; + + /// + /// Координата X объекта + /// + public int GetPosX(){return _startPosX;}; + /// + /// Координата Y объекта + /// + public int GetPosY(){return _startPosY;} + /// + /// РЁРёСЂРёРЅР° объекта + /// + public int GetWidth(){return _carWidth;}; + /// + /// Высота объекта + /// + public int GetHeight(){return _carHeight;} + public IDop wheels; + + + + + public DrawingCruiser(int speed, double weight, Color bodyColor, int width, int height) + { + if(width < _carWidth || height < _carHeight) + { + return; + } + _pictureWidth = width; + _pictureHeight = height; + + EntityCruiser = new EntityCruiser(speed, weight, bodyColor); + + } + public EntityCruiser getEntity(){ + return EntityCruiser; + } + + protected DrawingCruiser(int speed, double weight, Color bodyColor, int + width, int height, int carWidth, int carHeight) + { + if (width <= _carWidth || height <= _carHeight) + { + return; + } + _pictureWidth = width; + _pictureHeight = height; + _carWidth = carWidth; + _carHeight = carHeight; + EntityCruiser = new EntityCruiser(speed, weight, bodyColor); + } + + public DrawingCruiser(EntityCruiser entityCar, IDop wheels) + { + EntityCruiser = entityCar; + this.wheels = wheels; + _pictureWidth = 1000; + _pictureHeight = 1000; + _carWidth = 110; + _carHeight = 60; + } + + public void SetPosition(int x, int y) + { + if (x < 0 || x >= _pictureWidth || y < 0 || y >= _pictureHeight) + { + _startPosX = 0; + _startPosY = 0; + } + _startPosX = x; + _startPosY = y; + } + + public void MoveTransport(Direction direction) + { + if (!CanMove(direction) || EntityCruiser == null) + { + return; + } + switch (direction) + { + //влево + case Left: + _startPosX -= (int)EntityCruiser.Step(); + break; + //вверх + case Up: + _startPosY -= (int)EntityCruiser.Step(); + break; + // вправо + case Right: + _startPosX += (int)EntityCruiser.Step(); + break; + //РІРЅРёР· + case Down: + _startPosY += (int)EntityCruiser.Step(); + break; + } + } + + public boolean CanMove(Direction direction) + { + if (EntityCruiser == null) + { + return false; + } + switch (direction) + { + //влево + case Left: + return (_startPosX - EntityCruiser.Step()) > 0; + + //вверх + case Up: + return _startPosY - EntityCruiser.Step() > 0; + + // вправо + case Right: + return _startPosX + EntityCruiser.Step() + _carWidth < _pictureWidth; + + //РІРЅРёР· + case Down: + return _startPosY + EntityCruiser.Step() + _carHeight < _pictureHeight; + + default: + return false; + + } + } + + + public void DrawTransport(Graphics g) + { + + if (EntityCruiser == null) + { + return; + } + + g.setColor(EntityCruiser.BodyColor); + + //границы автомобиля + g.fillRect(_startPosX, _startPosY + 35, 110, 10); + g.fillRect( _startPosX + 85, _startPosY, 25, 35); + /*g.fillOval( _startPosX, _startPosY + 35 + 10, 15, 15); + g.fillOval( _startPosX + 15, _startPosY + 35 + 10, 15, 15); + g.fillOval( _startPosX + 95, _startPosY + 35 + 10, 15, 15);*/ + + if (wheels == null){return;} + wheels.drawWheels(g, _startPosX, _startPosY, EntityCruiser.BodyColor); + } + public IMoveableObject GetMoveableObject(){return new DrawingObjectCruiser(this);} + +} diff --git a/DrawingObjectCruiser.java b/DrawingObjectCruiser.java new file mode 100644 index 0000000..ef171ce --- /dev/null +++ b/DrawingObjectCruiser.java @@ -0,0 +1,29 @@ +public class DrawingObjectCruiser implements IMoveableObject { + private DrawingCruiser _drawningCruiser = null; + + public DrawingObjectCruiser(DrawingCruiser drawningCruiser) { + _drawningCruiser = drawningCruiser; + } + + public ObjectParameters GetObjectPosition() { + if (_drawningCruiser == null || _drawningCruiser.EntityCruiser == null) { + return null; + } + return new ObjectParameters(_drawningCruiser.GetPosX(), _drawningCruiser.GetPosY(), _drawningCruiser.GetWidth(), _drawningCruiser.GetHeight()); + } + + public int GetStep() { + return (int) (_drawningCruiser.EntityCruiser.Step()); + } + + public boolean CheckCanMove(Direction direction) { + if (_drawningCruiser.CanMove(direction)){ + return true; + } + return false; + } + + public void MoveObject(Direction direction) { + _drawningCruiser.MoveTransport(direction); + } +} diff --git a/DrawningCruiser.java b/DrawningCruiser.java deleted file mode 100644 index 24e3019..0000000 --- a/DrawningCruiser.java +++ /dev/null @@ -1,108 +0,0 @@ -import java.awt.*; -public class DrawningCruiser { - public EntityCruiser EntityCruiser; - private int _pictureWidth; - private int _pictureHeight; - private int _startPosX; - private int _startPosY; - final private int _cruiserWidth = 110; - final private int _cruiserHeight = 60; - private NumberOfWheels wheels = new NumberOfWheels(); - public boolean Init(int speed, double weight, Color bodyColor, Color additionalColor, boolean helicopterPad, boolean coating, int width, int height) - { - _pictureWidth = width; - _pictureHeight = height; - EntityCruiser = new EntityCruiser(); - EntityCruiser.Init(speed, weight, bodyColor, additionalColor, helicopterPad, coating); - return true; - } - public void SetPosition(int x, int y) - { - if (EntityCruiser == null) { - return; - } - _startPosX = x; - _startPosY = y; - if (x + _cruiserWidth <= _pictureWidth || y + _cruiserHeight <= _pictureHeight) { - _startPosX = 0; - _startPosY = 0; - } - } - public void MoveTransport(Direction direction) - { - if (EntityCruiser == null) - { - return; - } - switch (direction) - { - case Left: - if (_startPosX - EntityCruiser.Step() > 0) - { - _startPosX -= (int)EntityCruiser.Step(); - } - break; - case Up: - if (_startPosY - EntityCruiser.Step() > 0) - { - _startPosY -= (int)EntityCruiser.Step(); - } - break; - case Right: - if (_startPosX + EntityCruiser.Step() + _cruiserWidth < _pictureWidth) - { - _startPosX += (int)EntityCruiser.Step(); - } - break; - case Down: - if (_startPosY + EntityCruiser.Step() + _cruiserHeight < _pictureHeight) - { - _startPosY += (int)EntityCruiser.Step(); - } - break; - } - } - public void setWheels(int num){ - wheels.setNumOfWheels(Integer.toString(num)); - } - public void DrawTransport(Graphics g) - { - if (EntityCruiser == null) - { - return; - } - g.setColor(EntityCruiser.BodyColor()); - g.drawRect( _startPosX + 9, _startPosY + 15, 10, 30); - g.drawRect( _startPosX + 90, _startPosY + 15, 10, - 30); - g.drawRect( _startPosX + 20, _startPosY + 4, 70, 52); - g.fillRect( _startPosX + 10, _startPosY + 15, 10, 30); - g.fillRect( _startPosX + 90, _startPosY + 15, 10, 30); - g.fillRect( _startPosX + 20, _startPosY + 5, 70, 50); - Point[] points1 = new Point[3]; - points1[0] = new Point(_startPosX + 100, _startPosY + 5); - points1[1] = new Point(_startPosX + 100, _startPosY + 55); - points1[2] = new Point(_startPosX + 150, _startPosY + 30); - g.fillPolygon(new int[] {points1[0].x, points1[1].x, points1[2].x}, - new int[] {points1[0].y, points1[1].y, points1[2].y}, 3); - g.fillRect( _startPosX + 5, _startPosY + 15, 10, 10); - g.fillRect( _startPosX + 5, _startPosY + 35, 10, 10); - if (EntityCruiser.HelicopterPad()) - { - g.setColor(EntityCruiser.AdditionalColor()); - g.drawRect(_startPosX + 35, - _startPosY + 23, 15, 15); - g.drawRect(_startPosX + 50, - _startPosY + 19, 30, 25); - } - if (EntityCruiser.Coating()) - { - g.setColor(EntityCruiser.AdditionalColor()); - g.fillRect(_startPosX + 35, - _startPosY + 23, 15, 15); - g.fillRect(_startPosX + 50, - _startPosY + 19, 30, 25); - } - wheels.drawWheels(g, _startPosX, _startPosY, EntityCruiser.BodyColor()); - } -} \ No newline at end of file diff --git a/EntityAdvancedCruiser.java b/EntityAdvancedCruiser.java new file mode 100644 index 0000000..352e8a3 --- /dev/null +++ b/EntityAdvancedCruiser.java @@ -0,0 +1,31 @@ +import java.awt.*; + +public class EntityAdvancedCruiser extends EntityCruiser{ + /// + /// Дополнительный цвет (для опциональных элементов) + /// + public Color AdditionalColor; + + /// + /// Признак (опция) наличия кузова + /// + public boolean HelicopterPad ; + + /// + /// Признак (опция) наличия tent + /// + public boolean Coating ; + + + public EntityAdvancedCruiser(int speed, double weight, Color bodyColor, Color + additionalColor, boolean helicopterPad, boolean coating) + { + super(speed, weight, bodyColor); + AdditionalColor = additionalColor; + HelicopterPad = helicopterPad; + Coating = coating; + + } +} + + diff --git a/EntityCruiser.java b/EntityCruiser.java index 0c33c7b..b30d1cb 100644 --- a/EntityCruiser.java +++ b/EntityCruiser.java @@ -1,47 +1,16 @@ import java.awt.*; public class EntityCruiser { - private int Speed; - private double Weight; - private Color BodyColor; - private Color AdditionalColor; - private boolean HelicopterPad; - private boolean Coating; - private double Step; - public int Speed() { - return Speed; - } - public double Weight() { - return Weight; - } - - public Color BodyColor() { - return BodyColor; - } - - public Color AdditionalColor() { - return AdditionalColor; - } - - public boolean HelicopterPad() { - return HelicopterPad; - } - - public boolean Coating() { - return Coating; - } - public double Step() { - return Step; - } - public void Init(int speed, double weight, Color bodyColor, Color - additionalColor, boolean helicopterPad, boolean coating) + public int Speed; + public double Weight; + public Color BodyColor; + public double Step() {return (double)Speed * 100 / Weight;}; + public boolean OrnamentWheels; + + public EntityCruiser(int speed, double weight, Color bodyColor) { Speed = speed; Weight = weight; BodyColor = bodyColor; - AdditionalColor = additionalColor; - HelicopterPad = helicopterPad; - Coating = coating; - Step = Speed * 100 / Weight; } } \ No newline at end of file diff --git a/GameFrame.java b/GameFrame.java index 0d0d956..1f1c42e 100644 --- a/GameFrame.java +++ b/GameFrame.java @@ -6,39 +6,164 @@ import java.awt.event.ActionListener; import java.util.Random; public class GameFrame extends JFrame { + GameFrame() { this.setSize(710, 535); - this.setTitle("Cruiser"); + this.setTitle("DumpTruck"); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setResizable(false); this.setLocationRelativeTo(null); + GamePanel panel = new GamePanel(); this.add(panel); + this.setVisible(true); } + private boolean intToBoolean(int input) { return input != 0; } - public class GamePanel extends JPanel implements ActionListener { + + public class GamePanel extends JPanel { static final int SCREEN_W = 700; static final int SCREEN_H = 500; int xx = 0; int yy = 0; - private DrawningCruiser _drawningCruiser; + private DrawingAdvancedCruiser _drawningDumpTruck; + private DrawingCruiser _drawningCar; + private AbstractStrategy _abstractStrategy; + GamePanel() { this.setLayout(null); this.setPreferredSize(new Dimension(SCREEN_W, SCREEN_H)); GridBagConstraints layers = new GridBagConstraints(); - JButton button = new JButton("Создать") ; - button.setBounds(20, 390, 90, 30); - this.add(button); - button.addActionListener(this); + + JButton buttonCar = new JButton("Создать грузовик"); + buttonCar.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + Random random = new Random(); + _drawningCar = new DrawingCruiser(random.nextInt(100, 300), + random.nextInt(1000, 3000), + Color.getHSBColor(random.nextInt(0, 256), random.nextInt(0, 256), + random.nextInt(0, 256)), + GamePanel.SCREEN_W, GamePanel.SCREEN_H); + _drawningCar.SetPosition(random.nextInt(10, 100), random.nextInt(10, + 100)); + + int ornament = random.nextInt(1, 4); + switch (ornament){ + case 1: + _drawningCar.wheels = new NumberOfWheels(random.nextInt(2, 5)); + break; + case 2: + _drawningCar.wheels = new DopClassRect(random.nextInt(2, 5)); + break; + default: + _drawningCar.wheels = new DopClassTriangle(random.nextInt(2, 5)); + break; + } + repaint(); + } + }); + buttonCar.setBounds(20 + 150, 390, 120, 30); + this.add(buttonCar); + + JButton buttonDumpCar = new JButton("Создать самосвал"); + buttonDumpCar.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + Random random = new Random(); + System.out.println("F"); + _drawningCar = new DrawingAdvancedCruiser(random.nextInt(100, 300), + random.nextInt(1000, 3000), + Color.getHSBColor(random.nextInt(0, 256), random.nextInt(0, 256), + random.nextInt(0, 256)), Color.getHSBColor(random.nextInt(0, 256), random.nextInt(0, 256), + random.nextInt(0, 256)), random.nextBoolean(),random.nextBoolean(),random.nextBoolean(), + GamePanel.SCREEN_W, GamePanel.SCREEN_H); //TODO + _drawningCar.SetPosition(random.nextInt(10, 100), random.nextInt(10, + 100)); + + int ornament = random.nextInt(1, 4); + switch (ornament){ + case 1: + _drawningCar.wheels = new NumberOfWheels(random.nextInt(2, 5)); + break; + case 2: + _drawningCar.wheels = new DopClassRect(random.nextInt(2, 5)); + break; + default: + _drawningCar.wheels = new DopClassTriangle(random.nextInt(2, 5)); + break; + } + + + repaint(); + } + }); + buttonDumpCar.setBounds( 20, 390, 120, 30); + this.add(buttonDumpCar); + + JTextField textStrategy = new JTextField(); + textStrategy.setBounds(550, 20, 120, 30); + this.add(textStrategy); + + JButton buttonStep = new JButton("Шаг"); + buttonStep.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (_drawningCar == null) + { + return; + } + if (textStrategy.getText() != null) + { + switch(textStrategy.getText()) + { + case "center": + + _abstractStrategy = new MoveToCenter(); + break; + case "border": + _abstractStrategy = new MoveToBorder(); + break; + }; + + if (_abstractStrategy == null) + { + + return; + } + _abstractStrategy.SetData(new DrawingObjectCruiser(_drawningCar), SCREEN_W, SCREEN_H); + //textStrategy.setText(); + } + if (_abstractStrategy == null) + { + + return; + } + System.out.println("step"); + _abstractStrategy.MakeStep(); + repaint(); + if (_abstractStrategy.GetStatus() == Status.Finish) + { + //comboBoxStrategy.Enabled = true; + _abstractStrategy = null; + } + } + }); + buttonStep.setBounds(550, 60, 70, 30); + this.add(buttonStep); + + + JPanel panel = new JPanel(new GridBagLayout()); + JButton up = new BasicArrowButton(BasicArrowButton.NORTH); up.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - _drawningCruiser.MoveTransport(Direction.Up); + _drawningCar.MoveTransport(Direction.Up); repaint(); } }); @@ -46,7 +171,7 @@ public class GameFrame extends JFrame { left.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - _drawningCruiser.MoveTransport(Direction.Left); + _drawningCar.MoveTransport(Direction.Left); repaint(); } }); @@ -54,7 +179,7 @@ public class GameFrame extends JFrame { down.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - _drawningCruiser.MoveTransport(Direction.Down); + _drawningCar.MoveTransport(Direction.Down); repaint(); } }); @@ -62,45 +187,53 @@ public class GameFrame extends JFrame { right.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - _drawningCruiser.MoveTransport(Direction.Right); + _drawningCar.MoveTransport(Direction.Right); repaint(); } }); + + up.setBounds(570, 350, 30, 30); this.add(up); + down.setBounds(570, 350 + 40, 30, 30); this.add(down); + left.setBounds(570 - 40, 350 + 40, 30, 30); this.add(left); + right.setBounds(570 + 40, 350 + 40, 30, 30); this.add(right); } + + void setTruck(DrawingAdvancedCruiser _truck) { + _drawningDumpTruck = _truck; + } + + private void Draw() + { + if (_drawningCar == null) + { + return; + } + + Graphics gr =new DebugGraphics(); + _drawningCar.DrawTransport(gr); + + } @Override public void paintComponent(Graphics g) { super.paintComponent(g); draw(g); } - public void draw(Graphics g){ - if (_drawningCruiser != null){ - _drawningCruiser.DrawTransport(g); + + public void draw(Graphics g) { + if (_drawningCar != null) { + _drawningCar.DrawTransport(g); } } - @Override - public void actionPerformed(ActionEvent e) { - Random random = new Random(); - _drawningCruiser = new DrawningCruiser(); - _drawningCruiser.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)), - intToBoolean(random.nextInt(0, 2)), - intToBoolean(random.nextInt(0, 2)), - GamePanel.SCREEN_W, GamePanel.SCREEN_H); - _drawningCruiser.SetPosition(random.nextInt(10, 100), - random.nextInt(10, 100)); - _drawningCruiser.setWheels(random.nextInt(1, 5)); - repaint(); - }; } -} \ No newline at end of file +} + + diff --git a/IDop.java b/IDop.java new file mode 100644 index 0000000..944863a --- /dev/null +++ b/IDop.java @@ -0,0 +1,7 @@ +import java.awt.*; + +public interface IDop { + public void setNumOfWheels(String num); + public void drawWheels(Graphics g, int _startPosX, int _startPosY, Color c); + +} \ No newline at end of file diff --git a/IMoveableObject.java b/IMoveableObject.java new file mode 100644 index 0000000..1fd5c4a --- /dev/null +++ b/IMoveableObject.java @@ -0,0 +1,17 @@ +public interface IMoveableObject { + /// + /// Получение координаты X объекта + /// + public ObjectParameters GetObjectPosition(); + /// + /// Шаг объекта + /// + public int GetStep(); + + boolean CheckCanMove(Direction direction); + /// + /// Изменение направления пермещения объекта + /// + /// Направление + void MoveObject(Direction direction); +} \ No newline at end of file diff --git a/MoveToBorder.java b/MoveToBorder.java new file mode 100644 index 0000000..e63d8bb --- /dev/null +++ b/MoveToBorder.java @@ -0,0 +1,37 @@ +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 = FieldWidth - objParams.ObjectMiddleHorizontal(); + if (Math.abs(diffX) > GetStep()) { + + MoveRight(); + + } + var diffY = FieldHeight - objParams.ObjectMiddleVertical(); + if (Math.abs(diffY) > GetStep()) { + + MoveDown(); + + } + } + +} + diff --git a/MoveToCenter.java b/MoveToCenter.java new file mode 100644 index 0000000..47fc647 --- /dev/null +++ b/MoveToCenter.java @@ -0,0 +1,44 @@ +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/NumberOfWheels.java b/NumberOfWheels.java index d2af550..d48dc38 100644 --- a/NumberOfWheels.java +++ b/NumberOfWheels.java @@ -1,6 +1,10 @@ import java.awt.*; -public class NumberOfWheels { + +public class NumberOfWheels implements IDop{ public int numOfWheels; + public NumberOfWheels(int num){ + numOfWheels = num; + } public void setNumOfWheels(String num){ switch (Integer.valueOf(num)){ case 2: @@ -16,20 +20,16 @@ public class NumberOfWheels { numOfWheels = NumberOfWheelsEnum.wheel_2.value; } } + public void drawWheels(Graphics g, int _startPosX, int _startPosY, Color c) { - g.setColor(Color.black); - if (numOfWheels == 2) { - g.fillOval(_startPosX+20, _startPosY + 20, 15, 15); - g.fillOval(_startPosX + 45, _startPosY + 20, 15, 15); - } else if (numOfWheels == 3) { - g.fillOval(_startPosX, _startPosY + 20, 15, 15); - g.fillOval(_startPosX + 25, _startPosY + 20, 15, 15); - g.fillOval(_startPosX + 55, _startPosY + 20, 15, 15); - } else { - g.fillOval(_startPosX, _startPosY + 20, 15, 15); - g.fillOval(_startPosX + 15, _startPosY + 20, 15, 15); - g.fillOval(_startPosX + 65 , _startPosY + 20, 15, 15); - g.fillOval(_startPosX + 85, _startPosY + 20, 15, 15); + g.setColor(c); + if (numOfWheels >= 2) { + g.fillOval(_startPosX, _startPosY + 35 + 10, 15, 15); + g.fillOval(_startPosX + 95, _startPosY + 35 + 10, 15, 15); + } if (numOfWheels >= 3) { + g.fillOval(_startPosX + 15, _startPosY + 35 + 10, 15, 15); + } if(numOfWheels >= 4){ + g.fillOval(_startPosX + 95 - 20, _startPosY + 35 + 10, 15, 15); } } -} \ No newline at end of file +} diff --git a/NumberOfWheelsEnum.java b/NumberOfWheelsEnum.java index 53b6b5a..a862ddf 100644 --- a/NumberOfWheelsEnum.java +++ b/NumberOfWheelsEnum.java @@ -3,6 +3,7 @@ public enum NumberOfWheelsEnum { wheel_3(3), wheel_4(4); public int value; + NumberOfWheelsEnum(int value) { this.value = value; } diff --git a/ObjectParameters.java b/ObjectParameters.java new file mode 100644 index 0000000..ea3174c --- /dev/null +++ b/ObjectParameters.java @@ -0,0 +1,49 @@ +public class ObjectParameters { + + private final int _x; + private final int _y; + private final int _width; + private final int _height; + + + /// + /// Левая граница + /// + public int LeftBorder(){return _x;} + + /// + /// Верхняя граница + /// + public int TopBorder(){return _y;} + /// + /// Правая граница + /// + 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;} + /// + /// Конструктор + /// + /// Координата X + /// Координата Y + /// Ширина + /// Высота + public ObjectParameters(int x, int y, int width, int height) + { + _x = x; + _y = y; + _width = width; + _height = height; + } +} + diff --git a/Status.java b/Status.java new file mode 100644 index 0000000..1df7125 --- /dev/null +++ b/Status.java @@ -0,0 +1,8 @@ +public enum Status { + + + NotInit, + InProgress, + Finish + +} \ No newline at end of file