PIbd-21 Yakovlev M.G. lab work 02 Hard #2

Closed
YakovlevMaxim wants to merge 2 commits from Lab2SailBoatHard into Lab1SailBoatHard
25 changed files with 1025 additions and 0 deletions
Showing only changes of commit 8aec9d27f6 - Show all commits

3
.idea/.gitignore generated vendored Normal file
View File

@ -0,0 +1,3 @@
# Default ignored files
/shelf/
/workspace.xml

View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

6
.idea/misc.xml generated Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="19" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

8
.idea/modules.xml generated Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/PIbd-21_Yakovlev_M.G_Parusnik_Harder.iml" filepath="$PROJECT_DIR$/.idea/PIbd-21_Yakovlev_M.G_Parusnik_Harder.iml" />
</modules>
</component>
</project>

124
.idea/uiDesigner.xml generated Normal file
View File

@ -0,0 +1,124 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Palette2">
<group name="Swing">
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
</item>
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
</item>
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.svg" removable="false" auto-create-binding="false" can-attach-label="true">
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
</item>
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
<initial-values>
<property name="text" value="Button" />
</initial-values>
</item>
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="RadioButton" />
</initial-values>
</item>
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="CheckBox" />
</initial-values>
</item>
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
<initial-values>
<property name="text" value="Label" />
</initial-values>
</item>
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
</item>
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
</item>
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
<preferred-size width="-1" height="20" />
</default-constraints>
</item>
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
</item>
<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>
</group>
</component>
</project>

6
.idea/vcs.xml generated Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

View File

@ -0,0 +1,53 @@
package SailBoatHard.Complication;
import java.awt.*;
public class DrawPaddle implements IDrawPaddle {
private Paddle paddleCount;
@Override
public void SetPaddleCount(int num) {
switch (num){
case 1:
paddleCount = Paddle.One;
break;
case 2:
paddleCount = Paddle.Two;
break;
default:
paddleCount = Paddle.Three;
}
}
@Override
public Paddle GetPaddleCount() {
return paddleCount;
}
public void DrawPaddles(int _startPosX, int _startPosY,Graphics g2d){
switch (paddleCount)
{
case One:
break;
case Two:
DrawFirstDop(_startPosX,_startPosY,g2d);
break;
case Three:
DrawSecondDop(_startPosX,_startPosY,g2d);
break;
}
}
public void DrawFirstDop(int _startPosX, int _startPosY, Graphics g) {
g.setColor(Color.BLACK);
g.drawLine(_startPosX + 30, _startPosY + 60, _startPosX + 45, _startPosY + 27);
g.drawOval(_startPosX+42, _startPosY+13, 10, 15);
g.drawLine(_startPosX + 30, _startPosY + 120, _startPosX + 45, _startPosY + 150);
g.drawOval(_startPosX+42, _startPosY+150, 10, 15);
}
public void DrawSecondDop(int _startPosX, int _startPosY, Graphics g) {
DrawFirstDop(_startPosX,_startPosY,g);
g.setColor(Color.BLACK);
g.drawLine(_startPosX + 75, _startPosY + 60, _startPosX + 90, _startPosY + 27);
g.drawOval(_startPosX+87, _startPosY+13, 10, 15);
g.drawLine(_startPosX + 75, _startPosY + 120, _startPosX + 90, _startPosY + 150);
g.drawOval(_startPosX+87, _startPosY+150, 10, 15);
}
}

View File

