Лабораторная 2

This commit is contained in:
Никита Белянин 2023-10-24 01:01:37 +04:00
parent 3c072a98a3
commit e8a6638e15
31 changed files with 1133 additions and 437 deletions

9
.idea/Git.iml Normal file
View File

@ -0,0 +1,9 @@
<?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$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

29
Tank/.gitignore vendored
View File

@ -1,29 +0,0 @@
### IntelliJ IDEA ###
out/
!**/src/main/**/out/
!**/src/test/**/out/
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store

View File

@ -1,8 +1,3 @@
# Default ignored files # Default ignored files
/shelf/ /shelf/
/workspace.xml /workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

View File

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

124
Tank/.idea/uiDesigner.xml 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>

View File

@ -0,0 +1,82 @@
public abstract class AbstractStrategy {
// Перемещаемый объект
private IMoveableObject _moveableObject;
// Статус перемещения
private Status _state = Status.NotInit;
// Ширина поля
protected int FieldWidth;
// Высота поля
protected int FieldHeight;
// Статус перемещения
public Status GetStatus() { return _state; }
// Установка данных
public void SetData(IMoveableObject moveableObject, int width, int height) {
if (moveableObject == null) {
_state = Status.NotInit;
return;
}
_state = Status.InProgress;
_moveableObject = moveableObject;
FieldWidth = width;
FieldHeight = height;
}
// Шаг перемещения
public void MakeStep() {
if (_state != Status.InProgress) {
return;
}
if (IsTargetDestinaion()) {
_state = Status.Finish;
return;
}
MoveToTarget();
}
// Перемещение влево
protected boolean MoveLeft() { return MoveTo(Direction.Left);}
// Перемещение вправо
protected boolean MoveRight() { return MoveTo(Direction.Right);}
// Перемещение вверх
protected boolean MoveUp() { return MoveTo(Direction.Up);}
// Перемещение вниз
protected boolean MoveDown() { return MoveTo(Direction.Down);}
// Параметры объекта
protected ObjectParameters GetObjectParameters() { return _moveableObject.GetObjectPosition(); }
// Шаг объекта
protected int GetStep() {
if (_state != Status.InProgress) {
return 0;
}
return _moveableObject.GetStep();
}
// Перемещение к цели
protected abstract void MoveToTarget();
// Достигнута ли цель
protected abstract boolean IsTargetDestinaion();
// Попытка перемещения в требуемом направлении
private boolean MoveTo(Direction Direction) {
if (_state != Status.InProgress) {
return false;
}
if (_moveableObject.CheckCanMove(Direction)) {
_moveableObject.MoveObject(Direction);
return true;
}
return false;
}
}

View File

@ -10,5 +10,4 @@ public enum CountWheels {
public int getCountWheels(){ public int getCountWheels(){
return Value; return Value;
} }
public int setCountWheels() { return Value; }
} }

View File

@ -1,6 +1,6 @@
public enum Direction { public enum Direction {
Up, Up,
Down,
Left, Left,
Down,
Right Right
} }

View File

@ -0,0 +1,128 @@
import java.awt.*;
import java.util.Random;
public class DrawingArmoVehicle {
protected IOrnamentForm OrnamentsForm;
public EntityArmoVehicle ArmoVehicle;
public EntityArmoVehicle getTank() {
return ArmoVehicle;
}
protected int _pictureWidth;
protected int _pictureHeight;
protected int _startPosX;
protected int _startPosY;
protected int _TankWidth = 160;
protected int _TankHeight = 55;
public DrawingArmoVehicle(int speed, double weight, Color bodyColor, int _numWheel, int width, int height) {
_pictureWidth = width;
_pictureHeight = height;
if (_pictureHeight < _TankHeight || _pictureWidth < _TankWidth)
return;
ArmoVehicle = new EntityArmoVehicle(speed, weight, bodyColor, _numWheel);
Random random = new Random();
switch(random.nextInt(0, 3)) {
case 0:
OrnamentsForm = new DrawingWheelsCombination();
break;
case 1:
OrnamentsForm = new DrawingAsteriskOrnament();
break;
case 2:
OrnamentsForm = new DrawingSuspensionOrnament();
break;
default:
OrnamentsForm = new DrawingWheelsCombination();
break;
}
OrnamentsForm.setDigit(_numWheel);
}
public void SetPosition(int x, int y) {
_startPosX = Math.min(x, _pictureWidth - _TankWidth);
_startPosY = Math.min(y, _pictureHeight - _TankHeight);
}
public int GetPosX (){ return _startPosX; }
public int GetPosY (){ return _startPosY; }
public int GetWidth (){ return _TankWidth; }
public int GetHeight (){return _TankHeight;}
public boolean CanMove(Direction direction) {
if (ArmoVehicle == null) {
return false;
}
switch (direction) {
case Left:
return _startPosX - ArmoVehicle.Step > 0;
case Right:
return _startPosX + _TankWidth + ArmoVehicle.Step < _pictureWidth;
case Up:
return _startPosY - ArmoVehicle.Step > 0;
case Down:
return _startPosY + _TankHeight + ArmoVehicle.Step < _pictureHeight;
default:
return false;
}
}
public void MoveTransport(Direction direction) {
if (!CanMove(direction) || ArmoVehicle == null) {
return;
}
switch (direction) {
//влево
case Left:
_startPosX -= (int)ArmoVehicle.Step;
break;
//вверх
case Up:
_startPosY -= (int)ArmoVehicle.Step;
break;
// вправо
case Right:
_startPosX += (int)ArmoVehicle.Step;
break;
//вниз
case Down:
_startPosY += (int)ArmoVehicle.Step;
break;
}
}
public void SetWheels(int number) {
OrnamentsForm.setDigit(number);
}
public void DrawTransport(Graphics2D g) {
if (ArmoVehicle == null) {
return;
}
g.setColor(Color.BLUE);
int[] xPoints = {_startPosX + 5, _startPosX + 140, _startPosX + 130,_startPosX + 12};
int[] yPoints = {_startPosY + 30, _startPosY + 30, _startPosY + 42, _startPosY + 42};
int nPoints = 4;
g.drawPolygon(xPoints,yPoints,nPoints);
g.fillPolygon(xPoints,yPoints,nPoints);
//wheels
OrnamentsForm.Draw(g, _startPosX, _startPosY);
}
public void ChangeBorders(int width,int height) {
_pictureWidth = width;
_pictureHeight = height;
if (_pictureWidth<=_TankWidth||_pictureHeight<=_TankHeight) {
_pictureWidth = Integer.parseInt(null);
_pictureHeight = Integer.parseInt(null);
return;
}
if (_startPosX + _TankWidth > _pictureWidth) {
_startPosX = _pictureWidth - _TankWidth;
}
if (_startPosY + _TankHeight > _pictureHeight) {
_startPosY = _pictureHeight - _TankHeight;
}
}
}

