до сдачи 2л

This commit is contained in:
Arkadiy Radaev 2023-12-10 11:59:44 +04:00
parent cbfbf06e0a
commit caac7cbff4
16 changed files with 750 additions and 152 deletions

70
AbstractStrategy.java Normal file
View File

@ -0,0 +1,70 @@
public abstract class AbstractStrategy {
private IMoveableObject _moveableObject;
private Status _state = Status.NotInit;
private int FieldWidth;
protected int FieldWidth(){return FieldWidth;}
private int FieldHeight;
protected int FieldHeight(){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;
_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 _moveableObject.GetObjectParameters();
else return null;
}
protected Integer GetStep()
{
if (_state != Status.InProgress)
{
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.CheckCanMove(directionType))
{
_moveableObject.MoveObject(directionType);
return true;
}
return false;
}
}

View File

@ -1,125 +1,166 @@
import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.util.Random; import java.util.Random;
public class DrawningCatamaran { public class DrawningCatamaran {
JPanel CatamaranPanel; protected EntityCatamaran EntityCatamaran;
private EntityCatamaran EntityCatamaran;
private DrawningOars drawningOars;
private int _pictureWidth; private int _pictureWidth;
private int _pictureHeight; private int _pictureHeight;
private int _startPosX = 0; protected int _startPosX = 0;
private int _startPosY = 0; protected int _startPosY = 0;
private int _catamaranWidth = 120; protected int _catamaranWidth = 120;
private int _catamaranHeight = 120; protected int _catamaranHeight = 80;
protected IDraw DrawningOars;
public EntityCatamaran EntityCatamaran(){ public EntityCatamaran EntityCatamaran() {
return EntityCatamaran; return EntityCatamaran;
} }
public boolean Init(int speed, double weight, Color bodyColor, Color oarColor, int width, int height, boolean boduKit, boolean seats, JPanel catamaranPanel){ public DrawningCatamaran(int speed, double weight, Color bodyColor, Color oarColor, int width, int height,
if(width <= _catamaranWidth || height <= _catamaranHeight) boolean boduKit, boolean seats) {
return false; if (width <= _catamaranWidth || height <= _catamaranHeight)
return;
Random rand = new Random(); Random rand = new Random();
catamaranPanel.setSize(width, height);
CatamaranPanel = catamaranPanel;
catamaranPanel.paint(CatamaranPanel.getGraphics());
_pictureWidth = width; _pictureWidth = width;
_pictureHeight = height; _pictureHeight = height;
EntityCatamaran = new EntityCatamaran(); EntityCatamaran = new EntityCatamaran(speed, weight, bodyColor, oarColor, boduKit, seats);
EntityCatamaran.Init(speed, weight, bodyColor, oarColor, boduKit, seats); SetPosition(rand.nextInt(10,50),rand.nextInt(10,50));
SetPosition(rand.nextInt(50), rand.nextInt(50));
drawningOars =new DrawningOars(); DrawningOars = new DrawningOars(_startPosX, _startPosY, oarColor);
drawningOars.Init(_startPosX, _startPosY, oarColor, catamaranPanel); int variant = rand.nextInt(0, 3);
drawningOars.ChangeOarsNumb(rand.nextInt(3)); if (variant == 0)
return true; DrawningOars = new DrawningOars(_startPosX, _startPosY, oarColor);
else if (variant == 1)
DrawningOars = new DrawningOarsOrn(_startPosX, _startPosY, oarColor);
else
DrawningOars = new DrawningOarsHandle(_startPosX, _startPosY, oarColor);
DrawningOars.ChangeOarsNumb(rand.nextInt(1, 4));
} }
public void SetPosition(int x, int y){ public void SetPosition(int x, int y) {
if(EntityCatamaran == null) if (EntityCatamaran == null)
return; return;
_startPosX = x; _startPosX = x;
_startPosY = y; _startPosY = y;
// проверка говна - фикс
if(x + _catamaranWidth >= _pictureWidth || y + _catamaranHeight >= _pictureHeight){ if (x + _catamaranWidth >= _pictureWidth || y + _catamaranHeight >= _pictureHeight) {
_startPosX = 0; _startPosX = 0;
_startPosY = 0; _startPosY = 0;
} }
} }
public void MoveTransport(DirectionType direction){
public int GetPosX() {
return _startPosX;
}
public int GetPosY() {
return _startPosY;
}
public int GetWidth() {
return _catamaranWidth;
}
public int GetHeight() {
return _catamaranHeight;
}
public boolean CanMove(DirectionType direction) {
if (EntityCatamaran == null) if (EntityCatamaran == null)
return false;
boolean can = false;
switch (direction) {
case Left:
can = _startPosX - EntityCatamaran.Step() >= 0;
break;
case Right:
can = _startPosX + EntityCatamaran.Step() + _catamaranWidth < _pictureWidth;
break;
case Down:
can = _startPosY + EntityCatamaran.Step() + _catamaranHeight < _pictureHeight + 20;
break;
case Up:
can = _startPosY - EntityCatamaran.Step() >= 0;
break;
}
;
return can;
}
public void MoveTransport(DirectionType direction) {
if (!CanMove(direction) || EntityCatamaran == null)
return; return;
CatamaranPanel.paint(CatamaranPanel.getGraphics()); switch (direction) {
switch (direction)
{
case Left: case Left:
if (_startPosX - EntityCatamaran.Step() >= 0) if (_startPosX - EntityCatamaran.Step() >= 0)
_startPosX -= (int)EntityCatamaran.Step(); _startPosX -= (int) EntityCatamaran.Step();
else else
_startPosX = 0; _startPosX = 0;
break; break;
case Up: case Up:
if (_startPosY - EntityCatamaran.Step() >= 0) if (_startPosY - EntityCatamaran.Step() >= 0)
_startPosY -= (int)EntityCatamaran.Step(); _startPosY -= (int) EntityCatamaran.Step();
else else
_startPosY = 0; _startPosY = 0;
break; break;
case Right: case Right:
if (_startPosX + _catamaranWidth + EntityCatamaran.Step() < _pictureWidth) if (_startPosX + _catamaranWidth + EntityCatamaran.Step() < _pictureWidth)
_startPosX += (int)EntityCatamaran.Step(); _startPosX += (int) EntityCatamaran.Step();
else else
_startPosX = _pictureWidth - _catamaranWidth - 20; _startPosX = _pictureWidth - _catamaranWidth;
break; break;
case Down: case Down:
if (_startPosY + _catamaranHeight + EntityCatamaran.Step() < _pictureHeight) if (_startPosY + _catamaranHeight + EntityCatamaran.Step() < _pictureHeight)
_startPosY += (int)EntityCatamaran.Step(); _startPosY += (int) EntityCatamaran.Step();
else else
_startPosY = _pictureHeight - _catamaranHeight; _startPosY = _pictureHeight - _catamaranHeight;
break; break;
} }
drawningOars.OarPosX = _startPosX; DrawningOars.ChangeX(_startPosX);
drawningOars.OarPosY = _startPosY; DrawningOars.ChangeY(_startPosY);
} }
public void DrawnCatamaran(){
Graphics2D g2d = (Graphics2D)CatamaranPanel.getGraphics();
if (EntityCatamaran == null) return;
public void DrawnCatamaran(Graphics2D g2d) {
if (EntityCatamaran == null)
return;
g2d.setColor(EntityCatamaran.BodyColor()); g2d.setColor(EntityCatamaran.BodyColor());
// korpus // korpus
g2d.fillRect(_startPosX+10, _startPosY+10, _catamaranWidth - 50, 60); g2d.fillRect(_startPosX + 10, _startPosY + 10, _catamaranWidth - 50, 60);
// nos // nos
int[] x_triag = {_startPosX + _catamaranWidth - 40,_startPosX + _catamaranWidth,_startPosX + _catamaranWidth - 40}; int[] x_triag = { _startPosX + _catamaranWidth - 40, _startPosX + _catamaranWidth,
int[] y_triag = { _startPosY+10, _startPosY + 40,_startPosY + 70}; _startPosX + _catamaranWidth - 40 };
g2d.fillPolygon(x_triag,y_triag,3); int[] y_triag = { _startPosY + 10, _startPosY + 40, _startPosY + 70 };
g2d.fillPolygon(x_triag, y_triag, 3);
// poplav // poplav
g2d.setColor(Color.DARK_GRAY); g2d.setColor(Color.DARK_GRAY);
int[] poplav1_x = {_startPosX + _catamaranWidth -20,_startPosX + _catamaranWidth,_startPosX+_catamaranWidth - 20}; int[] poplav1_x = { _startPosX + _catamaranWidth - 20, _startPosX + _catamaranWidth,
int[] poplav1_y = {_startPosY,_startPosY + 5,_startPosY + 10}; _startPosX + _catamaranWidth - 20 };
g2d.fillPolygon(poplav1_x,poplav1_y,3); int[] poplav1_y = { _startPosY, _startPosY + 5, _startPosY + 10 };
int[] poplav2_x = {_startPosX + _catamaranWidth -20,_startPosX + _catamaranWidth,_startPosX+_catamaranWidth - 20}; g2d.fillPolygon(poplav1_x, poplav1_y, 3);
int[] poplav2_y = {_startPosY+70,_startPosY + 75,_startPosY + 80}; int[] poplav2_x = { _startPosX + _catamaranWidth - 20, _startPosX + _catamaranWidth,
g2d.fillPolygon(poplav2_x,poplav2_y,3); _startPosX + _catamaranWidth - 20 };
int[] poplav2_y = { _startPosY + 70, _startPosY + 75, _startPosY + 80 };
g2d.fillPolygon(poplav2_x, poplav2_y, 3);
g2d.fillRect(_startPosX, _startPosY, _catamaranWidth - 20, 10); g2d.fillRect(_startPosX, _startPosY, _catamaranWidth - 20, 10);
g2d.fillRect( _startPosX, _startPosY + 70, _catamaranWidth - 20, 10); g2d.fillRect(_startPosX, _startPosY + 70, _catamaranWidth - 20, 10);
// //
g2d.setColor(Color.BLACK); g2d.setColor(Color.BLACK);
g2d.drawPolygon(poplav1_x,poplav1_y,3); g2d.drawPolygon(poplav1_x, poplav1_y, 3);
g2d.drawPolygon(poplav2_x,poplav2_y,3); g2d.drawPolygon(poplav2_x, poplav2_y, 3);
g2d.drawPolygon(x_triag,y_triag,3); g2d.drawPolygon(x_triag, y_triag, 3);
g2d.drawRect(_startPosX+10, _startPosY+10, _catamaranWidth - 50, 60); g2d.drawRect(_startPosX + 10, _startPosY + 10, _catamaranWidth - 50, 60);
g2d.drawRect(_startPosX, _startPosY, _catamaranWidth - 20, 10); g2d.drawRect(_startPosX, _startPosY, _catamaranWidth - 20, 10);
g2d.drawRect( _startPosX, _startPosY + 70, _catamaranWidth - 20, 10); g2d.drawRect(_startPosX, _startPosY + 70, _catamaranWidth - 20, 10);
// sedenie // sedenie
g2d.setColor(Color.CYAN); g2d.setColor(Color.CYAN);
g2d.fillOval(_startPosX + 15, _startPosY + 25, 60, 30); g2d.fillOval(_startPosX + 15, _startPosY + 25, 60, 30);
if (EntityCatamaran.Seats()) if (EntityCatamaran.Seats()) {
{
g2d.setColor(Color.ORANGE); g2d.setColor(Color.ORANGE);
g2d.fillOval(_startPosX + 20, _startPosY + 35, 10, 10); g2d.fillOval(_startPosX + 20, _startPosY + 35, 10, 10);
g2d.fillOval(_startPosX + 35, _startPosY + 35, 10, 10); g2d.fillOval(_startPosX + 35, _startPosY + 35, 10, 10);
g2d.fillOval(_startPosX + 50, _startPosY + 35, 10, 10); g2d.fillOval(_startPosX + 50, _startPosY + 35, 10, 10);
} }
drawningOars.DrawOars(); DrawningOars.DrawOars(g2d);
} }
} }

