Боровков М В ПИбд-22 2 лабораторная работа #2
@ -5,120 +5,33 @@ import MovementStrategy.*;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawningElectricLocomotive {
|
||||
public EntityElectricLocomotive _entityElectricLocomotive;
|
||||
private DrawningWheels drawningWheels;
|
||||
private int _pictureWidth;
|
||||
private int _pictureHeight;
|
||||
private int _startPosX;
|
||||
private int _startPosY;
|
||||
private final int _locomotiveWidth = 120;
|
||||
private final int _locomotiveHeight = 70;
|
||||
|
||||
public boolean Init(int speed, double weight, Color bodyColor, Color
|
||||
additionalColor, int countWheels, boolean horns, boolean battery, int width, int height) {
|
||||
if (width < _locomotiveWidth || height < _locomotiveHeight) {
|
||||
return false;
|
||||
}
|
||||
_pictureWidth = width;
|
||||
_pictureHeight = height;
|
||||
_entityElectricLocomotive = new EntityElectricLocomotive();
|
||||
_entityElectricLocomotive.Init(speed, weight, bodyColor, additionalColor,
|
||||
horns, battery);
|
||||
drawningWheels = new DrawningWheels();
|
||||
drawningWheels.setCount(countWheels);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void SetPosition(int x, int y) {
|
||||
x = Math.min(Math.max(0, x), _pictureWidth - _locomotiveWidth);
|
||||
y = Math.min(Math.max(0, y), _pictureHeight - _locomotiveHeight);
|
||||
_startPosX = x;
|
||||
_startPosY = y;
|
||||
}
|
||||
|
||||
public void MoveTransport(DirectionType direction) {
|
||||
if (_entityElectricLocomotive == null) {
|
||||
return;
|
||||
}
|
||||
switch (direction) {
|
||||
//влево
|
||||
case DirectionType.Left:
|
||||
if (_startPosX - _entityElectricLocomotive.Step > 0) {
|
||||
_startPosX -= (int) _entityElectricLocomotive.Step;
|
||||
}
|
||||
break;
|
||||
//вверх
|
||||
case DirectionType.Up:
|
||||
if (_startPosY - _entityElectricLocomotive.Step > 0) {
|
||||
_startPosY -= (int) _entityElectricLocomotive.Step;
|
||||
}
|
||||
break;
|
||||
// вправо
|
||||
case DirectionType.Right:
|
||||
if (_startPosX + _locomotiveWidth + _entityElectricLocomotive.Step < _pictureWidth) {
|
||||
_startPosX += (int) _entityElectricLocomotive.Step;
|
||||
}
|
||||
break;
|
||||
//вниз
|
||||
case DirectionType.Down:
|
||||
if (_startPosY + _locomotiveHeight + _entityElectricLocomotive.Step < _pictureHeight) {
|
||||
_startPosY += (int) _entityElectricLocomotive.Step;
|
||||
}
|
||||
break;
|
||||
public class DrawningElectricLocomotive extends DrawningLocomotive {
|
||||
public DrawningElectricLocomotive(int speed, double weight,
|
||||
Color bodyColor, Color additionalColor,
|
||||
int typeWheels, int countWheels,
|
||||
boolean horns, boolean battery,
|
||||
int width, int height){
|
||||
super(speed, weight, bodyColor, typeWheels, countWheels, width, height, 120, 70);
|
||||
if (entityLocomotive != null){
|
||||
entityLocomotive = new EntityElectricLocomotive(
|
||||
speed, weight, bodyColor, additionalColor, horns, battery);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void DrawTransport(Graphics g) {
|
||||
if (_entityElectricLocomotive == null) {
|
||||
return;
|
||||
}
|
||||
super.DrawTransport(g);
|
||||
EntityElectricLocomotive entityElectricLocomotiveLocomotive = (EntityElectricLocomotive)entityLocomotive;
|
||||
|
||||
g.clearRect(0, 0, _pictureWidth, _pictureHeight);
|
||||
// корпус электровоза
|
||||
g.setColor(_entityElectricLocomotive.BodyColor);
|
||||
|
||||
int[] posX = {
|
||||
_startPosX,
|
||||
_startPosX + 10,
|
||||
_startPosX + 120,
|
||||
_startPosX + 120,
|
||||
_startPosX
|
||||
};
|
||||
|
||||
int[] posY = {
|
||||
_startPosY + 40,
|
||||
_startPosY + 20,
|
||||
_startPosY + 20,
|
||||
_startPosY + 60,
|
||||
_startPosY + 60
|
||||
};
|
||||
|
||||
g.fillPolygon(posX, posY, 5);
|
||||
g.drawLine(_startPosX, _startPosY + 40,
|
||||
_startPosX + 120, _startPosY + 40);
|
||||
|
||||
// окна
|
||||
g.setColor(Color.blue);
|
||||
g.fillRect(_startPosX + 12, _startPosY + 24, 30, 11);
|
||||
g.fillRect(_startPosX + 55, _startPosY + 24, 11, 11);
|
||||
g.fillRect(_startPosX + 75, _startPosY + 24, 11, 11);
|
||||
g.fillRect(_startPosX + 95, _startPosY + 24, 11, 11);
|
||||
|
||||
// колёса
|
||||
drawningWheels.DrawWheels(g, _startPosX, _startPosY);
|
||||
|
||||
// рога
|
||||
if (_entityElectricLocomotive.Horns) {
|
||||
if (entityElectricLocomotiveLocomotive.Horns) {
|
||||
g.setColor(Color.black);
|
||||
posX = new int[]{
|
||||
int[] posX = new int[]{
|
||||
_startPosX + 50,
|
||||
_startPosX + 40,
|
||||
_startPosX + 50,
|
||||
_startPosX + 60
|
||||
};
|
||||
|
||||
posY = new int[]{
|
||||
int[] posY = new int[]{
|
||||
_startPosY + 20,
|
||||
_startPosY + 10,
|
||||
_startPosY,
|
||||
@ -127,14 +40,9 @@ public class DrawningElectricLocomotive {
|
||||
g.drawPolygon(posX, posY, 4);
|
||||
}
|
||||
// отсек для батарей
|
||||
if (_entityElectricLocomotive.Battery) {
|
||||
g.setColor(_entityElectricLocomotive.AdditionalColor);
|
||||
if (entityElectricLocomotiveLocomotive.Battery) {
|
||||
g.setColor(entityElectricLocomotiveLocomotive.AdditionalColor);
|
||||
g.fillRect(_startPosX + 80, _startPosY + 45, 35, 9);
|
||||
}
|
||||
}
|
||||
public void SetPictureSize(int width, int height)
|
||||
{
|
||||
_pictureWidth = width;
|
||||
_pictureHeight = height;
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,179 @@
|
||||
package DrawningObjects;
|
||||
|
||||
import Entities.EntityLocomotive;
|
||||
import MovementStrategy.DirectionType;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawningLocomotive {
|
||||
public EntityLocomotive entityLocomotive;
|
||||
protected int _pictureWidth;
|
||||
protected int _pictureHeight;
|
||||
protected int _startPosX;
|
||||
protected int _startPosY;
|
||||
protected int _locomotiveWidth = 140;
|
||||
protected int _locomotiveHeight = 75;
|
||||
|
||||
protected IDrawWheels wheels;
|
||||
|
||||
public int GetPosX() {
|
||||
return _startPosX;
|
||||
}
|
||||
|
||||
public int GetPosY() {
|
||||
return _startPosY;
|
||||
}
|
||||
|
||||
public int GetWidth() {
|
||||
return _locomotiveWidth;
|
||||
}
|
||||
|
||||
public int GetHeight() {
|
||||
return _locomotiveHeight;
|
||||
}
|
||||
|
||||
public DrawningLocomotive(int speed, double weight,
|
||||
Color bodyColor,
|
||||
int typeWheels, int countWheels,
|
||||
int width, int height) {
|
||||
if (width < _locomotiveWidth || height < _locomotiveHeight) {
|
||||
return;
|
||||
}
|
||||
_pictureWidth = width;
|
||||
_pictureHeight = height;
|
||||
switch (typeWheels){
|
||||
case 0:
|
||||
wheels = new DrawningWheels();
|
||||
break;
|
||||
case 1:
|
||||
wheels = new DrawningWheelsPolygon();
|
||||
break;
|
||||
case 2:
|
||||
wheels = new DrawningWheelsStar();
|
||||
break;
|
||||
}
|
||||
wheels.setWheelsCount(countWheels);
|
||||
|
||||
entityLocomotive = new EntityLocomotive(speed, weight, bodyColor);
|
||||
}
|
||||
|
||||
public DrawningLocomotive(int speed, double weight,
|
||||
Color bodyColor,
|
||||
int typeWheels, int countWheels,
|
||||
int width, int height,
|
||||
int locomotiveWidth, int locomotiveHeight) {
|
||||
if (width < locomotiveWidth || height < locomotiveHeight) {
|
||||
return;
|
||||
}
|
||||
_pictureWidth = width;
|
||||
_pictureHeight = height;
|
||||
_locomotiveWidth = locomotiveWidth;
|
||||
_locomotiveHeight = locomotiveHeight;
|
||||
switch (typeWheels){
|
||||
case 0:
|
||||
wheels = new DrawningWheels();
|
||||
break;
|
||||
case 1:
|
||||
wheels = new DrawningWheelsPolygon();
|
||||
break;
|
||||
case 2:
|
||||
wheels = new DrawningWheelsStar();
|
||||
break;
|
||||
}
|
||||
wheels.setWheelsCount(countWheels);
|
||||
|
||||
entityLocomotive = new EntityLocomotive(speed, weight, bodyColor);
|
||||
}
|
||||
|
||||
public void SetPosition(int x, int y) {
|
||||
x = Math.min(Math.max(0, x), _pictureWidth - _locomotiveWidth);
|
||||
y = Math.min(Math.max(0, y), _pictureHeight - _locomotiveHeight);
|
||||
_startPosX = x;
|
||||
_startPosY = y;
|
||||
}
|
||||
|
||||
public void DrawTransport(Graphics g) {
|
||||
if (entityLocomotive == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
g.clearRect(0, 0, _pictureWidth, _pictureHeight);
|
||||
// корпус
|
||||
g.setColor(entityLocomotive.BodyColor);
|
||||
|
||||
int[] posX = {
|
||||
_startPosX,
|
||||
_startPosX + 10,
|
||||
_startPosX + 140,
|
||||
_startPosX + 140,
|
||||
_startPosX
|
||||
};
|
||||
|
||||
int[] posY = {
|
||||
_startPosY + 40,
|
||||
_startPosY + 20,
|
||||
_startPosY + 20,
|
||||
_startPosY + 60,
|
||||
_startPosY + 60
|
||||
};
|
||||
|
||||
g.fillPolygon(posX, posY, 5);
|
||||
g.drawLine(_startPosX, _startPosY + 40,
|
||||
_startPosX + 120, _startPosY + 40);
|
||||
|
||||
// окна
|
||||
g.setColor(Color.blue);
|
||||
g.fillRect(_startPosX + 12, _startPosY + 24, 30, 11);
|
||||
g.fillRect(_startPosX + 55, _startPosY + 24, 11, 11);
|
||||
g.fillRect(_startPosX + 75, _startPosY + 24, 11, 11);
|
||||
g.fillRect(_startPosX + 95, _startPosY + 24, 11, 11);
|
||||
|
||||
// колёса
|
||||
wheels.DrawWheels(g, _startPosX, _startPosY);
|
||||
}
|
||||
|
||||
public void SetPictureSize(int width, int height) {
|
||||
_pictureWidth = width;
|
||||
_pictureHeight = height;
|
||||
}
|
||||
|
||||
public boolean CanMove(DirectionType direction) {
|
||||
if (entityLocomotive == null) {
|
||||
return false;
|
||||
}
|
||||
switch (direction) {
|
||||
case Down -> {
|
||||
return _startPosY + _locomotiveHeight + entityLocomotive.Step < _pictureHeight;
|
||||
}
|
||||
case Up -> {
|
||||
return _startPosY - entityLocomotive.Step > 0;
|
||||
}
|
||||
case Right -> {
|
||||
return _startPosX + _locomotiveWidth + entityLocomotive.Step < _pictureWidth;
|
||||
}
|
||||
case Left -> {
|
||||
return _startPosX - entityLocomotive.Step > 0;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
public void MoveTransport(DirectionType direction) {
|
||||
if (!CanMove(direction) || entityLocomotive == null) {
|
||||
return;
|
||||
}
|
||||
switch (direction) {
|
||||
case Left -> {
|
||||
_startPosX -= (int) entityLocomotive.Step;
|
||||
}
|
||||
case Up -> {
|
||||
_startPosY -= (int) entityLocomotive.Step;
|
||||
}
|
||||
case Right -> {
|
||||
_startPosX += (int) entityLocomotive.Step;
|
||||
}
|
||||
case Down -> {
|
||||
_startPosY += (int) entityLocomotive.Step;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -2,29 +2,24 @@ package DrawningObjects;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawningWheels {
|
||||
private CountEnum countWhils;
|
||||
public class DrawningWheels implements IDrawWheels {
|
||||
private CountEnum _wheelsCount;
|
||||
final protected int _wheelsSize = 18;
|
||||
|
||||
public void setCount(int count){
|
||||
count = Math.min(4, count);
|
||||
count = Math.max(2, count);
|
||||
switch (count) {
|
||||
case 2:
|
||||
countWhils = CountEnum.Min;
|
||||
return;
|
||||
case 3:
|
||||
countWhils = CountEnum.Mid;
|
||||
return;
|
||||
case 4:
|
||||
countWhils = CountEnum.Max;
|
||||
return;
|
||||
}
|
||||
@Override
|
||||
public CountEnum WheelsCount() {
|
||||
return _wheelsCount;
|
||||
}
|
||||
protected void drawWheel(Graphics g, int posX, int posY){
|
||||
|
||||
g.fillOval(posX, posY, _wheelsSize, _wheelsSize);
|
||||
}
|
||||
|
||||
void DrawWheels(Graphics g, int posX, int posY){
|
||||
@Override
|
||||
public void DrawWheels(Graphics g, int posX, int posY){
|
||||
g.setColor(Color.black);
|
||||
int count = 0;
|
||||
switch (countWhils){
|
||||
switch (_wheelsCount){
|
||||
case Min -> {
|
||||
count = 2;
|
||||
posX += 15;
|
||||
@ -38,8 +33,28 @@ public class DrawningWheels {
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < count; ++i){
|
||||
g.fillOval(posX + (i * 15), posY + 55, 15, 15);
|
||||
g.fillOval(posX + 60 + (i * 15), posY + 55, 15, 15);
|
||||
drawWheel(g, posX + (i * 18), posY + 55);
|
||||
drawWheel(g, posX + 75 + (i * 18), posY + 55);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setWheelsCount(int count) {
|
||||
count = Math.min(4, count);
|
||||
count = Math.max(2, count);
|
||||
switch (count) {
|
||||
case 2:
|
||||
_wheelsCount = CountEnum.Min;
|
||||
break;
|
||||
case 3:
|
||||
_wheelsCount = CountEnum.Mid;
|
||||
break;
|
||||
case 4:
|
||||
_wheelsCount = CountEnum.Max;
|
||||
break;
|
||||
}
|
||||
}
|
||||
public String toString(){
|
||||
return "Обычное колёса";
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,22 @@
|
||||
package DrawningObjects;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawningWheelsPolygon extends DrawningWheels {
|
||||
private final int _segmentsCount = 6;
|
||||
private final double _angle = Math.PI * 2 / _segmentsCount;
|
||||
@Override
|
||||
protected void drawWheel(Graphics g, int posX, int posY){
|
||||
g.drawOval(posX, posY, _wheelsSize, _wheelsSize);
|
||||
int[] pointsPosX = new int[_segmentsCount];
|
||||
int[] pointsPosY = new int[_segmentsCount];
|
||||
for (int i = 0; i < _segmentsCount; ++i){
|
||||
pointsPosX[i] = (int)((Math.cos(_angle * i) + 1) * _wheelsSize / 2) + posX;
|
||||
pointsPosY[i] = (int)((Math.sin(_angle * i) + 1) * _wheelsSize / 2) + posY;
|
||||
}
|
||||
g.drawPolygon(pointsPosX, pointsPosY, _segmentsCount);
|
||||
}
|
||||
public String toString(){
|
||||
return "колёса с узором многоугольник";
|
||||
}
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
package DrawningObjects;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawningWheelsStar extends DrawningWheels {
|
||||
private final int _segmentsCount = 8;
|
||||
private final double _angle = Math.PI * 2 / _segmentsCount;
|
||||
@Override
|
||||
protected void drawWheel(Graphics g, int posX, int posY){
|
||||
g.drawOval(posX, posY, _wheelsSize, _wheelsSize);
|
||||
int[] pointsPosX = new int[_segmentsCount];
|
||||
int[] pointsPosY = new int[_segmentsCount];
|
||||
for (int i = 0; i < _segmentsCount; ++i){
|
||||
pointsPosX[i] = (int)((Math.cos(_angle * i) + 1) * _wheelsSize / 2) + posX;
|
||||
pointsPosY[i] = (int)((Math.sin(_angle * i) + 1) * _wheelsSize / 2) + posY;
|
||||
}
|
||||
for (int i = 0; i < _segmentsCount; ++i){
|
||||
int inversePoint = (i + _segmentsCount / 2 + 1) % _segmentsCount;
|
||||
g.drawLine(pointsPosX[i], pointsPosY[i],
|
||||
pointsPosX[inversePoint], pointsPosY[inversePoint]);
|
||||
}
|
||||
}
|
||||
public String toString(){
|
||||
return "колёса с узором звезда";
|
||||
}
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
package DrawningObjects;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public interface IDrawWheels {
|
||||
public CountEnum WheelsCount();
|
||||
public void DrawWheels(Graphics g, int posX, int posY);
|
||||
public void setWheelsCount(int count);
|
||||
}
|
@ -1,27 +1,19 @@
|
||||
package Entities;
|
||||
|
||||
import DrawningObjects.IDrawWheels;
|
||||
|
||||
import java.awt.*;
|
||||
import java.lang.FunctionalInterface;
|
||||
|
||||
public class EntityElectricLocomotive {
|
||||
public int Speed;
|
||||
public double Weight;
|
||||
public Color BodyColor;
|
||||
public class EntityElectricLocomotive extends EntityLocomotive {
|
||||
public Color AdditionalColor;
|
||||
public boolean Horns;
|
||||
public boolean Battery;
|
||||
public double Step;
|
||||
|
||||
public void Init(int speed, double weight, Color bodyColor, Color
|
||||
additionalColor, boolean horns, boolean battery)
|
||||
{
|
||||
Speed = speed;
|
||||
Weight = weight;
|
||||
BodyColor = bodyColor;
|
||||
AdditionalColor = additionalColor;
|
||||
public EntityElectricLocomotive(int speed, double weight,
|
||||
Color bodyColor, Color additionalColor,
|
||||
boolean horns, boolean battery) {
|
||||
super(speed, weight, bodyColor);
|
||||
Horns = horns;
|
||||
Battery = battery;
|
||||
Step = Speed * 100.0f / Weight;
|
||||
}
|
||||
|
||||
}
|
||||
|
19
ProjectElectricLocomotive/src/Entities/EntityLocomotive.java
Normal file
19
ProjectElectricLocomotive/src/Entities/EntityLocomotive.java
Normal file
@ -0,0 +1,19 @@
|
||||
package Entities;
|
||||
|
||||
import DrawningObjects.IDrawWheels;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class EntityLocomotive {
|
||||
public int Speed;
|
||||
public double Weight;
|
||||
public Color BodyColor;
|
||||
public double Step;
|
||||
public EntityLocomotive(int speed, double weight, Color bodyColor)
|
||||
{
|
||||
Speed = speed;
|
||||
Weight = weight;
|
||||
BodyColor = bodyColor;
|
||||
Step = Speed * 100.0f / Weight;
|
||||
}
|
||||
}
|
@ -1,7 +1,6 @@
|
||||
package Forms;
|
||||
|
||||
import DrawningObjects.CountEnum;
|
||||
import DrawningObjects.DrawningElectricLocomotive;
|
||||
import DrawningObjects.*;
|
||||
import MovementStrategy.*;
|
||||
|
||||
import javax.swing.*;
|
||||
@ -11,23 +10,44 @@ import java.awt.event.ActionListener;
|
||||
import java.util.Random;
|
||||
|
||||
public class FormElectricLocomotive extends JFrame {
|
||||
private DrawningElectricLocomotive _drawningElectricLocomotive;
|
||||
private DrawningLocomotive _drawningLocomotive;
|
||||
private AbstractStrategy _abstractStrategy;
|
||||
private Canvas canvas;
|
||||
private JButton buttonCreate;
|
||||
private JButton buttonCreateElectricLocomotive;
|
||||
private JButton buttonCreateLocomotive;
|
||||
private JButton buttonUp;
|
||||
private JButton buttonRight;
|
||||
private JButton buttonDown;
|
||||
private JButton buttonLeft;
|
||||
private JButton buttonStep;
|
||||
private JTextField numberField;
|
||||
private JComboBox<AbstractStrategy> comboBoxStrategy;
|
||||
private JComboBox<IDrawWheels> comboBoxWheels;
|
||||
public void Draw() {
|
||||
if (_drawningElectricLocomotive == null)
|
||||
if (_drawningLocomotive == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
_drawningElectricLocomotive.DrawTransport(canvas.getGraphics());
|
||||
_drawningLocomotive.DrawTransport(canvas.getGraphics());
|
||||
}
|
||||
private void InitializeComponent(){
|
||||
buttonCreate = new JButton("Создать");
|
||||
buttonCreateElectricLocomotive = new JButton("Создать Электропоезд");
|
||||
buttonCreateElectricLocomotive.setMargin(new Insets(0, 0, 0, 0));
|
||||
|
||||
buttonCreateLocomotive = new JButton("Создать локомотив");
|
||||
buttonCreateLocomotive.setMargin(new Insets(0, 0, 0, 0));
|
||||
|
||||
buttonStep = new JButton("шаг");
|
||||
buttonStep.setName("шаг");
|
||||
|
||||
comboBoxStrategy = new JComboBox<>();
|
||||
comboBoxStrategy.addItem(new MoveToCenter());
|
||||
comboBoxStrategy.addItem(new MoveToBorder());
|
||||
|
||||
comboBoxWheels = new JComboBox<>();
|
||||
comboBoxWheels.addItem(new DrawningWheels());
|
||||
comboBoxWheels.addItem(new DrawningWheelsPolygon());
|
||||
comboBoxWheels.addItem(new DrawningWheelsStar());
|
||||
|
||||
buttonUp = new JButton();
|
||||
buttonUp.setBorderPainted(false);
|
||||
@ -68,27 +88,65 @@ public class FormElectricLocomotive extends JFrame {
|
||||
setLayout(null);
|
||||
|
||||
canvas = new Canvas();
|
||||
canvas.setBounds(0, 0, 1000, 600);
|
||||
buttonCreate.setBounds(10, 520, 120, 40);
|
||||
buttonUp.setBounds(50, 430, 40, 40);
|
||||
buttonDown.setBounds(50, 470, 40, 40);
|
||||
buttonRight.setBounds(90, 470, 40, 40);
|
||||
buttonLeft.setBounds(10, 470, 40, 40);
|
||||
numberField.setBounds(150, 520, 40, 40);
|
||||
canvas.setBounds(0, 0, 980, 560);
|
||||
buttonCreateElectricLocomotive.setBounds(10, 470, 200, 40);
|
||||
buttonCreateLocomotive.setBounds(10, 520, 200, 40);
|
||||
buttonUp.setBounds(50, 380, 40, 40);
|
||||
buttonDown.setBounds(50, 420, 40, 40);
|
||||
buttonRight.setBounds(90, 420, 40, 40);
|
||||
buttonLeft.setBounds(10, 420, 40, 40);
|
||||
numberField.setBounds(220, 470, 40, 40);
|
||||
buttonStep.setBounds(770, 50, 200, 30);
|
||||
comboBoxStrategy.setBounds(770, 10, 200, 30);
|
||||
comboBoxWheels.setBounds(220, 520, 250, 40);
|
||||
|
||||
|
||||
add(buttonCreate);
|
||||
add(buttonCreateElectricLocomotive);
|
||||
add(buttonCreateLocomotive);
|
||||
add(buttonUp);
|
||||
add(buttonDown);
|
||||
add(buttonRight);
|
||||
add(buttonLeft);
|
||||
add(numberField);
|
||||
add(buttonStep);
|
||||
add(comboBoxStrategy);
|
||||
add(comboBoxWheels);
|
||||
add(canvas);
|
||||
}
|
||||
private void InitializeLogic(){
|
||||
buttonCreate.addActionListener(
|
||||
buttonCreateElectricLocomotive.addActionListener(
|
||||
new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e){
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
int countWheels;
|
||||
try {
|
||||
countWheels = Integer.parseInt(numberField.getText());
|
||||
} catch (Exception ex) {
|
||||
countWheels = 0;
|
||||
}
|
||||
IDrawWheels _drawWheels = (IDrawWheels) comboBoxWheels.getSelectedItem();
|
||||
_drawWheels.setWheelsCount(countWheels);
|
||||
System.out.println(e.getActionCommand());
|
||||
Random random = new Random();
|
||||
_drawningLocomotive = new DrawningElectricLocomotive(
|
||||
random.nextInt(100, 300), random.nextInt(1000, 3000),
|
||||
new Color(random.nextInt(0, 256),
|
||||
random.nextInt(0, 256),
|
||||
random.nextInt(0, 256)),
|
||||
new Color(random.nextInt(0, 256),
|
||||
random.nextInt(0, 256),
|
||||
random.nextInt(0, 256)),
|
||||
comboBoxWheels.getSelectedIndex(), countWheels,
|
||||
random.nextInt(0, 2) == 1, random.nextInt(0, 2) == 1,
|
||||
canvas.getWidth(), canvas.getHeight()
|
||||
);
|
||||
_drawningLocomotive.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100));
|
||||
Draw();
|
||||
}
|
||||
}
|
||||
);
|
||||
buttonCreateLocomotive.addActionListener(
|
||||
new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
int countWheels;
|
||||
try {
|
||||
countWheels = Integer.parseInt(numberField.getText());
|
||||
@ -98,38 +156,68 @@ public class FormElectricLocomotive extends JFrame {
|
||||
}
|
||||
System.out.println(e.getActionCommand());
|
||||
Random random = new Random();
|
||||
_drawningElectricLocomotive = new DrawningElectricLocomotive();
|
||||
_drawningElectricLocomotive.Init(random.nextInt(100, 300), random.nextInt(1000, 3000),
|
||||
new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)),
|
||||
new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)),
|
||||
countWheels,
|
||||
random.nextInt(0, 2) == 1, random.nextInt(0, 2) == 1,
|
||||
1000, 560);
|
||||
_drawningElectricLocomotive.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100));
|
||||
_drawningLocomotive = new DrawningLocomotive(
|
||||
random.nextInt(100, 300), random.nextInt(1000, 3000),
|
||||
new Color(random.nextInt(0, 256),
|
||||
random.nextInt(0, 256),
|
||||
random.nextInt(0, 256)),
|
||||
comboBoxWheels.getSelectedIndex(), countWheels,
|
||||
canvas.getWidth(), canvas.getHeight()
|
||||
);
|
||||
_drawningLocomotive.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100));
|
||||
Draw();
|
||||
}
|
||||
}
|
||||
);
|
||||
buttonStep.addActionListener(
|
||||
new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
System.out.println(((JButton)(e.getSource())).getName());
|
||||
if (_drawningLocomotive == null){
|
||||
return;
|
||||
}
|
||||
if (comboBoxStrategy.isEnabled()){
|
||||
_abstractStrategy = (AbstractStrategy) comboBoxStrategy.getSelectedItem();
|
||||
if (_abstractStrategy == null){
|
||||
return;
|
||||
}
|
||||
_abstractStrategy.SetData(new DrawningObjectLocomotive(_drawningLocomotive),
|
||||
canvas.getWidth(), canvas.getHeight());
|
||||
comboBoxStrategy.setEnabled(false);
|
||||
}
|
||||
if (_abstractStrategy == null){
|
||||
return;
|
||||
}
|
||||
_abstractStrategy.MakeStep();
|
||||
Draw();
|
||||
if (_abstractStrategy.GetStatus() == Status.Finish){
|
||||
comboBoxStrategy.setEnabled(true);
|
||||
_abstractStrategy = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
ActionListener actionListener = new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e){
|
||||
System.out.println(((JButton)(e.getSource())).getName());
|
||||
if (_drawningElectricLocomotive == null)
|
||||
if (_drawningLocomotive == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
switch(((JButton)(e.getSource())).getName()){
|
||||
case "up":
|
||||
_drawningElectricLocomotive.MoveTransport(DirectionType.Up);
|
||||
_drawningLocomotive.MoveTransport(DirectionType.Up);
|
||||
break;
|
||||
case "down":
|
||||
_drawningElectricLocomotive.MoveTransport(DirectionType.Down);
|
||||
_drawningLocomotive.MoveTransport(DirectionType.Down);
|
||||
break;
|
||||
case "left":
|
||||
_drawningElectricLocomotive.MoveTransport(DirectionType.Left);
|
||||
_drawningLocomotive.MoveTransport(DirectionType.Left);
|
||||
break;
|
||||
case "right":
|
||||
_drawningElectricLocomotive.MoveTransport(DirectionType.Right);
|
||||
_drawningLocomotive.MoveTransport(DirectionType.Right);
|
||||
break;
|
||||
}
|
||||
Draw();
|
||||
|
@ -0,0 +1,93 @@
|
||||
package MovementStrategy;
|
||||
|
||||
public abstract class AbstractStrategy {
|
||||
private IMoveableObject _movableObject;
|
||||
private Status _state = Status.NotInit;
|
||||
private int _fieldWidth;
|
||||
|
||||
protected int FieldWidth() {
|
||||
return _fieldWidth;
|
||||
}
|
||||
|
||||
private int _fieldHeight;
|
||||
|
||||
protected int FieldHeight() {
|
||||
return _fieldHeight;
|
||||
}
|
||||
|
||||
public Status GetStatus() {
|
||||
return _state;
|
||||
}
|
||||
|
||||
public void SetData(IMoveableObject movableObject, int width, int height) {
|
||||
if (movableObject == null) {
|
||||
_state = Status.NotInit;
|
||||
return;
|
||||
}
|
||||
_state = Status.InProgress;
|
||||
_movableObject = movableObject;
|
||||
_fieldWidth = width;
|
||||
_fieldHeight = height;
|
||||
}
|
||||
|
||||
public void MakeStep() {
|
||||
if (_state != Status.InProgress) {
|
||||
return;
|
||||
}
|
||||
if (IsTargetDestination()) {
|
||||
_state = Status.Finish;
|
||||
return;
|
||||
}
|
||||
MoveToTarget();
|
||||
}
|
||||
|
||||
protected boolean MoveLeft() {
|
||||
return MoveTo(DirectionType.Left);
|
||||
}
|
||||
|
||||
protected boolean MoveRight() {
|
||||
return MoveTo(DirectionType.Right);
|
||||
}
|
||||
|
||||
protected boolean MoveUp() {
|
||||
return MoveTo(DirectionType.Up);
|
||||
}
|
||||
|
||||
protected boolean MoveDown() {
|
||||
return MoveTo(DirectionType.Down);
|
||||
}
|
||||
|
||||
protected ObjectParameters GetObjectParameters() {
|
||||
if (_movableObject != null) {
|
||||
return _movableObject.GetObjectPosition();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
protected int GetStep() {
|
||||
if (_state != Status.InProgress) {
|
||||
return 0;
|
||||
}
|
||||
return _movableObject.GetStep();
|
||||
}
|
||||
|
||||
protected abstract void MoveToTarget();
|
||||
|
||||
protected abstract boolean IsTargetDestination();
|
||||
|
||||
private boolean MoveTo(DirectionType directionType) {
|
||||
if (_state != Status.InProgress) {
|
||||
return false;
|
||||
}
|
||||
if (_movableObject != null && _movableObject.CheckCanMove(directionType)) {
|
||||
_movableObject.MoveObject(directionType);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void SetFieldSize(int width, int height) {
|
||||
_fieldWidth = width;
|
||||
_fieldHeight = height;
|
||||
}
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
package MovementStrategy;
|
||||
|
||||
import DrawningObjects.DrawningLocomotive;
|
||||
|
||||
public class DrawningObjectLocomotive implements IMoveableObject {
|
||||
private DrawningLocomotive _drawningLocomotive = null;
|
||||
public DrawningObjectLocomotive(DrawningLocomotive drawningLocomotive)
|
||||
{
|
||||
_drawningLocomotive = drawningLocomotive;
|
||||
}
|
||||
@Override
|
||||
public ObjectParameters GetObjectPosition() {
|
||||
if (_drawningLocomotive == null || _drawningLocomotive.entityLocomotive == null){
|
||||
return null;
|
||||
}
|
||||
return new ObjectParameters(
|
||||
_drawningLocomotive.GetPosX(), _drawningLocomotive.GetPosY(),
|
||||
_drawningLocomotive.GetWidth(), _drawningLocomotive.GetHeight());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int GetStep() {
|
||||
if (_drawningLocomotive == null){
|
||||
return 0;
|
||||
}
|
||||
return (int)_drawningLocomotive.entityLocomotive.Step;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean CheckCanMove(DirectionType direction) {
|
||||
if (_drawningLocomotive == null){
|
||||
return false;
|
||||
}
|
||||
return _drawningLocomotive.CanMove(direction);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void MoveObject(DirectionType direction) {
|
||||
if (_drawningLocomotive == null){
|
||||
return;
|
||||
}
|
||||
_drawningLocomotive.MoveTransport(direction);
|
||||
}
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
package MovementStrategy;
|
||||
|
||||
public interface IMoveableObject {
|
||||
public ObjectParameters GetObjectPosition();
|
||||
int GetStep();
|
||||
boolean CheckCanMove(DirectionType direction);
|
||||
void MoveObject(DirectionType direction);
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
package MovementStrategy;
|
||||
|
||||
public class MoveToBorder extends AbstractStrategy{
|
||||
@Override
|
||||
protected void MoveToTarget() {
|
||||
var objParams = GetObjectParameters();
|
||||
if (objParams == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
MoveRight();
|
||||
MoveDown();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean IsTargetDestination() {
|
||||
var objParams = GetObjectParameters();
|
||||
if (objParams == null){
|
||||
return false;
|
||||
}
|
||||
return objParams.RightBorder() <= FieldWidth() &&
|
||||
objParams.RightBorder() + GetStep() >= FieldWidth() &&
|
||||
objParams.DownBorder() <= FieldHeight() &&
|
||||
objParams.DownBorder() + GetStep() >= FieldHeight();
|
||||
}
|
||||
|
||||
public String toString(){
|
||||
return "идти к краю экрана";
|
||||
}
|
||||
}
|
@ -0,0 +1,53 @@
|
||||
package MovementStrategy;
|
||||
|
||||
public class MoveToCenter extends AbstractStrategy{
|
||||
|
||||
@Override
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean IsTargetDestination() {
|
||||
var objParams = GetObjectParameters();
|
||||
if (objParams == null){
|
||||
return false;
|
||||
}
|
||||
return objParams.ObjectMiddleHorizontal() <= FieldWidth() / 2 &&
|
||||
objParams.ObjectMiddleHorizontal() + GetStep() >= FieldWidth() / 2 &&
|
||||
objParams.ObjectMiddleVertical() <= FieldHeight() / 2 &&
|
||||
objParams.ObjectMiddleVertical() + GetStep() >= FieldHeight() / 2;
|
||||
}
|
||||
|
||||
public String toString(){
|
||||
return "идти к центру экрана";
|
||||
}
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
package MovementStrategy;
|
||||
|
||||
public class ObjectParameters {
|
||||
private int _x;
|
||||
private int _y;
|
||||
private int _width;
|
||||
private int _height;
|
||||
public int LeftBorder(){
|
||||
return _x;
|
||||
}
|
||||
public int TopBorder(){
|
||||
return _y;
|
||||
}
|
||||
public int RightBorder(){
|
||||
return _x + _width;
|
||||
}
|
||||
public int DownBorder(){
|
||||
return _y + _height;
|
||||
}
|
||||
public int ObjectMiddleHorizontal(){
|
||||
return _x + _width / 2;
|
||||
}
|
||||
public int ObjectMiddleVertical(){
|
||||
return _y + _height / 2;
|
||||
}
|
||||
public ObjectParameters(int x, int y, int width, int height){
|
||||
_x = x;
|
||||
_y = y;
|
||||
_width = width;
|
||||
_height = height;
|
||||
}
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package MovementStrategy;
|
||||
|
||||
public enum Status {
|
||||
NotInit,
|
||||
InProgress,
|
||||
Finish
|
||||
}
|
Loading…
Reference in New Issue
Block a user