Compare commits

...

17 Commits

Author SHA1 Message Date
6ce40d0e13 лаб5 2023-11-20 19:04:16 +04:00
b836adc7ce Merge branch 'lab3_compl' into lab4_compl 2023-11-17 16:38:49 +04:00
5508315dd2 мелкие ошибки 2023-11-07 16:05:40 +04:00
7770c539ae ready 2023-11-04 20:38:51 +04:00
2d2dc5741e комм 2023-11-04 19:51:02 +04:00
947ef24205 change 2023-11-04 19:41:05 +04:00
9d0594068b полиморф 2023-11-03 23:38:13 +04:00
aeaba7615e mbFinal 2023-10-24 13:02:54 +04:00
9c43a295f5 наверно финал 2023-10-24 01:45:26 +04:00
941cc8140e 3 слож 2023-10-24 01:06:14 +04:00
a33fc9db36 Merge branch 'lab1_compl' into lab2_co 2023-10-20 19:36:30 +04:00
53064877f6 изм 2023-10-20 18:55:41 +04:00
2f936be74a коммит 2023-10-20 18:49:21 +04:00
1c91b62c04 лаб1 сложная готова точно 2023-10-10 13:11:02 +04:00
91f5aff2a2 готова 2023-10-09 23:11:13 +04:00
7b57f7dc42 промежуточная 2023-10-07 21:42:37 +04:00
911e8bfdc5 лаба1 2023-09-25 18:40:25 +04:00
30 changed files with 1974 additions and 0 deletions

79
AbstractStrategy.java Normal file
View File

@ -0,0 +1,79 @@
public abstract class AbstractStrategy {
private IMoveableObject _moveableObject;
private Status _state = Status.NotInit;
protected int FieldWidth;
protected int 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 (IsTargetDestinaion())
{
_state = Status.Finish;
return;
}
MoveToTarget();
}
protected boolean MoveLeft() { return MoveTo(Direction.Left);}
protected boolean MoveRight() { return MoveTo(Direction.Right);}
protected boolean MoveUp() {return MoveTo(Direction.Up);}
protected boolean MoveDown(){return MoveTo(Direction.Down);}
protected ObjectParameters GetObjectParameters(){
return _moveableObject.GetObjectPosition();
}
protected int GetStep()
{
if (_state != Status.InProgress)
{
return 0;
}
return _moveableObject.GetStep();
}
protected abstract void MoveToTarget();
protected abstract boolean IsTargetDestinaion();
private boolean MoveTo(Direction direction)
{
if (_state != Status.InProgress)
{
return false;
}
if (_moveableObject.CheckCanMove(direction))
{
_moveableObject.MoveObject(direction);
return true;
}
return false;
}
}

3
Direction.java Normal file
View File

@ -0,0 +1,3 @@
public enum Direction {
Up, Down, Left, Right;
}

79
DrawingContainerShip.java Normal file
View File

@ -0,0 +1,79 @@
import java.awt.*;
public class DrawingContainerShip extends DrawingShip{
public IDecksDrawing iDecksDrawing;
public DrawingContainerShip(int speed, double weight, Color bodyColor, Color
additionalColor, boolean crane, boolean containers,int deck, int deckType, int width, int height)
{
super(speed, weight, bodyColor, width, height, 110, 65);
if (EntityShip != null)
{
EntityShip = new EntityContainerShip(speed, weight, bodyColor,
additionalColor, crane, containers,deck, deckType);
if(deckType == 1){
iDecksDrawing = new DrawingDecks();
}
if(deckType == 2){
iDecksDrawing = new DrawingDecksTrapez();
}
if(deckType == 3){
iDecksDrawing = new DrawingDecksRect();
}
iDecksDrawing.setNumDecks(deck);
}
}
public DrawingContainerShip(EntityContainerShip ship, IDecksDrawing decks, int width, int height){
super(ship,width, height);
if(width < _pictureWidth || height < _pictureHeight){
return;
}
iDecksDrawing = decks;
iDecksDrawing.setNumDecks(ship.Deck);
}
@Override
public void DrawShip(Graphics2D g)
{
super.DrawShip(g);
if (EntityShip == null)
{
return;
}
//контейнеры
if (((EntityContainerShip)EntityShip).Conteiners)
{
g.setPaint(((EntityContainerShip)EntityShip).AdditionalColor);
g.fillRect(_startPosX + 50, _startPosY + 55, 35, 10);
g.fillRect(_startPosX + 85, _startPosY + 55, 20, 10);
g.fillRect(_startPosX + 105, _startPosY + 50, 15, 15);
g.fillRect(_startPosX + 50, _startPosY + 45, 15, 10);
g.fillRect(_startPosX + 65, _startPosY + 45, 55, 5);
g.fillRect(_startPosX + 65, _startPosY + 50, 40, 5);
g.setPaint(Color.BLACK);
g.drawRect(_startPosX + 50, _startPosY + 55, 35, 10);
g.drawRect(_startPosX + 85, _startPosY + 55, 20, 10);
g.drawRect(_startPosX + 105, _startPosY + 50, 15, 15);
g.drawRect(_startPosX + 50, _startPosY + 45, 15, 10);
g.drawRect(_startPosX + 65, _startPosY + 45, 55, 5);
g.drawRect(_startPosX + 65, _startPosY + 50, 40, 5);
}
//кран
if (((EntityContainerShip)EntityShip).Crane)
{
g.setPaint(((EntityContainerShip)EntityShip).AdditionalColor);
g.fillRect(_startPosX + 43, _startPosY+20, 5, 45);
g.fillRect(_startPosX + 47, _startPosY + 30, 20, 3);
g.setPaint(Color.BLACK);
g.drawRect(_startPosX + 43, _startPosY+20, 5, 45);
g.drawRect(_startPosX + 47, _startPosY + 30, 20, 3);
g.drawLine(_startPosX + 47, _startPosY+20, _startPosX + 67, _startPosY + 30);
g.drawLine(_startPosX + 67, _startPosY + 33, _startPosX + 67, _startPosY + 45);
}
iDecksDrawing.DrawDeck(_startPosX, _startPosY, EntityShip.BodyColor, g);
}
}

56
DrawingDecks.java Normal file
View File

@ -0,0 +1,56 @@
import java.awt.*;
public class DrawingDecks implements IDecksDrawing{
private NumberOfDecks numDecks;
public NumberOfDecks getProperty(){
return numDecks;
}
public void setNumDecks(int nDecks){
switch(nDecks){
case 1:
numDecks = NumberOfDecks.Deck_1;
break;
case 2:
numDecks = NumberOfDecks.Deck_2;
break;
case 3:
numDecks = NumberOfDecks.Deck_3;
break;
default:
numDecks = NumberOfDecks.Deck_1;
System.out.println("Что-то пошло не так, количество палуб неверное" + Integer.toString(nDecks) + "сделаем вид, будто она одна");
break;
}
}
public void DrawDeck(int _startPosX, int _startPosY,Color bodyColor, Graphics2D g){
if(numDecks == NumberOfDecks.Deck_1){
g.setPaint(bodyColor);
// заполнение борта
int x[] = {_startPosX+ 20, _startPosX+40, _startPosX+110, _startPosX+130, _startPosX+ 20};
int y[] = {_startPosY+65,_startPosY+85, _startPosY+85, _startPosY+65, _startPosY+65};
g.fillPolygon(x, y, 5);
//борт корабля контур
g.setPaint(Color.BLACK);
int _x[] = {_startPosX+ 20, _startPosX+40, _startPosX+110, _startPosX+130, _startPosX+ 20};
int _y[] = {_startPosY+65,_startPosY+85, _startPosY+85, _startPosY+65, _startPosY+65};
g.drawPolyline(_x, _y, 5);
//рисунок на борту
g.drawLine(_startPosX + 43, _startPosY + 80, _startPosX + 47, _startPosY + 80);
g.drawLine(_startPosX + 45, _startPosY + 70, _startPosX + 45, _startPosY + 80);
g.drawLine(_startPosX + 40, _startPosY + 75, _startPosX + 50, _startPosY + 75);
}
if(numDecks == NumberOfDecks.Deck_2){
g.setPaint(Color.BLACK);
g.drawLine(_startPosX + 120, _startPosY + 75, _startPosX+ 55, _startPosY + 75);
}
if(numDecks == NumberOfDecks.Deck_3){
g.setPaint(Color.BLACK);
g.drawLine(_startPosX + 120, _startPosY + 75, _startPosX+ 55, _startPosY + 75);
g.drawLine(_startPosX + 125, _startPosY + 70, _startPosX+ 50, _startPosY + 70);
}
}
}