@ -0,0 +1,56 @@
package SailBoatHard.Complication;
import java.awt.*;
public class DrawPaddleRectangle implements IDrawPaddle {
private Paddle paddleCount;
@Override
public void SetPaddleCount(int num) {
switch (num){
case 1:
paddleCount = Paddle.One;
break;
case 2:
paddleCount = Paddle.Two;
break;
default:
paddleCount = Paddle.Three;
}
}
@Override
public Paddle GetPaddleCount() {
return paddleCount;
}
@Override
public void DrawPaddles(int _startPosX, int _startPosY,Graphics g2d) {
switch (paddleCount)
{
case One:
break;
case Two:
DrawFirstDopRect(_startPosX,_startPosY,g2d);
break;
case Three:
DrawSecondDopRect(_startPosX,_startPosY,g2d);
break;
}
}
public void DrawFirstDopRect(int _startPosX, int _startPosY, Graphics g) {
g.setColor(Color.BLACK);
g.drawLine(_startPosX + 30, _startPosY + 60, _startPosX + 45, _startPosY + 27);
g.drawRect(_startPosX+42, _startPosY+13, 10, 15);
g.drawLine(_startPosX + 30, _startPosY + 120, _startPosX + 45, _startPosY + 150);
g.drawRect(_startPosX+42, _startPosY+150, 10, 15);
}
public void DrawSecondDopRect(int _startPosX, int _startPosY, Graphics g) {
DrawFirstDopRect(_startPosX,_startPosY,g);
g.setColor(Color.BLACK);
g.drawLine(_startPosX + 75, _startPosY + 60, _startPosX + 90, _startPosY + 27);
g.drawRect(_startPosX+87, _startPosY+13, 10, 15);
g.drawLine(_startPosX + 75, _startPosY + 120, _startPosX + 90, _startPosY + 150);
g.drawRect(_startPosX+87, _startPosY+150, 10, 15);
}
}

View File

@ -0,0 +1,77 @@
package SailBoatHard.Complication;
import java.awt.*;
public class DrawPaddleTriangle implements IDrawPaddle {
private Paddle paddleCount;
@Override
public void SetPaddleCount(int num) {
switch (num) {
case 1:
paddleCount = Paddle.One;
break;
case 2:
paddleCount = Paddle.Two;
break;
default:
paddleCount = Paddle.Three;
}
}
@Override
public Paddle GetPaddleCount() {
return paddleCount;
}
@Override
public void DrawPaddles(int _startPosX, int _startPosY, Graphics g2d) {
switch (paddleCount) {
case One:
break;
case Two:
DrawFirstDopTriangle(_startPosX, _startPosY, g2d);
break;
case Three:
DrawSecondDopTriangle(_startPosX, _startPosY, g2d);
break;
}
}
public void DrawFirstDopTriangle(int _startPosX, int _startPosY, Graphics g) {
g.setColor(Color.BLACK);
g.drawLine(_startPosX + 30, _startPosY + 60, _startPosX + 45, _startPosY + 27);
g.drawLine(_startPosX + 30, _startPosY + 120, _startPosX + 45, _startPosY + 150);
Polygon elementsSec = new Polygon();
elementsSec.addPoint(_startPosX + 45, _startPosY + 13);
elementsSec.addPoint(_startPosX + 52, _startPosY + 35);
elementsSec.addPoint(_startPosX + 35, _startPosY + 35);
g.setColor(Color.BLACK);
g.fillPolygon(elementsSec);
elementsSec = new Polygon();
elementsSec.addPoint(_startPosX + 45, _startPosY + 163);
elementsSec.addPoint(_startPosX + 52, _startPosY + 145);
elementsSec.addPoint(_startPosX + 35, _startPosY + 145);
g.fillPolygon(elementsSec);
}
public void DrawSecondDopTriangle(int _startPosX, int _startPosY, Graphics g) {
DrawFirstDopTriangle(_startPosX, _startPosY, g);
g.setColor(Color.BLACK);
g.drawLine(_startPosX + 75, _startPosY + 60, _startPosX + 90, _startPosY + 27);
g.drawLine(_startPosX + 75, _startPosY + 120, _startPosX + 90, _startPosY + 150);
Polygon elementsThree = new Polygon();
elementsThree.addPoint(_startPosX + 90, _startPosY + 13);
elementsThree.addPoint(_startPosX + 97, _startPosY + 35);
elementsThree.addPoint(_startPosX + 80, _startPosY + 35);
g.setColor(Color.BLACK);
g.fillPolygon(elementsThree);
elementsThree = new Polygon();
elementsThree.addPoint(_startPosX + 90, _startPosY + 163);
elementsThree.addPoint(_startPosX + 97, _startPosY + 145);
elementsThree.addPoint(_startPosX + 80, _startPosY + 145);
g.fillPolygon(elementsThree);
}
}

View File

