Compare commits

..

2 Commits

Author SHA1 Message Date
40b7fc09c2 LabWork02 2024-06-08 08:00:59 +04:00
523e7d8412 LabWork01 2024-06-08 06:17:18 +04:00
27 changed files with 1007 additions and 1 deletions

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>

View File

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

View File

@ -0,0 +1,43 @@
package DifferentDrawingPaddles;
import java.awt.*;
public class DrawingPaddlesType1 implements IDifferentPaddles {
private NumberOfPaddles numberOfPaddles;
@Override
public void setNumberOfPaddles(int numberofpaddle) {
for (NumberOfPaddles numofenum : NumberOfPaddles.values()) {
if (numofenum.getNumpaddles() == numberofpaddle) {
numberOfPaddles = numofenum;
return;
}
}
}
@Override
public NumberOfPaddles getNumberOfPaddles() {
return numberOfPaddles;
}
@Override
public void DrawPaddles(Graphics2D g, int x, int y, int width, int height, Color bodyColor) {
g.setColor(Color.BLACK);
// Весло слева
int paddleLeftX1 = x + 35;
int paddleLeftY1 = y + 10;
int paddleLeftX2 = x + 35;
int paddleLeftY2 = y - 10;
g.drawLine(paddleLeftX1, paddleLeftY1, paddleLeftX2, paddleLeftY2);
// Весло справа
int paddleRightX1 = x + 35;
int paddleRightY1 = y + 60;
int paddleRightX2 = x + 35;
int paddleRightY2 = y + 80;
g.drawLine(paddleRightX1, paddleRightY1, paddleRightX2, paddleRightY2);
// Лопасти весел
g.setColor(Color.BLACK);
g.fillOval(paddleLeftX2 - 5, paddleLeftY2 - 5, 10, 10);
g.fillOval(paddleRightX2 - 5, paddleRightY2 - 5, 10, 10);
}
}

View File

@ -0,0 +1,43 @@
package DifferentDrawingPaddles;
import java.awt.*;
public class DrawingPaddlesType2 implements IDifferentPaddles {
private NumberOfPaddles numberOfPaddles;
@Override
public void setNumberOfPaddles(int numberofpaddle) {
for (NumberOfPaddles numofenum : NumberOfPaddles.values()) {
if (numofenum.getNumpaddles() == numberofpaddle) {
numberOfPaddles = numofenum;
return;
}
}
}
@Override
public NumberOfPaddles getNumberOfPaddles() {
return numberOfPaddles;
}
@Override
public void DrawPaddles(Graphics2D g, int x, int y, int width, int height, Color bodyColor) {
g.setColor(Color.BLACK);
// Весло слева
int paddleLeftX1 = x + 35;
int paddleLeftY1 = y + 10;
int paddleLeftX2 = x + 25;
int paddleLeftY2 = y - 10;
g.drawLine(paddleLeftX1, paddleLeftY1, paddleLeftX2, paddleLeftY2);
// Весло справа
int paddleRightX1 = x + 35;
int paddleRightY1 = y + 60;
int paddleRightX2 = x + 25;
int paddleRightY2 = y + 80;
g.drawLine(paddleRightX1, paddleRightY1, paddleRightX2, paddleRightY2);
// Лопасти весел
g.setColor(Color.BLACK);
g.fillOval(paddleLeftX2 - 5, paddleLeftY2 - 5, 10, 10);
g.fillOval(paddleRightX2 - 5, paddleRightY2 - 5, 10, 10);
}
}

View File