73
DrawingDecksRect.java Normal file
View File

@ -0,0 +1,73 @@
import java.awt.*;
public class DrawingDecksRect implements IDecksDrawing {
private NumberOfDecks numDecks;
public NumberOfDecks getProperty(){
return numDecks;
}
public void setNumDecks(int nDecks){
switch(nDecks){
case 1:
numDecks = NumberOfDecks.Deck_1;
break;
case 2:
numDecks = NumberOfDecks.Deck_2;
break;
case 3:
numDecks = NumberOfDecks.Deck_3;
break;
default:
numDecks = NumberOfDecks.Deck_1;
System.out.println("Что-то пошло не так, количество палуб неверное" + Integer.toString(nDecks) + "сделаем вид, будто она одна");
break;
}
}
public void DrawDeck(int _startPosX, int _startPosY, Color bodyColor, Graphics2D g){
if(numDecks == NumberOfDecks.Deck_1){
g.setPaint(bodyColor);
int x[] = {_startPosX+ 20, _startPosX+20, _startPosX+130, _startPosX+130,_startPosX+ 20};
int y[] = {_startPosY+65,_startPosY+85, _startPosY+85, _startPosY+65,_startPosY+65};
g.fillPolygon(x, y, 5);
g.setPaint(Color.BLACK);
int _x[] = {_startPosX+ 20, _startPosX+20, _startPosX+130, _startPosX+130,_startPosX+ 20};
int _y[] = {_startPosY+65,_startPosY+85, _startPosY+85, _startPosY+65,_startPosY+65};
g.drawPolyline(_x, _y, 5);
g.drawLine(_startPosX + 43, _startPosY + 80, _startPosX + 47, _startPosY + 80);
g.drawLine(_startPosX + 45, _startPosY + 70, _startPosX + 45, _startPosY + 80);
g.drawLine(_startPosX + 40, _startPosY + 75, _startPosX + 50, _startPosY + 75);
}
if(numDecks == NumberOfDecks.Deck_2){
g.setPaint(bodyColor);
int x[] = {_startPosX+ 20, _startPosX+20, _startPosX+130, _startPosX+130,_startPosX+ 20};
int y[] = {_startPosY+65,_startPosY+85, _startPosY+85, _startPosY+65,_startPosY+65};
g.fillPolygon(x, y, 5);
g.setPaint(Color.BLACK);
int _x[] = {_startPosX+ 20, _startPosX+20, _startPosX+130, _startPosX+130,_startPosX+ 20};
int _y[] = {_startPosY+65,_startPosY+85, _startPosY+85, _startPosY+65,_startPosY+65};
g.drawPolyline(_x, _y, 5);
g.drawLine(_startPosX + 43, _startPosY + 80, _startPosX + 47, _startPosY + 80);
g.drawLine(_startPosX + 45, _startPosY + 70, _startPosX + 45, _startPosY + 80);
g.drawLine(_startPosX + 40, _startPosY + 75, _startPosX + 50, _startPosY + 75);
g.drawLine(_startPosX + 20, _startPosY + 75, _startPosX+ 35, _startPosY + 75);
g.drawLine(_startPosX + 55, _startPosY + 75, _startPosX+ 130, _startPosY + 75);
}
if(numDecks == NumberOfDecks.Deck_3){
g.setPaint(bodyColor);
int x[] = {_startPosX+ 20, _startPosX+20, _startPosX+130, _startPosX+130,_startPosX+ 20};
int y[] = {_startPosY+65,_startPosY+85, _startPosY+85, _startPosY+65,_startPosY+65};
g.fillPolygon(x, y, 5);
g.setPaint(Color.BLACK);
int _x[] = {_startPosX+ 20, _startPosX+20, _startPosX+130, _startPosX+130,_startPosX+ 20};
int _y[] = {_startPosY+65,_startPosY+85, _startPosY+85, _startPosY+65,_startPosY+65};
g.drawPolyline(_x, _y, 5);
g.drawLine(_startPosX + 43, _startPosY + 80, _startPosX + 47, _startPosY + 80);
g.drawLine(_startPosX + 45, _startPosY + 70, _startPosX + 45, _startPosY + 80);
g.drawLine(_startPosX + 40, _startPosY + 75, _startPosX + 50, _startPosY + 75);
g.drawLine(_startPosX + 20, _startPosY + 75, _startPosX+ 35, _startPosY + 75);
g.drawLine(_startPosX + 55, _startPosY + 75, _startPosX+ 130, _startPosY + 75);
g.drawLine(_startPosX + 20, _startPosY + 70, _startPosX+ 40, _startPosY + 70);
g.drawLine(_startPosX + 50, _startPosY + 70, _startPosX+ 130, _startPosY + 70);
}
}
}

73
DrawingDecksTrapez.java Normal file
View File

