Compare commits

...

9 Commits
main ... Lab02

Author SHA1 Message Date
DyCTaTOR
70f208be56 2 2023-12-19 22:44:10 +04:00
DyCTaTOR
ce4309fe95 Продолжение работы 2023-12-19 19:39:16 +04:00
DyCTaTOR
0338792f12 Продолжение 2 лабы 2023-12-19 16:57:13 +04:00
DyCTaTOR
34805d3ec6 Начало 2023-12-06 11:25:08 +04:00
DyCTaTOR
46f5f0f3e8 Merge branch 'Lab1' of http://student.git.athene.tech/StroevVladimir/PIbd-22-Stroev-V.M.-Monorail.Hard into Lab02 2023-12-06 10:14:59 +04:00
DyCTaTOR
8be5839af0 Финал 2023-12-06 10:09:34 +04:00
DyCTaTOR
5bb5a8e93e Начало 2023-12-06 00:23:52 +04:00
DyCTaTOR
8737449c24 Финал 2023-12-05 17:24:43 +04:00
DyCTaTOR
41bc1ec037 Начало 2023-11-22 14:51:08 +04:00
21 changed files with 992 additions and 0 deletions

View File

View File

@ -0,0 +1,26 @@
package Directions;
public enum CountWheels{
Min(2),
Mid(3),
Max(4);
private final int val;
private CountWheels(int val){
this.val = val;
}
public int getCountWheels(){
return val;
}
public static CountWheels fromNumberToEnum(int number) {
try{
for (CountWheels countWheels : CountWheels.values()) {
if (countWheels.getCountWheels() == number) {
return countWheels;
}
}
}catch(NumberFormatException e){
}
return Min;
}
}

View File

@ -0,0 +1,15 @@
package Directions;
public enum Direction{
UP(1),
DOWN(2),
LEFT(3),
RIGHT(4);
private final int val;
private Direction(int val){
this.val = val;
}
public int getDirection(){
return val;
}
}

View File

@ -0,0 +1,58 @@
package DrawningObjects;
import java.awt.Color;
import java.awt.Graphics;
import Directions.CountWheels;
public class DrawingWheels{
Graphics g;
Color color;
private CountWheels _countWheels;
public void setNumWheels(int numRollers) {
if (numRollers < 4 || numRollers > 6 ){
_countWheels = CountWheels.fromNumberToEnum(numRollers);
return;
}
_countWheels = CountWheels.fromNumberToEnum(numRollers);
}
public DrawingWheels(Color color, int countWheels){
this.color = color;
setNumWheels(countWheels);
}
public void Draw(Graphics g, int _startPosX, int _startPosY){
switch(_countWheels){
case Min:
g.setColor(Color.BLACK);
g.drawOval(_startPosX + 20, _startPosY + 35, 15, 15);
g.drawOval(_startPosX + 110, _startPosY + 35, 15, 15);
g.setColor(color);
g.fillOval(_startPosX + 20, _startPosY + 35, 15, 15);
g.fillOval(_startPosX + 110, _startPosY + 35, 15, 15);
break;
case Mid:
g.setColor(Color.BLACK);
g.drawOval(_startPosX + 20, _startPosY + 35, 15, 15);
g.drawOval(_startPosX + 65, _startPosY + 35, 15, 15);
g.drawOval(_startPosX + 110, _startPosY + 35, 15, 15);
g.setColor(color);
g.fillOval(_startPosX + 20, _startPosY + 35, 15, 15);
g.fillOval(_startPosX + 65, _startPosY + 35, 15, 15);
g.fillOval(_startPosX + 110, _startPosY + 35, 15, 15);
break;
case Max:
g.setColor(Color.BLACK);
g.drawOval(_startPosX + 20, _startPosY + 35, 15, 15);
g.drawOval(_startPosX + 50, _startPosY + 35, 15, 15);
g.drawOval( _startPosX + 80, _startPosY + 35, 15, 15);
g.drawOval(_startPosX + 110, _startPosY + 35, 15, 15);
g.setColor(color);
g.fillOval(_startPosX + 20, _startPosY + 35, 15, 15);
g.fillOval(_startPosX + 50, _startPosY + 35, 15, 15);
g.fillOval(_startPosX + 80, _startPosY + 35, 15, 15);
g.fillOval(_startPosX + 110, _startPosY + 35, 15, 15);
break;
}
}
}

