Compare commits

..

10 Commits

Author SHA1 Message Date
ae4cefd616 финальный рефакторинг 2024-03-13 00:04:49 +04:00
pyzhov.egor
7072feff6d fix высоты лодки 2024-03-11 23:42:45 +04:00
pyzhov.egor
f6ba6fe601 Доделал 2 часть с стратегиями и усложненное задание 2024-03-11 18:49:30 +04:00
pyzhov.egor
f1f7561301 Перенес первую часть лабораторной 2024-03-10 20:28:14 +04:00
pyzhov.egor
a27cb59ee0 убрал лишние файлы 2024-02-27 23:17:07 +04:00
7fb5ba8c39 final 2024-02-27 13:59:00 +04:00
pyzhov.egor
fac7aba105 fixes 2024-02-27 00:14:26 +04:00
pyzhov.egor
80f133e150 some fixes 2024-02-13 00:48:29 +04:00
pyzhov.egor
6b543de74e 1.0 2024-02-12 00:18:53 +04:00
pyzhov.egor
c913a06626 1.0 2024-02-12 00:13:20 +04:00
27 changed files with 950 additions and 5 deletions

1
.idea/.name Normal file
View File

@ -0,0 +1 @@
Main.java

View File

@ -119,6 +119,9 @@
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
</item>
<item class="FormCatamaran" icon="" removable="true" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="0" />
</item>
</group>
</component>
</project>

View File

@ -3,6 +3,7 @@
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/Resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />

Binary file not shown.

After

Width:  |  Height:  |  Size: 244 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 249 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 254 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 257 B

View File

@ -0,0 +1,143 @@
package Drawnings;
import Entities.*;
import MovementStrategy.*;
import java.awt.*;
import java.util.Random;
public class DrawningBoat {
private final EntityBoat entityBoat;
public EntityBoat getEntityBoat() {
return entityBoat;
}
private Integer _pictureWidth;
private Integer _pictureHeight;
protected Integer _startPosX;
protected Integer _startPosY;
private int _drawingBoatWidth = 107;
private int _drawingBoatHeight = 75;
public int GetPosX(){return _startPosX;}
public int GetPosY(){return _startPosY;}
public int GetWidth(){return _drawingBoatWidth;}
public int GetHeight(){return _drawingBoatHeight;}
private IDrawPaddles drawPaddles;
public DrawningBoat(int speed, float weight, Color bodyColor, int paddlesType) {
entityBoat = new EntityBoat(speed, weight, bodyColor);
_startPosY = null;
_startPosX = null;
_pictureWidth = null;
_pictureHeight = null;
switch (paddlesType) {
case 0:
drawPaddles = new DrawningPaddles();
break;
case 1:
drawPaddles = new DrawningOvalPaddles();
break;
case 2:
drawPaddles = new DrawningRectanglePaddles();
break;
}
Random random = new Random();
int paddlesCount = random.nextInt(1,4);
drawPaddles.setNumber(paddlesCount);
}
protected DrawningBoat(int speed, float weight, Color bodyColor, int paddlesType, int boatWidth, int boatHeight) {
this(speed, weight, bodyColor, paddlesType);
_drawingBoatHeight = boatHeight;
_drawingBoatWidth = boatWidth;
}
public void setPosition(int x, int y) {
if (_pictureHeight == null || _pictureWidth == null)
return;
_startPosX = x;
_startPosY = y;
if (_drawingBoatWidth + x > _pictureWidth || x < 0) {
_startPosX = 0;
}
if (_drawingBoatHeight + y > _pictureHeight || y < 0) {
_startPosY = 0;
}
}
public boolean setPictureSize(int width, int height) {
if (_drawingBoatHeight > height || _drawingBoatWidth > width)
return false;
_pictureHeight = height;
_pictureWidth = width;
if (_startPosX != null && _startPosY != null)
{
if (_startPosX + _drawingBoatWidth > width)
_startPosX = width - _drawingBoatWidth;
if (_startPosY + _drawingBoatHeight > height)
_startPosY = height - _drawingBoatHeight;
}
return true;
}
public boolean moveTransport(MovementDirection direction) {
if (entityBoat == null || _pictureWidth == null || _pictureHeight == null)
return false;
switch (direction) {
case MovementDirection.Left:
if (_startPosX - entityBoat.getStep() > 0)
_startPosX -= (int) entityBoat.getStep();
return true;
case MovementDirection.Up:
if (_startPosY - entityBoat.getStep() > 0)
_startPosY -= (int) entityBoat.getStep();
return true;
case MovementDirection.Right:
if (_startPosX + entityBoat.getStep() < _pictureWidth - _drawingBoatWidth)
_startPosX += (int) entityBoat.getStep();
return true;
case MovementDirection.Down:
if (_startPosY + entityBoat.getStep() < _pictureHeight - _drawingBoatHeight)
_startPosY += (int) entityBoat.getStep();
return true;
default:
return false;
}
}
public void drawBoat(Graphics g) {
if (entityBoat == null || _startPosX == null || _startPosY == null) {
return;
}
Graphics2D g2d = (Graphics2D) g;
Point[] boatBorders = new Point[]{
new Point(_startPosX + 10, _startPosY + 20),
new Point(_startPosX + 80, _startPosY + 20),
new Point(_startPosX + 107, _startPosY + 40),
new Point(_startPosX + 80, _startPosY + 55),
new Point(_startPosX + 10, _startPosY + 55)
};
Polygon catamaranPolygon = new Polygon();
for (Point point : boatBorders)
catamaranPolygon.addPoint(point.x, point.y);
g2d.setColor(Color.BLACK);
g2d.draw(catamaranPolygon);
g2d.setColor(entityBoat.getBodyColor());
g2d.fill(catamaranPolygon);
int ellipseX = _startPosX + 17;
int ellipseY = _startPosY + 24;
int ellipseWidth = 65;
int ellipseHeight = 27;
g2d.setColor(Color.BLACK);
g2d.drawOval(ellipseX, ellipseY, ellipseWidth, ellipseHeight);
g2d.setColor(Color.WHITE);
g2d.fillOval(ellipseX, ellipseY, ellipseWidth, ellipseHeight);
drawPaddles.drawPaddles(g2d, entityBoat.getBodyColor(), _startPosX, _startPosY);
}
}

