Compare commits
No commits in common. "dc8923d85015470fa62ea91e800df65f5ee91eb9" and "f9487fc660ade9482722489df67a6d57c991005a" have entirely different histories.
dc8923d850
...
f9487fc660
@ -1,3 +1,4 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project version="4">
|
<project version="4">
|
||||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="21" project-jdk-type="JavaSDK">
|
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="21" project-jdk-type="JavaSDK">
|
||||||
<output url="file://$PROJECT_DIR$/out" />
|
<output url="file://$PROJECT_DIR$/out" />
|
||||||
|
@ -6,7 +6,6 @@ public abstract class AbstractStrategy {
|
|||||||
private int fieldHeight;
|
private int fieldHeight;
|
||||||
protected int getFieldHeight(){return fieldHeight;}
|
protected int getFieldHeight(){return fieldHeight;}
|
||||||
public Status getStatus() {return state;}
|
public Status getStatus() {return state;}
|
||||||
|
|
||||||
public void setData(IMoveableObject moveableObject, int width, int height){
|
public void setData(IMoveableObject moveableObject, int width, int height){
|
||||||
if (moveableObject == null)
|
if (moveableObject == null)
|
||||||
{
|
{
|
||||||
|
@ -1,137 +1,2 @@
|
|||||||
import java.awt.*;
|
package PACKAGE_NAME;public class DrawingBus {
|
||||||
|
|
||||||
public class DrawingBus {
|
|
||||||
protected EntityBus entityBus;
|
|
||||||
public EntityBus getEntityBus() {
|
|
||||||
return entityBus;
|
|
||||||
}
|
|
||||||
private int pictureWidth;
|
|
||||||
private int pictureHeight;
|
|
||||||
protected int _startPosX;
|
|
||||||
public int getPosX() {
|
|
||||||
return _startPosX;
|
|
||||||
}
|
|
||||||
protected int _startPosY;
|
|
||||||
public int getPosY() {
|
|
||||||
return _startPosY;
|
|
||||||
}
|
|
||||||
private final int busWidth = 200;
|
|
||||||
public int getWidth() {
|
|
||||||
return busWidth;
|
|
||||||
}
|
|
||||||
private final int busHeight = 135;
|
|
||||||
public int getHeight() {
|
|
||||||
return busHeight;
|
|
||||||
}
|
|
||||||
private IDrawDoors drawingDoors;
|
|
||||||
public DrawingBus(int speed, double weight, Color bodyColor, int width, int height, int doorsNumber, int doorsType) {
|
|
||||||
if (width < busWidth || height < busHeight)
|
|
||||||
return;
|
|
||||||
pictureWidth = width;
|
|
||||||
pictureHeight = height;
|
|
||||||
entityBus = new EntityBus(speed, weight, bodyColor);
|
|
||||||
switch (doorsType) {
|
|
||||||
case 1:
|
|
||||||
drawingDoors = new DrawingDoorsRoundedUp();
|
|
||||||
break;
|
|
||||||
case 2:
|
|
||||||
drawingDoors = new DrawingDoorsRoundedUpAndDown();
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
drawingDoors = new DrawingDoors();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
drawingDoors.setNumber(doorsNumber);
|
|
||||||
}
|
|
||||||
public void setPosition(int x, int y) {
|
|
||||||
if (x < 0 || y < 0 || x + busWidth > pictureWidth || y + busHeight > pictureHeight) {
|
|
||||||
x = 0;
|
|
||||||
y = 0;
|
|
||||||
}
|
|
||||||
_startPosX = x;
|
|
||||||
_startPosY = y;
|
|
||||||
}
|
|
||||||
public boolean canMove(DirectionType direction) {
|
|
||||||
if (entityBus == null) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
switch (direction) {
|
|
||||||
case LEFT:
|
|
||||||
return _startPosX - entityBus.step.get().intValue() > 0;
|
|
||||||
case UP:
|
|
||||||
return _startPosY - entityBus.step.get().intValue() > 0;
|
|
||||||
case RIGHT:
|
|
||||||
return _startPosX + entityBus.step.get().intValue() + busWidth < pictureWidth;
|
|
||||||
case DOWN:
|
|
||||||
return _startPosY + entityBus.step.get().intValue() + busHeight < pictureHeight;
|
|
||||||
default:
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
public void moveTransport(DirectionType direction) {
|
|
||||||
if (!canMove(direction) || entityBus == null)
|
|
||||||
return;
|
|
||||||
switch (direction) {
|
|
||||||
//влево
|
|
||||||
case LEFT:
|
|
||||||
_startPosX -= entityBus.step.get().intValue();
|
|
||||||
break;
|
|
||||||
//вверх
|
|
||||||
case UP:
|
|
||||||
_startPosY -= entityBus.step.get().intValue();
|
|
||||||
break;
|
|
||||||
// вправо
|
|
||||||
case RIGHT:
|
|
||||||
_startPosX += entityBus.step.get().intValue();
|
|
||||||
break;
|
|
||||||
//вниз
|
|
||||||
case DOWN:
|
|
||||||
_startPosY += entityBus.step.get().intValue();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
public void drawTransport(Graphics2D graphics2D) {
|
|
||||||
if (entityBus == null)
|
|
||||||
return;
|
|
||||||
BasicStroke pen = new BasicStroke(2);
|
|
||||||
graphics2D.setStroke(pen);
|
|
||||||
Color bodyColor = entityBus.getBodyColor();
|
|
||||||
//колеса
|
|
||||||
graphics2D.setPaint(Color.BLACK);
|
|
||||||
graphics2D.fillOval(_startPosX + 31, _startPosY + 106, 30, 30);
|
|
||||||
graphics2D.fillOval(_startPosX + 151, _startPosY + 106, 30, 30);
|
|
||||||
//кузов
|
|
||||||
graphics2D.setPaint(bodyColor);
|
|
||||||
graphics2D.fillRect(_startPosX + 6, _startPosY + 31, 200, 90);
|
|
||||||
//стекла
|
|
||||||
graphics2D.setPaint(Color.BLUE);
|
|
||||||
graphics2D.fillRect(_startPosX + 186, _startPosY + 40, 20, 40);
|
|
||||||
graphics2D.fillOval(_startPosX + 151, _startPosY + 35, 30, 40);
|
|
||||||
graphics2D.fillOval(_startPosX + 118, _startPosY + 35, 30, 40);
|
|
||||||
graphics2D.fillOval(_startPosX + 85, _startPosY + 35, 30, 40);
|
|
||||||
graphics2D.fillOval(_startPosX + 52, _startPosY + 35, 30, 40);
|
|
||||||
graphics2D.fillOval(_startPosX + 19, _startPosY + 35, 30, 40);
|
|
||||||
//двери
|
|
||||||
graphics2D.setPaint(Color.BLACK);
|
|
||||||
drawingDoors.drawDoors(graphics2D, _startPosX, _startPosY);
|
|
||||||
//границы троллейбуса
|
|
||||||
graphics2D.setPaint(Color.BLACK);
|
|
||||||
graphics2D.drawRect(_startPosX + 6, _startPosY + 31, 200, 90);
|
|
||||||
graphics2D.drawOval(_startPosX + 151, _startPosY + 35, 30, 40);
|
|
||||||
graphics2D.drawOval(_startPosX + 118, _startPosY + 35, 30, 40);
|
|
||||||
graphics2D.drawOval(_startPosX + 85, _startPosY + 35, 30, 40);
|
|
||||||
graphics2D.drawOval(_startPosX + 52, _startPosY + 35, 30, 40);
|
|
||||||
graphics2D.drawOval(_startPosX + 19, _startPosY + 35, 30, 40);
|
|
||||||
graphics2D.drawRect(_startPosX + 186, _startPosY + 40, 20, 40);
|
|
||||||
//задние фары
|
|
||||||
graphics2D.setPaint(Color.RED);
|
|
||||||
graphics2D.fillRect(_startPosX + 6, _startPosY + 91, 10, 20);
|
|
||||||
graphics2D.setPaint(Color.BLACK);
|
|
||||||
graphics2D.drawRect(_startPosX + 6, _startPosY + 91, 10, 20);
|
|
||||||
//передние фары
|
|
||||||
graphics2D.setPaint(Color.YELLOW);
|
|
||||||
graphics2D.fillRect(_startPosX + 196, _startPosY + 91, 10, 20);
|
|
||||||
graphics2D.setPaint(Color.BLACK);
|
|
||||||
graphics2D.drawRect(_startPosX + 196, _startPosY + 91, 10, 20);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,7 @@
|
|||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
|
public class DrawingDoors {
|
||||||
public class DrawingDoors implements IDrawDoors{
|
|
||||||
private DoorsNumber number;
|
private DoorsNumber number;
|
||||||
@Override
|
public void getNumber(int x){
|
||||||
public void setNumber(int x){
|
|
||||||
if(x <= 3)
|
if(x <= 3)
|
||||||
number = DoorsNumber.THREE;
|
number = DoorsNumber.THREE;
|
||||||
if(x == 4)
|
if(x == 4)
|
||||||
@ -11,7 +9,6 @@ public class DrawingDoors implements IDrawDoors{
|
|||||||
if(x >= 5)
|
if(x >= 5)
|
||||||
number = DoorsNumber.FIVE;
|
number = DoorsNumber.FIVE;
|
||||||
}
|
}
|
||||||
@Override
|
|
||||||
public void drawDoors(Graphics2D graphics2D, int _startX, int _startY){
|
public void drawDoors(Graphics2D graphics2D, int _startX, int _startY){
|
||||||
graphics2D.fillRect(_startX+52, _startY+81, 25, 40);
|
graphics2D.fillRect(_startX+52, _startY+81, 25, 40);
|
||||||
graphics2D.fillRect(_startX+85, _startY+81, 25, 40);
|
graphics2D.fillRect(_startX+85, _startY+81, 25, 40);
|
||||||
|
@ -1,31 +1,2 @@
|
|||||||
import java.awt.*;
|
package PACKAGE_NAME;public class DrawingDoorsRoundedUp {
|
||||||
|
|
||||||
public class DrawingDoorsRoundedUp implements IDrawDoors{
|
|
||||||
private DoorsNumber number;
|
|
||||||
@Override
|
|
||||||
public void setNumber(int x){
|
|
||||||
if(x <= 2)
|
|
||||||
number = DoorsNumber.THREE;
|
|
||||||
if(x == 4)
|
|
||||||
number = DoorsNumber.FOUR;
|
|
||||||
if(x >= 6)
|
|
||||||
number = DoorsNumber.FIVE;
|
|
||||||
}
|
|
||||||
@Override
|
|
||||||
public void drawDoors(Graphics2D graphics2D, int _startX, int _startY){
|
|
||||||
graphics2D.fillRect(_startX+52, _startY+86, 25, 35);
|
|
||||||
graphics2D.fillOval(_startX+52, _startY+81, 25, 12);
|
|
||||||
graphics2D.fillRect(_startX+85, _startY+86, 25, 35);
|
|
||||||
graphics2D.fillOval(_startX+85, _startY+81, 25, 12);
|
|
||||||
graphics2D.fillRect(_startX+118, _startY+86, 25, 35);
|
|
||||||
graphics2D.fillOval(_startX+118, _startY+81, 25, 12);
|
|
||||||
if (number == DoorsNumber.FOUR || number == DoorsNumber.FIVE){
|
|
||||||
graphics2D.fillRect(_startX+151, _startY+86, 25, 35);
|
|
||||||
graphics2D.fillOval(_startX+151, _startY+81, 25, 12);
|
|
||||||
}
|
|
||||||
if (number == DoorsNumber.FIVE){
|
|
||||||
graphics2D.fillRect(_startX+19, _startY+86, 25, 35);
|
|
||||||
graphics2D.fillOval(_startX+19, _startY+81, 25, 12);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,36 +1,2 @@
|
|||||||
import java.awt.*;
|
package PACKAGE_NAME;public class DrawingDoorsRoundedUpAndDown {
|
||||||
|
|
||||||
public class DrawingDoorsRoundedUpAndDown implements IDrawDoors{
|
|
||||||
private DoorsNumber number;
|
|
||||||
@Override
|
|
||||||
public void setNumber(int x){
|
|
||||||
if(x <= 2)
|
|
||||||
number = DoorsNumber.THREE;
|
|
||||||
if(x == 4)
|
|
||||||
number = DoorsNumber.FOUR;
|
|
||||||
if(x >= 6)
|
|
||||||
number = DoorsNumber.FIVE;
|
|
||||||
}
|
|
||||||
@Override
|
|
||||||
public void drawDoors(Graphics2D graphics2D, int _startX, int _startY){
|
|
||||||
graphics2D.fillRect(_startX+52, _startY+86, 25, 30);
|
|
||||||
graphics2D.fillOval(_startX+52, _startY+81, 25, 12);
|
|
||||||
graphics2D.fillOval(_startX+52, _startY+111, 25, 10);
|
|
||||||
graphics2D.fillRect(_startX+85, _startY+86, 25, 30);
|
|
||||||
graphics2D.fillOval(_startX+85, _startY+81, 25, 12);
|
|
||||||
graphics2D.fillOval(_startX+85, _startY+111, 25, 10);
|
|
||||||
graphics2D.fillRect(_startX+118, _startY+86, 25, 30);
|
|
||||||
graphics2D.fillOval(_startX+118, _startY+81, 25, 12);
|
|
||||||
graphics2D.fillOval(_startX+118, _startY+111, 25, 10);
|
|
||||||
if (number == DoorsNumber.FOUR || number == DoorsNumber.FIVE){
|
|
||||||
graphics2D.fillRect(_startX+151, _startY+86, 25, 30);
|
|
||||||
graphics2D.fillOval(_startX+151, _startY+81, 25, 12);
|
|
||||||
graphics2D.fillOval(_startX+151, _startY+111, 25, 10);
|
|
||||||
}
|
|
||||||
if (number == DoorsNumber.FIVE){
|
|
||||||
graphics2D.fillRect(_startX+19, _startY+86, 25, 30);
|
|
||||||
graphics2D.fillOval(_startX+19, _startY+81, 25, 12);
|
|
||||||
graphics2D.fillOval(_startX+19, _startY+111, 25, 10);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,29 +1,2 @@
|
|||||||
public class DrawingObjectBus implements IMoveableObject{
|
package PACKAGE_NAME;public class DrawingObjectBus {
|
||||||
private final DrawingBus drawingBus;
|
|
||||||
public DrawingObjectBus(DrawingBus drawingBus){
|
|
||||||
this.drawingBus = drawingBus;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public ObjectParameters getObjectPosition(){
|
|
||||||
if(drawingBus == null || drawingBus.getEntityBus() == null)
|
|
||||||
return null;
|
|
||||||
return new ObjectParameters(drawingBus.getPosX(), drawingBus.getPosY(),
|
|
||||||
drawingBus.getWidth(), drawingBus.getHeight());
|
|
||||||
}
|
|
||||||
public int getStep(){
|
|
||||||
if(drawingBus.getEntityBus() == null)
|
|
||||||
return 0;
|
|
||||||
return drawingBus.getEntityBus().step.get().intValue();
|
|
||||||
}
|
|
||||||
public boolean checkCanMove(DirectionType direction){
|
|
||||||
if(drawingBus == null)
|
|
||||||
return false;
|
|
||||||
return drawingBus.canMove(direction);
|
|
||||||
}
|
|
||||||
public void moveObject(DirectionType direction){
|
|
||||||
if(drawingBus == null)
|
|
||||||
return;
|
|
||||||
drawingBus.moveTransport(direction);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,21 +1,89 @@
|
|||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
|
public class DrawingTrolleybus {
|
||||||
public class DrawingTrolleybus extends DrawingBus{
|
private EntityTrolleybus entityTrolleybus;
|
||||||
public DrawingTrolleybus(int speed, double weight, Color bodyColor, Color additionalColor,
|
public EntityTrolleybus getEntityTrolleybus() {
|
||||||
boolean roga, boolean battery, int width, int height, int doorsNumber, int doorsType)
|
return entityTrolleybus;
|
||||||
|
}
|
||||||
|
private int _pictureWidth;
|
||||||
|
private int _pictureHeight;
|
||||||
|
private int _startPosX;
|
||||||
|
private int _startPosY;
|
||||||
|
private final int trolleybusWidth = 200;
|
||||||
|
private final int trolleybusHeight = 135;
|
||||||
|
private DrawingDoors drawingDoors;
|
||||||
|
public boolean init(int speed, double weight, Color bodyColor, Color additionalColor,
|
||||||
|
boolean roga, boolean battery, int width, int height, int doorsNumber)
|
||||||
{
|
{
|
||||||
super(speed, weight, bodyColor, width, height, doorsNumber,doorsType);
|
if (width < trolleybusWidth || height < trolleybusHeight)
|
||||||
if (entityBus != null)
|
return false;
|
||||||
entityBus = new EntityTrolleybus(speed, weight, bodyColor, additionalColor, roga, battery);
|
_pictureWidth = width;
|
||||||
|
_pictureHeight = height;
|
||||||
|
entityTrolleybus = new EntityTrolleybus();
|
||||||
|
entityTrolleybus.init(speed, weight, bodyColor, additionalColor, roga, battery);
|
||||||
|
drawingDoors = new DrawingDoors();
|
||||||
|
drawingDoors.getNumber(doorsNumber);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
public void setPosition(int x, int y)
|
||||||
|
{
|
||||||
|
if (x < 0 || y < 0 || x + trolleybusWidth >= _pictureWidth || y + trolleybusHeight >= _pictureHeight) {
|
||||||
|
x = 0;
|
||||||
|
y = 0;
|
||||||
|
}
|
||||||
|
_startPosX = x;
|
||||||
|
_startPosY = y;
|
||||||
|
}
|
||||||
|
public void moveTransport(DirectionType direction) {
|
||||||
|
if (entityTrolleybus == null)
|
||||||
|
return;
|
||||||
|
switch (direction) {
|
||||||
|
//влево
|
||||||
|
case LEFT:
|
||||||
|
if (_startPosX - entityTrolleybus.step.get().intValue() > 0)
|
||||||
|
_startPosX -= entityTrolleybus.step.get().intValue();
|
||||||
|
break;
|
||||||
|
//вверх
|
||||||
|
case UP:
|
||||||
|
if (_startPosY - entityTrolleybus.step.get().intValue() > 0)
|
||||||
|
_startPosY -= entityTrolleybus.step.get().intValue();
|
||||||
|
break;
|
||||||
|
// вправо
|
||||||
|
case RIGHT:
|
||||||
|
if (_startPosX + trolleybusWidth + entityTrolleybus.step.get().intValue() < _pictureWidth)
|
||||||
|
_startPosX += entityTrolleybus.step.get().intValue();
|
||||||
|
break;
|
||||||
|
//вниз
|
||||||
|
case DOWN:
|
||||||
|
if (_startPosY + trolleybusHeight + entityTrolleybus.step.get().intValue() < _pictureHeight)
|
||||||
|
_startPosY += entityTrolleybus.step.get().intValue();
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
public void drawTransport(Graphics2D graphics2D) {
|
public void drawTransport(Graphics2D graphics2D) {
|
||||||
if (!(entityBus instanceof EntityTrolleybus))
|
if (entityTrolleybus == null)
|
||||||
return;
|
return;
|
||||||
EntityTrolleybus entityTrolleybus = (EntityTrolleybus) entityBus;
|
|
||||||
BasicStroke pen = new BasicStroke(2);
|
BasicStroke pen = new BasicStroke(2);
|
||||||
graphics2D.setStroke(pen);
|
graphics2D.setStroke(pen);
|
||||||
|
Color bodyColor = entityTrolleybus.getBodyColor();
|
||||||
Color additionalColor = entityTrolleybus.getAdditionalColor();
|
Color additionalColor = entityTrolleybus.getAdditionalColor();
|
||||||
super.drawTransport(graphics2D);
|
//колеса
|
||||||
|
graphics2D.setPaint(Color.BLACK);
|
||||||
|
graphics2D.fillOval(_startPosX + 31, _startPosY + 106, 30, 30);
|
||||||
|
graphics2D.fillOval(_startPosX + 151, _startPosY + 106, 30, 30);
|
||||||
|
//кузов
|
||||||
|
graphics2D.setPaint(bodyColor);
|
||||||
|
graphics2D.fillRect(_startPosX + 6, _startPosY + 31, 200, 90);
|
||||||
|
//стекла
|
||||||
|
graphics2D.setPaint(Color.BLUE);
|
||||||
|
graphics2D.fillRect(_startPosX + 186, _startPosY + 40, 20, 40);
|
||||||
|
graphics2D.fillOval(_startPosX + 151, _startPosY + 35, 30, 40);
|
||||||
|
graphics2D.fillOval(_startPosX + 118, _startPosY + 35, 30, 40);
|
||||||
|
graphics2D.fillOval(_startPosX + 85, _startPosY + 35, 30, 40);
|
||||||
|
graphics2D.fillOval(_startPosX + 52, _startPosY + 35, 30, 40);
|
||||||
|
graphics2D.fillOval(_startPosX + 19, _startPosY + 35, 30, 40);
|
||||||
|
//двери
|
||||||
|
graphics2D.setPaint(Color.BLACK);
|
||||||
|
drawingDoors.drawDoors(graphics2D, _startPosX, _startPosY);
|
||||||
//рога
|
//рога
|
||||||
graphics2D.setPaint(Color.BLACK);
|
graphics2D.setPaint(Color.BLACK);
|
||||||
if (entityTrolleybus.getRoga()) {
|
if (entityTrolleybus.getRoga()) {
|
||||||
@ -34,5 +102,24 @@ public class DrawingTrolleybus extends DrawingBus{
|
|||||||
graphics2D.drawLine(_startPosX + 178, _startPosY + 111, _startPosX + 189, _startPosY + 111);
|
graphics2D.drawLine(_startPosX + 178, _startPosY + 111, _startPosX + 189, _startPosY + 111);
|
||||||
graphics2D.drawLine(_startPosX + 189, _startPosY + 111, _startPosX + 183, _startPosY + 119);
|
graphics2D.drawLine(_startPosX + 189, _startPosY + 111, _startPosX + 183, _startPosY + 119);
|
||||||
}
|
}
|
||||||
|
//границы троллейбуса
|
||||||
|
graphics2D.setPaint(Color.BLACK);
|
||||||
|
graphics2D.drawRect(_startPosX + 6, _startPosY + 31, 200, 90);
|
||||||
|
graphics2D.drawOval(_startPosX + 151, _startPosY + 35, 30, 40);
|
||||||
|
graphics2D.drawOval(_startPosX + 118, _startPosY + 35, 30, 40);
|
||||||
|
graphics2D.drawOval(_startPosX + 85, _startPosY + 35, 30, 40);
|
||||||
|
graphics2D.drawOval(_startPosX + 52, _startPosY + 35, 30, 40);
|
||||||
|
graphics2D.drawOval(_startPosX + 19, _startPosY + 35, 30, 40);
|
||||||
|
graphics2D.drawRect(_startPosX + 186, _startPosY + 40, 20, 40);
|
||||||
|
//задние фары
|
||||||
|
graphics2D.setPaint(Color.RED);
|
||||||
|
graphics2D.fillRect(_startPosX + 6, _startPosY + 91, 10, 20);
|
||||||
|
graphics2D.setPaint(Color.BLACK);
|
||||||
|
graphics2D.drawRect(_startPosX + 6, _startPosY + 91, 10, 20);
|
||||||
|
//передние фары
|
||||||
|
graphics2D.setPaint(Color.YELLOW);
|
||||||
|
graphics2D.fillRect(_startPosX + 196, _startPosY + 91, 10, 20);
|
||||||
|
graphics2D.setPaint(Color.BLACK);
|
||||||
|
graphics2D.drawRect(_startPosX + 196, _startPosY + 91, 10, 20);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,23 +1,2 @@
|
|||||||
import java.awt.*;
|
package PACKAGE_NAME;public class EntityBus {
|
||||||
import java.util.function.Supplier;
|
|
||||||
|
|
||||||
public class EntityBus {
|
|
||||||
private int speed;
|
|
||||||
public int getSpeed() {
|
|
||||||
return speed;
|
|
||||||
}
|
|
||||||
private double weight;
|
|
||||||
public double getWeight() {
|
|
||||||
return weight;
|
|
||||||
}
|
|
||||||
private Color bodyColor;
|
|
||||||
public Color getBodyColor() {
|
|
||||||
return bodyColor;
|
|
||||||
}
|
|
||||||
public Supplier<Double> step = () -> (double) speed * 100 / weight;
|
|
||||||
public EntityBus(int speed, double weight, Color bodyColor) {
|
|
||||||
this.speed = speed;
|
|
||||||
this.weight = weight;
|
|
||||||
this.bodyColor = bodyColor;
|
|
||||||
}
|
|
||||||
}
|
}
|
@ -1,6 +1,19 @@
|
|||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
public class EntityTrolleybus extends EntityBus{
|
public class EntityTrolleybus {
|
||||||
|
private int speed;
|
||||||
|
public int getSpeed(){
|
||||||
|
return speed;
|
||||||
|
}
|
||||||
|
private double weight;
|
||||||
|
public double getWeight(){
|
||||||
|
return weight;
|
||||||
|
}
|
||||||
|
private Color bodyColor;
|
||||||
|
public Color getBodyColor(){
|
||||||
|
return bodyColor;
|
||||||
|
}
|
||||||
private Color additionalColor;
|
private Color additionalColor;
|
||||||
public Color getAdditionalColor(){return additionalColor;}
|
public Color getAdditionalColor(){return additionalColor;}
|
||||||
private boolean roga;
|
private boolean roga;
|
||||||
@ -11,9 +24,12 @@ public class EntityTrolleybus extends EntityBus{
|
|||||||
public boolean getBattery() {
|
public boolean getBattery() {
|
||||||
return battery;
|
return battery;
|
||||||
}
|
}
|
||||||
public EntityTrolleybus(int speed, double weight, Color bodyColor, Color
|
public Supplier<Double> step = () -> (double) speed * 100 / weight;
|
||||||
|
public void init(int speed, double weight, Color bodyColor, Color
|
||||||
additionalColor, boolean roga, boolean battery) {
|
additionalColor, boolean roga, boolean battery) {
|
||||||
super(speed, weight, bodyColor);
|
this.speed = speed;
|
||||||
|
this.weight = weight;
|
||||||
|
this.bodyColor = bodyColor;
|
||||||
this.additionalColor = additionalColor;
|
this.additionalColor = additionalColor;
|
||||||
this.roga = roga;
|
this.roga = roga;
|
||||||
this.battery = battery;
|
this.battery = battery;
|
||||||
|
@ -5,11 +5,8 @@ import java.awt.event.ActionEvent;
|
|||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
|
||||||
public class FrameTrolleybus extends JFrame {
|
public class FrameTrolleybus extends JFrame {
|
||||||
private DrawingBus drawingBus;
|
private DrawingTrolleybus drawingTrolleybus;
|
||||||
private AbstractStrategy abstractStrategy;
|
|
||||||
private JComboBox<String> comboBoxStrategy;
|
|
||||||
private final JComponent pictureBoxTrolleybus;
|
private final JComponent pictureBoxTrolleybus;
|
||||||
public FrameTrolleybus() throws IOException {
|
public FrameTrolleybus() throws IOException {
|
||||||
super("Троллейбус");
|
super("Троллейбус");
|
||||||
@ -19,22 +16,17 @@ public class FrameTrolleybus extends JFrame {
|
|||||||
public void paintComponent(Graphics graphics){
|
public void paintComponent(Graphics graphics){
|
||||||
super.paintComponent(graphics);
|
super.paintComponent(graphics);
|
||||||
Graphics2D graphics2D = (Graphics2D) graphics;
|
Graphics2D graphics2D = (Graphics2D) graphics;
|
||||||
if (drawingBus != null) drawingBus.drawTransport(graphics2D);
|
if (drawingTrolleybus != null) drawingTrolleybus.drawTransport(graphics2D);
|
||||||
super.repaint();
|
super.repaint();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
comboBoxStrategy = new JComboBox<>(new String[]{"к центру", "к границе"});
|
JButton createButton = new JButton("Создать");
|
||||||
JButton stepButton = new JButton("Шаг");
|
|
||||||
JButton createBusButton = new JButton("Создать автобус");
|
|
||||||
JButton createTrolleybusButton = new JButton("Создать троллейбус");
|
|
||||||
JButton rightButton = new JButton(new ImageIcon(ImageIO.read(new File("img/right.png"))));
|
JButton rightButton = new JButton(new ImageIcon(ImageIO.read(new File("img/right.png"))));
|
||||||
JButton leftButton = new JButton(new ImageIcon(ImageIO.read(new File("img/left.png"))));
|
JButton leftButton = new JButton(new ImageIcon(ImageIO.read(new File("img/left.png"))));
|
||||||
JButton upButton = new JButton(new ImageIcon(ImageIO.read(new File("img/up.png"))));
|
JButton upButton = new JButton(new ImageIcon(ImageIO.read(new File("img/up.png"))));
|
||||||
JButton downButton = new JButton(new ImageIcon(ImageIO.read(new File("img/down.png"))));
|
JButton downButton = new JButton(new ImageIcon(ImageIO.read(new File("img/down.png"))));
|
||||||
pictureBoxTrolleybus.setBounds( 0, 0, getContentPane().getWidth(), getContentPane().getHeight());
|
pictureBoxTrolleybus.setBounds( 0, 0, getContentPane().getWidth(), getContentPane().getHeight());
|
||||||
createBusButton.addActionListener(e -> buttonCreateBusClick());
|
createButton.addActionListener(e -> buttonCreateTrolleybus());
|
||||||
createTrolleybusButton.addActionListener(e -> buttonCreateTrolleybusClick());
|
|
||||||
stepButton.addActionListener(e -> buttonStepClick());
|
|
||||||
rightButton.setActionCommand("right");
|
rightButton.setActionCommand("right");
|
||||||
rightButton.addActionListener(this::buttonMoveClick);
|
rightButton.addActionListener(this::buttonMoveClick);
|
||||||
leftButton.setActionCommand("left");
|
leftButton.setActionCommand("left");
|
||||||
@ -45,19 +37,14 @@ public class FrameTrolleybus extends JFrame {
|
|||||||
downButton.addActionListener(this::buttonMoveClick);
|
downButton.addActionListener(this::buttonMoveClick);
|
||||||
setLayout(new BorderLayout());
|
setLayout(new BorderLayout());
|
||||||
JPanel panelTrolleybus = new JPanel(new BorderLayout());
|
JPanel panelTrolleybus = new JPanel(new BorderLayout());
|
||||||
JPanel createPanel = new JPanel(new GridBagLayout());
|
JPanel createPanel = new JPanel(new BorderLayout());
|
||||||
GridBagConstraints constraints = new GridBagConstraints();
|
createPanel.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
|
||||||
constraints.gridx = 0;
|
createPanel.add(createButton, BorderLayout.SOUTH);
|
||||||
constraints.gridy = 0;
|
|
||||||
createPanel.add(createBusButton, constraints);
|
|
||||||
constraints.gridx = 1;
|
|
||||||
constraints.gridy = 0;
|
|
||||||
createPanel.add(createTrolleybusButton, constraints);
|
|
||||||
JPanel movementPanel = new JPanel(new GridBagLayout());
|
JPanel movementPanel = new JPanel(new GridBagLayout());
|
||||||
JPanel rightPanel = new JPanel(new BorderLayout());
|
JPanel rightPanel = new JPanel(new BorderLayout());
|
||||||
JPanel leftPanel = new JPanel(new BorderLayout());
|
|
||||||
rightPanel.add(movementPanel, BorderLayout.SOUTH);
|
rightPanel.add(movementPanel, BorderLayout.SOUTH);
|
||||||
rightButton.setPreferredSize(new Dimension(30,30));
|
rightButton.setPreferredSize(new Dimension(30,30));
|
||||||
|
GridBagConstraints constraints = new GridBagConstraints();
|
||||||
constraints.gridx = 2;
|
constraints.gridx = 2;
|
||||||
constraints.gridy = 1;
|
constraints.gridy = 1;
|
||||||
constraints.insets.left = constraints.insets.top = constraints.insets.bottom = constraints.insets.right = 2;
|
constraints.insets.left = constraints.insets.top = constraints.insets.bottom = constraints.insets.right = 2;
|
||||||
@ -74,101 +61,46 @@ public class FrameTrolleybus extends JFrame {
|
|||||||
constraints.gridx = 1;
|
constraints.gridx = 1;
|
||||||
constraints.gridy = 1;
|
constraints.gridy = 1;
|
||||||
movementPanel.add(downButton, constraints);
|
movementPanel.add(downButton, constraints);
|
||||||
JPanel stepPanel = new JPanel(new GridBagLayout());
|
|
||||||
constraints.gridx = 0;
|
|
||||||
constraints.gridy = 0;
|
|
||||||
stepPanel.add(comboBoxStrategy, constraints);
|
|
||||||
constraints.gridx = 0;
|
|
||||||
constraints.gridy = 1;
|
|
||||||
stepPanel.add(stepButton, constraints);
|
|
||||||
add(pictureBoxTrolleybus);
|
add(pictureBoxTrolleybus);
|
||||||
rightPanel.add(stepPanel, BorderLayout.NORTH);
|
|
||||||
leftPanel.add(createPanel, BorderLayout.SOUTH);
|
|
||||||
panelTrolleybus.add(rightPanel, BorderLayout.EAST);
|
panelTrolleybus.add(rightPanel, BorderLayout.EAST);
|
||||||
panelTrolleybus.add(leftPanel, BorderLayout.WEST);
|
panelTrolleybus.add(createPanel, BorderLayout.WEST);
|
||||||
add(panelTrolleybus,BorderLayout.CENTER);
|
add(panelTrolleybus,BorderLayout.CENTER);
|
||||||
setVisible(true);
|
setVisible(true);
|
||||||
}
|
}
|
||||||
private void buttonCreateTrolleybusClick() {
|
private void buttonCreateTrolleybus() {
|
||||||
Random random = new Random();
|
Random random = new Random();
|
||||||
|
drawingTrolleybus = new DrawingTrolleybus();
|
||||||
pictureBoxTrolleybus.setBounds(0,0,getContentPane().getWidth(),getContentPane().getHeight());
|
pictureBoxTrolleybus.setBounds(0,0,getContentPane().getWidth(),getContentPane().getHeight());
|
||||||
drawingBus = new DrawingTrolleybus(random.nextInt(200) + 100, random.nextInt(2000) + 1000,
|
drawingTrolleybus.init(random.nextInt(200) + 100, random.nextInt(2000) + 1000,
|
||||||
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)),
|
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)),
|
||||||
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)),
|
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)),
|
||||||
random.nextBoolean(), random.nextBoolean(), pictureBoxTrolleybus.getWidth(), pictureBoxTrolleybus.getHeight(),
|
random.nextBoolean(), random.nextBoolean(), pictureBoxTrolleybus.getWidth(), pictureBoxTrolleybus.getHeight(),
|
||||||
(random.nextInt(3)+1)*2, random.nextInt(3));
|
(random.nextInt(3)+1)*2);
|
||||||
drawingBus.setPosition(random.nextInt(90) + 10, random.nextInt(90) + 10);
|
drawingTrolleybus.setPosition(random.nextInt(90) + 10, random.nextInt(90) + 10);
|
||||||
draw();
|
draw();
|
||||||
}
|
}
|
||||||
private void buttonCreateBusClick(){
|
|
||||||
Random random = new Random();
|
|
||||||
pictureBoxTrolleybus.setBounds(0,0,getContentPane().getWidth(),getContentPane().getHeight());
|
|
||||||
drawingBus = new DrawingBus(
|
|
||||||
random.nextInt(200) + 100,
|
|
||||||
random.nextInt(2000) + 1000,
|
|
||||||
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)),
|
|
||||||
pictureBoxTrolleybus.getWidth(),
|
|
||||||
pictureBoxTrolleybus.getHeight(),
|
|
||||||
(random.nextInt(3)+1)*2,
|
|
||||||
random.nextInt(3));
|
|
||||||
drawingBus.setPosition(random.nextInt(90) + 10, random.nextInt(90) + 10);
|
|
||||||
draw();
|
|
||||||
}
|
|
||||||
private void buttonStepClick(){
|
|
||||||
if (drawingBus == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (comboBoxStrategy.isEnabled()) {
|
|
||||||
switch(comboBoxStrategy.getSelectedIndex()) {
|
|
||||||
case 0:
|
|
||||||
abstractStrategy = new MoveToCenter();
|
|
||||||
break;
|
|
||||||
case 1:
|
|
||||||
abstractStrategy = new MoveToBorder();
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
abstractStrategy = null;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if (abstractStrategy == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
abstractStrategy.setData(new DrawingObjectBus(drawingBus), pictureBoxTrolleybus.getWidth(), pictureBoxTrolleybus.getHeight());
|
|
||||||
comboBoxStrategy.setEnabled(false);
|
|
||||||
}
|
|
||||||
if (abstractStrategy == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
abstractStrategy.makeStep();
|
|
||||||
draw();
|
|
||||||
if (abstractStrategy.getStatus() == Status.FINISH)
|
|
||||||
{
|
|
||||||
comboBoxStrategy.setEnabled(true);
|
|
||||||
abstractStrategy = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
private void buttonMoveClick(ActionEvent event) {
|
private void buttonMoveClick(ActionEvent event) {
|
||||||
if(drawingBus == null || drawingBus.getEntityBus() == null)
|
if(drawingTrolleybus == null || drawingTrolleybus.getEntityTrolleybus() == null)
|
||||||
return;
|
return;
|
||||||
switch (event.getActionCommand())
|
switch (event.getActionCommand())
|
||||||
{
|
{
|
||||||
case "left":
|
case "left":
|
||||||
drawingBus.moveTransport(DirectionType.LEFT);
|
drawingTrolleybus.moveTransport(DirectionType.LEFT);
|
||||||
break;
|
break;
|
||||||
case "right":
|
case "right":
|
||||||
drawingBus.moveTransport(DirectionType.RIGHT);
|
drawingTrolleybus.moveTransport(DirectionType.RIGHT);
|
||||||
break;
|
break;
|
||||||
case "up":
|
case "up":
|
||||||
drawingBus.moveTransport(DirectionType.UP);
|
drawingTrolleybus.moveTransport(DirectionType.UP);
|
||||||
break;
|
break;
|
||||||
case "down":
|
case "down":
|
||||||
drawingBus.moveTransport(DirectionType.DOWN);
|
drawingTrolleybus.moveTransport(DirectionType.DOWN);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
draw();
|
draw();
|
||||||
}
|
}
|
||||||
private void draw() {
|
private void draw() {
|
||||||
if (drawingBus == null)
|
if (drawingTrolleybus == null)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,2 @@
|
|||||||
import java.awt.*;
|
package PACKAGE_NAME;public interface IDrawDoors {
|
||||||
|
|
||||||
public interface IDrawDoors {
|
|
||||||
void setNumber(int x);
|
|
||||||
void drawDoors(Graphics2D graphics2D, int _startX, int _startY);
|
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,2 @@
|
|||||||
public interface IMoveableObject {
|
package PACKAGE_NAME;public interface IMoveableObject {
|
||||||
ObjectParameters getObjectPosition();
|
|
||||||
|
|
||||||
int getStep();
|
|
||||||
boolean checkCanMove(DirectionType direction);
|
|
||||||
void moveObject(DirectionType direction);
|
|
||||||
}
|
}
|
||||||
|
@ -1,31 +1,2 @@
|
|||||||
public class MoveToBorder extends AbstractStrategy{
|
package PACKAGE_NAME;public class MoveToBorder {
|
||||||
@Override
|
|
||||||
protected boolean isTargetDestination() {
|
|
||||||
var objParams = getObjectParameters();
|
|
||||||
if (objParams == null) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return objParams.getRightBorder() + getStep() >= getFieldWidth() &&
|
|
||||||
objParams.getDownBorder() + getStep() >= getFieldHeight();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void moveToTarget() {
|
|
||||||
var objParams = getObjectParameters();
|
|
||||||
if (objParams == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
var diffX = objParams.getRightBorder() - getFieldWidth();
|
|
||||||
if (Math.abs(diffX) >= getStep()) {
|
|
||||||
if (diffX < 0) {
|
|
||||||
moveRight();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
var diffY = objParams.getDownBorder() - getFieldHeight();
|
|
||||||
if (Math.abs(diffY) >= getStep()) {
|
|
||||||
if (diffY < 0) {
|
|
||||||
moveDown();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -9,7 +9,6 @@ public class MoveToCenter extends AbstractStrategy{
|
|||||||
objParams.getObjectMiddleVertical() <= getFieldHeight() / 2 &&
|
objParams.getObjectMiddleVertical() <= getFieldHeight() / 2 &&
|
||||||
objParams.getObjectMiddleVertical() + getStep() >= getFieldHeight() / 2;
|
objParams.getObjectMiddleVertical() + getStep() >= getFieldHeight() / 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void moveToTarget() {
|
protected void moveToTarget() {
|
||||||
ObjectParameters objParams = getObjectParameters();
|
ObjectParameters objParams = getObjectParameters();
|
||||||
|
@ -1,20 +1,2 @@
|
|||||||
public class ObjectParameters {
|
package PACKAGE_NAME;public class ObjectParameters {
|
||||||
private final int POS_X;
|
|
||||||
private final int POS_Y;
|
|
||||||
private final int WIDTH;
|
|
||||||
private final int HEIGHT;
|
|
||||||
public int getLeftBorder() {return POS_X;}
|
|
||||||
public int getTopBorder() {return POS_Y;}
|
|
||||||
public int getRightBorder() {return POS_X + WIDTH;}
|
|
||||||
public int getDownBorder() {return POS_Y + HEIGHT;}
|
|
||||||
public int getObjectMiddleHorizontal() {return POS_X + this.WIDTH / 2;}
|
|
||||||
public int getObjectMiddleVertical() {return POS_Y + this.HEIGHT / 2;}
|
|
||||||
|
|
||||||
public ObjectParameters(int x, int y, int width, int height)
|
|
||||||
{
|
|
||||||
POS_X = x;
|
|
||||||
POS_Y = y;
|
|
||||||
WIDTH = width;
|
|
||||||
HEIGHT = height;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -2,5 +2,4 @@ public enum Status {
|
|||||||
NOT_INIT,
|
NOT_INIT,
|
||||||
IN_PROGRESS,
|
IN_PROGRESS,
|
||||||
FINISH
|
FINISH
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user