@ -0,0 +1,7 @@
package SailBoatHard.Complication;
import java.awt.*;
public interface IDrawPaddle {
void SetPaddleCount(int number);
Paddle GetPaddleCount();
void DrawPaddles(int _startPosX, int _startPosY,Graphics g2d);
}

View File

@ -0,0 +1,8 @@
package SailBoatHard.Complication;
public enum Paddle {
One(1),
Two(2),
Three(3);
Paddle(int i) {}
}

View File

@ -0,0 +1,16 @@
package SailBoatHard.Direction;
public enum Direction {
Up(1),
Down(2),
Left (3),
Right(4)
;
private final int Direct;
Direction(int i) {
this.Direct=i;
}
public int GetDirect() {
return Direct;
}
}

View File

@ -0,0 +1,107 @@
package SailBoatHard.Drawing;
import SailBoatHard.Direction.Direction;
import SailBoatHard.Entity.EntityBoat;
import javax.swing.*;
import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.util.*;
public class DrawingBoat extends JPanel {
public EntityBoat boat; //Класс-сущность
public int _startPosX; //Координаты отрисовки по оси x
public EntityBoat GetBoat(){return boat;}
public int GetStartX() {
return _startPosX;
}
public int _startPosY; //Координаты отрисовки по оси y
public int GetStartY() {
return _startPosY;
}
private Integer _pictureWidth = null; //Ширина окна
private Integer _pictureHeight = null; //Высота окна
private final int _boatWidth = 180; //Ширина отрисовки крейсера
public int GetWidth() {
return _boatWidth;
}
private final int _boatHeight = 180; //Высота отрисовки крейсера
public int GetHeight() {
return _boatHeight;
}
//Инициализация
public DrawingBoat(int speed, float weight, Color bodyColor, int width, int height, int blockCount) {
boat = new EntityBoat(speed,weight,bodyColor);
_pictureWidth = width;
_pictureHeight = height;
}
//Начальные коордитанты
public void SetPosition(int x, int y, int width, int height)
{
if (width < _boatWidth || height < _boatHeight) return;
Random random = new Random();
_startPosX = x < 0 || x + _boatWidth > width ? random.nextInt(0, width - _boatWidth) : x;
_startPosY = y < 0 || y + _boatHeight > height ? random.nextInt(0, height - _boatHeight) : y;
_pictureWidth = width;
_pictureHeight = height;
}
//Движение транспорта по координатам
public void MoveTransport(Direction direction)
{
if (_pictureWidth == null || _pictureHeight == null) return;
switch (direction)
{
case Left: //Влево
if (_startPosX - boat.GetStep() > 0) _startPosX -= boat.GetStep();
break;
case Up: //Вверх
if (_startPosY - boat.GetStep() > 0) _startPosY -= boat.GetStep();
break;
case Right: //Вправо
if (_startPosX + _boatWidth + boat.GetStep() < _pictureWidth) _startPosX += boat.GetStep();
break;
case Down: //Вниз
if (_startPosY + _boatHeight + boat.GetStep() < _pictureHeight) _startPosY += boat.GetStep();
break;
}
}
//Отрисовка транспорта
public void DrawTransport(Graphics gr)
{
if (GetBoat() == null) return;
if (_startPosX < 0 || _startPosY < 0 || _pictureWidth == null || _pictureHeight == null)
{
return;
}
Graphics2D g2 = (Graphics2D)gr;
g2.setColor(Color.BLACK);
g2.drawLine(_startPosX + 10, _startPosY + 60, _startPosX + 110, _startPosY + 60);
g2.drawLine( _startPosX + 110, _startPosY + 60, _startPosX + 180, _startPosY + 90);
g2.drawLine( _startPosX + 180, _startPosY + 90, _startPosX + 110, _startPosY + 120);
g2.drawLine( _startPosX + 110, _startPosY + 120, _startPosX + 10, _startPosY + 120);
g2.drawLine(_startPosX + 10, _startPosY + 120, _startPosX + 10, _startPosY + 60);
g2.drawOval( _startPosX + 15, _startPosY + 65, 95, 50);
}
//Изменение границ отрисовки
public boolean CanMove(Direction direction) {
if (boat == null) {
return false;
}
return switch (direction) {
case Left -> _startPosX - boat.GetStep() > 0;
case Up -> _startPosY - boat.GetStep() > 0;
case Right -> _startPosX + _boatWidth + boat.GetStep() < _pictureWidth;
case Down -> _startPosY + _boatHeight + boat.GetStep() < _pictureHeight;
};
}
}

