финальный рефакторинг
This commit is contained in:
parent
7072feff6d
commit
ae4cefd616
@ -15,7 +15,7 @@ public class DrawningBoat {
|
|||||||
protected Integer _startPosX;
|
protected Integer _startPosX;
|
||||||
protected Integer _startPosY;
|
protected Integer _startPosY;
|
||||||
private int _drawingBoatWidth = 107;
|
private int _drawingBoatWidth = 107;
|
||||||
private int _drawingBoatHeight = 72;
|
private int _drawingBoatHeight = 75;
|
||||||
public int GetPosX(){return _startPosX;}
|
public int GetPosX(){return _startPosX;}
|
||||||
public int GetPosY(){return _startPosY;}
|
public int GetPosY(){return _startPosY;}
|
||||||
public int GetWidth(){return _drawingBoatWidth;}
|
public int GetWidth(){return _drawingBoatWidth;}
|
||||||
@ -80,8 +80,6 @@ public class DrawningBoat {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean moveTransport(MovementDirection direction) {
|
public boolean moveTransport(MovementDirection direction) {
|
||||||
@ -106,8 +104,6 @@ public class DrawningBoat {
|
|||||||
return true;
|
return true;
|
||||||
default:
|
default:
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public void drawBoat(Graphics g) {
|
public void drawBoat(Graphics g) {
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
package Drawnings;
|
package Drawnings;
|
||||||
|
|
||||||
import Drawnings.DrawningBoat;
|
|
||||||
import Entities.*;
|
import Entities.*;
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
|
|
||||||
@ -8,16 +7,9 @@ public class DrawningCatamaran extends DrawningBoat {
|
|||||||
EntityCatamaran EntityBoat;
|
EntityCatamaran EntityBoat;
|
||||||
|
|
||||||
public DrawningCatamaran(int speed, float weight, Color bodyColor, int paddlesType, Color additionalColor, boolean sail, boolean floaters) {
|
public DrawningCatamaran(int speed, float weight, Color bodyColor, int paddlesType, Color additionalColor, boolean sail, boolean floaters) {
|
||||||
super(speed, weight, bodyColor, paddlesType, 110, 68);
|
super(speed, weight, bodyColor, paddlesType, 110, 75);
|
||||||
EntityBoat = new EntityCatamaran(speed, weight, bodyColor, additionalColor, floaters, sail);
|
EntityBoat = new EntityCatamaran(speed, weight, bodyColor, additionalColor, floaters, sail);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private void drawFloater(int y0, Color additionalColor, Graphics2D g2d)
|
private void drawFloater(int y0, Color additionalColor, Graphics2D g2d)
|
||||||
{
|
{
|
||||||
g2d.setColor(additionalColor);
|
g2d.setColor(additionalColor);
|
||||||
@ -75,11 +67,6 @@ public class DrawningCatamaran extends DrawningBoat {
|
|||||||
g2d.drawPolygon(sailPolygon);
|
g2d.drawPolygon(sailPolygon);
|
||||||
g2d.setColor(EntityBoat.getAdditionalColor());
|
g2d.setColor(EntityBoat.getAdditionalColor());
|
||||||
g2d.fillPolygon(sailPolygon);
|
g2d.fillPolygon(sailPolygon);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -13,7 +13,6 @@ public class DrawningOvalPaddles implements IDrawPaddles {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void drawPaddles(Graphics2D g2d, Color color, int _startX, int _startY) {
|
public void drawPaddles(Graphics2D g2d, Color color, int _startX, int _startY) {
|
||||||
g2d.setColor(color);
|
g2d.setColor(color);
|
||||||
|
@ -4,7 +4,6 @@ import java.awt.*;
|
|||||||
|
|
||||||
public class DrawningPaddles implements IDrawPaddles {
|
public class DrawningPaddles implements IDrawPaddles {
|
||||||
private PaddlesCount _paddlesCount;
|
private PaddlesCount _paddlesCount;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setNumber(int paddlesCount) {
|
public void setNumber(int paddlesCount) {
|
||||||
for (PaddlesCount value : PaddlesCount.values()) {
|
for (PaddlesCount value : PaddlesCount.values()) {
|
||||||
@ -13,9 +12,7 @@ public class DrawningPaddles implements IDrawPaddles {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void drawPaddles(Graphics2D g2d, Color color, int _startX, int _startY) {
|
public void drawPaddles(Graphics2D g2d, Color color, int _startX, int _startY) {
|
||||||
g2d.setColor(color);
|
g2d.setColor(color);
|
||||||
|
@ -4,7 +4,6 @@ public enum PaddlesCount {
|
|||||||
One(1),
|
One(1),
|
||||||
Two(2),
|
Two(2),
|
||||||
Three(3);
|
Three(3);
|
||||||
|
|
||||||
final private int EnumNumber;
|
final private int EnumNumber;
|
||||||
PaddlesCount(int enumNumber) {
|
PaddlesCount(int enumNumber) {
|
||||||
EnumNumber = enumNumber;
|
EnumNumber = enumNumber;
|
||||||
@ -12,5 +11,4 @@ public enum PaddlesCount {
|
|||||||
public int getEnumNumber() {
|
public int getEnumNumber() {
|
||||||
return EnumNumber;
|
return EnumNumber;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -27,11 +27,11 @@ public class FormCatamaran extends JFrame {
|
|||||||
Random random = new Random();
|
Random random = new Random();
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case "Drawnings.DrawningBoat":
|
case "Drawnings.DrawningBoat":
|
||||||
_drawningBoat = new DrawningBoat(random.nextInt(100 - 30) + 30, random.nextInt(500 - 100) + 100,
|
_drawningBoat = new DrawningBoat(random.nextInt(70 - 30) + 30, random.nextInt(500 - 100) + 100,
|
||||||
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)), random.nextInt(3));
|
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)), random.nextInt(3));
|
||||||
break;
|
break;
|
||||||
case "Drawnings.DrawningCatamaran":
|
case "Drawnings.DrawningCatamaran":
|
||||||
_drawningBoat = new DrawningCatamaran(random.nextInt(100 - 30) + 30, random.nextInt(500 - 100) + 100,
|
_drawningBoat = new DrawningCatamaran(random.nextInt(70 - 30) + 30, random.nextInt(500 - 100) + 100,
|
||||||
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)), random.nextInt(3),
|
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)), random.nextInt(3),
|
||||||
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());
|
random.nextBoolean(), random.nextBoolean());
|
||||||
@ -96,7 +96,6 @@ public class FormCatamaran extends JFrame {
|
|||||||
}
|
}
|
||||||
if (result)
|
if (result)
|
||||||
Draw();
|
Draw();
|
||||||
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
buttonRight.addActionListener(buttonMoveClickedListener);
|
buttonRight.addActionListener(buttonMoveClickedListener);
|
||||||
@ -104,13 +103,8 @@ public class FormCatamaran extends JFrame {
|
|||||||
buttonLeft.addActionListener(buttonMoveClickedListener);
|
buttonLeft.addActionListener(buttonMoveClickedListener);
|
||||||
buttonUp.addActionListener(buttonMoveClickedListener);
|
buttonUp.addActionListener(buttonMoveClickedListener);
|
||||||
|
|
||||||
String[] itemsComboBox = {
|
comboBoxStrategy.addItem("К центру");
|
||||||
"К центру",
|
comboBoxStrategy.addItem("К краю");
|
||||||
"К краю"
|
|
||||||
};
|
|
||||||
for (String item: itemsComboBox) {
|
|
||||||
comboBoxStrategy.addItem(item);
|
|
||||||
}
|
|
||||||
buttonStrategyStep.addActionListener(new ActionListener() {
|
buttonStrategyStep.addActionListener(new ActionListener() {
|
||||||
@Override
|
@Override
|
||||||
public void actionPerformed(ActionEvent e) {
|
public void actionPerformed(ActionEvent e) {
|
||||||
@ -140,6 +134,7 @@ public class FormCatamaran extends JFrame {
|
|||||||
}
|
}
|
||||||
_strategy.MakeStep();
|
_strategy.MakeStep();
|
||||||
Draw();
|
Draw();
|
||||||
|
comboBoxStrategy.setEnabled(false);
|
||||||
|
|
||||||
if (_strategy.GetStatus() == StrategyStatus.Finish) {
|
if (_strategy.GetStatus() == StrategyStatus.Finish) {
|
||||||
comboBoxStrategy.setEnabled(true);
|
comboBoxStrategy.setEnabled(true);
|
||||||
@ -149,8 +144,6 @@ public class FormCatamaran extends JFrame {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private void Draw() {
|
private void Draw() {
|
||||||
if (_drawningBoat.getEntityBoat() == null)
|
if (_drawningBoat.getEntityBoat() == null)
|
||||||
return;
|
return;
|
||||||
@ -163,7 +156,6 @@ public class FormCatamaran extends JFrame {
|
|||||||
_drawningBoat.drawBoat(g);
|
_drawningBoat.drawBoat(g);
|
||||||
|
|
||||||
RepaintControls();
|
RepaintControls();
|
||||||
|
|
||||||
}
|
}
|
||||||
private void RepaintControls() {
|
private void RepaintControls() {
|
||||||
for (JComponent control : controls) {
|
for (JComponent control : controls) {
|
||||||
@ -182,6 +174,4 @@ public class FormCatamaran extends JFrame {
|
|||||||
controls.add(comboBoxStrategy);
|
controls.add(comboBoxStrategy);
|
||||||
controls.add(buttonStrategyStep);
|
controls.add(buttonStrategyStep);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -7,7 +7,6 @@ public abstract class AbstractStrategy {
|
|||||||
protected int FieldHeight;
|
protected int FieldHeight;
|
||||||
public StrategyStatus GetStatus() { return _state; }
|
public StrategyStatus GetStatus() { return _state; }
|
||||||
|
|
||||||
// Изменить статус, установить поля
|
|
||||||
public void SetData(IMoveableObject moveableObject, int width, int height)
|
public void SetData(IMoveableObject moveableObject, int width, int height)
|
||||||
{
|
{
|
||||||
if (moveableObject == null)
|
if (moveableObject == null)
|
||||||
@ -21,7 +20,6 @@ public abstract class AbstractStrategy {
|
|||||||
FieldHeight = height;
|
FieldHeight = height;
|
||||||
}
|
}
|
||||||
|
|
||||||
// сделать шаг
|
|
||||||
public void MakeStep()
|
public void MakeStep()
|
||||||
{
|
{
|
||||||
if (_state != StrategyStatus.InProgress)
|
if (_state != StrategyStatus.InProgress)
|
||||||
@ -36,15 +34,12 @@ public abstract class AbstractStrategy {
|
|||||||
MoveToTarget();
|
MoveToTarget();
|
||||||
}
|
}
|
||||||
|
|
||||||
// перемещения
|
|
||||||
protected boolean MoveLeft() { return MoveTo(MovementDirection.Left); }
|
protected boolean MoveLeft() { return MoveTo(MovementDirection.Left); }
|
||||||
protected boolean MoveRight() { return MoveTo(MovementDirection.Right); }
|
protected boolean MoveRight() { return MoveTo(MovementDirection.Right); }
|
||||||
protected boolean MoveUp() { return MoveTo(MovementDirection.Up); }
|
protected boolean MoveUp() { return MoveTo(MovementDirection.Up); }
|
||||||
protected boolean MoveDown() { return MoveTo(MovementDirection.Down); }
|
protected boolean MoveDown() { return MoveTo(MovementDirection.Down); }
|
||||||
|
|
||||||
// параметры
|
|
||||||
protected ObjectParameters GetObjectParameters() { return _moveableObject.GetObjectPosition(); }
|
protected ObjectParameters GetObjectParameters() { return _moveableObject.GetObjectPosition(); }
|
||||||
// шаг
|
|
||||||
protected int GetStep()
|
protected int GetStep()
|
||||||
{
|
{
|
||||||
if (_state != StrategyStatus.InProgress)
|
if (_state != StrategyStatus.InProgress)
|
||||||
@ -53,13 +48,8 @@ public abstract class AbstractStrategy {
|
|||||||
}
|
}
|
||||||
return _moveableObject.GetStep();
|
return _moveableObject.GetStep();
|
||||||
}
|
}
|
||||||
// перемещение
|
|
||||||
protected abstract void MoveToTarget();
|
protected abstract void MoveToTarget();
|
||||||
|
|
||||||
// достигнута ли цель
|
|
||||||
protected abstract boolean IsTargetDestination();
|
protected abstract boolean IsTargetDestination();
|
||||||
|
|
||||||
// попытка перемещения по направлению
|
|
||||||
private boolean MoveTo(MovementDirection directionType)
|
private boolean MoveTo(MovementDirection directionType)
|
||||||
{
|
{
|
||||||
if (_state != StrategyStatus.InProgress)
|
if (_state != StrategyStatus.InProgress)
|
||||||
|
@ -15,23 +15,33 @@ public class MoveToBorder extends AbstractStrategy {
|
|||||||
|
|
||||||
protected void MoveToTarget()
|
protected void MoveToTarget()
|
||||||
{
|
{
|
||||||
|
|
||||||
var objParams = GetObjectParameters();
|
var objParams = GetObjectParameters();
|
||||||
if (objParams == null) {
|
if (objParams == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
int diffX = objParams.RightBorder() - FieldWidth;
|
||||||
var diffX = FieldWidth - objParams.ObjectMiddleHorizontal();
|
if (Math.abs(diffX) > GetStep())
|
||||||
if (Math.abs(diffX) > GetStep()) {
|
{
|
||||||
|
if (diffX > 0)
|
||||||
|
{
|
||||||
|
MoveLeft();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
MoveRight();
|
MoveRight();
|
||||||
|
|
||||||
}
|
}
|
||||||
var diffY = FieldHeight - objParams.ObjectMiddleVertical();
|
}
|
||||||
if (Math.abs(diffY) > GetStep()) {
|
int diffY = objParams.DownBorder() - FieldHeight;
|
||||||
|
if (Math.abs(diffY) > GetStep())
|
||||||
|
{
|
||||||
|
if (diffY > 0)
|
||||||
|
{
|
||||||
|
MoveUp();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
MoveDown();
|
MoveDown();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,12 +7,11 @@ public class MoveToCenter extends AbstractStrategy {
|
|||||||
if (objParams == null) {
|
if (objParams == null) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return (objParams.ObjectMiddleHorizontal() <= FieldWidth / 2 &&
|
return (objParams.ObjectMiddleHorizontal() - GetStep() <= FieldWidth / 2 &&
|
||||||
objParams.ObjectMiddleHorizontal() + GetStep() >= FieldWidth / 2 &&
|
objParams.ObjectMiddleHorizontal() + GetStep() >= FieldWidth / 2 &&
|
||||||
objParams.ObjectMiddleVertical() <= FieldHeight / 2 &&
|
objParams.ObjectMiddleVertical() - GetStep() <= FieldHeight / 2 &&
|
||||||
objParams.ObjectMiddleVertical() + GetStep() >= FieldHeight / 2);
|
objParams.ObjectMiddleVertical() + GetStep() >= FieldHeight / 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void MoveToTarget()
|
protected void MoveToTarget()
|
||||||
{
|
{
|
||||||
|
|
||||||
|
@ -1,12 +1,8 @@
|
|||||||
package MovementStrategy;
|
package MovementStrategy;
|
||||||
import Drawnings.*;
|
import Drawnings.*;
|
||||||
import Entities.EntityBoat;
|
|
||||||
|
|
||||||
|
|
||||||
public class MoveableBoat implements IMoveableObject {
|
public class MoveableBoat implements IMoveableObject {
|
||||||
private DrawningBoat _boat = null;
|
private DrawningBoat _boat = null;
|
||||||
|
|
||||||
|
|
||||||
public MoveableBoat(DrawningBoat drawningBoat)
|
public MoveableBoat(DrawningBoat drawningBoat)
|
||||||
{
|
{
|
||||||
_boat = drawningBoat;
|
_boat = drawningBoat;
|
||||||
@ -24,5 +20,4 @@ public class MoveableBoat implements IMoveableObject {
|
|||||||
public int GetStep() { return (int) _boat.getEntityBoat().getStep(); }
|
public int GetStep() { return (int) _boat.getEntityBoat().getStep(); }
|
||||||
public boolean TryMoveObject(MovementDirection direction) { return _boat.moveTransport(direction); }
|
public boolean TryMoveObject(MovementDirection direction) { return _boat.moveTransport(direction); }
|
||||||
public void MoveObject(MovementDirection direction) { _boat.moveTransport(direction); }
|
public void MoveObject(MovementDirection direction) { _boat.moveTransport(direction); }
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user