HardLab2
This commit is contained in:
parent
10984e7206
commit
44e5cb0293
98
src/AbstractStrategy.java
Normal file
98
src/AbstractStrategy.java
Normal file
@ -0,0 +1,98 @@
|
||||
package src;
|
||||
|
||||
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 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(DirectionType directionType) {
|
||||
if (state != Status.InProgress) {
|
||||
return false;
|
||||
}
|
||||
if (moveableObject == null) {
|
||||
return false;
|
||||
}
|
||||
if (moveableObject.checkCanMove(directionType)) {
|
||||
moveableObject.moveObject(directionType);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
163
src/DrawingBoat.java
Normal file
163
src/DrawingBoat.java
Normal file
@ -0,0 +1,163 @@
|
||||
package src;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.util.Random;
|
||||
|
||||
public class DrawingBoat extends JComponent {
|
||||
|
||||
private EntityBoat entityBoat;
|
||||
|
||||
public EntityBoat getEntityBoat() {
|
||||
return entityBoat;
|
||||
}
|
||||
|
||||
protected void setEntityBoat(EntityBoat entityBoat) {
|
||||
this.entityBoat = entityBoat;
|
||||
}
|
||||
|
||||
private IDrawingOars drawingOars;
|
||||
|
||||
private int pictureWidth;
|
||||
|
||||
private int pictureHeight;
|
||||
|
||||
protected int startPosX;
|
||||
|
||||
protected int startPosY;
|
||||
|
||||
protected int boatWidth = 90;
|
||||
|
||||
protected int boatHeight = 56;
|
||||
|
||||
public int getPosX() {
|
||||
return startPosX;
|
||||
}
|
||||
|
||||
public int getPosY() {
|
||||
return startPosY;
|
||||
}
|
||||
|
||||
public int getWidth() {
|
||||
return boatWidth;
|
||||
}
|
||||
|
||||
public int getHeight() {
|
||||
return boatHeight;
|
||||
}
|
||||
|
||||
public DrawingBoat(int speed, double weight, Color mainColor, int width, int height, int oarsNumber) {
|
||||
if (width < boatWidth || height < boatHeight) {
|
||||
return;
|
||||
}
|
||||
pictureWidth = width;
|
||||
pictureHeight = height;
|
||||
entityBoat = new EntityBoat(speed, weight, mainColor);
|
||||
Random rand = new Random();
|
||||
drawingOars = switch (rand.nextInt(0, 3)) {
|
||||
case 0 -> new DrawingOars();
|
||||
case 1 -> new DrawingSquareBlueOars();
|
||||
case 2 -> new DrawingRedAndGreenOars();
|
||||
default -> new DrawingOars();
|
||||
};
|
||||
|
||||
drawingOars.setOarsNumber(oarsNumber);
|
||||
}
|
||||
|
||||
protected DrawingBoat(int speed, double weight, Color mainColor,
|
||||
int width, int height, int _boatWidth, int _boatHeight, int oarsNumber) {
|
||||
if (width < _boatWidth || height < _boatHeight) {
|
||||
return;
|
||||
}
|
||||
pictureWidth = width;
|
||||
pictureHeight = height;
|
||||
boatWidth = _boatWidth;
|
||||
boatHeight = _boatHeight;
|
||||
entityBoat = new EntityBoat(speed, weight, mainColor);
|
||||
Random rand = new Random();
|
||||
drawingOars = switch (rand.nextInt(0, 3)) {
|
||||
case 0 -> new DrawingOars();
|
||||
case 1 -> new DrawingSquareBlueOars();
|
||||
case 2 -> new DrawingRedAndGreenOars();
|
||||
default -> new DrawingOars();
|
||||
};
|
||||
drawingOars.setOarsNumber(oarsNumber);
|
||||
}
|
||||
|
||||
public void setPosition(int x, int y) {
|
||||
if (x < 0 || x + boatWidth > pictureWidth) {
|
||||
x = 0;
|
||||
}
|
||||
if (y < 0 || y + boatHeight > pictureHeight) {
|
||||
y = 0;
|
||||
}
|
||||
|
||||
startPosX = x;
|
||||
startPosY = y;
|
||||
}
|
||||
|
||||
public boolean canMove(DirectionType direction) {
|
||||
if (entityBoat == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return switch (direction) {
|
||||
case Left -> startPosX - entityBoat.getStep() > 0;
|
||||
case Up -> startPosY - entityBoat.getStep() > 0;
|
||||
case Right -> startPosX + boatWidth + entityBoat.getStep() < pictureWidth;
|
||||
case Down -> startPosY + boatHeight + entityBoat.getStep() < pictureHeight;
|
||||
default -> false;
|
||||
};
|
||||
}
|
||||
|
||||
public void moveTransport(DirectionType direction) {
|
||||
if (!canMove(direction) || entityBoat == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
switch (direction) {
|
||||
case Left:
|
||||
startPosX -= (int) entityBoat.getStep();
|
||||
break;
|
||||
|
||||
case Up:
|
||||
startPosY -= (int) entityBoat.getStep();
|
||||
break;
|
||||
|
||||
case Right:
|
||||
startPosX += (int) entityBoat.getStep();
|
||||
break;
|
||||
|
||||
case Down:
|
||||
startPosY += (int) entityBoat.getStep();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public void drawTransport(Graphics2D g) {
|
||||
if (entityBoat == null) {
|
||||
return;
|
||||
}
|
||||
Color pen = new Color(0, 0, 0);
|
||||
//границы лодки
|
||||
int [] pointsX = new int[]{(startPosX + 15),(startPosX + 65),(startPosX + 85),(startPosX + 65),(startPosX + 15)};
|
||||
int [] pointsY = new int[]{(startPosY),(startPosY),(startPosY + 20),(startPosY + 40),(startPosY+40)};
|
||||
|
||||
try { pen = entityBoat.getMainColor(); }
|
||||
catch (Exception e) {}
|
||||
|
||||
g.setPaint(pen);
|
||||
g.fillPolygon(pointsX,pointsY,5);
|
||||
g.setPaint(Color.black);
|
||||
g.drawPolygon(pointsX,pointsY,5);
|
||||
|
||||
g.setPaint(Color.orange);
|
||||
g.fillOval( (startPosX + 25),(int)( startPosY + 7.5), 40, 25);
|
||||
g.setPaint(Color.black);
|
||||
g.drawOval( (startPosX + 25),(int)( startPosY + 7.5), 40, 25);
|
||||
|
||||
drawingOars.drawOars(g, startPosX, startPosY, pen);
|
||||
|
||||
super.repaint();
|
||||
}
|
||||
}
|
@ -3,9 +3,14 @@ package src;
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawingOars extends JComponent {
|
||||
public class DrawingOars implements IDrawingOars {
|
||||
private OarsNumber oarsNumber;
|
||||
public void setOarNumber(int number) {
|
||||
@Override
|
||||
public OarsNumber getOarsNumber() {
|
||||
return oarsNumber;
|
||||
}
|
||||
@Override
|
||||
public void setOarsNumber(int number) {
|
||||
switch (number) {
|
||||
case 1:
|
||||
oarsNumber = OarsNumber.One;
|
||||
@ -19,16 +24,13 @@ public class DrawingOars extends JComponent {
|
||||
default: oarsNumber = OarsNumber.Two;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drawOars(Graphics gr, int _startXCoordOar, int _startYCoordOar, Color pen) {
|
||||
super.paintComponent(gr);
|
||||
Graphics2D g=(Graphics2D)gr;
|
||||
|
||||
_startXCoordOar += 40;
|
||||
_startYCoordOar += 33;
|
||||
|
||||
System.out.println("oarsNum(drawOars): "+ oarsNumber);
|
||||
|
||||
switch (oarsNumber) {
|
||||
case One:
|
||||
paintPaddle(g, _startXCoordOar, _startYCoordOar, _startYCoordOar + 16,pen);
|
||||
|
39
src/DrawingObjectBoat.java
Normal file
39
src/DrawingObjectBoat.java
Normal file
@ -0,0 +1,39 @@
|
||||
package src;
|
||||
|
||||
public class DrawingObjectBoat implements IMoveableObject {
|
||||
|
||||
private DrawingBoat drawingBoat = null;
|
||||
|
||||
public DrawingObjectBoat(DrawingBoat drawingBoat) {
|
||||
this.drawingBoat = drawingBoat;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ObjectParameters getObjectsPosition() {
|
||||
if (drawingBoat == null || drawingBoat.getEntityBoat() == null) {
|
||||
return null;
|
||||
}
|
||||
return new ObjectParameters(drawingBoat.getPosX(), drawingBoat.getPosY(),
|
||||
drawingBoat.getWidth(), drawingBoat.getHeight());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getStep() {
|
||||
if (drawingBoat == null)
|
||||
return 0;
|
||||
return (int) ((drawingBoat.getEntityBoat() != null) ? drawingBoat.getEntityBoat().getStep() : 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean checkCanMove(DirectionType direction) {
|
||||
if (drawingBoat == null)
|
||||
return false;
|
||||
return drawingBoat.canMove(direction);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void moveObject(DirectionType direction) {
|
||||
if (drawingBoat != null)
|
||||
drawingBoat.moveTransport(direction);
|
||||
}
|
||||
}
|
61
src/DrawingRedAndGreenOars.java
Normal file
61
src/DrawingRedAndGreenOars.java
Normal file
@ -0,0 +1,61 @@
|
||||
package src;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawingRedAndGreenOars implements IDrawingOars {
|
||||
|
||||
private OarsNumber oarsNumber;
|
||||
|
||||
@Override
|
||||
public OarsNumber getOarsNumber() {
|
||||
return oarsNumber;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setOarsNumber(int number) {
|
||||
switch (number) {
|
||||
case 2:
|
||||
oarsNumber = OarsNumber.One;
|
||||
break;
|
||||
case 3:
|
||||
oarsNumber = OarsNumber.Two;
|
||||
break;
|
||||
case 4:
|
||||
oarsNumber = OarsNumber.Three;
|
||||
break;
|
||||
default:
|
||||
oarsNumber = OarsNumber.Two;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drawOars(Graphics gr, int _startXCoordOar, int _startYCoordOar, Color pen) {
|
||||
Graphics2D g=(Graphics2D)gr;
|
||||
|
||||
_startXCoordOar += 40;
|
||||
_startYCoordOar += 33;
|
||||
|
||||
switch (oarsNumber) {
|
||||
case One:
|
||||
paintPaddle(g, _startXCoordOar, _startYCoordOar, _startYCoordOar + 16,pen);
|
||||
break;
|
||||
case Two:
|
||||
paintPaddle(g, _startXCoordOar, _startYCoordOar, _startYCoordOar + 16,pen);
|
||||
paintPaddle(g, _startXCoordOar -10, _startYCoordOar -41, _startYCoordOar -51,pen);
|
||||
break;
|
||||
case Three:
|
||||
paintPaddle(g, _startXCoordOar, _startYCoordOar, _startYCoordOar + 16,pen);
|
||||
paintPaddle(g, _startXCoordOar -10, _startYCoordOar -41, _startYCoordOar -51,pen);
|
||||
paintPaddle(g, _startXCoordOar -20, _startYCoordOar, _startYCoordOar + 16,pen);
|
||||
break;
|
||||
}
|
||||
}
|
||||
protected void paintPaddle(Graphics2D g, int _startPosX, int _startPosY1,int _startPosY2, Color pen) {
|
||||
g.setPaint(Color.red);
|
||||
g.fillRect(_startPosX,_startPosY1,6,17);
|
||||
g.fillOval(_startPosX-1, _startPosY2, 6, 10);
|
||||
g.setPaint(Color.green);
|
||||
g.drawRect(_startPosX,_startPosY1,6,17);
|
||||
g.drawOval(_startPosX-1, _startPosY2, 6, 10);
|
||||
}
|
||||
}
|
@ -3,125 +3,32 @@ package src;
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawingSpeedBoat extends JComponent {
|
||||
public class DrawingSpeedBoat extends DrawingBoat {
|
||||
public DrawingSpeedBoat(int speed, double weight, Color mainColor, Color secondColor,
|
||||
boolean isMotor, boolean isProtectedGlass, int width, int height, int oarsNumber) {
|
||||
super(speed, weight, mainColor, width, height, 90, 80, oarsNumber);
|
||||
|
||||
private EntitySpeedBoat entitySpeedBoat;
|
||||
|
||||
public EntitySpeedBoat getEntitySpeedBoat() {
|
||||
return entitySpeedBoat;
|
||||
if (getEntityBoat() != null) {
|
||||
setEntityBoat(new EntitySpeedBoat(speed, weight, mainColor, secondColor, isMotor, isProtectedGlass));
|
||||
}
|
||||
|
||||
private DrawingOars drawingOars;
|
||||
|
||||
private int pictureWidth;
|
||||
|
||||
private int pictureHeight;
|
||||
|
||||
private int startPosX;
|
||||
|
||||
private int startPosY;
|
||||
|
||||
private int boatWidth = 75;
|
||||
|
||||
private int boatHeight = 70;
|
||||
|
||||
public boolean Init(int speed, double weight, Color mainColor, Color secondColor, boolean isMotor,
|
||||
boolean isProtectedGLass, int width, int height, int oarsNumber) {
|
||||
if (width < boatWidth || height < boatHeight) { return false; }
|
||||
pictureWidth = width;
|
||||
pictureHeight = height;
|
||||
entitySpeedBoat = new EntitySpeedBoat();
|
||||
entitySpeedBoat.Init(speed, weight, mainColor, secondColor,
|
||||
isMotor, isProtectedGLass);
|
||||
drawingOars = new DrawingOars();
|
||||
drawingOars.setOarNumber(oarsNumber);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void SetPosition(int x, int y) {
|
||||
if (x < 0 || x + boatWidth > pictureWidth) { x = 0; }
|
||||
if (y < 0 || y + boatHeight > pictureHeight) { y = 0; }
|
||||
|
||||
startPosX = x;
|
||||
startPosY = y;
|
||||
}
|
||||
|
||||
public void MoveTransport(DirectionType direction) {
|
||||
if (entitySpeedBoat == null) {
|
||||
@Override
|
||||
public void drawTransport(Graphics2D g)
|
||||
{
|
||||
if (!(getEntityBoat() instanceof EntitySpeedBoat speedBoat)) {
|
||||
return;
|
||||
}
|
||||
|
||||
switch (direction) {
|
||||
//влево
|
||||
case Left:
|
||||
if (startPosX - entitySpeedBoat.getStep() > 0)
|
||||
{
|
||||
startPosX -= (int) entitySpeedBoat.getStep();
|
||||
}
|
||||
break;
|
||||
//вверх
|
||||
case Up:
|
||||
if (startPosY - entitySpeedBoat.getStep() > 0)
|
||||
{
|
||||
startPosY -= (int) entitySpeedBoat.getStep();
|
||||
}
|
||||
break;
|
||||
//вправо
|
||||
case Right:
|
||||
if (startPosX + boatWidth + entitySpeedBoat.getStep() < pictureWidth)
|
||||
{
|
||||
startPosX += (int) entitySpeedBoat.getStep();
|
||||
}
|
||||
break;
|
||||
//вниз
|
||||
case Down:
|
||||
if (startPosY + boatHeight + entitySpeedBoat.getStep() < pictureHeight)
|
||||
{
|
||||
startPosY += (int) entitySpeedBoat.getStep();
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
super.drawTransport(g);
|
||||
|
||||
public void DrawTransport(Graphics2D g)
|
||||
{
|
||||
if (entitySpeedBoat == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
super.paintComponent(g);
|
||||
if (startPosX < 0 || startPosY < 0
|
||||
|| pictureHeight == 0 || pictureWidth == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Color pen = new Color(0, 0, 0);
|
||||
//границы лодки
|
||||
int [] pointsX = new int[]{(startPosX + 15),(startPosX + 65),(startPosX + 85),(startPosX + 65),(startPosX + 15)};
|
||||
int [] pointsY = new int[]{(startPosY),(startPosY),(startPosY + 20),(startPosY + 40),(startPosY+40)};
|
||||
|
||||
try { pen = entitySpeedBoat.getMainColor(); }
|
||||
catch (Exception e) {}
|
||||
|
||||
g.setPaint(pen);
|
||||
g.fillPolygon(pointsX,pointsY,5);
|
||||
g.setPaint(Color.black);
|
||||
g.drawPolygon(pointsX,pointsY,5);
|
||||
|
||||
g.setPaint(Color.orange);
|
||||
g.fillOval((startPosX + 25),(int)( startPosY + 7.5), 40, 25);
|
||||
g.setPaint(Color.black);
|
||||
g.drawOval((startPosX + 25),(int)( startPosY + 7.5), 40, 25);
|
||||
|
||||
if(entitySpeedBoat.get_isMotor()){
|
||||
if(speedBoat.get_IsMotor()){
|
||||
Color addPen = new Color(200, 200, 0);
|
||||
|
||||
//Motor
|
||||
int [] pointXMotor = new int[]{(startPosX),(startPosX + 15),(startPosX + 15),(startPosX)};
|
||||
int [] pointYMotor = new int[]{(startPosY + 10),(startPosY + 10),(startPosY + 30),(startPosY + 30)};
|
||||
int [] pointXMotor = new int[]{(int)(startPosX),(int)(startPosX + 15),(int)(startPosX + 15),(int)(startPosX)};
|
||||
int [] pointYMotor = new int[]{(int)(startPosY + 10),(int)(startPosY + 10),(int)(startPosY + 30),(int)(startPosY + 30)};
|
||||
|
||||
try { addPen = entitySpeedBoat.getsecondColor(); }
|
||||
try { addPen = speedBoat.getSecondColor(); }
|
||||
catch(Exception e) {}
|
||||
|
||||
g.setPaint(addPen);
|
||||
@ -130,27 +37,22 @@ public class DrawingSpeedBoat extends JComponent {
|
||||
g.drawPolygon(pointXMotor, pointYMotor, 4);
|
||||
}
|
||||
|
||||
if(entitySpeedBoat.get_isProtectedGlass()){
|
||||
if(speedBoat.get_IsProtectedGlass()) {
|
||||
Color addPen = new Color(200, 200, 0);
|
||||
|
||||
//protectedGlass
|
||||
int [] pointXGlass = new int[]{(startPosX + 55),(startPosX + 70),(startPosX + 70),(startPosX + 55)};
|
||||
int [] pointYGlass = new int[]{(startPosY + 10),(startPosY + 6),(startPosY + 34),(startPosY + 30)};
|
||||
int[] pointXGlass = new int[]{(int) (startPosX + 55), (int) (startPosX + 70), (int) (startPosX + 70), (int) (startPosX + 55)};
|
||||
int[] pointYGlass = new int[]{(int) (startPosY + 10), (int) (startPosY + 6), (int) (startPosY + 34), (int) (startPosY + 30)};
|
||||
|
||||
try { addPen = entitySpeedBoat.getsecondColor(); }
|
||||
catch(Exception e) {}
|
||||
try {
|
||||
addPen = speedBoat.getSecondColor();
|
||||
} catch (Exception e) {
|
||||
}
|
||||
|
||||
g.setPaint(addPen);
|
||||
g.fillPolygon(pointXGlass, pointYGlass, 4);
|
||||
g.setPaint(Color.black);
|
||||
g.drawPolygon(pointXGlass, pointYGlass, 4);
|
||||
}
|
||||
|
||||
try { pen = entitySpeedBoat.getsecondColor(); }
|
||||
catch (Exception e) {}
|
||||
|
||||
drawingOars.drawOars(g, startPosX, startPosY, pen);
|
||||
|
||||
super.repaint();
|
||||
}
|
||||
}
|
||||
|
64
src/DrawingSquareBlueOars.java
Normal file
64
src/DrawingSquareBlueOars.java
Normal file
@ -0,0 +1,64 @@
|
||||
package src;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawingSquareBlueOars implements IDrawingOars {
|
||||
|
||||
private OarsNumber oarsNumber;
|
||||
|
||||
@Override
|
||||
public OarsNumber getOarsNumber() {
|
||||
return oarsNumber;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setOarsNumber(int number) {
|
||||
switch (number) {
|
||||
case 2:
|
||||
oarsNumber = OarsNumber.One;
|
||||
break;
|
||||
case 3:
|
||||
oarsNumber = OarsNumber.Two;
|
||||
break;
|
||||
case 4:
|
||||
oarsNumber = OarsNumber.Three;
|
||||
break;
|
||||
default:
|
||||
oarsNumber = OarsNumber.Two;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drawOars(Graphics gr, int _startXCoordOar, int _startYCoordOar, Color pen) {
|
||||
Graphics2D g=(Graphics2D)gr;
|
||||
|
||||
_startXCoordOar += 40;
|
||||
_startYCoordOar += 33;
|
||||
|
||||
switch (oarsNumber) {
|
||||
case One:
|
||||
paintPaddle(g, _startXCoordOar, _startYCoordOar, _startYCoordOar + 16,pen);
|
||||
break;
|
||||
case Two:
|
||||
paintPaddle(g, _startXCoordOar, _startYCoordOar, _startYCoordOar + 16,pen);
|
||||
paintPaddle(g, _startXCoordOar -10, _startYCoordOar -41, _startYCoordOar -51,pen);
|
||||
break;
|
||||
case Three:
|
||||
paintPaddle(g, _startXCoordOar, _startYCoordOar, _startYCoordOar + 16,pen);
|
||||
paintPaddle(g, _startXCoordOar -10, _startYCoordOar -41, _startYCoordOar -51,pen);
|
||||
paintPaddle(g, _startXCoordOar -20, _startYCoordOar, _startYCoordOar + 16,pen);
|
||||
break;
|
||||
}
|
||||
}
|
||||
protected void paintPaddle(Graphics2D g, int _startPosX, int _startPosY1,int _startPosY2, Color pen) {
|
||||
try { g.setPaint(pen); }
|
||||
catch (Exception e) {
|
||||
g.setPaint(Color.black);
|
||||
}
|
||||
g.fillRect(_startPosX,_startPosY1,4,17);
|
||||
g.fillRect(_startPosX-1, _startPosY2, 6, 6);
|
||||
g.setPaint(Color.BLUE);
|
||||
g.drawRect(_startPosX,_startPosY1,4,17);
|
||||
g.drawRect(_startPosX-1, _startPosY2, 6, 6);
|
||||
}
|
||||
}
|
34
src/EntityBoat.java
Normal file
34
src/EntityBoat.java
Normal file
@ -0,0 +1,34 @@
|
||||
package src;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class EntityBoat {
|
||||
|
||||
private int speed;
|
||||
|
||||
public int getSpeed() {
|
||||
return speed;
|
||||
}
|
||||
|
||||
private double weight;
|
||||
|
||||
public double getWeight() {
|
||||
return weight;
|
||||
}
|
||||
|
||||
private Color mainColor;
|
||||
|
||||
public Color getMainColor() {
|
||||
return mainColor;
|
||||
}
|
||||
|
||||
public double getStep() {
|
||||
return (double) speed * 100 / weight;
|
||||
}
|
||||
|
||||
public EntityBoat(int speed, double weight, Color mainColor) {
|
||||
this.speed = speed;
|
||||
this.weight = weight;
|
||||
this.mainColor = mainColor;
|
||||
}
|
||||
}
|
@ -2,52 +2,28 @@ package src;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class EntitySpeedBoat {
|
||||
|
||||
private int speed;
|
||||
|
||||
public int getSpeed() {
|
||||
return speed;
|
||||
}
|
||||
|
||||
private double weight;
|
||||
|
||||
public double getWeight() {
|
||||
return weight;
|
||||
}
|
||||
|
||||
private Color mainColor;
|
||||
|
||||
public Color getMainColor() {
|
||||
return mainColor;
|
||||
}
|
||||
|
||||
public class EntitySpeedBoat extends EntityBoat{
|
||||
private Color secondColor;
|
||||
|
||||
public Color getsecondColor() {
|
||||
public Color getSecondColor() {
|
||||
return secondColor;
|
||||
}
|
||||
|
||||
private boolean isMotor;
|
||||
|
||||
public boolean get_isMotor() {
|
||||
public boolean get_IsMotor() {
|
||||
return isMotor;
|
||||
}
|
||||
|
||||
private boolean isProtectedGlass;
|
||||
|
||||
public boolean get_isProtectedGlass() {
|
||||
public boolean get_IsProtectedGlass() {
|
||||
return isProtectedGlass;
|
||||
}
|
||||
|
||||
public double getStep() {
|
||||
return (double)speed * 20 / weight;
|
||||
}
|
||||
|
||||
public void Init(int speed, double weight, Color mainColor, Color secondColor, boolean isMotor, boolean isProtectedGlass) {
|
||||
this.speed = speed;
|
||||
this.weight = weight;
|
||||
this.mainColor = mainColor;
|
||||
public EntitySpeedBoat(int speed, double weight, Color mainColor, Color
|
||||
secondColor, boolean isMotor, boolean isProtectedGlass) {
|
||||
super(speed, weight, mainColor);
|
||||
this.secondColor = secondColor;
|
||||
this.isMotor = isMotor;
|
||||
this.isProtectedGlass = isProtectedGlass;
|
||||
|
13
src/IDrawingOars.java
Normal file
13
src/IDrawingOars.java
Normal file
@ -0,0 +1,13 @@
|
||||
package src;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public interface IDrawingOars {
|
||||
void setOarsNumber(int number);
|
||||
|
||||
|
||||
void drawOars(Graphics gr, int _startXCoordOar, int _startYCoordOar, Color pen);
|
||||
|
||||
|
||||
OarsNumber getOarsNumber();
|
||||
}
|
12
src/IMoveableObject.java
Normal file
12
src/IMoveableObject.java
Normal file
@ -0,0 +1,12 @@
|
||||
package src;
|
||||
|
||||
public interface IMoveableObject {
|
||||
|
||||
ObjectParameters getObjectsPosition();
|
||||
|
||||
int getStep();
|
||||
|
||||
boolean checkCanMove(DirectionType direction);
|
||||
|
||||
void moveObject(DirectionType direction);
|
||||
}
|
36
src/MoveToBorder.java
Normal file
36
src/MoveToBorder.java
Normal file
@ -0,0 +1,36 @@
|
||||
package src;
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
40
src/MoveToCenter.java
Normal file
40
src/MoveToCenter.java
Normal file
@ -0,0 +1,40 @@
|
||||
package src;
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
43
src/ObjectParameters.java
Normal file
43
src/ObjectParameters.java
Normal file
@ -0,0 +1,43 @@
|
||||
package src;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
@ -7,7 +7,9 @@ import java.util.Random;
|
||||
|
||||
public class PictureBox extends JPanel {
|
||||
|
||||
private DrawingSpeedBoat drawingSpeedBoat;
|
||||
private DrawingBoat drawingBoat;
|
||||
|
||||
private AbstractStrategy abstractStrategy;
|
||||
|
||||
private JButton buttonLeft;
|
||||
|
||||
@ -17,13 +19,23 @@ public class PictureBox extends JPanel {
|
||||
|
||||
private JButton buttonDown;
|
||||
|
||||
private JButton buttonCreateLocomotive;
|
||||
|
||||
private JButton buttonCreateSpeedBoat;
|
||||
|
||||
private JButton buttonStep;
|
||||
|
||||
private JComboBox comboBoxStrategy;
|
||||
|
||||
private JPanel buttonsPanel;
|
||||
|
||||
private JPanel buttonsMovePanel;
|
||||
|
||||
private JPanel paddingPanel;
|
||||
private JPanel movePaddingPanel;
|
||||
|
||||
private JPanel strategyPaddingPanel;
|
||||
|
||||
private JPanel strategyPanel;
|
||||
|
||||
public PictureBox() {
|
||||
setLayout(new BorderLayout());
|
||||
@ -32,32 +44,44 @@ public class PictureBox extends JPanel {
|
||||
buttonsPanel.setLayout(new BoxLayout(buttonsPanel, BoxLayout.X_AXIS));
|
||||
buttonsPanel.setOpaque(false);
|
||||
|
||||
buttonCreateSpeedBoat = new JButton("Create");
|
||||
buttonCreateLocomotive = new JButton("Create Boat");
|
||||
buttonCreateLocomotive.setFocusable(false);
|
||||
buttonCreateLocomotive.setFont(new Font("Segoe UI", Font.PLAIN, 12));
|
||||
buttonCreateLocomotive.setBackground(Color.LIGHT_GRAY);
|
||||
buttonCreateLocomotive.setMaximumSize(new Dimension(140, 42));
|
||||
|
||||
buttonCreateLocomotive.addActionListener(e -> {
|
||||
Random random = new Random();
|
||||
drawingBoat = new DrawingBoat(random.nextInt(50, 70),
|
||||
random.nextInt(40, 120),
|
||||
new Color(random.nextInt(0, 256), random.nextInt(0, 256),
|
||||
random.nextInt(0, 256)),
|
||||
this.getWidth(), this.getHeight(), random.nextInt(2, 5));
|
||||
drawingBoat.setPosition(random.nextInt(10, 100), random.nextInt(10, 100));
|
||||
repaint();
|
||||
});
|
||||
|
||||
buttonCreateSpeedBoat = new JButton("Create SpeedBoat");
|
||||
buttonCreateSpeedBoat.setFocusable(false);
|
||||
buttonCreateSpeedBoat.setFont(new Font("Segoe UI", Font.PLAIN, 12));
|
||||
buttonCreateSpeedBoat.setBackground(Color.LIGHT_GRAY);
|
||||
buttonCreateSpeedBoat.setPreferredSize(new Dimension(75, 23));
|
||||
buttonCreateSpeedBoat.setMaximumSize(new Dimension(140, 42));
|
||||
|
||||
buttonCreateSpeedBoat.addActionListener (e -> {
|
||||
buttonCreateSpeedBoat.addActionListener(e -> {
|
||||
Random random = new Random();
|
||||
drawingSpeedBoat = new DrawingSpeedBoat();
|
||||
|
||||
int oars = random.nextInt(1, 4);
|
||||
drawingSpeedBoat.Init(random.nextInt(50, 150),
|
||||
drawingBoat = new DrawingSpeedBoat(random.nextInt(50, 70),
|
||||
random.nextInt(40, 120),
|
||||
new Color(random.nextInt(0, 256), random.nextInt(0, 256),
|
||||
random.nextInt(0, 256)),
|
||||
new Color(random.nextInt(0, 256), random.nextInt(0, 256),
|
||||
random.nextInt(0, 256)),
|
||||
random.nextBoolean(), random.nextBoolean(), this.getWidth(), this.getHeight(), oars);
|
||||
|
||||
drawingSpeedBoat.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100));
|
||||
random.nextBoolean(), random.nextBoolean(), this.getWidth(), this.getHeight(), random.nextInt(2, 5));
|
||||
drawingBoat.setPosition(random.nextInt(10, 100), random.nextInt(10, 100));
|
||||
repaint();
|
||||
});
|
||||
|
||||
ActionListener buttonMoveListener = e -> {
|
||||
if (drawingSpeedBoat == null)
|
||||
{
|
||||
if (drawingBoat == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
@ -65,16 +89,16 @@ public class PictureBox extends JPanel {
|
||||
|
||||
switch (buttonName) {
|
||||
case ("buttonUp"):
|
||||
drawingSpeedBoat.MoveTransport(DirectionType.Up);
|
||||
drawingBoat.moveTransport(DirectionType.Up);
|
||||
break;
|
||||
case ("buttonDown"):
|
||||
drawingSpeedBoat.MoveTransport(DirectionType.Down);
|
||||
drawingBoat.moveTransport(DirectionType.Down);
|
||||
break;
|
||||
case ("buttonLeft"):
|
||||
drawingSpeedBoat.MoveTransport(DirectionType.Left);
|
||||
drawingBoat.moveTransport(DirectionType.Left);
|
||||
break;
|
||||
case ("buttonRight"):
|
||||
drawingSpeedBoat.MoveTransport(DirectionType.Right);
|
||||
drawingBoat.moveTransport(DirectionType.Right);
|
||||
break;
|
||||
}
|
||||
repaint();
|
||||
@ -113,7 +137,7 @@ public class PictureBox extends JPanel {
|
||||
buttonsMovePanel.setOpaque(false);
|
||||
|
||||
GridBagConstraints constrains = new GridBagConstraints();
|
||||
constrains.insets = new Insets(5,5,5,5);
|
||||
constrains.insets = new Insets(5, 5, 5, 5);
|
||||
constrains.gridx = 0;
|
||||
constrains.gridy = 0;
|
||||
|
||||
@ -144,30 +168,93 @@ public class PictureBox extends JPanel {
|
||||
|
||||
buttonsMovePanel.add(buttonRight, constrains);
|
||||
|
||||
paddingPanel = new JPanel();
|
||||
paddingPanel.setLayout(new BoxLayout(paddingPanel, BoxLayout.Y_AXIS));
|
||||
paddingPanel.setOpaque(false);
|
||||
movePaddingPanel = new JPanel();
|
||||
movePaddingPanel.setLayout(new BoxLayout(movePaddingPanel, BoxLayout.Y_AXIS));
|
||||
movePaddingPanel.setOpaque(false);
|
||||
|
||||
paddingPanel.add(buttonsMovePanel);
|
||||
paddingPanel.add(Box.createVerticalStrut(15));
|
||||
movePaddingPanel.add(buttonsMovePanel);
|
||||
movePaddingPanel.add(Box.createVerticalStrut(22));
|
||||
|
||||
buttonsPanel.add(Box.createHorizontalStrut(20));
|
||||
buttonsPanel.add(Box.createHorizontalStrut(12));
|
||||
buttonsPanel.add(buttonCreateSpeedBoat);
|
||||
buttonsPanel.add(Box.createHorizontalStrut(650));
|
||||
buttonsPanel.add(paddingPanel);
|
||||
buttonsPanel.add(Box.createHorizontalStrut(16));
|
||||
buttonsPanel.add(buttonCreateLocomotive);
|
||||
buttonsPanel.add(Box.createHorizontalStrut(460));
|
||||
buttonsPanel.add(movePaddingPanel);
|
||||
|
||||
add(buttonsPanel, BorderLayout.SOUTH);
|
||||
|
||||
strategyPaddingPanel = new JPanel();
|
||||
strategyPaddingPanel.setLayout(new BoxLayout(strategyPaddingPanel, BoxLayout.X_AXIS));
|
||||
strategyPaddingPanel.setOpaque(false);
|
||||
|
||||
String[] items = {
|
||||
"Form center",
|
||||
"Form border"
|
||||
};
|
||||
comboBoxStrategy = new JComboBox(items);
|
||||
comboBoxStrategy.setMaximumSize(new Dimension(150, 23));
|
||||
|
||||
buttonStep = new JButton("Step");
|
||||
buttonStep.setFocusable(false);
|
||||
buttonStep.setFont(new Font("Segoe UI", Font.PLAIN, 12));
|
||||
buttonStep.setBackground(Color.LIGHT_GRAY);
|
||||
buttonStep.setMaximumSize(new Dimension(75, 28));
|
||||
|
||||
buttonStep.addActionListener(e -> {
|
||||
if (drawingBoat == 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 DrawingObjectBoat(drawingBoat), this.getWidth(), this.getHeight()); //900, 500
|
||||
}
|
||||
if (abstractStrategy == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
comboBoxStrategy.setEnabled(false);
|
||||
abstractStrategy.makeStep();
|
||||
repaint();
|
||||
if (abstractStrategy.getStatus() == Status.Finish)
|
||||
{
|
||||
comboBoxStrategy.setEnabled(true);
|
||||
abstractStrategy = null;
|
||||
}
|
||||
});
|
||||
|
||||
strategyPanel = new JPanel();
|
||||
strategyPanel.setLayout(new BoxLayout(strategyPanel, BoxLayout.Y_AXIS));
|
||||
strategyPanel.setOpaque(false);
|
||||
|
||||
strategyPanel.add(Box.createVerticalStrut(10));
|
||||
strategyPanel.add(comboBoxStrategy);
|
||||
strategyPanel.add(Box.createVerticalStrut(15));
|
||||
strategyPanel.add(buttonStep);
|
||||
|
||||
strategyPaddingPanel.add(Box.createHorizontalStrut(735));
|
||||
strategyPaddingPanel.add(strategyPanel);
|
||||
|
||||
add(strategyPaddingPanel, BorderLayout.NORTH);
|
||||
|
||||
setPreferredSize(new Dimension(900, 500));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void paintComponent(Graphics g) {
|
||||
if (drawingSpeedBoat == null) {
|
||||
if (drawingBoat == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
super.paintComponent(g);
|
||||
Graphics2D g2d = (Graphics2D) g;
|
||||
drawingSpeedBoat.DrawTransport(g2d);
|
||||
drawingBoat.drawTransport(g2d);
|
||||
}
|
||||
}
|
||||
|
9
src/Status.java
Normal file
9
src/Status.java
Normal file
@ -0,0 +1,9 @@
|
||||
package src;
|
||||
|
||||
public enum Status {
|
||||
NotInit,
|
||||
|
||||
InProgress,
|
||||
|
||||
Finish
|
||||
}
|
Loading…
Reference in New Issue
Block a user