View File

@ -0,0 +1,171 @@
package DrawningObjects;
import java.awt.*;
import javax.swing.JPanel;
import Directions.Direction;
import Entities.*;
import java.util.Random;
public class DrawningMonorail extends JPanel {
private EntityMonorail entity;
private int pictureWidth;
private int pictureHeight;
private int _startPosX;
private int _startPos ;
private int rollVar;
public int relWidth = 150;
private int relHeight = 46;
Random rd = new Random();
public DrawingWheels _drawingWheels;
public DrawningMonorail(int wheelCount ,int rollVar,int speed, double weight, Color bodyColor, int width, int height){
if (relWidth >= width || relHeight >= height)
{
return;
}
this.rollVar = rollVar;
pictureWidth = width;
pictureHeight = height;
entity = new EntityMonorail(wheelCount, speed, weight, bodyColor);
_drawingWheels = new DrawingWheels(entity.getBodyColor(), wheelCount);
}
public EntityMonorail getEntityMonorail(){
return entity;
}
protected void setEntityMonorail(EntityMonorail entity){
this.entity = entity;
}
protected DrawningMonorail(int wheelCount, int rollVar, int speed, double weight, Color bodyColor, int width,
int height, int carWidth, int carHeight){
if (relWidth >= width || relHeight >= height)
{
return;
}
this.rollVar = rollVar;
pictureWidth = width;
pictureHeight = height;
relWidth = carWidth;
relHeight = carHeight;
entity = new EntityMonorail(wheelCount, speed, weight, bodyColor);
}
public void setPosition(int x, int y) {
if (x < 0 || x > pictureWidth - relWidth)
{
x = 0;
}
if (y < 0 || y > pictureHeight - relHeight)
{
y = 0;
}
_startPosX = x;
_startPos = y;
}
public void moveTransport(Direction dr) {
if (entity == null) {
return;
}
switch (dr) {
case LEFT:
_startPosX -= (int)entity.getStep();
break;
case RIGHT:
_startPosX += (int)entity.getStep();
break;
case UP:
_startPos -= (int)entity.getStep();
break;
case DOWN:
_startPos += (int)entity.getStep();
break;
}
}
public int getPosX(){
return _startPosX;
}
public int getPosY(){
return _startPos;
}
public int getHeight(){
return relHeight;
}
public int getWidth(){
return relWidth;
}
public boolean canMove(Direction direction)
{
if (entity == null)
{
return false;
}
switch (direction) {
case LEFT:
return _startPosX - entity.getStep() > 0;
case UP:
return _startPos - entity.getStep() > 0;
case RIGHT:
return _startPosX + entity.getStep() < pictureWidth - relWidth;
case DOWN:
return _startPos + entity.getStep() < pictureHeight - relHeight;
default:
return false;
}
}
public void DrawTransport(Graphics g) {
if (entity == null) {
return;
}
relWidth = 150;
//Колёса
g.setColor(Color.BLACK);
g.drawRect(_startPosX + 22, _startPos + 36, 40, 8);
g.drawRect(_startPosX+82, _startPos + 36, 40, 8);
g.drawOval(_startPosX + 3, _startPos + 37, 30, 8);
g.drawOval(_startPosX + 110, _startPos + 37, 29, 8);
g.fillRect(_startPosX + 22, _startPos + 36, 40, 8);
g.fillRect(_startPosX + 82, _startPos + 36, 40, 8);
g.fillOval( _startPosX + 3, _startPos + 37, 30, 8);
g.fillOval( _startPosX + 110, _startPos + 37, 29, 8);
_drawingWheels.Draw(g, _startPosX, _startPos);
//Кабина
g.setColor(entity.getBodyColor());
g.fillRect(_startPosX + 10, _startPos + 20, 120, 16);
g.setColor(Color.BLACK);
g.drawRect(_startPosX + 10, _startPos + 20, 120, 16);
int[] xp1 = new int[]{_startPosX+10,_startPosX+130,_startPosX+130,_startPosX+13};
int[] yp1 = new int[]{_startPos + 20,_startPos + 20,_startPos + 5,_startPos + 5};
Polygon pol1 = new Polygon(xp1, yp1, yp1.length);
g.setColor(entity.getBodyColor());
g.fillPolygon(pol1);
g.setColor(Color.BLACK);
g.drawPolygon(pol1);
//Дверь
g.setColor(Color.WHITE);
g.fillRect(_startPosX + 49, _startPos + 9, 12, 22);
g.setColor(Color.BLACK);
g.drawRect(_startPosX + 49, _startPos + 9, 12, 22);
//Окна и прочее
g.setColor(Color.CYAN);
g.drawRect(_startPosX + 20, _startPos + 8, 10, 10);
g.drawRect(_startPosX + 35, _startPos + 8, 10, 10);
g.drawRect(_startPosX + 117, _startPos + 8, 10, 10);
g.setColor(Color.BLACK);
g.drawRect(_startPosX + 130, _startPos + 10, 2, 22);
g.fillRect(_startPosX + 130, _startPos + 10, 2, 22);
}
}