@ -0,0 +1,42 @@
package DifferentDrawingPaddles;
import java.awt.*;
public class DrawingPaddlesType3 implements IDifferentPaddles {
private NumberOfPaddles numberOfPaddles;
@Override
public void setNumberOfPaddles(int numberofpaddle) {
for (NumberOfPaddles numofenum : NumberOfPaddles.values()) {
if (numofenum.getNumpaddles() == numberofpaddle) {
numberOfPaddles = numofenum;
return;
}
}
}
@Override
public NumberOfPaddles getNumberOfPaddles() {
return numberOfPaddles;
}
@Override
public void DrawPaddles(Graphics2D g, int x, int y, int width, int height, Color bodyColor) {
g.setColor(Color.BLACK);
// Весло слева
int paddleLeftX1 = x + 35;
int paddleLeftY1 = y + 10;
int paddleLeftX2 = x + 45;
int paddleLeftY2 = y - 10;
g.drawLine(paddleLeftX1, paddleLeftY1, paddleLeftX2, paddleLeftY2);
// Весло справа
int paddleRightX1 = x + 35;
int paddleRightY1 = y + 60;
int paddleRightX2 = x + 45;
int paddleRightY2 = y + 80;
g.drawLine(paddleRightX1, paddleRightY1, paddleRightX2, paddleRightY2);
// Лопасти весел
g.setColor(Color.BLACK);
g.fillOval(paddleLeftX2 - 5, paddleLeftY2 - 5, 10, 10);
g.fillOval(paddleRightX2 - 5, paddleRightY2 - 5, 10, 10);
}
}

View File

@ -0,0 +1,10 @@
package DifferentDrawingPaddles;
import java.awt.*;
public interface IDifferentPaddles {
void setNumberOfPaddles(int numberofpaddle);
NumberOfPaddles getNumberOfPaddles();
void DrawPaddles(Graphics2D g, int x, int y, int width, int height, Color bodyColor);
}

View File

@ -0,0 +1,13 @@
package DifferentDrawingPaddles;
public enum NumberOfPaddles {
OnePaddle(1),
TwoPaddles(2),
ThreePaddles(3);
private int numberofpaddles;
NumberOfPaddles(int numberofpaddles) {
this.numberofpaddles = numberofpaddles;
}
public int getNumpaddles() {return numberofpaddles;}
}

View File