View File

@ -0,0 +1,85 @@
import java.awt.*;
public class DrawingAsteriskOrnament implements IOrnamentForm {
private CountWheels wheels;
private Color additionalColor;
public CountWheels getNumWheel() {
return wheels;
}
public void setDigit(int number) {
switch(number){
case 2:
wheels = CountWheels.Two;
break;
case 3:
wheels = CountWheels.Three;
break;
case 4:
wheels = CountWheels.Four;
break;
default:
wheels = CountWheels.Two;
break;
}
}
public void CaterpillarStar(Graphics g, int _startPosX, int _startPosY) {
additionalColor = Color.RED;
g.setColor(additionalColor);
int xPontsStar[] = {_startPosX + 15, _startPosX + 18, _startPosX + 21, _startPosX + 18, _startPosX + 19, _startPosX + 16, _startPosX + 12, _startPosX + 12, _startPosX + 11, _startPosX + 15, _startPosX + 16};
int yPontsStar[] = {_startPosY + 35, _startPosY + 38, _startPosY + 38, _startPosY + 42, _startPosY + 45, _startPosY + 42, _startPosY + 45, _startPosY + 41, _startPosY + 38, _startPosY + 38, _startPosY + 35};
g.drawPolygon(xPontsStar, yPontsStar, xPontsStar.length);
g.fillPolygon(xPontsStar, yPontsStar, xPontsStar.length);
}
public void DrawWheels(Graphics g, int _startPosX, int _startPosY){
g.setColor(Color.BLACK);
g.fillOval(10 + _startPosX, 42 + _startPosY, 20, 20);
}
public void Draw(Graphics g, int _startPosX, int _startPosY) {
if (wheels == CountWheels.Two){
DrawWheels(g,_startPosX, _startPosY);
DrawWheels(g,_startPosX + 100, _startPosY);
CaterpillarStar(g,_startPosX + 5, _startPosY + 12);
CaterpillarStar(g,_startPosX + 105, _startPosY + 12);
}
if (wheels == CountWheels.Three) {
DrawWheels(g,_startPosX, _startPosY);
DrawWheels(g,_startPosX + 50, _startPosY);
DrawWheels(g,_startPosX + 100, _startPosY);
CaterpillarStar(g,_startPosX + 5, _startPosY + 12);
CaterpillarStar(g,_startPosX + 55, _startPosY + 12);
CaterpillarStar(g,_startPosX + 105, _startPosY + 12);
}
if (wheels == CountWheels.Four) {
DrawWheels(g,_startPosX, _startPosY);
DrawWheels(g,_startPosX + 25, _startPosY);
DrawWheels(g,_startPosX + 50, _startPosY);
DrawWheels(g,_startPosX + 100, _startPosY);
CaterpillarStar(g,_startPosX + 5, _startPosY + 12);
CaterpillarStar(g,_startPosX + 30, _startPosY + 12);
CaterpillarStar(g,_startPosX + 55, _startPosY + 12);
CaterpillarStar(g,_startPosX + 105, _startPosY + 12);
}
if(wheels == CountWheels.Five) {
DrawWheels(g,_startPosX, _startPosY);
DrawWheels(g,_startPosX + 25, _startPosY);
DrawWheels(g,_startPosX + 50, _startPosY);
DrawWheels(g,_startPosX + 75, _startPosY);
DrawWheels(g,_startPosX + 100, _startPosY);
CaterpillarStar(g,_startPosX + 5, _startPosY + 12);
CaterpillarStar(g,_startPosX + 30, _startPosY + 12);
CaterpillarStar(g,_startPosX + 55, _startPosY + 12);
CaterpillarStar(g,_startPosX + 80, _startPosY + 12);
CaterpillarStar(g,_startPosX + 105, _startPosY + 12);
}
}
}

View File