View File

@ -0,0 +1,72 @@
package Drawnings;
import Entities.*;
import java.awt.*;
public class DrawningCatamaran extends DrawningBoat {
EntityCatamaran EntityBoat;
public DrawningCatamaran(int speed, float weight, Color bodyColor, int paddlesType, Color additionalColor, boolean sail, boolean floaters) {
super(speed, weight, bodyColor, paddlesType, 110, 75);
EntityBoat = new EntityCatamaran(speed, weight, bodyColor, additionalColor, floaters, sail);
}
private void drawFloater(int y0, Color additionalColor, Graphics2D g2d)
{
g2d.setColor(additionalColor);
g2d.setStroke(new BasicStroke(2));
Point[] floater = new Point[]
{
new Point(_startPosX + 10, _startPosY + y0 + 6),
new Point(_startPosX + 90, _startPosY + y0 + 6),
new Point(_startPosX + 110, _startPosY + y0 + 3),
new Point(_startPosX + 90, _startPosY + y0),
new Point(_startPosX + 10, _startPosY + y0),
};
Polygon floaterPolygon = new Polygon();
for (Point point: floater) {
floaterPolygon.addPoint(point.x, point.y);
}
g2d.fillPolygon(floaterPolygon);
g2d.drawPolygon(floaterPolygon);
}
@Override
public void drawBoat(Graphics g) {
if (EntityBoat == null || !(EntityBoat instanceof EntityCatamaran) || _startPosX == null || _startPosY == null) {
return;
}
super.drawBoat(g);
Graphics2D g2d = (Graphics2D) g;
if (EntityBoat.getFloaters()) {
g2d.setColor(Color.BLACK);
g2d.drawLine(_startPosX+20, _startPosY+20, _startPosX + 20, _startPosY+17);
g2d.drawLine(_startPosX+80, _startPosY+20, _startPosX + 80, _startPosY+17);
g2d.drawLine(_startPosX+20, _startPosY+55, _startPosX + 20, _startPosY+58);
g2d.drawLine(_startPosX+80, _startPosY+55, _startPosX + 80, _startPosY+58);
drawFloater(11, EntityBoat.getAdditionalColor(), g2d);
drawFloater(58, EntityBoat.getAdditionalColor(), g2d);
}
if (EntityBoat.getSail()) {
g2d.setColor(Color.BLACK);
g2d.setStroke(new BasicStroke(1));
Point[] sail = new Point[] {
new Point(_startPosX + 50, _startPosY + 45),
new Point(_startPosX + 35, _startPosY + 39),
new Point(_startPosX + 35, _startPosY + 24),
new Point(_startPosX + 50, _startPosY + 18),
new Point(_startPosX + 50, _startPosY + 43),
};
Polygon sailPolygon = new Polygon();
for(Point point: sail) {
sailPolygon.addPoint(point.x, point.y);
}
g2d.drawPolygon(sailPolygon);
g2d.setColor(EntityBoat.getAdditionalColor());
g2d.fillPolygon(sailPolygon);
}
}
}