View File

@ -0,0 +1,95 @@
package DrawningObjects;
import java.awt.*;
import Entities.*;
public class DrawningSecondMonorail extends DrawningMonorail{
public DrawningSecondMonorail(int wheelCount, int rollVar, int speed, double weight, Color bodyColor, Color
additionalColor, boolean monorails, boolean secondCabin, int width, int height){
super(wheelCount, rollVar, speed, weight, bodyColor, width, height, 110, 60);
if(secondCabin){
relWidth = 290;
}
if (getEntityMonorail() != null)
{
setEntityMonorail(new EntitySecondMonorail(wheelCount, speed,
weight, bodyColor, additionalColor, monorails, secondCabin));
}
}
@Override
public void DrawTransport(Graphics g){
super.DrawTransport(g);
if (!(getEntityMonorail() instanceof EntitySecondMonorail secondMonorail)) {
return;
}
//Магнитная рельса
if(secondMonorail.getMonorails())
{
g.drawRect(getPosX(), getPosY() + 50, 140, 10);
g.fillRect(getPosX(), getPosY() + 50, 140, 10);
g.setColor(Color.WHITE);
g.fillRect(getPosX() + 5, getPosY() + 53, 130, 5);
}
if (secondMonorail.getSecondCabin())
{
//низ
g.setColor(Color.BLACK);
g.drawRect(getPosX() + 162, getPosY() + 36, 40, 8);
g.drawRect(getPosX() + 222, getPosY() + 36, 40, 8);
g.drawOval(getPosX() + 143, getPosY() + 37, 30, 8);
g.drawOval(getPosX() + 250, getPosY() + 37, 29, 8);
g.setColor(Color.BLACK);
g.fillRect(getPosX() + 162, getPosY() + 36, 40, 8);
g.fillRect(getPosX() + 222, getPosY() + 36, 40, 8);
g.fillOval(getPosX() + 143, getPosY() + 37, 30, 8);
g.fillOval(getPosX() + 250, getPosY() + 37, 29, 8);
_drawingWheels.Draw(g, getPosX() + 140, getPosY());
if (secondMonorail.getMonorails())
{
g.setColor(Color.BLACK);
g.drawRect(getPosX() + 140, getPosY() + 50, 145, 10);
g.fillRect(getPosX() + 140, getPosY() + 50, 145, 10);
g.setColor(Color.WHITE);
g.fillRect(getPosX() + 135, getPosY() + 53, 145, 5);
}
//Кабина
g.setColor(Color.BLACK);
g.drawRect(getPosX() + 150, getPosY() + 20, 120, 16);
g.fillRect(getPosX() + 150, getPosY() + 20, 120, 16);
int[] xp2 = new int[]{getPosX() + 150,getPosX() + 270,getPosX() + 267,getPosX() + 150};
int[] yp2 = new int[] {getPosX() + 20, getPosY() + 20, getPosY() + 5, getPosY() + 6};
Polygon pol2 = new Polygon(xp2, yp2, yp2.length);
g.setColor(secondMonorail.getAdditionalColor());
g.fillPolygon(pol2);
g.setColor(Color.BLACK);
g.drawPolygon(pol2);
//дверь
g.setColor(Color.WHITE);
g.fillRect(getPosX() + 189, getPosY() + 9, 12, 22);
g.setColor(Color.BLACK);
g.drawRect(getPosX() + 189, getPosY() + 9, 12, 22);
//Окна и прочее
g.setColor(Color.CYAN);
g.drawRect(getPosX() + 160, getPosY() + 8, 10, 10);
g.drawRect(getPosX() + 175, getPosY() + 8, 10, 10);
g.drawRect(getPosX() + 257, getPosY() + 8, 10, 10);
g.setColor(Color.BLACK);
g.drawRect(getPosX() + 270, getPosY() + 10, 2, 22);
g.fillRect(getPosX() + 270, getPosY() + 10, 2, 22);
}
}
}

