diff --git a/src/AbstractStrategy.java b/src/AbstractStrategy.java new file mode 100644 index 0000000..4a70f71 --- /dev/null +++ b/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/src/DrawingAir.java b/src/DrawingAir.java new file mode 100644 index 0000000..11fa3ad --- /dev/null +++ b/src/DrawingAir.java @@ -0,0 +1,144 @@ +import java.awt.*; + +public class DrawingAir { + protected EntityAir entityAir; + protected void setEntityPlane(EntityAir entityPlane){ + this.entityAir = entityAir; + } + public EntityAir getEntityPlane() { + return entityAir; + } + private IDrawEngines drawingEngines; + 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 _PlaneWidth = 160; + public int getWidth() { + return _PlaneWidth; + } + private final int _PlaneHeight = 160; + public int getHeight(){ + return _PlaneHeight; + } + public DrawingAir(int speed, double weight, Color bodyColor, int width, int height, int enginesType, int enginesNumber) { + if (width < _PlaneWidth || height < _PlaneHeight) + return; + _pictureWidth = width; + _pictureHeight = height; + entityAir = new EntityAir(speed, weight, bodyColor); + switch (enginesType){ + case 1: + drawingEngines = new DrawingEnginesSquare(); + break; + case 2: + drawingEngines = new DrawingEnginesRound(); + break; + default: + drawingEngines = new DrawingEnginesOval(); + break; + } + drawingEngines.setNumber(enginesNumber); + } + public void setPosition(int x, int y) + { + if (x <= _pictureWidth - _PlaneWidth && x >= 0 && y <= _pictureHeight - _PlaneHeight && y >= 0) + x = y = 2; + _startPosX = x; + _startPosY = y; + } + public void drawTransport(Graphics2D g) { + if (entityAir == null) + return; + BasicStroke pen = new BasicStroke(2); + Color penColor = Color.BLACK; + Color bodyColor = entityAir.getBodyColor(); + g.setStroke(pen); + g.setColor(bodyColor); + //фюзеляж + g.fillRect(_startPosX + 20, _startPosY + 70, 140, 20); + //кабина + int[] pointX = new int[]{_startPosX, _startPosX + 20, _startPosX + 20}; + int[] pointY = new int[]{_startPosY + 80, _startPosY + 70, _startPosY + 90}; + g.setColor(Color.BLACK); + g.fillPolygon(pointX, pointY, 3); + //границы самолета + g.setColor(penColor); + g.drawPolygon(pointX, pointY, 3); + g.drawRect(_startPosX + 20, _startPosY + 70, 140, 20); + //Крылья + pointX = new int[]{_startPosX + 70, _startPosX + 70, _startPosX + 90, _startPosX + 100}; + pointY = new int[]{_startPosY + 70, _startPosY, _startPosY, _startPosY + 70}; + g.setColor(bodyColor); + g.fillPolygon(pointX, pointY, 4); + g.setColor(penColor); + g.drawPolygon(pointX, pointY, 4); + pointX = new int[]{_startPosX + 70, _startPosX + 70, _startPosX + 90, _startPosX + 100}; + pointY = new int[]{_startPosY + 90, _startPosY + 160, _startPosY + 160, _startPosY + 90}; + g.setColor(bodyColor); + g.fillPolygon(pointX, pointY, 4); + g.setColor(penColor); + g.drawPolygon(pointX, pointY, 4); + pointX = new int[]{_startPosX + 130, _startPosX + 130, _startPosX + 160, _startPosX + 160}; + pointY = new int[]{_startPosY + 70, _startPosY + 50, _startPosY + 30, _startPosY + 70}; + g.setColor(bodyColor); + g.fillPolygon(pointX, pointY, 4); + g.setColor(penColor); + g.drawPolygon(pointX, pointY, 4); + pointX = new int[]{_startPosX + 130, _startPosX + 130, _startPosX + 160, _startPosX + 160}; + pointY = new int[]{_startPosY + 90, _startPosY + 110, _startPosY + 130, _startPosY + 90}; + g.setColor(bodyColor); + g.fillPolygon(pointX, pointY, 4); + g.setColor(penColor); + g.drawPolygon(pointX, pointY, 4); + drawingEngines.drawEngines(g, _startPosX, _startPosY); + } + public boolean canMove(DirectionType direction) { + if (entityAir == null) { + return false; + } + switch(direction) { + case LEFT: + return _startPosX - entityAir.step.get().intValue() > 0; + case UP: + return _startPosY - entityAir.step.get().intValue() > 0; + case RIGHT: + return _startPosX + entityAir.step.get().intValue() + _PlaneWidth < _pictureWidth; + case DOWN: + return _startPosY + entityAir.step.get().intValue() + _PlaneWidth < _pictureWidth; + default: + return false; + } + } + public void moveTransport(DirectionType direction) + { + if (entityAir == null) + return; + int step = entityAir.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 + _PlaneWidth + step < _pictureWidth) + _startPosX += step; + break; + case DOWN: + if (_startPosY + _PlaneHeight + step < _pictureHeight) + _startPosY += step; + break; + } + } +} diff --git a/src/DrawingAirBomber.java b/src/DrawingAirBomber.java index 911d67b..931fda6 100644 --- a/src/DrawingAirBomber.java +++ b/src/DrawingAirBomber.java @@ -1,117 +1,29 @@ -import javax.swing.*; import java.awt.*; -public class DrawingAirBomber extends JPanel { - private EntityAirBomber entityAirBomber; - public EntityAirBomber getEntityAirBomber(){ - return entityAirBomber; +public class DrawingAirBomber extends DrawingAir { + + public DrawingAirBomber(int speed, double weight, Color bodyColor, + Color additionalColor, boolean rocket, boolean toplivo, int width, int height, int enginesType, int enginesNumber){ + super(speed, weight, bodyColor, width, height, enginesType, enginesNumber); + if(entityAir != null) + entityAir = new EntityAirBomber(speed, weight, bodyColor, additionalColor,rocket, toplivo); } - private int _pictureWidth; - private int _pictureHeight; - private int _startPosX; - private int _startPosY; - private final int _PlaneWidth = 160; - private final int _PlaneHeight = 160; - private DrawingEngines drawingEngines; - public boolean init(int speed, double weight, Color bodyColor, Color - additionalColor, boolean rocket, boolean toplivo, int width, int height, int enginesNumber) + + public void drawTransport(Graphics2D g) { - if (_PlaneWidth > width || _PlaneHeight > height) - return false; - _pictureWidth = width; - _pictureHeight = height; - entityAirBomber = new EntityAirBomber(); - entityAirBomber.init(speed, weight, bodyColor, additionalColor, - rocket, toplivo); - drawingEngines = new DrawingEngines(); - drawingEngines.setNumber(enginesNumber); - return true; - } - public void setPosition(int x, int y) - { - if (x <= _pictureWidth - _PlaneWidth && x >= 0 && y <= _pictureHeight - _PlaneHeight && y >= 0) - x = y = 2; - _startPosX = x; - _startPosY = y; - } - public void moveTransport(DirectionType direction) - { - if (entityAirBomber == null) - return; - int step = entityAirBomber.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 + _PlaneWidth + step < _pictureWidth) - _startPosX += step; - break; - case DOWN: - if (_startPosY + _PlaneHeight + step < _pictureHeight) - _startPosY += step; - break; - } - } - public void drawTransport(Graphics gr) - { - super.paintComponent(gr); - Graphics2D g = (Graphics2D) gr; - if (entityAirBomber == null) + if (!(entityAir instanceof EntityAirBomber)) return; + EntityAirBomber entityAirBomber = (EntityAirBomber) entityAir; BasicStroke pen = new BasicStroke(2); Color penColor = Color.BLACK; - Color bodyColor = entityAirBomber.getBodyColor(); Color additionalColor = entityAirBomber.getAdditionalColor(); g.setStroke(pen); - g.setColor(bodyColor); - //фюзеляж - g.fillRect( _startPosX + 20, _startPosY + 70, 140, 20); - //кабина - int[] pointX = new int[]{ _startPosX, _startPosX+20, _startPosX+20}; - int[] pointY = new int[]{ _startPosY + 80, _startPosY+70, _startPosY+90}; - g.setColor(Color.BLACK); - g.fillPolygon(pointX, pointY, 3); - //границы самолета - g.setColor(penColor); - g.drawPolygon(pointX, pointY, 3); - g.drawRect(_startPosX + 20, _startPosY + 70, 140, 20); - //Крылья - pointX = new int[] {_startPosX+70, _startPosX+70, _startPosX + 90, _startPosX + 100}; - pointY = new int[] { _startPosY+70, _startPosY, _startPosY, _startPosY+70}; - g.setColor(bodyColor); - g.fillPolygon(pointX, pointY, 4); - g.setColor(penColor); - g.drawPolygon(pointX, pointY, 4); - pointX = new int[] {_startPosX+70, _startPosX+70, _startPosX + 90, _startPosX + 100}; - pointY = new int[] { _startPosY+90, _startPosY+160, _startPosY+160, _startPosY+90}; - g.setColor(bodyColor); - g.fillPolygon(pointX, pointY, 4); - g.setColor(penColor); - g.drawPolygon(pointX, pointY, 4); - pointX = new int[] {_startPosX+130, _startPosX+130, _startPosX + 160, _startPosX + 160}; - pointY = new int[] { _startPosY+70, _startPosY+50, _startPosY+30, _startPosY+70}; - g.setColor(bodyColor); - g.fillPolygon(pointX, pointY, 4); - g.setColor(penColor); - g.drawPolygon(pointX, pointY, 4); - pointX = new int[] {_startPosX+130, _startPosX+130, _startPosX + 160, _startPosX + 160}; - pointY = new int[] { _startPosY+90, _startPosY+110, _startPosY+130, _startPosY+90}; - g.setColor(bodyColor); - g.fillPolygon(pointX, pointY, 4); - g.setColor(penColor); - g.drawPolygon(pointX, pointY, 4); + super.drawTransport(g); // топливо if (entityAirBomber.getFuel()) { - pointX = new int[]{_startPosX + 60, _startPosX + 60, _startPosX + 100, _startPosX + 100}; - pointY = new int[]{_startPosY + 10, _startPosY, _startPosY, _startPosY + 10}; + int[] pointX = new int[]{_startPosX + 60, _startPosX + 60, _startPosX + 100, _startPosX + 100}; + int[] pointY = new int[]{_startPosY + 10, _startPosY, _startPosY, _startPosY + 10}; g.setColor(penColor); g.fillPolygon(pointX, pointY, 4); pointX = new int[]{_startPosX + 60, _startPosX + 60, _startPosX + 100, _startPosX + 100}; @@ -122,8 +34,8 @@ public class DrawingAirBomber extends JPanel { //ракеты if (entityAirBomber.getBombs()) { - pointX = new int[]{_startPosX+70, _startPosX+55, _startPosX+70, _startPosX+70}; - pointY = new int[]{_startPosY+30, _startPosY+20, _startPosY+10, _startPosY+30}; + int[] pointX = new int[]{_startPosX+70, _startPosX+55, _startPosX+70, _startPosX+70}; + int[] pointY = new int[]{_startPosY+30, _startPosY+20, _startPosY+10, _startPosY+30}; g.setColor(additionalColor); g.fillPolygon(pointX, pointY, 4); pointX = new int[]{_startPosX+70, _startPosX+55, _startPosX+70, _startPosX+70}; @@ -139,6 +51,5 @@ public class DrawingAirBomber extends JPanel { g.setColor(additionalColor); g.fillPolygon(pointX, pointY, 4); } - drawingEngines.drawEngines(g, _startPosX, _startPosY); } } \ No newline at end of file diff --git a/src/DrawingEnginesOval.java b/src/DrawingEnginesOval.java new file mode 100644 index 0000000..41ac532 --- /dev/null +++ b/src/DrawingEnginesOval.java @@ -0,0 +1,37 @@ +import java.awt.*; + +public class DrawingEnginesOval implements IDrawEngines{ + private EngineNumber number; + public void setNumber(int x){ + if(x <= 2) + number = EngineNumber.TWO; + if(x == 4) + number = EngineNumber.FOUR; + if(x >= 6) + number = EngineNumber.SIX; + } + public void drawEngines(Graphics2D graphics2D, int _startX, int _startY){ + graphics2D.fillOval(_startX+85, _startY+20, 20, 15); + graphics2D.fillRect(_startX+75, _startY+20, 20, 15); + graphics2D.fillOval(_startX+65, _startY+20, 20, 15); + graphics2D.fillOval(_startX+85, _startY+125, 20, 15); + graphics2D.fillRect(_startX+75, _startY+125, 20, 15); + graphics2D.fillOval(_startX+65, _startY+125, 20, 15); + if (number == EngineNumber.FOUR || number == EngineNumber.SIX){ + graphics2D.fillOval(_startX+85, _startY+40, 20, 15); + graphics2D.fillRect(_startX+75, _startY+40, 20, 15); + graphics2D.fillOval(_startX+65, _startY+40, 20, 15); + graphics2D.fillOval(_startX+85, _startY+105, 20, 15); + graphics2D.fillRect(_startX+75, _startY+105, 20, 15); + graphics2D.fillOval(_startX+65, _startY+105, 20, 15); + } + if (number == EngineNumber.SIX){ + graphics2D.fillOval(_startX+145, _startY+50, 20, 15); + graphics2D.fillRect(_startX+135, _startY+50, 25, 15); + graphics2D.fillOval(_startX+125, _startY+50, 20, 15); + graphics2D.fillOval(_startX+145, _startY+95, 20, 15); + graphics2D.fillRect(_startX+135, _startY+95, 25, 15); + graphics2D.fillOval(_startX+125, _startY+95, 20, 15); + } + } +} diff --git a/src/DrawingEnginesRound.java b/src/DrawingEnginesRound.java new file mode 100644 index 0000000..b6587df --- /dev/null +++ b/src/DrawingEnginesRound.java @@ -0,0 +1,25 @@ +import java.awt.*; + +public class DrawingEnginesRound implements IDrawEngines{ + private EngineNumber number; + public void setNumber(int x){ + if(x <= 2) + number = EngineNumber.TWO; + if(x == 4) + number = EngineNumber.FOUR; + if(x >= 6) + number = EngineNumber.SIX; + } + public void drawEngines(Graphics2D graphics2D, int _startX, int _startY){ + graphics2D.fillOval(_startX+70, _startY+20, 20, 15); + graphics2D.fillOval(_startX+70, _startY+125, 20, 15); + if (number == EngineNumber.FOUR || number == EngineNumber.SIX){ + graphics2D.fillOval(_startX+70, _startY+40, 20, 15); + graphics2D.fillOval(_startX+70, _startY+105, 20, 15); + } + if (number == EngineNumber.SIX){ + graphics2D.fillOval(_startX+135, _startY+50, 20, 15); + graphics2D.fillOval(_startX+135, _startY+95, 20, 15); + } + } +} diff --git a/src/DrawingEngines.java b/src/DrawingEnginesSquare.java similarity index 93% rename from src/DrawingEngines.java rename to src/DrawingEnginesSquare.java index 517825a..a5b45cc 100644 --- a/src/DrawingEngines.java +++ b/src/DrawingEnginesSquare.java @@ -1,6 +1,6 @@ import java.awt.*; -public class DrawingEngines { +public class DrawingEnginesSquare implements IDrawEngines{ private EngineNumber number; public void setNumber(int x){ if(x <= 2) diff --git a/src/DrawingObjectAirBomber.java b/src/DrawingObjectAirBomber.java new file mode 100644 index 0000000..c20ccaf --- /dev/null +++ b/src/DrawingObjectAirBomber.java @@ -0,0 +1,27 @@ +public class DrawingObjectAirBomber implements IMoveableObject{ + private final DrawingAir drawingAir; + + public DrawingObjectAirBomber(DrawingAir drawingPlane){ + this.drawingAir = drawingPlane; + } + public ObjectParameters getObjectPosition(){ + if(drawingAir == null || drawingAir.getEntityPlane() == null) + return null; + return new ObjectParameters(drawingAir.getPosX(), drawingAir.getPosY(), + drawingAir.getWidth(), drawingAir.getHeight()); + } + public int getStep(){ + if(drawingAir.getEntityPlane() == null) + return 0; + return drawingAir.getEntityPlane().step.get().intValue(); + } + public boolean checkCanMove(DirectionType direction){ + if(drawingAir == null) + return false; + return drawingAir.canMove(direction); + } + public void moveObject(DirectionType direction){ + drawingAir.moveTransport(direction); + } +} + diff --git a/src/EntityAir.java b/src/EntityAir.java new file mode 100644 index 0000000..514b824 --- /dev/null +++ b/src/EntityAir.java @@ -0,0 +1,24 @@ +import java.awt.*; +import java.util.function.Supplier; + +public class EntityAir { + 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 EntityAir(int speed, double weight, Color bodyColor){ + this.speed = speed; + this.weight = weight; + this.bodyColor = bodyColor; + } +} diff --git a/src/EntityAirBomber.java b/src/EntityAirBomber.java index b4d7405..4146ce8 100644 --- a/src/EntityAirBomber.java +++ b/src/EntityAirBomber.java @@ -1,19 +1,5 @@ import java.awt.*; -import java.util.function.Supplier; - -public class EntityAirBomber { - 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 EntityAirBomber extends EntityAir{ private Color additionalColor; public Color getAdditionalColor(){ return additionalColor; @@ -26,12 +12,9 @@ public class EntityAirBomber { public boolean getBombs() { return isBombs; } - public Supplier step = () -> (double) speed * 100 / weight; - public void init(int speed, double weight, Color bodyColor, Color + public EntityAirBomber(int speed, double weight, Color bodyColor, Color additionalColor, boolean isFuel, boolean isBombs) { - this.speed = speed; - this.weight = weight; - this.bodyColor = bodyColor; + super(speed, weight, bodyColor); this.additionalColor = additionalColor; this.isFuel = isFuel; this.isBombs = isBombs; diff --git a/src/FrameAirBomber.java b/src/FrameAirBomber.java index 1254bc5..ee17374 100644 --- a/src/FrameAirBomber.java +++ b/src/FrameAirBomber.java @@ -6,27 +6,36 @@ import java.io.File; import java.io.IOException; import java.util.Random; public class FrameAirBomber extends JFrame { - private DrawingAirBomber drawingAirBomber; + private DrawingAir drawingAir; + private AbstractStrategy abstractStrategy; + private JComboBox comboBoxStrategy; private final JComponent pictureBox; public FrameAirBomber() throws IOException { super("Бомбардировщик"); setSize(new Dimension(900,500)); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + //components initialisation pictureBox = new JComponent(){ public void paintComponent(Graphics graphics){ super.paintComponent(graphics); Graphics2D graphics2D = (Graphics2D) graphics; - if (drawingAirBomber != null) drawingAirBomber.drawTransport(graphics2D); + if (drawingAir != null) drawingAir.drawTransport(graphics2D); super.repaint(); } }; - JButton createButton = new JButton("Создать"); + comboBoxStrategy = new JComboBox<>(new String[]{"К центру", "К границе"}); + JButton stepButton = new JButton("Шаг"); + JButton createPlaneButton = new JButton("Создать самолет"); + JButton createAirBomberButton = new JButton("Создать бомбардировщик"); JButton rightButton = new JButton(new ImageIcon(ImageIO.read(new File("images/right.png")))); JButton leftButton = new JButton(new ImageIcon(ImageIO.read(new File("images/left.png")))); JButton upButton = new JButton(new ImageIcon(ImageIO.read(new File("images/up.png")))); JButton downButton = new JButton(new ImageIcon(ImageIO.read(new File("images/down.png")))); pictureBox.setBounds( 0, 0, getContentPane().getWidth(), getContentPane().getHeight()); - createButton.addActionListener(e -> buttonCreateClick()); + //ActionListeners and ActionCommand addition + createPlaneButton.addActionListener(e -> buttonCreatePlaneClick()); + createAirBomberButton.addActionListener(e -> buttonCreateAirBomberClick()); + stepButton.addActionListener(e -> buttonStepClick()); rightButton.setActionCommand("right"); rightButton.addActionListener(this::buttonMoveClick); leftButton.setActionCommand("left"); @@ -37,18 +46,24 @@ public class FrameAirBomber extends JFrame { downButton.addActionListener(this::buttonMoveClick); //component addition setLayout(new BorderLayout()); - JPanel panelBattleship = 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 panelAirBomber = new JPanel(new BorderLayout()); 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; + //createPanel + JPanel createPanel = new JPanel(new GridBagLayout()); + constraints.gridx = 0; + constraints.gridy = 0; + createPanel.add(createPlaneButton, constraints); + constraints.gridx = 1; + constraints.gridy = 0; + createPanel.add(createAirBomberButton, constraints); + //movementPanel + 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 +77,99 @@ public class FrameAirBomber extends JFrame { constraints.gridx = 1; constraints.gridy = 1; movementPanel.add(downButton, constraints); + //stepPanel + 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); + //addition to frame add(pictureBox); - panelBattleship.add(rightPanel, BorderLayout.EAST); - panelBattleship.add(createPanel, BorderLayout.WEST); - add(panelBattleship,BorderLayout.CENTER); + rightPanel.add(movementPanel, BorderLayout.SOUTH); + rightPanel.add(stepPanel, BorderLayout.NORTH); + leftPanel.add(createPanel, BorderLayout.SOUTH); + panelAirBomber.add(rightPanel, BorderLayout.EAST); + panelAirBomber.add(leftPanel, BorderLayout.WEST); + add(panelAirBomber,BorderLayout.CENTER); setVisible(true); } - private void buttonCreateClick() { + private void buttonCreateAirBomberClick() { Random random = new Random(); - drawingAirBomber = new DrawingAirBomber(); pictureBox.setBounds(0,0,getContentPane().getWidth(),getContentPane().getHeight()); - drawingAirBomber.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); - drawingAirBomber.setPosition(random.nextInt(90) + 10, random.nextInt(90) + 10); + drawingAir = new DrawingAirBomber(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); + drawingAir.setPosition(random.nextInt(90) + 10, random.nextInt(90) + 10); draw(); } + private void buttonCreatePlaneClick(){ + Random random = new Random(); + pictureBox.setBounds(0,0,getContentPane().getWidth(),getContentPane().getHeight()); + drawingAir = new DrawingAir(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); + drawingAir.setPosition(random.nextInt(90) + 10, random.nextInt(90) + 10); + draw(); + } + private void buttonStepClick(){ + if (drawingAir == 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 DrawingObjectAirBomber(drawingAir), 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(drawingAirBomber == null || drawingAirBomber.getEntityAirBomber() == null) + if(drawingAir == null || drawingAir.getEntityPlane() == null) return; switch (event.getActionCommand()) { case "left": - drawingAirBomber.moveTransport(DirectionType.LEFT); + drawingAir.moveTransport(DirectionType.LEFT); break; case "right": - drawingAirBomber.moveTransport(DirectionType.RIGHT); + drawingAir.moveTransport(DirectionType.RIGHT); break; case "up": - drawingAirBomber.moveTransport(DirectionType.UP); + drawingAir.moveTransport(DirectionType.UP); break; case "down": - drawingAirBomber.moveTransport(DirectionType.DOWN); + drawingAir.moveTransport(DirectionType.DOWN); break; } draw(); } private void draw() { - if (drawingAirBomber == null) + if (drawingAir == null) return; pictureBox.repaint(); } diff --git a/src/IDrawEngines.java b/src/IDrawEngines.java new file mode 100644 index 0000000..9b95621 --- /dev/null +++ b/src/IDrawEngines.java @@ -0,0 +1,6 @@ +import java.awt.*; + +public interface IDrawEngines { + public void setNumber(int x); + public void drawEngines(Graphics2D graphics2D, int _startX, int _startY); +} diff --git a/src/IMoveableObject.java b/src/IMoveableObject.java new file mode 100644 index 0000000..329d7ad --- /dev/null +++ b/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/src/MoveToBorder.java b/src/MoveToBorder.java new file mode 100644 index 0000000..07b0c19 --- /dev/null +++ b/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/src/MoveToCenter.java b/src/MoveToCenter.java new file mode 100644 index 0000000..18363f9 --- /dev/null +++ b/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/src/ObjectParameters.java b/src/ObjectParameters.java new file mode 100644 index 0000000..7b04f57 --- /dev/null +++ b/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/src/Status.java b/src/Status.java new file mode 100644 index 0000000..8fb12ef --- /dev/null +++ b/src/Status.java @@ -0,0 +1,5 @@ +public enum Status { + NOTINIT, + INPROGRESS, + FINISH +}