Lab 02 hard with dopClasses, mb ready

This commit is contained in:
ekallin 2023-10-10 01:30:20 +04:00
parent 93622be5ba
commit 4284b48c00
9 changed files with 179 additions and 56 deletions

View File

@ -2,7 +2,6 @@ package ProjectElectricLocomotive;
public abstract class AbstractStrategy { public abstract class AbstractStrategy {
private IMoveableObject _moveableObject; private IMoveableObject _moveableObject;
private Status _state = Status.NotInit; private Status _state = Status.NotInit;
protected int FieldWidth; protected int FieldWidth;
protected int FieldHeight; protected int FieldHeight;
@ -52,17 +51,18 @@ public abstract class AbstractStrategy {
} }
/// Параметры объекта /// Параметры объекта
protected ObjectParameters/*?*/ GetObjectParameters(){ protected ObjectParameters GetObjectParameters(){
if(_moveableObject == null) return null; if(_moveableObject == null) return null;
return _moveableObject.GetObjectPosition(); return _moveableObject.GetObjectPosition();
} }
protected int/*?*/ GetStep() protected int GetStep()
{ {
if (_state != Status.InProgress) if (_state != Status.InProgress)
{ {
return -1; return -1;
} }
if(_moveableObject == null) return -1;
return _moveableObject.GetStep(); return _moveableObject.GetStep();
} }
protected abstract void MoveToTarget(); protected abstract void MoveToTarget();

View File

@ -1,11 +1,10 @@
package ProjectElectricLocomotive; package ProjectElectricLocomotive;
import java.awt.*; import java.awt.*;
public class DrawingElectricLocomotive extends DrawingLocomotive { public class DrawingElectricLocomotive extends DrawingLocomotive {
EntityElectricLocomotive electricLocomotive;
public DrawingElectricLocomotive(int speed, double weight, Color bodyColor, Color additionalColor, public DrawingElectricLocomotive(int speed, double weight, Color bodyColor, Color additionalColor,
boolean horns, boolean seifBatteries, int width, int height) boolean horns, boolean seifBatteries, int width, int height)
{ {
super(speed, weight, bodyColor, width, height, 85, 50); super(speed, weight, bodyColor, width, height, 150, 50);
if (EntityLocomotive != null) if (EntityLocomotive != null)
{ {
EntityLocomotive = new EntityElectricLocomotive(speed, width, bodyColor, additionalColor, horns, seifBatteries); EntityLocomotive = new EntityElectricLocomotive(speed, width, bodyColor, additionalColor, horns, seifBatteries);
@ -17,7 +16,6 @@ public class DrawingElectricLocomotive extends DrawingLocomotive {
if (EntityLocomotive instanceof EntityElectricLocomotive electricLocomotive) ///////// WARNING INSTANCEOF if (EntityLocomotive instanceof EntityElectricLocomotive electricLocomotive) ///////// WARNING INSTANCEOF
{ {
Color colorBlack = Color.BLACK; Color colorBlack = Color.BLACK;
/*Brush blackBrush = new SolidBrush(Color.Black);*/
Color windows = Color.BLUE; Color windows = Color.BLUE;
Color bodyColor = EntityLocomotive.BodyColor; Color bodyColor = EntityLocomotive.BodyColor;
Color additionalBrush = electricLocomotive.AdditionalColor; Color additionalBrush = electricLocomotive.AdditionalColor;
@ -37,7 +35,6 @@ public class DrawingElectricLocomotive extends DrawingLocomotive {
g.fillRect(_startPosX + 80, _startPosY + 30, 5, 10); g.fillRect(_startPosX + 80, _startPosY + 30, 5, 10);
} }
super.DrawTransport(g); super.DrawTransport(g);
} }
} }
} }

View 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);
}
}

View File

