Compare commits

...

11 Commits
main ... laba2

Author SHA1 Message Date
e14664fe07 Merge branch 'laba1' of http://student.git.athene.tech/TImourka/PIbd-21_Kouvshinoff_T._A._WarmlyLocomotive._Harder into laba2 2023-10-10 22:45:56 +04:00
9abbe9bdc0 difficult 2023-10-09 21:35:00 +04:00
ab1835aec7 difficult 2023-10-09 21:18:09 +04:00
ae7baff597 base 2023-10-08 19:50:49 +04:00
567a87a848 Base 2023-10-08 01:10:54 +04:00
815fc43a7d commit2 2023-10-07 21:05:55 +04:00
2b2b700c59 commit1 2023-10-07 21:04:33 +04:00
b6967382fa коммит 2023-10-07 20:22:30 +04:00
d6152b45fd ПИбд-21 Кувшинов Тимур 1 лаба 2023-09-26 22:13:53 +04:00
caf0310dff deleted: laba1Loco/Direction.java
deleted:    laba1Loco/DrawingLoco.java
	deleted:    laba1Loco/EntityLoco.java
	deleted:    laba1Loco/Form1.java
	deleted:    laba1Loco/Main.java
	deleted:    laba1Loco/NumWheel.java
	deleted:    laba1Loco/images/arowDown340x259.png
	deleted:    laba1Loco/images/arowL340x259.png
	deleted:    laba1Loco/images/arowR340x259.png
	deleted:    laba1Loco/images/arowUp340x259.png
2023-09-26 22:12:32 +04:00
567f5c5402 ПИбд-21 Кувшинов Тимур лаба 1 2023-09-26 22:11:44 +04:00
23 changed files with 1235 additions and 0 deletions

View File

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

21
laba1Loco/Direction.java Normal file
View File

@ -0,0 +1,21 @@
package laba1Loco;
public enum Direction
{
/// <summary>
/// Вверх
/// </summary>
Up,
/// <summary>
/// Вниз
/// </summary>
Down,
/// <summary>
/// Влево
/// </summary>
Left,
/// <summary>
/// Вправо
/// </summary>
Right
}

127
laba1Loco/DrawingLoco.java Normal file
View File