43
DrawningCatamaranPro.java Normal file
View File

@ -0,0 +1,43 @@
import java.awt.*;
public class DrawningCatamaranPro extends DrawningCatamaran {
public DrawningCatamaranPro(int speed, double weight, Color bodyColor, Color oarColor, int width, int height,
boolean bodyKit, boolean seats, Color additionalColor, boolean motor, boolean sheet) {
super(speed, weight, bodyColor, oarColor, width, height, bodyKit, seats);
if (EntityCatamaran() != null) {
EntityCatamaran = new EntityCatamaranPro(speed, weight, bodyColor, oarColor, additionalColor, bodyKit,
motor, sheet, seats);
}
}
@Override
public void DrawnCatamaran(Graphics2D g2d) {
if (!(EntityCatamaran instanceof EntityCatamaranPro)) {
return;
}
EntityCatamaranPro _catamaran = (EntityCatamaranPro) EntityCatamaran;
g2d.setColor(_catamaran.AdditionalColor());
if (_catamaran.Motor()) {
g2d.fillRect(_startPosX, _startPosY + 25, 10, 30);
g2d.setColor(Color.BLACK);
g2d.drawRect(_startPosX, _startPosY + 25, 10, 30);
g2d.drawRect(_startPosX + 5, _startPosY + 30, 5, 20);
}
if (_catamaran.Sheet()) {
g2d.setColor(Color.BLACK);
for (int i = 10; i < 100; i += 10) {
g2d.drawLine(_startPosX + i, _startPosY, _startPosX + i, _startPosY + 10);
}
for (int i = 10; i < 100; i += 10) {
g2d.drawLine(_startPosX + i, _startPosY + 70, _startPosX + i, _startPosY + _catamaranHeight - 20);
}
}
super.DrawnCatamaran(g2d);
int[] x = { _startPosX + _catamaranWidth - 35, _startPosX + _catamaranWidth - 10, _startPosX + _catamaranWidth - 35 };
int[] y = { _startPosY + 20, _startPosY + 40, _startPosY + 60 };
g2d.setColor(_catamaran.AdditionalColor());
g2d.fillPolygon(x,y,3);
g2d.setColor(Color.BLACK);
g2d.drawPolygon(x,y,3);
}
}