View File

@ -0,0 +1,37 @@
package Drawnings;
import java.awt.*;
public class DrawningOvalPaddles implements IDrawPaddles {
private PaddlesCount _paddlesCount;
@Override
public void setNumber(int paddlesCount) {
for (PaddlesCount value : PaddlesCount.values()) {
if (value.getEnumNumber() == paddlesCount) {
_paddlesCount = value;
return;
}
}
}
@Override
public void drawPaddles(Graphics2D g2d, Color color, int _startX, int _startY) {
g2d.setColor(color);
g2d.setStroke(new BasicStroke(4));
int distanceBetweenPaddles = 27;
for (int i = 0; i < _paddlesCount.getEnumNumber(); i++) {
int posX = (int)(_startX + i * distanceBetweenPaddles); // Позиция X для текущей пары весел
drawPaddlePair(g2d, posX, (int)_startY + 5);
}
}
private void drawPaddlePair(Graphics2D g2d, int posX, int posY) {
g2d.drawLine(posX + 20, posY + 15, posX + 9, posY - 3); // Рисуем левое весло
g2d.drawLine(posX + 20, posY + 50, posX + 9, posY + 68); // Рисуем правое весло
// Добавляем овалы на концы весел
int ovalSize = 10; // Размер овала
int halfStrokeWidth = 2; // Половина толщины линии
g2d.fillOval(posX + 9 - halfStrokeWidth - ovalSize / 2, posY + 68 - ovalSize / 2, ovalSize, ovalSize);
g2d.fillOval(posX + 9 - halfStrokeWidth - ovalSize / 2, posY - 3 - ovalSize / 2, ovalSize, ovalSize);
}
}

View File

@ -0,0 +1,32 @@
package Drawnings;
import java.awt.*;
public class DrawningPaddles implements IDrawPaddles {
private PaddlesCount _paddlesCount;
@Override
public void setNumber(int paddlesCount) {
for (PaddlesCount value : PaddlesCount.values()) {
if (value.getEnumNumber() == paddlesCount) {
_paddlesCount = value;
return;
}
}
}
@Override
public void drawPaddles(Graphics2D g2d, Color color, int _startX, int _startY) {
g2d.setColor(color);
g2d.setStroke(new BasicStroke(4));
int distanceBetweenPaddles = 27;
for (int i = 0; i < _paddlesCount.getEnumNumber(); i++) {
int posX = (int)(_startX + i * distanceBetweenPaddles); // Позиция X для текущей пары весел
drawPaddlePair(g2d, posX, (int)_startY + 5);
}
}
private void drawPaddlePair(Graphics2D g2d, int posX, int posY) {
g2d.drawLine(posX + 20, posY + 15, posX + 9, posY - 3); // Рисуем левое весло
g2d.drawLine(posX + 20, posY + 50, posX + 9, posY + 68); // Рисуем правое весло
}
}

View File