@ -0,0 +1,73 @@
import java.awt.*;
public class DrawingDecksTrapez implements IDecksDrawing{
private NumberOfDecks numDecks;
public NumberOfDecks getProperty(){
return numDecks;
}
public void setNumDecks(int nDecks){
switch(nDecks){
case 1:
numDecks = NumberOfDecks.Deck_1;
break;
case 2:
numDecks = NumberOfDecks.Deck_2;
break;
case 3:
numDecks = NumberOfDecks.Deck_3;
break;
default:
numDecks = NumberOfDecks.Deck_1;
System.out.println("Что-то пошло не так, количество палуб неверное" + Integer.toString(nDecks) + "сделаем вид, будто она одна");
break;
}
}
public void DrawDeck(int _startPosX, int _startPosY, Color bodyColor, Graphics2D g){
if(numDecks == NumberOfDecks.Deck_1){
g.setPaint(bodyColor);
int x[] = {_startPosX+ 20, _startPosX+20, _startPosX+25, _startPosX+40, _startPosX+ 110,_startPosX+125, _startPosX+130, _startPosX+130, _startPosX+ 20};
int y[] = {_startPosY+65,_startPosY+70, _startPosY+70, _startPosY+85, _startPosY+85,_startPosY+70, _startPosY+70, _startPosY+65, _startPosY+65};
g.fillPolygon(x, y, 9);
g.setPaint(Color.BLACK);
int _x[] = {_startPosX+ 20, _startPosX+20, _startPosX+25, _startPosX+40, _startPosX+ 110,_startPosX+125, _startPosX+130, _startPosX+130, _startPosX+ 20};
int _y[] = {_startPosY+65,_startPosY+70, _startPosY+70, _startPosY+85, _startPosY+85,_startPosY+70, _startPosY+70, _startPosY+65, _startPosY+65};
g.drawPolyline(_x, _y, 9);
g.drawLine(_startPosX + 43, _startPosY + 80, _startPosX + 47, _startPosY + 80);
g.drawLine(_startPosX + 45, _startPosY + 70, _startPosX + 45, _startPosY + 80);
g.drawLine(_startPosX + 40, _startPosY + 75, _startPosX + 50, _startPosY + 75);
}
if(numDecks == NumberOfDecks.Deck_2){
g.setPaint(bodyColor);
int x[] = {_startPosX+ 20, _startPosX+20, _startPosX+25,_startPosX+ 25, _startPosX+30, _startPosX+40, _startPosX+ 110, _startPosX+120, _startPosX+ 125,_startPosX+125, _startPosX+130, _startPosX+130, _startPosX+ 20};
int y[] = {_startPosY+65,_startPosY+70, _startPosY+70,_startPosY+75, _startPosY+75, _startPosY+85, _startPosY+85,_startPosY+75, _startPosY+75,_startPosY+70, _startPosY+70, _startPosY+65, _startPosY+65};
g.fillPolygon(x, y, 13);
g.setPaint(Color.BLACK);
int _x[] = {_startPosX+ 20, _startPosX+20, _startPosX+25,_startPosX+ 25, _startPosX+30, _startPosX+40, _startPosX+ 110, _startPosX+120, _startPosX+ 125,_startPosX+125, _startPosX+130, _startPosX+130, _startPosX+ 20};
int _y[] = {_startPosY+65,_startPosY+70, _startPosY+70,_startPosY+75, _startPosY+75, _startPosY+85, _startPosY+85,_startPosY+75, _startPosY+75,_startPosY+70, _startPosY+70, _startPosY+65, _startPosY+65};
g.drawPolyline(_x, _y, 13);
g.drawLine(_startPosX + 43, _startPosY + 80, _startPosX + 47, _startPosY + 80);
g.drawLine(_startPosX + 45, _startPosY + 70, _startPosX + 45, _startPosY + 80);
g.drawLine(_startPosX + 40, _startPosY + 75, _startPosX + 50, _startPosY + 75);
g.drawLine(_startPosX + 120, _startPosY + 75, _startPosX+ 55, _startPosY + 75);
g.drawLine(_startPosX + 30, _startPosY + 75, _startPosX+ 35, _startPosY + 75);
}
if(numDecks == NumberOfDecks.Deck_3){
g.setPaint(bodyColor);
int x[] = {_startPosX+ 20, _startPosX+20, _startPosX+25,_startPosX+ 25, _startPosX+30, _startPosX+30, _startPosX+ 120, _startPosX+120, _startPosX+ 125,_startPosX+125, _startPosX+130, _startPosX+130, _startPosX+ 20};
int y[] = {_startPosY+65,_startPosY+70, _startPosY+70,_startPosY+75, _startPosY+75, _startPosY+85, _startPosY+85,_startPosY+75, _startPosY+75,_startPosY+70, _startPosY+70, _startPosY+65, _startPosY+65};
g.fillPolygon(x, y, 13);
g.setPaint(Color.BLACK);
int _x[] = {_startPosX+ 20, _startPosX+20, _startPosX+25,_startPosX+ 25, _startPosX+30, _startPosX+30, _startPosX+ 120, _startPosX+120, _startPosX+ 125,_startPosX+125, _startPosX+130, _startPosX+130, _startPosX+ 20};
int _y[] = {_startPosY+65,_startPosY+70, _startPosY+70,_startPosY+75, _startPosY+75, _startPosY+85, _startPosY+85,_startPosY+75, _startPosY+75,_startPosY+70, _startPosY+70, _startPosY+65, _startPosY+65};
g.drawPolyline(_x, _y, 13);
g.drawLine(_startPosX + 43, _startPosY + 80, _startPosX + 47, _startPosY + 80);
g.drawLine(_startPosX + 45, _startPosY + 70, _startPosX + 45, _startPosY + 80);
g.drawLine(_startPosX + 40, _startPosY + 75, _startPosX + 50, _startPosY + 75);
g.drawLine(_startPosX + 120, _startPosY + 75, _startPosX+ 55, _startPosY + 75);
g.drawLine(_startPosX + 125, _startPosY + 70, _startPosX+ 50, _startPosY + 70);
g.drawLine(_startPosX + 30, _startPosY + 75, _startPosX+ 35, _startPosY + 75);
g.drawLine(_startPosX + 25, _startPosY + 70, _startPosX+ 40, _startPosY + 70);
}
}
}

149
DrawingShip.java Normal file
View File

@ -0,0 +1,149 @@
import java.awt.*;
public class DrawingShip {
public IMoveableObject GetMoveableObject()
{
return new DrawningObjectShip(this);
}
public EntityShip EntityShip;
public int _pictureWidth;
public int _pictureHeight;
public int _startPosX;
public int _startPosY;
public int _shipWidth = 110;
public int _shipHeight = 65;
public int GetPosX(){
return _startPosX;
}
public int GetPosY(){
return _startPosY;
}
public int GetWidth(){
return _shipWidth;
}
public int GetHeight(){
return _shipHeight;
}
public boolean CanMove(Direction direction)
{
if(EntityShip == null) return false;
switch(direction)
{
case Left:
return _startPosX - EntityShip.Step > 0;
case Right:
return _startPosX + _shipWidth + EntityShip.Step < _pictureWidth;
case Up:
return _startPosY - EntityShip.Step > 0;
case Down:
return _startPosY + _shipHeight+ EntityShip.Step < _pictureHeight;
default:
return false;
}
}
public DrawingShip(int speed, double weight, Color bodyColor, int width, int height)
{
if(width < _shipWidth || height < _shipHeight)
{
return;
}
_pictureWidth = width;
_pictureHeight = height;
EntityShip = new EntityShip(speed, weight, bodyColor);
}
public DrawingShip(EntityShip ship, int width, int height){
if(width < _shipWidth || height < _shipHeight)
{
return;
}
_pictureWidth = width;
_pictureHeight = height;
EntityShip = ship;
}
protected DrawingShip(int speed, double weight, Color bodyColor, int width, int height, int shipWidth, int shipHeight)
{
if (width < _shipWidth || height < _shipHeight)
{
return;
}
_pictureWidth = width;
_pictureHeight = height;
_shipHeight = shipHeight;
_shipWidth = shipWidth;
EntityShip = new EntityShip(speed, weight, bodyColor);
}
public void SetPosition(int x, int y)
{
_startPosX = Math.min(x, _pictureWidth - _shipWidth);
_startPosY = Math.min(y, _pictureHeight - _shipHeight);
}
public void MoveTransport(Direction direction)
{
if (EntityShip == null)
{
return;
}
switch (direction)
{
case Left:
if (_startPosX - EntityShip.Step > 0)
{
_startPosX -= (int)EntityShip.Step;
}
break;
case Up:
if (_startPosY - EntityShip.Step > 0)
{
_startPosY -= (int)EntityShip.Step;
}
break;
case Right:
if (_startPosX + EntityShip.Step + _shipWidth < _pictureWidth)
{
_startPosX += (int)EntityShip.Step;
}
break;
case Down:
if (_startPosY + EntityShip.Step + _shipHeight< _pictureHeight)
{
_startPosY += (int)EntityShip.Step;
}
break;
}
}
public void DrawShip(Graphics2D g)
{
if (EntityShip == null)
{
return;
}
g.setPaint(EntityShip.BodyColor);
// заполнение борта
int x[] = {_startPosX+ 20, _startPosX+40, _startPosX+110, _startPosX+130, _startPosX+ 20};
int y[] = {_startPosY+65,_startPosY+85, _startPosY+85, _startPosY+65, _startPosY+65};
g.fillPolygon(x, y, 5);
//борт корабля контур
g.setPaint(Color.BLACK);
int _x[] = {_startPosX+ 20, _startPosX+40, _startPosX+110, _startPosX+130, _startPosX+ 20};
int _y[] = {_startPosY+65,_startPosY+85, _startPosY+85, _startPosY+65, _startPosY+65};
g.drawPolyline(_x, _y, 5);
//рисунок на борту
g.drawLine(_startPosX + 43, _startPosY + 80, _startPosX + 47, _startPosY + 80);
g.drawLine(_startPosX + 45, _startPosY + 70, _startPosX + 45, _startPosY + 80);
g.drawLine(_startPosX + 40, _startPosY + 75, _startPosX + 50, _startPosY + 75);
}
}

32
DrawningObjectShip.java Normal file
View File

@ -0,0 +1,32 @@
public class DrawningObjectShip implements IMoveableObject{
private DrawingShip _drawingShip = null;
public DrawningObjectShip(DrawingShip drawingShip)
{
_drawingShip = drawingShip;
}
public ObjectParameters GetObjectPosition()
{
if (_drawingShip == null || _drawingShip.EntityShip ==
null)
{
return null;
}
return new ObjectParameters(_drawingShip.GetPosX(),
_drawingShip.GetPosY(), _drawingShip.GetWidth(), _drawingShip.GetHeight());
}
public int GetStep()
{
return (int)(_drawingShip.EntityShip.Step);
}
public boolean CheckCanMove(Direction direction)
{
return _drawingShip.CanMove(direction);
}
public void MoveObject(Direction direction)
{
_drawingShip.MoveTransport(direction);
}
}