@ -1,54 +0,0 @@
import javax.swing.*;
import java.awt.*;
import java.util.Random;
public class DrawingField extends JPanel {
private final FormTank field;
DrawingTank _Tank;
public DrawingField(FormTank field) {
this.field = field;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 =(Graphics2D)g;
if (_Tank!=null)
_Tank.DrawTransport(g2);
else return;
}
public void UpButtonAction(){
if (_Tank!=null)
_Tank.MoveTransport(Direction.Up);
else
return;
}
public void DownButtonAction(){
if (_Tank!=null)
_Tank.MoveTransport(Direction.Down);
else
return;
}
public void RightButtonAction(){
if (_Tank!=null)
_Tank.MoveTransport(Direction.Right);
else
return;
}
public void LeftButtonAction(){
if (_Tank!=null)
_Tank.MoveTransport(Direction.Left);
else
return;
}
public void CreateButtonAction(){
Random rnd=new Random();
_Tank=new DrawingTank();
_Tank.Init(rnd.nextInt(50)+10,rnd.nextInt(100)+500,new Color(rnd.nextInt(256),rnd.nextInt(256),rnd.nextInt(256)));
_Tank.SetPosition(rnd.nextInt(100)+10,rnd.nextInt(100)+10,getWidth(),getHeight());
}
public void ResizeField(){
if (_Tank!=null)
_Tank.ChangeBorders(getWidth(),getHeight());
else return;
}
}

View File

@ -0,0 +1,17 @@
public class DrawingObjectTank implements IMoveableObject {
private DrawingArmoVehicle _ArmoVehicle = null;
public DrawingObjectTank(DrawingArmoVehicle drawingTrain)
{
_ArmoVehicle = drawingTrain;
}
public ObjectParameters GetObjectPosition() {
if (_ArmoVehicle == null || _ArmoVehicle.ArmoVehicle == null) {
return null;
}
return new ObjectParameters(_ArmoVehicle.GetPosX(),_ArmoVehicle.GetPosY(),
_ArmoVehicle.GetWidth(), _ArmoVehicle.GetHeight());
}
public int GetStep(){ return (int)_ArmoVehicle.ArmoVehicle.Step; }
public boolean CheckCanMove(Direction direction) { return _ArmoVehicle.CanMove(direction);}
public void MoveObject(Direction direction) { _ArmoVehicle.MoveTransport(direction); }
}

View File

@ -0,0 +1,65 @@
import java.awt.*;
public class DrawingSuspensionOrnament implements IOrnamentForm {
private CountWheels wheels;
private Color additionalColor;
public CountWheels getNumWheel() {
return wheels;
}
public void setDigit(int number) {
switch(number){
case 2:
wheels = CountWheels.Two;
break;
case 3:
wheels = CountWheels.Three;
break;
case 4:
wheels = CountWheels.Four;
break;
default:
wheels = CountWheels.Two;
break;
}
}
public void DrawWheels(Graphics g, int _startPosX, int _startPosY) {
g.setColor(Color.BLACK);
g.fillOval(10 + _startPosX, 42 + _startPosY, 20, 20);
}
public void Draw(Graphics g, int _startPosX, int _startPosY) {
if (wheels == CountWheels.Two){
DrawWheels(g,_startPosX, _startPosY);
DrawWheels(g,_startPosX + 100, _startPosY);
additionalColor = Color.RED;
g.setColor(additionalColor);
g.fillRect(_startPosX + 27, _startPosY + 50, 10, 3);
g.fillRect(_startPosX + 100, _startPosY + 50, 10, 3);
}
if (wheels == CountWheels.Three) {
DrawWheels(g,_startPosX, _startPosY);
DrawWheels(g,_startPosX + 50, _startPosY);
DrawWheels(g,_startPosX + 100, _startPosY);
g.fillRect(_startPosX + 27, _startPosY + 50, 10, 3);
g.fillRect(_startPosX + 51, _startPosY + 50, 10, 3);
g.fillRect(_startPosX + 100, _startPosY + 50, 10, 3);
}
if (wheels == CountWheels.Four) {
DrawWheels(g,_startPosX, _startPosY);
DrawWheels(g,_startPosX + 25, _startPosY);
DrawWheels(g,_startPosX + 50, _startPosY);
DrawWheels(g,_startPosX + 75, _startPosY);
DrawWheels(g,_startPosX + 100, _startPosY);
g.fillRect(_startPosX + 27, _startPosY + 50, 10, 3);
g.fillRect(_startPosX + 51, _startPosY + 50, 10, 3);
g.fillRect(_startPosX + 80, _startPosY + 50, 10, 3);
g.fillRect(_startPosX + 101, _startPosY + 50, 10, 3);
}
}
}

View File

