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">
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="21" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
|
@ -6,6 +6,7 @@ public abstract class AbstractStrategy {
|
||||
private int fieldHeight;
|
||||
protected int getFieldHeight(){return fieldHeight;}
|
||||
public Status getStatus() {return state;}
|
||||
|
||||
public void setData(IMoveableObject moveableObject, int width, int height){
|
||||
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.*;
|
||||
public class DrawingDoors {
|
||||
|
||||
public class DrawingDoors implements IDrawDoors{
|
||||
private DoorsNumber number;
|
||||
public void getNumber(int x){
|
||||
@Override
|
||||
public void setNumber(int x){
|
||||
if(x <= 3)
|
||||
number = DoorsNumber.THREE;
|
||||
if(x == 4)
|
||||
@ -9,6 +11,7 @@ public class DrawingDoors {
|
||||
if(x >= 5)
|
||||
number = DoorsNumber.FIVE;
|
||||
}
|
||||
@Override
|
||||
public void drawDoors(Graphics2D graphics2D, int _startX, int _startY){
|
||||
graphics2D.fillRect(_startX+52, _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.*;
|
||||
public class DrawingTrolleybus {
|
||||
private EntityTrolleybus entityTrolleybus;
|
||||
public EntityTrolleybus getEntityTrolleybus() {
|
||||
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)
|
||||
|
||||
public class DrawingTrolleybus extends DrawingBus{
|
||||
public DrawingTrolleybus(int speed, double weight, Color bodyColor, Color additionalColor,
|
||||
boolean roga, boolean battery, int width, int height, int doorsNumber, int doorsType)
|
||||
{
|
||||
if (width < trolleybusWidth || height < trolleybusHeight)
|
||||
return false;
|
||||
_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;
|
||||
}
|
||||
super(speed, weight, bodyColor, width, height, doorsNumber,doorsType);
|
||||
if (entityBus != null)
|
||||
entityBus = new EntityTrolleybus(speed, weight, bodyColor, additionalColor, roga, battery);
|
||||
}
|
||||
public void drawTransport(Graphics2D graphics2D) {
|
||||
if (entityTrolleybus == null)
|
||||
if (!(entityBus instanceof EntityTrolleybus))
|
||||
return;
|
||||
EntityTrolleybus entityTrolleybus = (EntityTrolleybus) entityBus;
|
||||
BasicStroke pen = new BasicStroke(2);
|
||||
graphics2D.setStroke(pen);
|
||||
Color bodyColor = entityTrolleybus.getBodyColor();
|
||||
Color additionalColor = entityTrolleybus.getAdditionalColor();
|
||||
//колеса
|
||||
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);
|
||||
super.drawTransport(graphics2D);
|
||||
//рога
|
||||
graphics2D.setPaint(Color.BLACK);
|
||||
if (entityTrolleybus.getRoga()) {
|
||||
@ -102,24 +34,5 @@ public class DrawingTrolleybus {
|
||||
graphics2D.drawLine(_startPosX + 178, _startPosY + 111, _startPosX + 189, _startPosY + 111);
|
||||
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.util.function.Supplier;
|
||||
|
||||
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;
|
||||
}
|
||||
public class EntityTrolleybus extends EntityBus{
|
||||
private Color additionalColor;
|
||||
public Color getAdditionalColor(){return additionalColor;}
|
||||
private boolean roga;
|
||||
@ -24,12 +11,9 @@ public class EntityTrolleybus {
|
||||
public boolean getBattery() {
|
||||
return battery;
|
||||
}
|
||||
public Supplier<Double> step = () -> (double) speed * 100 / weight;
|
||||
public void init(int speed, double weight, Color bodyColor, Color
|
||||
public EntityTrolleybus(int speed, double weight, Color bodyColor, Color
|
||||
additionalColor, boolean roga, boolean battery) {
|
||||
this.speed = speed;
|
||||
this.weight = weight;
|
||||
this.bodyColor = bodyColor;
|
||||
super(speed, weight, bodyColor);
|
||||
this.additionalColor = additionalColor;
|
||||
this.roga = roga;
|
||||
this.battery = battery;
|
||||
|
@ -5,8 +5,11 @@ import java.awt.event.ActionEvent;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Random;
|
||||
|
||||
public class FrameTrolleybus extends JFrame {
|
||||
private DrawingTrolleybus drawingTrolleybus;
|
||||
private DrawingBus drawingBus;
|
||||
private AbstractStrategy abstractStrategy;
|
||||
private JComboBox<String> comboBoxStrategy;
|
||||
private final JComponent pictureBoxTrolleybus;
|
||||
public FrameTrolleybus() throws IOException {
|
||||
super("Троллейбус");
|
||||
@ -16,17 +19,22 @@ public class FrameTrolleybus extends JFrame {
|
||||
public void paintComponent(Graphics graphics){
|
||||
super.paintComponent(graphics);
|
||||
Graphics2D graphics2D = (Graphics2D) graphics;
|
||||
if (drawingTrolleybus != null) drawingTrolleybus.drawTransport(graphics2D);
|
||||
if (drawingBus != null) drawingBus.drawTransport(graphics2D);
|
||||
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 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 downButton = new JButton(new ImageIcon(ImageIO.read(new File("img/down.png"))));
|
||||
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.addActionListener(this::buttonMoveClick);
|
||||
leftButton.setActionCommand("left");
|
||||
@ -37,14 +45,19 @@ public class FrameTrolleybus extends JFrame {
|
||||
downButton.addActionListener(this::buttonMoveClick);
|
||||
setLayout(new BorderLayout());
|
||||
JPanel panelTrolleybus = new JPanel(new BorderLayout());
|
||||
JPanel createPanel = new JPanel(new BorderLayout());
|
||||
createPanel.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
|
||||
createPanel.add(createButton, BorderLayout.SOUTH);
|
||||
JPanel createPanel = new JPanel(new GridBagLayout());
|
||||
GridBagConstraints constraints = new GridBagConstraints();
|
||||
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 rightPanel = new JPanel(new BorderLayout());
|
||||
JPanel leftPanel = new JPanel(new BorderLayout());
|
||||
rightPanel.add(movementPanel, BorderLayout.SOUTH);
|
||||
rightButton.setPreferredSize(new Dimension(30,30));
|
||||
GridBagConstraints constraints = new GridBagConstraints();
|
||||
constraints.gridx = 2;
|
||||
constraints.gridy = 1;
|
||||
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.gridy = 1;
|
||||
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);
|
||||
rightPanel.add(stepPanel, BorderLayout.NORTH);
|
||||
leftPanel.add(createPanel, BorderLayout.SOUTH);
|
||||
panelTrolleybus.add(rightPanel, BorderLayout.EAST);
|
||||
panelTrolleybus.add(createPanel, BorderLayout.WEST);
|
||||
panelTrolleybus.add(leftPanel, BorderLayout.WEST);
|
||||
add(panelTrolleybus,BorderLayout.CENTER);
|
||||
setVisible(true);
|
||||
}
|
||||
private void buttonCreateTrolleybus() {
|
||||
private void buttonCreateTrolleybusClick() {
|
||||
Random random = new Random();
|
||||
drawingTrolleybus = new DrawingTrolleybus();
|
||||
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)),
|
||||
random.nextBoolean(), random.nextBoolean(), pictureBoxTrolleybus.getWidth(), pictureBoxTrolleybus.getHeight(),
|
||||
(random.nextInt(3)+1)*2);
|
||||
drawingTrolleybus.setPosition(random.nextInt(90) + 10, random.nextInt(90) + 10);
|
||||
(random.nextInt(3)+1)*2, random.nextInt(3));
|
||||
drawingBus.setPosition(random.nextInt(90) + 10, random.nextInt(90) + 10);
|
||||
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) {
|
||||
if(drawingTrolleybus == null || drawingTrolleybus.getEntityTrolleybus() == null)
|
||||
if(drawingBus == null || drawingBus.getEntityBus() == null)
|
||||
return;
|
||||
switch (event.getActionCommand())
|
||||
{
|
||||
case "left":
|
||||
drawingTrolleybus.moveTransport(DirectionType.LEFT);
|
||||
drawingBus.moveTransport(DirectionType.LEFT);
|
||||
break;
|
||||
case "right":
|
||||
drawingTrolleybus.moveTransport(DirectionType.RIGHT);
|
||||
drawingBus.moveTransport(DirectionType.RIGHT);
|
||||
break;
|
||||
case "up":
|
||||
drawingTrolleybus.moveTransport(DirectionType.UP);
|
||||
drawingBus.moveTransport(DirectionType.UP);
|
||||
break;
|
||||
case "down":
|
||||
drawingTrolleybus.moveTransport(DirectionType.DOWN);
|
||||
drawingBus.moveTransport(DirectionType.DOWN);
|
||||
break;
|
||||
}
|
||||
draw();
|
||||
}
|
||||
private void draw() {
|
||||
if (drawingTrolleybus == null)
|
||||
if (drawingBus == null)
|
||||
{
|
||||
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() + getStep() >= getFieldHeight() / 2;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void moveToTarget() {
|
||||
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,
|
||||
IN_PROGRESS,
|
||||
FINISH
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user