@ -0,0 +1,39 @@
package Drawnings;
import java.awt.*;
public class DrawningRectanglePaddles implements IDrawPaddles {
private PaddlesCount _paddlesCount;
@Override
public void setNumber(int paddlesCount) {
for (PaddlesCount value : PaddlesCount.values()) {
if (value.getEnumNumber() == paddlesCount) {
_paddlesCount = value;
return;
}
}
}
@Override
public void drawPaddles(Graphics2D g2d, Color color, int _startX, int _startY) {
g2d.setColor(color);
g2d.setStroke(new BasicStroke(4));
int distanceBetweenPaddles = 27;
for (int i = 0; i < _paddlesCount.getEnumNumber(); i++) {
int posX = (int)(_startX + i * distanceBetweenPaddles); // Позиция X для текущей пары весел
drawPaddlePair(g2d, posX, (int)_startY + 5);
}
}
private void drawPaddlePair(Graphics2D g2d, int posX, int posY) {
g2d.drawLine(posX + 20, posY + 15, posX + 9, posY - 3); // Рисуем левое весло
g2d.drawLine(posX + 20, posY + 50, posX + 9, posY + 68); // Рисуем правое весло
int rectangleSize = 8; // Размер прямоугольника
int halfStrokeWidth = 2; // Половина толщины линии
// Добавляем прямоугольники на концы весел
g2d.fillRect(posX + 9 - halfStrokeWidth - rectangleSize / 2, posY + 68 - rectangleSize / 2, rectangleSize, rectangleSize);
g2d.fillRect(posX + 9 - halfStrokeWidth - rectangleSize / 2, posY - 3 - rectangleSize / 2, rectangleSize, rectangleSize);
}
}

View File

@ -0,0 +1,8 @@
package Drawnings;
import java.awt.*;
public interface IDrawPaddles {
void setNumber(int x);
void drawPaddles(Graphics2D graphics2D, Color color, int _startX, int _startY);
}

View File

@ -0,0 +1,14 @@
package Drawnings;
public enum PaddlesCount {
One(1),
Two(2),
Three(3);
final private int EnumNumber;
PaddlesCount(int enumNumber) {
EnumNumber = enumNumber;
}
public int getEnumNumber() {
return EnumNumber;
}
}

View File

@ -0,0 +1,28 @@
package Entities;
import java.awt.*;
public class EntityBoat {
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 double Step;
public double getStep() {
return Speed*100/Weight;
}
public EntityBoat(int speed, double weight, Color bodyColor) {
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
}
}

View File

@ -0,0 +1,24 @@
package Entities;
import java.awt.*;
public class EntityCatamaran extends EntityBoat {
private Color AdditionalColor;
public Color getAdditionalColor() {
return AdditionalColor;
}
private boolean Sail;
public boolean getSail() {
return Sail;
}
private boolean Floaters;
public boolean getFloaters() {
return Floaters;
}
public EntityCatamaran(int speed, double weight, Color bodyColor ,Color additionalColor, boolean sail, boolean floaters) {
super(speed, weight, bodyColor);
AdditionalColor = additionalColor;
Sail = sail;
Floaters = floaters;
}
}

View File

@ -0,0 +1,131 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="FormCatamaran">
<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="599" height="476"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<grid id="1ff9a" binding="PictureBox" layout-manager="GridLayoutManager" row-count="6" column-count="5" 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"/>
</constraints>
<properties>
<toolTipText value=""/>
</properties>
<border type="none"/>
<children>
<vspacer id="7b40c">
<constraints>
<grid row="2" column="0" row-span="2" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false">
<preferred-size width="88" height="14"/>
</grid>
</constraints>
</vspacer>
<hspacer id="e0b21">
<constraints>
<grid row="4" column="1" 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="a8c2" class="javax.swing.JButton" binding="buttonCreateCatamaran">
<constraints>
<grid row="5" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="2" fill="1" indent="0" use-parent-layout="false">
<minimum-size width="150" height="30"/>
<preferred-size width="150" height="30"/>
<maximum-size width="150" height="30"/>
</grid>
</constraints>
<properties>
<text value="Создать катамаран"/>
</properties>
</component>
<component id="79b3e" class="javax.swing.JButton" binding="buttonCreateBoat">
<constraints>
<grid row="5" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="10" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="150" height="30"/>
<preferred-size width="150" height="30"/>
<maximum-size width="150" height="30"/>
</grid>
</constraints>
<properties>
<text value="Создать лодку"/>
</properties>
</component>
<component id="fe25b" class="javax.swing.JButton" binding="buttonLeft">
<constraints>
<grid row="5" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="4" 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"/>
</grid>
</constraints>
<properties>
<icon value="30px_arrow_left.png"/>
<text value=""/>
</properties>
</component>
<component id="3eabd" class="javax.swing.JButton" binding="buttonStrategyStep">
<constraints>
<grid row="1" column="3" row-span="1" col-span="2" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Шаг"/>
</properties>
</component>
<component id="86d84" class="javax.swing.JButton" binding="buttonRight">
<constraints>
<grid row="5" 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"/>
</grid>
</constraints>
<properties>
<icon value="30px_arrow_right.png"/>
<text value=""/>
</properties>
</component>
<component id="3dc0" class="javax.swing.JButton" binding="buttonDown">
<constraints>
<grid row="5" 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"/>
</grid>
</constraints>
<properties>
<icon value="30px_arrow_down.png"/>
<text value=""/>
</properties>
</component>
<component id="ccb2a" class="javax.swing.JButton" binding="buttonUp">
<constraints>
<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"/>
</grid>
</constraints>
<properties>
<icon value="30px_arrow_up.png"/>
<text value=""/>
</properties>
</component>
<component id="988c8" class="javax.swing.JComboBox" binding="comboBoxStrategy">
<constraints>
<grid row="0" column="2" row-span="1" col-span="3" vsize-policy="0" hsize-policy="2" anchor="9" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<actionCommand value=""/>
<doubleBuffered value="false"/>
<toolTipText value=""/>
</properties>
</component>
</children>
</grid>
</children>
</grid>
</form>

