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