@ -1,143 +1,69 @@
import java.awt.*; import java.awt.*;
public class DrawingTank { public class DrawingTank extends DrawingArmoVehicle {
public EntityTank Tank; protected IOrnamentForm OrnamentsForm;
public EntityTank getTank() { private boolean WheelsOrnament;
return Tank; private Color AdditionalColor;
} private boolean OrnamentAdd;
public DrawingWheels Wheels;
private int _startPosX;
private int _startPosY;
private Integer _pictureWidth = null;
private Integer _pictureHeight = null;
private final int _TankWidth = 175;
private final int _TankHeight = 100;
public void Init(int speed, float weight, Color bodyColor) public DrawingTank(int speed, double weight, Color bodyColor, int _numWheel, Color additionalColor, boolean bodyKit, boolean caterpillar, boolean tower, int width, int height, boolean wheelsOrnament, boolean ornamentAdd)
{ {
Tank = new EntityTank(); super(speed, weight, bodyColor, _numWheel, width, height);
Tank.Init(speed, weight, bodyColor); ArmoVehicle = new EntityTank(speed, weight, bodyColor, _numWheel, additionalColor, bodyKit, caterpillar, tower);
Wheels = new DrawingWheels(); _TankWidth = ((EntityTank)ArmoVehicle).BodyKit ? 169 : 83;
Wheels.SetCountWheels((int)(2 + Math.random() + Math.random()*2));
this.AdditionalColor = additionalColor;
this.OrnamentAdd = ornamentAdd;
} }
public void SetPosition(int x, int y, int width, int height) public void SetPosition(int x, int y) {
{ _startPosX = Math.min(x, _pictureWidth-_TankWidth);
if (x >= 0 && x+_TankWidth <= width && y >= 0 && y+_TankHeight <= height) _startPosY = Math.min(y, _pictureHeight-_TankHeight);
{
_startPosX = x;
_startPosY = y;
_pictureWidth = width;
_pictureHeight = height;
}
} }
public void MoveTransport(Direction direction) public boolean isOrnamentAdd() {
{ return OrnamentAdd;
if (_pictureWidth == null || _pictureHeight == null) }
{ private void setOrnamentAdd(boolean ornamentAdd) {
this.OrnamentAdd = ornamentAdd;
}
public void DrawTransport(Graphics2D g) {
if (ArmoVehicle == null) {
return; return;
} }
switch (direction) super.DrawTransport(g);
{
case Right:
if (_startPosX + _TankWidth + Tank.Step < _pictureWidth)
{
_startPosX += Tank.Step;
}
break;
case Left: if (((EntityTank) ArmoVehicle).BodyKit) {
if (_startPosX- Tank.Step >= 0) g.setColor(Color.DARK_GRAY);
{ int[] xPointsBody = {_startPosX + 52, _startPosX + 52, _startPosX + 40, _startPosX + 15,_startPosX + 15, _startPosX + 60,_startPosX + 90,_startPosX + 120,_startPosX + 100,_startPosX + 95, _startPosX + 90};
_startPosX -= Tank.Step; int[] yPointsBody = {_startPosY + 30, _startPosY + 27, _startPosY + 23, _startPosY + 18,_startPosY + 15, _startPosY + 11,_startPosY + 11,_startPosY + 20,_startPosY + 25,_startPosY + 27,_startPosY + 30};
} int nPointsBody = xPointsBody.length;
break;
case Up: g.drawPolygon(xPointsBody,yPointsBody,nPointsBody);
if (_startPosY - Tank.Step >= 0) g.fillPolygon(xPointsBody,yPointsBody,nPointsBody);
{
_startPosY -= Tank.Step;
}
break;
case Down:
if (_startPosY + _TankHeight + Tank.Step < _pictureHeight)
{
_startPosY += Tank.Step;
}
break;
}
}
public void DrawTransport(Graphics g)
{
if (_startPosX < 0 || _startPosY < 0 || _pictureHeight== null || _pictureWidth== null)
{
return;
} }
Graphics2D g2 = (Graphics2D) g; if (((EntityTank) ArmoVehicle).Caterpillar) {
Color Gray = new Color(128, 128, 128);
//Гусеница g.setColor(Gray);
Color Gray = new Color(128, 128, 128); g.drawOval(_startPosX + 10, _startPosY + 30, 120, 30);
g2.setColor(Gray);
g2.drawOval(_startPosX + 10, _startPosY + 30, 120, 30);
// Ведущие колёса танка (в размере чуть меньше)
g2.setColor(Color.BLACK);
g2.drawOval(_startPosX + 113, _startPosY + 41, 11, 11);
g2.fillOval(_startPosX + 113, _startPosY + 41, 11, 11);
g2.drawOval(_startPosX + 13, _startPosY + 40, 11, 11);
g2.fillOval(_startPosX + 13, _startPosY + 40, 11, 11);
Wheels.DrawWheels(g2, _startPosX, _startPosY);
// Корпус танка
g2.setColor(Color.DARK_GRAY);
int[] xPoints = {_startPosX + 5, _startPosX + 140, _startPosX + 130,_startPosX + 12};
int[] yPoints = {_startPosY + 30, _startPosY + 30, _startPosY + 42, _startPosY + 42};
int nPoints = 4;
g2.drawPolygon(xPoints,yPoints,nPoints);
g2.fillPolygon(xPoints,yPoints,nPoints);
// Башня
int[] xPointsBody = {_startPosX + 52, _startPosX + 52, _startPosX + 40, _startPosX + 15,_startPosX + 15, _startPosX + 60,_startPosX + 90,_startPosX + 120,_startPosX + 100,_startPosX + 95, _startPosX + 90};
int[] yPointsBody = {_startPosY + 30, _startPosY + 27, _startPosY + 23, _startPosY + 18,_startPosY + 15, _startPosY + 11,_startPosY + 11,_startPosY + 20,_startPosY + 25,_startPosY + 27,_startPosY + 30};
int nPointsBody = 11;
g2.drawPolygon(xPointsBody,yPointsBody,nPointsBody);
g2.fillPolygon(xPointsBody,yPointsBody,nPointsBody);
// Орудие
g2.drawRect(_startPosX + 112, _startPosY+17, 50, 5);
g2.fillRect(_startPosX + 112, _startPosY+17, 50, 5);
// Зенитное орудие
int[] xPointsGun = {_startPosX + 45, _startPosX + 45, _startPosX + 41, _startPosX + 41, _startPosX + 42, _startPosX + 41, _startPosX + 44,_startPosX + 50 ,_startPosX + 52,_startPosX + 53, _startPosX + 58};
int[] yPointsGun = {_startPosY + 12, _startPosY + 10, _startPosY + 8, _startPosY + 7, _startPosY + 5, _startPosY + 4,_startPosY + 3,_startPosY + 3,_startPosY + 5,_startPosY + 7,_startPosY + 10};
int nPointsGun = 11;
g2.fillRect(_startPosX + 50, _startPosY+5, 20, 2);
g2.drawPolygon(xPointsGun,yPointsGun,nPointsGun);
g2.fillPolygon(xPointsGun,yPointsGun,nPointsGun);
}
public void ChangeBorders(int width,int height)
{
_pictureWidth = width;
_pictureHeight = height;
if (_pictureWidth<=_TankWidth||_pictureHeight<=_TankHeight)
{
_pictureWidth = null;
_pictureHeight = null;
return;
} }
if (_startPosX + _TankWidth > _pictureWidth)
{ if (((EntityTank) ArmoVehicle).Tower) {
_startPosX = _pictureWidth - _TankWidth; g.setColor(AdditionalColor);
} // Орудие
if (_startPosY + _TankHeight > _pictureHeight) g.drawRect(_startPosX + 112, _startPosY + 17, 60, 4);
{ g.fillRect(_startPosX + 112, _startPosY + 17, 60, 4);
_startPosY = _pictureHeight - _TankHeight;
// Зенитное орудие
int[] xPointsGun = {_startPosX + 45, _startPosX + 45, _startPosX + 41, _startPosX + 41, _startPosX + 42, _startPosX + 41, _startPosX + 44,_startPosX + 50 ,_startPosX + 52,_startPosX + 53, _startPosX + 58};
int[] yPointsGun = {_startPosY + 12, _startPosY + 10, _startPosY + 8, _startPosY + 7, _startPosY + 5, _startPosY + 4,_startPosY + 3,_startPosY + 3,_startPosY + 5,_startPosY + 7,_startPosY + 10};
int nPointsGun = xPointsGun.length;
g.fillRect(_startPosX + 50, _startPosY + 5, 20, 2);
g.drawPolygon(xPointsGun,yPointsGun,nPointsGun);
g.fillPolygon(xPointsGun,yPointsGun,nPointsGun);
} }
} }
} }