21
EntityContainerShip.java Normal file
View File

@ -0,0 +1,21 @@
import java.awt.*;
public class EntityContainerShip extends EntityShip{
public Color AdditionalColor;
public boolean Crane;
public boolean Conteiners;
public int Deck;
public int DeckType;
public EntityContainerShip(int speed, double weight, Color bodyColor, Color
additionalColor, boolean crane, boolean containers, int deck, int deckType)
{
super(speed, weight,bodyColor);
AdditionalColor = additionalColor;
Crane = crane;
Conteiners = containers;
Deck = deck;
DeckType = deckType;
}
public void setAdditionalColor(Color color){AdditionalColor = color;}
}

15
EntityShip.java Normal file
View File

@ -0,0 +1,15 @@
import java.awt.*;
public class EntityShip {
public int Speed;
public double Weight;
public Color BodyColor;
public double Step;
public EntityShip(int speed, double weight, Color bodyColor)
{
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
Step = (double)Speed * 100 / Weight;
}
public void setBodyColor(Color color){BodyColor = color;}
}

209
FormContainerShip.java Normal file
View File

@ -0,0 +1,209 @@
import java.awt.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
public class FormContainerShip{
public DrawingShip _drawingShip;
private AbstractStrategy _abstractStrategy;
Canvas canv;
class Canvas extends JComponent{
public Canvas(){
}
public void paintComponent (Graphics g){
if (_drawingShip == null){
return;
}
super.paintComponents (g) ;
Graphics2D g2d = (Graphics2D)g;
_drawingShip.DrawShip(g2d);
super.repaint();
}
}
public DrawingShip SelectedShip;
public boolean DialogResult = false;
public JButton buttonSelectedShip;
public JFrame w;
public void Draw(){
canv.repaint();
}
public FormContainerShip(){
SelectedShip = null;
w = new JFrame ("ContainerShip");
JButton buttonCreateShip = new JButton("создать кораблик");
JButton buttonCreateContainerShip = new JButton("создать контейнеровоз");
JButton buttonStep = new JButton("шаг");
buttonSelectedShip = new JButton("выбрать");
String[] items = {
"MoveToCenter", "MoveToRight-Down"
};
JComboBox comboBoxStrategy = new JComboBox(items);
comboBoxStrategy.setEditable(false);
JButton up = new JButton();
up.setBorderPainted(false);
up.setFocusPainted(false);
up.setContentAreaFilled(false);
up.setName("up");
up.setIcon(new ImageIcon("D:\\рабочий стол\\рпп\\лаб1.репозит.сложная\\PIbd21.LyovushkinaA.A.Container_ship.Complicated\\photo11.png"));
JButton down = new JButton();
down.setBorderPainted(false);
down.setFocusPainted(false);
down.setContentAreaFilled(false);
down.setName("down");
down.setIcon(new ImageIcon("D:\\рабочий стол\\рпп\\лаб1.репозит.сложная\\PIbd21.LyovushkinaA.A.Container_ship.Complicated\\photo33.png"));
JButton left = new JButton();
left.setBorderPainted(false);
left.setFocusPainted(false);
left.setContentAreaFilled(false);
left.setName("left");
left.setIcon(new ImageIcon("D:\\рабочий стол\\рпп\\лаб1.репозит.сложная\\PIbd21.LyovushkinaA.A.Container_ship.Complicated\\photo44.png"));
JButton right = new JButton();
right.setBorderPainted(false);
right.setFocusPainted(false);
right.setContentAreaFilled(false);
right.setName("right");
right.setIcon(new ImageIcon("D:\\рабочий стол\\рпп\\лаб1.репозит.сложная\\PIbd21.LyovushkinaA.A.Container_ship.Complicated\\photo22.png"));
buttonCreateShip.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e){
System.out.println(e.getActionCommand());
Random random = new Random();
Color color = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256));
Color selectedColor = JColorChooser.showDialog(w, "Выберите цвет", Color.WHITE);
if(selectedColor != null){
color = selectedColor;
}
_drawingShip = new DrawingShip(random.nextInt(100, 300),
random.nextInt(1000, 3000),
color,960, 560);
_drawingShip.SetPosition(random.nextInt(10, 100),random.nextInt(10, 100));
Draw();
}
}
);
buttonCreateContainerShip.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e){
System.out.println(e.getActionCommand());
Random random = new Random();
Color color = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256));
Color selectedColor = JColorChooser.showDialog(w, "Выберите цвет", Color.WHITE);
if(selectedColor != null){
color = selectedColor;
}
Color color2 = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256));
Color selectedColor2 = JColorChooser.showDialog(w, "Выберите цвет", Color.WHITE);
if(selectedColor2 != null){
color2 = selectedColor2;
}
_drawingShip = new DrawingContainerShip(random.nextInt(100, 300),
random.nextInt(1000, 3000),color,color2,random.nextBoolean(), random.nextBoolean(), random.nextInt(1,4),random.nextInt(1,4),960, 560);
_drawingShip.SetPosition(random.nextInt(10, 100),random.nextInt(10, 100));
Draw();
}
}
);
buttonStep.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
if (_drawingShip == 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
DrawningObjectShip(_drawingShip), 960,
560);
comboBoxStrategy.setEnabled(false);
}
if (_abstractStrategy == null)
{
return;
}
_abstractStrategy.MakeStep();
Draw();
if (_abstractStrategy.GetStatus() == Status.Finish)
{
comboBoxStrategy.setEnabled(true);
_abstractStrategy = null;
}
}
}
);
ActionListener actioListener = new ActionListener() {
public void actionPerformed(ActionEvent e){
System.out.println(((JButton)(e.getSource())).getName());
if (_drawingShip == null)
{
return;
}
switch(((JButton)(e.getSource())).getName()){
case "up":
_drawingShip.MoveTransport(Direction.Up);
break;
case "down":
_drawingShip.MoveTransport(Direction.Down);
break;
case "left":
_drawingShip.MoveTransport(Direction.Left);
break;
case "right":
_drawingShip.MoveTransport(Direction.Right);
break;
}
Draw();
}
};
up.addActionListener(actioListener);
down.addActionListener(actioListener);
left.addActionListener(actioListener);
right.addActionListener(actioListener);
w.setSize (1000, 600);
w.setLayout(null);
canv = new Canvas();
canv.setBounds(0, 0, 1000, 600);
buttonCreateShip.setBounds(2, 540, 200, 20);
buttonCreateContainerShip.setBounds(215, 540, 200, 20);
buttonSelectedShip.setBounds(550, 540, 150, 20);
comboBoxStrategy.setBounds(800, 10, 150, 30);
buttonStep.setBounds(800, 45, 100, 20);
up.setBounds(900, 480, 45, 45);
down.setBounds(900, 520, 45, 45);
left.setBounds(860, 520, 45, 45);
right.setBounds(940, 520, 45, 45);
w.add(canv);
w.add(buttonCreateShip);
w.add(buttonCreateContainerShip);
w.add(buttonSelectedShip);
w.add(comboBoxStrategy);
w.add(buttonStep);
w.add(up);
w.add(down);
w.add(left);
w.add(right);
w.setVisible (true);
}
}

229
FormShipCollection.java Normal file
View File