@ -0,0 +1,20 @@
package DrawingBoat;
import javax.swing.*;
import java.awt.*;
public class CanvasMotorBoat extends JComponent {
public DrawingBoat _drawingBoat;
public CanvasMotorBoat(){}
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,10 @@
package DrawingBoat;
public enum DirectionType {
Unknown,
Up,
Down,
Left,
Right
}

View File

@ -0,0 +1,166 @@
package DrawingBoat;
import DifferentDrawingPaddles.DrawingPaddlesType1;
import DifferentDrawingPaddles.DrawingPaddlesType2;
import DifferentDrawingPaddles.DrawingPaddlesType3;
import DifferentDrawingPaddles.IDifferentPaddles;
import Entities.EntityBoat;
import javax.swing.*;
import java.awt.*;
public class DrawingBoat extends JPanel {
public Entities.EntityBoat EntityBoat;
public IDifferentPaddles drawingPaddles;
private Integer picture_width;
private Integer picture_height;
public Integer _StartPosX;
public Integer _StartPosY;
private int drawingBoatWidth = 140;
protected int drawingBoatHeight = 90;
public Integer GetPosX() {
return _StartPosX;
}
public Integer GetPosY() {
return _StartPosY;
}
public Integer GetWidth() {
return drawingBoatWidth;
}
public Integer GetHeight() {
return drawingBoatHeight;
}
protected DrawingBoat() {
picture_width = null;
picture_height = null;
_StartPosX = null;
_StartPosY = null;
}
protected void SetAmountandTypePaddles() {
int numberOfPaddles = (int) (Math.random() * 4 + 0);
switch ((int) (Math.random() * 3 + 1)) {
case 1:
drawingPaddles = new DrawingPaddlesType1();
break;
case 2:
drawingPaddles = new DrawingPaddlesType2();
break;
case 3:
drawingPaddles = new DrawingPaddlesType3();
break;
default:
numberOfPaddles = 0;
break;
}
drawingPaddles.setNumberOfPaddles(numberOfPaddles);
}
public DrawingBoat(int speed, double weight, Color bodycolor) {
super();
EntityBoat = new EntityBoat(speed, weight, bodycolor);
SetAmountandTypePaddles();
}
public boolean SetPictureSize(int width, int height) {
if (width < drawingBoatWidth || height < drawingBoatHeight) return false;
picture_width = width;
picture_height = height;
if (_StartPosX != null || _StartPosY != null) {
if (_StartPosX + drawingBoatWidth > picture_width) {
_StartPosX = _StartPosX - (_StartPosX + drawingBoatWidth - picture_width);
} else if (_StartPosX < 10) _StartPosX = 10;
if (_StartPosY + drawingBoatHeight + 15 > picture_height) {
_StartPosY = 15 + _StartPosY - (_StartPosY + drawingBoatHeight - picture_height);
} else if (_StartPosY < 10) _StartPosY = 10;
}
return true;
}
public void SetPosition(int x, int y) {
if (!(picture_width != null && picture_height != null)) return;
if (x + drawingBoatWidth > picture_width) {
_StartPosX = x - (x + drawingBoatWidth - picture_width);
} else if (x < 10) _StartPosX = 10;
else _StartPosX = x;
if (y + drawingBoatHeight + 15 > picture_height) {
_StartPosY = 15 + y - (y + drawingBoatHeight - picture_height);
} else if (y < 10) _StartPosY = 15;
else _StartPosY = y;
}
public boolean MoveTransport(DirectionType direction) {
if (EntityBoat == null || _StartPosX == null || _StartPosY == null) return false;
switch (direction) {
case Left:
if (_StartPosX - EntityBoat.Step > 0) {
_StartPosX -= (int) EntityBoat.Step;
}
return true;
case Up:
if (_StartPosY - (EntityBoat.Step + 15) > 0) {
_StartPosY -= (int) EntityBoat.Step;
}
return true;
case Right:
if (_StartPosX + drawingBoatWidth + (int) EntityBoat.Step < picture_width - EntityBoat.Step) {
_StartPosX += (int) EntityBoat.Step;
}
return true;
case Down:
if (_StartPosY + drawingBoatHeight + (int) EntityBoat.Step < picture_height - EntityBoat.Step) {
_StartPosY += (int) EntityBoat.Step;
}
return true;
default:
return false;
}
}
public void DrawTransport(Graphics2D g) {
if (EntityBoat == null || _StartPosX == null || _StartPosY == null) return;
int y = _StartPosY;
g.setColor(EntityBoat.getBodyColor());
if (drawingPaddles.getNumberOfPaddles() != null) {
switch (drawingPaddles.getNumberOfPaddles()) {
case OnePaddle:
drawingPaddles.DrawPaddles(g, _StartPosX + 30, y, 100, 15, EntityBoat.getBodyColor());
break;
case TwoPaddles:
drawingPaddles.DrawPaddles(g, _StartPosX + 30, y, 100, 15, EntityBoat.getBodyColor());
drawingPaddles.DrawPaddles(g, _StartPosX + 45, y, 100, 15, EntityBoat.getBodyColor());
break;
case ThreePaddles:
drawingPaddles.DrawPaddles(g, _StartPosX + 15, y, 100, 15, EntityBoat.getBodyColor());
drawingPaddles.DrawPaddles(g, _StartPosX + 30, y, 100, 15, EntityBoat.getBodyColor());
drawingPaddles.DrawPaddles(g, _StartPosX + 45, y, 100, 15, EntityBoat.getBodyColor());
break;
}
}
Point[] hull = new Point[]{
new Point(_StartPosX + 5, _StartPosY),
new Point(_StartPosX + 100, _StartPosY),
new Point(_StartPosX + 140, _StartPosY + 35),
new Point(_StartPosX + 100, _StartPosY + 70),
new Point(_StartPosX + 5, _StartPosY + 70),
};
Polygon hullPolygon = new Polygon();
for (Point point : hull) {
hullPolygon.addPoint(point.x, point.y);
}
g.setColor(EntityBoat.getBodyColor());
g.fillPolygon(hullPolygon);
g.setColor(Color.BLACK);
g.drawPolygon(hullPolygon);
}
}

View File

@ -0,0 +1,49 @@
package DrawingBoat;
import Entities.EntityMotorBoat;
import java.awt.*;
public class DrawingMotorBoat extends DrawingBoat {
public DrawingMotorBoat(int speed, double weight, Color bodycolor, Color additionalcolor, boolean glass, boolean engine) {
EntityBoat = new EntityMotorBoat(speed, weight, bodycolor, additionalcolor, glass, engine);
SetAmountandTypePaddles();
}
@Override
public void DrawTransport(Graphics2D g) {
if (EntityBoat == null || !(EntityBoat instanceof EntityMotorBoat motorboat) || _StartPosX == null || _StartPosY == null)
return;
int y = _StartPosY;
super.DrawTransport(g);
int count_paddles = 0;
if (drawingPaddles.getNumberOfPaddles() != null) {
switch (drawingPaddles.getNumberOfPaddles()) {
case OnePaddle:
count_paddles = 1;
break;
case TwoPaddles:
count_paddles = 2;
break;
case ThreePaddles:
count_paddles = 3;
break;
}
}
if (motorboat.Glass) {
//стекло
g.setColor(Color.blue);
g.fillRect(_StartPosX + 20, _StartPosY + 15, 80, 40);
g.setColor(Color.BLACK);
g.drawRect(_StartPosX + 20, _StartPosY + 15, 80, 40);
}
if (motorboat.Engine) {
g.setColor(motorboat.getAdditionalColor());
g.fillRect(_StartPosX, _StartPosY + 10, 5, 50);
g.setColor(Color.BLACK);
g.drawRect(_StartPosX, _StartPosY + 10, 5, 50);
}
}
}

View File

@ -0,0 +1,19 @@
package Entities;
import java.awt.*;
public class EntityBoat {
private int Speed;
private double Weight;
private Color BodyColor;
public Color getBodyColor() {return BodyColor;}
public double Step;
public EntityBoat(int speed, double weight, Color bodycolor)
{
Speed = speed;
Weight = weight;
BodyColor = bodycolor;
Step = Speed * 100 / Weight;
}
}

View File

@ -0,0 +1,18 @@
package Entities;
import java.awt.*;
public class EntityMotorBoat extends EntityBoat{
public Color AdditionalColor;
public Color getAdditionalColor() {return AdditionalColor;}
public boolean Glass;
public boolean Engine;
public EntityMotorBoat(int speed, double weight, Color bodyColor, Color additionalcolor, boolean engine, boolean glass)
{
super(speed, weight, bodyColor);
AdditionalColor = additionalcolor;
Glass = glass;
Engine = engine;
}
}

View File

@ -0,0 +1,207 @@
import DrawingBoat.CanvasMotorBoat;
import DrawingBoat.DirectionType;
import DrawingBoat.DrawingBoat;
import DrawingBoat.DrawingMotorBoat;
import MovementStrategy.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.util.Random;
public class FormMotorBoat extends JFrame {
private String title;
private Dimension dimension;
private int Width, Height;
public CanvasMotorBoat canvasMotorBoat = new CanvasMotorBoat();
private JButton CreateButton = new JButton("Create MotorBoat");;
private JButton CreateShipButton = new JButton("Create Boat");
private JButton UpButton = new JButton();
private JButton DownButton = new JButton();;
private JButton LeftButton = new JButton();;
private JButton RightButton = new JButton();
private AbstractStrategy _strategy;
private JComboBox ComboBoxStrategy = new JComboBox(new String[]{"К центру", "К краю"});
private JButton ButtonStrategy = new JButton("Шаг");
public FormMotorBoat(String title, Dimension dimension) {
this.title = title;
this.dimension = dimension;
}
private void CreateObject(String typeOfClass) {
int StartPositionX = (int)(Math.random() * 90 + 10);
int StartPositionY = (int)(Math.random() * 90 + 10);
int speed = (int)(Math.random() * 300 + 100);
double weight = (double)(Math.random() * 3000 + 1000);
Color bodyColor = new Color((int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0));
switch (typeOfClass) {
case "DrawingBoat":
canvasMotorBoat._drawingBoat = new DrawingBoat(speed, weight, bodyColor);
canvasMotorBoat._drawingBoat.SetPictureSize(Width, Height);
canvasMotorBoat._drawingBoat.SetPosition(StartPositionX, StartPositionY);
canvasMotorBoat.repaint();
break;
case "DrawingMotorBoat":
Color additionalColor = new Color((int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0));;
boolean glass = new Random().nextBoolean();
boolean engine = new Random().nextBoolean();;
canvasMotorBoat._drawingBoat = new DrawingMotorBoat(speed, weight, bodyColor, additionalColor, glass, engine);
canvasMotorBoat._drawingBoat.SetPictureSize(Width, Height);
canvasMotorBoat._drawingBoat.SetPosition(StartPositionX, StartPositionY);
canvasMotorBoat.repaint();
break;
default: return;
}
_strategy = null;
ComboBoxStrategy.setEnabled(true);
}
public void Init() {
setTitle(title);
setMinimumSize(dimension);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Width = getWidth() - 10;
Height = getHeight() - 34;
_strategy = null;
CreateButton.setName("CREATE");
CreateShipButton.setName("CREATEBOATBUTTON");
Icon iconUp = new ImageIcon("src\\images\\up.png");
UpButton.setIcon(iconUp);
UpButton.setName("UP");
DownButton.setName("DOWN");
Icon iconDown = new ImageIcon("src\\images\\down.png");
DownButton.setIcon(iconDown);
LeftButton.setName("LEFT");
Icon iconLeft = new ImageIcon("src\\images\\left.png");
LeftButton.setIcon(iconLeft);
RightButton.setName("RIGHT");
Icon iconRight = new ImageIcon("src\\images\\right.png");
RightButton.setIcon(iconRight);
CreateButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
CreateObject("DrawingMotorBoat");
}
});
CreateShipButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
CreateObject("DrawingBoat");
}
});
ButtonStrategy.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (canvasMotorBoat._drawingBoat == null) return;
if (ComboBoxStrategy.isEnabled())
{
int index = ComboBoxStrategy.getSelectedIndex();
switch(index)
{
case 0:
_strategy = new MoveToCenter();
break;
case 1:
_strategy = new MoveToBorder();
break;
default:
_strategy = null;
break;
};
if (_strategy == null)
{
return;
}
_strategy.SetData(new MoveableBoat(canvasMotorBoat._drawingBoat), Width, Height);
}
if (_strategy == null)
{
return;
}
ComboBoxStrategy.setEnabled(false);
_strategy.MakeStep();
if (_strategy.GetStatus() == StrategyStatus.Finish)
{
ComboBoxStrategy.setEnabled(true);
_strategy = null;
}
}
});
ActionListener actionListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
if (canvasMotorBoat._drawingBoat == null) return;
boolean result = false;
switch ((((JButton)(event.getSource())).getName())) {
case "UP":
result = canvasMotorBoat._drawingBoat.MoveTransport(DirectionType.Up);
break;
case "DOWN":
result = canvasMotorBoat._drawingBoat.MoveTransport(DirectionType.Down);
break;
case "LEFT":
result = canvasMotorBoat._drawingBoat.MoveTransport(DirectionType.Left);
break;
case "RIGHT":
result = canvasMotorBoat._drawingBoat.MoveTransport(DirectionType.Right);
break;
}
if (result) {
canvasMotorBoat.repaint();
}
}
};
UpButton.addActionListener(actionListener);
DownButton.addActionListener(actionListener);
LeftButton.addActionListener(actionListener);
RightButton.addActionListener(actionListener);
setSize(dimension.width,dimension.height);
setLayout(null);
canvasMotorBoat.setBounds(0,0, getWidth(), getHeight());
CreateButton.setBounds(10, getHeight() - 90, 140, 40);
CreateShipButton.setBounds(160, getHeight() - 90, 140, 40);
UpButton.setBounds(getWidth() - 140, getHeight() - 160, 35, 34);
DownButton.setBounds(getWidth() - 140, getHeight() - 100, 35, 34);
RightButton.setBounds(getWidth() - 80, getHeight() - 100, 35, 34);
LeftButton.setBounds(getWidth() - 200, getHeight() - 100, 35, 34);
ComboBoxStrategy.setBounds(getWidth() - 170, 10, 140, 35);
ButtonStrategy.setBounds(getWidth() - 130, 55, 100, 25);
add(CreateButton);
add(CreateShipButton);
add(UpButton);
add(DownButton);
add(RightButton);
add(LeftButton);
add(ComboBoxStrategy);
add(ButtonStrategy);
add(canvasMotorBoat);
setVisible(true);
//обработка события изменения размеров окна
addComponentListener(new ComponentAdapter() {
public void componentResized(ComponentEvent e) {
Width = getWidth() - 10;
Height = getHeight() - 34;
if (canvasMotorBoat._drawingBoat != null)
canvasMotorBoat._drawingBoat.SetPictureSize(Width, Height);
canvasMotorBoat.setBounds(0,0, getWidth(), getHeight());
CreateButton.setBounds(10, getHeight() - 90, 140, 40);
CreateShipButton.setBounds(160, getHeight() - 90, 140, 40);
UpButton.setBounds(getWidth() - 140, getHeight() - 160, 35, 34);
DownButton.setBounds(getWidth() - 140, getHeight() - 100, 35, 34);
RightButton.setBounds(getWidth() - 80, getHeight() - 100, 35, 34);
LeftButton.setBounds(getWidth() - 200, getHeight() - 100, 35, 34);
ComboBoxStrategy.setBounds(getWidth() - 170, 10, 140, 35);
ButtonStrategy.setBounds(getWidth() - 130, 55, 100, 25);
}
});
}
}

