diff --git a/.idea/misc.xml b/.idea/misc.xml index 6f29fee..5af9c98 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,3 @@ - diff --git a/src/DrawingBus.java b/src/DrawingBus.java index 1c8f8af..fc783f1 100644 --- a/src/DrawingBus.java +++ b/src/DrawingBus.java @@ -1,2 +1,136 @@ -package PACKAGE_NAME;public class DrawingBus { +import java.awt.*; +public class DrawingBus { + protected EntityBus entityBus; + public EntityBus getEntityBus() { + return entityBus; + } + 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 busWidth = 200; + public int getWidth() { + return busWidth; + } + private final int busHeight = 135; + public int getHeight() { + return busHeight; + } + private IDrawDoors drawingDoors; + public DrawingBus(int speed, double weight, Color bodyColor, int width, int height, int doorsNumber, int doorsType) { + if (width < busWidth || height < busHeight) + return; + pictureWidth = width; + pictureHeight = height; + entityBus = new EntityBus(speed, weight, bodyColor); + switch (doorsType) { + case 1: + drawingDoors = new DrawingDoorsRoundedUp(); + break; + case 2: + drawingDoors = new DrawingDoorsRoundedUpAndDown(); + break; + default: + drawingDoors = new DrawingDoors(); + break; + } + drawingDoors.setNumber(doorsNumber); + } + public void setPosition(int x, int y) { + if (x < 0 || y < 0 || x + busWidth > pictureWidth || y + busHeight > pictureHeight) { + x = 0; + y = 0; + } + _startPosX = x; + _startPosY = y; + } + public boolean canMove(DirectionType direction) { + if (entityBus == null) { + return false; + } + switch (direction) { + case LEFT: + return _startPosX - entityBus.step.get().intValue() > 0; + case UP: + return _startPosY - entityBus.step.get().intValue() > 0; + case RIGHT: + return _startPosX + entityBus.step.get().intValue() + busWidth < pictureWidth; + case DOWN: + return _startPosY + entityBus.step.get().intValue() + busHeight < pictureHeight; + default: + return false; + } + } + public void moveTransport(DirectionType direction) { + if (!canMove(direction) || entityBus == null) + return; + switch (direction) { + //влево + case LEFT: + _startPosX -= entityBus.step.get().intValue(); + break; + //вверх + case UP: + _startPosY -= entityBus.step.get().intValue(); + break; + // вправо + case RIGHT: + _startPosX += entityBus.step.get().intValue(); + break; + //вниз + case DOWN: + _startPosY += entityBus.step.get().intValue(); + break; + } + } + public void drawTransport(Graphics2D graphics2D) { + if (entityBus == null) + return; + BasicStroke pen = new BasicStroke(2); + graphics2D.setStroke(pen); + Color bodyColor = entityBus.getBodyColor(); + //колеса + graphics2D.setPaint(Color.BLACK); + graphics2D.fillOval(_startPosX + 31, _startPosY + 106, 30, 30); + graphics2D.fillOval(_startPosX + 151, _startPosY + 106, 30, 30); + //кузов + graphics2D.setPaint(bodyColor); + graphics2D.fillRect(_startPosX + 6, _startPosY + 31, 200, 90); + //стекла + graphics2D.setPaint(Color.BLUE); + graphics2D.fillRect(_startPosX + 186, _startPosY + 40, 20, 40); + graphics2D.fillOval(_startPosX + 151, _startPosY + 35, 30, 40); + graphics2D.fillOval(_startPosX + 118, _startPosY + 35, 30, 40); + graphics2D.fillOval(_startPosX + 85, _startPosY + 35, 30, 40); + graphics2D.fillOval(_startPosX + 52, _startPosY + 35, 30, 40); + graphics2D.fillOval(_startPosX + 19, _startPosY + 35, 30, 40); + //двери + graphics2D.setPaint(Color.BLACK); + drawingDoors.drawDoors(graphics2D, _startPosX, _startPosY); + //границы троллейбуса + graphics2D.setPaint(Color.BLACK); + graphics2D.drawRect(_startPosX + 6, _startPosY + 31, 200, 90); + graphics2D.drawOval(_startPosX + 151, _startPosY + 35, 30, 40); + graphics2D.drawOval(_startPosX + 118, _startPosY + 35, 30, 40); + graphics2D.drawOval(_startPosX + 85, _startPosY + 35, 30, 40); + graphics2D.drawOval(_startPosX + 52, _startPosY + 35, 30, 40); + graphics2D.drawOval(_startPosX + 19, _startPosY + 35, 30, 40); + graphics2D.drawRect(_startPosX + 186, _startPosY + 40, 20, 40); + //задние фары + graphics2D.setPaint(Color.RED); + graphics2D.fillRect(_startPosX + 6, _startPosY + 91, 10, 20); + graphics2D.setPaint(Color.BLACK); + graphics2D.drawRect(_startPosX + 6, _startPosY + 91, 10, 20); + //передние фары + graphics2D.setPaint(Color.YELLOW); + graphics2D.fillRect(_startPosX + 196, _startPosY + 91, 10, 20); + graphics2D.setPaint(Color.BLACK); + graphics2D.drawRect(_startPosX + 196, _startPosY + 91, 10, 20); + } } diff --git a/src/DrawingDoors.java b/src/DrawingDoors.java index 3ab1642..53df0a9 100644 --- a/src/DrawingDoors.java +++ b/src/DrawingDoors.java @@ -1,7 +1,8 @@ import java.awt.*; -public class DrawingDoors { +public class DrawingDoors implements IDrawDoors{ private DoorsNumber number; - public void getNumber(int x){ + @Override + public void setNumber(int x){ if(x <= 3) number = DoorsNumber.THREE; if(x == 4) @@ -9,6 +10,7 @@ public class DrawingDoors { if(x >= 5) number = DoorsNumber.FIVE; } + @Override public void drawDoors(Graphics2D graphics2D, int _startX, int _startY){ graphics2D.fillRect(_startX+52, _startY+81, 25, 40); graphics2D.fillRect(_startX+85, _startY+81, 25, 40); diff --git a/src/DrawingDoorsRoundedUp.java b/src/DrawingDoorsRoundedUp.java index ec2c0ac..8c781ec 100644 --- a/src/DrawingDoorsRoundedUp.java +++ b/src/DrawingDoorsRoundedUp.java @@ -1,2 +1,30 @@ -package PACKAGE_NAME;public class DrawingDoorsRoundedUp { +import java.awt.*; +public class DrawingDoorsRoundedUp implements IDrawDoors{ + private DoorsNumber number; + @Override + public void setNumber(int x){ + if(x <= 2) + number = DoorsNumber.THREE; + if(x == 4) + number = DoorsNumber.FOUR; + if(x >= 6) + number = DoorsNumber.FIVE; + } + @Override + public void drawDoors(Graphics2D graphics2D, int _startX, int _startY){ + graphics2D.fillRect(_startX+52, _startY+86, 25, 35); + graphics2D.fillOval(_startX+52, _startY+81, 25, 12); + graphics2D.fillRect(_startX+85, _startY+86, 25, 35); + graphics2D.fillOval(_startX+85, _startY+81, 25, 12); + graphics2D.fillRect(_startX+118, _startY+86, 25, 35); + graphics2D.fillOval(_startX+118, _startY+81, 25, 12); + if (number == DoorsNumber.FOUR || number == DoorsNumber.FIVE){ + graphics2D.fillRect(_startX+151, _startY+86, 25, 35); + graphics2D.fillOval(_startX+151, _startY+81, 25, 12); + } + if (number == DoorsNumber.FIVE){ + graphics2D.fillRect(_startX+19, _startY+86, 25, 35); + graphics2D.fillOval(_startX+19, _startY+81, 25, 12); + } + } } diff --git a/src/DrawingDoorsRoundedUpAndDown.java b/src/DrawingDoorsRoundedUpAndDown.java index b0df919..ad962a6 100644 --- a/src/DrawingDoorsRoundedUpAndDown.java +++ b/src/DrawingDoorsRoundedUpAndDown.java @@ -1,2 +1,35 @@ -package PACKAGE_NAME;public class DrawingDoorsRoundedUpAndDown { +import java.awt.*; +public class DrawingDoorsRoundedUpAndDown implements IDrawDoors{ + private DoorsNumber number; + @Override + public void setNumber(int x){ + if(x <= 2) + number = DoorsNumber.THREE; + if(x == 4) + number = DoorsNumber.FOUR; + if(x >= 6) + number = DoorsNumber.FIVE; + } + @Override + public void drawDoors(Graphics2D graphics2D, int _startX, int _startY){ + graphics2D.fillRect(_startX+52, _startY+86, 25, 30); + graphics2D.fillOval(_startX+52, _startY+81, 25, 12); + graphics2D.fillOval(_startX+52, _startY+111, 25, 10); + graphics2D.fillRect(_startX+85, _startY+86, 25, 30); + graphics2D.fillOval(_startX+85, _startY+81, 25, 12); + graphics2D.fillOval(_startX+85, _startY+111, 25, 10); + graphics2D.fillRect(_startX+118, _startY+86, 25, 30); + graphics2D.fillOval(_startX+118, _startY+81, 25, 12); + graphics2D.fillOval(_startX+118, _startY+111, 25, 10); + if (number == DoorsNumber.FOUR || number == DoorsNumber.FIVE){ + graphics2D.fillRect(_startX+151, _startY+86, 25, 30); + graphics2D.fillOval(_startX+151, _startY+81, 25, 12); + graphics2D.fillOval(_startX+151, _startY+111, 25, 10); + } + if (number == DoorsNumber.FIVE){ + graphics2D.fillRect(_startX+19, _startY+86, 25, 30); + graphics2D.fillOval(_startX+19, _startY+81, 25, 12); + graphics2D.fillOval(_startX+19, _startY+111, 25, 10); + } + } } diff --git a/src/DrawingObjectBus.java b/src/DrawingObjectBus.java index ae1c60a..4ecc71b 100644 --- a/src/DrawingObjectBus.java +++ b/src/DrawingObjectBus.java @@ -1,2 +1,28 @@ -package PACKAGE_NAME;public class DrawingObjectBus { +public class DrawingObjectBus implements IMoveableObject{ + private final DrawingBus drawingBus; + public DrawingObjectBus(DrawingBus drawingBus){ + this.drawingBus = drawingBus; + } + @Override + public ObjectParameters getObjectPosition(){ + if(drawingBus == null || drawingBus.getEntityBus() == null) + return null; + return new ObjectParameters(drawingBus.getPosX(), drawingBus.getPosY(), + drawingBus.getWidth(), drawingBus.getHeight()); + } + public int getStep(){ + if(drawingBus.getEntityBus() == null) + return 0; + return drawingBus.getEntityBus().step.get().intValue(); + } + public boolean checkCanMove(DirectionType direction){ + if(drawingBus == null) + return false; + return drawingBus.canMove(direction); + } + public void moveObject(DirectionType direction){ + if(drawingBus == null) + return; + drawingBus.moveTransport(direction); + } } diff --git a/src/DrawingTrolleybus.java b/src/DrawingTrolleybus.java index 7e4b69b..4441ae4 100644 --- a/src/DrawingTrolleybus.java +++ b/src/DrawingTrolleybus.java @@ -1,89 +1,20 @@ import java.awt.*; -public class DrawingTrolleybus { - private EntityTrolleybus entityTrolleybus; - public EntityTrolleybus getEntityTrolleybus() { - return entityTrolleybus; - } - private int _pictureWidth; - private int _pictureHeight; - private int _startPosX; - private int _startPosY; - private final int trolleybusWidth = 200; - private final int trolleybusHeight = 135; - private DrawingDoors drawingDoors; - public boolean init(int speed, double weight, Color bodyColor, Color additionalColor, - boolean roga, boolean battery, int width, int height, int doorsNumber) +public class DrawingTrolleybus extends DrawingBus{ + public DrawingTrolleybus(int speed, double weight, Color bodyColor, Color additionalColor, + boolean roga, boolean battery, int width, int height, int doorsNumber, int doorsType) { - if (width < trolleybusWidth || height < trolleybusHeight) - return false; - _pictureWidth = width; - _pictureHeight = height; - entityTrolleybus = new EntityTrolleybus(); - entityTrolleybus.init(speed, weight, bodyColor, additionalColor, roga, battery); - drawingDoors = new DrawingDoors(); - drawingDoors.getNumber(doorsNumber); - return true; - } - public void setPosition(int x, int y) - { - if (x < 0 || y < 0 || x + trolleybusWidth >= _pictureWidth || y + trolleybusHeight >= _pictureHeight) { - x = 0; - y = 0; - } - _startPosX = x; - _startPosY = y; - } - public void moveTransport(DirectionType direction) { - if (entityTrolleybus == null) - return; - switch (direction) { - //влево - case LEFT: - if (_startPosX - entityTrolleybus.step.get().intValue() > 0) - _startPosX -= entityTrolleybus.step.get().intValue(); - break; - //вверх - case UP: - if (_startPosY - entityTrolleybus.step.get().intValue() > 0) - _startPosY -= entityTrolleybus.step.get().intValue(); - break; - // вправо - case RIGHT: - if (_startPosX + trolleybusWidth + entityTrolleybus.step.get().intValue() < _pictureWidth) - _startPosX += entityTrolleybus.step.get().intValue(); - break; - //вниз - case DOWN: - if (_startPosY + trolleybusHeight + entityTrolleybus.step.get().intValue() < _pictureHeight) - _startPosY += entityTrolleybus.step.get().intValue(); - break; - } + super(speed, weight, bodyColor, width, height, doorsNumber,doorsType); + if (entityBus != null) + entityBus = new EntityTrolleybus(speed, weight, bodyColor, additionalColor, roga, battery); } public void drawTransport(Graphics2D graphics2D) { - if (entityTrolleybus == null) + if (!(entityBus instanceof EntityTrolleybus)) return; + EntityTrolleybus entityTrolleybus = (EntityTrolleybus) entityBus; BasicStroke pen = new BasicStroke(2); graphics2D.setStroke(pen); - Color bodyColor = entityTrolleybus.getBodyColor(); Color additionalColor = entityTrolleybus.getAdditionalColor(); - //колеса - graphics2D.setPaint(Color.BLACK); - graphics2D.fillOval(_startPosX + 31, _startPosY + 106, 30, 30); - graphics2D.fillOval(_startPosX + 151, _startPosY + 106, 30, 30); - //кузов - graphics2D.setPaint(bodyColor); - graphics2D.fillRect(_startPosX + 6, _startPosY + 31, 200, 90); - //стекла - graphics2D.setPaint(Color.BLUE); - graphics2D.fillRect(_startPosX + 186, _startPosY + 40, 20, 40); - graphics2D.fillOval(_startPosX + 151, _startPosY + 35, 30, 40); - graphics2D.fillOval(_startPosX + 118, _startPosY + 35, 30, 40); - graphics2D.fillOval(_startPosX + 85, _startPosY + 35, 30, 40); - graphics2D.fillOval(_startPosX + 52, _startPosY + 35, 30, 40); - graphics2D.fillOval(_startPosX + 19, _startPosY + 35, 30, 40); - //двери - graphics2D.setPaint(Color.BLACK); - drawingDoors.drawDoors(graphics2D, _startPosX, _startPosY); + super.drawTransport(graphics2D); //рога graphics2D.setPaint(Color.BLACK); if (entityTrolleybus.getRoga()) { @@ -102,24 +33,5 @@ public class DrawingTrolleybus { graphics2D.drawLine(_startPosX + 178, _startPosY + 111, _startPosX + 189, _startPosY + 111); graphics2D.drawLine(_startPosX + 189, _startPosY + 111, _startPosX + 183, _startPosY + 119); } - //границы троллейбуса - graphics2D.setPaint(Color.BLACK); - graphics2D.drawRect(_startPosX + 6, _startPosY + 31, 200, 90); - graphics2D.drawOval(_startPosX + 151, _startPosY + 35, 30, 40); - graphics2D.drawOval(_startPosX + 118, _startPosY + 35, 30, 40); - graphics2D.drawOval(_startPosX + 85, _startPosY + 35, 30, 40); - graphics2D.drawOval(_startPosX + 52, _startPosY + 35, 30, 40); - graphics2D.drawOval(_startPosX + 19, _startPosY + 35, 30, 40); - graphics2D.drawRect(_startPosX + 186, _startPosY + 40, 20, 40); - //задние фары - graphics2D.setPaint(Color.RED); - graphics2D.fillRect(_startPosX + 6, _startPosY + 91, 10, 20); - graphics2D.setPaint(Color.BLACK); - graphics2D.drawRect(_startPosX + 6, _startPosY + 91, 10, 20); - //передние фары - graphics2D.setPaint(Color.YELLOW); - graphics2D.fillRect(_startPosX + 196, _startPosY + 91, 10, 20); - graphics2D.setPaint(Color.BLACK); - graphics2D.drawRect(_startPosX + 196, _startPosY + 91, 10, 20); } } \ No newline at end of file diff --git a/src/EntityBus.java b/src/EntityBus.java index 2eb5b6c..30392e6 100644 --- a/src/EntityBus.java +++ b/src/EntityBus.java @@ -1,2 +1,22 @@ -package PACKAGE_NAME;public class EntityBus { -} +import java.awt.*; +import java.util.function.Supplier; +public class EntityBus { + 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 EntityBus(int speed, double weight, Color bodyColor) { + this.speed = speed; + this.weight = weight; + this.bodyColor = bodyColor; + } +} \ No newline at end of file diff --git a/src/EntityTrolleybus.java b/src/EntityTrolleybus.java index c468cd9..84f8463 100644 --- a/src/EntityTrolleybus.java +++ b/src/EntityTrolleybus.java @@ -1,19 +1,5 @@ import java.awt.*; -import java.util.function.Supplier; - -public class EntityTrolleybus { - 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 EntityTrolleybus extends EntityBus{ private Color additionalColor; public Color getAdditionalColor(){return additionalColor;} private boolean roga; @@ -24,12 +10,9 @@ public class EntityTrolleybus { public boolean getBattery() { return battery; } - public Supplier step = () -> (double) speed * 100 / weight; - public void init(int speed, double weight, Color bodyColor, Color + public EntityTrolleybus(int speed, double weight, Color bodyColor, Color additionalColor, boolean roga, boolean battery) { - this.speed = speed; - this.weight = weight; - this.bodyColor = bodyColor; + super(speed, weight, bodyColor); this.additionalColor = additionalColor; this.roga = roga; this.battery = battery; diff --git a/src/FrameTrolleybus.java b/src/FrameTrolleybus.java index c684551..6bb4fc4 100644 --- a/src/FrameTrolleybus.java +++ b/src/FrameTrolleybus.java @@ -6,7 +6,9 @@ import java.io.File; import java.io.IOException; import java.util.Random; public class FrameTrolleybus extends JFrame { - private DrawingTrolleybus drawingTrolleybus; + private DrawingBus drawingBus; + private AbstractStrategy abstractStrategy; + private JComboBox comboBoxStrategy; private final JComponent pictureBoxTrolleybus; public FrameTrolleybus() throws IOException { super("Троллейбус"); @@ -16,17 +18,22 @@ public class FrameTrolleybus extends JFrame { public void paintComponent(Graphics graphics){ super.paintComponent(graphics); Graphics2D graphics2D = (Graphics2D) graphics; - if (drawingTrolleybus != null) drawingTrolleybus.drawTransport(graphics2D); + if (drawingBus != null) drawingBus.drawTransport(graphics2D); super.repaint(); } }; - JButton createButton = new JButton("Создать"); + comboBoxStrategy = new JComboBox<>(new String[]{"к центру", "к границе"}); + JButton stepButton = new JButton("Шаг"); + JButton createBusButton = new JButton("Создать автобус"); + JButton createTrolleybusButton = new JButton("Создать троллейбус"); JButton rightButton = new JButton(new ImageIcon(ImageIO.read(new File("img/right.png")))); JButton leftButton = new JButton(new ImageIcon(ImageIO.read(new File("img/left.png")))); JButton upButton = new JButton(new ImageIcon(ImageIO.read(new File("img/up.png")))); JButton downButton = new JButton(new ImageIcon(ImageIO.read(new File("img/down.png")))); pictureBoxTrolleybus.setBounds( 0, 0, getContentPane().getWidth(), getContentPane().getHeight()); - createButton.addActionListener(e -> buttonCreateTrolleybus()); + createBusButton.addActionListener(e -> buttonCreateBusClick()); + createTrolleybusButton.addActionListener(e -> buttonCreateTrolleybusClick()); + stepButton.addActionListener(e -> buttonStepClick()); rightButton.setActionCommand("right"); rightButton.addActionListener(this::buttonMoveClick); leftButton.setActionCommand("left"); @@ -37,14 +44,19 @@ public class FrameTrolleybus extends JFrame { downButton.addActionListener(this::buttonMoveClick); setLayout(new BorderLayout()); JPanel panelTrolleybus = new JPanel(new BorderLayout()); - JPanel createPanel = new JPanel(new BorderLayout()); - createPanel.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2)); - createPanel.add(createButton, BorderLayout.SOUTH); + JPanel createPanel = new JPanel(new GridBagLayout()); + GridBagConstraints constraints = new GridBagConstraints(); + constraints.gridx = 0; + constraints.gridy = 0; + createPanel.add(createBusButton, constraints); + constraints.gridx = 1; + constraints.gridy = 0; + createPanel.add(createTrolleybusButton, constraints); JPanel movementPanel = new JPanel(new GridBagLayout()); JPanel rightPanel = new JPanel(new BorderLayout()); + JPanel leftPanel = new JPanel(new BorderLayout()); rightPanel.add(movementPanel, BorderLayout.SOUTH); rightButton.setPreferredSize(new Dimension(30,30)); - GridBagConstraints constraints = new GridBagConstraints(); constraints.gridx = 2; constraints.gridy = 1; constraints.insets.left = constraints.insets.top = constraints.insets.bottom = constraints.insets.right = 2; @@ -61,46 +73,101 @@ public class FrameTrolleybus 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(pictureBoxTrolleybus); + rightPanel.add(stepPanel, BorderLayout.NORTH); + leftPanel.add(createPanel, BorderLayout.SOUTH); panelTrolleybus.add(rightPanel, BorderLayout.EAST); - panelTrolleybus.add(createPanel, BorderLayout.WEST); + panelTrolleybus.add(leftPanel, BorderLayout.WEST); add(panelTrolleybus,BorderLayout.CENTER); setVisible(true); } - private void buttonCreateTrolleybus() { + private void buttonCreateTrolleybusClick() { Random random = new Random(); - drawingTrolleybus = new DrawingTrolleybus(); pictureBoxTrolleybus.setBounds(0,0,getContentPane().getWidth(),getContentPane().getHeight()); - drawingTrolleybus.init(random.nextInt(200) + 100, random.nextInt(2000) + 1000, + drawingBus = new DrawingTrolleybus(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(), pictureBoxTrolleybus.getWidth(), pictureBoxTrolleybus.getHeight(), - (random.nextInt(3)+1)*2); - drawingTrolleybus.setPosition(random.nextInt(90) + 10, random.nextInt(90) + 10); + (random.nextInt(3)+1)*2, random.nextInt(3)); + drawingBus.setPosition(random.nextInt(90) + 10, random.nextInt(90) + 10); draw(); } + private void buttonCreateBusClick(){ + Random random = new Random(); + pictureBoxTrolleybus.setBounds(0,0,getContentPane().getWidth(),getContentPane().getHeight()); + drawingBus = new DrawingBus( + random.nextInt(200) + 100, + random.nextInt(2000) + 1000, + new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)), + pictureBoxTrolleybus.getWidth(), + pictureBoxTrolleybus.getHeight(), + (random.nextInt(3)+1)*2, + random.nextInt(3)); + drawingBus.setPosition(random.nextInt(90) + 10, random.nextInt(90) + 10); + draw(); + } + private void buttonStepClick(){ + if (drawingBus == 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 DrawingObjectBus(drawingBus), pictureBoxTrolleybus.getWidth(), pictureBoxTrolleybus.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(drawingTrolleybus == null || drawingTrolleybus.getEntityTrolleybus() == null) + if(drawingBus == null || drawingBus.getEntityBus() == null) return; switch (event.getActionCommand()) { case "left": - drawingTrolleybus.moveTransport(DirectionType.LEFT); + drawingBus.moveTransport(DirectionType.LEFT); break; case "right": - drawingTrolleybus.moveTransport(DirectionType.RIGHT); + drawingBus.moveTransport(DirectionType.RIGHT); break; case "up": - drawingTrolleybus.moveTransport(DirectionType.UP); + drawingBus.moveTransport(DirectionType.UP); break; case "down": - drawingTrolleybus.moveTransport(DirectionType.DOWN); + drawingBus.moveTransport(DirectionType.DOWN); break; } draw(); } private void draw() { - if (drawingTrolleybus == null) + if (drawingBus == null) { return; } diff --git a/src/IDrawDoors.java b/src/IDrawDoors.java index 2cc9af2..2cc00b1 100644 --- a/src/IDrawDoors.java +++ b/src/IDrawDoors.java @@ -1,2 +1,5 @@ -package PACKAGE_NAME;public interface IDrawDoors { +import java.awt.*; +public interface IDrawDoors { + void setNumber(int x); + void drawDoors(Graphics2D graphics2D, int _startX, int _startY); } diff --git a/src/IMoveableObject.java b/src/IMoveableObject.java index 7c1970d..329d7ad 100644 --- a/src/IMoveableObject.java +++ b/src/IMoveableObject.java @@ -1,2 +1,6 @@ -package PACKAGE_NAME;public interface IMoveableObject { +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 index ff02190..29a93bd 100644 --- a/src/MoveToBorder.java +++ b/src/MoveToBorder.java @@ -1,2 +1,30 @@ -package PACKAGE_NAME;public class MoveToBorder { +public class MoveToBorder extends AbstractStrategy{ + @Override + protected boolean isTargetDestination() { + var objParams = getObjectParameters(); + if (objParams == null) { + return false; + } + return objParams.getRightBorder() + getStep() >= getFieldWidth() && + objParams.getDownBorder() + getStep() >= getFieldHeight(); + } + @Override + protected void moveToTarget() { + var objParams = getObjectParameters(); + if (objParams == null) { + return; + } + var diffX = objParams.getRightBorder() - getFieldWidth(); + if (Math.abs(diffX) >= getStep()) { + if (diffX < 0) { + moveRight(); + } + } + var diffY = objParams.getDownBorder() - getFieldHeight(); + if (Math.abs(diffY) >= getStep()) { + if (diffY < 0) { + moveDown(); + } + } + } } diff --git a/src/ObjectParameters.java b/src/ObjectParameters.java index b8149d0..2026666 100644 --- a/src/ObjectParameters.java +++ b/src/ObjectParameters.java @@ -1,2 +1,19 @@ -package PACKAGE_NAME;public class ObjectParameters { +public class ObjectParameters { + private final int POS_X; + private final int POS_Y; + private final int WIDTH; + private final int HEIGHT; + public int getLeftBorder() {return POS_X;} + public int getTopBorder() {return POS_Y;} + public int getRightBorder() {return POS_X + WIDTH;} + public int getDownBorder() {return POS_Y + HEIGHT;} + public int getObjectMiddleHorizontal() {return POS_X + this.WIDTH / 2;} + public int getObjectMiddleVertical() {return POS_Y + this.HEIGHT / 2;} + public ObjectParameters(int x, int y, int width, int height) + { + POS_X = x; + POS_Y = y; + WIDTH = width; + HEIGHT = height; + } }