@ -1,10 +1,11 @@
package ProjectElectricLocomotive; package ProjectElectricLocomotive;
import java.awt.*; import java.awt.*;
import java.security.cert.PolicyNode; import java.security.cert.PolicyNode;
import java.util.Random;
public class DrawingLocomotive { public class DrawingLocomotive {
public EntityLocomotive EntityLocomotive; public EntityLocomotive EntityLocomotive;
private DrawingWheel _drawingWheel; protected IDrawingWheels _drawingWheels;
private int _pictureWidth; private int _pictureWidth;
private int _pictureHeight; private int _pictureHeight;
public int _startPosX; public int _startPosX;
@ -34,11 +35,19 @@ public class DrawingLocomotive {
_pictureWidth = width; _pictureWidth = width;
_pictureHeight = heigth; _pictureHeight = heigth;
EntityLocomotive = new EntityLocomotive(speed, weight, bodyColor); 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, protected DrawingLocomotive(int speed, double weight, Color bodyColor, int width,
int height, int locoWidth, int locoHeight) int height, int locoWidth, int locoHeight)
{ {
this(speed,weight, bodyColor, width, height);
if (width < _locoWidth || height < _locoHeight) if (width < _locoWidth || height < _locoHeight)
{ {
return; return;
@ -47,7 +56,11 @@ public class DrawingLocomotive {
_pictureHeight = height; _pictureHeight = height;
_locoWidth = locoWidth; _locoWidth = locoWidth;
_locoHeight = locoHeight; _locoHeight = locoHeight;
EntityLocomotive = new EntityLocomotive(speed, weight, bodyColor); // EntityLocomotive = new EntityLocomotive(speed, weight, bodyColor);
}
public void SetWheelsCount(int wheelsCount){
_drawingWheels.SetWheelsCount(wheelsCount);
} }
//Установка позиции //Установка позиции
@ -118,15 +131,6 @@ public class DrawingLocomotive {
g.fillPolygon(locoP); g.fillPolygon(locoP);
g.setColor(colorBlack); g.setColor(colorBlack);
/*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.drawPolygon(locoP); g.drawPolygon(locoP);
@ -146,10 +150,41 @@ public class DrawingLocomotive {
//обязательные колеса //обязательные колеса
//loco //loco
g.fillOval(_startPosX + 10, _startPosY + 45, 5, 5); g.fillOval(_startPosX + 10, _startPosY + 45, 9, 9);
g.fillOval(_startPosX + 25, _startPosY + 45, 5, 5); g.fillOval(_startPosX + 25, _startPosY + 45, 9, 9);
g.fillOval(_startPosX + 50, _startPosY + 45, 5, 5); g.fillOval(_startPosX + 50, _startPosY + 45, 9, 9);
g.fillOval(_startPosX + 65, _startPosY + 45, 5, 5); g.fillOval(_startPosX + 65, _startPosY + 45, 9, 9);
//telega
g.setColor(colorBlack);
g.fillOval(_startPosX + 95, _startPosY + 45, 9, 9);
g.fillOval(_startPosX + 140, _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);
//telejka
//телега окна
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) public boolean CanMove(DyrectionType direction)

View File

@ -17,12 +17,15 @@ public class DrawingObjectLocomotive implements IMoveableObject {
_drawningLocomotive.GetWidth(), _drawningLocomotive.GetHeight()); _drawningLocomotive.GetWidth(), _drawningLocomotive.GetHeight());
} }
public int GetStep(){ public int GetStep(){
if(_drawningLocomotive == null) return -1;
return (int)(_drawningLocomotive.EntityLocomotive.Step()); return (int)(_drawningLocomotive.EntityLocomotive.Step());
} }
public boolean CheckCanMove(DyrectionType direction){ public boolean CheckCanMove(DyrectionType direction){
if(_drawningLocomotive == null) return false;
return _drawningLocomotive.CanMove(direction); return _drawningLocomotive.CanMove(direction);
} }
public void MoveObject(DyrectionType direction){ public void MoveObject(DyrectionType direction){
if(_drawningLocomotive == null) return;
_drawningLocomotive.MoveTransport(direction); _drawningLocomotive.MoveTransport(direction);
} }
} }

View File

@ -2,7 +2,7 @@ package ProjectElectricLocomotive;
import java.awt.*; import java.awt.*;
public class DrawingWheel { public class DrawingWheel implements IDrawingWheels {
private WheelsCount _wheelsCount; private WheelsCount _wheelsCount;
public void SetWheelsCount(int wheelsCount) { public void SetWheelsCount(int wheelsCount) {
for (WheelsCount val : WheelsCount.values()) { for (WheelsCount val : WheelsCount.values()) {
@ -11,32 +11,16 @@ public class DrawingWheel {
return; 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.setColor(Color.BLACK);
g2d.fillOval(x, y, w, h); 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
);
}
}
} }

View 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);
}
}

View File

@ -6,7 +6,8 @@ import java.awt.event.ActionListener;
import java.util.Random; import java.util.Random;
public class FormElectricLocomotive { public class FormElectricLocomotive {
//DrawingElectricLocomotive _drawingElectricLocomotive = new DrawingElectricLocomotive(); DrawingLocomotive _drawingLocomotive;
AbstractStrategy _abstractStrategy;
private JButton buttonCreateElectricLocomotive; private JButton buttonCreateElectricLocomotive;
private JPanel pictureBox; private JPanel pictureBox;
private JButton buttonUp; private JButton buttonUp;
@ -16,8 +17,6 @@ public class FormElectricLocomotive {
public JComboBox comboBoxStrategy; public JComboBox comboBoxStrategy;
private JButton buttonStep; private JButton buttonStep;
private JButton buttonCreateLocomotive; private JButton buttonCreateLocomotive;
private DrawingLocomotive _drawingLocomotive;
private AbstractStrategy _abstractStrategy;
public JPanel getPictureBox() { public JPanel getPictureBox() {
return pictureBox; return pictureBox;
@ -31,22 +30,35 @@ public class FormElectricLocomotive {
buttonCreateLocomotive.addActionListener(e -> { buttonCreateLocomotive.addActionListener(e -> {
Random rnd = new Random(); Random rnd = new Random();
_drawingLocomotive = new DrawingLocomotive(rnd.nextInt(100, 300), rnd.nextInt(1000, 3000), _drawingLocomotive = new DrawingLocomotive(rnd.nextInt(100, 300),
new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)), rnd.nextInt(1000, 3000),
pictureBox.getWidth(), pictureBox.getHeight()); new Color(rnd.nextInt(0, 256),
rnd.nextInt(0, 256),
rnd.nextInt(0, 256)),
pictureBox.getWidth(),
pictureBox.getHeight());
_drawingLocomotive.SetWheelsCount(rnd.nextInt(2, 5));
_drawingLocomotive.SetPosition(rnd.nextInt(10, 100), rnd.nextInt(10, 100)); _drawingLocomotive.SetPosition(rnd.nextInt(10, 100), rnd.nextInt(10, 100));
Draw(); Draw();
}); });
buttonCreateElectricLocomotive.addActionListener(e -> { buttonCreateElectricLocomotive.addActionListener(e -> {
Random random = new Random(); Random random = new Random();
_drawingLocomotive = new DrawingElectricLocomotive(random.nextInt(100, 300),random.nextInt(1000, 3000), _drawingLocomotive = new DrawingElectricLocomotive(
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)),
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)),
random.nextBoolean(), random.nextBoolean(),
random.nextBoolean(), random.nextBoolean(),
pictureBox.getWidth(), pictureBox.getHeight()); pictureBox.getWidth(),
pictureBox.getHeight()
);
_drawingLocomotive.SetWheelsCount(random.nextInt(2, 5));
_drawingLocomotive.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100)); _drawingLocomotive.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100));
//drawingElectricLocomotive.SetWheelsCount(random.nextInt(2,5));
Draw(); Draw();
}); });

View File

@ -0,0 +1,29 @@
package ProjectElectricLocomotive;
import java.awt.*;
public interface IDrawingWheels {
void SetWheelsCount(int wheelsCount);
WheelsCount GetWheelsCount();
// 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.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);
}
}
}