View File

@ -1,5 +1,8 @@
import java.awt.*;
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
System.out.println("Hello world!"); FormMotorBoat form = new FormMotorBoat("Моторная лодка", new Dimension(700,500));
form.Init();
} }
} }

View File

@ -0,0 +1,57 @@
package MovementStrategy;
public abstract class AbstractStrategy {
private IMoveableObjects _moveableObject;
private StrategyStatus _state = StrategyStatus.NotInit;
public int FieldWidth;
public int FieldHeight;
public StrategyStatus GetStatus() {return _state;}
public void SetData(IMoveableObjects moveableObjects, int width, int height)
{
if (moveableObjects == null)
{
_state = StrategyStatus.NotInit;
return;
}
_state = StrategyStatus.InProgress;
_moveableObject = moveableObjects;
FieldWidth = width;
FieldHeight = height;
}
public void MakeStep()
{
if (_state != StrategyStatus.InProgress) return;
if (IsTargetDestinaion())
{
_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 Integer GetStep()
{
if (_state != StrategyStatus.InProgress)
{
return null;
}
return _moveableObject.GetStep();
}
protected abstract void MoveToTarget();
protected abstract boolean IsTargetDestinaion();
private boolean MoveTo(MovementDirection movementDirection)
{
if (_state != StrategyStatus.InProgress)
{
return false;
}
boolean stateTryMoveObject = _moveableObject.TryMoveObject(movementDirection);
if (stateTryMoveObject) return stateTryMoveObject;
return false;
}
}

View File

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

View File

@ -0,0 +1,28 @@
package MovementStrategy;
public class MoveToBorder extends AbstractStrategy{
@Override
protected boolean IsTargetDestinaion() {
ObjectParameters objParams = GetObjectParameters();
if (objParams == null)
{
return false;
}
return objParams.RightBorder + GetStep() >= FieldWidth-GetStep() &&
objParams.DownBorder + GetStep() >= FieldHeight-GetStep();
}
@Override
protected void MoveToTarget() {
ObjectParameters objParams = GetObjectParameters();
if (objParams == null)
{
return;
}
//реализация в правый нижний угол
int x = objParams.RightBorder;
if (x + GetStep() < FieldWidth) MoveRight();
int y = objParams.DownBorder;
if (y + GetStep() < FieldHeight) MoveDown();
}
}

View File

@ -0,0 +1,49 @@
package MovementStrategy;
public class MoveToCenter extends AbstractStrategy{
@Override
protected boolean IsTargetDestinaion() {
ObjectParameters 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;
}
@Override
protected void MoveToTarget() {
ObjectParameters objParams = GetObjectParameters();
if (objParams == null)
{
return;
}
int diffX = objParams.ObjectMiddleHorizontal - FieldWidth / 2;
if (Math.abs(diffX) > GetStep())
{
if (diffX > 0)
{
MoveLeft();
}
else
{
MoveRight();
}
}
int diffY = objParams.ObjectMiddleVertical - FieldHeight / 2;
if (Math.abs(diffY) > GetStep())
{
if (diffY > 0)
{
MoveUp();
}
else
{
MoveDown();
}
}
}
}

View File

@ -0,0 +1,46 @@
package MovementStrategy;
import DrawingBoat.CanvasMotorBoat;
import DrawingBoat.DirectionType;
import DrawingBoat.DrawingBoat;
public class MoveableBoat implements IMoveableObjects{
private CanvasMotorBoat canvas = new CanvasMotorBoat();
public MoveableBoat(DrawingBoat drawingboat)
{
canvas._drawingBoat = drawingboat;
}
@Override
public ObjectParameters GetObjectPosition() {
if (canvas._drawingBoat == null || canvas._drawingBoat.EntityBoat == null ||
canvas._drawingBoat.GetPosX() == null || canvas._drawingBoat.GetPosY() == null)
{
return null;
}
return new ObjectParameters(canvas._drawingBoat.GetPosX(), canvas._drawingBoat.GetPosY(),
canvas._drawingBoat.GetWidth(), canvas._drawingBoat.GetHeight());
}
@Override
public int GetStep() {
return (int)(canvas._drawingBoat.EntityBoat.Step);
}
@Override
public boolean TryMoveObject(MovementDirection direction) {
if (canvas._drawingBoat == null || canvas._drawingBoat.EntityBoat == null)
{
return false;
}
return canvas._drawingBoat.MoveTransport(GetDirectionType(direction));
}
private static DirectionType GetDirectionType(MovementDirection direction)
{
switch (direction) {
case MovementDirection.Left: return DirectionType.Left;
case MovementDirection.Right: return DirectionType.Right;
case MovementDirection.Up: return DirectionType.Up;
case MovementDirection.Down: return DirectionType.Down;
default: return DirectionType.Unknown;
}
}
}

View File

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

View File

@ -0,0 +1,28 @@
package MovementStrategy;
public class ObjectParameters {
private int _x;
private int _y;
private int _width;
private int _height;
public int LeftBorder = _x;
public int TopBorder = _y;
public int RightBorder = _x + _width;
public int DownBorder = _y + _height;
public int ObjectMiddleHorizontal = _x + _width / 2;
public int ObjectMiddleVertical = _y + _height / 2;
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;
}
}

View File

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

Binary file not shown.

After

Width:  |  Height:  |  Size: 407 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 463 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 446 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 413 B