View File

@ -1,50 +0,0 @@
import java.awt.*;
public class DrawingWheels {
private CountWheels _wheels;
public void SetCountWheels(int Count){
for (CountWheels temp: CountWheels.values())
if (temp.getCountWheels() == Count){
_wheels=temp;
return;
}
}
public void DrawWheels(Graphics2D g,int _startPosX, int _startPosY) {
switch (_wheels.getCountWheels())
{
case 2:
// Отрисовка 2 катков
g.setColor(Color.BLACK);
g.drawOval(_startPosX + 30, _startPosY + 42, 15, 15);
g.fillOval(_startPosX + 30, _startPosY + 42, 15, 15);
g.drawOval(_startPosX + 50, _startPosY + 45, 15, 15);
g.fillOval(_startPosX + 50, _startPosY + 45, 15, 15);
break;
case 3:
// Отрисовка 3 катков
g.setColor(Color.BLACK);
g.drawOval(_startPosX + 30, _startPosY + 42, 15, 15);
g.fillOval(_startPosX + 30, _startPosY + 42, 15, 15);
g.drawOval(_startPosX + 50, _startPosY + 45, 15, 15);
g.fillOval(_startPosX + 50, _startPosY + 45, 15, 15);
g.drawOval(_startPosX + 75, _startPosY + 45, 15, 15);
g.fillOval(_startPosX + 75, _startPosY + 45, 15, 15);
break;
case 4:
// Отрисовка 4 катков
g.setColor(Color.BLACK);
g.drawOval(_startPosX + 30, _startPosY + 42, 15, 15);
g.fillOval(_startPosX + 30, _startPosY + 42, 15, 15);
g.drawOval(_startPosX + 50, _startPosY + 45, 15, 15);
g.fillOval(_startPosX + 50, _startPosY + 45, 15, 15);
g.drawOval(_startPosX + 75, _startPosY + 45, 15, 15);
g.fillOval(_startPosX + 75, _startPosY + 45, 15, 15);
g.drawOval(_startPosX + 95, _startPosY + 42, 15, 15);
g.fillOval(_startPosX + 95, _startPosY + 42, 15, 15);
break;
}
}
}

View File