@ -0,0 +1,127 @@
package laba1Loco;
import java.awt.*;
import java.util.*;
import javax.swing.*;
import javax.swing.Timer;
import java.awt.event.*;
public class DrawingLoco extends DrawingTrain{
/// <summary>
/// Инициализация свойств
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес</param>
/// <param name="bodyColor">Цвет кузова</param>
/// <param name="additionalColor">Дополнительный цвет</param>
/// <param name="tube">Признак наличия трубы</param>
/// <param name="fuelTank">Признак наличия бака</param>
/// <param name="locoLine">Признак наличия паровозной полосы</param>
/// <param name="width">Ширина картинки</param>
/// <param name="height">Высота картинки</param>
public DrawingLoco(int speed, double weight, Color bodyColor, int _numWheel, Color additionalColor, boolean tube, boolean fuelTank, boolean locoLine, int width, int height)
{
super(speed, weight, bodyColor, _numWheel, width, height);
EntityTrain = new EntityLoco(speed, weight, bodyColor, _numWheel, additionalColor, tube, fuelTank, locoLine);
_locoWidth = ((EntityLoco)EntityTrain).FuelTank ? 169 : 83;
}
/// <summary>
/// Установка позиции
/// </summary>
/// <param name="x">Координата X</param>
/// <param name="y">Координата Y</param>
public void SetPosition(int x, int y)
{
_startPosX = Math.min(x, _pictureWidth-_locoWidth);
_startPosY = Math.min(y, _pictureHeight-_locoHeight);
}
/// <summary>
/// Прорисовка объекта
/// </summary>
/// <param name="g"></param>
public void DrawTransport(Graphics2D g2d)
{
if (EntityTrain == null)
{
return;
}
super.DrawTransport(g2d);
g2d.setColor(((EntityLoco)EntityTrain).AdditionalColor);
if (((EntityLoco)EntityTrain).Tube)
{
g2d.drawPolyline(new int[] {
_startPosX + 40,_startPosX + 40,_startPosX + 45,_startPosX + 41,_startPosX + 41,_startPosX + 44,_startPosX + 44,_startPosX + 45,_startPosX + 45,
},new int[] {
_startPosY+9,_startPosY+3,_startPosY+3,_startPosY+3,_startPosY,_startPosY,_startPosY+3,_startPosY+3,_startPosY+9,
},9);
}
if (((EntityLoco)EntityTrain).LocoLine)
{
g2d.drawPolyline(new int[] {
_startPosX + 60,_startPosX + 38,
},new int[] {
_startPosY+10,_startPosY+32
},2);
g2d.drawPolyline(new int[] {
_startPosX + 65,_startPosX + 43,
},new int[] {
_startPosY+10,_startPosY+32
},2);
g2d.drawPolyline(new int[] {
_startPosX + 70,_startPosX + 48,
},new int[] {
_startPosY+10,_startPosY+32
},2);
}
if (((EntityLoco)EntityTrain).FuelTank)
{
// body
g2d.setColor(EntityTrain.BodyColor);
g2d.drawPolyline(new int[]{
_startPosX + 89, _startPosX + 164, _startPosX + 164, _startPosX + 89, _startPosX + 89,
}, new int[]{
_startPosY+10, _startPosY+10, _startPosY+32, _startPosY+32, _startPosY+10
}, 5);
g2d.drawPolyline(new int[]{
_startPosX + 89, _startPosX + 164,
}, new int[]{
_startPosY+21, _startPosY+21,
}, 2);
// trucks
g2d.fillPolygon(new int[]{
_startPosX + 0+85,_startPosX + 5+85,_startPosX + 32+85,_startPosX + 36+85,
}, new int[]{
_startPosY+37,_startPosY+33,_startPosY+33,_startPosY+37
}, 4);
g2d.fillPolygon(new int[]{
_startPosX + 44+85,_startPosX + 49+85,_startPosX + 76+85,_startPosX + 80+85,
}, new int[]{
_startPosY+37,_startPosY+33,_startPosY+33,_startPosY+37
}, 4);
//back
g2d.fillPolygon(new int[]{
_startPosX + 79+85,_startPosX + 82+85,_startPosX + 82+85,_startPosX + 79+85,
}, new int[]{
_startPosY+12,_startPosY+12,_startPosY+30,_startPosY+30
}, 4);
//front
g2d.fillPolygon(new int[]{
_startPosX + 86,_startPosX + 89,_startPosX + 89,_startPosX + 86,
}, new int[]{
_startPosY+12,_startPosY+12,_startPosY+30,_startPosY+30
}, 4);
}
//wheels
wheelDrawing.Draw(_startPosX, _startPosY, ((EntityLoco)EntityTrain).FuelTank, EntityTrain.BodyColor, g2d);
}
}

207
laba1Loco/DrawingTrain.java Normal file
View File