@ -0,0 +1,229 @@
import java.awt.*;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.event.*;
import java.util.Stack;
public class FormShipCollection {
class Canvas extends JComponent{
public Canvas(){}
public void paintComponent(Graphics g){
super.paintComponent(g);
if (jListStorage.getSelectedIndex() == -1)
{
return;
}
var obj = _storage.get(jListStorage.getSelectedValue());
if (obj == null)
{
return;
}
if(obj.ShowShips() != null){
g.drawImage(obj.ShowShips(), 0, 0, this);
}
super.repaint();
}
}
Canvas canv;
static int pictureBoxWidth = 820;
static int pictureBoxHeight = 580;
private Stack<DrawingShip> removedShips;
private ShipGenericStorage _storage;
private JList<String> jListStorage;
private DefaultListModel<String> listModel;
private void ReloadObjects()
{
int index = jListStorage.getSelectedIndex();
listModel.clear();
for (String key : _storage.Keys()) {
listModel.addElement(key);
}
if (listModel.size() > 0 && (index == -1 || index >= listModel.size()))
{
jListStorage.setSelectedIndex(0);
}
else if (listModel.size() > 0 && index > -1 && index < listModel.size())
{
jListStorage.setSelectedIndex(index);
}
}
public void Draw(){
canv.repaint();
}
FormShipCollection(){
canv = new Canvas();
JFrame w = new JFrame("FormShipCollection");
_storage = new ShipGenericStorage(pictureBoxWidth, pictureBoxHeight);
listModel = new DefaultListModel<String>();
jListStorage = new JList<String>(listModel);
JButton ButtonAddShip = new JButton("Добавить кораблик");
JButton ButtonDeleteShip = new JButton("Удалить кораблик");
JButton ButtonRefreshCollection = new JButton("Обновить коллекцию");
JButton forNewForm = new JButton("новая форма");
JButton buttonGetRemoved = new JButton("Получить удалённое");
JButton buttonAddSet = new JButton("Добавить коллекцию");
JButton buttonRemoveSet = new JButton("Удалить коллекцию");
JTextField textBoxSetName = new JTextField();
JTextField TextBoxNumber = new JTextField();
ButtonAddShip.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e){
if (jListStorage.getSelectedIndex() == -1)
{
return;
}
var obj = _storage.get(jListStorage.getSelectedValue());
if (obj == null)
{
return;
}
FormShipConfig form = new FormShipConfig();
form.buttonAdd.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
if(obj.Add(form._drawingShip) != -1 && obj != null){
JOptionPane.showMessageDialog(null, "Объект добавлен", "Информация", JOptionPane.INFORMATION_MESSAGE);
Draw();
}
else{
JOptionPane.showMessageDialog(null, "Объект не добавлен", "Информация", JOptionPane.INFORMATION_MESSAGE);
}
form.w.dispose();
}
}
);
}
}
);
forNewForm.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e){
NewFormRand newFormRand = new NewFormRand();
}
}
);
removedShips = new Stack<DrawingShip>();
ButtonDeleteShip.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e){
if (jListStorage.getSelectedIndex() == -1)
{
return;
}
var obj = _storage.get(jListStorage.getSelectedValue());
if (obj == null)
{
return;
}
if (JOptionPane.showConfirmDialog(null, "Удалить объект?", "Удаление", JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION)
{
return;
}
int pos = Integer.parseInt(TextBoxNumber.getText());
var rem = obj.remove(pos);
if (rem != null)
{
removedShips.push(rem);
JOptionPane.showMessageDialog(null, "Объект удален", "Информация", JOptionPane.INFORMATION_MESSAGE);
Draw();
}
else
{
JOptionPane.showMessageDialog(null, "Не удалось удалить объект", "Информация", JOptionPane.INFORMATION_MESSAGE);
}
}
}
);
buttonGetRemoved.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e){
if (removedShips.empty()){
JOptionPane.showMessageDialog(null, "Нет удалённых", "Информация", JOptionPane.INFORMATION_MESSAGE);
return;
}
FormContainerShip form = new FormContainerShip();
form._drawingShip = removedShips.pop();
form._drawingShip.SetPosition(100, 100);
form.Draw();
}
}
);
ButtonRefreshCollection.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e){
Draw();
}
}
);
buttonAddSet.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e){
if (textBoxSetName.getText().length() == 0)
{
JOptionPane.showMessageDialog(null, "Не все данные заполнены", "Информация", JOptionPane.INFORMATION_MESSAGE);
return;
}
_storage.AddSet(textBoxSetName.getText());
ReloadObjects();
}
}
);
jListStorage.addListSelectionListener(
new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e){
Draw();
}
}
);
buttonRemoveSet.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e){
if (jListStorage.getSelectedIndex() == -1)
{
return;
}
if (JOptionPane.showConfirmDialog(null, "Удалить объект " + jListStorage.getSelectedValue() + "?", "Удаление", JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION)
{
return;
}
_storage.DelSet(jListStorage.getSelectedValue());
ReloadObjects();
}
}
);
w.setSize (1000, 600);
w.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
w.setLayout(null);
canv.setBounds(0, 0, pictureBoxWidth, pictureBoxHeight);
ButtonAddShip.setBounds(pictureBoxWidth, 0, 130, 20);
TextBoxNumber.setBounds(pictureBoxWidth, 50, 130, 20);
ButtonDeleteShip.setBounds(pictureBoxWidth, 80, 130, 20);
ButtonRefreshCollection.setBounds(pictureBoxWidth, 130, 120, 20);
forNewForm.setBounds(pictureBoxWidth, 150, 130, 20);
buttonAddSet.setBounds(pictureBoxWidth, 180, 130, 20);
textBoxSetName.setBounds(pictureBoxWidth, 210, 130, 20);
jListStorage.setBounds(pictureBoxWidth, 240, 130, 60);
buttonRemoveSet.setBounds(pictureBoxWidth, 310, 130, 20);
buttonGetRemoved.setBounds(pictureBoxWidth, 340, 130, 20);
w.add(canv);
w.add(ButtonAddShip);
w.add(ButtonDeleteShip);
w.add(ButtonRefreshCollection);
w.add(forNewForm);
w.add(TextBoxNumber);
w.add(buttonAddSet);
w.add(textBoxSetName);
w.add(jListStorage);
w.add(buttonRemoveSet);
w.add(buttonGetRemoved);
w.setVisible(true);
}
}

399
FormShipConfig.java Normal file
View File