View File

@ -0,0 +1,60 @@
package SailBoatHard.Drawing;
import java.awt.*;
import java.util.Random;
import SailBoatHard.Complication.DrawPaddle;
import SailBoatHard.Complication.DrawPaddleRectangle;
import SailBoatHard.Complication.DrawPaddleTriangle;
import SailBoatHard.Complication.IDrawPaddle;
import SailBoatHard.Entity.*;
public class DrawingSailBoat extends DrawingBoat {
private IDrawPaddle drawingPaddles;
public DrawingSailBoat(int speed, float weight, Color bodyColor, Color
elementsColor, boolean rocketMines, boolean helipad, int width, int height, int blockCount)
{
super(speed, weight, bodyColor, width, height, blockCount);
if (boat != null)
{
boat = new EntitySailBoat(speed, weight, bodyColor,
elementsColor, rocketMines, helipad);
}
Random rand = new Random();
drawingPaddles = switch (rand.nextInt(3)) {
case 0 -> new DrawPaddle();
case 1 -> new DrawPaddleRectangle();
case 2 -> new DrawPaddleTriangle();
default -> new DrawPaddle();
};
drawingPaddles.SetPaddleCount(blockCount);
}
@Override
public void DrawTransport(Graphics g) {
if (!(boat instanceof EntitySailBoat sailBoat)) {
return;
}
super.DrawTransport(g);
drawingPaddles.DrawPaddles(_startPosX, _startPosY,g);
if (sailBoat.GetRainforcedBody())
{
g.setColor(Color.GRAY);
g.fillRect( _startPosX + 2, _startPosY + 65, 10, 50);
g.fillRect( _startPosX + 15, _startPosY + 53, 90, 10);
g.fillRect( _startPosX + 15, _startPosY + 118, 90, 10);
}
if (sailBoat.GetSail()){
g.setColor(Color.BLACK);
g.drawRect( _startPosX + 60, _startPosY , 4, 90);
g.drawRect( _startPosX + 60, _startPosY , 4, 90);
g.drawLine( _startPosX + 25, _startPosY + 20, _startPosX + 100, _startPosY);
g.drawLine( _startPosX + 100, _startPosY, _startPosX + 100, _startPosY + 50);
g.drawLine( _startPosX + 25, _startPosY + 70, _startPosX + 100, _startPosY + 50);
g.drawLine( _startPosX + 25, _startPosY + 70, _startPosX + 25, _startPosY + 20);
}
}
}

View File

@ -0,0 +1,26 @@
package SailBoatHard.Entity;
import java.awt.*;
import java.util.*;
public class EntityBoat {
private int Speed; //Скорость
private float Weight; //Вес
private Color BodyColor; //Цвет
private float Step; //Шаг при перемещении
//Инициализация
public EntityBoat(int speed, float weight, Color bodyColor) {
Random random = new Random();
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
Step = Speed * 100 / Weight;
}
public Color GetBodyColor(){
return BodyColor;
}
public float GetStep(){
return Step;
}
}

View File

@ -0,0 +1,23 @@
package SailBoatHard.Entity;
import java.awt.*;
public class EntitySailBoat extends EntityBoat {
private Color ElementsColor;
public Color GetElementsColor() { return ElementsColor; }
private boolean Sail;
public boolean GetSail(){
return Sail;
}
private boolean RainforcedBody;
public boolean GetRainforcedBody(){
return RainforcedBody;
}
public EntitySailBoat(int speed, float weight, Color bodyColor, Color
elementsColor, boolean sail, boolean rainforcedBody) {
super(speed, weight, bodyColor);
ElementsColor = elementsColor;
Sail = sail;
RainforcedBody = rainforcedBody;
}
}

View File