@ -0,0 +1,207 @@
package laba1Loco;
import java.awt.*;
import java.util.*;
import javax.swing.*;
import javax.swing.Timer;
import java.awt.event.*;
public class DrawingTrain {
protected IWheelDrawing wheelDrawing;
/// <summary>
/// Класс-сущность
/// </summary>
public EntityTrain EntityTrain;
/// <summary>
/// Ширина окна
/// </summary>
protected int _pictureWidth;
/// <summary>
/// Высота окна
/// </summary>
protected int _pictureHeight;
/// <summary>
/// Левая координата прорисовки локомотива
/// </summary>
protected int _startPosX;
/// <summary>
/// Верхняя кооридната прорисовки локомотива
/// </summary>
protected int _startPosY;
/// <summary>
/// Ширина прорисовки локомотива
/// </summary>
protected int _locoWidth = 83;
/// <summary>
/// Высота прорисовки локомотива
/// </summary>
protected int _locoHeight = 41;
/// <summary>
/// Инициализация свойств
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес</param>
/// <param name="bodyColor">Цвет кузова</param>
/// <param name="width">Ширина картинки</param>
/// <param name="height">Высота картинки</param>
public DrawingTrain(int speed, double weight, Color bodyColor, int _numWheel, int width, int height)
{
_pictureWidth = width;
_pictureHeight = height;
if (_pictureHeight < _locoHeight || _pictureWidth < _locoWidth)
return;
EntityTrain = new EntityTrain(speed, weight, bodyColor, _numWheel);
Random random = new Random();
switch(random.nextInt(0, 3)){
case 0:
wheelDrawing = new WheelDrawingSimple();
break;
case 1:
wheelDrawing = new WheelDrawingBalls();
break;
case 2:
wheelDrawing = new WheelDrawingDavidStar();
break;
default:
wheelDrawing = new WheelDrawingSimple();
break;
}
wheelDrawing.setNumWheel(_numWheel);
}
/// <summary>
/// Установка позиции
/// </summary>
/// <param name="x">Координата X</param>
/// <param name="y">Координата Y</param>
public void SetPosition(int x, int y)
{
_startPosX = Math.min(x, _pictureWidth - _locoWidth);
_startPosY = Math.min(y, _pictureHeight - _locoHeight);
}
/// <summary>
/// Координата X объекта
/// </summary>
public int GetPosX (){return _startPosX;}
/// <summary>
/// Координата Y объекта
/// </summary>
public int GetPosY (){return _startPosY;}
/// <summary>
/// Ширина объекта
/// </summary>
public int GetWidth (){return _locoWidth;}
/// <summary>
/// Высота объекта
/// </summary>
public int GetHeight (){return _locoHeight;}
/// <summary>
/// Проверка, что объект может переместится по указанному направлению
/// </summary>
/// <param name="direction">Направление</param>
/// <returns>true - можно переместится по указанному направлению</returns>
public boolean CanMove(Direction direction)
{
if (EntityTrain == null)
{
return false;
}
switch (direction)
{
case Left:
return _startPosX - EntityTrain.Step > 0;
case Right:
return _startPosX + _locoWidth + EntityTrain.Step < _pictureWidth;
case Up:
return _startPosY - EntityTrain.Step > 0;
case Down:
return _startPosY + _locoHeight + EntityTrain.Step < _pictureHeight;
default:
return false;
}
}
/// <summary>
/// Изменение направления перемещения
/// </summary>
/// <param name="direction">Направление</param>
public void MoveTransport(Direction direction)
{
if (!CanMove(direction) || EntityTrain == null)
{
return;
}
switch (direction)
{
//влево
case Left:
_startPosX -= (int)EntityTrain.Step;
break;
//вверх
case Up:
_startPosY -= (int)EntityTrain.Step;
break;
// вправо
case Right:
_startPosX += (int)EntityTrain.Step;
break;
//вниз
case Down:
_startPosY += (int)EntityTrain.Step;
break;
}
}
/// <summary>
/// Прорисовка объекта
/// </summary>
/// <param name="g"></param>
public void DrawTransport(Graphics2D g2d)
{
if (EntityTrain == null)
{
return;
}
// body
g2d.setColor(EntityTrain.BodyColor);
g2d.drawPolyline(new int[]{
_startPosX + 8, _startPosX + 79, _startPosX + 79, _startPosX + 4, _startPosX + 4, _startPosX + 8,
}, new int[]{
_startPosY+10, _startPosY+10, _startPosY+32, _startPosY+32, _startPosY+20, _startPosY+10
}, 6);
g2d.drawPolyline(new int[]{
_startPosX + 4, _startPosX + 29, _startPosX + 29, _startPosX + 37, _startPosX + 37, _startPosX + 79, _startPosX + 37, _startPosX + 37, _startPosX + 29, _startPosX + 29,
}, new int[]{
_startPosY+21, _startPosY+21, _startPosY+14, _startPosY+14, _startPosY+21, _startPosY+21, _startPosY+21, _startPosY+29, _startPosY+29, _startPosY+21
}, 10);
// trucks
g2d.fillPolygon(new int[]{
_startPosX + 0,_startPosX + 5,_startPosX + 32,_startPosX + 36,
}, new int[]{
_startPosY+37,_startPosY+33,_startPosY+33,_startPosY+37
}, 4);
g2d.fillPolygon(new int[]{
_startPosX + 44,_startPosX + 49,_startPosX + 76,_startPosX + 80,
}, new int[]{
_startPosY+37,_startPosY+33,_startPosY+33,_startPosY+37
}, 4);
//back
g2d.fillPolygon(new int[]{
_startPosX + 79,_startPosX + 82,_startPosX + 82,_startPosX + 79,
}, new int[]{
_startPosY+12,_startPosY+12,_startPosY+30,_startPosY+30
}, 4);
//windows
g2d.setColor(Color.BLUE);
g2d.drawRect(_startPosX + 10, _startPosY + 12, 6, 7);
g2d.drawRect(_startPosX + 19, _startPosY + 12, 6, 7);
g2d.drawRect(_startPosX + 72, _startPosY + 12, 6, 7);
//wheels
wheelDrawing.Draw(_startPosX, _startPosY, false, EntityTrain.BodyColor, g2d);
}
}