View File

@ -0,0 +1,52 @@
package Entities;
import java.awt.*;
public class EntityMonorail {
private int speed;
private int wheelCount;
private double weight;
private Color bodyColor;
public double getStep() {
return (double) speed * 130 / weight;
}
public EntityMonorail(int wheelCount, int speed, double weight, Color bodyColor) {
setSpeed(speed);
setWeight(weight);
setBodyColor(bodyColor);
setWheelCount(wheelCount);
}
public int getSpeed() {
return speed;
}
private void setSpeed(int speed) {
this.speed = speed;
}
public double getWeight() {
return weight;
}
private void setWeight(double weight) {
this.weight = weight;
}
public Color getBodyColor() {
return bodyColor;
}
private void setBodyColor(Color bodyColor) {
this.bodyColor = bodyColor;
}
public int getWheelCount(){
return wheelCount;
}
private void setWheelCount(int wheelCount){
this.wheelCount = wheelCount;
}
}

View File

@ -0,0 +1,39 @@
package Entities;
import java.awt.*;
public class EntitySecondMonorail extends EntityMonorail{
private Color additionalColor;
private boolean monorails;
private boolean secondCabin;
public EntitySecondMonorail(int wheelCount, int speed, double weight,
Color bodyColor, Color additionalColor, boolean monorails, boolean secondCabin){
super(wheelCount, speed,weight, bodyColor);
setAdditionalColor(additionalColor);
setMonorails(monorails);
setSecondCabin(secondCabin);
}
public Color getAdditionalColor() {
return additionalColor;
}
private void setAdditionalColor(Color additionalColor) {
this.additionalColor = additionalColor;
}
public boolean getMonorails() {
return monorails;
}
private void setMonorails(boolean monorails) {
this.monorails = monorails;
}
public boolean getSecondCabin() {
return secondCabin;
}
private void setSecondCabin(boolean secondCabin) {
this.secondCabin = secondCabin;
}
}

253
Monorail/MonoFrame.java Normal file
View File