View File

@ -1,86 +1,87 @@
import javax.swing.*;
import java.awt.*; import java.awt.*;
public class DrawningOars { public class DrawningOars implements IDraw {
private NumberType OarsNumb; private NumberType OarsNumb;
public int Cont;
public int OarPosX, OarPosY; public int OarPosX, OarPosY;
JPanel CatamaranPanel;
private Color OarsColor; private Color OarsColor;
// vesla Oars // vesla Oars
boolean Init(int oarPosX,int oarPosY, Color oarsColor, JPanel catamaranPanel){ public DrawningOars(int oarPosX, int oarPosY, Color oarsColor) {
OarPosX = oarPosX; OarPosX = oarPosX;
OarPosY = oarPosY; OarPosY = oarPosY;
OarsColor = oarsColor; OarsColor = oarsColor;
CatamaranPanel = catamaranPanel;
return true;
} }
public void ChangeOarsNumb(int x){ public void ChangeOarsNumb(int x) {
if(x<=1) if (x <= 1)
OarsNumb = NumberType.One; OarsNumb = NumberType.One;
if(x == 2) if (x == 2)
OarsNumb = NumberType.Two; OarsNumb = NumberType.Two;
if(x >= 3) if (x >= 3)
OarsNumb = NumberType.Three; OarsNumb = NumberType.Three;
} }
public NumberType OarsNumb(){ public void ChangeX(int x) {
OarPosX = x;
}
public void ChangeY(int y) {
OarPosY = y;
}
public NumberType OarsNumb() {
return OarsNumb; return OarsNumb;
} }
public void DrawOars(){ public void DrawOars(Graphics2D g2d) {
Graphics2D g2d = (Graphics2D)CatamaranPanel.getGraphics();
g2d.setColor(OarsColor); g2d.setColor(OarsColor);
int ind = 0; int ind = 0;
// 1 veslo // 1 veslo
int[] x_lop1 = {OarPosX+ind,OarPosX+15+ind,OarPosX+25+ind,OarPosX+10+ind}; int[] x_lop1 = { OarPosX + ind, OarPosX + 15 + ind, OarPosX + 25 + ind, OarPosX + 10 + ind };
int[] y_lop1 = {OarPosY+5,OarPosY,OarPosY+10,OarPosY+15}; int[] y_lop1 = { OarPosY + 5, OarPosY, OarPosY + 10, OarPosY + 15 };
int[] x_oar1 = {OarPosX+10+ind,OarPosX+20+ind,OarPosX+36+ind,OarPosX+26+ind}; int[] x_oar1 = { OarPosX + 10 + ind, OarPosX + 20 + ind, OarPosX + 36 + ind, OarPosX + 26 + ind };
int[] y_oar1 = {OarPosY+10,OarPosY+10,OarPosY+26,OarPosY+26}; int[] y_oar1 = { OarPosY + 10, OarPosY + 10, OarPosY + 26, OarPosY + 26 };
g2d.fillPolygon(x_oar1,y_oar1,4); g2d.fillPolygon(x_oar1, y_oar1, 4);
g2d.setColor(Color.BLACK); g2d.setColor(Color.BLACK);
g2d.drawPolygon(x_oar1,y_oar1,4); g2d.drawPolygon(x_oar1, y_oar1, 4);
g2d.setColor(OarsColor); g2d.setColor(OarsColor);
g2d.fillPolygon(x_lop1,y_lop1,4); g2d.fillPolygon(x_lop1, y_lop1, 4);
g2d.setColor(Color.BLACK); g2d.setColor(Color.BLACK);
g2d.drawPolygon(x_lop1,y_lop1,4); g2d.drawPolygon(x_lop1, y_lop1, 4);
//2 vesla
if (OarsNumb == NumberType.Two || OarsNumb == NumberType.Three) // 2 vesla
{ if (OarsNumb == NumberType.Two || OarsNumb == NumberType.Three) {
ind = 30; ind = 30;
g2d.setColor(OarsColor); g2d.setColor(OarsColor);
int[] x_lop2 = {OarPosX+ind,OarPosX+15+ind,OarPosX+25+ind,OarPosX+10+ind}; int[] x_lop2 = { OarPosX + ind, OarPosX + 15 + ind, OarPosX + 25 + ind, OarPosX + 10 + ind };
int[] y_lop2 = {OarPosY+5,OarPosY,OarPosY+10,OarPosY+15}; int[] y_lop2 = { OarPosY + 5, OarPosY, OarPosY + 10, OarPosY + 15 };
int[] x_oar2 = {OarPosX+10+ind,OarPosX+20+ind,OarPosX+36+ind,OarPosX+26+ind}; int[] x_oar2 = { OarPosX + 10 + ind, OarPosX + 20 + ind, OarPosX + 36 + ind, OarPosX + 26 + ind };
int[] y_oar2 = {OarPosY+10,OarPosY+10,OarPosY+26,OarPosY+26}; int[] y_oar2 = { OarPosY + 10, OarPosY + 10, OarPosY + 26, OarPosY + 26 };
g2d.fillPolygon(x_oar2,y_oar2,4); g2d.fillPolygon(x_oar2, y_oar2, 4);
g2d.setColor(Color.BLACK); g2d.setColor(Color.BLACK);
g2d.drawPolygon(x_oar2,y_oar2,4); g2d.drawPolygon(x_oar2, y_oar2, 4);
g2d.setColor(OarsColor); g2d.setColor(OarsColor);
g2d.fillPolygon(x_lop2,y_lop2,4); g2d.fillPolygon(x_lop2, y_lop2, 4);
g2d.setColor(Color.BLACK); g2d.setColor(Color.BLACK);
g2d.drawPolygon(x_lop2,y_lop2,4); g2d.drawPolygon(x_lop2, y_lop2, 4);
} }
//3 vesla // 3 vesla
if (OarsNumb == NumberType.Three) if (OarsNumb == NumberType.Three) {
{
ind = 0; ind = 0;
g2d.setColor(OarsColor); g2d.setColor(OarsColor);
int[] x_oar3 = {OarPosX+36+ind,OarPosX+26+ind,OarPosX+10+ind,OarPosX+20+ind}; int[] x_oar3 = { OarPosX + 36 + ind, OarPosX + 26 + ind, OarPosX + 10 + ind, OarPosX + 20 + ind };
int[] y_oar3 = {OarPosY+55,OarPosY+55,OarPosY+71,OarPosY+71}; int[] y_oar3 = { OarPosY + 55, OarPosY + 55, OarPosY + 71, OarPosY + 71 };
int[] y_lop3 = {OarPosY+71,OarPosY+80,OarPosY+76,OarPosY+67}; int[] y_lop3 = { OarPosY + 71, OarPosY + 80, OarPosY + 76, OarPosY + 67 };
int[] x_lop3 = {OarPosX+24+ind,OarPosX+16+ind,OarPosX+3+ind,OarPosX+10+ind}; int[] x_lop3 = { OarPosX + 24 + ind, OarPosX + 16 + ind, OarPosX + 3 + ind, OarPosX + 10 + ind };
g2d.fillPolygon(x_oar3,y_oar3,4); g2d.fillPolygon(x_oar3, y_oar3, 4);
g2d.setColor(Color.BLACK); g2d.setColor(Color.BLACK);
g2d.drawPolygon(x_oar3,y_oar3,4); g2d.drawPolygon(x_oar3, y_oar3, 4);
g2d.setColor(OarsColor); g2d.setColor(OarsColor);
g2d.fillPolygon(x_lop3,y_lop3,4); g2d.fillPolygon(x_lop3, y_lop3, 4);
g2d.setColor(Color.BLACK); g2d.setColor(Color.BLACK);
g2d.drawPolygon(x_lop3,y_lop3,4); g2d.drawPolygon(x_lop3, y_lop3, 4);
} }
} }
} }