View File

@ -0,0 +1,22 @@
package laba1Loco;
public class DrawningObjectTrain implements IMoveableObject {
private DrawingTrain _drawingTrain = null;
public DrawningObjectTrain(DrawingTrain drawingTrain)
{
_drawingTrain = drawingTrain;
}
public ObjectParameters GetObjectPosition(){
if (_drawingTrain == null || _drawingTrain.EntityTrain ==
null)
{
return null;
}
return new ObjectParameters(_drawingTrain.GetPosX(),
_drawingTrain.GetPosY(), _drawingTrain.GetWidth(), _drawingTrain.GetHeight());
}
public int GetStep(){ return (int)_drawingTrain.EntityTrain.Step; }
public boolean CheckCanMove(Direction direction) { return _drawingTrain.CanMove(direction);}
public void MoveObject(Direction direction) { _drawingTrain.MoveTransport(direction); }
}

40
laba1Loco/EntityLoco.java Normal file
View File

@ -0,0 +1,40 @@
package laba1Loco;
import java.awt.*;
import java.util.*;
import javax.swing.*;
import javax.swing.Timer;
import java.awt.event.*;
public class EntityLoco extends EntityTrain{
public Color AdditionalColor;
/// <summary>
/// Признак (опция) наличия трубы
/// </summary>
public boolean Tube;
/// Признак (опция) наличия топливного бака
/// </summary>
public boolean FuelTank;
/// <summary>
/// Признак (опция) наличия паровозной полосы
/// </summary>
public boolean LocoLine;
/// <summary>
/// Инициализация полей объекта-класса Локомотива
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес автомобиля</param>
/// <param name="bodyColor">Основной цвет</param>
/// <param name="additionalColor">Дополнительный цвет</param>
/// <param name="tube">Признак наличия трубы</param>
/// <param name="fuelTank">Признак наличия бака</param>
/// <param name="locoLine">Признак наличия паровозной полосы</param>
public EntityLoco(int speed, double weight, Color bodyColor, int _numWheel, Color additionalColor, boolean tube, boolean fuelTank, boolean locoLine)
{
super(speed, weight, bodyColor, _numWheel);
AdditionalColor = additionalColor;
Tube = tube;
FuelTank = fuelTank;
LocoLine = locoLine;
}
}

View File

@ -0,0 +1,44 @@
package laba1Loco;
import java.awt.*;
import java.util.*;
import javax.swing.*;
import javax.swing.Timer;
import java.awt.event.*;
public class EntityTrain {
/// <summary>
/// Скорость
/// </summary>
public int Speed;
/// <summary>
/// Вес
/// </summary>
public double Weight;
/// <summary>
/// Основной цвет
/// </summary>
public Color BodyColor;
/// <summary>
/// Шаг перемещения поезда
/// </summary>
public double Step;
/// <summary>
/// количество колёс [2;4]
/// </summary>
public int numWheel;
/// <summary>
/// Инициализация полей объекта-класса Локомотива
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес автомобиля</param>
/// <param name="bodyColor">Основной цвет</param>
public EntityTrain(int speed, double weight, Color bodyColor, int _numWheel)
{
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
numWheel = _numWheel;
Step = (double)Speed * 100 / Weight;
}
}

204
laba1Loco/FormTrain.java Normal file
View File