@ -0,0 +1,175 @@
package SailBoatHard;
import javax.swing.*;
import java.awt.*;
import java.awt.Graphics;
import java.awt.event.ActionListener;
import java.util.*;
import SailBoatHard.Drawing.*;
import SailBoatHard.Direction.*;
import SailBoatHard.MovementStrategy.*;
public class FormBoat {
private DrawingBoat _drawingBoat;
private AbstractStrategy _abstractStrategy;
Canvas canv;
public void Draw(){
canv.repaint();
}
public FormBoat(){
JFrame frame = new JFrame("SailBoat");
JButton buttonCreateBoat = new JButton("Лодка");
buttonCreateBoat.setFocusPainted(false);
buttonCreateBoat.setContentAreaFilled(false);
JButton buttonCreateSailBoat = new JButton("Парусник ");
buttonCreateSailBoat.setFocusPainted(false);
buttonCreateSailBoat.setContentAreaFilled(false);
String[] items = {
"ToCenter",
"ToBorder"
};
JComboBox comboBoxStrategy = new JComboBox(items);
JButton buttonStep = new JButton("Шаг");
buttonStep.setFocusPainted(false);
buttonStep.setContentAreaFilled(false);
JButton buttonUp = new JButton();
buttonUp.setFocusPainted(false);
buttonUp.setContentAreaFilled(false);
buttonUp.setName("up");
buttonUp.setIcon(new ImageIcon(((new ImageIcon("Resources/up.png")).getImage()).getScaledInstance(35, 35, java.awt.Image.SCALE_SMOOTH)));
JButton buttonDown = new JButton();
buttonDown.setFocusPainted(false);
buttonDown.setContentAreaFilled(false);
buttonDown.setName("down");
buttonDown.setIcon(new ImageIcon(((new ImageIcon("Resources/down.png")).getImage()).getScaledInstance(35, 35, java.awt.Image.SCALE_SMOOTH)));
JButton buttonLeft = new JButton();
buttonLeft.setFocusPainted(false);
buttonLeft.setContentAreaFilled(false);
buttonLeft.setName("left");
buttonLeft.setIcon(new ImageIcon(((new ImageIcon("Resources/left.png")).getImage()).getScaledInstance(35, 35, java.awt.Image.SCALE_SMOOTH)));
JButton buttonRight = new JButton();
buttonRight.setFocusPainted(false);
buttonRight.setContentAreaFilled(false);
buttonRight.setName("right");
buttonRight.setIcon(new ImageIcon(((new ImageIcon("Resources/right.png")).getImage()).getScaledInstance(35, 35, java.awt.Image.SCALE_SMOOTH)));
buttonCreateBoat.addActionListener(
e -> {
Random random = new Random();
_drawingBoat = new DrawingBoat(random.nextInt(200) + 100,
random.nextInt(2000) + 1000,
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)),
this.canv.getWidth(), this.canv.getHeight(), random.nextInt(3-1+1)+1);
_drawingBoat.SetPosition(random.nextInt(100-30+1)+30, //начальные и конечные значения для рандома
random.nextInt(90-20+1)+20 //начальные и конечные значения для рандома
,this.canv.getWidth(), this.canv.getHeight());
canv._drawingBoat = _drawingBoat;
Draw();
}
);
buttonCreateSailBoat.addActionListener(
e -> {
Random random = new Random();
_drawingBoat = new DrawingSailBoat(random.nextInt(100) + 50,
random.nextInt(1000) + 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(),
this.canv.getWidth(), this.canv.getHeight(), random.nextInt(3-1+1)+1);
_drawingBoat.SetPosition(random.nextInt(100-30+1)+30, //начальные и конечные значения для рандома
random.nextInt(90-20+1)+20 //начальные и конечные значения для рандома
,this.canv.getWidth(), this.canv.getHeight());
canv._drawingBoat = _drawingBoat;
Draw();
}
);
ActionListener actionListener = e -> {
if (_drawingBoat == null){
return;
}
switch ((((JButton)(e.getSource())).getName())){
case "up":
_drawingBoat.MoveTransport(Direction.Up);
break;
case "down":
_drawingBoat.MoveTransport(Direction.Down);
break;
case "left":
_drawingBoat.MoveTransport(Direction.Left);
break;
case "right":
_drawingBoat.MoveTransport(Direction.Right);
break;
}
Draw();
};
buttonUp.addActionListener(actionListener);
buttonDown.addActionListener(actionListener);
buttonLeft.addActionListener(actionListener);
buttonRight.addActionListener(actionListener);
buttonStep.addActionListener(e -> {
if (_drawingBoat == null) {
return;
}
if (comboBoxStrategy.isEnabled()) {
_abstractStrategy = switch (comboBoxStrategy.getSelectedIndex()) {
case 0 -> new MoveToCenter();
case 1 -> new MoveToBorder();
default -> null;
};
if (_abstractStrategy == null)
{
return;
}
_abstractStrategy.SetData(new DrawingObjectBoat(_drawingBoat), this.canv.getWidth(), this.canv.getHeight());
}
if (_abstractStrategy == null)
{
return;
}
comboBoxStrategy.setEnabled(false);
_abstractStrategy.MakeStep();
Draw();
if (_abstractStrategy.GetStatus() == Status.Finish)
{
comboBoxStrategy.setEnabled(true);
_abstractStrategy = null;
}
});
frame.setSize(910, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(null);
canv = new Canvas();
canv.setBounds(0, 0, 895, 500 - 40); // 40 - const, высота панели сверху
buttonCreateBoat.setBounds(20, 420, 100, 40);
buttonCreateSailBoat.setBounds(140, 420, 100, 40);
buttonUp.setBounds(800, 380, 40, 40);
buttonDown.setBounds(800, 420, 40, 40);
buttonLeft.setBounds(760, 420, 40, 40);
buttonRight.setBounds(840, 420, 40, 40);
comboBoxStrategy.setBounds(800,10,100,50);
buttonStep.setBounds(800,80,100,40);
frame.add(canv);
frame.add(buttonCreateBoat);
frame.add(buttonCreateSailBoat);
frame.add(buttonUp);
frame.add(buttonDown);
frame.add(buttonLeft);
frame.add(buttonRight);
frame.add(comboBoxStrategy);
frame.add(buttonStep);
frame.setVisible(true);
}
class Canvas extends JComponent{
public DrawingBoat _drawingBoat;
public Canvas(){}
public void paintComponent(Graphics g){
if (_drawingBoat == null){
return;
}
super.paintComponents(g);
Graphics2D g2d = (Graphics2D)g;
_drawingBoat.DrawTransport(g2d);
super.repaint();
}
}
}