@ -0,0 +1,253 @@
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import javax.swing.*;
import Directions.Direction;
import DrawningObjects.DrawningMonorail;
import DrawningObjects.DrawningSecondMonorail;
import MovementStrategy.*;
import java.util.Random;
class Form extends JFrame{
private DrawningMonorail _drawningMonorail;
private AbstractStrategy abstractStrategy;
private JLabel pictureBoxMonorail;
private JLabel wheelsLabel;
private JPanel mainPanel;
private JButton btUp;
private JButton btDown;
private JButton btRight;
private JButton btLeft;
private JButton btCreateMonorail;
private JButton btCreateSecondMonorail;
private JButton btStep;
private JComboBox<String> comboBoxWheel;
private JComboBox<String> comboBoxStrategy;
private JTextField wheelsTextField;
Random rd = new Random();
public Form(){
initComponents();
}
private void Draw(){
if(_drawningMonorail == null){
return;
}
BufferedImage bmp = new BufferedImage(pictureBoxMonorail.getWidth(),
pictureBoxMonorail.getHeight(),BufferedImage.TYPE_INT_ARGB);
Graphics2D gr = bmp.createGraphics();
_drawningMonorail.DrawTransport(gr);
ImageIcon imageIcon = new ImageIcon(bmp);
pictureBoxMonorail.setIcon(imageIcon);
}
private void initComponents(){
mainPanel = new JPanel();
btCreateMonorail = new JButton("Создать Монорельс");
btCreateSecondMonorail = new JButton("Создать два монорельса");
btUp = new JButton(new ImageIcon("resources/upper-arrow.png"));
btDown = new JButton(new ImageIcon("resources/down-arrow.png"));
btLeft = new JButton(new ImageIcon("resources/left-arrow.png"));
btRight = new JButton(new ImageIcon("resources/right-arrow.png"));
pictureBoxMonorail = new JLabel();
wheelsLabel = new JLabel("Кол-во колёс: ");
wheelsTextField = new JTextField();
setLayout(new BorderLayout());
add(mainPanel, BorderLayout.CENTER);
mainPanel.setLayout(null);
pictureBoxMonorail.setBounds(0, 0, 882, 453);
mainPanel.add(pictureBoxMonorail);
wheelsLabel.setBounds(22, 410, 150, 29);
mainPanel.add(wheelsLabel);
wheelsTextField.setBounds(150, 410, 80, 29);
mainPanel.add(wheelsTextField);
btCreateMonorail.setBounds(250, 410, 100, 29);
mainPanel.add(btCreateMonorail);
btCreateMonorail.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
buttonCreateActionPerformed(e);
}
});
btCreateSecondMonorail.setBounds(500, 409, 150, 30);
mainPanel.add(btCreateSecondMonorail);
btCreateSecondMonorail.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
btCreateSecondMonorail_Click(e);
}
});
btStep = new JButton("Шаг");
mainPanel.add(btStep);
btStep.setBounds(794, 55, 76, 30);
btStep.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
btStep_Click(e);
}
});
comboBoxWheel = new JComboBox<>();
mainPanel.add(comboBoxWheel);
comboBoxWheel.setModel(new DefaultComboBoxModel<>(new String[] { "1", "2", "3" }));
comboBoxWheel.setBounds(767, 232, 103, 28);
comboBoxStrategy = new JComboBox<>();
mainPanel.add(comboBoxStrategy);
comboBoxStrategy.setModel(new DefaultComboBoxModel<>(new String[] { "0", "1" }));
comboBoxStrategy.setBounds(767, 12, 103, 28);
btUp.setBounds(790, 355, 40, 40);
mainPanel.add(btUp);
ImageIcon iconUp = new ImageIcon("resources/upper-arrow.png");
btUp.setIcon(iconUp);
btUp.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
buttonMoveActionPerformed(btUp, e);
}
});
btDown.setBounds(790, 401, 40, 40);
mainPanel.add(btDown);
ImageIcon iconDown = new ImageIcon("resources/down-arrow.png");
btDown.setIcon(iconDown);
btDown.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
buttonMoveActionPerformed(btDown, e);
}
});
btLeft.setBounds(744, 401, 40, 40);
mainPanel.add(btLeft);
ImageIcon iconLeft = new ImageIcon("resources/left-arrow.png");
btLeft.setIcon(iconLeft);
btLeft.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
buttonMoveActionPerformed(btLeft, e);
}
});
btRight.setBounds(836, 401, 40, 40);
mainPanel.add(btRight);
ImageIcon iconRight = new ImageIcon("resources/right-arrow.png");
btRight.setIcon(iconRight);
btRight.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
buttonMoveActionPerformed(btRight, e);
}
});
btUp.setName("buttonUp");
btDown.setName("buttonDown");
btLeft.setName("buttonLeft");
btRight.setName("buttonRight");
setPreferredSize(new Dimension(900, 500));
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
pack();
}
protected void buttonCreateActionPerformed(ActionEvent e) {
int wheelCount;
try {
wheelCount = Integer.parseInt(wheelsTextField.getText());
} catch (NumberFormatException ex) {
wheelCount = 0;
}
if (wheelCount > 0) {
Random random = new Random();
_drawningMonorail = new DrawningMonorail(wheelCount, Integer.parseInt((String) comboBoxWheel.getSelectedItem()),
random.nextInt(200) + 100, (double)random.nextInt(2000) + 1000,
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)),
pictureBoxMonorail.getWidth(), pictureBoxMonorail.getHeight());
_drawningMonorail.setPosition(random.nextInt(90) + 10,
random.nextInt(90) + 10);
Draw();
}
}
protected void btCreateSecondMonorail_Click(ActionEvent e) {
int wheelCount;
try {
wheelCount = Integer.parseInt(wheelsTextField.getText());
} catch (NumberFormatException ex) {
wheelCount = 0;
}
if (wheelCount > 0) {
Random random = new Random();
_drawningMonorail = new DrawningSecondMonorail(wheelCount, Integer.parseInt((String) comboBoxWheel.getSelectedItem()),
random.nextInt(200) + 100,
(double) 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)),
true, true,
pictureBoxMonorail.getWidth(), pictureBoxMonorail.getHeight());
_drawningMonorail.setPosition(random.nextInt(90) + 20, random.nextInt(90) + 20);
Draw();
}
}
protected void btStep_Click (ActionEvent e) {
if (_drawningMonorail == null) {
return;
}
if (comboBoxStrategy.isEnabled()) {
abstractStrategy = switch (comboBoxStrategy.getSelectedIndex()){
case 0 -> new MoveToCenter();
case 1 -> new MoveToBorder();
default -> null;
};
if (abstractStrategy == null) {
return;
}
abstractStrategy.setData(new DrawingObjectMonorail(_drawningMonorail), pictureBoxMonorail.getWidth(), pictureBoxMonorail.getHeight());
comboBoxStrategy.setEnabled(false);
}
if (abstractStrategy == null) {
return;
}
abstractStrategy.makeStep();
Draw();
if (abstractStrategy.getStatus() == Status.Finish) {
comboBoxStrategy.setEnabled(true);
abstractStrategy = null;
}
}
protected void buttonMoveActionPerformed(Object sender, ActionEvent e) {
if (_drawningMonorail == null) {
return;
}
String name = (sender instanceof JButton) ? ((JButton) sender).getName() : "";
switch (name) {
case "buttonUp":
_drawningMonorail.moveTransport(Direction.UP);
break;
case "buttonDown":
_drawningMonorail.moveTransport(Direction.DOWN);
break;
case "buttonLeft":
_drawningMonorail.moveTransport(Direction.LEFT);
break;
case "buttonRight":
_drawningMonorail.moveTransport(Direction.RIGHT);
break;
}
Draw();
}
}
class MonoFrame{
public static void main(String[] args) {
Form fr = new Form();
}
}