@ -0,0 +1,204 @@
package laba1Loco;
import java.awt.*;
import java.util.*;
import javax.swing.*;
import javax.swing.Timer;
import java.awt.event.*;
public class FormTrain{
private DrawingTrain _drawingTrain;
private AbstractStrategy abstractStrategy;
Canvas canv;
static int pictureBoxWidth = 980;
static int pictureBoxHeight = 580;
public void Draw(){
canv.repaint();
}
public FormTrain(){
JFrame w=new JFrame ("Loco");
JButton buttonCreate = new JButton("create");
JButton buttonCreateLocomotive = new JButton("createLocomotive");
JButton buttonStrategysStep = new JButton("strategys step");
JComboBox comboBoxStrategy = new JComboBox(
new String[]{
"к центру",
"к краю",
});
JButton up = new JButton();
up.setBorderPainted(false);
up.setFocusPainted(false);
up.setContentAreaFilled(false);
up.setName("up");
up.setIcon(new ImageIcon("D:\\Coffee\\PIbd-21_Kouvshinoff_T._A._WarmlyLocomotive._Harder\\laba1Loco\\images\\arowUp340x259.png"));
JButton down = new JButton();
down.setBorderPainted(false);
down.setFocusPainted(false);
down.setContentAreaFilled(false);
down.setName("down");
down.setIcon(new ImageIcon("D:\\Coffee\\PIbd-21_Kouvshinoff_T._A._WarmlyLocomotive._Harder\\laba1Loco\\images\\arowDown340x259.png"));
JButton left = new JButton();
left.setBorderPainted(false);
left.setFocusPainted(false);
left.setContentAreaFilled(false);
left.setName("left");
left.setIcon(new ImageIcon("D:\\Coffee\\PIbd-21_Kouvshinoff_T._A._WarmlyLocomotive._Harder\\laba1Loco\\images\\arowL340x259.png"));
JButton right = new JButton();
right.setBorderPainted(false);
right.setFocusPainted(false);
right.setContentAreaFilled(false);
right.setName("right");
right.setIcon(new ImageIcon("D:\\Coffee\\PIbd-21_Kouvshinoff_T._A._WarmlyLocomotive._Harder\\laba1Loco\\images\\arowR340x259.png"));
buttonStrategysStep.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e){
if (_drawingTrain == null)
{
return;
}
if (comboBoxStrategy.isEnabled())
{
switch (comboBoxStrategy.getSelectedIndex())
{
case 0:
abstractStrategy = new MoveToCenter();
break;
case 1:
abstractStrategy = new MoveToBorder();
break;
default:
abstractStrategy = null;
break;
};
if (abstractStrategy == null)
{
return;
}
abstractStrategy.SetData(new
DrawningObjectTrain(_drawingTrain), pictureBoxWidth,
pictureBoxHeight);
comboBoxStrategy.setEnabled(false);
}
if (abstractStrategy == null)
{
return;
}
abstractStrategy.MakeStep();
Draw();
if (abstractStrategy.GetStatus() == Status.Finish)
{
comboBoxStrategy.setEnabled(true);
abstractStrategy = null;
}
}
}
);
buttonCreateLocomotive.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e){
System.out.println(e.getActionCommand());
Random random = new Random();
_drawingTrain = new DrawingLoco(
random.nextInt(100, 300),
random.nextInt(1000, 3000),
new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)),
random.nextInt(2, 5),
new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)),
random.nextInt(0, 2) == 1,
random.nextInt(0, 2) == 1,
random.nextInt(0, 2) == 1,
pictureBoxWidth,
pictureBoxHeight);
_drawingTrain.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100));
canv._drawingTrain = _drawingTrain;
Draw();
}
}
);
buttonCreate.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e){
System.out.println(e.getActionCommand());
Random random = new Random();
_drawingTrain = new DrawingTrain(
random.nextInt(100, 300),
random.nextInt(1000, 3000),
new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)),
random.nextInt(2, 5),
pictureBoxWidth,
pictureBoxHeight);
_drawingTrain.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100));
canv._drawingTrain = _drawingTrain;
Draw();
}
}
);
ActionListener actioListener = new ActionListener() {
public void actionPerformed(ActionEvent e){
System.out.println(((JButton)(e.getSource())).getName());
if (_drawingTrain == null)
{
return;
}
switch(((JButton)(e.getSource())).getName()){
case "up":
_drawingTrain.MoveTransport(Direction.Up);
break;
case "down":
_drawingTrain.MoveTransport(Direction.Down);
break;
case "left":
_drawingTrain.MoveTransport(Direction.Left);
break;
case "right":
_drawingTrain.MoveTransport(Direction.Right);
break;
}
Draw();
}
};
up.addActionListener(actioListener);
down.addActionListener(actioListener);
left.addActionListener(actioListener);
right.addActionListener(actioListener);
w.setSize (1000, 600);
w.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
w.setLayout(null);
canv = new Canvas();
canv.setBounds(0, 0, pictureBoxWidth, pictureBoxHeight);
buttonCreate.setBounds(2, 540, 100, 20);
buttonCreateLocomotive.setBounds(105, 540, 150, 20);
up.setBounds(900, 480, 40, 40);
down.setBounds(900, 520, 40, 40);
left.setBounds(860, 520, 40, 40);
right.setBounds(940, 520, 40, 40);
comboBoxStrategy.setBounds(pictureBoxWidth - 150, 20, 150, 20);
buttonStrategysStep.setBounds(pictureBoxWidth - 150, 45, 150, 20);
w.add(canv);
w.add(buttonCreate);
w.add(buttonCreateLocomotive);
w.add(up);
w.add(down);
w.add(left);
w.add(right);
w.add(comboBoxStrategy);
w.add(buttonStrategysStep);
}
}
class Canvas extends JComponent{
public DrawingTrain _drawingTrain;
public Canvas(){
}
public void paintComponent (Graphics g){
if (_drawingTrain == null){
return;
}
super.paintComponents (g) ;
Graphics2D g2d = (Graphics2D)g;
_drawingTrain.DrawTransport(g2d);
super.repaint();
}
}