View File

@ -0,0 +1,6 @@
package SailBoatHard;
public class Main {
public static void main(String[] args) {
FormBoat fm = new FormBoat();
}
}

View File

@ -0,0 +1,91 @@
package SailBoatHard.MovementStrategy;;
import SailBoatHard.Direction.*;
public abstract class AbstractStrategy {
private IMoveableObject moveableObject;
private Status state = Status.NotInit;
private int fieldWidth;
protected int GetFieldWidth() {
return fieldWidth;
}
private int fieldHeight;
protected int GetFieldHeight() {
return fieldHeight;
}
public Status GetStatus() {
return state;
}
public void SetData(IMoveableObject moveableObject, int width, int height) {
if (moveableObject == null) {
state = Status.NotInit;
return;
}
state = Status.InProgress;
this.moveableObject = moveableObject;
fieldWidth = width;
fieldHeight = height;
}
public void MakeStep() {
if (state != Status.InProgress) {
return;
}
if (isTargetDestination()) {
state = Status.Finish;
return;
}
MoveToTarget();
}
protected boolean MoveLeft() {
return MoveTo(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() {
if (moveableObject == null) {
return null;
}
return moveableObject.GetObjectPosition();
}
protected Integer GetStep() {
if (state != Status.InProgress) {
return null;
}
return moveableObject.GetStep();
}
protected abstract void MoveToTarget();
protected abstract boolean isTargetDestination();
private boolean MoveTo(Direction direction) {
if (state != Status.InProgress) {
return false;
}
if (moveableObject == null) {
return false;
}
if (moveableObject.CheckCanMove(direction)) {
moveableObject.MoveObject(direction);
return true;
}
return false;
}
}

View File

@ -0,0 +1,39 @@
package SailBoatHard.MovementStrategy;
import SailBoatHard.Direction.*;
import SailBoatHard.Drawing.*;
public class DrawingObjectBoat implements IMoveableObject{
private DrawingBoat _drawingBoat = null;
public DrawingObjectBoat(DrawingBoat drawingBoat)
{
_drawingBoat = drawingBoat;
}
@Override
public ObjectParameters GetObjectPosition() {
if (_drawingBoat == null || _drawingBoat.boat == null) {
return null;
}
return new ObjectParameters(_drawingBoat.GetStartX(), _drawingBoat.GetStartY(),
_drawingBoat.GetWidth(), _drawingBoat.GetHeight());
}
@Override
public int GetStep() {
if (_drawingBoat != null && _drawingBoat.boat!=null)
return (int)(_drawingBoat.boat.GetStep());
return 0;
}
@Override
public boolean CheckCanMove(Direction direction) {
if (_drawingBoat != null)
return _drawingBoat.CanMove(direction);
return false;
}
@Override
public void MoveObject(Direction direction) {
if (_drawingBoat != null)
_drawingBoat.MoveTransport(direction);
}
}

View File

@ -0,0 +1,12 @@
package SailBoatHard.MovementStrategy;
import SailBoatHard.Direction.*;
public interface IMoveableObject{
ObjectParameters GetObjectPosition();
int GetStep();
boolean CheckCanMove(Direction direction);
void MoveObject(Direction direction);
}

View File

@ -0,0 +1,34 @@
package SailBoatHard.MovementStrategy;
public class MoveToBorder extends AbstractStrategy{
@Override
protected boolean isTargetDestination() {
var objParams = GetObjectParameters();
if (objParams == null) {
return false;
}
return objParams.RightBorder() <= GetFieldWidth() &&
objParams.RightBorder() + GetStep() >= GetFieldWidth() &&
objParams.DownBorder() <= GetFieldHeight() &&
objParams.DownBorder() + GetStep() >= GetFieldHeight();
}
@Override
protected void MoveToTarget() {
var objParams = GetObjectParameters();
if (objParams == null) {
return;
}
var diffX = objParams.ObjectMiddleHorizontal() - GetFieldWidth();
if (Math.abs(diffX) > GetStep()) {
if (diffX < 0) {
MoveRight();
}
}
var diffY = objParams.ObjectMiddleVertical() - GetFieldHeight();
if (Math.abs(diffY) > GetStep()) {
if (diffY < 0) {
MoveDown();
}
}
}
}

View File

@ -0,0 +1,38 @@
package SailBoatHard.MovementStrategy;
public class MoveToCenter extends AbstractStrategy{
@Override
protected boolean isTargetDestination() {
var objParams = GetObjectParameters();
if (objParams == null) {
return false;
}
return objParams.ObjectMiddleHorizontal() <= GetFieldWidth() / 2 &&
objParams.ObjectMiddleHorizontal() + GetStep() >= GetFieldWidth() / 2 &&
objParams.ObjectMiddleVertical() <= GetFieldHeight() / 2 &&
objParams.ObjectMiddleVertical() + GetStep() >= GetFieldHeight() / 2;
}
@Override
protected void MoveToTarget() {
var objParams = GetObjectParameters();
if (objParams == null) {
return;
}
var diffX = objParams.ObjectMiddleHorizontal() - GetFieldWidth() / 2;
if (Math.abs(diffX) > GetStep()) {
if (diffX > 0) {
MoveLeft();
} else {
MoveRight();
}
}
var diffY = objParams.ObjectMiddleVertical() - GetFieldHeight() / 2;
if (Math.abs(diffY) > GetStep()) {
if (diffY > 0) {
MoveUp();
} else {
MoveDown();
}
}
}
}

View File

@ -0,0 +1,33 @@
package SailBoatHard.MovementStrategy;
public class ObjectParameters {
private final int _x;
private final int _y;
private final int _width;
private final int _height;
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,6 @@
package SailBoatHard.MovementStrategy;
public enum Status {
NotInit,
InProgress,
Finish
}