готова к сдаче

This commit is contained in:
MorozovDanil 2024-06-05 02:58:10 +04:00
parent 1ba1e154a3
commit 92c6fd868b
9 changed files with 266 additions and 320 deletions

View File

@ -1,4 +1,4 @@
package ProjectContainerShip.src;
package ProjectContainerShip.src.Drawings;
public enum DeckCount {
One(1),

View File

@ -1,4 +1,4 @@
package ProjectContainerShip.src;
package ProjectContainerShip.src.Drawings;
public enum DirectionType {
Up,

View File

@ -0,0 +1,88 @@
package ProjectContainerShip.src.Drawings;
import ProjectContainerShip.src.Entities.EntityContainerShip;
import java.awt.*;
public class DrawningContainerShip extends DrawningShip {
EntityContainerShip entityContainerShip;
public DrawningContainerShip(int speed, double weight, Color bodyColor, int deckType, Color additionalColor, boolean crane, boolean container) {
super(speed, weight, bodyColor, deckType, 130, 60);
entityContainerShip = new EntityContainerShip(speed, weight, bodyColor, additionalColor, crane, container);
}
// тут рисовашки
@Override
public void DrawShip(Graphics g) {
if (entityContainerShip == null || _startPosX == null || _startPosY == null) {
return;
}
super.DrawShip(g);
Graphics2D g2d = (Graphics2D) g;
if (entityContainerShip.getCrane()) {
drawCrane( entityContainerShip.getAdditionalColor(), g2d);
}
if (entityContainerShip.getContainer()) {
drawContainer( entityContainerShip.getAdditionalColor(), g2d);
}
}
private void drawCrane(Color additionalColor, Graphics g2d)
{
g2d.setColor(additionalColor);
Point[] crane = new Point[]
{
new Point(_startPosX + 75, _startPosY + 50),
new Point(_startPosX + 85, _startPosY+ 50),
new Point(_startPosX + 85, _startPosY + 20),
new Point(_startPosX + 105, _startPosY + 20),
new Point(_startPosX + 105, _startPosY + 15),
new Point(_startPosX + 75, _startPosY + 15),
};
Polygon cranePolygon = new Polygon();
for (Point point: crane) {
cranePolygon.addPoint(point.x, point.y);
}
g2d.fillPolygon(cranePolygon);
g2d.drawPolygon(cranePolygon);
}
private void drawContainer(Color additionalColor, Graphics g2d)
{
g2d.setColor(Color.BLACK);
Point[] container1 = new Point[] {
new Point(_startPosX + 90, _startPosY + 50),
new Point(_startPosX + 120, _startPosY + 50),
new Point(_startPosX + 120, _startPosY + 40),
new Point(_startPosX + 90, _startPosY + 40)
};
Polygon container1Polygon = new Polygon();
for(Point point: container1) {
container1Polygon.addPoint(point.x, point.y);
}
Point[] container2 = new Point[] {
new Point(_startPosX + 90, _startPosY + 40),
new Point(_startPosX + 90, _startPosY + 30),
new Point(_startPosX + 120, _startPosY + 30),
new Point(_startPosX + 120, _startPosY + 40)
};
Polygon container2Polygon = new Polygon();
for(Point point: container1) {
container2Polygon.addPoint(point.x, point.y);
}
g2d.drawPolygon(container1Polygon);
g2d.drawPolygon(container2Polygon);
g2d.setColor(entityContainerShip.getAdditionalColor());
g2d.fillPolygon(container1Polygon);
g2d.fillPolygon(container2Polygon);
}
}

View File

@ -1,7 +1,7 @@
package ProjectContainerShip.src;
package ProjectContainerShip.src.Drawings;
import java.awt.*;
public class DrawningContainerShipDeck {
public class DrawningContainerShipDeck implements IDrawningDeck {
private DeckCount _deckCount;
public void setEnumNumber(int deckCount) {
@ -13,7 +13,7 @@ public class DrawningContainerShipDeck {
}
}
public void drawContainerShipDeck(Graphics g, Color color, float startPosX, float startPosY)
public void drawContainerShipDeck(Graphics g, Color color, int startPosX, int startPosY)
{
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(color);

View File

@ -1,188 +0,0 @@
package ProjectContainerShip.src;
import java.awt.*;
import java.util.Random;
public class DrawningContainerShip {
private EntityContainerShip entityContainerShip;
public EntityContainerShip getEntityContainerShip() {
return entityContainerShip;
}
private Integer _pictureWidth;
private Integer _pictureHeight;
private Integer _startPosX;
private Integer _startPosY;
private final int _drawingContainerShipWidth = 130;
private final int _drawingContainerShipHeight = 60;
public DrawningContainerShipDeck _drawingContainerShipDeck;
public void Init(int speed, float weight, Color bodyColor, Color additionalColor, boolean crane, boolean container) {
entityContainerShip = new EntityContainerShip();
entityContainerShip.Init(speed, weight, bodyColor, additionalColor, crane, container);
_startPosY = null;
_startPosX = null;
_pictureWidth = null;
_pictureHeight = null;
_drawingContainerShipDeck = new DrawningContainerShipDeck();
Random random = new Random();
int deckCount = random.nextInt(1,4);
_drawingContainerShipDeck.setEnumNumber(deckCount);
}
public void setPosition(int x, int y) {
if (_pictureHeight == null || _pictureWidth == null)
return;
_startPosX = x;
_startPosY = y;
if (_drawingContainerShipWidth + x > _pictureWidth || x < 0) {
_startPosX = 0;
}
if (_drawingContainerShipHeight + y > _pictureHeight || y < 0) {
_startPosY = 0;
}
}
public boolean setPictureSize(int width, int height) {
if (_drawingContainerShipHeight > height || _drawingContainerShipWidth > width)
return false;
_pictureHeight = height;
_pictureWidth = width;
if (_startPosX != null && _startPosY != null)
{
if (_startPosX + _drawingContainerShipWidth > width)
_startPosX = width - _drawingContainerShipWidth;
if (_startPosY + _drawingContainerShipHeight > height)
_startPosY = height - _drawingContainerShipHeight;
}
return true;
}
public boolean moveTransport(DirectionType direction) {
if (entityContainerShip == null || _pictureWidth == null || _pictureHeight == null)
return false;
switch (direction) {
case Left:
if (_startPosX - entityContainerShip.Step() > 0)
_startPosX -= (int) entityContainerShip.Step();
return true;
case Up:
if (_startPosY - entityContainerShip.Step() > 0)
_startPosY -= (int) entityContainerShip.Step();
return true;
case Right:
if (_startPosX + entityContainerShip.Step() < _pictureWidth - _drawingContainerShipWidth)
_startPosX += (int) entityContainerShip.Step();
return true;
case Down:
if (_startPosY + entityContainerShip.Step() < _pictureHeight - _drawingContainerShipHeight)
_startPosY += (int) entityContainerShip.Step();
return true;
default:
return false;
}
}
// тут рисовашки
private void drawCrane(Color additionalColor, Graphics g2d)
{
g2d.setColor(additionalColor);
Point[] crane = new Point[]
{
new Point(_startPosX + 75, _startPosY + 50),
new Point(_startPosX + 85, _startPosY+ 50),
new Point(_startPosX + 85, _startPosY + 20),
new Point(_startPosX + 105, _startPosY + 20),
new Point(_startPosX + 105, _startPosY + 15),
new Point(_startPosX + 75, _startPosY + 15),
};
Polygon cranePolygon = new Polygon();
for (Point point: crane) {
cranePolygon.addPoint(point.x, point.y);
}
g2d.fillPolygon(cranePolygon);
g2d.drawPolygon(cranePolygon);
}
private void drawContainer(Color additionalColor, Graphics g2d)
{
g2d.setColor(Color.BLACK);
Point[] container1 = new Point[] {
new Point(_startPosX + 90, _startPosY + 50),
new Point(_startPosX + 120, _startPosY + 50),
new Point(_startPosX + 120, _startPosY + 40),
new Point(_startPosX + 90, _startPosY + 40)
};
Polygon container1Polygon = new Polygon();
for(Point point: container1) {
container1Polygon.addPoint(point.x, point.y);
}
Point[] container2 = new Point[] {
new Point(_startPosX + 90, _startPosY + 40),
new Point(_startPosX + 90, _startPosY + 30),
new Point(_startPosX + 120, _startPosY + 30),
new Point(_startPosX + 120, _startPosY + 40)
};
Polygon container2Polygon = new Polygon();
for(Point point: container1) {
container2Polygon.addPoint(point.x, point.y);
}
g2d.drawPolygon(container1Polygon);
g2d.drawPolygon(container2Polygon);
g2d.setColor(entityContainerShip.getAdditionalColor());
g2d.fillPolygon(container1Polygon);
g2d.fillPolygon(container2Polygon);
}
public void drawContainerShip(Graphics g) {
if (entityContainerShip == null || _startPosX == null || _startPosY == null) {
return;
}
Graphics2D g2d = (Graphics2D) g;
Point[] containerShipBorders = new Point[] {
new Point(_startPosX, _startPosY + 50),
new Point(_startPosX + 20, _startPosY + 70),
new Point(_startPosX + 110, _startPosY + 70),
new Point(_startPosX + 130, _startPosY + 50)
};
Polygon containerShipPolygon = new Polygon();
for(Point point : containerShipBorders)
containerShipPolygon.addPoint(point.x, point.y);
g2d.setColor(Color.BLACK);
g2d.draw(containerShipPolygon);
g2d.setColor(entityContainerShip.getBodyColor());
g2d.fill(containerShipPolygon);
if (entityContainerShip.getCrane()) {
drawCrane( entityContainerShip.getAdditionalColor(), g2d);
}
if (entityContainerShip.getContainer()) {
drawContainer( entityContainerShip.getAdditionalColor(), g2d);
}
_drawingContainerShipDeck.drawContainerShipDeck(g, entityContainerShip.getBodyColor(), _startPosX, _startPosY);
}
}

View File

@ -0,0 +1,32 @@
package ProjectContainerShip.src.Entities;
import java.awt.*;
public class EntityContainerShip extends EntityShip {
// доп цвет
private Color AdditionalColor;
public Color getAdditionalColor() {
return AdditionalColor;
}
// доп опция кран
private boolean Crane;
public boolean getCrane() {
return Crane;
}
// доп опция контейнер
private boolean Container;
public boolean getContainer() {
return Container;
}
// инициализация полей обьекта
public EntityContainerShip(int speed, double weight, Color bodyColor, Color additionalColor, boolean crane, boolean container) {
super(speed, weight, bodyColor);
AdditionalColor = additionalColor;
Crane = crane;
Container = container;
}
}

View File

@ -1,55 +0,0 @@
package ProjectContainerShip.src;
import java.awt.*;
public class EntityContainerShip {
// скорость
private int Speed;
public int getSpeed() {
return Speed;
}
// вес
private double Weight;
public double getWeight() {
return Weight;
}
// основной цвет
private Color BodyColor;
public Color getBodyColor() {
return BodyColor;
}
// доп цвет
private Color AdditionalColor;
public Color getAdditionalColor() {
return AdditionalColor;
}
// шаг
public double Step() {
return Speed*100/Weight;
}
// доп опция кран
private boolean Crane;
public boolean getCrane() {
return Crane;
}
// доп опция контейнер
private boolean Container;
public boolean getContainer() {
return Container;
}
// инициализация полей обьекта
public void Init(int speed, double weight, Color bodyColor, Color additionalColor, boolean crane, boolean container) {
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
AdditionalColor = additionalColor;
Crane = crane;
Container = container;
}
}

View File

@ -3,12 +3,12 @@
<grid id="27dc6" binding="PanelWrapper" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="500" height="400"/>
<xy x="20" y="20" width="960" height="545"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<grid id="34630" binding="PictureBox" layout-manager="GridLayoutManager" row-count="3" column-count="5" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="34630" binding="PictureBox" layout-manager="GridLayoutManager" row-count="5" column-count="6" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
@ -16,22 +16,9 @@
<properties/>
<border type="none"/>
<children>
<component id="b0a96" class="javax.swing.JButton" binding="buttonCreate">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="2" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Создать"/>
</properties>
</component>
<vspacer id="78215">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
<component id="31c3f" class="javax.swing.JButton" binding="buttonDown">
<constraints>
<grid row="2" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false">
<grid row="4" column="4" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
@ -44,12 +31,12 @@
</component>
<hspacer id="b5bb2">
<constraints>
<grid row="2" column="1" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
<grid row="4" column="2" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<component id="6deee" class="javax.swing.JButton" binding="buttonRight">
<constraints>
<grid row="2" column="4" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false">
<grid row="4" column="5" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
@ -62,7 +49,7 @@
</component>
<component id="f5ad2" class="javax.swing.JButton" binding="buttonLeft">
<constraints>
<grid row="2" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false">
<grid row="4" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
@ -75,7 +62,7 @@
</component>
<component id="e26e8" class="javax.swing.JButton" binding="buttonUp">
<constraints>
<grid row="1" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false">
<grid row="3" column="4" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
@ -86,6 +73,44 @@
<text value=""/>
</properties>
</component>
<component id="b4b3c" class="javax.swing.JButton" binding="buttonStrategyStep">
<constraints>
<grid row="1" column="4" row-span="1" col-span="2" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<label value="Шаг"/>
<text value="Шаг"/>
</properties>
</component>
<component id="aeb6b" class="javax.swing.JComboBox" binding="comboBoxStrategy">
<constraints>
<grid row="0" column="3" row-span="1" col-span="3" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
</component>
<vspacer id="396ed">
<constraints>
<grid row="2" column="4" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
<component id="c737e" class="javax.swing.JButton" binding="buttonCreateShip">
<constraints>
<grid row="4" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<doubleBuffered value="false"/>
<text value="Создать Корабль"/>
</properties>
</component>
<component id="b8ca7" class="javax.swing.JButton" binding="buttonCreateContainerShip">
<constraints>
<grid row="4" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<doubleBuffered value="false"/>
<text value="Создать Контейнеровоз"/>
</properties>
</component>
</children>
</grid>
</children>

View File

@ -1,5 +1,8 @@
package ProjectContainerShip.src;
import ProjectContainerShip.src.Drawings.*;
import ProjectContainerShip.src.MovementStrategy.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
@ -9,16 +12,44 @@ import java.util.Random;
import java.util.List;
public class FormContainerShip extends JFrame {
protected DrawningContainerShip _drawningContainerShip = new DrawningContainerShip();
protected DrawningShip _drawningShip;
private AbstractStrategy _strategy;
JPanel PanelWrapper;
private JPanel PictureBox;
private JButton buttonCreate;
private JButton buttonCreateContainerShip;
private JButton buttonCreateShip;
private JButton buttonRight;
private JButton buttonDown;
private JButton buttonLeft;
private JButton buttonUp;
private JButton buttonStrategyStep;
private JComboBox comboBoxStrategy;
private List<JComponent> controls;
public void createObject(String obj){
Random rand = new Random();
switch(obj){
case "Ship":
_drawningShip = new DrawningShip(rand.nextInt(100, 300), rand.nextDouble(1000, 3000),
new Color(rand.nextInt(255), rand.nextInt(255), rand.nextInt(255)), rand.nextInt(0, 3));
break;
case "ContainerShip":
_drawningShip = new DrawningContainerShip(rand.nextInt(100, 300), rand.nextDouble(1000, 3000),
new Color(rand.nextInt(255), rand.nextInt(255), rand.nextInt(255)), rand.nextInt(0, 3),
new Color(rand.nextInt(255), rand.nextInt(255), rand.nextInt(255)),
rand.nextBoolean(), rand.nextBoolean());
break;
default:
return;
}
_drawningShip.SetPictureSize(PictureBox.getWidth(), PictureBox.getHeight());
_drawningShip.SetPosition(rand.nextInt(50), rand.nextInt(50));
_strategy = null;
comboBoxStrategy.setEnabled(true);
Draw();
}
public FormContainerShip() {
buttonUp.setName("buttonUp");
buttonDown.setName("buttonDown");
@ -27,46 +58,28 @@ public class FormContainerShip extends JFrame {
InitializeControlsRepaintList();
buttonCreate.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
_drawningContainerShip = new DrawningContainerShip();
Random random = new Random();
buttonCreateContainerShip.addActionListener(e -> createObject("ContainerShip"));
_drawningContainerShip.Init(random.nextInt(30, 100),
random.nextInt(100, 500),
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)),
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)),
random.nextBoolean(), random.nextBoolean() );
_drawningContainerShip.setPictureSize(PictureBox.getWidth(), PictureBox.getHeight());
_drawningContainerShip.setPosition(random.nextInt(25, 100),
random.nextInt(25, 100));
Draw();
}
});
ActionListener buttonMoveClickedListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
buttonCreateShip.addActionListener(e -> createObject("Ship"));
ActionListener buttonMoveClickedListener = e -> {
String buttonName = ((JButton) e.getSource()).getName();
boolean result = false;
switch (buttonName) {
case "buttonUp": {
result = _drawningContainerShip.moveTransport(DirectionType.Up);
result = _drawningShip.MoveTransport(DirectionType.Up);
}
break;
case "buttonDown": {
result = _drawningContainerShip.moveTransport(DirectionType.Down);
result = _drawningShip.MoveTransport(DirectionType.Down);
}
break;
case "buttonLeft": {
result = _drawningContainerShip.moveTransport(DirectionType.Left);
result = _drawningShip.MoveTransport(DirectionType.Left);
}
break;
case "buttonRight": {
result = _drawningContainerShip.moveTransport(DirectionType.Right);
result = _drawningShip.MoveTransport(DirectionType.Right);
}
break;
@ -74,28 +87,59 @@ public class FormContainerShip extends JFrame {
if (result)
Draw();
}
};
buttonRight.addActionListener(buttonMoveClickedListener);
buttonDown.addActionListener(buttonMoveClickedListener);
buttonLeft.addActionListener(buttonMoveClickedListener);
buttonUp.addActionListener(buttonMoveClickedListener);
comboBoxStrategy.addItem("К Центру");
comboBoxStrategy.addItem("К Краю");
buttonStrategyStep.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_drawningShip == null){return;}
if (comboBoxStrategy.isEnabled()){
switch (comboBoxStrategy.getSelectedIndex()){
case 0:
_strategy = new MoveToCenter();
break;
case 1:
_strategy = new MoveToBorder();
break;
default:
_strategy = null;
break;
}
if (_strategy == null) {return;}
_strategy.SetData(new MovableShip(_drawningShip), PictureBox.getWidth(), PictureBox.getHeight());
}
if (_strategy == null) {return;}
_strategy.MakeStep();
Draw();
comboBoxStrategy.setEnabled(false);
if (_strategy.GetStatus() == StrategyStatus.Finish) {
comboBoxStrategy.setEnabled(true);
_strategy = null;
}
}
});
}
private void Draw() {
if (_drawningContainerShip.getEntityContainerShip() == null)
if (_drawningShip.getEntityShip() == null)
return;
if (PictureBox.getWidth() == 0 || PictureBox.getHeight() == 0) {
return;
}
Graphics g = PictureBox.getGraphics();
g.setColor(PictureBox.getBackground());
g.fillRect(0,0, PictureBox.getWidth(), PictureBox.getHeight());
_drawningContainerShip.drawContainerShip(g);
g.fillRect(0, 0, PictureBox.getWidth(), PictureBox.getHeight());
_drawningShip.DrawShip(g);
RepaintControls();
}
private void RepaintControls() {
for (JComponent control : controls) {
control.repaint();
@ -105,12 +149,12 @@ public class FormContainerShip extends JFrame {
private void InitializeControlsRepaintList() {
controls = new LinkedList<>();
controls.add(buttonCreate);
controls.add(buttonCreateContainerShip);
controls.add(buttonCreateShip);
controls.add(buttonUp);
controls.add(buttonDown);
controls.add(buttonLeft);
controls.add(buttonRight);
}
}