View File

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

View File

@ -0,0 +1,15 @@
package laba1Loco;
import java.awt.*;
import java.util.*;
import javax.swing.*;
import javax.swing.Timer;
import java.awt.event.*;
public interface IWheelDrawing{
public NumWheel getNumWheel();
public void setNumWheel(int kwheel);
public void Draw(int _startPosX, int _startPosY, boolean fuelTank, Color color, Graphics2D g2d);
}

7
laba1Loco/Main.java Normal file
View File

@ -0,0 +1,7 @@
package laba1Loco;
public class Main{
public static void main(String[] args) {
FormTrain formTrain = new FormTrain();
}
}

View File

@ -0,0 +1,48 @@
package laba1Loco;
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,48 @@
package laba1Loco;
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();
}
}
}
}

17
laba1Loco/NumWheel.java Normal file
View File

@ -0,0 +1,17 @@
package laba1Loco;
public enum NumWheel
{
/// <summary>
/// 2 колеса
/// </summary>
TwoWheel,
/// <summary>
/// 3 колеса
/// </summary>
ThreeWheel,
/// <summary>
/// 4 колеса
/// </summary>
FourWheel
}

View File

@ -0,0 +1,46 @@
package laba1Loco;
public class ObjectParameters {
private int _x;
private int _y;
private int _width;
private int _height;
/// <summary>
/// Левая граница
/// </summary>
public int LeftBorder() {return _x;}
/// <summary>
/// Верхняя граница
/// </summary>
public int TopBorder () {return _y;}
/// <summary>
/// Правая граница
/// </summary>
public int RightBorder () {return _x + _width;}
/// <summary>
/// Нижняя граница
/// </summary>
public int DownBorder () {return _y + _height;}
/// <summary>
/// Середина объекта
/// </summary>
public int ObjectMiddleHorizontal () {return _x + _width / 2;}
/// <summary>
/// Середина объекта
/// </summary>
public int ObjectMiddleVertical () {return _y + _height / 2;}
/// <summary>
/// Конструктор
/// </summary>
/// <param name="x">Координата X</param>
/// <param name="y">Координата Y</param>
/// <param name="width">Ширина</param>
/// <param name="height">Высота</param>
public ObjectParameters(int x, int y, int width, int height)
{
_x = x;
_y = y;
_width = width;
_height = height;
}
}

7
laba1Loco/Status.java Normal file
View File

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

View File