@ -0,0 +1,104 @@
import java.awt.*;
public class DrawingWheelsCombination implements IOrnamentForm {
private CountWheels _wheels;
private CountWheels wheels;
private Color additionalColor;
public CountWheels getNumWheel() {
return wheels;
}
public void setDigit(int number) {
switch(number) {
case 2:
wheels = CountWheels.Two;
break;
case 3:
wheels = CountWheels.Three;
break;
case 4:
wheels = CountWheels.Four;
break;
default:
wheels = CountWheels.Two;
break;
}
}
public void CaterpillarStar(Graphics g, int _startPosX, int _startPosY) {
additionalColor = Color.RED;
g.setColor(additionalColor);
int xPontsStar[] = {_startPosX + 15, _startPosX + 18, _startPosX + 21, _startPosX + 18, _startPosX + 19, _startPosX + 16, _startPosX + 12, _startPosX + 13, _startPosX + 12, _startPosX + 15, _startPosX + 16};
int yPontsStar[] = {_startPosY + 35, _startPosY + 38, _startPosY + 38, _startPosY + 42, _startPosY + 45, _startPosY + 42, _startPosY + 45, _startPosY + 41, _startPosY + 38, _startPosY + 38, _startPosY + 35};
g.drawPolygon(xPontsStar, yPontsStar, xPontsStar.length);
g.fillPolygon(xPontsStar, yPontsStar, xPontsStar.length);
}
public void DrawWheels(Graphics g, int _startPosX, int _startPosY){
g.setColor(Color.BLACK);
g.fillOval(10 + _startPosX, 42 + _startPosY, 20, 20);
}
public void Draw(Graphics g, int _startPosX, int _startPosY) {
if (wheels == CountWheels.Two){
DrawWheels(g,_startPosX, _startPosY);
DrawWheels(g,_startPosX + 100, _startPosY);
g.fillRect(_startPosX + 27, _startPosY + 50, 10, 3);
g.fillRect(_startPosX + 101, _startPosY + 50, 10, 3);
CaterpillarStar(g,_startPosX + 5, _startPosY + 12);
CaterpillarStar(g,_startPosX + 105, _startPosY + 12);
}
if (wheels == CountWheels.Three) {
DrawWheels(g,_startPosX, _startPosY);
DrawWheels(g,_startPosX + 50, _startPosY);
DrawWheels(g,_startPosX + 100, _startPosY);
g.fillRect(_startPosX + 27, _startPosY + 50, 10, 3);
g.fillRect(_startPosX + 51, _startPosY + 50, 10, 3);
g.fillRect(_startPosX + 100, _startPosY + 50, 10, 3);
CaterpillarStar(g,_startPosX + 5, _startPosY + 12);
CaterpillarStar(g,_startPosX + 55, _startPosY + 12);
CaterpillarStar(g,_startPosX + 105, _startPosY + 12);
}
if (wheels == CountWheels.Four) {
DrawWheels(g,_startPosX, _startPosY);
DrawWheels(g,_startPosX + 25, _startPosY);
DrawWheels(g,_startPosX + 50, _startPosY);
DrawWheels(g,_startPosX + 100, _startPosY);
g.fillRect(_startPosX + 27, _startPosY + 50, 10, 3);
g.fillRect(_startPosX + 51, _startPosY + 50, 10, 3);
g.fillRect(_startPosX + 80, _startPosY + 50, 10, 3);
g.fillRect(_startPosX + 101, _startPosY + 50, 10, 3);
CaterpillarStar(g,_startPosX + 5, _startPosY + 12);
CaterpillarStar(g,_startPosX + 30, _startPosY + 12);
CaterpillarStar(g,_startPosX + 55, _startPosY + 12);
CaterpillarStar(g,_startPosX + 105, _startPosY + 12);
}
if(wheels == CountWheels.Five) {
DrawWheels(g,_startPosX, _startPosY);
DrawWheels(g,_startPosX + 25, _startPosY);
DrawWheels(g,_startPosX + 50, _startPosY);
DrawWheels(g,_startPosX + 75, _startPosY);
DrawWheels(g,_startPosX + 100, _startPosY);
g.fillRect(_startPosX + 27, _startPosY + 50, 10, 3);
g.fillRect(_startPosX + 51, _startPosY + 50, 10, 3);
g.fillRect(_startPosX + 80, _startPosY + 50, 10, 3);
g.fillRect(_startPosX + 101, _startPosY + 50, 10, 3);
CaterpillarStar(g,_startPosX + 5, _startPosY + 12);
CaterpillarStar(g,_startPosX + 30, _startPosY + 12);
CaterpillarStar(g,_startPosX + 55, _startPosY + 12);
CaterpillarStar(g,_startPosX + 80, _startPosY + 12);
CaterpillarStar(g,_startPosX + 105, _startPosY + 12);
}
}
}

View File

@ -0,0 +1,20 @@
import java.awt.*;
public class EntityArmoVehicle {
public int Speed;
public double Weight;
public double getWeight() {
return Weight;
}
public Color BodyColor;
public double Step;
public int numWheel;
public EntityArmoVehicle(int speed, double weight, Color bodyColor, int _numWheel) {
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
numWheel = _numWheel;
Step = (double) Speed * 300 / Weight;
}
}

View File

@ -1,27 +1,20 @@
import java.awt.*; import java.awt.*;
import java.util.Random;
public class EntityTank { public class EntityTank extends EntityArmoVehicle {
private int Speed; public Color AdditionalColor;
public int getSpeed() { public boolean BodyKit;
return Speed; public boolean Caterpillar;
} public boolean Tower;
private float Weight;
public float getWeight() {
return Weight;
}
private Color BodyColor;
public Color getBodyColor() {
return BodyColor;
}
public float Step;
public void Init(int speed, float weight, Color bodyColor){ public EntityTank(int speed, double weight, Color bodyColor, int _numWheel, Color additionalColor, boolean bodyKit, boolean caterpillar, boolean tower)
Random rnd = new Random(); {
Speed = speed <= 0 ? rnd.nextInt(50)+10 : speed; super(speed, weight, bodyColor, _numWheel);
Weight = weight <= 0 ? rnd.nextInt(100)+500 : weight; AdditionalColor = additionalColor;
BodyColor = bodyColor; BodyKit = bodyKit;
Step = Speed * 600 / (int)Weight; Caterpillar = caterpillar;
Tower = tower;
} }
} }

112
Tank/src/FormTank.form Normal file
View File