@ -0,0 +1,399 @@
import java.awt.*;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.util.*;
import javax.swing.*;
import javax.swing.border.Border;
import java.awt.event.*;
import java.io.IOException;
public class FormShipConfig {
class Canvas extends JComponent{
public Canvas(){
}
public void paintComponent (Graphics g){
if (_drawingShip == null){
return;
}
super.paintComponents (g) ;
Graphics2D g2d = (Graphics2D)g;
_drawingShip.DrawShip(g2d);
super.repaint();
}
}
Canvas canv;
//для перетаскивания типа объекта
private class LabelTransferHandler extends TransferHandler {
@Override
public int getSourceActions(JComponent c) {
return TransferHandler.COPY;
}
@Override
protected Transferable createTransferable(JComponent c) {
return new StringSelection(((JLabel)c).getText());
}
}
//что бы цвет можно было таскать
private class ColorTransferable implements Transferable {
private Color color;
private static final DataFlavor colorDataFlavor = new DataFlavor(Color.class, "Color");
public ColorTransferable(Color color) {
this.color = color;
}
@Override
public DataFlavor[] getTransferDataFlavors() {
return new DataFlavor[]{colorDataFlavor};
}
@Override
public boolean isDataFlavorSupported(DataFlavor flavor) {
return colorDataFlavor.equals(flavor);
}
@Override
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
if (isDataFlavorSupported(flavor)) {
return color;
} else {
throw new UnsupportedFlavorException(flavor);
}
}
}
//для перетаскивания цвета
private class PanelTransferHandler extends TransferHandler {
@Override
public int getSourceActions(JComponent c) {
return TransferHandler.COPY;
}
@Override
protected Transferable createTransferable(JComponent c) {
return new ColorTransferable(((JPanel)c).getBackground());
}
}
//для обработки нажатий
private class LabelMouseAdapter extends MouseAdapter{
@Override
public void mousePressed(MouseEvent e) {
((JLabel)e.getComponent()).getTransferHandler().exportAsDrag(((JLabel)e.getComponent()), e, TransferHandler.COPY);
}
}
private class PanelMouseAdapter extends MouseAdapter{
@Override
public void mousePressed(MouseEvent e) {
((JPanel)e.getComponent()).getTransferHandler().exportAsDrag(((JPanel)e.getComponent()), e, TransferHandler.COPY);
}
}
//что бы палубы можно было таскать
private class DeckTransferable implements Transferable {
private IDecksDrawing decksDrawing;
private static final DataFlavor deckDrawingDataFlavor = new DataFlavor(IDecksDrawing.class, "Отрисовка палуб");
public DeckTransferable(IDecksDrawing decksDrawing) {
this.decksDrawing = decksDrawing;
}
@Override
public DataFlavor[] getTransferDataFlavors() {
return new DataFlavor[]{deckDrawingDataFlavor};
}
@Override
public boolean isDataFlavorSupported(DataFlavor flavor) {
return flavor.equals(deckDrawingDataFlavor);
}
@Override
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
if (isDataFlavorSupported(flavor)) {
return decksDrawing;
} else {
throw new UnsupportedFlavorException(flavor);
}
}
}
//для отрисовки палуб, которые можно взять и потащить
private class ComponentDeck extends JComponent{
public IDecksDrawing decksDrawing;
public ComponentDeck(IDecksDrawing _decksDrawing){
decksDrawing = _decksDrawing;
decksDrawing.setNumDecks(1);
this.addMouseListener(
new MouseAdapter(){
@Override
public void mousePressed(MouseEvent e) {
((ComponentDeck)e.getComponent()).getTransferHandler().exportAsDrag(((ComponentDeck)e.getComponent()), e, TransferHandler.COPY);
}
}
);
this.setTransferHandler(
new TransferHandler(){
@Override
public int getSourceActions(JComponent c) {
return TransferHandler.COPY;
}
@Override
protected Transferable createTransferable(JComponent c) {
return new DeckTransferable(((ComponentDeck)c).decksDrawing);
}
}
);
}
public void paintComponent (Graphics g){
super.paintComponents (g) ;
Graphics2D g2d = (Graphics2D)g;
decksDrawing.DrawDeck(-20,-64,Color.BLACK,g2d);
super.repaint();
}
}
final int WindowHeight = 700;
final int WindowWidth = 800;
final int CanvasHeight = 600;
final int CanvasWidth = 600;
public DrawingShip _drawingShip = null;
public JButton buttonAdd;
public JFrame w;
public FormShipConfig(){
canv = new Canvas();
Border border = BorderFactory.createLineBorder(Color.GRAY);
JLabel labelSpeed = new JLabel("скорость");
JLabel labelWeight = new JLabel("вес");
JLabel labelWheelNum = new JLabel("палубы");
SpinnerModel spinnerModel = new SpinnerNumberModel(100, 100, 1000, 1);
JSpinner numericSpeed = new JSpinner(spinnerModel);
SpinnerModel spinnerModel2 = new SpinnerNumberModel(100, 100, 1000, 1);
JSpinner numericWeight = new JSpinner(spinnerModel2);
SpinnerModel spinnerModel3 = new SpinnerNumberModel(1, 1, 3, 1);
JSpinner numericDeckNum = new JSpinner(spinnerModel3);
JCheckBox checkBoxContainers = new JCheckBox("контейнеры");
JCheckBox checkBoxCrane = new JCheckBox("кран");
JPanel[] colorPanels = {
new JPanel(),new JPanel(),new JPanel(),new JPanel(),
new JPanel(),new JPanel(),new JPanel(),new JPanel(),
};
colorPanels[0].setBackground(Color.CYAN);
colorPanels[1].setBackground(Color.BLUE);
colorPanels[2].setBackground(Color.PINK);
colorPanels[3].setBackground(Color.YELLOW);
colorPanels[4].setBackground(Color.RED);
colorPanels[5].setBackground(Color.GREEN);
colorPanels[6].setBackground(Color.ORANGE);
colorPanels[7].setBackground(Color.MAGENTA);
for (var it : colorPanels){
it.setTransferHandler(new PanelTransferHandler());
it.addMouseListener(new PanelMouseAdapter());
}
JLabel labelShip = new JLabel("кораблик");
labelShip.setTransferHandler(new LabelTransferHandler());
labelShip.addMouseListener(new LabelMouseAdapter());
labelShip.setBorder(border);
labelShip.setHorizontalAlignment(SwingConstants.CENTER);
labelShip.setVerticalAlignment(SwingConstants.CENTER);
JLabel labelContainerShip = new JLabel("контейнеровоз");
labelContainerShip.setTransferHandler(new LabelTransferHandler());
labelContainerShip.addMouseListener(new LabelMouseAdapter());
labelContainerShip.setBorder(border);
labelContainerShip.setHorizontalAlignment(SwingConstants.CENTER);
labelContainerShip.setVerticalAlignment(SwingConstants.CENTER);
JLabel labelColor = new JLabel("цвет");
labelColor.setBorder(border);
labelColor.setHorizontalAlignment(SwingConstants.CENTER);
labelColor.setVerticalAlignment(SwingConstants.CENTER);
labelColor.setTransferHandler(
new TransferHandler(){
@Override
public boolean canImport(TransferHandler.TransferSupport support) {
return support.isDataFlavorSupported(ColorTransferable.colorDataFlavor);
}
@Override
public boolean importData(TransferHandler.TransferSupport support) {
if (canImport(support)) {
try {
Color color = (Color) support.getTransferable().getTransferData(ColorTransferable.colorDataFlavor);
if (_drawingShip == null)
return false;
_drawingShip.EntityShip.setBodyColor(color);
canv.repaint();
return true;
} catch (UnsupportedFlavorException | IOException e) {
e.printStackTrace();
}
}
return false;
}
}
);
JLabel labelAdditionalColor = new JLabel("доп цвет");
labelAdditionalColor.setBorder(border);
labelAdditionalColor.setHorizontalAlignment(SwingConstants.CENTER);
labelAdditionalColor.setVerticalAlignment(SwingConstants.CENTER);
labelAdditionalColor.setTransferHandler(
new TransferHandler(){
@Override
public boolean canImport(TransferHandler.TransferSupport support) {
return support.isDataFlavorSupported(ColorTransferable.colorDataFlavor);
}
@Override
public boolean importData(TransferHandler.TransferSupport support) {
if (canImport(support)) {
try {
Color color = (Color) support.getTransferable().getTransferData(ColorTransferable.colorDataFlavor);
if (_drawingShip == null)
return false;
if (!(_drawingShip instanceof DrawingContainerShip))
return false;
((EntityContainerShip)_drawingShip.EntityShip).setAdditionalColor(color);
return true;
} catch (UnsupportedFlavorException | IOException e) {
e.printStackTrace();
}
}
return false;
}
}
);
JLabel labelDeck = new JLabel("палубы");
labelDeck.setBorder(border);
labelDeck.setHorizontalAlignment(SwingConstants.CENTER);
labelDeck.setVerticalAlignment(SwingConstants.CENTER);
labelDeck.setTransferHandler(
new TransferHandler(){
@Override
public boolean canImport(TransferHandler.TransferSupport support) {
return support.isDataFlavorSupported(DeckTransferable.deckDrawingDataFlavor);
}
@Override
public boolean importData(TransferHandler.TransferSupport support) {
if (canImport(support)) {
try {
IDecksDrawing decksDrawing = (IDecksDrawing)support.getTransferable().getTransferData(DeckTransferable.deckDrawingDataFlavor).getClass().getDeclaredConstructor().newInstance();
if (_drawingShip == null)
return false;
if(_drawingShip instanceof DrawingContainerShip){
decksDrawing.setNumDecks(((EntityContainerShip) _drawingShip.EntityShip).Deck);
((DrawingContainerShip)_drawingShip).iDecksDrawing = decksDrawing;
}
canv.repaint();
} catch (Exception e) {
e.printStackTrace();
}
}
return false;
}
}
);
canv.setTransferHandler(
new TransferHandler(){
@Override
public boolean canImport(TransferHandler.TransferSupport support) {
return support.isDataFlavorSupported(DataFlavor.stringFlavor);
}
@Override
public boolean importData(TransferHandler.TransferSupport support) {
if (canImport(support)) {
try {
String data = (String) support.getTransferable().getTransferData(DataFlavor.stringFlavor);
switch (data) {
case "кораблик":
_drawingShip = new DrawingShip((int)numericSpeed.getValue(),(int)numericWeight.getValue(), Color.WHITE,CanvasWidth,CanvasHeight);
break;
case "контейнеровоз":
_drawingShip = new DrawingContainerShip((int)numericSpeed.getValue(), (int)numericWeight.getValue(), Color.WHITE, Color.BLACK,checkBoxCrane.isSelected(), checkBoxContainers.isSelected(),(int)numericDeckNum.getValue(), 1, CanvasWidth,CanvasHeight);
break;
}
canv.repaint();
return true;
} catch (UnsupportedFlavorException | IOException e) {
e.printStackTrace();
}
}
return false;
}
}
);
buttonAdd = new JButton("добавить");
JButton buttonCancel = new JButton("отмена");
buttonCancel.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e){
w.dispose();
}
}
);
ComponentDeck componentSimple = new ComponentDeck(new DrawingDecks());
ComponentDeck componentRect = new ComponentDeck(new DrawingDecksRect());
ComponentDeck componentTrapez = new ComponentDeck(new DrawingDecksTrapez());
labelSpeed.setBounds(10,10,40,20);
labelWeight.setBounds(10,40,40,20);
labelWheelNum.setBounds(10,70,40,20);
numericSpeed.setBounds(55,10,80,20);
numericWeight.setBounds(55,40,80,20);
numericDeckNum.setBounds(55,70,80,20);
checkBoxContainers.setBounds(10,100,120,20);
checkBoxCrane.setBounds(10,130,120,20);
for (int i = 0; i < colorPanels.length; i+=2){
colorPanels[i].setBounds(10,200+i/2*60,50,50);
colorPanels[i+1].setBounds(70,200+i/2*60,50,50);
}
componentSimple.setBounds(10,470,110,20);
componentRect.setBounds(10,500,110,20);
componentTrapez.setBounds(10,530,110,20);
labelShip.setBounds(10,560 ,50,30);
labelContainerShip.setBounds(70,560 ,50,30);
labelColor.setBounds(WindowWidth-CanvasWidth, 10, CanvasWidth/3, 30);
labelAdditionalColor.setBounds(WindowWidth-CanvasWidth + CanvasWidth/3, 10, CanvasWidth/3, 30);
labelDeck.setBounds(WindowWidth-CanvasWidth + CanvasWidth*2/3, 10, CanvasWidth/3, 30);
canv.setBounds(WindowWidth-CanvasWidth, 50, CanvasWidth, CanvasHeight);
buttonAdd.setBounds(WindowWidth-CanvasWidth, CanvasHeight+60, CanvasWidth/3, 30);
buttonCancel.setBounds(WindowWidth-CanvasWidth + CanvasWidth*2/3, CanvasHeight+60, CanvasWidth/3, 30);
w = new JFrame();
w.setSize (WindowWidth+20, WindowHeight+40);
w.setLayout(null);
w.add(labelSpeed);
w.add(labelWeight);
w.add(labelWheelNum);
w.add(numericSpeed);
w.add(numericWeight);
w.add(numericDeckNum);
w.add(checkBoxContainers);
w.add(checkBoxCrane);
for (var it : colorPanels)
w.add(it);
w.add(labelShip);
w.add(labelContainerShip);
w.add(labelColor);
w.add(labelAdditionalColor);
w.add(labelDeck);
w.add(canv);
w.add(buttonAdd);
w.add(buttonCancel);
w.add(componentSimple);
w.add(componentRect);
w.add(componentTrapez);
w.setVisible(true);
}
}