108
DrawningOarsHandle.java Normal file
View File

@ -0,0 +1,108 @@
import java.awt.*;
public class DrawningOarsHandle implements IDraw {
private NumberType OarsNumb;
public int Cont;
public int OarPosX, OarPosY;
private Color OarsColor;
// vesla Oars
public DrawningOarsHandle(int oarPosX, int oarPosY, Color oarsColor) {
OarPosX = oarPosX;
OarPosY = oarPosY;
OarsColor = oarsColor;
}
public void ChangeOarsNumb(int x) {
if (x <= 1)
OarsNumb = NumberType.One;
if (x == 2)
OarsNumb = NumberType.Two;
if (x >= 3)
OarsNumb = NumberType.Three;
}
public void ChangeX(int x) {
OarPosX = x;
}
public void ChangeY(int y) {
OarPosY = y;
}
public NumberType OarsNumb() {
return OarsNumb;
}
public void DrawOars(Graphics2D g2d) {
g2d.setColor(OarsColor);
int ind = 0;
// 1 veslo
int[] x_lop1 = { OarPosX + ind, OarPosX + 15 + ind, OarPosX + 25 + ind, OarPosX + 10 + ind };
int[] y_lop1 = { OarPosY + 5, OarPosY, OarPosY + 10, OarPosY + 15 };
int[] x_oar1 = { OarPosX + 10 + ind, OarPosX + 20 + ind, OarPosX + 36 + ind, OarPosX + 26 + ind };
int[] y_oar1 = { OarPosY + 10, OarPosY + 10, OarPosY + 26, OarPosY + 26 };
g2d.fillPolygon(x_oar1, y_oar1, 4);
g2d.setColor(Color.BLACK);
g2d.drawPolygon(x_oar1, y_oar1, 4);
g2d.setColor(OarsColor);
g2d.fillPolygon(x_lop1, y_lop1, 4);
g2d.setColor(Color.BLACK);
g2d.drawPolygon(x_lop1, y_lop1, 4);
// handle
g2d.setColor(Color.GREEN);
int[] y_hangle = { OarPosY + 20, OarPosY + 20, OarPosY + 26, OarPosY + 26 };
int[] x_hangle = { OarPosX + 20 + ind, OarPosX + 30 + ind, OarPosX + 36 + ind, OarPosX + 26 + ind };
g2d.fillPolygon(x_hangle, y_hangle, 4);
g2d.setColor(Color.BLACK);
g2d.drawPolygon(x_hangle, y_hangle, 4);
// 2 vesla
if (OarsNumb == NumberType.Two || OarsNumb == NumberType.Three) {
ind = 30;
g2d.setColor(OarsColor);
int[] x_lop2 = { OarPosX + ind, OarPosX + 15 + ind, OarPosX + 25 + ind, OarPosX + 10 + ind };
int[] y_lop2 = { OarPosY + 5, OarPosY, OarPosY + 10, OarPosY + 15 };
int[] x_oar2 = { OarPosX + 10 + ind, OarPosX + 20 + ind, OarPosX + 36 + ind, OarPosX + 26 + ind };
int[] y_oar2 = { OarPosY + 10, OarPosY + 10, OarPosY + 26, OarPosY + 26 };
g2d.fillPolygon(x_oar2, y_oar2, 4);
g2d.setColor(Color.BLACK);
g2d.drawPolygon(x_oar2, y_oar2, 4);
g2d.setColor(OarsColor);
g2d.fillPolygon(x_lop2, y_lop2, 4);
g2d.setColor(Color.BLACK);
g2d.drawPolygon(x_lop2, y_lop2, 4);
// hangle
g2d.setColor(Color.GREEN);
int[] y_hangle2 = { OarPosY + 20, OarPosY + 20, OarPosY + 26, OarPosY + 26 };
int[] x_hangle2 = { OarPosX + 20 + ind, OarPosX + 30 + ind, OarPosX + 36 + ind, OarPosX + 26 + ind };
g2d.fillPolygon(x_hangle2, y_hangle2, 4);
g2d.setColor(Color.BLACK);
g2d.drawPolygon(x_hangle2, y_hangle2, 4);
}
// 3 vesla
if (OarsNumb == NumberType.Three) {
ind = 0;
g2d.setColor(OarsColor);
int[] x_oar3 = { OarPosX + 36 + ind, OarPosX + 26 + ind, OarPosX + 10 + ind, OarPosX + 20 + ind };
int[] y_oar3 = { OarPosY + 55, OarPosY + 55, OarPosY + 71, OarPosY + 71 };
int[] y_lop3 = { OarPosY + 71, OarPosY + 80, OarPosY + 76, OarPosY + 67 };
int[] x_lop3 = { OarPosX + 24 + ind, OarPosX + 16 + ind, OarPosX + 3 + ind, OarPosX + 10 + ind };
g2d.fillPolygon(x_oar3, y_oar3, 4);
g2d.setColor(Color.BLACK);
g2d.drawPolygon(x_oar3, y_oar3, 4);
g2d.setColor(OarsColor);
g2d.fillPolygon(x_lop3, y_lop3, 4);
g2d.setColor(Color.BLACK);
g2d.drawPolygon(x_lop3, y_lop3, 4);
// hangle
g2d.setColor(Color.GREEN);
int[] y_hangle3 = { OarPosY + 55, OarPosY + 55, OarPosY + 61, OarPosY + 61 };
int[] x_hangle3 = { OarPosX + 36 + ind, OarPosX + 26 + ind, OarPosX + 20 + ind, OarPosX + 30 + ind };
g2d.fillPolygon(x_hangle3, y_hangle3, 4);
g2d.setColor(Color.BLACK);
g2d.drawPolygon(x_hangle3, y_hangle3, 4);
}
}
}