@ -0,0 +1,112 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="FormTank">
<grid id="27dc6" binding="pictureBox" layout-manager="GridLayoutManager" row-count="5" column-count="6" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="980" height="560"/>
</constraints>
<properties>
<minimumSize width="960" height="560"/>
<preferredSize width="960" height="560"/>
</properties>
<border type="none"/>
<children>
<hspacer id="f9ba0">
<constraints>
<grid row="4" column="2" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<vspacer id="ce5ea">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
<component id="ac2ff" class="javax.swing.JButton" binding="buttonDown">
<constraints>
<grid row="4" column="4" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<hideActionText value="true"/>
<horizontalTextPosition value="0"/>
<text value=""/>
</properties>
</component>
<component id="b1382" class="javax.swing.JButton" binding="buttonUp">
<constraints>
<grid row="3" column="4" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<hideActionText value="true"/>
<horizontalTextPosition value="0"/>
<text value=""/>
</properties>
</component>
<component id="8b2ff" class="javax.swing.JButton" binding="buttonLeft">
<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>
<hideActionText value="true"/>
<horizontalTextPosition value="0"/>
<text value=""/>
<verticalAlignment value="0"/>
</properties>
</component>
<component id="25771" class="javax.swing.JButton" binding="buttonRight">
<constraints>
<grid row="4" column="5" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<horizontalTextPosition value="0"/>
<text value=""/>
</properties>
</component>
<component id="aefae" class="javax.swing.JComboBox" binding="comboBoxStrategy" custom-create="true">
<constraints>
<grid row="0" column="3" row-span="1" col-span="3" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
</component>
<component id="c79a6" class="javax.swing.JButton" binding="buttonStep">
<constraints>
<grid row="1" column="3" row-span="1" col-span="3" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Шаг"/>
</properties>
</component>
<component id="d1f31" class="javax.swing.JButton" binding="buttonCreateVehicle">
<constraints>
<grid row="3" column="1" row-span="2" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Создать бронемашину"/>
</properties>
</component>
<component id="35f01" class="javax.swing.JButton" binding="buttonCreateTank">
<constraints>
<grid row="3" column="0" row-span="2" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Создать Танк"/>
</properties>
</component>
</children>
</grid>
</form>

View File

