PIbd-21, Bakalskaya E.D. LabWork2 HARD #2
84
ProjectElectricLocomotive/AbstractStrategy.java
Normal file
84
ProjectElectricLocomotive/AbstractStrategy.java
Normal file
@ -0,0 +1,84 @@
|
||||
package ProjectElectricLocomotive;
|
||||
|
||||
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(DyrectionType.Left);
|
||||
}
|
||||
|
||||
protected boolean MoveRight(){
|
||||
return MoveTo(DyrectionType.Right);
|
||||
}
|
||||
|
||||
protected boolean MoveUp(){
|
||||
return MoveTo(DyrectionType.Up);
|
||||
}
|
||||
|
||||
protected boolean MoveDown(){
|
||||
return MoveTo(DyrectionType.Down);
|
||||
}
|
||||
|
||||
/// Параметры объекта
|
||||
protected ObjectParameters GetObjectParameters(){
|
||||
if(_moveableObject == null) return null;
|
||||
return _moveableObject.GetObjectPosition();
|
||||
}
|
||||
|
||||
protected int GetStep()
|
||||
{
|
||||
if (_state != Status.InProgress)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
if(_moveableObject == null) return -1;
|
||||
return _moveableObject.GetStep();
|
||||
}
|
||||
protected abstract void MoveToTarget();
|
||||
|
||||
protected abstract boolean IsTargetDestinaion();
|
||||
|
||||
private boolean MoveTo(DyrectionType directionType)
|
||||
{
|
||||
if (_state != Status.InProgress)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (_moveableObject.CheckCanMove(directionType) == false) return false;
|
||||
{
|
||||
_moveableObject.MoveObject(directionType);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,173 +1,37 @@
|
||||
package ProjectElectricLocomotive;
|
||||
import java.awt.*;
|
||||
public class DrawingElectricLocomotive {
|
||||
public EntityElectricLocomotive EntityElectricLocomotive;
|
||||
private DrawingWheel _drawingWheel;
|
||||
private int _pictureWidth;
|
||||
private int _pictureHeight;
|
||||
private int _startPosX;
|
||||
private int _startPosY;
|
||||
private final int _locoWidth = 150;
|
||||
private final int _locoHeight = 50;
|
||||
|
||||
public boolean Init(int speed, double weight, Color bodyColor, Color additionalColor,
|
||||
boolean horns, boolean seifbatteries, int width, int height)
|
||||
public class DrawingElectricLocomotive extends DrawingLocomotive {
|
||||
public DrawingElectricLocomotive(int speed, double weight, Color bodyColor, Color additionalColor,
|
||||
boolean horns, boolean seifBatteries, int width, int height)
|
||||
{
|
||||
|
||||
if (width < _locoWidth || height < _locoHeight)
|
||||
super(speed, weight, bodyColor, width, height, 150, 50);
|
||||
if (EntityLocomotive != null)
|
||||
{
|
||||
return false;
|
||||
EntityLocomotive = new EntityElectricLocomotive(speed, width, bodyColor, additionalColor, horns, seifBatteries);
|
||||
}
|
||||
_pictureWidth = width;
|
||||
_pictureHeight = height;
|
||||
EntityElectricLocomotive = new EntityElectricLocomotive();
|
||||
_drawingWheel = new DrawingWheel();
|
||||
EntityElectricLocomotive.Init(speed, weight, bodyColor, additionalColor, horns, seifbatteries);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void SetWheelsCount(int weelsCount) {
|
||||
_drawingWheel.SetWheelsCount(weelsCount);
|
||||
}
|
||||
|
||||
public void SetPosition(int x, int y)
|
||||
@Override
|
||||
public void DrawTransport(Graphics g)
|
||||
{
|
||||
if (x < 0 || x + _locoWidth > _pictureWidth)
|
||||
if (EntityLocomotive instanceof EntityElectricLocomotive electricLocomotive) ///////// WARNING INSTANCEOF
|
||||
{
|
||||
x = 20;
|
||||
}
|
||||
if (y < 0 || y + _locoHeight > _pictureHeight)
|
||||
{
|
||||
y = 20;
|
||||
}
|
||||
_startPosX = x;
|
||||
_startPosY = y;
|
||||
}
|
||||
Color colorBlack = Color.BLACK;
|
||||
|
||||
public void MoveTransport(DyrectionType direction){
|
||||
if(EntityElectricLocomotive == null) return;
|
||||
switch(direction)
|
||||
{
|
||||
case Up -> {
|
||||
if(_startPosY - EntityElectricLocomotive.Step() >= 0)
|
||||
_startPosY -= (int) EntityElectricLocomotive.Step();
|
||||
if (electricLocomotive.Horns) {
|
||||
//horns
|
||||
g.setColor(colorBlack);
|
||||
g.fillRect(_startPosX + 30, _startPosY + 15, 20, 5);
|
||||
g.drawLine(_startPosX + 40, _startPosY + 15, _startPosX + 50, _startPosY + 10);
|
||||
g.drawLine(_startPosX + 50, _startPosY + 10, _startPosX + 45, _startPosY);
|
||||
g.drawLine(_startPosX + 45, _startPosY + 15, _startPosX + 50, _startPosY + 10);
|
||||
g.drawLine(_startPosX + 50, _startPosY + 10, _startPosX + 40, _startPosY);
|
||||
}
|
||||
case Down -> {
|
||||
if(_startPosY + EntityElectricLocomotive.Step() + _locoHeight <= _pictureHeight)
|
||||
_startPosY += (int) EntityElectricLocomotive.Step();
|
||||
}
|
||||
case Left -> {
|
||||
if(_startPosX - EntityElectricLocomotive.Step() >= 0)
|
||||
_startPosX -= (int) EntityElectricLocomotive.Step();
|
||||
}
|
||||
case Right -> {
|
||||
if(_startPosX + EntityElectricLocomotive.Step() + _locoWidth <= _pictureWidth)
|
||||
_startPosX += (int) EntityElectricLocomotive.Step();
|
||||
|
||||
if (electricLocomotive.SeifBatteries) {
|
||||
g.setColor(colorBlack);
|
||||
g.fillRect(_startPosX + 80, _startPosY + 30, 5, 10);
|
||||
}
|
||||
super.DrawTransport(g);
|
||||
}
|
||||
}
|
||||
public void DrawTransport(Graphics g) {
|
||||
if (EntityElectricLocomotive == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
Graphics2D g2d = (Graphics2D) g;
|
||||
|
||||
Color bodyColor = EntityElectricLocomotive.BodyColor;
|
||||
Color additionalColor = EntityElectricLocomotive.AdditionalColor;
|
||||
Color blackBrush = Color.BLACK;
|
||||
Color windowsColor = Color.BLUE;
|
||||
|
||||
_drawingWheel.DrawWheels(g, additionalColor, _startPosX, _startPosY, 5, 5);
|
||||
|
||||
if(EntityElectricLocomotive.Horns)
|
||||
{
|
||||
g2d.fillRect(_startPosX + 30, _startPosY + 15, 20, 5);
|
||||
g2d.drawLine(_startPosX + 40, _startPosY + 15, _startPosX + 50, _startPosY + 10);
|
||||
g2d.drawLine(_startPosX + 50, _startPosY + 10, _startPosX + 45, _startPosY);
|
||||
g2d.drawLine(_startPosX + 45, _startPosY + 15, _startPosX + 50, _startPosY + 10);
|
||||
g2d.drawLine(_startPosX + 50, _startPosY + 10, _startPosX + 40, _startPosY);
|
||||
g2d.setColor(blackBrush);
|
||||
}
|
||||
|
||||
//locomotive
|
||||
Polygon loco = new Polygon();
|
||||
|
||||
loco.addPoint(_startPosX, _startPosY + 40);
|
||||
loco.addPoint(_startPosX, _startPosY + 30);
|
||||
loco.addPoint(_startPosX + 20, _startPosY + 20);
|
||||
loco.addPoint(_startPosX + 70, _startPosY + 20);
|
||||
loco.addPoint(_startPosX +80, _startPosY + 30);
|
||||
loco.addPoint(_startPosX +80, _startPosY + 40);
|
||||
loco.addPoint(_startPosX +75, _startPosY + 45);
|
||||
loco.addPoint(_startPosX +5, _startPosY + 45);
|
||||
loco.addPoint(_startPosX, _startPosY + 40);
|
||||
|
||||
g2d.setColor(blackBrush);
|
||||
g2d.drawPolygon(loco);
|
||||
g2d.setColor(bodyColor);
|
||||
g2d.fillPolygon(loco);
|
||||
// windows
|
||||
Polygon window = new Polygon();
|
||||
window.addPoint(_startPosX + 10, _startPosY + 30);
|
||||
window.addPoint(_startPosX +15, _startPosY + 25);
|
||||
window.addPoint(_startPosX + 20, _startPosY + 25);
|
||||
window.addPoint(_startPosX + 20, _startPosY + 30);
|
||||
window.addPoint(_startPosX +10, _startPosY + 30);
|
||||
|
||||
g2d.setColor(blackBrush);
|
||||
g2d.drawPolygon(window);
|
||||
g2d.setColor(windowsColor);
|
||||
g2d.fillPolygon(window);
|
||||
|
||||
g2d.fillRect(_startPosX + 25, _startPosY + 25, 10, 5);
|
||||
g2d.setColor(windowsColor);
|
||||
g2d.drawRect(_startPosX + 25, _startPosY + 25, 10, 5);
|
||||
g2d.setColor(blackBrush);
|
||||
//locomotive
|
||||
|
||||
if(EntityElectricLocomotive.SeifBatteries)
|
||||
{
|
||||
g2d.drawRect(_startPosX + 50, _startPosY + 25, 20, 10);
|
||||
g2d.setColor(blackBrush);
|
||||
}
|
||||
|
||||
|
||||
//обязательные колеса
|
||||
//loco
|
||||
g2d.fillOval(_startPosX + 10, _startPosY + 45, 5, 5);
|
||||
g2d.fillOval(_startPosX + 25, _startPosY + 45, 5, 5);
|
||||
g2d.fillOval(_startPosX + 50, _startPosY + 45, 5, 5);
|
||||
g2d.fillOval(_startPosX + 65, _startPosY + 45, 5, 5);
|
||||
|
||||
|
||||
//telega
|
||||
g2d.setColor(blackBrush);
|
||||
g2d.fillOval(_startPosX + 95, _startPosY + 45, 5, 5);
|
||||
g2d.fillOval(_startPosX + 140, _startPosY + 45, 5, 5);
|
||||
|
||||
//telejka
|
||||
Polygon telega = new Polygon();
|
||||
|
||||
telega.addPoint(_startPosX + 90, _startPosY + 25);
|
||||
telega.addPoint(_startPosX + 95, _startPosY + 20);
|
||||
telega.addPoint(_startPosX + 145, _startPosY + 20);
|
||||
telega.addPoint(_startPosX + 150, _startPosY + 25);
|
||||
telega.addPoint(_startPosX + 150, _startPosY + 45);
|
||||
telega.addPoint(_startPosX + 90, _startPosY + 45);
|
||||
telega.addPoint(_startPosX + 90, _startPosY + 25);
|
||||
|
||||
g2d.setColor(additionalColor);
|
||||
g2d.fillPolygon(telega);
|
||||
g2d.setColor(blackBrush);
|
||||
g2d.drawPolygon(telega);
|
||||
//telejka
|
||||
|
||||
//телега окна
|
||||
g2d.setColor(blackBrush);
|
||||
g.drawLine(_startPosX + 80, _startPosY + 40, _startPosX + 90, _startPosY + 40);
|
||||
g2d.setColor(windowsColor); g.fillRect(_startPosX + 95, _startPosY + 30, 10, 5);
|
||||
g.fillRect(_startPosX + 115, _startPosY + 30, 10, 5);
|
||||
g.fillRect(_startPosX + 135, _startPosY + 30, 10, 5);
|
||||
}
|
||||
}
|
||||
|
28
ProjectElectricLocomotive/DrawingEmptyWheels.java
Normal file
28
ProjectElectricLocomotive/DrawingEmptyWheels.java
Normal file
@ -0,0 +1,28 @@
|
||||
package ProjectElectricLocomotive;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawingEmptyWheels implements IDrawingWheels{
|
||||
private WheelsCount _wheelsCount;
|
||||
@Override
|
||||
public void SetWheelsCount(int wheelsCount) {
|
||||
for (WheelsCount val : WheelsCount.values()) {
|
||||
if (val.count == wheelsCount) {
|
||||
_wheelsCount = val;
|
||||
return;
|
||||
}
|
||||
}
|
||||
this._wheelsCount = WheelsCount.Three;
|
||||
}
|
||||
|
||||
@Override
|
||||
public WheelsCount GetWheelsCount() {
|
||||
return _wheelsCount;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void DrawWheel(Graphics2D g2d, Color color, int x, int y, int w, int h) {
|
||||
g2d.setColor(Color.BLACK);
|
||||
g2d.drawOval(x, y, w, h);
|
||||
}
|
||||
}
|
209
ProjectElectricLocomotive/DrawingLocomotive.java
Normal file
209
ProjectElectricLocomotive/DrawingLocomotive.java
Normal file
@ -0,0 +1,209 @@
|
||||
package ProjectElectricLocomotive;
|
||||
import java.awt.*;
|
||||
import java.security.cert.PolicyNode;
|
||||
import java.util.Random;
|
||||
|
||||
public class DrawingLocomotive {
|
||||
public EntityLocomotive EntityLocomotive;
|
||||
protected IDrawingWheels _drawingWheels;
|
||||
private int _pictureWidth;
|
||||
private int _pictureHeight;
|
||||
public int _startPosX;
|
||||
public int _startPosY;
|
||||
private int _locoWidth = 150;
|
||||
private int _locoHeight = 50;
|
||||
|
||||
public int GetPosX(){
|
||||
return _startPosX;
|
||||
}
|
||||
public int GetPosY(){
|
||||
return _startPosY;
|
||||
}
|
||||
public int GetWidth(){
|
||||
return _locoWidth;
|
||||
}
|
||||
public int GetHeight(){
|
||||
return _locoHeight;
|
||||
}
|
||||
|
||||
public DrawingLocomotive(int speed, double weight, Color bodyColor, int width, int heigth)
|
||||
{
|
||||
if (width < _locoWidth || heigth < _locoHeight)
|
||||
{
|
||||
return;
|
||||
}
|
||||
_pictureWidth = width;
|
||||
_pictureHeight = heigth;
|
||||
EntityLocomotive = new EntityLocomotive(speed, weight, bodyColor);
|
||||
|
||||
Random rnd = new Random();
|
||||
int WhatWheels = rnd.nextInt(0, 3);
|
||||
|
||||
if(WhatWheels == 0) _drawingWheels = new DrawingWheel();
|
||||
if(WhatWheels == 1) _drawingWheels = new DrawingEmptyWheels();
|
||||
if(WhatWheels == 2) _drawingWheels = new DrawingWheelsBlueCrom();
|
||||
}
|
||||
|
||||
protected DrawingLocomotive(int speed, double weight, Color bodyColor, int width,
|
||||
int height, int locoWidth, int locoHeight)
|
||||
{
|
||||
this(speed,weight, bodyColor, width, height);
|
||||
if (width < _locoWidth || height < _locoHeight)
|
||||
{
|
||||
return;
|
||||
}
|
||||
_pictureWidth = width;
|
||||
_pictureHeight = height;
|
||||
_locoWidth = locoWidth;
|
||||
_locoHeight = locoHeight;
|
||||
}
|
||||
|
||||
public void SetWheelsCount(int wheelsCount){
|
||||
_drawingWheels.SetWheelsCount(wheelsCount);
|
||||
}
|
||||
|
||||
//Установка позиции
|
||||
public void SetPosition(int x, int y)
|
||||
{
|
||||
if (x < 0 || x + _locoWidth > _pictureWidth)
|
||||
{
|
||||
x = _pictureWidth - _locoWidth;
|
||||
}
|
||||
if (y < 0 || y + _locoHeight > _pictureHeight)
|
||||
{
|
||||
y = _pictureHeight - _locoHeight;
|
||||
}
|
||||
_startPosX = x;
|
||||
_startPosY = y;
|
||||
}
|
||||
|
||||
public void MoveTransport(DyrectionType direction) {
|
||||
if (EntityLocomotive == null) {
|
||||
return;
|
||||
}
|
||||
switch (direction) {
|
||||
case Left:
|
||||
if (_startPosX - EntityLocomotive.Step() > 0) {
|
||||
_startPosX -= (int) EntityLocomotive.Step();
|
||||
}
|
||||
break;
|
||||
case Up:
|
||||
if (_startPosY - EntityLocomotive.Step() > 0) {
|
||||
_startPosY -= (int) EntityLocomotive.Step();
|
||||
}
|
||||
break;
|
||||
case Right:
|
||||
if (_startPosX + EntityLocomotive.Step() + _locoWidth < _pictureWidth) {
|
||||
_startPosX += (int) EntityLocomotive.Step();
|
||||
}
|
||||
break;
|
||||
case Down:
|
||||
if (_startPosY + EntityLocomotive.Step() + _locoHeight < _pictureHeight) {
|
||||
_startPosY += (int) EntityLocomotive.Step();
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public void DrawTransport(Graphics g)
|
||||
{
|
||||
{
|
||||
if (EntityLocomotive == null) return;
|
||||
}
|
||||
|
||||
Color colorBlack = Color.BLACK;
|
||||
Color windows = Color.BLUE;
|
||||
Color bodyColor = EntityLocomotive.BodyColor;
|
||||
|
||||
//локомотив
|
||||
g.setColor(bodyColor);
|
||||
Polygon locoP = new Polygon();
|
||||
locoP.addPoint(_startPosX, _startPosY + 40);
|
||||
locoP.addPoint(_startPosX, _startPosY + 30);
|
||||
locoP.addPoint(_startPosX + 20, _startPosY + 20);
|
||||
locoP.addPoint(_startPosX + 70, _startPosY + 20);
|
||||
locoP.addPoint(_startPosX +80, _startPosY + 30);
|
||||
locoP.addPoint(_startPosX +80, _startPosY + 40);
|
||||
locoP.addPoint(_startPosX +75, _startPosY + 45);
|
||||
locoP.addPoint(_startPosX +5, _startPosY + 45);
|
||||
locoP.addPoint(_startPosX, _startPosY + 40);
|
||||
g.fillPolygon(locoP);
|
||||
|
||||
g.setColor(colorBlack);
|
||||
g.drawPolygon(locoP);
|
||||
|
||||
//окошки
|
||||
Polygon window = new Polygon();
|
||||
window.addPoint(_startPosX + 10, _startPosY + 30);
|
||||
window.addPoint(_startPosX +15, _startPosY + 25);
|
||||
window.addPoint(_startPosX + 20, _startPosY + 25);
|
||||
window.addPoint(_startPosX + 20, _startPosY + 30);
|
||||
window.addPoint(_startPosX +10, _startPosY + 30);
|
||||
g.setColor(windows);
|
||||
g.fillPolygon(window);
|
||||
g.fillRect(_startPosX + 25, _startPosY + 25, 10, 5);
|
||||
g.setColor(Color.black);
|
||||
g.drawPolygon(window);
|
||||
g.drawRect(_startPosX + 25, _startPosY + 25, 10, 5);
|
||||
|
||||
//обязательные колеса
|
||||
//loco
|
||||
g.fillOval(_startPosX + 10, _startPosY + 45, 9, 9);
|
||||
g.fillOval(_startPosX + 25, _startPosY + 45, 9, 9);
|
||||
g.fillOval(_startPosX + 50, _startPosY + 45, 9, 9);
|
||||
g.fillOval(_startPosX + 65, _startPosY + 45, 9, 9);
|
||||
|
||||
//telejka
|
||||
Polygon telega = new Polygon();
|
||||
|
||||
telega.addPoint(_startPosX + 90, _startPosY + 25);
|
||||
telega.addPoint(_startPosX + 95, _startPosY + 20);
|
||||
telega.addPoint(_startPosX + 145, _startPosY + 20);
|
||||
telega.addPoint(_startPosX + 150, _startPosY + 25);
|
||||
telega.addPoint(_startPosX + 150, _startPosY + 45);
|
||||
telega.addPoint(_startPosX + 90, _startPosY + 45);
|
||||
telega.addPoint(_startPosX + 90, _startPosY + 25);
|
||||
|
||||
g.setColor(bodyColor);
|
||||
g.fillPolygon(telega);
|
||||
g.setColor(colorBlack);
|
||||
g.drawPolygon(telega);
|
||||
|
||||
//телега окна
|
||||
g.setColor(colorBlack);
|
||||
g.drawLine(_startPosX + 80, _startPosY + 40, _startPosX + 90, _startPosY + 40);
|
||||
g.setColor(windows); g.fillRect(_startPosX + 95, _startPosY + 30, 10, 5);
|
||||
g.fillRect(_startPosX + 115, _startPosY + 30, 10, 5);
|
||||
g.fillRect(_startPosX + 135, _startPosY + 30, 10, 5);
|
||||
|
||||
_drawingWheels.DrawWheels(g, colorBlack, _startPosX, _startPosY, 9,9);
|
||||
}
|
||||
|
||||
public boolean CanMove(DyrectionType direction)
|
||||
{
|
||||
if (EntityLocomotive == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
switch(direction) {
|
||||
//влево
|
||||
case Left:
|
||||
if (_startPosX - EntityLocomotive.Step() > 0) return true;
|
||||
break;
|
||||
case Up:
|
||||
if (_startPosY - EntityLocomotive.Step() > 0) return true;
|
||||
break;
|
||||
case Right:
|
||||
if (_startPosX + EntityLocomotive.Step() < _pictureWidth) return true;
|
||||
break;
|
||||
case Down:
|
||||
if (_startPosY + EntityLocomotive.Step() < _pictureHeight) return true;
|
||||
break;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public IMoveableObject GetMoveableObject() {
|
||||
return new DrawingObjectLocomotive(this);
|
||||
}
|
||||
}
|
30
ProjectElectricLocomotive/DrawingObjectLocomotive.java
Normal file
30
ProjectElectricLocomotive/DrawingObjectLocomotive.java
Normal file
@ -0,0 +1,30 @@
|
||||
package ProjectElectricLocomotive;
|
||||
|
||||
public class DrawingObjectLocomotive implements IMoveableObject {
|
||||
private DrawingLocomotive _drawningLocomotive = null;
|
||||
public DrawingObjectLocomotive(DrawingLocomotive drawningLocomotive)
|
||||
{
|
||||
_drawningLocomotive = drawningLocomotive;
|
||||
}
|
||||
public ObjectParameters GetObjectPosition()
|
||||
{
|
||||
if (_drawningLocomotive == null || _drawningLocomotive.EntityLocomotive == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return new ObjectParameters(_drawningLocomotive.GetPosX(), _drawningLocomotive.GetPosY(),
|
||||
_drawningLocomotive.GetWidth(), _drawningLocomotive.GetHeight());
|
||||
}
|
||||
public int GetStep(){
|
||||
if(_drawningLocomotive == null) return -1;
|
||||
return (int)(_drawningLocomotive.EntityLocomotive.Step());
|
||||
}
|
||||
public boolean CheckCanMove(DyrectionType direction){
|
||||
if(_drawningLocomotive == null) return false;
|
||||
return _drawningLocomotive.CanMove(direction);
|
||||
}
|
||||
public void MoveObject(DyrectionType direction){
|
||||
if(_drawningLocomotive == null) return;
|
||||
_drawningLocomotive.MoveTransport(direction);
|
||||
}
|
||||
}
|
@ -2,41 +2,25 @@ package ProjectElectricLocomotive;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawingWheel {
|
||||
public class DrawingWheel implements IDrawingWheels {
|
||||
private WheelsCount _wheelsCount;
|
||||
public void SetWheelsCount(int enginesCount) {
|
||||
public void SetWheelsCount(int wheelsCount) {
|
||||
for (WheelsCount val : WheelsCount.values()) {
|
||||
if (val.count == enginesCount) {
|
||||
this._wheelsCount = val;
|
||||
if (val.count == wheelsCount) {
|
||||
_wheelsCount = val;
|
||||
return;
|
||||
}
|
||||
}
|
||||
this._wheelsCount = WheelsCount.Three;
|
||||
}
|
||||
|
||||
private void DrawWheel(Graphics2D g2d, Color color, int x, int y, int w, int h) {
|
||||
@Override
|
||||
public WheelsCount GetWheelsCount() {
|
||||
return _wheelsCount;
|
||||
}
|
||||
|
||||
public void DrawWheel(Graphics2D g2d, Color color, int x, int y, int w, int h) {
|
||||
g2d.setColor(Color.BLACK);
|
||||
g2d.fillOval(x, y, w, h);
|
||||
}
|
||||
|
||||
public void DrawWheels(Graphics g, Color color, int startPosX, int startPosY, int drawingWidth, int drawingHeight) {
|
||||
if (_wheelsCount == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
Graphics2D g2d = (Graphics2D) g;
|
||||
int wheelWidth = 5;
|
||||
int wheelHeight = 5;
|
||||
|
||||
if (_wheelsCount.count >= _wheelsCount.Three.count) {
|
||||
DrawWheel(g2d, color, startPosX + 105, startPosY + 45, wheelWidth, wheelHeight
|
||||
);
|
||||
}
|
||||
|
||||
if (_wheelsCount.count >= _wheelsCount.Four.count) {
|
||||
DrawWheel(g2d, color, startPosX + 105, startPosY + 45, wheelWidth, wheelHeight
|
||||
);
|
||||
DrawWheel(g2d, color, startPosX + 130, startPosY + 45, wheelWidth, wheelHeight
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
35
ProjectElectricLocomotive/DrawingWheelsBlueCrom.java
Normal file
35
ProjectElectricLocomotive/DrawingWheelsBlueCrom.java
Normal file
@ -0,0 +1,35 @@
|
||||
package ProjectElectricLocomotive;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawingWheelsBlueCrom implements IDrawingWheels{
|
||||
private WheelsCount _wheelsCount;
|
||||
@Override
|
||||
public void SetWheelsCount(int wheelsCount) {
|
||||
for (WheelsCount val : WheelsCount.values()) {
|
||||
if (val.count == wheelsCount) {
|
||||
_wheelsCount = val;
|
||||
return;
|
||||
}
|
||||
}
|
||||
this._wheelsCount = WheelsCount.Three;
|
||||
}
|
||||
|
||||
@Override
|
||||
public WheelsCount GetWheelsCount() {
|
||||
return _wheelsCount;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void DrawWheel(Graphics2D g2d, Color color, int x, int y, int w, int h) {
|
||||
g2d.setColor(Color.BLUE);
|
||||
g2d.drawOval(x, y + 3, w + 2, h + 2);
|
||||
g2d.setColor(Color.RED);
|
||||
g2d.drawOval(x - 2, y - 2, w + 2, h + 2);
|
||||
g2d.setColor(Color.MAGENTA);
|
||||
g2d.drawOval(x + 3, y, w + 3, h + 3);
|
||||
|
||||
g2d.setColor(Color.BLACK);
|
||||
g2d.drawOval(x, y, w, h);
|
||||
}
|
||||
}
|
@ -1,5 +1,5 @@
|
||||
package ProjectElectricLocomotive;
|
||||
|
||||
public enum DyrectionType {
|
||||
Up, Down, Left, Right
|
||||
Up, Down, Left, Right;
|
||||
}
|
||||
|
@ -2,25 +2,16 @@ package ProjectElectricLocomotive;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
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 SeifBatteries;
|
||||
public double Step()
|
||||
public EntityElectricLocomotive(int speed, double weight, Color bodyColor, Color additionalColor, boolean horns, boolean seifBatteries)
|
||||
{
|
||||
return (double) Speed * 100 / Weight;
|
||||
}
|
||||
public void Init(int speed, double weight, Color bodyColor, Color additionalColor,
|
||||
boolean horns, boolean seifBatteries)
|
||||
{
|
||||
Speed = speed;
|
||||
Weight = weight;
|
||||
BodyColor = bodyColor;
|
||||
super(speed, weight, bodyColor);
|
||||
AdditionalColor = additionalColor;
|
||||
Horns = horns;
|
||||
SeifBatteries = seifBatteries;
|
||||
}
|
||||
|
||||
}
|
||||
|
23
ProjectElectricLocomotive/EntityLocomotive.java
Normal file
23
ProjectElectricLocomotive/EntityLocomotive.java
Normal file
@ -0,0 +1,23 @@
|
||||
package ProjectElectricLocomotive;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class EntityLocomotive {
|
||||
public int Speed;
|
||||
public double Weight;
|
||||
public Color BodyColor;
|
||||
public EntityLocomotive(int speed, double weight, Color bodyColor) {
|
||||
Speed = speed;
|
||||
Weight = weight;
|
||||
BodyColor = bodyColor;
|
||||
}
|
||||
public double Step()
|
||||
{
|
||||
return (double) Speed * 100 / Weight;
|
||||
}
|
||||
public void Init(int speed, double weight, Color bodyColor, Color additionalColor, boolean horns, boolean seifbatteries) {
|
||||
Speed = speed;
|
||||
Weight = weight;
|
||||
BodyColor = bodyColor;
|
||||
}
|
||||
}
|
@ -1,61 +1,62 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="ProjectElectricLocomotive.FormElectricLocomotive">
|
||||
<grid id="27dc6" binding="pictureBox" layout-manager="GridLayoutManager" row-count="3" column-count="5" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
|
||||
<grid id="27dc6" binding="pictureBox" layout-manager="GridLayoutManager" row-count="6" 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="24" y="37" width="635" height="383"/>
|
||||
<xy x="24" y="37" width="663" height="500"/>
|
||||
</constraints>
|
||||
<properties/>
|
||||
<properties>
|
||||
<minimumSize width="650" height="400"/>
|
||||
<preferredSize width="650" height="400"/>
|
||||
</properties>
|
||||
<border type="none">
|
||||
<font/>
|
||||
</border>
|
||||
<children>
|
||||
<hspacer id="ce44b">
|
||||
<constraints>
|
||||
<grid row="2" column="1" 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="17764">
|
||||
<constraints>
|
||||
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
|
||||
<grid row="3" 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="2fa92" class="javax.swing.JButton" binding="buttonDown">
|
||||
<component id="6cb47" class="javax.swing.JButton" binding="buttonCreateElectricLocomotive">
|
||||
<constraints>
|
||||
<grid row="2" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" 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>
|
||||
<grid row="5" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<hideActionText value="true"/>
|
||||
<horizontalTextPosition value="0"/>
|
||||
<icon value="ProjectElectricLocomotive/img/arrowDown.jpg"/>
|
||||
<text value=""/>
|
||||
<text value="Создать электролокомотив"/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="326c5" class="javax.swing.JButton" binding="buttonLeft">
|
||||
<component id="ca377" class="javax.swing.JButton" binding="buttonCreateLocomotive">
|
||||
<constraints>
|
||||
<grid row="2" column="2" 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>
|
||||
<grid row="5" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<hideActionText value="false"/>
|
||||
<horizontalTextPosition value="0"/>
|
||||
<icon value="ProjectElectricLocomotive/img/arrowLeft.jpg"/>
|
||||
<text value=""/>
|
||||
<text value="Создать локомотив"/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="dac01" class="javax.swing.JComboBox" binding="comboBoxStrategy">
|
||||
<constraints>
|
||||
<grid row="0" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<model>
|
||||
<item value="MoveToCenter"/>
|
||||
<item value="MoveToRightCorner"/>
|
||||
</model>
|
||||
<toolTipText value=""/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="42f28" class="javax.swing.JButton" binding="buttonStep">
|
||||
<constraints>
|
||||
<grid row="1" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<text value="Шаг"/>
|
||||
</properties>
|
||||
<clientProperties>
|
||||
<hideActionText class="java.lang.Boolean" value="false"/>
|
||||
</clientProperties>
|
||||
</component>
|
||||
<component id="cc460" class="javax.swing.JButton" binding="buttonRight">
|
||||
<constraints>
|
||||
<grid row="2" column="4" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false">
|
||||
<grid row="5" column="5" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false">
|
||||
<minimum-size width="30" height="30"/>
|
||||
<preferred-size width="30" height="30"/>
|
||||
<maximum-size width="30" height="30"/>
|
||||
@ -68,9 +69,24 @@
|
||||
<text value=""/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="2fa92" class="javax.swing.JButton" binding="buttonDown">
|
||||
<constraints>
|
||||
<grid row="5" column="4" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" 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"/>
|
||||
<icon value="ProjectElectricLocomotive/img/arrowDown.jpg"/>
|
||||
<text value=""/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="bbbb" class="javax.swing.JButton" binding="buttonUp">
|
||||
<constraints>
|
||||
<grid row="1" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false">
|
||||
<grid row="4" column="4" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false">
|
||||
<minimum-size width="30" height="30"/>
|
||||
<preferred-size width="30" height="30"/>
|
||||
<maximum-size width="30" height="30"/>
|
||||
@ -87,12 +103,30 @@
|
||||
<hideActionText class="java.lang.Boolean" value="false"/>
|
||||
</clientProperties>
|
||||
</component>
|
||||
<component id="6cb47" class="javax.swing.JButton" binding="buttonCreate">
|
||||
<component id="326c5" class="javax.swing.JButton" binding="buttonLeft">
|
||||
<constraints>
|
||||
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
<grid row="5" 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>
|
||||
<text value="Создать"/>
|
||||
<hideActionText value="false"/>
|
||||
<horizontalTextPosition value="0"/>
|
||||
<icon value="ProjectElectricLocomotive/img/arrowLeft.jpg"/>
|
||||
<text value=""/>
|
||||
</properties>
|
||||
<clientProperties>
|
||||
<hideActionText class="java.lang.Boolean" value="false"/>
|
||||
</clientProperties>
|
||||
</component>
|
||||
<component id="7aad1" class="javax.swing.JButton" binding="ButtonSelectLocomotive">
|
||||
<constraints>
|
||||
<grid row="2" column="2" row-span="1" 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>
|
||||
|
@ -1,17 +1,25 @@
|
||||
package ProjectElectricLocomotive;
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.util.Random;
|
||||
|
||||
public class FormElectricLocomotive {
|
||||
DrawingElectricLocomotive _drawingElectricLocomotive = new DrawingElectricLocomotive();
|
||||
private JButton buttonCreate;
|
||||
public DrawingLocomotive _drawingLocomotive;
|
||||
AbstractStrategy _abstractStrategy;
|
||||
private JButton buttonCreateElectricLocomotive;
|
||||
private JPanel pictureBox;
|
||||
private JButton buttonUp;
|
||||
private JButton buttonDown;
|
||||
private JButton buttonLeft;
|
||||
private JButton buttonRight;
|
||||
public JComboBox comboBoxStrategy;
|
||||
private JButton buttonStep;
|
||||
private JButton buttonCreateLocomotive;
|
||||
public JButton ButtonSelectLocomotive;
|
||||
public DrawingLocomotive SelectedLocomotive;
|
||||
public boolean IsSelect = false;
|
||||
|
||||
public JPanel getPictureBox() {
|
||||
return pictureBox;
|
||||
@ -23,41 +31,101 @@ public class FormElectricLocomotive {
|
||||
buttonLeft.setName("buttonLeft");
|
||||
buttonRight.setName("buttonRight");
|
||||
|
||||
buttonCreate.addActionListener(e -> {
|
||||
_drawingElectricLocomotive = new DrawingElectricLocomotive();
|
||||
Random random = new Random();
|
||||
buttonCreateLocomotive.addActionListener(e -> {
|
||||
Random rnd = new Random();
|
||||
Color color = JColorChooser.showDialog(null, "Цвет", null);
|
||||
_drawingLocomotive = new DrawingLocomotive(rnd.nextInt(100, 300),
|
||||
rnd.nextInt(1000, 3000), color,
|
||||
pictureBox.getWidth(),
|
||||
pictureBox.getHeight());
|
||||
|
||||
_drawingElectricLocomotive.Init(
|
||||
_drawingLocomotive.SetWheelsCount(rnd.nextInt(2, 5));
|
||||
_drawingLocomotive.SetPosition(rnd.nextInt(10, 100), rnd.nextInt(10, 100));
|
||||
Draw();
|
||||
});
|
||||
|
||||
buttonCreateElectricLocomotive.addActionListener(e -> {
|
||||
Random random = new Random();
|
||||
Color color = JColorChooser.showDialog(null, "Цвет", null);
|
||||
Color addColor = JColorChooser.showDialog(null, "Цвет2", null);
|
||||
|
||||
_drawingLocomotive = new DrawingElectricLocomotive(
|
||||
random.nextInt(100, 300),
|
||||
random.nextInt(1000, 3000),
|
||||
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)),
|
||||
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)),
|
||||
color,
|
||||
addColor,
|
||||
random.nextBoolean(),
|
||||
random.nextBoolean(),
|
||||
pictureBox.getWidth(),
|
||||
pictureBox.getHeight()
|
||||
);
|
||||
|
||||
_drawingElectricLocomotive.SetWheelsCount(random.nextInt(2, 5));
|
||||
_drawingElectricLocomotive.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100));
|
||||
_drawingLocomotive.SetWheelsCount(random.nextInt(2, 5));
|
||||
_drawingLocomotive.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100));
|
||||
Draw();
|
||||
});
|
||||
|
||||
ButtonSelectLocomotive.addActionListener(e->{
|
||||
SelectedLocomotive = _drawingLocomotive;
|
||||
IsSelect = true;
|
||||
// DialogResult = DialogResult.OK;
|
||||
});
|
||||
|
||||
buttonStep.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if (_drawingLocomotive == null) {
|
||||
return;
|
||||
}
|
||||
if (comboBoxStrategy.isEnabled()) {
|
||||
switch(comboBoxStrategy.getSelectedIndex())
|
||||
{
|
||||
case 0:
|
||||
_abstractStrategy = new MoveToCenter();
|
||||
break;
|
||||
case 1:
|
||||
_abstractStrategy = new MoveToRigthCorner();
|
||||
break;
|
||||
default:
|
||||
_abstractStrategy = null;
|
||||
break;
|
||||
} ;
|
||||
if (_abstractStrategy == null) {
|
||||
return;
|
||||
}
|
||||
_abstractStrategy.SetData(new
|
||||
DrawingObjectLocomotive(_drawingLocomotive), pictureBox.getWidth(),
|
||||
pictureBox.getHeight());
|
||||
comboBoxStrategy.setEnabled(false);
|
||||
}
|
||||
if (_abstractStrategy == null) {
|
||||
return;
|
||||
}
|
||||
_abstractStrategy.MakeStep();
|
||||
Draw();
|
||||
if (_abstractStrategy.GetStatus() == Status.Finish) {
|
||||
comboBoxStrategy.setEnabled(true);
|
||||
_abstractStrategy = null;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
ActionListener buttonMoveClickedListener = e -> {
|
||||
String buttonName = ((JButton) e.getSource()).getName();
|
||||
|
||||
switch (buttonName) {
|
||||
case ("buttonUp") -> {
|
||||
_drawingElectricLocomotive.MoveTransport(DyrectionType.Up);
|
||||
_drawingLocomotive.MoveTransport(DyrectionType.Up);
|
||||
}
|
||||
case ("buttonDown") -> {
|
||||
_drawingElectricLocomotive.MoveTransport(DyrectionType.Down);
|
||||
_drawingLocomotive.MoveTransport(DyrectionType.Down);
|
||||
}
|
||||
case ("buttonLeft") -> {
|
||||
_drawingElectricLocomotive.MoveTransport(DyrectionType.Left);
|
||||
_drawingLocomotive.MoveTransport(DyrectionType.Left);
|
||||
}
|
||||
case ("buttonRight") -> {
|
||||
_drawingElectricLocomotive.MoveTransport(DyrectionType.Right);
|
||||
_drawingLocomotive.MoveTransport(DyrectionType.Right);
|
||||
}
|
||||
}
|
||||
Draw();
|
||||
@ -68,12 +136,11 @@ public class FormElectricLocomotive {
|
||||
buttonRight.addActionListener(buttonMoveClickedListener);
|
||||
}
|
||||
public void Draw() {
|
||||
if (_drawingElectricLocomotive.EntityElectricLocomotive == null) {
|
||||
if (_drawingLocomotive.EntityLocomotive == null) {
|
||||
return;
|
||||
}
|
||||
Graphics g = pictureBox.getGraphics();
|
||||
pictureBox.paint(g);
|
||||
_drawingElectricLocomotive.DrawTransport(g);
|
||||
_drawingLocomotive.DrawTransport(g);
|
||||
}
|
||||
|
||||
}
|
||||
|
83
ProjectElectricLocomotive/FormLocomotiveCollections.form
Normal file
83
ProjectElectricLocomotive/FormLocomotiveCollections.form
Normal file
@ -0,0 +1,83 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="ProjectElectricLocomotive.FormLocomotiveCollections">
|
||||
<grid id="27dc6" binding="MainPanel" layout-manager="GridLayoutManager" row-count="1" column-count="2" 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="708" height="423"/>
|
||||
</constraints>
|
||||
<properties/>
|
||||
<border type="none"/>
|
||||
<children>
|
||||
<grid id="c25f7" binding="pictureBoxCollections" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
|
||||
<margin top="0" left="0" bottom="0" right="0"/>
|
||||
<constraints>
|
||||
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="true">
|
||||
<minimum-size width="450" height="400"/>
|
||||
<preferred-size width="450" height="400"/>
|
||||
<maximum-size width="450" height="400"/>
|
||||
</grid>
|
||||
</constraints>
|
||||
<properties/>
|
||||
<border type="line"/>
|
||||
<children/>
|
||||
</grid>
|
||||
<grid id="40d5" binding="Instruments" layout-manager="GridLayoutManager" row-count="6" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
|
||||
<margin top="0" left="0" bottom="0" right="0"/>
|
||||
<constraints>
|
||||
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="true">
|
||||
<minimum-size width="160" height="400"/>
|
||||
<preferred-size width="160" height="400"/>
|
||||
<maximum-size width="160" height="400"/>
|
||||
</grid>
|
||||
</constraints>
|
||||
<properties>
|
||||
<enabled value="true"/>
|
||||
</properties>
|
||||
<border type="none">
|
||||
<font/>
|
||||
</border>
|
||||
<children>
|
||||
<vspacer id="1eee0">
|
||||
<constraints>
|
||||
<grid row="1" column="1" row-span="5" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
</vspacer>
|
||||
<component id="138d" class="javax.swing.JButton" binding="ButtonAddLocomotive">
|
||||
<constraints>
|
||||
<grid row="0" column="0" row-span="1" 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="1d01d" class="javax.swing.JButton" binding="ButtonRemoveLocomotive">
|
||||
<constraints>
|
||||
<grid row="2" column="0" row-span="1" 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="25553" class="javax.swing.JTextField" binding="textFieldNumber">
|
||||
<constraints>
|
||||
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
|
||||
<preferred-size width="150" height="-1"/>
|
||||
</grid>
|
||||
</constraints>
|
||||
<properties>
|
||||
<text value="-"/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="b21cb" class="javax.swing.JButton" binding="ButtonRefreshCollection">
|
||||
<constraints>
|
||||
<grid row="3" column="0" row-span="1" 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>
|
||||
</children>
|
||||
</grid>
|
||||
</form>
|
76
ProjectElectricLocomotive/FormLocomotiveCollections.java
Normal file
76
ProjectElectricLocomotive/FormLocomotiveCollections.java
Normal file
@ -0,0 +1,76 @@
|
||||
package ProjectElectricLocomotive;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
|
||||
public class FormLocomotiveCollections {
|
||||
private JPanel MainPanel;
|
||||
private JPanel pictureBoxCollections;
|
||||
private JPanel Instruments;
|
||||
private JButton ButtonAddLocomotive;
|
||||
private JTextField textFieldNumber;
|
||||
private JButton ButtonRefreshCollection;
|
||||
private JButton ButtonRemoveLocomotive;
|
||||
public DrawingLocomotive loco;
|
||||
FormElectricLocomotive _formElectricLocomotive;
|
||||
private final LocomotiveGenericCollection<DrawingLocomotive, DrawingObjectLocomotive> _locomotives;
|
||||
|
||||
public JPanel getPictureBoxCollections() {
|
||||
return MainPanel;
|
||||
}
|
||||
|
||||
public FormLocomotiveCollections() {
|
||||
_locomotives = new LocomotiveGenericCollection<>(400, 300);
|
||||
|
||||
|
||||
ButtonAddLocomotive.addActionListener(e -> {
|
||||
FrameElectricLocomotive frameElectricLocomotive = new FrameElectricLocomotive();
|
||||
frameElectricLocomotive.setVisible(true);
|
||||
frameElectricLocomotive._formLocomotiveCollection.ButtonSelectLocomotive.addActionListener(e2 -> {
|
||||
loco = frameElectricLocomotive._formLocomotiveCollection._drawingLocomotive;
|
||||
frameElectricLocomotive.dispose();
|
||||
if (loco != null) {
|
||||
//проверяем, удалось ли нам загрузить объект
|
||||
if (_locomotives.AddOverload(loco) != -1) {
|
||||
JOptionPane.showMessageDialog(getPictureBoxCollections(), "Объект добавлен");
|
||||
Refresh();
|
||||
} else {
|
||||
JOptionPane.showMessageDialog(getPictureBoxCollections(), "Не удалось добавить объект");
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
ButtonRemoveLocomotive.addActionListener(e->{
|
||||
try {
|
||||
int pos = Integer.parseInt(textFieldNumber.getText());
|
||||
if (_locomotives.SubOverload(pos) != null) {
|
||||
Refresh();
|
||||
JOptionPane.showMessageDialog(this.getPictureBoxCollections(),
|
||||
"Объект удален",
|
||||
"Успех",
|
||||
JOptionPane.INFORMATION_MESSAGE);
|
||||
} else {
|
||||
JOptionPane.showMessageDialog(this.getPictureBoxCollections(),
|
||||
"Не удалось удалить объект",
|
||||
"Ошибка",
|
||||
JOptionPane.ERROR_MESSAGE);
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
JOptionPane.showMessageDialog(this.getPictureBoxCollections(),
|
||||
"Неверное значение",
|
||||
"Ошибка",
|
||||
JOptionPane.ERROR_MESSAGE);
|
||||
}
|
||||
});
|
||||
|
||||
ButtonRefreshCollection.addActionListener(e->{
|
||||
Refresh();
|
||||
});
|
||||
}
|
||||
public void Refresh() {
|
||||
Graphics g = pictureBoxCollections.getGraphics();
|
||||
pictureBoxCollections.paint(g);
|
||||
_locomotives.ShowLocomotives(g);
|
||||
}
|
||||
}
|
18
ProjectElectricLocomotive/FrameElectricLocomotive.java
Normal file
18
ProjectElectricLocomotive/FrameElectricLocomotive.java
Normal file
@ -0,0 +1,18 @@
|
||||
package ProjectElectricLocomotive;
|
||||
|
||||
import javax.swing.*;
|
||||
|
||||
public class FrameElectricLocomotive extends JFrame {
|
||||
public FormElectricLocomotive _formLocomotiveCollection;
|
||||
public FrameElectricLocomotive() {
|
||||
super();
|
||||
setTitle("ElectroLoco");
|
||||
setDefaultCloseOperation(EXIT_ON_CLOSE);
|
||||
_formLocomotiveCollection = new FormElectricLocomotive();
|
||||
setContentPane(_formLocomotiveCollection.getPictureBox());
|
||||
setDefaultLookAndFeelDecorated(false);
|
||||
setLocation(500, 200);
|
||||
pack();
|
||||
setVisible(true);
|
||||
}
|
||||
}
|
18
ProjectElectricLocomotive/FrameLocomotiveCollection.java
Normal file
18
ProjectElectricLocomotive/FrameLocomotiveCollection.java
Normal file
@ -0,0 +1,18 @@
|
||||
package ProjectElectricLocomotive;
|
||||
|
||||
import javax.swing.*;
|
||||
|
||||
public class FrameLocomotiveCollection extends JFrame {
|
||||
public FormLocomotiveCollections _formLocomotiveCollections;
|
||||
public FrameLocomotiveCollection(){
|
||||
super();
|
||||
setTitle("LocoCollection");
|
||||
setDefaultCloseOperation(EXIT_ON_CLOSE);
|
||||
_formLocomotiveCollections = new FormLocomotiveCollections();
|
||||
setContentPane(_formLocomotiveCollections.getPictureBoxCollections());
|
||||
setDefaultLookAndFeelDecorated(false);
|
||||
setLocation(400, 50);
|
||||
pack();
|
||||
setVisible(true);
|
||||
}
|
||||
}
|
38
ProjectElectricLocomotive/IDrawingWheels.java
Normal file
38
ProjectElectricLocomotive/IDrawingWheels.java
Normal file
@ -0,0 +1,38 @@
|
||||
package ProjectElectricLocomotive;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public interface IDrawingWheels {
|
||||
void SetWheelsCount(int wheelsCount);
|
||||
|
||||
WheelsCount GetWheelsCount();
|
||||
|
||||
void DrawWheel(Graphics2D g2d, Color color, int x, int y, int w, int h);
|
||||
|
||||
default void DrawWheels(Graphics g, Color color, int startPosX, int startPosY, int drawingWidth, int drawingHeight){
|
||||
WheelsCount wheelsCount = GetWheelsCount();
|
||||
if(wheelsCount == null) return;
|
||||
Graphics2D g2d = (Graphics2D) g;
|
||||
int wheelWidth = 9;
|
||||
int wheelHeight = 9;
|
||||
|
||||
if(wheelsCount.count <= wheelsCount.Two.count){
|
||||
DrawWheel(g2d, color, startPosX + 95, startPosY + 45, 9, 9);
|
||||
DrawWheel(g2d, color, startPosX + 140, startPosY + 45, 9, 9);
|
||||
}
|
||||
|
||||
if (wheelsCount.count == wheelsCount.Three.count) {
|
||||
DrawWheel(g2d, color, startPosX + 95, startPosY + 45, 9, 9);
|
||||
DrawWheel(g2d, color, startPosX + 140, startPosY + 45, 9, 9);
|
||||
|
||||
DrawWheel(g2d, color, startPosX + 105, startPosY + 45, wheelWidth, wheelHeight);
|
||||
}
|
||||
if (wheelsCount.count == wheelsCount.Four.count) {
|
||||
DrawWheel(g2d, color, startPosX + 95, startPosY + 45, 9, 9);
|
||||
DrawWheel(g2d, color, startPosX + 140, startPosY + 45, 9, 9);
|
||||
|
||||
DrawWheel(g2d, color, startPosX + 105, startPosY + 45, wheelWidth, wheelHeight);
|
||||
DrawWheel(g2d, color, startPosX + 130, startPosY + 45, wheelWidth, wheelHeight);
|
||||
}
|
||||
}
|
||||
}
|
9
ProjectElectricLocomotive/IMoveableObject.java
Normal file
9
ProjectElectricLocomotive/IMoveableObject.java
Normal file
@ -0,0 +1,9 @@
|
||||
package ProjectElectricLocomotive;
|
||||
import java.awt.*;
|
||||
|
||||
public interface IMoveableObject {
|
||||
ObjectParameters GetObjectPosition();
|
||||
int GetStep();
|
||||
boolean CheckCanMove(DyrectionType direction);
|
||||
void MoveObject(DyrectionType direction);
|
||||
}
|
86
ProjectElectricLocomotive/LocomotiveGenericCollection.java
Normal file
86
ProjectElectricLocomotive/LocomotiveGenericCollection.java
Normal file
@ -0,0 +1,86 @@
|
||||
package ProjectElectricLocomotive;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class LocomotiveGenericCollection<T extends DrawingLocomotive,U extends IMoveableObject>
|
||||
{
|
||||
//ширина/высота окна
|
||||
private final int _pictureWidth;
|
||||
private final int _pictureHeight;
|
||||
//ширина/высота занимаемого места
|
||||
private final int _placeSizeWidth = 150;
|
||||
private final int _placeSizeHeight = 50;
|
||||
|
||||
/// Набор объектов
|
||||
private final SetGeneric<T> _collection;
|
||||
|
||||
public LocomotiveGenericCollection(int picWidth, int picHeight)
|
||||
{
|
||||
// немного странная логика, что-то я пока ее не особо понимаю, зачем нам ААААА дошло...
|
||||
// высчитываем размер массива для setgeneric
|
||||
int width = picWidth / _placeSizeWidth;
|
||||
int height = picHeight / _placeSizeHeight;
|
||||
_pictureWidth = picWidth;
|
||||
_pictureHeight = picHeight;
|
||||
_collection = new SetGeneric<T>(width*height);
|
||||
}
|
||||
|
||||
/// Перегрузка оператора сложения
|
||||
//да емае, почему в яве все по-другому?...
|
||||
|
||||
public int AddOverload(T loco){
|
||||
if(loco == null){
|
||||
return -1;
|
||||
}
|
||||
return _collection.Insert(loco);
|
||||
}
|
||||
|
||||
public T SubOverload(int pos){
|
||||
return _collection.Remove(pos);
|
||||
}
|
||||
|
||||
// получение объекта imoveableObj
|
||||
public U GetU(int pos)
|
||||
{
|
||||
return (U)_collection.Get(pos).GetMoveableObject();
|
||||
}
|
||||
|
||||
/// Вывод всего набора объектов
|
||||
public void ShowLocomotives(Graphics gr)
|
||||
{
|
||||
DrawBackground(gr);
|
||||
DrawObjects(gr);
|
||||
}
|
||||
private void DrawBackground(Graphics g)
|
||||
{
|
||||
Color blackColor = Color.BLACK;
|
||||
g.setColor(blackColor);
|
||||
for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++)
|
||||
{
|
||||
for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j)
|
||||
{
|
||||
//линия рамзетки места
|
||||
g.drawLine(i * _placeSizeWidth, j * _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight);
|
||||
}
|
||||
g.drawLine(i * _placeSizeWidth, 0, i * _placeSizeWidth, _pictureHeight / _placeSizeHeight * _placeSizeHeight);
|
||||
}
|
||||
}
|
||||
|
||||
private void DrawObjects(Graphics g)
|
||||
{
|
||||
int HeightObjCount = _pictureHeight / _placeSizeHeight;
|
||||
int WidthObjCount = _pictureWidth / _placeSizeWidth;
|
||||
for (int i = 0; i < _collection.Count(); i++)
|
||||
{
|
||||
T type = _collection.Get(i);
|
||||
if (type != null)
|
||||
{
|
||||
type.SetPosition(
|
||||
(int)(i / HeightObjCount * _placeSizeWidth),
|
||||
(HeightObjCount - 1) * _placeSizeHeight - (int)(i % HeightObjCount * _placeSizeHeight)
|
||||
);
|
||||
type.DrawTransport(g);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -3,6 +3,6 @@ package ProjectElectricLocomotive;
|
||||
public class Main {
|
||||
public static void main(String[] args)
|
||||
{
|
||||
MainFrameElectricLocomotive mainFrameElectricLocomotive = new MainFrameElectricLocomotive();
|
||||
FrameLocomotiveCollection mainFrame = new FrameLocomotiveCollection();
|
||||
}
|
||||
}
|
||||
|
@ -1,19 +0,0 @@
|
||||
package ProjectElectricLocomotive;
|
||||
|
||||
import javax.swing.*;
|
||||
|
||||
public class MainFrameElectricLocomotive extends JFrame {
|
||||
private FormElectricLocomotive _formElectricLocomotive;
|
||||
|
||||
public MainFrameElectricLocomotive() {
|
||||
super();
|
||||
setTitle("ElectroLoco");
|
||||
setDefaultCloseOperation(EXIT_ON_CLOSE);
|
||||
_formElectricLocomotive = new FormElectricLocomotive();
|
||||
setContentPane(_formElectricLocomotive.getPictureBox());
|
||||
setDefaultLookAndFeelDecorated(false);
|
||||
setLocation(500, 50);
|
||||
pack();
|
||||
setVisible(true);
|
||||
}
|
||||
}
|
45
ProjectElectricLocomotive/MoveToCenter.java
Normal file
45
ProjectElectricLocomotive/MoveToCenter.java
Normal file
@ -0,0 +1,45 @@
|
||||
package ProjectElectricLocomotive;
|
||||
|
||||
public class MoveToCenter extends AbstractStrategy{
|
||||
@Override
|
||||
protected boolean IsTargetDestinaion()
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
@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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
21
ProjectElectricLocomotive/MoveToRigthCorner.java
Normal file
21
ProjectElectricLocomotive/MoveToRigthCorner.java
Normal file
@ -0,0 +1,21 @@
|
||||
package ProjectElectricLocomotive;
|
||||
|
||||
public class MoveToRigthCorner extends AbstractStrategy {
|
||||
@Override
|
||||
protected boolean IsTargetDestinaion()
|
||||
{
|
||||
var objParams = GetObjectParameters();
|
||||
if (objParams == null) return false;
|
||||
|
||||
return objParams.RightBorder() >= FieldWidth - GetStep() && objParams.DownBorder() >= FieldHeight - GetStep();
|
||||
}
|
||||
@Override
|
||||
protected void MoveToTarget()
|
||||
{
|
||||
var objParams = GetObjectParameters();
|
||||
if (objParams == null) return;
|
||||
|
||||
if (objParams.RightBorder() < FieldWidth - GetStep()) MoveRight();
|
||||
if (objParams.DownBorder() < FieldHeight - GetStep()) MoveDown();
|
||||
}
|
||||
}
|
51
ProjectElectricLocomotive/ObjectParameters.java
Normal file
51
ProjectElectricLocomotive/ObjectParameters.java
Normal file
@ -0,0 +1,51 @@
|
||||
package ProjectElectricLocomotive;
|
||||
public class ObjectParameters {
|
||||
private final int _x;
|
||||
private final int _y;
|
||||
private final int _width;
|
||||
private final 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;
|
||||
}
|
||||
}
|
63
ProjectElectricLocomotive/SetGeneric.java
Normal file
63
ProjectElectricLocomotive/SetGeneric.java
Normal file
@ -0,0 +1,63 @@
|
||||
package ProjectElectricLocomotive;
|
||||
|
||||
public class SetGeneric<T extends DrawingLocomotive>{
|
||||
private T[] _places;
|
||||
public int Count(){
|
||||
return _places.length;
|
||||
}
|
||||
public SetGeneric(int count) {
|
||||
_places = (T[]) new DrawingLocomotive[count];
|
||||
}
|
||||
|
||||
public int Insert(T loco)
|
||||
{
|
||||
return Insert(loco, 0);
|
||||
}
|
||||
|
||||
public int Insert(T loco, int position)
|
||||
{
|
||||
int NoEmpty = 0, temp = 0;
|
||||
for (int i = position; i < Count(); i++)
|
||||
{
|
||||
if (_places[i] != null) NoEmpty++;
|
||||
}
|
||||
if (NoEmpty == Count() - position - 1) return -1;
|
||||
|
||||
if (position < Count() && position >= 0)
|
||||
{
|
||||
for (int j = position; j < Count(); j++)
|
||||
{
|
||||
if (_places[j] == null)
|
||||
{
|
||||
temp = j;
|
||||
break;
|
||||
}
|
||||
}
|
||||
// shift right
|
||||
for (int i = temp; i > position; i--)
|
||||
{
|
||||
_places[i] = _places[i - 1];
|
||||
}
|
||||
_places[position] = loco;
|
||||
return position;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
public T Remove(int position)
|
||||
{
|
||||
if (position >= Count() || position < 0)
|
||||
return null;
|
||||
|
||||
T tmp = _places[position];
|
||||
_places[position] = null;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public T Get(int position)
|
||||
{
|
||||
// TODO проверка позиции
|
||||
if (position < 0 || position >= Count()) return null;
|
||||
return _places[position];
|
||||
}
|
||||
}
|
7
ProjectElectricLocomotive/Status.java
Normal file
7
ProjectElectricLocomotive/Status.java
Normal file
@ -0,0 +1,7 @@
|
||||
package ProjectElectricLocomotive;
|
||||
|
||||
public enum Status {
|
||||
NotInit,
|
||||
InProgress,
|
||||
Finish
|
||||
}
|
Binary file not shown.
After Width: | Height: | Size: 702 B |
Binary file not shown.
After Width: | Height: | Size: 706 B |
Binary file not shown.
After Width: | Height: | Size: 702 B |
Binary file not shown.
After Width: | Height: | Size: 706 B |
Loading…
x
Reference in New Issue
Block a user