6
IDecksDrawing.java Normal file
View File

@ -0,0 +1,6 @@
import java.awt.*;
public interface IDecksDrawing {
public NumberOfDecks getProperty();
public void setNumDecks(int nDecks);
public void DrawDeck(int _startPosX, int _startPosY,Color bodyColor, Graphics2D g);
}

18
IMoveableObject.java Normal file
View File

@ -0,0 +1,18 @@
public interface IMoveableObject {
ObjectParameters GetObjectPosition();
/// <summary>
/// Шаг объекта
/// </summary>
int GetStep();
/// <summary>
/// Проверка, можно ли переместиться по нужному направлению
/// </summary>
/// <param name="direction"></param>
/// <returns></returns>
boolean CheckCanMove(Direction direction);
/// <summary>
/// Изменение направления пермещения объекта
/// </summary>
/// <param name="direction">Направление</param>
void MoveObject(Direction direction);
}

5
Main.java Normal file
View File

@ -0,0 +1,5 @@
public class Main {
public static void main(String[] args){
FormShipCollection formShipCollection = new FormShipCollection();
}
}

46
MoveToBorder.java Normal file
View File

@ -0,0 +1,46 @@
public class MoveToBorder extends AbstractStrategy{
protected boolean IsTargetDestinaion()
{
var objParams = GetObjectParameters();
if (objParams == null)
{
return false;
}
return objParams.RightBorder <= FieldWidth &&
objParams.RightBorder + GetStep() >= FieldWidth &&
objParams.DownBorder <= FieldHeight &&
objParams.DownBorder + GetStep() >= FieldHeight;
}
protected void MoveToTarget()
{
var objParams = GetObjectParameters();
if (objParams == null)
{
return;
}
var diffX = objParams.RightBorder - FieldWidth;
if (Math.abs(diffX) > GetStep())
{
if (diffX > 0)
{
MoveLeft();
}
else
{
MoveRight();
}
}
var diffY = objParams.DownBorder - FieldHeight;
if (Math.abs(diffY) > GetStep())
{
if (diffY > 0)
{
MoveUp();
}
else
{
MoveDown();
}
}
}
}

47
MoveToCenter.java Normal file
View File

@ -0,0 +1,47 @@
public class MoveToCenter extends AbstractStrategy{
protected boolean IsTargetDestinaion()
{
var objParams = GetObjectParameters();
if (objParams == null)
{
return false;
}
return objParams.ObjectMiddleHorizontal <= FieldWidth / 2 &&
objParams.ObjectMiddleHorizontal + GetStep() >= FieldWidth / 2 &&
objParams.ObjectMiddleVertical <= FieldHeight / 2 &&
objParams.ObjectMiddleVertical + GetStep() >= FieldHeight / 2;
}
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();
}
}
}
}

53
NewFormRand.java Normal file
View File

@ -0,0 +1,53 @@
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class NewFormRand extends JFrame {
static int pictureBoxWidth = 960;
static int pictureBoxHeight = 560;
public DrawingShip _drawingShip;
private class Canvas extends JComponent{
public Canvas(){
}
public void paintComponent (Graphics g){
if (_drawingShip == null){
return;
}
super.paintComponents (g) ;
Graphics2D g2d = (Graphics2D)g;
_drawingShip.SetPosition(100, 100);
_drawingShip.DrawShip(g2d);
super.repaint();
}
}
ShipGenericDop<EntityShip, IDecksDrawing> _ShipGenericDop;
public NewFormRand(){
_drawingShip = null;
Canvas canv = new Canvas();
setSize(1000, 600);
setLayout(null);
_ShipGenericDop = new ShipGenericDop<>(50,50,pictureBoxWidth,pictureBoxHeight);
canv.setBounds(0, 0, pictureBoxWidth, pictureBoxHeight);
_ShipGenericDop.Add(new EntityShip(200,200,Color.PINK));
_ShipGenericDop.Add(new EntityShip(200,200,Color.GRAY));
_ShipGenericDop.Add(new EntityContainerShip(200, 200, Color.GREEN, Color.BLUE, true, false, 2, 1));
_ShipGenericDop.Add(new EntityContainerShip(200, 200, Color.LIGHT_GRAY, Color.MAGENTA, false, true, 3, 3));
_ShipGenericDop.Add(new DrawingDecks());
_ShipGenericDop.Add(new DrawingDecksRect());
_ShipGenericDop.Add(new DrawingDecksTrapez());
JButton creatButton = new JButton("создать");
creatButton.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e){
_drawingShip = _ShipGenericDop.getObjectRandomShip();
canv.repaint();
}
}
);
creatButton.setBounds(820, 490, 120, 20);
add(canv);
add(creatButton);
setVisible(true);
}
}