79
DrawningOarsOrn.java Normal file
View File

@ -0,0 +1,79 @@
import java.awt.*;
public class DrawningOarsOrn implements IDraw {
private NumberType OarsNumb;
public int Cont;
public int OarPosX, OarPosY;
private Color OarsColor;
// vesla Oars
public DrawningOarsOrn(int oarPosX, int oarPosY, Color oarsColor) {
OarPosX = oarPosX;
OarPosY = oarPosY;
OarsColor = oarsColor;
}
public void ChangeOarsNumb(int x) {
if (x <= 1)
OarsNumb = NumberType.One;
if (x == 2)
OarsNumb = NumberType.Two;
if (x >= 3)
OarsNumb = NumberType.Three;
}
public void ChangeX(int x) {
OarPosX = x;
}
public void ChangeY(int y) {
OarPosY = y;
}
public NumberType OarsNumb() {
return OarsNumb;
}
public void DrawOars(Graphics2D g2d) {
g2d.setColor(OarsColor);
int ind = 0;
// 1 veslo
int[] x_oar1 = { OarPosX + 10 + ind, OarPosX + 20 + ind, OarPosX + 36 + ind, OarPosX + 26 + ind };
int[] y_oar1 = { OarPosY + 10, OarPosY + 10, OarPosY + 26, OarPosY + 26 };
g2d.fillPolygon(x_oar1, y_oar1, 4);
g2d.setColor(Color.BLACK);
g2d.drawPolygon(x_oar1, y_oar1, 4);
g2d.setColor(OarsColor);
g2d.fillOval(OarPosX + ind + 5, OarPosY, 20, 15);
g2d.setColor(Color.BLACK);
g2d.drawOval(OarPosX + ind + 5, OarPosY, 20, 15);
// 2 vesla
if (OarsNumb == NumberType.Two || OarsNumb == NumberType.Three) {
ind = 30;
g2d.setColor(OarsColor);
int[] x_oar2 = { OarPosX + 10 + ind, OarPosX + 20 + ind, OarPosX + 36 + ind, OarPosX + 26 + ind };
int[] y_oar2 = { OarPosY + 10, OarPosY + 10, OarPosY + 26, OarPosY + 26 };
g2d.fillPolygon(x_oar2, y_oar2, 4);
g2d.setColor(Color.BLACK);
g2d.drawPolygon(x_oar2, y_oar2, 4);
g2d.setColor(OarsColor);
g2d.fillOval(OarPosX + ind + 5, OarPosY, 20, 15);
g2d.setColor(Color.BLACK);
g2d.drawOval(OarPosX + ind + 5, OarPosY, 20, 15);
}
// 3 vesla
if (OarsNumb == NumberType.Three) {
ind = 0;
g2d.setColor(OarsColor);
int[] x_oar3 = { OarPosX + 36 + ind, OarPosX + 26 + ind, OarPosX + 10 + ind, OarPosX + 20 + ind };
int[] y_oar3 = { OarPosY + 55, OarPosY + 55, OarPosY + 71, OarPosY + 71 };
g2d.fillPolygon(x_oar3, y_oar3, 4);
g2d.setColor(Color.BLACK);
g2d.drawPolygon(x_oar3, y_oar3, 4);
g2d.setColor(OarsColor);
g2d.fillOval(OarPosX + 5, OarPosY + 65, 20, 15);
g2d.setColor(Color.BLACK);
g2d.drawOval(OarPosX + 5, OarPosY + 65, 20, 15);
}
}
}

