diff --git a/AirFighter/src/AbstractStrategy.java b/AirFighter/src/AbstractStrategy.java new file mode 100644 index 0000000..4a70f71 --- /dev/null +++ b/AirFighter/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/AirFighter/src/DrawingAirFighter.java b/AirFighter/src/DrawingAirFighter.java index 42ded64..6d3ed80 100644 --- a/AirFighter/src/DrawingAirFighter.java +++ b/AirFighter/src/DrawingAirFighter.java @@ -1,118 +1,33 @@ -import javax.swing.*; import java.awt.*; -public class DrawingAirFighter extends JPanel { - private EntityAirFighter entityAirFighter; - public EntityAirFighter getEntityAirFighter(){ - return entityAirFighter; +public class DrawingAirFighter extends DrawingAirplane { + + public DrawingAirFighter(int speed, double weight, Color bodyColor, + Color additionalColor, boolean rockets, boolean wings, int width, int height, int enginesType, int enginesNumber){ + super(speed, weight, bodyColor, width, height, enginesType, enginesNumber); + if(entityAirplane != null) + entityAirplane = new EntityAirFighter(speed, weight, bodyColor, additionalColor, rockets, wings); } - 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 rockets, boolean wings, int width, int height, int enginesNumber) + + public void drawTransport(Graphics2D g) { - if (_PlaneWidth > width || _PlaneHeight > height) - return false; - _pictureWidth = width; - _pictureHeight = height; - entityAirFighter = new EntityAirFighter(); - entityAirFighter.init(speed, weight, bodyColor, additionalColor, - rockets, wings); - drawingEngines = new DrawingEngines(); - drawingEngines.setNumber(enginesNumber); - return true; - } - - public void setPosition(int x, int y) - { - _startPosX = x; - _startPosY = y; - - if (_startPosX < 0 || _startPosY < 0 || _startPosX > (_pictureWidth - _PlaneWidth) || _startPosY > (_pictureHeight - _PlaneHeight)) - { - _startPosX = 50; - _startPosY = 50; - } - } - public void moveTransport(DirectionType direction) - { - if (entityAirFighter == null) - return; - int step = entityAirFighter.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 (entityAirFighter == null) + if (!(entityAirplane instanceof EntityAirFighter)) return; + EntityAirFighter entityAirFighter = (EntityAirFighter) entityAirplane; BasicStroke pen = new BasicStroke(2); - Color bodyColor = entityAirFighter.getBodyColor(); + Color additionalColor = entityAirFighter.getAdditionalColor(); g.setStroke(pen); - - - // тело - g.setColor(bodyColor); - int[] pointX = new int[]{ _startPosX, _startPosX+25, _startPosX+25}; - int[] pointY = new int[]{ _startPosY + 80, _startPosY+69, _startPosY+91}; - g.fillPolygon(pointX, pointY, 3); - g.setColor(Color.BLACK); - g.drawRect( _startPosX + 25, _startPosY + 70, 135, 20); - - //Крылья - g.setColor(Color.BLACK); - - pointX = new int[] {_startPosX+60, _startPosX+60, _startPosX + 70, _startPosX + 80}; - pointY = new int[] { _startPosY+70, _startPosY, _startPosY, _startPosY+70}; - g.drawPolygon(pointX, pointY, 4); - - - pointX = new int[] {_startPosX+60, _startPosX+60, _startPosX + 70, _startPosX + 80}; - pointY = new int[] { _startPosY+90, _startPosY+160, _startPosY+160, _startPosY+90}; - g.drawPolygon(pointX, pointY, 4); - - pointX = new int[] {_startPosX+140, _startPosX+140, _startPosX + 160, _startPosX + 160}; - pointY = new int[] { _startPosY+70, _startPosY+50, _startPosY+30, _startPosY+70}; - g.drawPolygon(pointX, pointY, 4); - - - pointX = new int[] {_startPosX+140, _startPosX+140, _startPosX + 160, _startPosX + 160}; - pointY = new int[] { _startPosY+90, _startPosY+110, _startPosY+130, _startPosY+90}; - g.drawPolygon(pointX, pointY, 4); - - - // кралья + super.drawTransport(g); + // крылья if (entityAirFighter.getWings()) { g.setColor(additionalColor); - pointX = new int[]{_startPosX + 100, _startPosX + 100, _startPosX + 110, _startPosX + 120}; - pointY = new int[]{_startPosY + 70, _startPosY+30, _startPosY+30, _startPosY + 70}; + int[] pointX = new int[]{_startPosX + 100, _startPosX + 100, _startPosX + 110, _startPosX + 120}; + int[] pointY = new int[]{_startPosY + 70, _startPosY+30, _startPosY+30, _startPosY + 70}; + + g.fillPolygon(pointX, pointY, 4); pointX = new int[]{_startPosX + 100, _startPosX + 100, _startPosX + 110, _startPosX + 120}; @@ -124,15 +39,13 @@ public class DrawingAirFighter extends JPanel { { g.setColor(additionalColor); - pointX = new int[]{_startPosX+30, _startPosX+40, _startPosX+90, _startPosX+90, _startPosX+40}; - pointY = new int[]{_startPosY+25, _startPosY+20, _startPosY+20, _startPosY+30, _startPosY+30}; + int[] pointX = new int[]{_startPosX+30, _startPosX+40, _startPosX+90, _startPosX+90, _startPosX+40}; + int[] pointY = new int[]{_startPosY+25, _startPosY+20, _startPosY+20, _startPosY+30, _startPosY+30}; g.fillPolygon(pointX, pointY, 5); pointX = new int[]{_startPosX+30, _startPosX+40, _startPosX+90, _startPosX+90, _startPosX+40}; pointY = new int[]{_startPosY+135, _startPosY+130, _startPosY+130, _startPosY+140, _startPosY+140}; g.fillPolygon(pointX, pointY, 5); } - g.setColor(Color.BLACK); - drawingEngines.drawEngines(g, _startPosX, _startPosY); } } \ No newline at end of file diff --git a/AirFighter/src/DrawingAirplane.java b/AirFighter/src/DrawingAirplane.java new file mode 100644 index 0000000..e6c3385 --- /dev/null +++ b/AirFighter/src/DrawingAirplane.java @@ -0,0 +1,142 @@ +import java.awt.*; + +public class DrawingAirplane { + protected EntityAirplane entityAirplane; + protected void setEntityPlane(EntityAirplane entityPlane){ + this.entityAirplane = entityAirplane; + } + public EntityAirplane getEntityPlane() { + return entityAirplane; + } + 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 DrawingAirplane(int speed, double weight, Color bodyColor, int width, int height, int enginesType, int enginesNumber) { + if (width < _PlaneWidth || height < _PlaneHeight) + return; + _pictureWidth = width; + _pictureHeight = height; + entityAirplane = new EntityAirplane(speed, weight, bodyColor); + switch (enginesType){ + case 1: + drawingEngines = new DrawingEnginesSquare(); + break; + case 2: + drawingEngines = new DrawingEnginesRound(); + break; + default: + drawingEngines = new DrawingEnginesTriangle(); + break; + } + drawingEngines.setNumber(enginesNumber); + } + public void setPosition(int x, int y) + { + _startPosX = x; + _startPosY = y; + + if (_startPosX < 0 || _startPosY < 0 || _startPosX > (_pictureWidth - _PlaneWidth) || _startPosY > (_pictureHeight - _PlaneHeight)) + { + _startPosX = 50; + _startPosY = 50; + } + } + + public void drawTransport(Graphics2D g) { + if (entityAirplane == null) + return; + BasicStroke pen = new BasicStroke(2); + Color bodyColor = entityAirplane.getBodyColor(); + g.setStroke(pen); + g.setColor(bodyColor); + + // тело + g.setColor(bodyColor); + int[] pointX = new int[]{ _startPosX, _startPosX+25, _startPosX+25}; + int[] pointY = new int[]{ _startPosY + 80, _startPosY+69, _startPosY+91}; + g.fillPolygon(pointX, pointY, 3); + g.setColor(Color.BLACK); + g.drawRect( _startPosX + 25, _startPosY + 70, 135, 20); + + //Крылья + g.setColor(Color.BLACK); + + pointX = new int[] {_startPosX+60, _startPosX+60, _startPosX + 70, _startPosX + 80}; + pointY = new int[] { _startPosY+70, _startPosY, _startPosY, _startPosY+70}; + g.drawPolygon(pointX, pointY, 4); + + + pointX = new int[] {_startPosX+60, _startPosX+60, _startPosX + 70, _startPosX + 80}; + pointY = new int[] { _startPosY+90, _startPosY+160, _startPosY+160, _startPosY+90}; + g.drawPolygon(pointX, pointY, 4); + + pointX = new int[] {_startPosX+140, _startPosX+140, _startPosX + 160, _startPosX + 160}; + pointY = new int[] { _startPosY+70, _startPosY+50, _startPosY+30, _startPosY+70}; + g.drawPolygon(pointX, pointY, 4); + + + pointX = new int[] {_startPosX+140, _startPosX+140, _startPosX + 160, _startPosX + 160}; + pointY = new int[] { _startPosY+90, _startPosY+110, _startPosY+130, _startPosY+90}; + g.drawPolygon(pointX, pointY, 4); + drawingEngines.drawEngines(g, _startPosX, _startPosY); + } + + public boolean canMove(DirectionType direction) { + if (entityAirplane == null) { + return false; + } + switch(direction) { + case LEFT: + return _startPosX - entityAirplane.step.get().intValue() > 0; + case UP: + return _startPosY - entityAirplane.step.get().intValue() > 0; + case RIGHT: + return _startPosX + entityAirplane.step.get().intValue() + _PlaneWidth < _pictureWidth; + case DOWN: + return _startPosY + entityAirplane.step.get().intValue() + _PlaneWidth < _pictureWidth; + default: + return false; + } + } + public void moveTransport(DirectionType direction) + { + if (entityAirplane == null) + return; + int step = entityAirplane.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/AirFighter/src/DrawingEnginesRound.java b/AirFighter/src/DrawingEnginesRound.java new file mode 100644 index 0000000..422c473 --- /dev/null +++ b/AirFighter/src/DrawingEnginesRound.java @@ -0,0 +1,27 @@ +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+53, _startY+35, 7, 7); + graphics2D.fillOval(_startX+53, _startY+120, 7, 7); + + if (number == EngineNumber.FOUR || number == EngineNumber.SIX){ + graphics2D.fillOval(_startX+53, _startY+55, 7, 7); + graphics2D.fillOval(_startX+53, _startY+100, 7, 7); + } + + if (number == EngineNumber.SIX){ + graphics2D.fillOval(_startX+53, _startY+45, 7, 7); + graphics2D.fillOval(_startX+53, _startY+110, 7, 7); + } + } +} diff --git a/AirFighter/src/DrawingEngines.java b/AirFighter/src/DrawingEnginesSquare.java similarity index 53% rename from AirFighter/src/DrawingEngines.java rename to AirFighter/src/DrawingEnginesSquare.java index be9b76c..0de7639 100644 --- a/AirFighter/src/DrawingEngines.java +++ b/AirFighter/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) @@ -11,16 +11,16 @@ public class DrawingEngines { number = EngineNumber.SIX; } public void drawEngines(Graphics2D graphics2D, int _startX, int _startY){ - graphics2D.fillRect(_startX+53, _startY+35, 7, 5); - graphics2D.fillRect(_startX+53, _startY+120, 7, 5); + graphics2D.fillRect(_startX+53, _startY+35, 7, 7); + graphics2D.fillRect(_startX+53, _startY+120, 7, 7); if (number == EngineNumber.FOUR || number == EngineNumber.SIX){ - graphics2D.fillRect(_startX+53, _startY+55, 7, 5); - graphics2D.fillRect(_startX+53, _startY+100, 7, 5); + graphics2D.fillRect(_startX+53, _startY+55, 7, 7); + graphics2D.fillRect(_startX+53, _startY+100, 7, 7); } if (number == EngineNumber.SIX){ - graphics2D.fillRect(_startX+53, _startY+45, 7, 5); - graphics2D.fillRect(_startX+53, _startY+110, 7, 5); + graphics2D.fillRect(_startX+53, _startY+45, 7, 7); + graphics2D.fillRect(_startX+53, _startY+110, 7, 7); } } } \ No newline at end of file diff --git a/AirFighter/src/DrawingEnginesTriangle.java b/AirFighter/src/DrawingEnginesTriangle.java new file mode 100644 index 0000000..6b4f9e4 --- /dev/null +++ b/AirFighter/src/DrawingEnginesTriangle.java @@ -0,0 +1,43 @@ +import java.awt.*; + +public class DrawingEnginesTriangle 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){ + + int[] pointX = new int[]{ _startX+45, _startX+60, _startX+60}; + int[] pointY = new int[]{ _startY + 35, _startY+30, _startY+40}; + graphics2D.fillPolygon(pointX, pointY, 3); + + pointX = new int[]{_startX+45, _startX+60, _startX+60}; + pointY = new int[]{ _startY + 125, _startY+120, _startY+130}; + graphics2D.fillPolygon(pointX, pointY, 3); + + if (number == EngineNumber.FOUR || number == EngineNumber.SIX){ + pointX = new int[]{ _startX+45, _startX+60, _startX+60}; + pointY = new int[]{ _startY + 55, _startY+50, _startY+60}; + graphics2D.fillPolygon(pointX, pointY, 3); + + pointX = new int[]{_startX+45, _startX+60, _startX+60}; + pointY = new int[]{ _startY + 105, _startY+100, _startY+110}; + graphics2D.fillPolygon(pointX, pointY, 3); + } + + if (number == EngineNumber.SIX){ + pointX = new int[]{ _startX+45, _startX+60, _startX+60}; + pointY = new int[]{ _startY + 45, _startY+40, _startY+50}; + graphics2D.fillPolygon(pointX, pointY, 3); + + pointX = new int[]{_startX+45, _startX+60, _startX+60}; + pointY = new int[]{ _startY + 115, _startY+110, _startY+120}; + graphics2D.fillPolygon(pointX, pointY, 3); + } + } +} diff --git a/AirFighter/src/DrawingObjectAirFighter.java b/AirFighter/src/DrawingObjectAirFighter.java new file mode 100644 index 0000000..d0c81a8 --- /dev/null +++ b/AirFighter/src/DrawingObjectAirFighter.java @@ -0,0 +1,27 @@ +public class DrawingObjectAirFighter implements IMoveableObject{ + private final DrawingAirplane drawingAirplane; + + public DrawingObjectAirFighter(DrawingAirplane drawingPlane){ + this.drawingAirplane = drawingPlane; + } + public ObjectParameters getObjectPosition(){ + if(drawingAirplane == null || drawingAirplane.getEntityPlane() == null) + return null; + return new ObjectParameters(drawingAirplane.getPosX(), drawingAirplane.getPosY(), + drawingAirplane.getWidth(), drawingAirplane.getHeight()); + } + public int getStep(){ + if(drawingAirplane.getEntityPlane() == null) + return 0; + return drawingAirplane.getEntityPlane().step.get().intValue(); + } + public boolean checkCanMove(DirectionType direction){ + if(drawingAirplane == null) + return false; + return drawingAirplane.canMove(direction); + } + public void moveObject(DirectionType direction){ + drawingAirplane.moveTransport(direction); + } +} + diff --git a/AirFighter/src/EntityAirFighter.java b/AirFighter/src/EntityAirFighter.java index 2bfa9aa..9495f7a 100644 --- a/AirFighter/src/EntityAirFighter.java +++ b/AirFighter/src/EntityAirFighter.java @@ -1,19 +1,7 @@ import java.awt.*; -import java.util.function.Supplier; -public class EntityAirFighter { - 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 EntityAirFighter extends EntityAirplane{ private Color additionalColor; public Color getAdditionalColor(){ return additionalColor; @@ -26,14 +14,11 @@ public class EntityAirFighter { public boolean getRockets() { return isRockets; } - public Supplier step = () -> (double) speed * 100 / weight; - public void init(int speed, double weight, Color bodyColor, Color - additionalColor, boolean isWings, boolean isRockets) { - this.speed = speed; - this.weight = weight; - this.bodyColor = bodyColor; - this.additionalColor = additionalColor; - this.isWings = isWings; - this.isRockets = isRockets; - } + public EntityAirFighter(int speed, double weight, Color bodyColor, Color + additionalColor, boolean isWings, boolean isRockets) { +super(speed, weight, bodyColor); +this.additionalColor = additionalColor; +this.isWings = isWings; +this.isRockets = isRockets; +} } \ No newline at end of file diff --git a/AirFighter/src/EntityAirplane.java b/AirFighter/src/EntityAirplane.java new file mode 100644 index 0000000..8065ce7 --- /dev/null +++ b/AirFighter/src/EntityAirplane.java @@ -0,0 +1,24 @@ +import java.awt.*; +import java.util.function.Supplier; + +public class EntityAirplane { + 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 EntityAirplane(int speed, double weight, Color bodyColor){ + this.speed = speed; + this.weight = weight; + this.bodyColor = bodyColor; + } +} diff --git a/AirFighter/src/FrameAirFighter.java b/AirFighter/src/FrameAirFighter.java index ab1fa69..df5e7bc 100644 --- a/AirFighter/src/FrameAirFighter.java +++ b/AirFighter/src/FrameAirFighter.java @@ -6,7 +6,9 @@ import java.io.File; import java.io.IOException; import java.util.Random; public class FrameAirFighter extends JFrame { - private DrawingAirFighter drawingAirFighter; + private DrawingAirplane drawingAirplane; + private AbstractStrategy abstractStrategy; + private JComboBox comboBoxStrategy; private final JComponent pictureBox; public FrameAirFighter() throws IOException { super("Истребитель"); @@ -16,17 +18,22 @@ public class FrameAirFighter extends JFrame { public void paintComponent(Graphics graphics){ super.paintComponent(graphics); Graphics2D graphics2D = (Graphics2D) graphics; - if (drawingAirFighter != null) drawingAirFighter.drawTransport(graphics2D); + if (drawingAirplane != null) drawingAirplane.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("C:\\Users\\nasty\\OneDrive\\Рабочий стол\\images\\right.png")))); JButton leftButton = new JButton(new ImageIcon(ImageIO.read(new File("C:\\Users\\nasty\\OneDrive\\Рабочий стол\\images\\left.png")))); JButton upButton = new JButton(new ImageIcon(ImageIO.read(new File("C:\\Users\\nasty\\OneDrive\\Рабочий стол\\images\\up.png")))); JButton downButton = new JButton(new ImageIcon(ImageIO.read(new File("C:\\Users\\nasty\\OneDrive\\Рабочий стол\\images\\down.png")))); pictureBox.setBounds( 0, 0, getContentPane().getWidth(), getContentPane().getHeight()); - createButton.addActionListener(e -> buttonCreateClick()); + createPlaneButton.addActionListener(e -> buttonCreateAirplaneClick()); + createAirBomberButton.addActionListener(e -> buttonCreateAirFighterClick()); + stepButton.addActionListener(e -> buttonStepClick()); rightButton.setActionCommand("right"); rightButton.addActionListener(this::buttonMoveClick); leftButton.setActionCommand("left"); @@ -35,20 +42,27 @@ public class FrameAirFighter extends JFrame { upButton.addActionListener(this::buttonMoveClick); downButton.setActionCommand("down"); downButton.addActionListener(this::buttonMoveClick); - //component addition + setLayout(new BorderLayout()); JPanel panelAirFighter = 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(createPlaneButton, constraints); + constraints.gridx = 1; + constraints.gridy = 0; + createPanel.add(createAirBomberButton, 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,47 +76,101 @@ public class FrameAirFighter 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); + rightPanel.add(movementPanel, BorderLayout.SOUTH); + rightPanel.add(stepPanel, BorderLayout.NORTH); + leftPanel.add(createPanel, BorderLayout.SOUTH); panelAirFighter.add(rightPanel, BorderLayout.EAST); - panelAirFighter.add(createPanel, BorderLayout.WEST); + panelAirFighter.add(leftPanel, BorderLayout.WEST); add(panelAirFighter,BorderLayout.CENTER); setVisible(true); } - private void buttonCreateClick() { + private void buttonCreateAirFighterClick() { Random random = new Random(); - drawingAirFighter = new DrawingAirFighter(); pictureBox.setBounds(0,0,getContentPane().getWidth(),getContentPane().getHeight()); - drawingAirFighter.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); - drawingAirFighter.setPosition(random.nextInt(90) + 10, random.nextInt(90) + 10); - + drawingAirplane = new DrawingAirFighter(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); + drawingAirplane.setPosition(random.nextInt(90) + 10, random.nextInt(90) + 10); draw(); } - private void buttonMoveClick(ActionEvent event) { - if(drawingAirFighter == null || drawingAirFighter.getEntityAirFighter() == null) + private void buttonCreateAirplaneClick(){ + Random random = new Random(); + pictureBox.setBounds(0,0,getContentPane().getWidth(),getContentPane().getHeight()); + drawingAirplane = new DrawingAirplane(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); + drawingAirplane.setPosition(random.nextInt(90) + 10, random.nextInt(90) + 10); + draw(); + } + + private void buttonStepClick(){ + if (drawingAirplane == 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 DrawingObjectAirFighter(drawingAirplane), 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(drawingAirplane == null || drawingAirplane.getEntityPlane() == null) return; switch (event.getActionCommand()) { case "left": - drawingAirFighter.moveTransport(DirectionType.LEFT); + drawingAirplane.moveTransport(DirectionType.LEFT); break; case "right": - drawingAirFighter.moveTransport(DirectionType.RIGHT); + drawingAirplane.moveTransport(DirectionType.RIGHT); break; case "up": - drawingAirFighter.moveTransport(DirectionType.UP); + drawingAirplane.moveTransport(DirectionType.UP); break; case "down": - drawingAirFighter.moveTransport(DirectionType.DOWN); + drawingAirplane.moveTransport(DirectionType.DOWN); break; } draw(); } private void draw() { - if (drawingAirFighter == null) + if (drawingAirplane == null) return; pictureBox.repaint(); } diff --git a/AirFighter/src/IDrawEngines.java b/AirFighter/src/IDrawEngines.java new file mode 100644 index 0000000..9b95621 --- /dev/null +++ b/AirFighter/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/AirFighter/src/IMoveableObject.java b/AirFighter/src/IMoveableObject.java new file mode 100644 index 0000000..329d7ad --- /dev/null +++ b/AirFighter/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/AirFighter/src/MoveToBorder.java b/AirFighter/src/MoveToBorder.java new file mode 100644 index 0000000..07b0c19 --- /dev/null +++ b/AirFighter/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/AirFighter/src/MoveToCenter.java b/AirFighter/src/MoveToCenter.java new file mode 100644 index 0000000..18363f9 --- /dev/null +++ b/AirFighter/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/AirFighter/src/ObjectParameters.java b/AirFighter/src/ObjectParameters.java new file mode 100644 index 0000000..7b04f57 --- /dev/null +++ b/AirFighter/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/AirFighter/src/Status.java b/AirFighter/src/Status.java new file mode 100644 index 0000000..8fb12ef --- /dev/null +++ b/AirFighter/src/Status.java @@ -0,0 +1,5 @@ +public enum Status { + NOTINIT, + INPROGRESS, + FINISH +}