@ -1,135 +1,149 @@
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.awt.event.ComponentAdapter; import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent; import java.util.Random;
public class FormTank extends JFrame{
private int Width;
private int Height;
JPanel BottomPanel = new JPanel(); public class FormTank {
JPanel CreatePanel = new JPanel(); DrawingArmoVehicle _drawingTank;
JPanel BottomAndCreatePanel = new JPanel(); AbstractStrategy _abstractStrategy;
JPanel DimentionPanel = new JPanel(); private JButton buttonCreateTank;
JPanel UpPanel = new JPanel(); private JPanel pictureBox;
JPanel DownPanel = new JPanel(); private JButton buttonDown;
JPanel LRPanel = new JPanel(); private JButton buttonUp;
private JButton buttonLeft;
private JButton buttonRight;
private JButton buttonCreateVehicle;
private JComboBox comboBoxStrategy;
private JButton buttonStep;
DrawingField field = new DrawingField(this); public JPanel getPictureBox() {
return pictureBox;
JButton ButtonCreate=new JButton("Создать");
Icon iconUp = new ImageIcon("Resources/KeyUp.png");
JButton ButtonUp=new JButton(iconUp);
Icon iconDown = new ImageIcon("Resources/KeyDown.png");
JButton ButtonDown=new JButton(iconDown);
Icon iconRight = new ImageIcon("Resources/KeyRight.png");
JButton ButtonRight=new JButton(iconRight);
Icon iconLeft = new ImageIcon("Resources/KeyLeft.png");
JButton ButtonLeft=new JButton(iconLeft);
public FormTank(){
super("Tank");
setSize(800,600);
Width=getWidth();
Height=getHeight();
ShowWindow();
RefreshWindow();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
} }
public void ShowWindow(){ public FormTank() {
buttonUp.setName("buttonUp");
buttonDown.setName("buttonDown");
buttonLeft.setName("buttonLeft");
buttonRight.setName("buttonRight");
Dimension dimen=new Dimension(30,30); ImageIcon iconUp = new ImageIcon("Resources/KeyUp.png");
buttonUp.setIcon(iconUp);
ImageIcon iconDown = new ImageIcon("Resources/KeyDown.png");
buttonDown.setIcon(iconDown);
ImageIcon iconRight = new ImageIcon("Resources/KeyRight.png");
buttonRight.setIcon(iconRight);
ImageIcon iconLeft = new ImageIcon("Resources/KeyLeft.png");
buttonLeft.setIcon(iconLeft);
// Обработка нажатия кнопки buttonCreateTank.addActionListener(e -> {
ButtonUp.setPreferredSize(dimen); Random random = new Random();
ButtonUp.addActionListener(e->{ _drawingTank = new DrawingTank(
field.UpButtonAction(); random.nextInt(100, 300),
repaint(); random.nextInt(1000, 3000),
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)),
random.nextInt(2, 6),
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)),
true,
true,
true,
pictureBox.getWidth(),
pictureBox.getHeight(),
true,
true
);
_drawingTank.SetWheels(random.nextInt(2, 6));
_drawingTank.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100));
Draw();
}); });
// Обработка нажатия кнопки buttonCreateVehicle.addActionListener(e -> {
ButtonDown.setPreferredSize(dimen); Random random = new Random();
ButtonDown.addActionListener(e->{ _drawingTank = new DrawingArmoVehicle(
field.DownButtonAction(); random.nextInt(100, 300),
repaint(); random.nextInt(1000, 3000),
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)),
random.nextInt(2, 6),
pictureBox.getWidth(),
pictureBox.getHeight()
);
_drawingTank.SetWheels(random.nextInt(2, 6));
_drawingTank.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100));
Draw();
}); });
// Обработка нажатия кнопки buttonStep.addActionListener(e -> {
ButtonRight.setPreferredSize(dimen); if (_drawingTank == null)
ButtonRight.addActionListener(e->{ return;
field.RightButtonAction(); if (comboBoxStrategy.isEnabled()) {
repaint(); _abstractStrategy = null;
}); int comboBoxStrategySelectedIndex = comboBoxStrategy.getSelectedIndex();
if (comboBoxStrategySelectedIndex == 0) {
_abstractStrategy = new MoveToCenter();
} else if (comboBoxStrategySelectedIndex == 1) {
_abstractStrategy = new MoveToBorder();
}
if (_abstractStrategy == null)
return;
_abstractStrategy.SetData(new DrawingObjectTank(_drawingTank), pictureBox.getWidth(), pictureBox.getHeight());
comboBoxStrategy.setEnabled(false);
}
// Обработка нажатия кнопки if (_abstractStrategy == null)
ButtonLeft.setPreferredSize(dimen); return;
ButtonLeft.addActionListener(e->{ _abstractStrategy.MakeStep();
field.LeftButtonAction(); Draw();
repaint(); if (_abstractStrategy.GetStatus() == Status.Finish) {
}); comboBoxStrategy.setEnabled(true);
_abstractStrategy = null;
// Добавление кнопок на панель (Левая и правая стрелки)
LRPanel.setLayout(new FlowLayout(FlowLayout.CENTER,50,0));
LRPanel.setBackground(new Color(0,0,0,0));
LRPanel.add(ButtonLeft);
LRPanel.add(ButtonRight);
// Добавление кнопки (Стрелка вверх)
UpPanel.setLayout(new FlowLayout());
UpPanel.setBackground(new Color(0,0,0,0));
UpPanel.add(ButtonUp);
// Добавление кнопки (Стрелка вниз)
DownPanel.setLayout(new FlowLayout());
DownPanel.setBackground(new Color(0,0,0,0));
DownPanel.add(ButtonDown);
DimentionPanel.setLayout(new BoxLayout(DimentionPanel,BoxLayout.Y_AXIS));
DimentionPanel.setBackground(new Color(0,0,0,0));
DimentionPanel.add(UpPanel);
DimentionPanel.add(LRPanel);
DimentionPanel.add(DownPanel);
add(DimentionPanel);
// нажатие кнопки
CreatePanel.setLayout(new FlowLayout());
CreatePanel.setBackground(new Color(0,0,0,0));
CreatePanel.add(ButtonCreate);
ButtonCreate.addActionListener(e->{
field.CreateButtonAction();
repaint();
});
BottomPanel.setLayout(new FlowLayout());
BottomPanel.setBackground(new Color(0,0,0,0));
BottomAndCreatePanel.setLayout(new BoxLayout(BottomAndCreatePanel,BoxLayout.Y_AXIS));
BottomAndCreatePanel.setBackground(new Color(0,0,0,0));
BottomAndCreatePanel.add(CreatePanel);
BottomAndCreatePanel.add(BottomPanel);
add(BottomAndCreatePanel);
add(field);
addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
super.componentResized(e);
Width=getWidth();
Height=getHeight();
field.ResizeField();
repaint();
RefreshWindow();
} }
}); });
ActionListener buttonMoveClickedListener = e -> {
String buttonName = ((JButton) e.getSource()).getName();
switch (buttonName) {
case ("buttonUp") -> {
_drawingTank.MoveTransport(Direction.Up);
}
case ("buttonDown") -> {
_drawingTank.MoveTransport(Direction.Down);
}
case ("buttonLeft") -> {
_drawingTank.MoveTransport(Direction.Left);
}
case ("buttonRight") -> {
_drawingTank.MoveTransport(Direction.Right);
}
}
Draw();
};
buttonUp.addActionListener(buttonMoveClickedListener);
buttonDown.addActionListener(buttonMoveClickedListener);
buttonLeft.addActionListener(buttonMoveClickedListener);
buttonRight.addActionListener(buttonMoveClickedListener);
} }
public void RefreshWindow(){
field.setBounds(0,0,Width,Height); public void Draw() {
BottomAndCreatePanel.setBounds(-320,Height-110,Width,80); if (_drawingTank == null) {
DimentionPanel.setBounds(Width-170,Height-170,190,140); return;
}
Graphics g = pictureBox.getGraphics();
pictureBox.paint(g);
_drawingTank.DrawTransport((Graphics2D)g);
}
private void createUIComponents() {
String[] strategiesList = {
"Движение к центру",
"Движение к краю формы"
};
comboBoxStrategy = new JComboBox(strategiesList);
} }
} }

17
Tank/src/FrameTank.java Normal file
View File

@ -0,0 +1,17 @@
import javax.swing.*;
public class FrameTank extends JFrame {
private FormTank _formTank;
public FrameTank() {
super();
setTitle("Танк");
setDefaultCloseOperation(EXIT_ON_CLOSE);
_formTank = new FormTank();
setContentPane(_formTank.getPictureBox());
setDefaultLookAndFeelDecorated(false);
setLocation(300, 100);
pack();
setVisible(true);
}
}

View File

@ -0,0 +1,13 @@
public interface IMoveableObject {
// Получение координаты X объекта
ObjectParameters GetObjectPosition();
// Шаг объекта
int GetStep();
// Проверка, можно ли переместиться по нужному направлению
boolean CheckCanMove(Direction direction);
/// Изменение направления пермещения объекта
void MoveObject(Direction direction);
}

View File

@ -0,0 +1,7 @@
import java.awt.*;
public interface IOrnamentForm {
public CountWheels getNumWheel();
void setDigit(int number);
void Draw(Graphics g, int _startPosX, int _startPosY);
}

View File

@ -1,5 +1,5 @@
public class Main { public class Main {
public static void main(String[] args){ public static void main(String[] args) {
new FormTank(); new FrameTank();
} }
} }

View File

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

View File

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

5
Tank/src/Status.java Normal file
View File

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