View File

@ -0,0 +1,98 @@
package MovementStrategy;
import Directions.Direction;
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(Direction.LEFT);
}
protected boolean moveRight() {
return moveTo(Direction.RIGHT);
}
protected boolean moveUp() {
return moveTo(Direction.UP);
}
protected boolean moveDown() {
return moveTo(Direction.DOWN);
}
protected ObjectParameters getObjectParameters() {
if (moveableObject == null) {
return null;
}
return moveableObject.getObjectsPosition();
}
protected Integer getStep() {
if (state != Status.InProgress) {
return null;
}
if (moveableObject == null) {
return null;
}
return moveableObject.getStep();
}
protected abstract void moveToTarget();
protected abstract boolean isTargetDestination();
private boolean moveTo(Direction directionType) {
if (state != Status.InProgress) {
return false;
}
if (moveableObject == null) {
return false;
}
if (moveableObject.checkCanMove(directionType)) {
moveableObject.moveObject(directionType);
return true;
}
return false;
}
}

View File

@ -0,0 +1,43 @@
package MovementStrategy;
import Directions.Direction;
import DrawningObjects.DrawningMonorail;
public class DrawingObjectMonorail implements IMoveableObject {
private DrawningMonorail drawingMonorail = null;
public DrawingObjectMonorail(DrawningMonorail drawingMonorail) {
this.drawingMonorail = drawingMonorail;
}
@Override
public ObjectParameters getObjectsPosition() {
if (drawingMonorail == null || drawingMonorail.getEntityMonorail() == null) {
return null;
}
return new ObjectParameters(drawingMonorail.getPosX(), drawingMonorail.getPosY(),
drawingMonorail.getWidth(), drawingMonorail.getHeight());
}
@Override
public int getStep() {
if (drawingMonorail == null)
return 0;
return (int) ((drawingMonorail.getEntityMonorail() != null) ?
drawingMonorail.getEntityMonorail().getStep() : 0);
}
@Override
public boolean checkCanMove(Direction direction) {
if (drawingMonorail == null)
return false;
return drawingMonorail.canMove(direction);
}
@Override
public void moveObject(Direction direction) {
if (drawingMonorail != null)
drawingMonorail.moveTransport(direction);
}
}