View File

@ -0,0 +1,177 @@
import Drawnings.*;
import MovementStrategy.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;
import java.util.Random;
import java.util.List;
public class FormCatamaran extends JFrame {
protected DrawningBoat _drawningBoat;
JPanel PanelWrapper;
private JPanel PictureBox;
private JButton buttonCreateCatamaran;
private JButton buttonRight;
private JButton buttonDown;
private JButton buttonLeft;
private JButton buttonUp;
private JButton buttonCreateBoat;
private JComboBox comboBoxStrategy;
private JButton buttonStrategyStep;
private AbstractStrategy _strategy;
private List<JComponent> controls;
private void createObject(String type) {
Random random = new Random();
switch (type) {
case "Drawnings.DrawningBoat":
_drawningBoat = new DrawningBoat(random.nextInt(70 - 30) + 30, random.nextInt(500 - 100) + 100,
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)), random.nextInt(3));
break;
case "Drawnings.DrawningCatamaran":
_drawningBoat = new DrawningCatamaran(random.nextInt(70 - 30) + 30, random.nextInt(500 - 100) + 100,
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)), random.nextInt(3),
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)),
random.nextBoolean(), random.nextBoolean());
break;
default:
return;
}
_drawningBoat.setPictureSize(PictureBox.getWidth(), PictureBox.getHeight());
_drawningBoat.setPosition(random.nextInt(25, 100),
random.nextInt(25, 100));
_strategy = null;
comboBoxStrategy.setEnabled(true);
Draw();
}
public FormCatamaran() {
buttonUp.setName("buttonUp");
buttonDown.setName("buttonDown");
buttonLeft.setName("buttonLeft");
buttonRight.setName("buttonRight");
InitializeControlsRepaintList();
buttonCreateCatamaran.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
createObject("Drawnings.DrawningCatamaran");
}
});
buttonCreateBoat.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
createObject("Drawnings.DrawningBoat");
}
});
ActionListener buttonMoveClickedListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String buttonName = ((JButton) e.getSource()).getName();
boolean result = false;
switch (buttonName) {
case "buttonUp": {
result = _drawningBoat.moveTransport(MovementDirection.Up);
}
break;
case "buttonDown": {
result = _drawningBoat.moveTransport(MovementDirection.Down);
}
break;
case "buttonLeft": {
result = _drawningBoat.moveTransport(MovementDirection.Left);
}
break;
case "buttonRight": {
result = _drawningBoat.moveTransport(MovementDirection.Right);
}
break;
}
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 (_drawningBoat == 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 MoveableBoat(_drawningBoat), 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 (_drawningBoat.getEntityBoat() == 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());
_drawningBoat.drawBoat(g);
RepaintControls();
}
private void RepaintControls() {
for (JComponent control : controls) {
control.repaint();
}
}
private void InitializeControlsRepaintList() {
controls = new LinkedList<>();
controls.add(buttonCreateCatamaran);
controls.add(buttonCreateBoat);
controls.add(buttonUp);
controls.add(buttonDown);
controls.add(buttonLeft);
controls.add(buttonRight);
controls.add(comboBoxStrategy);
controls.add(buttonStrategyStep);
}
}

View File