3
NumberOfDecks.java Normal file
View File

@ -0,0 +1,3 @@
public enum NumberOfDecks {
Deck_1, Deck_2, Deck_3;
}

32
ObjectParameters.java Normal file
View File

@ -0,0 +1,32 @@
public class ObjectParameters {
private int _x;
private int _y;
private int _width;
private 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;
}
}

111
SetGeneric.java Normal file
View File

@ -0,0 +1,111 @@
import java.util.ArrayList;
import java.util.Iterator;
import java.util.NoSuchElementException;
public class SetGeneric<T extends Object>
{
private ArrayList<T> _places;
public int Count() {return _places.size();};
public int _maxCount;
public SetGeneric(int count)
{
_maxCount = count;
_places = new ArrayList<T>(count);
}
public int Insert(T ship)
{
if(_places.size() >= _maxCount)
{
return -1;
}
else
{
_places.add(0, ship);
return 0;
}
}
public boolean Insert(T ship, int position)
{
if(_places.size() >= _maxCount)
{
return false;
}
if(position < 0 || position > _places.size())
{
return false;
}
if(position == _places.size())
{
_places.add(ship);
}
else
{
_places.add(position, ship);
}
return true;
}
public boolean Remove(int position)
{
if(position < _places.size() && _places.size() < _maxCount)
{
_places.remove(position);
return true;
}
else
{
return false;
}
}
public T Get(int position)
{
if(position < _places.size() && position >= 0)
{
return _places.get(position);
}
else
{
return null;
}
}
public Iterable<T> GetShips(final Integer maxShips) {
return new Iterable<T>() {
@Override
public Iterator<T> iterator() {
return new Iterator<T>() {
private int currentIndex = 0;
private int count = 0;
@Override
public boolean hasNext() {
return currentIndex < _places.size() && (maxShips == null || count < maxShips);
}
@Override
public T next() {
if (hasNext()) {
count++;
return _places.get(currentIndex++);
}
throw new NoSuchElementException();
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
};
}
};
}
}

View File

@ -0,0 +1,88 @@
import java.awt.image.BufferedImage;
import java.awt.Color;
import java.awt.Graphics2D;
public class ShipGenericCollection<T extends DrawingShip, U extends IMoveableObject>
{
private int _pictureWidth;
private int _pictureHeight;
private int _placeSizeWidth = 210;
private int _placeSizeHeight = 90;
private SetGeneric<T> _collection;
public ShipGenericCollection(int picWidth, int picHeight)
{
int width = picWidth / _placeSizeWidth;
int height = picHeight / _placeSizeHeight;
_pictureWidth = picWidth;
_pictureHeight = picHeight;
_collection = new SetGeneric<T>(width * height);
}
public int Add(T obj)
{
if (obj == null)
{
return -1;
}
return _collection.Insert(obj);
}
public T remove(int pos)
{
T obj = _collection.Get(pos);
if (obj != null)
{
_collection.Remove(pos);
}
return obj;
}
public U GetU(int pos)
{
return (U)_collection.Get(pos).GetMoveableObject();
}
public BufferedImage ShowShips()
{
BufferedImage bmp = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_ARGB);
Graphics2D gr = bmp.createGraphics();
DrawBackground(gr);
DrawObjects(gr);
gr.dispose();
return bmp;
}
private void DrawBackground(Graphics2D gr)
{
gr.setColor(Color.BLACK);
for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++)
{
for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j)
{//линия рамзетки места
gr.drawLine(i * _placeSizeWidth, j * _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight);
}
gr.drawLine(i * _placeSizeWidth, 0, i * _placeSizeWidth, _pictureHeight / _placeSizeHeight * _placeSizeHeight);
}
}
private void DrawObjects(Graphics2D gr)
{
int i = 0;
for (T ship: _collection.GetShips(100))
{
if (ship != null)
{
ship.SetPosition(((_collection._maxCount -i - 1) % (_pictureWidth / _placeSizeWidth)) * _placeSizeWidth, (_collection._maxCount - i-1) / (_pictureWidth / _placeSizeWidth) * _placeSizeHeight);
ship.DrawShip(gr);
}
i++;
}
}
}

57
ShipGenericDop.java Normal file
View File

@ -0,0 +1,57 @@
import java.util.Random;
import java.util.ArrayList;
public class ShipGenericDop<T extends EntityShip, U extends IDecksDrawing> {
private ArrayList<T> Ships;
private ArrayList<U> Decks;
private int countShips;
private int max_countShips;
private int countDecks;
private int max_countDecks;
private Random rand;
private int _pictureWidth;
private int _pictureHeight;
public ShipGenericDop(int _max_countShips, int _max_countDecks, int pictureWidth, int pictureHeight){
max_countShips = _max_countShips;
max_countDecks = _max_countDecks;
Ships = new ArrayList<T>(max_countShips);
Decks = new ArrayList<U>(max_countDecks);
countShips = 0;
countDecks = 0;
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
rand = new Random();
}
public boolean Add(T ship){
if(ship == null){
return false;
}
if (countShips > max_countShips){
return false;
}
Ships.add(countShips++, ship);
return true;
}
public boolean Add(U deck){
if(deck == null){
return false;
}
if (countDecks > max_countDecks){
return false;
}
Decks.add(countDecks++, deck);
return true;
}
public DrawingShip getObjectRandomShip(){
if(countShips == 0 || countDecks == 0){
return null;
}
int i = rand.nextInt(countShips);
int j = rand.nextInt(countDecks);
if(Ships.get(i) instanceof EntityContainerShip){
return new DrawingContainerShip((EntityContainerShip)Ships.get(i), (IDecksDrawing)Decks.get(j), _pictureWidth, _pictureHeight);
}
else{
return new DrawingShip((EntityShip)Ships.get(i),_pictureWidth, _pictureHeight);
}
}
}

86
ShipGenericStorage.java Normal file
View File

@ -0,0 +1,86 @@
import java.util.Dictionary;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;
public class ShipGenericStorage {
HashMap<String, ShipGenericCollection<DrawingShip,
DrawningObjectShip>> _shipStorages;
/// <summary>
/// Возвращение списка названий наборов
/// </summary>
public List<String> Keys() {return _shipStorages.keySet().stream().collect(Collectors.toList());}
/// <summary>
/// Ширина окна отрисовки
/// </summary>
private int _pictureWidth;
/// <summary>
/// Высота окна отрисовки
/// </summary>
private int _pictureHeight;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="pictureWidth"></param>
/// <param name="pictureHeight"></param>
public ShipGenericStorage(int pictureWidth, int pictureHeight)
{
_shipStorages = new HashMap<String, ShipGenericCollection<DrawingShip,DrawningObjectShip>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
/// <summary>
/// Добавление набора
/// </summary>
/// <param name="name">Название набора</param>
public void AddSet(String name)
{
if (_shipStorages.containsKey(name))
{
return;
}
else
{
_shipStorages.put(name, new ShipGenericCollection<DrawingShip, DrawningObjectShip>(_pictureWidth, _pictureHeight));
}
}
/// <summary>
/// Удаление набора
/// </summary>
/// <param name="name">Название набора</param>
public void DelSet(String name)
{
if (!_shipStorages.containsKey(name))
{
return;
}
else
{
_shipStorages.remove(name);
}
}
/// <summary>
/// Доступ к набору
/// </summary>
/// <param name="ind"></param>
/// <returns></returns>
public ShipGenericCollection<DrawingShip, DrawningObjectShip> get(String ind)
{
if (_shipStorages.containsKey(ind))
{
return _shipStorages.get(ind);
}
else
{
return null;
}
}
public DrawningObjectShip get(String ind1, int ind2){
if (!_shipStorages.containsKey(ind1))
return null;
return _shipStorages.get(ind1).GetU(ind2);
}
}

5
Status.java Normal file
View File

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

BIN
photo11.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.1 KiB

BIN
photo22.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.3 KiB

BIN
photo33.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.2 KiB

BIN
photo44.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.3 KiB