Compare commits
2 Commits
f9487fc660
...
dc8923d850
Author | SHA1 | Date | |
---|---|---|---|
dc8923d850 | |||
43d4951e5d |
@ -1,4 +1,3 @@
|
|||||||
<?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,6 +6,7 @@ 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,2 +1,137 @@
|
|||||||
package PACKAGE_NAME;public class DrawingBus {
|
import java.awt.*;
|
||||||
|
|
||||||
|
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,7 +1,9 @@
|
|||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
public class DrawingDoors {
|
|
||||||
|
public class DrawingDoors implements IDrawDoors{
|
||||||
private DoorsNumber number;
|
private DoorsNumber number;
|
||||||
public void getNumber(int x){
|
@Override
|
||||||
|
public void setNumber(int x){
|
||||||
if(x <= 3)
|
if(x <= 3)
|
||||||
number = DoorsNumber.THREE;
|
number = DoorsNumber.THREE;
|
||||||
if(x == 4)
|
if(x == 4)
|
||||||
@ -9,6 +11,7 @@ public class DrawingDoors {
|
|||||||
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,2 +1,31 @@
|
|||||||
package PACKAGE_NAME;public class DrawingDoorsRoundedUp {
|
import java.awt.*;
|
||||||
|
|
||||||
|
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,2 +1,36 @@
|
|||||||
package PACKAGE_NAME;public class DrawingDoorsRoundedUpAndDown {
|
import java.awt.*;
|
||||||
|
|
||||||
|
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,2 +1,29 @@
|
|||||||
package PACKAGE_NAME;public class DrawingObjectBus {
|
public class DrawingObjectBus implements IMoveableObject{
|
||||||
|
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,89 +1,21 @@
|
|||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
public class DrawingTrolleybus {
|
|
||||||
private EntityTrolleybus entityTrolleybus;
|
public class DrawingTrolleybus extends DrawingBus{
|
||||||
public EntityTrolleybus getEntityTrolleybus() {
|
public DrawingTrolleybus(int speed, double weight, Color bodyColor, Color additionalColor,
|
||||||
return entityTrolleybus;
|
boolean roga, boolean battery, int width, int height, int doorsNumber, int doorsType)
|
||||||
}
|
|
||||||
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)
|
|
||||||
{
|
{
|
||||||
if (width < trolleybusWidth || height < trolleybusHeight)
|
super(speed, weight, bodyColor, width, height, doorsNumber,doorsType);
|
||||||
return false;
|
if (entityBus != null)
|
||||||
_pictureWidth = width;
|
entityBus = new EntityTrolleybus(speed, weight, bodyColor, additionalColor, roga, battery);
|
||||||
_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 (entityTrolleybus == null)
|
if (!(entityBus instanceof EntityTrolleybus))
|
||||||
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()) {
|
||||||
@ -102,24 +34,5 @@ public class DrawingTrolleybus {
|
|||||||
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,2 +1,23 @@
|
|||||||
package PACKAGE_NAME;public class EntityBus {
|
import java.awt.*;
|
||||||
}
|
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,19 +1,6 @@
|
|||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
import java.util.function.Supplier;
|
|
||||||
|
|
||||||
public class EntityTrolleybus {
|
public class EntityTrolleybus extends 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;
|
|
||||||
}
|
|
||||||
private Color additionalColor;
|
private Color additionalColor;
|
||||||
public Color getAdditionalColor(){return additionalColor;}
|
public Color getAdditionalColor(){return additionalColor;}
|
||||||
private boolean roga;
|
private boolean roga;
|
||||||
@ -24,12 +11,9 @@ public class EntityTrolleybus {
|
|||||||
public boolean getBattery() {
|
public boolean getBattery() {
|
||||||
return battery;
|
return battery;
|
||||||
}
|
}
|
||||||
public Supplier<Double> step = () -> (double) speed * 100 / weight;
|
public EntityTrolleybus(int speed, double weight, Color bodyColor, Color
|
||||||
public void init(int speed, double weight, Color bodyColor, Color
|
|
||||||
additionalColor, boolean roga, boolean battery) {
|
additionalColor, boolean roga, boolean battery) {
|
||||||
this.speed = speed;
|
super(speed, weight, bodyColor);
|
||||||
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,8 +5,11 @@ 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 DrawingTrolleybus drawingTrolleybus;
|
private DrawingBus drawingBus;
|
||||||
|
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("Троллейбус");
|
||||||
@ -16,17 +19,22 @@ 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 (drawingTrolleybus != null) drawingTrolleybus.drawTransport(graphics2D);
|
if (drawingBus != null) drawingBus.drawTransport(graphics2D);
|
||||||
super.repaint();
|
super.repaint();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
JButton createButton = new JButton("Создать");
|
comboBoxStrategy = new JComboBox<>(new String[]{"к центру", "к границе"});
|
||||||
|
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());
|
||||||
createButton.addActionListener(e -> buttonCreateTrolleybus());
|
createBusButton.addActionListener(e -> buttonCreateBusClick());
|
||||||
|
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");
|
||||||
@ -37,14 +45,19 @@ 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 BorderLayout());
|
JPanel createPanel = new JPanel(new GridBagLayout());
|
||||||
createPanel.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
|
GridBagConstraints constraints = new GridBagConstraints();
|
||||||
createPanel.add(createButton, BorderLayout.SOUTH);
|
constraints.gridx = 0;
|
||||||
|
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;
|
||||||
@ -61,46 +74,101 @@ 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(createPanel, BorderLayout.WEST);
|
panelTrolleybus.add(leftPanel, BorderLayout.WEST);
|
||||||
add(panelTrolleybus,BorderLayout.CENTER);
|
add(panelTrolleybus,BorderLayout.CENTER);
|
||||||
setVisible(true);
|
setVisible(true);
|
||||||
}
|
}
|
||||||
private void buttonCreateTrolleybus() {
|
private void buttonCreateTrolleybusClick() {
|
||||||
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());
|
||||||
drawingTrolleybus.init(random.nextInt(200) + 100, random.nextInt(2000) + 1000,
|
drawingBus = new DrawingTrolleybus(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)+1)*2, random.nextInt(3));
|
||||||
drawingTrolleybus.setPosition(random.nextInt(90) + 10, random.nextInt(90) + 10);
|
drawingBus.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(drawingTrolleybus == null || drawingTrolleybus.getEntityTrolleybus() == null)
|
if(drawingBus == null || drawingBus.getEntityBus() == null)
|
||||||
return;
|
return;
|
||||||
switch (event.getActionCommand())
|
switch (event.getActionCommand())
|
||||||
{
|
{
|
||||||
case "left":
|
case "left":
|
||||||
drawingTrolleybus.moveTransport(DirectionType.LEFT);
|
drawingBus.moveTransport(DirectionType.LEFT);
|
||||||
break;
|
break;
|
||||||
case "right":
|
case "right":
|
||||||
drawingTrolleybus.moveTransport(DirectionType.RIGHT);
|
drawingBus.moveTransport(DirectionType.RIGHT);
|
||||||
break;
|
break;
|
||||||
case "up":
|
case "up":
|
||||||
drawingTrolleybus.moveTransport(DirectionType.UP);
|
drawingBus.moveTransport(DirectionType.UP);
|
||||||
break;
|
break;
|
||||||
case "down":
|
case "down":
|
||||||
drawingTrolleybus.moveTransport(DirectionType.DOWN);
|
drawingBus.moveTransport(DirectionType.DOWN);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
draw();
|
draw();
|
||||||
}
|
}
|
||||||
private void draw() {
|
private void draw() {
|
||||||
if (drawingTrolleybus == null)
|
if (drawingBus == null)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -1,2 +1,6 @@
|
|||||||
package PACKAGE_NAME;public interface IDrawDoors {
|
import java.awt.*;
|
||||||
|
|
||||||
|
public interface IDrawDoors {
|
||||||
|
void setNumber(int x);
|
||||||
|
void drawDoors(Graphics2D graphics2D, int _startX, int _startY);
|
||||||
}
|
}
|
||||||
|
@ -1,2 +1,7 @@
|
|||||||
package PACKAGE_NAME;public interface IMoveableObject {
|
public interface IMoveableObject {
|
||||||
|
ObjectParameters getObjectPosition();
|
||||||
|
|
||||||
|
int getStep();
|
||||||
|
boolean checkCanMove(DirectionType direction);
|
||||||
|
void moveObject(DirectionType direction);
|
||||||
}
|
}
|
||||||
|
@ -1,2 +1,31 @@
|
|||||||
package PACKAGE_NAME;public class MoveToBorder {
|
public class MoveToBorder extends AbstractStrategy{
|
||||||
|
@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,6 +9,7 @@ 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,2 +1,20 @@
|
|||||||
package PACKAGE_NAME;public class ObjectParameters {
|
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,4 +2,5 @@ public enum Status {
|
|||||||
NOT_INIT,
|
NOT_INIT,
|
||||||
IN_PROGRESS,
|
IN_PROGRESS,
|
||||||
FINISH
|
FINISH
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user