View File

@ -0,0 +1,31 @@
public class DrawningObjectCatamaran implements IMoveableObject{
private final DrawningCatamaran _DrawningCatamaran;
public DrawningObjectCatamaran(DrawningCatamaran drawningCatamaran){
_DrawningCatamaran = drawningCatamaran;
}
public ObjectParameters GetObjectParameters(){
if(_DrawningCatamaran == null || _DrawningCatamaran.EntityCatamaran() == null)
return null;
return new ObjectParameters(_DrawningCatamaran.GetPosX(), _DrawningCatamaran.GetPosY(),
_DrawningCatamaran.GetWidth(), _DrawningCatamaran.GetHeight());
}
public int GetStep(){
if(_DrawningCatamaran.EntityCatamaran() == null)
return 0;
return (int)_DrawningCatamaran.EntityCatamaran().Step();
}
public boolean CheckCanMove(DirectionType direction){
if(_DrawningCatamaran == null)
return false;
return _DrawningCatamaran.CanMove(direction);
}
public void MoveObject(DirectionType direction){
if(_DrawningCatamaran == null)
return;
_DrawningCatamaran.MoveTransport(direction);
}
}

View File

@ -16,7 +16,7 @@ public class EntityCatamaran {
public boolean BodyKit(){return BodyKit;} public boolean BodyKit(){return BodyKit;}
public boolean Seats(){return Seats;} public boolean Seats(){return Seats;}
public void Init(int speed, double weight, Color bodyColor,Color oarColor, boolean bodyKit, boolean seats){ public EntityCatamaran(int speed, double weight, Color bodyColor, Color oarColor, boolean bodyKit,boolean seats) {
Speed = speed; Speed = speed;
Weight = weight; Weight = weight;
Step = (double)Speed * 100 / Weight; Step = (double)Speed * 100 / Weight;

19
EntityCatamaranPro.java Normal file
View File

@ -0,0 +1,19 @@
import java.awt.*;
public class EntityCatamaranPro extends EntityCatamaran{
public Color AdditionalColor;
public boolean Motor;
public boolean Sheet;
public Color AdditionalColor(){return AdditionalColor;}
public boolean Motor(){return Motor;}
public boolean Sheet(){return Sheet;}
public EntityCatamaranPro(int speed, double weight, Color bodyColor, Color oarsColor,Color additionalColor, boolean bodykit, boolean motor, boolean sheet, boolean seats)
{
super(speed,weight,bodyColor,oarsColor,bodykit,seats);
AdditionalColor = additionalColor;
Motor = motor;
Sheet = sheet;
}
}

View File

@ -1,94 +1,178 @@
import java.awt.*; import java.awt.*;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.Random; import java.util.Random;
import javax.swing.*; import javax.swing.*;
public class FormCatamaran extends JFrame{ public class FormCatamaran {
private JPanel CatamaranPanel; Canvas canv;
private DrawningCatamaran DrawningCatamaran;
private AbstractStrategy _abstractStrategy;
private JButton UpButton; private JButton UpButton;
private JButton LeftButton; private JButton LeftButton;
private JButton RightButton; private JButton RightButton;
private JButton DownButton; private JButton DownButton;
private JButton CreateButton; private JButton CreateButton;
private JButton CreateButtonPro;
public FormCatamaran() throws IOException{ final int pictureBoxWidth = 885;
this.CatamaranPanel = crPanel(); final int pictureBoxHeight = 462;
public void Draw() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); canv.repaint();
setSize(900,500);
setLayout(new BorderLayout(1,1));
add(CatamaranPanel, BorderLayout.CENTER);
setTitle("Form Catamaran");
setVisible(true);
} }
public JPanel crPanel()throws IOException{ public FormCatamaran() {
JPanel panel = new JPanel(); JFrame CatamaranFrame = new JFrame();
panel.setLayout(null);
UpButton = new JButton(""); UpButton = new JButton("");
LeftButton = new JButton(""); LeftButton = new JButton("");
RightButton = new JButton(""); RightButton = new JButton("");
DownButton = new JButton(""); DownButton = new JButton("");
CreateButton = new JButton("Создать"); CreateButton = new JButton("Создать");
CreateButton.setBounds(12, 401, 90, 40); CreateButtonPro = new JButton("Создать про");
RightButton.setBounds(830,391,50,50); JButton buttonStep = new JButton("Шаг");
LeftButton.setBounds(718,391,50,50); JComboBox comboBoxStrategy = new JComboBox(
UpButton.setBounds(774,335,50,50); new String[] {"Довести до центра","Довести до края",
DownButton.setBounds(774,391,50,50); });
buttonStep.addActionListener(new ActionListener() {
Random random = new Random(); public void actionPerformed(ActionEvent e) {
DrawningCatamaran catamaran = new DrawningCatamaran(); if (DrawningCatamaran == 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 DrawningObjectCatamaran(DrawningCatamaran), pictureBoxWidth,
pictureBoxHeight);
comboBoxStrategy.setEnabled(false);
}
if (_abstractStrategy == null) {
return;
}
_abstractStrategy.MakeStep();
Draw();
if (_abstractStrategy.GetStatus() == Status.Finish) {
comboBoxStrategy.setEnabled(true);
_abstractStrategy = null;
}
}
});
CreateButton.addActionListener(new ActionListener() { CreateButton.addActionListener(new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
catamaran.Init(random.nextInt(1000),1000,Color.getHSBColor(random.nextInt(301), random.nextInt(301), random.nextInt(301)),Color.getHSBColor(random.nextInt(301), random.nextInt(301), random.nextInt(301)),900,500,random.nextBoolean(),random.nextBoolean(),panel); Random random = new Random();
catamaran.DrawnCatamaran(); DrawningCatamaran = new DrawningCatamaran(random.nextInt(200,1000), random.nextDouble(1000,3000),
Color.getHSBColor(random.nextInt(301), random.nextInt(301), random.nextInt(301)),
Color.getHSBColor(random.nextInt(301), random.nextInt(301), random.nextInt(301)), pictureBoxWidth,
pictureBoxHeight, random.nextBoolean(), random.nextBoolean());
canv.DrawningCatamaran = DrawningCatamaran;
comboBoxStrategy.enable(true);
Draw();
}
});
CreateButtonPro.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Random random = new Random();
DrawningCatamaran = new DrawningCatamaranPro(random.nextInt(200,1000), random.nextDouble(1000,3000),
Color.getHSBColor(random.nextInt(301), random.nextInt(301), random.nextInt(301)),
Color.getHSBColor(random.nextInt(301), random.nextInt(301), random.nextInt(301)),
pictureBoxWidth, pictureBoxHeight,
random.nextBoolean(), random.nextBoolean(),
Color.getHSBColor(random.nextInt(301), random.nextInt(301), random.nextInt(301)),
random.nextBoolean(), random.nextBoolean());
canv.DrawningCatamaran = DrawningCatamaran;
comboBoxStrategy.enable(true);
Draw();
} }
}); });
RightButton.addActionListener(new ActionListener() { RightButton.addActionListener(new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
if(catamaran.EntityCatamaran() == null) if (DrawningCatamaran.EntityCatamaran() == null) {
return; return;
catamaran.MoveTransport(DirectionType.Right); }
catamaran.DrawnCatamaran(); DrawningCatamaran.MoveTransport(DirectionType.Right);
Draw();
} }
}); });
LeftButton.addActionListener(new ActionListener() { LeftButton.addActionListener(new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
if(catamaran.EntityCatamaran() == null) if (DrawningCatamaran.EntityCatamaran() == null)
return; return;
catamaran.MoveTransport(DirectionType.Left); DrawningCatamaran.MoveTransport(DirectionType.Left);
catamaran.DrawnCatamaran(); Draw();
} }
}); });
UpButton.addActionListener(new ActionListener() { UpButton.addActionListener(new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
if(catamaran.EntityCatamaran() == null) if (DrawningCatamaran.EntityCatamaran() == null)
return; return;
catamaran.MoveTransport(DirectionType.Up); DrawningCatamaran.MoveTransport(DirectionType.Up);
catamaran.DrawnCatamaran(); Draw();
} }
}); });
DownButton.addActionListener(new ActionListener() { DownButton.addActionListener(new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
if(catamaran.EntityCatamaran() == null) if (DrawningCatamaran.EntityCatamaran() == null)
return; return;
catamaran.MoveTransport(DirectionType.Down); DrawningCatamaran.MoveTransport(DirectionType.Down);
catamaran.DrawnCatamaran(); Draw();
} }
}); });
CatamaranFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel.add(UpButton); CatamaranFrame.setSize(900, 500);
panel.add(LeftButton); CatamaranFrame.setLayout(null);
panel.add(RightButton); CatamaranFrame.setTitle("Form Catamaran");
panel.add(DownButton); CreateButtonPro.setBounds(120, 401, 120, 40);
panel.add(CreateButton); CreateButton.setBounds(12, 401, 90, 40);
return panel; RightButton.setBounds(830, 391, 50, 50);
LeftButton.setBounds(718, 391, 50, 50);
UpButton.setBounds(774, 335, 50, 50);
DownButton.setBounds(774, 391, 50, 50);
comboBoxStrategy.setBounds(719, 12, 151, 28);
buttonStep.setBounds(768, 46, 94, 29);
canv = new Canvas();
canv.setSize(pictureBoxWidth, pictureBoxHeight);
CatamaranFrame.add(canv);
CatamaranFrame.add(comboBoxStrategy);
CatamaranFrame.add(UpButton);
CatamaranFrame.add(DownButton);
CatamaranFrame.add(LeftButton);
CatamaranFrame.add(RightButton);
CatamaranFrame.add(CreateButton);
CatamaranFrame.add(CreateButtonPro);
CatamaranFrame.add(buttonStep);
CatamaranFrame.setVisible(true);
}
}
class Canvas extends JComponent {
public DrawningCatamaran DrawningCatamaran;
public Canvas() {
}
public void paintComponent(Graphics g) {
if (DrawningCatamaran == null) {
return;
}
super.paintComponents(g);
Graphics2D g2d = (Graphics2D) g;
DrawningCatamaran.DrawnCatamaran(g2d);
super.repaint();
} }
} }