@ -0,0 +1,80 @@
package laba1Loco;
import java.awt.*;
import java.util.*;
import javax.swing.*;
import javax.swing.Timer;
import java.awt.event.*;
public class WheelDrawingBalls implements IWheelDrawing{
private NumWheel numWheel;
public NumWheel getNumWheel() {
return numWheel;
}
public void setNumWheel(int kwheel){
switch(kwheel){
case 2:
numWheel = NumWheel.TwoWheel;
break;
case 3:
numWheel = NumWheel.ThreeWheel;
break;
case 4:
numWheel = NumWheel.FourWheel;
break;
default:
numWheel = NumWheel.TwoWheel;
System.out.println("ВСё плохо, колво колёс почему то не соответтвовало критериям, количество колёс:" + Integer.toString(kwheel) + "но вывели мы как будто их было 2");
break;
}
}
private void DrawWheel(int _startPosX, int _startPosY, Color color, Graphics2D g2d){
g2d.setColor(color);
g2d.fillOval(_startPosX, _startPosY, 8, 8);
g2d.setColor(Color.PINK);
g2d.fillOval( _startPosX+3, _startPosY+1, 2, 3);
g2d.fillOval( _startPosX+1, _startPosY+4, 2, 2);
g2d.fillOval( _startPosX+6, _startPosY+4, 2, 2);
}
public void Draw(int _startPosX, int _startPosY, boolean fuelTank, Color color, Graphics2D g2d){
DrawWheel(_startPosX + 3, _startPosY + 34, color, g2d);
DrawWheel(_startPosX + 26, _startPosY + 34, color, g2d);
DrawWheel(_startPosX + 46, _startPosY + 34, color, g2d);
DrawWheel(_startPosX + 72, _startPosY + 34, color, g2d);
if (numWheel == NumWheel.TwoWheel){
DrawWheel(_startPosX + 14, _startPosY + 34, color, g2d);
DrawWheel(_startPosX + 59, _startPosY + 34, color, g2d);
}
if (numWheel == NumWheel.FourWheel){
DrawWheel(_startPosX + 11, _startPosY + 34, color, g2d);
DrawWheel(_startPosX + 18, _startPosY + 34, color, g2d);
DrawWheel(_startPosX + 55, _startPosY + 34, color, g2d);
DrawWheel(_startPosX + 63, _startPosY + 34, color, g2d);
}
if (fuelTank){
DrawWheel(_startPosX + 3+85, _startPosY + 34, color, g2d);
DrawWheel(_startPosX + 26+85, _startPosY + 34, color, g2d);
DrawWheel(_startPosX + 46+85, _startPosY + 34, color, g2d);
DrawWheel(_startPosX + 72+85, _startPosY + 34, color, g2d);
if (numWheel == NumWheel.TwoWheel){
DrawWheel(_startPosX + 14+85, _startPosY + 34, color, g2d);
DrawWheel(_startPosX + 59+85, _startPosY + 34, color, g2d);
}
if (numWheel == NumWheel.FourWheel){
DrawWheel(_startPosX + 11+85, _startPosY + 34, color, g2d);
DrawWheel(_startPosX + 18+85, _startPosY + 34, color, g2d);
DrawWheel(_startPosX + 55+85, _startPosY + 34, color, g2d);
DrawWheel(_startPosX + 63+85, _startPosY + 34, color, g2d);
}
}
}
}

View File

@ -0,0 +1,87 @@
package laba1Loco;
import java.awt.*;
import java.util.*;
import javax.swing.*;
import javax.swing.Timer;
import java.awt.event.*;
public class WheelDrawingDavidStar implements IWheelDrawing{
private NumWheel numWheel;
public NumWheel getNumWheel() {
return numWheel;
}
public void setNumWheel(int kwheel){
switch(kwheel){
case 2:
numWheel = NumWheel.TwoWheel;
break;
case 3:
numWheel = NumWheel.ThreeWheel;
break;
case 4:
numWheel = NumWheel.FourWheel;
break;
default:
numWheel = NumWheel.TwoWheel;
System.out.println("ВСё плохо, колво колёс почему то не соответтвовало критериям, количество колёс:" + Integer.toString(kwheel) + "но вывели мы как будто их было 2");
break;
}
}
private void DrawWheel(int _startPosX, int _startPosY, Color color, Graphics2D g2d){
g2d.setColor(color);
g2d.fillOval(_startPosX, _startPosY, 8, 8);
g2d.setColor(Color.WHITE);
g2d.drawPolygon(new int[]{
_startPosX + 1, _startPosX + 7, _startPosX + 3
}, new int[]{
_startPosY + 2, _startPosY + 2, _startPosY + 7
}, 3);
g2d.drawPolygon(new int[]{
_startPosX + 1, _startPosX + 7, _startPosX + 4
}, new int[]{
_startPosY + 6, _startPosY + 6, _startPosY + 1
}, 3);
}
public void Draw(int _startPosX, int _startPosY, boolean fuelTank, Color color, Graphics2D g2d){
DrawWheel(_startPosX + 3, _startPosY + 34, color, g2d);
DrawWheel(_startPosX + 26, _startPosY + 34, color, g2d);
DrawWheel(_startPosX + 46, _startPosY + 34, color, g2d);
DrawWheel(_startPosX + 72, _startPosY + 34, color, g2d);
if (numWheel == NumWheel.TwoWheel){
DrawWheel(_startPosX + 14, _startPosY + 34, color, g2d);
DrawWheel(_startPosX + 59, _startPosY + 34, color, g2d);
}
if (numWheel == NumWheel.FourWheel){
DrawWheel(_startPosX + 11, _startPosY + 34, color, g2d);
DrawWheel(_startPosX + 18, _startPosY + 34, color, g2d);
DrawWheel(_startPosX + 55, _startPosY + 34, color, g2d);
DrawWheel(_startPosX + 63, _startPosY + 34, color, g2d);
}
if (fuelTank){
DrawWheel(_startPosX + 3+85, _startPosY + 34, color, g2d);
DrawWheel(_startPosX + 26+85, _startPosY + 34, color, g2d);
DrawWheel(_startPosX + 46+85, _startPosY + 34, color, g2d);
DrawWheel(_startPosX + 72+85, _startPosY + 34, color, g2d);
if (numWheel == NumWheel.TwoWheel){
DrawWheel(_startPosX + 14+85, _startPosY + 34, color, g2d);
DrawWheel(_startPosX + 59+85, _startPosY + 34, color, g2d);
}
if (numWheel == NumWheel.FourWheel){
DrawWheel(_startPosX + 11+85, _startPosY + 34, color, g2d);
DrawWheel(_startPosX + 18+85, _startPosY + 34, color, g2d);
DrawWheel(_startPosX + 55+85, _startPosY + 34, color, g2d);
DrawWheel(_startPosX + 63+85, _startPosY + 34, color, g2d);
}
}
}
}