View File

@ -0,0 +1,14 @@
package MovementStrategy;
import Directions.Direction;
public interface IMoveableObject {
ObjectParameters getObjectsPosition();
int getStep();
boolean checkCanMove(Direction direction);
void moveObject(Direction direction);
}

View File

@ -0,0 +1,36 @@
package MovementStrategy;
public class MoveToBorder extends AbstractStrategy {
@Override
protected boolean isTargetDestination() {
var objParams = getObjectParameters();
if (objParams == null) {
return false;
}
return objParams.rightBorder() <= getFieldWidth() &&
objParams.rightBorder() + getStep() >= getFieldWidth() &&
objParams.downBorder() <= getFieldHeight() &&
objParams.downBorder() + getStep() >= getFieldHeight();
}
@Override
protected void moveToTarget() {
var objParams = getObjectParameters();
if (objParams == null) {
return;
}
var diffX = objParams.objectMiddleHorizontal() - getFieldWidth();
if (Math.abs(diffX) > getStep()) {
if (diffX < 0) {
moveRight();
}
}
var diffY = objParams.objectMiddleVertical() - getFieldHeight();
if (Math.abs(diffY) > getStep()) {
if (diffY < 0) {
moveDown();
}
}
}
}

View File

@ -0,0 +1,40 @@
package MovementStrategy;
public class MoveToCenter extends AbstractStrategy {
@Override
protected boolean isTargetDestination() {
var objParams = getObjectParameters();
if (objParams == null) {
return false;
}
return objParams.objectMiddleHorizontal() <= getFieldWidth() / 2 &&
objParams.objectMiddleHorizontal() + getStep() >= getFieldWidth() / 2 &&
objParams.objectMiddleVertical() <= getFieldHeight() / 2 &&
objParams.objectMiddleVertical() + getStep() >= getFieldHeight() / 2;
}
@Override
protected void moveToTarget() {
var objParams = getObjectParameters();
if (objParams == null) {
return;
}
var diffX = objParams.objectMiddleHorizontal() - getFieldWidth() / 2;
if (Math.abs(diffX) > getStep()) {
if (diffX > 0) {
moveLeft();
} else {
moveRight();
}
}
var diffY = objParams.objectMiddleVertical() - getFieldHeight() / 2;
if (Math.abs(diffY) > getStep()) {
if (diffY > 0) {
moveUp();
} else {
moveDown();
}
}
}
}

View File

@ -0,0 +1,43 @@
package MovementStrategy;
public class ObjectParameters {
private final int x;
private final int y;
private final int width;
private final int height;
public int leftBorder() {
return x;
}
public int topBorder() {
return y;
}
public int rightBorder() {
return x + width;
}
public int downBorder() {
return y + height;
}
public int objectMiddleHorizontal() {
return x + width / 2;
}
public int objectMiddleVertical() {
return y + height / 2;
}
public ObjectParameters(int x, int y, int width, int height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
}

View File

@ -0,0 +1,9 @@
package MovementStrategy;
public enum Status {
NotInit,
InProgress,
Finish
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 281 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 265 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 224 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 264 B