8
IDraw.java Normal file
View File

@ -0,0 +1,8 @@
import java.awt.*;
public interface IDraw {
public void ChangeOarsNumb(int x);
public NumberType OarsNumb();
public void DrawOars(Graphics2D g2d);
public void ChangeX(int x);
public void ChangeY(int y);
}

6
IMoveableObject.java Normal file
View File

@ -0,0 +1,6 @@
public interface IMoveableObject {
public ObjectParameters GetObjectParameters();
public int GetStep();
boolean CheckCanMove(DirectionType direction);
void MoveObject(DirectionType direction);
}

34
MoveToBorder.java Normal file
View File

@ -0,0 +1,34 @@
public class MoveToBorder extends AbstractStrategy {
@Override
protected boolean IsTargetDestination() {
var objParams = GetObjectParameters();
if (objParams == null) {
return false;
}
int a = FieldWidth();
int b = FieldHeight();
int q = GetStep();
return objParams.RightBorder <= FieldWidth() && objParams.RightBorder + GetStep() >= FieldWidth() &&
objParams.DownBorder <= FieldHeight() && objParams.DownBorder + GetStep() >= FieldHeight();
}
@Override
protected void MoveToTarget() {
var objParams = GetObjectParameters();
if (objParams == null) {
return;
}
var diffX = objParams.RightBorder - FieldWidth();
if (Math.abs(diffX) >= GetStep()) {
if (diffX < 0) {
MoveRight();
}
}
var diffY = objParams.DownBorder - FieldHeight();
if (Math.abs(diffY) >= GetStep()) {
if (diffY < 0) {
MoveDown();
}
}
}
}