@ -1,9 +1,14 @@
//TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or
// click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter.
import javax.swing.*;
public class Main {
public static void main(String[] args) {
//TIP Press <shortcut actionId="ShowIntentionActions"/> with your caret at the highlighted text
// to see how IntelliJ IDEA suggests fixing it.
System.out.println("Hello and welcome!");
JFrame.setDefaultLookAndFeelDecorated(false);
JFrame frame = new JFrame("Катамаран");
frame.setContentPane(new FormCatamaran().PanelWrapper);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocation(500, 200);
frame.pack();
frame.setSize(700, 500);
frame.setVisible(true);
}
}

View File

@ -0,0 +1,66 @@
package MovementStrategy;
public abstract class AbstractStrategy {
private IMoveableObject _moveableObject;
private StrategyStatus _state = StrategyStatus.NotInit;
protected int FieldWidth;
protected int FieldHeight;
public StrategyStatus GetStatus() { return _state; }
public void SetData(IMoveableObject moveableObject, int width, int height)
{
if (moveableObject == null)
{
_state = StrategyStatus.NotInit;
return;
}
_state = StrategyStatus.InProgress;
_moveableObject = moveableObject;
FieldWidth = width;
FieldHeight = height;
}
public void MakeStep()
{
if (_state != StrategyStatus.InProgress)
{
return;
}
if (IsTargetDestination())
{
_state = StrategyStatus.Finish;
return;
}
MoveToTarget();
}
protected boolean MoveLeft() { return MoveTo(MovementDirection.Left); }
protected boolean MoveRight() { return MoveTo(MovementDirection.Right); }
protected boolean MoveUp() { return MoveTo(MovementDirection.Up); }
protected boolean MoveDown() { return MoveTo(MovementDirection.Down); }
protected ObjectParameters GetObjectParameters() { return _moveableObject.GetObjectPosition(); }
protected int GetStep()
{
if (_state != StrategyStatus.InProgress)
{
return 0;
}
return _moveableObject.GetStep();
}
protected abstract void MoveToTarget();
protected abstract boolean IsTargetDestination();
private boolean MoveTo(MovementDirection directionType)
{
if (_state != StrategyStatus.InProgress)
{
return false;
}
if (_moveableObject.TryMoveObject(directionType))
{
_moveableObject.MoveObject(directionType);
return true;
}
return false;
}
}

View File

@ -0,0 +1,8 @@
package MovementStrategy;
public interface IMoveableObject {
ObjectParameters GetObjectPosition();
int GetStep();
boolean TryMoveObject(MovementDirection direction);
void MoveObject(MovementDirection direction);
}

View File

@ -0,0 +1,47 @@
package MovementStrategy;
public class MoveToBorder extends AbstractStrategy {
protected boolean IsTargetDestination()
{
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;
}
int diffX = objParams.RightBorder() - FieldWidth;
if (Math.abs(diffX) > GetStep())
{
if (diffX > 0)
{
MoveLeft();
}
else
{
MoveRight();
}
}
int diffY = objParams.DownBorder() - FieldHeight;
if (Math.abs(diffY) > GetStep())
{
if (diffY > 0)
{
MoveUp();
}
else
{
MoveDown();
}
}
}
}

View File

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

View File

@ -0,0 +1,23 @@
package MovementStrategy;
import Drawnings.*;
public class MoveableBoat implements IMoveableObject {
private DrawningBoat _boat = null;
public MoveableBoat(DrawningBoat drawningBoat)
{
_boat = drawningBoat;
}
public ObjectParameters GetObjectPosition()
{
if (_boat == null || _boat.getEntityBoat() == null)
{
return null;
}
return new ObjectParameters(_boat.GetPosX(), _boat.GetPosY(), _boat.GetWidth(), _boat.GetHeight());
}
public int GetStep() { return (int) _boat.getEntityBoat().getStep(); }
public boolean TryMoveObject(MovementDirection direction) { return _boat.moveTransport(direction); }
public void MoveObject(MovementDirection direction) { _boat.moveTransport(direction); }
}

View File

@ -0,0 +1,8 @@
package MovementStrategy;
public enum MovementDirection {
Up,
Down,
Left,
Right;
}

View File

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

View File

@ -0,0 +1,7 @@
package MovementStrategy;
public enum StrategyStatus {
NotInit,
InProgress,
Finish
}