View File

@ -0,0 +1,70 @@
package laba1Loco;
import java.awt.*;
import java.util.*;
import javax.swing.*;
import javax.swing.Timer;
import java.awt.event.*;
public class WheelDrawingSimple implements IWheelDrawing{
private NumWheel numWheel;
public NumWheel getNumWheel() {
return numWheel;
}
public void setNumWheel(int kwheel){
switch(kwheel){
case 2:
numWheel = NumWheel.TwoWheel;
break;
case 3:
numWheel = NumWheel.ThreeWheel;
break;
case 4:
numWheel = NumWheel.FourWheel;
break;
default:
numWheel = NumWheel.TwoWheel;
System.out.println("ВСё плохо, колво колёс почему то не соответтвовало критериям, количество колёс:" + Integer.toString(kwheel) + "но вывели мы как будто их было 2");
break;
}
}
public void Draw(int _startPosX, int _startPosY, boolean fuelTank, Color color, Graphics2D g2d){
g2d.setColor(color);
g2d.fillOval(_startPosX + 3, _startPosY + 34, 8, 8);
g2d.fillOval(_startPosX + 26, _startPosY + 34, 8, 8);
g2d.fillOval(_startPosX + 46, _startPosY + 34, 8, 8);
g2d.fillOval(_startPosX + 72, _startPosY + 34, 8, 8);
if (numWheel == NumWheel.TwoWheel){
g2d.fillOval(_startPosX + 14, _startPosY + 34, 8, 8);
g2d.fillOval(_startPosX + 59, _startPosY + 34, 8, 8);
}
if (numWheel == NumWheel.FourWheel){
g2d.fillOval(_startPosX + 11, _startPosY + 34, 8, 8);
g2d.fillOval(_startPosX + 18, _startPosY + 34, 8, 8);
g2d.fillOval(_startPosX + 55, _startPosY + 34, 8, 8);
g2d.fillOval(_startPosX + 63, _startPosY + 34, 8, 8);
}
if (fuelTank){
g2d.fillOval(_startPosX + 3+85, _startPosY + 34, 8, 8);
g2d.fillOval(_startPosX + 26+85, _startPosY + 34, 8, 8);
g2d.fillOval(_startPosX + 46+85, _startPosY + 34, 8, 8);
g2d.fillOval(_startPosX + 72+85, _startPosY + 34, 8, 8);
if (numWheel == NumWheel.TwoWheel){
g2d.fillOval(_startPosX + 14+85, _startPosY + 34, 8, 8);
g2d.fillOval(_startPosX + 59+85, _startPosY + 34, 8, 8);
}
if (numWheel == NumWheel.FourWheel){
g2d.fillOval(_startPosX + 11+85, _startPosY + 34, 8, 8);
g2d.fillOval(_startPosX + 18+85, _startPosY + 34, 8, 8);
g2d.fillOval(_startPosX + 55+85, _startPosY + 34, 8, 8);
g2d.fillOval(_startPosX + 63+85, _startPosY + 34, 8, 8);
}
}
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 546 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 518 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 513 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 541 B