42
MoveToCenter.java Normal file
View File

@ -0,0 +1,42 @@
public class MoveToCenter extends AbstractStrategy {
@Override
protected boolean IsTargetDestination() {
var objParams = GetObjectParameters();
if (objParams == null)
return false;
return ((objParams.ObjectMiddleHorizontal <= FieldWidth() / 2 &&
objParams.ObjectMiddleHorizontal + GetStep() >= FieldWidth() / 2)
|| (objParams.ObjectMiddleHorizontal >= FieldWidth() / 2 &&
objParams.ObjectMiddleHorizontal - GetStep() <= FieldWidth() / 2))
&&
((objParams.ObjectMiddleVertical <= FieldHeight() / 2 &&
objParams.ObjectMiddleVertical + GetStep() >= FieldHeight() / 2) ||
(objParams.ObjectMiddleVertical >= FieldHeight() / 2 &&
objParams.ObjectMiddleVertical - GetStep() <= FieldHeight() / 2));
}
@Override
protected void MoveToTarget() {
var objParams = GetObjectParameters();
if (objParams == null) {
return;
}
var diffX = objParams.ObjectMiddleHorizontal - FieldWidth() / 2;
if (Math.abs(diffX) > GetStep()) {
if (diffX > 0) {
MoveLeft();
} else {
MoveRight();
}
}
var diffY = objParams.ObjectMiddleVertical - FieldHeight() / 2;
if (Math.abs(diffY) > GetStep()) {
if (diffY > 0) {
MoveUp();
} else {
MoveDown();
}
}
}
}

27
ObjectParameters.java Normal file
View File

@ -0,0 +1,27 @@
public class ObjectParameters {
private final int _x;
private final int _y;
private final int _width;
private final int _height;
public int LeftBorder;
public int TopBorder;
public int RightBorder;
public int DownBorder;
public int ObjectMiddleHorizontal;
public int ObjectMiddleVertical;
public ObjectParameters(int x, int y, int width, int height)
{
_x = x;
_y = y;
_width = width;
_height = height;
LeftBorder = _x;
TopBorder = _y;
RightBorder = _x + width;
DownBorder = _y + height;
ObjectMiddleHorizontal = _x + _width / 2;
ObjectMiddleVertical = _y + _height / 2;
}
}

5
Status.java Normal file
View File

@ -0,0 +1,5 @@
public enum Status {
NotInit,
InProgress,
Finish
}