diff --git a/AircraftCarrier/src/AbstractStrategy.java b/AircraftCarrier/src/AbstractStrategy.java new file mode 100644 index 0000000..4a70f71 --- /dev/null +++ b/AircraftCarrier/src/AbstractStrategy.java @@ -0,0 +1,57 @@ +import java.util.function.Supplier; + +public abstract class AbstractStrategy { + private IMoveableObject moveableObject; + private Status state = Status.NOTINIT; + private int FieldWidth; + protected int getFieldWidth(){return FieldWidth;} + private int FieldHeight; + protected int getFieldHeight(){return 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; + this.moveableObject = moveableObject; + FieldWidth = width; + FieldHeight = height; + } + + public void MakeStep(){ + if(state != Status.INPROGRESS) + return; + if(IsTargetDestination()){ + state = Status.FINISH; + return; + } + MoveToTarget(); + } + + protected boolean MoveLeft() {return MoveTo(DirectionType.LEFT);} + protected boolean MoveRight() {return MoveTo(DirectionType.RIGHT);} + protected boolean MoveUp() {return MoveTo(DirectionType.UP);} + protected boolean MoveDown() {return MoveTo(DirectionType.DOWN);} + protected Supplier getObjectParameters = () -> moveableObject.getObjectPosition(); + + protected Integer GetStep(){ + if(state != Status.INPROGRESS) + return null; + return moveableObject.getStep(); + } + + protected abstract void MoveToTarget(); + protected abstract boolean IsTargetDestination(); + + private boolean MoveTo(DirectionType direction){ + if(state != Status.INPROGRESS) + return false; + if(moveableObject.checkCanMove(direction)){ + moveableObject.moveObject(direction); + return true; + } + return false; + } +} diff --git a/AircraftCarrier/src/DrawingAircraft.java b/AircraftCarrier/src/DrawingAircraft.java new file mode 100644 index 0000000..6c93614 --- /dev/null +++ b/AircraftCarrier/src/DrawingAircraft.java @@ -0,0 +1,120 @@ +import java.awt.*; + +public class DrawingAircraft { + protected EntityAircraft entityAircraft ; + protected void setEntityShip(EntityAircraft entityShip){ + this.entityAircraft = entityAircraft; + } + public EntityAircraft getEntityShip() { + return entityAircraft; + } + private IDrawImprovements drawingImprovements; + private int _pictureWidth; + private int _pictureHeight; + protected int _startPosX; + public int getPosX(){ + return _startPosX; + } + protected int _startPosY; + public int getPosY(){ + return _startPosY; + } + private final int _ShipWidth = 164; + public int getWidth() { + return _ShipWidth; + } + private final int _ShipHeight = 40; + public int getHeight(){ + return _ShipHeight; + } + public DrawingAircraft(int speed, double weight, Color bodyColor, int width, int height, int improvementsType, int improvementsNumber) { + if (width < _ShipWidth || height < _ShipHeight) + return; + _pictureWidth = width; + _pictureHeight = height; + entityAircraft = new EntityAircraft(speed, weight, bodyColor); + switch (improvementsType){ + case 1: + drawingImprovements = new DrawingImprovementsSquare(); + break; + case 2: + drawingImprovements = new DrawingImprovementsCircle(); + break; + default: + drawingImprovements = new DrawingImprovementsTriangle(); + break; + } + drawingImprovements.setNumber(improvementsNumber); + } + public void setPosition(int x, int y) + { + if (_startPosX > (_pictureWidth - _ShipWidth) || _startPosY>(_pictureHeight - _ShipHeight) || x <= 0 || y <= 0) + x = y = 2; + _startPosX = x; + _startPosY = y; + } + public void drawTransport(Graphics2D g) { + if (entityAircraft == null) + return; + Color penColor = Color.BLACK; + Color bodyColor = entityAircraft.getBodyColor(); + g.setColor(penColor); + g.drawLine(_startPosX+4, _startPosY, _startPosX + 124, _startPosY); + g.drawLine(_startPosX+124, _startPosY, _startPosX + 164, _startPosY+20); + g.drawLine(_startPosX + 164, _startPosY+20, _startPosX + 124, _startPosY + 40); + g.drawLine(_startPosX + 164, _startPosY + 20, _startPosX + 124, _startPosY + 40); + g.drawLine(_startPosX + 124, _startPosY + 40, _startPosX + 4, _startPosY + 40); + g.drawLine(_startPosX+4, _startPosY + 40, _startPosX + 4, _startPosY); + g.setColor(bodyColor); + g.fillRect(_startPosX, _startPosY + 6, 4, 12); + g.fillRect(_startPosX, _startPosY + 24, 4, 12); + g.setColor(penColor); + g.drawOval(_startPosX + 115, _startPosY + 13, 14, 14); + g.drawRect(_startPosX + 63, _startPosY + 13, 27, 14); + g.drawRect(_startPosX + 90, _startPosY + 9, 14, 22); + g.setColor(bodyColor); + drawingImprovements.drawImprovements(g, _startPosX, _startPosY); + } + public boolean canMove(DirectionType direction) { + if (entityAircraft == null) { + return false; + } + switch(direction) { + case LEFT: + return _startPosX - entityAircraft.step.get().intValue() > 0; + case UP: + return _startPosY - entityAircraft.step.get().intValue() > 0; + case RIGHT: + return _startPosX + entityAircraft.step.get().intValue() + _ShipWidth < _pictureWidth; + case DOWN: + return _startPosY + entityAircraft.step.get().intValue() + _ShipWidth < _pictureWidth; + default: + return false; + } + } + public void moveTransport(DirectionType direction) + { + if (entityAircraft == null) + return; + int step = entityAircraft.step.get().intValue(); + switch (direction) + { + case LEFT: + if (_startPosX - step > 0) + _startPosX -= step; + break; + case UP: + if (_startPosY - step > 0) + _startPosY -= step; + break; + case RIGHT: + if (_startPosX + _ShipWidth + step < _pictureWidth) + _startPosX += step; + break; + case DOWN: + if (_startPosY + _ShipHeight + step < _pictureHeight) + _startPosY += step; + break; + } + } +} diff --git a/AircraftCarrier/src/DrawingAircraftCarrier.java b/AircraftCarrier/src/DrawingAircraftCarrier.java index 9ef6710..10a2846 100644 --- a/AircraftCarrier/src/DrawingAircraftCarrier.java +++ b/AircraftCarrier/src/DrawingAircraftCarrier.java @@ -1,87 +1,20 @@ -import javax.swing.*; + import java.awt.*; -public class DrawingAircraftCarrier extends JPanel { - private EntityAircraftCarrier entityAircraftCarrier; - public EntityAircraftCarrier getEntityAircraftCarrier(){ - return entityAircraftCarrier; +public class DrawingAircraftCarrier extends DrawingAircraft { + public DrawingAircraftCarrier(int speed, double weight, Color bodyColor, + Color additionalColor, boolean cabin, boolean runway, int width, int height, int improvementsType, int improvementsNumber){ + super(speed, weight, bodyColor, width, height, improvementsType, improvementsNumber); + if(entityAircraft != null) + entityAircraft = new EntityAircraftCarrier(speed, weight, bodyColor, additionalColor,cabin, runway); } - private int _pictureWidth; - private int _pictureHeight; - private int _startPosX; - private int _startPosY; - private final int _ShipWidth = 164; - private final int _ShipHeight = 40; - private DrawingImprovements drawingImprovements; - public boolean init(int speed, double weight, Color bodyColor, Color - additionalColor, boolean Cabin, boolean Runway, int width, int height, int improvementsNumber) + public void drawTransport(Graphics2D g) { - if (_ShipWidth > width || _ShipHeight > height) - return false; - _pictureWidth = width; - _pictureHeight = height; - entityAircraftCarrier = new EntityAircraftCarrier(); - entityAircraftCarrier.init(speed, weight, bodyColor, additionalColor, - Cabin, Runway); - drawingImprovements = new DrawingImprovements(); - drawingImprovements.setNumber(improvementsNumber); - return true; - } - public void setPosition(int x, int y) - { - if (_startPosX > (_pictureWidth - _ShipWidth) || _startPosY>(_pictureHeight - _ShipHeight) || x <= 0 || y <= 0) - x = y = 2; - _startPosX = x; - _startPosY = y; - } - public void moveTransport(DirectionType direction) - { - if (entityAircraftCarrier == null) + if (!(entityAircraft instanceof EntityAircraftCarrier)) return; - int step = entityAircraftCarrier.step.get().intValue(); - switch (direction) - { - case LEFT: - if (_startPosX - step > 0) - _startPosX -= step; - break; - case UP: - if (_startPosY - step > 0) - _startPosY -= step; - break; - case RIGHT: - if (_startPosX + _ShipWidth + step < _pictureWidth) - _startPosX += step; - break; - case DOWN: - if (_startPosY + _ShipHeight + step < _pictureHeight) - _startPosY += step; - break; - } - } - public void drawTransport(Graphics gr) - { - super.paintComponent(gr); - Graphics2D g = (Graphics2D) gr; - if (entityAircraftCarrier == null) - return; - Color penColor = Color.BLACK; - Color bodyColor = entityAircraftCarrier.getBodyColor(); + EntityAircraftCarrier entityAircraftCarrier = (EntityAircraftCarrier) entityAircraft; Color additionalColor = entityAircraftCarrier.getAdditionalColor(); - g.setColor(penColor); - g.drawLine(_startPosX+4, _startPosY, _startPosX + 124, _startPosY); - g.drawLine(_startPosX+124, _startPosY, _startPosX + 164, _startPosY+20); - g.drawLine(_startPosX + 164, _startPosY+20, _startPosX + 124, _startPosY + 40); - g.drawLine(_startPosX + 164, _startPosY + 20, _startPosX + 124, _startPosY + 40); - g.drawLine(_startPosX + 124, _startPosY + 40, _startPosX + 4, _startPosY + 40); - g.drawLine(_startPosX+4, _startPosY + 40, _startPosX + 4, _startPosY); - g.setColor(bodyColor); - g.fillRect(_startPosX, _startPosY + 6, 4, 12); - g.fillRect(_startPosX, _startPosY + 24, 4, 12); - g.setColor(penColor); - g.drawOval(_startPosX + 115, _startPosY + 13, 14, 14); - g.drawRect(_startPosX + 63, _startPosY + 13, 27, 14); - g.drawRect(_startPosX + 90, _startPosY + 9, 14, 22); + super.drawTransport(g); if(entityAircraftCarrier.getRunway()){ g.setColor(additionalColor); g.fillRect(_startPosX + 4, _startPosY + 13, 59, 14); @@ -96,7 +29,5 @@ public class DrawingAircraftCarrier extends JPanel { g.setColor(additionalColor); g.fillRect(_startPosX + 90, _startPosY, 15, 9); } - g.setColor(bodyColor); - drawingImprovements.drawEngines(g, _startPosX, _startPosY); } } \ No newline at end of file diff --git a/AircraftCarrier/src/DrawingImprovementsCircle.java b/AircraftCarrier/src/DrawingImprovementsCircle.java new file mode 100644 index 0000000..d0b5aa0 --- /dev/null +++ b/AircraftCarrier/src/DrawingImprovementsCircle.java @@ -0,0 +1,24 @@ +import java.awt.*; +public class DrawingImprovementsCircle implements IDrawImprovements{ + private NumberImprovement number; + public void setNumber(int x){ + if(x <= 2) + number = NumberImprovement.TWO; + if(x == 4) + number = NumberImprovement.FOUR; + if(x >= 6) + number = NumberImprovement.SIX; + } + public void drawImprovements(Graphics2D g, int _startPosX, int _startPosY){ + g.fillOval(_startPosX+5, _startPosY+1, 15, 12); + g.fillOval(_startPosX+5, _startPosY+27, 15, 13); + if (number == NumberImprovement.FOUR || number == NumberImprovement.SIX){ + g.fillOval(_startPosX+26, _startPosY+1, 15, 12); + g.fillOval(_startPosX+26, _startPosY+27, 15, 13); + } + if (number == NumberImprovement.SIX){ + g.fillOval(_startPosX+48, _startPosY+1, 15, 12); + g.fillOval(_startPosX+48, _startPosY+27, 15, 13); + } + } +} diff --git a/AircraftCarrier/src/DrawingImprovementsSquare.java b/AircraftCarrier/src/DrawingImprovementsSquare.java new file mode 100644 index 0000000..93a71c2 --- /dev/null +++ b/AircraftCarrier/src/DrawingImprovementsSquare.java @@ -0,0 +1,25 @@ +import java.awt.*; + +public class DrawingImprovementsSquare implements IDrawImprovements{ + private NumberImprovement number; + public void setNumber(int x){ + if(x <= 2) + number = NumberImprovement.TWO; + if(x == 4) + number = NumberImprovement.FOUR; + if(x >= 6) + number = NumberImprovement.SIX; + } + public void drawImprovements(Graphics2D g, int _startPosX, int _startPosY){ + g.fillRect(_startPosX+5, _startPosY+1, 15, 12); + g.fillRect(_startPosX+5, _startPosY+27, 15, 13); + if (number == NumberImprovement.FOUR || number == NumberImprovement.SIX){ + g.fillRect(_startPosX+26, _startPosY+1, 15, 12); + g.fillRect(_startPosX+26, _startPosY+27, 15, 13); + } + if (number == NumberImprovement.SIX){ + g.fillRect(_startPosX+48, _startPosY+1, 15, 12); + g.fillRect(_startPosX+48, _startPosY+27, 15, 13); + } + } +} \ No newline at end of file diff --git a/AircraftCarrier/src/DrawingImprovementsTriangle.java b/AircraftCarrier/src/DrawingImprovementsTriangle.java new file mode 100644 index 0000000..1f34d70 --- /dev/null +++ b/AircraftCarrier/src/DrawingImprovementsTriangle.java @@ -0,0 +1,36 @@ +import java.awt.*; +public class DrawingImprovementsTriangle implements IDrawImprovements{ + private NumberImprovement number; + public void setNumber(int x){ + if(x <= 2) + number = NumberImprovement.TWO; + if(x == 4) + number = NumberImprovement.FOUR; + if(x >= 6) + number = NumberImprovement.SIX; + } + public void drawImprovements(Graphics2D g, int _startPosX, int _startPosY){ + int[] pointX = new int[]{ _startPosX+5, _startPosX+13, _startPosX+ 21}; + int[] pointY = new int[]{ _startPosY + 1, _startPosY+13, _startPosY+1}; + g.fillPolygon(pointX, pointY, 3); + pointX = new int[]{ _startPosX+5, _startPosX+13, _startPosX+ 21}; + pointY = new int[]{ _startPosY + 40, _startPosY+27, _startPosY+40}; + g.fillPolygon(pointX, pointY, 3); + if (number == NumberImprovement.FOUR || number == NumberImprovement.SIX){ + pointX = new int[]{ _startPosX+26, _startPosX+34, _startPosX+ 42}; + pointY = new int[]{ _startPosY + 1, _startPosY+13, _startPosY+1}; + g.fillPolygon(pointX, pointY, 3); + pointX = new int[]{ _startPosX+26, _startPosX+34, _startPosX+ 42}; + pointY = new int[]{ _startPosY + 40, _startPosY+27, _startPosY+40}; + g.fillPolygon(pointX, pointY, 3); + } + if (number == NumberImprovement.SIX){ + pointX = new int[]{ _startPosX+47, _startPosX+55, _startPosX+ 63}; + pointY = new int[]{ _startPosY + 1, _startPosY+13, _startPosY+1}; + g.fillPolygon(pointX, pointY, 3); + pointX = new int[]{ _startPosX+47, _startPosX+55, _startPosX+ 63}; + pointY = new int[]{ _startPosY + 40, _startPosY+27, _startPosY+40}; + g.fillPolygon(pointX, pointY, 3); + } + } +} \ No newline at end of file diff --git a/AircraftCarrier/src/DrawingObjectAircraftCarrier.java b/AircraftCarrier/src/DrawingObjectAircraftCarrier.java new file mode 100644 index 0000000..51703f8 --- /dev/null +++ b/AircraftCarrier/src/DrawingObjectAircraftCarrier.java @@ -0,0 +1,27 @@ +public class DrawingObjectAircraftCarrier implements IMoveableObject{ + private final DrawingAircraft drawingAircraft; + + public DrawingObjectAircraftCarrier(DrawingAircraft drawingShip){ + this.drawingAircraft = drawingShip; + } + public ObjectParameters getObjectPosition(){ + if(drawingAircraft == null || drawingAircraft.getEntityShip() == null) + return null; + return new ObjectParameters(drawingAircraft.getPosX(), drawingAircraft.getPosY(), + drawingAircraft.getWidth(), drawingAircraft.getHeight()); + } + public int getStep(){ + if(drawingAircraft.getEntityShip() == null) + return 0; + return drawingAircraft.getEntityShip().step.get().intValue(); + } + public boolean checkCanMove(DirectionType direction){ + if(drawingAircraft == null) + return false; + return drawingAircraft.canMove(direction); + } + public void moveObject(DirectionType direction){ + drawingAircraft.moveTransport(direction); + } +} + diff --git a/AircraftCarrier/src/EntityAircraft.java b/AircraftCarrier/src/EntityAircraft.java new file mode 100644 index 0000000..36122ae --- /dev/null +++ b/AircraftCarrier/src/EntityAircraft.java @@ -0,0 +1,24 @@ +import java.awt.*; +import java.util.function.Supplier; + +public class EntityAircraft { + private int speed; + public int getSpeed(){ + return speed; + } + private double weight; + public double getWeight(){ + return weight; + } + private Color bodyColor; + public Color getBodyColor(){ + return bodyColor; + } + public Supplier step = () ->(double) speed * 100 / weight; + + public EntityAircraft(int speed, double weight, Color bodyColor){ + this.speed = speed; + this.weight = weight; + this.bodyColor = bodyColor; + } +} diff --git a/AircraftCarrier/src/EntityAircraftCarrier.java b/AircraftCarrier/src/EntityAircraftCarrier.java index 5c90f21..85df120 100644 --- a/AircraftCarrier/src/EntityAircraftCarrier.java +++ b/AircraftCarrier/src/EntityAircraftCarrier.java @@ -1,19 +1,6 @@ import java.awt.*; -import java.util.function.Supplier; -public class EntityAircraftCarrier { - private int speed; - public int getSpeed(){ - return speed; - } - private double weight; - public double getWeight(){ - return weight; - } - private Color bodyColor; - public Color getBodyColor(){ - return bodyColor; - } +public class EntityAircraftCarrier extends EntityAircraft{ private Color additionalColor; public Color getAdditionalColor(){ return additionalColor; @@ -26,12 +13,9 @@ public class EntityAircraftCarrier { public boolean getRunway() { return Runway; } - public Supplier step = () -> (double) speed * 100 / weight; - public void init(int speed, double weight, Color bodyColor, Color + public EntityAircraftCarrier(int speed, double weight, Color bodyColor, Color additionalColor, boolean Cabin, boolean Runway) { - this.speed = speed; - this.weight = weight; - this.bodyColor = bodyColor; + super(speed, weight, bodyColor); this.additionalColor = additionalColor; this.Cabin = Cabin; this.Runway = Runway; diff --git a/AircraftCarrier/src/FrameAircraftCarrier.java b/AircraftCarrier/src/FrameAircraftCarrier.java index c2369ef..a01f62b 100644 --- a/AircraftCarrier/src/FrameAircraftCarrier.java +++ b/AircraftCarrier/src/FrameAircraftCarrier.java @@ -6,7 +6,9 @@ import java.io.File; import java.io.IOException; import java.util.Random; public class FrameAircraftCarrier extends JFrame { - private DrawingAircraftCarrier drawingAircraftCarrier; + private DrawingAircraft drawingAircraft; + private AbstractStrategy abstractStrategy; + private JComboBox comboBoxStrategy; private final JComponent pictureBox; public FrameAircraftCarrier() throws IOException { super("Военный корабль"); @@ -16,17 +18,22 @@ public class FrameAircraftCarrier extends JFrame { public void paintComponent(Graphics graphics){ super.paintComponent(graphics); Graphics2D graphics2D = (Graphics2D) graphics; - if (drawingAircraftCarrier!= null) drawingAircraftCarrier.drawTransport(graphics2D); + if (drawingAircraft!= null) drawingAircraft.drawTransport(graphics2D); super.repaint(); } }; - JButton createButton = new JButton("Создать"); + comboBoxStrategy = new JComboBox<>(new String[]{"К центру", "К границе"}); + JButton stepButton = new JButton("Шаг"); + JButton createShipButton = new JButton("Создать корабль"); + JButton createAircraftCarrierButton = new JButton("Создать Военный корабль"); JButton rightButton = new JButton(new ImageIcon(ImageIO.read(new File("C:\\Users\\79962\\Desktop\\arrows\\right.png")))); JButton leftButton = new JButton(new ImageIcon(ImageIO.read(new File("C:\\Users\\79962\\Desktop\\arrows\\left.png")))); JButton upButton = new JButton(new ImageIcon(ImageIO.read(new File("C:\\Users\\79962\\Desktop\\arrows\\up.png")))); JButton downButton = new JButton(new ImageIcon(ImageIO.read(new File("C:\\Users\\79962\\Desktop\\arrows\\down.png")))); pictureBox.setBounds( 0, 0, getContentPane().getWidth(), getContentPane().getHeight()); - createButton.addActionListener(e -> buttonCreateClick()); + createShipButton.addActionListener(e -> buttonCreateShipClick()); + createAircraftCarrierButton.addActionListener(e -> buttonCreateAircraftCarrierClick()); + stepButton.addActionListener(e -> buttonStepClick()); rightButton.setActionCommand("right"); rightButton.addActionListener(this::buttonMoveClick); leftButton.setActionCommand("left"); @@ -38,17 +45,21 @@ public class FrameAircraftCarrier extends JFrame { //component addition setLayout(new BorderLayout()); JPanel panelAircraftCarrier = new JPanel(new BorderLayout()); - JPanel createPanel = new JPanel(new BorderLayout()); - createPanel.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2)); - createPanel.add(createButton, BorderLayout.SOUTH); - JPanel movementPanel = new JPanel(new GridBagLayout()); JPanel rightPanel = new JPanel(new BorderLayout()); - rightPanel.add(movementPanel, BorderLayout.SOUTH); - rightButton.setPreferredSize(new Dimension(30,30)); + JPanel leftPanel = new JPanel(new BorderLayout()); GridBagConstraints constraints = new GridBagConstraints(); + constraints.insets.left = constraints.insets.top = constraints.insets.bottom = constraints.insets.right = 2; + JPanel createPanel = new JPanel(new GridBagLayout()); + constraints.gridx = 0; + constraints.gridy = 0; + createPanel.add(createShipButton, constraints); + constraints.gridx = 1; + constraints.gridy = 0; + createPanel.add(createAircraftCarrierButton, constraints); + JPanel movementPanel = new JPanel(new GridBagLayout()); + rightButton.setPreferredSize(new Dimension(30,30)); constraints.gridx = 2; constraints.gridy = 1; - constraints.insets.left = constraints.insets.top = constraints.insets.bottom = constraints.insets.right = 2; movementPanel.add(rightButton, constraints); leftButton.setPreferredSize(new Dimension(30,30)); constraints.gridx = 0; @@ -62,46 +73,96 @@ public class FrameAircraftCarrier extends JFrame { constraints.gridx = 1; constraints.gridy = 1; movementPanel.add(downButton, constraints); + JPanel stepPanel = new JPanel(new GridBagLayout()); + constraints.gridx = 0; + constraints.gridy = 0; + stepPanel.add(comboBoxStrategy, constraints); + constraints.gridx = 0; + constraints.gridy = 1; + stepPanel.add(stepButton, constraints); add(pictureBox); + rightPanel.add(movementPanel, BorderLayout.SOUTH); + rightPanel.add(stepPanel, BorderLayout.NORTH); + leftPanel.add(createPanel, BorderLayout.SOUTH); panelAircraftCarrier.add(rightPanel, BorderLayout.EAST); - panelAircraftCarrier.add(createPanel, BorderLayout.WEST); + panelAircraftCarrier.add(leftPanel, BorderLayout.WEST); add(panelAircraftCarrier,BorderLayout.CENTER); setVisible(true); } - private void buttonCreateClick() { + private void buttonCreateAircraftCarrierClick() { Random random = new Random(); - drawingAircraftCarrier = new DrawingAircraftCarrier(); pictureBox.setBounds(0,0,getContentPane().getWidth(),getContentPane().getHeight()); - drawingAircraftCarrier.init(random.nextInt(200) + 100, random.nextInt(2000) + 1000, - new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)), - new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)), - random.nextBoolean(), random.nextBoolean(), pictureBox.getWidth(), pictureBox.getHeight(), - (random.nextInt(3)+1)*2); - drawingAircraftCarrier.setPosition(random.nextInt(90) + 5, random.nextInt(90) + 5); + drawingAircraft = new DrawingAircraftCarrier(random.nextInt(200) + 100, random.nextInt(2000) + 1000, new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)), + new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)), random.nextBoolean(), random.nextBoolean(), pictureBox.getWidth(), pictureBox.getHeight(), random.nextInt(3),(random.nextInt(3)+1)*2); + drawingAircraft.setPosition(random.nextInt(90) + 10, random.nextInt(90) + 10); draw(); } + private void buttonCreateShipClick(){ + Random random = new Random(); + pictureBox.setBounds(0,0,getContentPane().getWidth(),getContentPane().getHeight()); + drawingAircraft = new DrawingAircraft(random.nextInt(200) + 100, random.nextInt(2000) + 1000, new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)), + pictureBox.getWidth(), pictureBox.getHeight(), random.nextInt(3),(random.nextInt(3)+1)*2); + drawingAircraft.setPosition(random.nextInt(90) + 10, random.nextInt(90) + 10); + draw(); + } + private void buttonStepClick() { + if (drawingAircraft == 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 DrawingObjectAircraftCarrier(drawingAircraft), pictureBox.getWidth(), + pictureBox.getHeight()); + comboBoxStrategy.setEnabled(false); + } + if (abstractStrategy == null) + { + return; + } + abstractStrategy.MakeStep(); + draw(); + if (abstractStrategy.GetStatus() == Status.FINISH) + { + comboBoxStrategy.setEnabled(true); + abstractStrategy = null; + } + } private void buttonMoveClick(ActionEvent event) { - if(drawingAircraftCarrier == null || drawingAircraftCarrier.getEntityAircraftCarrier() == null) + if(drawingAircraft == null || drawingAircraft.getEntityShip() == null) return; switch (event.getActionCommand()) { case "left": - drawingAircraftCarrier.moveTransport(DirectionType.LEFT); + drawingAircraft.moveTransport(DirectionType.LEFT); break; case "right": - drawingAircraftCarrier.moveTransport(DirectionType.RIGHT); + drawingAircraft.moveTransport(DirectionType.RIGHT); break; case "up": - drawingAircraftCarrier.moveTransport(DirectionType.UP); + drawingAircraft.moveTransport(DirectionType.UP); break; case "down": - drawingAircraftCarrier.moveTransport(DirectionType.DOWN); + drawingAircraft.moveTransport(DirectionType.DOWN); break; } draw(); } private void draw() { - if (drawingAircraftCarrier == null) + if (drawingAircraft == null) return; pictureBox.repaint(); } diff --git a/AircraftCarrier/src/IDrawImprovements.java b/AircraftCarrier/src/IDrawImprovements.java new file mode 100644 index 0000000..c50d2dc --- /dev/null +++ b/AircraftCarrier/src/IDrawImprovements.java @@ -0,0 +1,6 @@ +import java.awt.*; + +public interface IDrawImprovements { + public void setNumber(int x); + public void drawImprovements(Graphics2D graphics2D, int _startX, int _startY); +} diff --git a/AircraftCarrier/src/IMoveableObject.java b/AircraftCarrier/src/IMoveableObject.java new file mode 100644 index 0000000..329d7ad --- /dev/null +++ b/AircraftCarrier/src/IMoveableObject.java @@ -0,0 +1,6 @@ +public interface IMoveableObject { + ObjectParameters getObjectPosition(); + int getStep(); + boolean checkCanMove(DirectionType direction); + void moveObject(DirectionType direction); +} diff --git a/AircraftCarrier/src/MoveToBorder.java b/AircraftCarrier/src/MoveToBorder.java new file mode 100644 index 0000000..07b0c19 --- /dev/null +++ b/AircraftCarrier/src/MoveToBorder.java @@ -0,0 +1,34 @@ +public class MoveToBorder extends AbstractStrategy{ + @Override + protected boolean IsTargetDestination() { + var objParams = getObjectParameters.get(); + if(objParams == null) + return false; + return objParams.getRightBorder() + GetStep() >= getFieldWidth() && + objParams.getDownBorder() + GetStep() >= getFieldHeight(); + } + @Override + protected void MoveToTarget() { + var objParams = getObjectParameters.get(); + if(objParams == null) + return; + var diffX = objParams.getRightBorder() - getFieldWidth(); + var diffY = objParams.getDownBorder() - getFieldHeight(); + if(diffX >= 0) + { + MoveDown(); + } + else if(diffY >= 0) + { + MoveRight(); + } + else if(Math.abs(diffX) > Math.abs(diffY)) + { + MoveRight(); + } + else + { + MoveDown(); + } + } +} diff --git a/AircraftCarrier/src/MoveToCenter.java b/AircraftCarrier/src/MoveToCenter.java new file mode 100644 index 0000000..18363f9 --- /dev/null +++ b/AircraftCarrier/src/MoveToCenter.java @@ -0,0 +1,32 @@ +public class MoveToCenter extends AbstractStrategy{ + @Override + protected boolean IsTargetDestination() { + var objParams = getObjectParameters.get(); + if(objParams == null) + return false; + return objParams.getObjectMiddleHorizontal() <= getFieldWidth() / 2 && + objParams.getObjectMiddleHorizontal()+GetStep() >= getFieldWidth() / 2 && + objParams.getObjectMiddleVertical() <= getFieldHeight() / 2 && + objParams.getObjectMiddleVertical() + GetStep() >= getFieldHeight() / 2; + } + @Override + protected void MoveToTarget() { + var objParams = getObjectParameters.get(); + if(objParams == null) + return; + var diffX = objParams.getObjectMiddleHorizontal() - getFieldWidth() / 2; + if(Math.abs(diffX) > GetStep()){ + if(diffX > 0) + MoveLeft(); + else + MoveRight(); + } + var diffY = objParams.getObjectMiddleVertical() - getFieldHeight() / 2; + if(Math.abs(diffY) > GetStep()){ + if(diffY > 0) + MoveUp(); + else + MoveDown(); + } + } +} diff --git a/AircraftCarrier/src/ObjectParameters.java b/AircraftCarrier/src/ObjectParameters.java new file mode 100644 index 0000000..7b04f57 --- /dev/null +++ b/AircraftCarrier/src/ObjectParameters.java @@ -0,0 +1,19 @@ +public class ObjectParameters { + private final int _x; + private final int _y; + private final int _width; + private final int _height; + public int getLeftBorder() {return _x;} + public int getTopBorder() {return _y;} + public int getRightBorder() {return _x + _width;} + public int getDownBorder() {return _y + _height;} + public int getObjectMiddleHorizontal() {return _x + this._width / 2;} + public int getObjectMiddleVertical() {return _y + this._height / 2;} + public ObjectParameters(int x, int y, int width, int height) + { + _x = x; + _y = y; + _width = width; + _height = height; + } +} diff --git a/AircraftCarrier/src/Status.java b/AircraftCarrier/src/Status.java new file mode 100644 index 0000000..8fb12ef --- /dev/null +++ b/AircraftCarrier/src/Status.java @@ -0,0 +1,5 @@ +public enum Status { + NOTINIT, + INPROGRESS, + FINISH +}