Compare commits
5 Commits
Author | SHA1 | Date | |
---|---|---|---|
4f75d6a8b6 | |||
07e04299a6 | |||
f681dfddc4 | |||
31cdee9b88 | |||
6f8fe243c3 |
95
Trolleybus/AbstractStrategy.java
Normal file
95
Trolleybus/AbstractStrategy.java
Normal file
@ -0,0 +1,95 @@
|
||||
package Trolleybus;
|
||||
|
||||
// Класс-стратегия перемещения объекта
|
||||
public abstract class AbstractStrategy {
|
||||
private IMoveableObject _moveableObject;
|
||||
|
||||
private Status _state = Status.NotInit;
|
||||
|
||||
private int FieldWidth;
|
||||
|
||||
protected int FieldWidth() {
|
||||
return FieldWidth;
|
||||
}
|
||||
|
||||
private int FieldHeight;
|
||||
|
||||
protected int FieldHeight() {
|
||||
return FieldHeight;
|
||||
}
|
||||
|
||||
public Status GetStatus() {
|
||||
return _state;
|
||||
}
|
||||
|
||||
public void SetData(IMoveableObject moveableObject, int width, int height) {
|
||||
if (moveableObject == null) {
|
||||
_state = Status.NotInit;
|
||||
return;
|
||||
}
|
||||
|
||||
_state = Status.InProgress;
|
||||
_moveableObject = moveableObject;
|
||||
FieldWidth = width;
|
||||
FieldHeight = height;
|
||||
}
|
||||
|
||||
public void MakeStep() {
|
||||
if (_state != Status.InProgress) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (IsTargetDestination()) {
|
||||
_state = Status.Finish;
|
||||
return;
|
||||
}
|
||||
MoveToTarget();
|
||||
}
|
||||
|
||||
protected boolean MoveLeft() {
|
||||
return MoveTo(DirectionType.Left);
|
||||
}
|
||||
|
||||
protected boolean MoveRight() {
|
||||
return MoveTo(DirectionType.Right);
|
||||
}
|
||||
|
||||
protected boolean MoveUp() {
|
||||
return MoveTo(DirectionType.Up);
|
||||
}
|
||||
|
||||
protected boolean MoveDown() {
|
||||
return MoveTo(DirectionType.Down);
|
||||
}
|
||||
|
||||
protected ObjectParameters GetObjectParameters(){
|
||||
if (_moveableObject != null) {
|
||||
return _moveableObject.GetObjectPosition();
|
||||
}
|
||||
else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
protected Integer GetStep() {
|
||||
if (_state != Status.InProgress) {
|
||||
return null;
|
||||
}
|
||||
return _moveableObject.GetStep();
|
||||
}
|
||||
|
||||
protected abstract void MoveToTarget();
|
||||
|
||||
protected abstract boolean IsTargetDestination();
|
||||
|
||||
private boolean MoveTo(DirectionType directionType) {
|
||||
if (_state != Status.InProgress) {
|
||||
return false;
|
||||
}
|
||||
if (_moveableObject.CheckCanMove(directionType)) {
|
||||
_moveableObject.MoveObject(directionType);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
86
Trolleybus/BusesGenericCollection.java
Normal file
86
Trolleybus/BusesGenericCollection.java
Normal file
@ -0,0 +1,86 @@
|
||||
package Trolleybus;
|
||||
|
||||
import java.awt.*;
|
||||
import javax.swing.*;
|
||||
// Параметризованный класс для набора объектов DrawingBus
|
||||
public class BusesGenericCollection <T extends DrawingBus, U extends IMoveableObject> {
|
||||
private final int _pictureWidth;
|
||||
private final int _pictureHeight;
|
||||
private final int _placeSizeWidth = 150;
|
||||
private final int _placeSizeHeight = 95;
|
||||
private final SetGeneric<T> _collection;
|
||||
public BusesGenericCollection(int picWidth, int picHeight)
|
||||
{
|
||||
int width = picWidth / _placeSizeWidth; //width - кол-во помещаемых на PictureBox автобусов по горизонтали
|
||||
int height = picHeight / _placeSizeHeight; //height - кол-во помещаемых на PictureBox автобусов по вертикали
|
||||
_pictureWidth = picWidth;
|
||||
_pictureHeight = picHeight;
|
||||
_collection = new SetGeneric<T>(width * height); //width*height - кол-во мест на PictureBox для автобусов; размер массива
|
||||
}
|
||||
//На Java нельзя перегрузить операторы + и -, поэтому ниже обычные методы
|
||||
public int Add(T obj){
|
||||
if (obj == null) {
|
||||
return -1;
|
||||
}
|
||||
return _collection.Insert(obj);
|
||||
}
|
||||
public boolean Remove(int position) {
|
||||
T obj = _collection.Get(position);
|
||||
if (obj != null) {
|
||||
_collection.Remove(position);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public U GetU(int pos){
|
||||
return (U)_collection.Get(pos).GetMoveableObject();
|
||||
}
|
||||
//Вывод всех объектов
|
||||
public void ShowBuses(JPanel panelToDraw) {
|
||||
Graphics gr = panelToDraw.getGraphics();
|
||||
//Очистка перед перерисовкой
|
||||
panelToDraw.paint(gr);
|
||||
DrawBackground(gr);
|
||||
DrawObjects(gr);
|
||||
}
|
||||
//Прорисовка фона (чёрных линий)
|
||||
private void DrawBackground(Graphics g) {
|
||||
Graphics2D g2d = (Graphics2D)g;
|
||||
g2d.setColor(Color.BLACK);
|
||||
//вертикальные линии
|
||||
for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++)
|
||||
{
|
||||
g2d.drawLine(i * (_placeSizeWidth + 10), 0, i * (_placeSizeWidth + 10), (_pictureHeight / _placeSizeHeight) * (_placeSizeHeight + 10));
|
||||
}
|
||||
//горизонтальные линии
|
||||
for (int i = 0; i <= _pictureHeight / _placeSizeHeight; i++)
|
||||
{
|
||||
for (int j = 0; j < _pictureWidth / _placeSizeWidth; j++)
|
||||
{
|
||||
g2d.drawLine(j * (_placeSizeWidth + 10), i * (_placeSizeHeight + 10), j * (_placeSizeWidth + 10) + _placeSizeWidth / 2, i * (_placeSizeHeight + 10));
|
||||
}
|
||||
}
|
||||
}
|
||||
//Прорисовка объектов
|
||||
private void DrawObjects(Graphics g) {
|
||||
int i = 0;
|
||||
int j = _pictureWidth / _placeSizeWidth - 1;
|
||||
for (int k = 0; k < _collection.Count; k++)
|
||||
{
|
||||
DrawingBus bus = _collection.Get(k);
|
||||
if (bus != null)
|
||||
{
|
||||
bus.SetPosition(j * (_placeSizeWidth + 10) + 5, i * (_placeSizeHeight) + 10);
|
||||
bus.DrawTransport(g);
|
||||
}
|
||||
j--;
|
||||
//переход на новую строчку
|
||||
if (j < 0)
|
||||
{
|
||||
j = _pictureWidth / _placeSizeWidth - 1;
|
||||
i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
7
Trolleybus/CntOfDoors.java
Normal file
7
Trolleybus/CntOfDoors.java
Normal file
@ -0,0 +1,7 @@
|
||||
package Trolleybus;
|
||||
|
||||
public enum CntOfDoors {
|
||||
Three,
|
||||
Four,
|
||||
Five
|
||||
}
|
12
Trolleybus/DirectionType.java
Normal file
12
Trolleybus/DirectionType.java
Normal file
@ -0,0 +1,12 @@
|
||||
package Trolleybus;
|
||||
|
||||
public enum DirectionType {
|
||||
Up (1),
|
||||
Down (2),
|
||||
Right (3),
|
||||
Left (4);
|
||||
private int Direction;
|
||||
DirectionType(int direction) {
|
||||
Direction = direction;
|
||||
}
|
||||
}
|
193
Trolleybus/DrawingBus.java
Normal file
193
Trolleybus/DrawingBus.java
Normal file
@ -0,0 +1,193 @@
|
||||
package Trolleybus;
|
||||
|
||||
import java.awt.*;
|
||||
import java.util.Random;
|
||||
|
||||
public class DrawingBus {
|
||||
public EntityBus EntityBus;
|
||||
public EntityBus getEntityBus() {return EntityBus;}
|
||||
private void setEntityBus(EntityBus entityBus) {EntityBus = entityBus;}
|
||||
public IDrawingDoors Doors;
|
||||
// Ширина окна
|
||||
private int _pictureWidth;
|
||||
// Высота окна
|
||||
private int _pictureHeight;
|
||||
// Левая координата прорисовки автобуса
|
||||
protected int _startPosX;
|
||||
// Верхняя координата прорисовки автобуса
|
||||
protected int _startPosY;
|
||||
// Ширина прорисовки автобуса
|
||||
protected int _busWidth = 150;
|
||||
// Высота прорисовки автобуса
|
||||
protected int _busHeight = 95;
|
||||
|
||||
public int GetPosX() {
|
||||
return _startPosX;
|
||||
}
|
||||
|
||||
public int GetPosY() {
|
||||
return _startPosY;
|
||||
}
|
||||
|
||||
public int GetWidth() {
|
||||
return _busWidth;
|
||||
}
|
||||
|
||||
public int GetHeight() {
|
||||
return _busHeight;
|
||||
}
|
||||
|
||||
public DrawingBus(int speed, double weight, Color bodyColor, int width, int height) {
|
||||
if (width < _busWidth || height < _busHeight) {
|
||||
return;
|
||||
}
|
||||
_startPosX = 0;
|
||||
_startPosY = 0;
|
||||
|
||||
_pictureWidth = width;
|
||||
_pictureHeight = height;
|
||||
EntityBus = new EntityBus(speed, weight, bodyColor);
|
||||
Random random = new Random();
|
||||
|
||||
//Форма дверей
|
||||
int shape = random.nextInt(1,4);
|
||||
switch (shape) {
|
||||
case 1:
|
||||
Doors = new DrawingDoors();
|
||||
break;
|
||||
case 2:
|
||||
Doors = new DrawingOvalDoors();
|
||||
break;
|
||||
case 3:
|
||||
Doors = new DrawingTriangleDoors();
|
||||
break;
|
||||
}
|
||||
//Количество дверей
|
||||
Doors.SetCntOfDoors(random.nextInt(3, 6));
|
||||
}
|
||||
protected DrawingBus(int speed, double weight, Color bodyColor, int width, int height, int busWidth, int busHeight) {
|
||||
if (width <= _busWidth || height <= _busHeight) {
|
||||
return;
|
||||
}
|
||||
|
||||
_startPosX = 0;
|
||||
_startPosY = 0;
|
||||
_pictureWidth = width;
|
||||
_pictureHeight = height;
|
||||
_busWidth = busWidth;
|
||||
_busHeight = busHeight;
|
||||
EntityBus = new EntityBus(speed, weight, bodyColor);
|
||||
Random random = new Random();
|
||||
|
||||
//Форма дверей
|
||||
int shape = random.nextInt(1,4);
|
||||
switch (shape) {
|
||||
case 1:
|
||||
Doors = new DrawingDoors();
|
||||
break;
|
||||
case 2:
|
||||
Doors = new DrawingOvalDoors();
|
||||
break;
|
||||
case 3:
|
||||
Doors = new DrawingTriangleDoors();
|
||||
break;
|
||||
}
|
||||
//Количество дверей
|
||||
Doors.SetCntOfDoors(random.nextInt(3, 6));
|
||||
}
|
||||
//Конструктор для усложнённой 3
|
||||
public DrawingBus(EntityBus bus, IDrawingDoors doors, int width, int height) {
|
||||
if (width < _busWidth || height < _busHeight) {
|
||||
return;
|
||||
}
|
||||
_startPosX = 0;
|
||||
_startPosY = 0;
|
||||
_pictureWidth = width;
|
||||
_pictureHeight = height;
|
||||
|
||||
EntityBus = bus;
|
||||
Doors = doors;
|
||||
}
|
||||
|
||||
public void SetPosition(int x, int y){
|
||||
_startPosX = Math.min(Math.max(x, 0), _pictureWidth - _busWidth);
|
||||
_startPosY = Math.min(Math.max(y, 0), _pictureHeight - _busHeight);
|
||||
}
|
||||
public boolean CanMove(DirectionType direction)
|
||||
{
|
||||
if (EntityBus == null)
|
||||
return false;
|
||||
boolean can = false;
|
||||
switch (direction)
|
||||
{
|
||||
case Left:
|
||||
can = _startPosX - EntityBus.Step >= 0;
|
||||
break;
|
||||
case Right:
|
||||
can = _startPosX + EntityBus.Step + _busWidth < _pictureWidth;
|
||||
break;
|
||||
case Down:
|
||||
can = _startPosY + EntityBus.Step + _busHeight < _pictureHeight;
|
||||
break;
|
||||
case Up:
|
||||
can = _startPosY - EntityBus.Step >= 0;
|
||||
break;
|
||||
};
|
||||
return can;
|
||||
}
|
||||
|
||||
public void MoveTransport(DirectionType direction){
|
||||
if (!CanMove(direction) || EntityBus == null) {
|
||||
return;
|
||||
}
|
||||
switch (direction)
|
||||
{
|
||||
case Left:
|
||||
if (_startPosX - EntityBus.Step >= 0) {
|
||||
_startPosX -= (int) EntityBus.Step;
|
||||
}
|
||||
break;
|
||||
case Up:
|
||||
if (_startPosY - EntityBus.Step >= 0) {
|
||||
_startPosY -= (int) EntityBus.Step;
|
||||
}
|
||||
break;
|
||||
case Right:
|
||||
if (_startPosX + EntityBus.Step + _busWidth <= _pictureWidth) {
|
||||
_startPosX += (int) EntityBus.Step;
|
||||
}
|
||||
break;
|
||||
case Down:
|
||||
if (_startPosY + EntityBus.Step + _busHeight <= _pictureHeight) {
|
||||
_startPosY += (int) EntityBus.Step;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public void DrawTransport(Graphics g) {
|
||||
|
||||
if (EntityBus == null) {
|
||||
return;
|
||||
}
|
||||
Graphics2D g2d = (Graphics2D)g;
|
||||
//Корпус
|
||||
g2d.setColor(EntityBus.getBodyColor());
|
||||
g2d.drawLine(_startPosX, _startPosY + 30, _startPosX, _startPosY + 80);
|
||||
g2d.drawLine(_startPosX, _startPosY + 80, _startPosX + 20, _startPosY + 80);
|
||||
g2d.drawLine(_startPosX + 45, _startPosY + 80, _startPosX + 105, _startPosY + 80);
|
||||
g2d.drawLine(_startPosX + 130, _startPosY + 80, _startPosX + 150, _startPosY + 80);
|
||||
g2d.drawLine(_startPosX + 150, _startPosY + 80, _startPosX + 150, _startPosY + 30);
|
||||
g2d.drawLine(_startPosX + 150, _startPosY + 30, _startPosX, _startPosY + 30);
|
||||
//Колёса
|
||||
g2d.setColor(Color.BLACK);
|
||||
g2d.drawOval(_startPosX + 20, _startPosY + 70, 25, 25);
|
||||
g2d.drawOval(_startPosX + 105, _startPosY + 70, 25, 25);
|
||||
//Двери
|
||||
Doors.DrawDoors(g2d, EntityBus.getBodyColor(), _startPosX, _startPosY);
|
||||
}
|
||||
|
||||
public IMoveableObject GetMoveableObject(){
|
||||
return new DrawingObjectBus(this);
|
||||
}
|
||||
}
|
56
Trolleybus/DrawingDoors.java
Normal file
56
Trolleybus/DrawingDoors.java
Normal file
@ -0,0 +1,56 @@
|
||||
package Trolleybus;
|
||||
|
||||
import java.awt.*;
|
||||
public class DrawingDoors implements IDrawingDoors{
|
||||
private CntOfDoors _cntOfDoors;
|
||||
public void SetCntOfDoors(int cnt) {
|
||||
if (cnt <= 3) {
|
||||
_cntOfDoors = CntOfDoors.Three;
|
||||
}
|
||||
if (cnt == 4) {
|
||||
_cntOfDoors = CntOfDoors.Four;
|
||||
}
|
||||
if (cnt >= 5) {
|
||||
_cntOfDoors = CntOfDoors.Five;
|
||||
}
|
||||
}
|
||||
|
||||
public void DrawDoors(Graphics2D g, Color BodyColor, int _startPosX, int _startPosY) {
|
||||
switch (_cntOfDoors)
|
||||
{
|
||||
case Three:
|
||||
g.setColor(BodyColor);
|
||||
g.drawRect(_startPosX + 2, _startPosY + 50, 16, 30);
|
||||
g.drawRect(_startPosX + 67, _startPosY + 50, 16, 30);
|
||||
g.drawRect(_startPosX + 132, _startPosY + 50, 16, 30);
|
||||
g.setColor(Color.CYAN);
|
||||
g.drawOval(_startPosX + 22, _startPosY + 35, 16, 24);
|
||||
g.drawOval(_startPosX + 47, _startPosY + 35, 16, 24);
|
||||
g.drawOval(_startPosX + 87, _startPosY + 35, 16, 24);
|
||||
g.drawOval(_startPosX + 112, _startPosY + 35, 16, 24);
|
||||
break;
|
||||
case Four:
|
||||
g.setColor(BodyColor);
|
||||
g.drawRect(_startPosX + 2, _startPosY + 50, 16, 30);
|
||||
g.drawRect(_startPosX + 48, _startPosY + 50, 16, 30);
|
||||
g.drawRect(_startPosX + 86, _startPosY + 50, 16, 30);
|
||||
g.drawRect(_startPosX + 132, _startPosY + 50, 16, 30);
|
||||
g.setColor(Color.CYAN);
|
||||
g.drawOval(_startPosX + 25, _startPosY + 35, 16, 24);
|
||||
g.drawOval(_startPosX + 67, _startPosY + 35, 16, 24);
|
||||
g.drawOval(_startPosX + 109, _startPosY + 35, 16, 24);
|
||||
break;
|
||||
case Five:
|
||||
g.setColor(BodyColor);
|
||||
g.drawRect(_startPosX + 2, _startPosY + 50, 16, 30);
|
||||
g.drawRect(_startPosX + 48, _startPosY + 50, 16, 30);
|
||||
g.drawRect(_startPosX + 67, _startPosY + 50, 16, 30);
|
||||
g.drawRect(_startPosX + 86, _startPosY + 50, 16, 30);
|
||||
g.drawRect(_startPosX + 132, _startPosY + 50, 16, 30);
|
||||
g.setColor(Color.CYAN);
|
||||
g.drawOval(_startPosX + 25, _startPosY + 35, 16, 24);
|
||||
g.drawOval(_startPosX + 109, _startPosY + 35, 16, 24);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
37
Trolleybus/DrawingObjectBus.java
Normal file
37
Trolleybus/DrawingObjectBus.java
Normal file
@ -0,0 +1,37 @@
|
||||
package Trolleybus;
|
||||
|
||||
public class DrawingObjectBus implements IMoveableObject {
|
||||
private final DrawingBus _drawingBus;
|
||||
|
||||
public DrawingObjectBus(DrawingBus drawingBus){
|
||||
_drawingBus = drawingBus;
|
||||
}
|
||||
|
||||
public ObjectParameters GetObjectPosition(){
|
||||
if (_drawingBus == null || _drawingBus.EntityBus == null) {
|
||||
return null;
|
||||
}
|
||||
return new ObjectParameters(_drawingBus.GetPosX(), _drawingBus.GetPosY(), _drawingBus.GetWidth(), _drawingBus.GetHeight());
|
||||
}
|
||||
|
||||
public int GetStep(){
|
||||
if (_drawingBus.EntityBus == null) {
|
||||
return 0;
|
||||
}
|
||||
return (int)_drawingBus.EntityBus.Step;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
59
Trolleybus/DrawingOvalDoors.java
Normal file
59
Trolleybus/DrawingOvalDoors.java
Normal file
@ -0,0 +1,59 @@
|
||||
package Trolleybus;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawingOvalDoors implements IDrawingDoors {
|
||||
private CntOfDoors _cntOfDoors;
|
||||
public void SetCntOfDoors(int cnt) {
|
||||
if (cnt <= 3) {
|
||||
_cntOfDoors = CntOfDoors.Three;
|
||||
}
|
||||
if (cnt == 4) {
|
||||
_cntOfDoors = CntOfDoors.Four;
|
||||
}
|
||||
if (cnt >= 5) {
|
||||
_cntOfDoors = CntOfDoors.Five;
|
||||
}
|
||||
}
|
||||
public void DrawDoors(Graphics2D g, Color BodyColor, int _startPosX, int _startPosY) {
|
||||
switch (_cntOfDoors)
|
||||
{
|
||||
case Three:
|
||||
g.setColor(BodyColor);
|
||||
g.drawArc(_startPosX + 2, _startPosY + 50, 16, 60, 0, 180);
|
||||
g.drawArc(_startPosX + 67, _startPosY + 50, 16, 60, 0, 180);
|
||||
g.drawArc(_startPosX + 132, _startPosY + 50, 16, 60, 0, 180);
|
||||
|
||||
g.setColor(Color.CYAN);
|
||||
g.drawOval(_startPosX + 22, _startPosY + 35, 16, 24);
|
||||
g.drawOval(_startPosX + 47, _startPosY + 35, 16, 24);
|
||||
g.drawOval(_startPosX + 87, _startPosY + 35, 16, 24);
|
||||
g.drawOval(_startPosX + 112, _startPosY + 35, 16, 24);
|
||||
break;
|
||||
case Four:
|
||||
g.setColor(BodyColor);
|
||||
g.drawArc(_startPosX + 2, _startPosY + 50, 16, 60, 0, 180);
|
||||
g.drawArc(_startPosX + 48, _startPosY + 50, 16, 60, 0, 180);
|
||||
g.drawArc(_startPosX + 86, _startPosY + 50, 16, 60, 0, 180);
|
||||
g.drawArc(_startPosX + 132, _startPosY + 50, 16, 60, 0, 180);
|
||||
|
||||
g.setColor(Color.CYAN);
|
||||
g.drawOval(_startPosX + 25, _startPosY + 35, 16, 24);
|
||||
g.drawOval(_startPosX + 67, _startPosY + 35, 16, 24);
|
||||
g.drawOval(_startPosX + 109, _startPosY + 35, 16, 24);
|
||||
break;
|
||||
case Five:
|
||||
g.setColor(BodyColor);
|
||||
g.drawArc(_startPosX + 2, _startPosY + 50, 16, 60, 0, 180);
|
||||
g.drawArc(_startPosX + 48, _startPosY + 50, 16, 60, 0, 180);
|
||||
g.drawArc(_startPosX + 67, _startPosY + 50, 16, 60, 0, 180);
|
||||
g.drawArc(_startPosX + 86, _startPosY + 50, 16, 60, 0, 180);
|
||||
g.drawArc(_startPosX + 132, _startPosY + 50, 16, 60, 0, 180);
|
||||
|
||||
g.setColor(Color.CYAN);
|
||||
g.drawOval(_startPosX + 25, _startPosY + 35, 16, 24);
|
||||
g.drawOval(_startPosX + 109, _startPosY + 35, 16, 24);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
83
Trolleybus/DrawingTriangleDoors.java
Normal file
83
Trolleybus/DrawingTriangleDoors.java
Normal file
@ -0,0 +1,83 @@
|
||||
package Trolleybus;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawingTriangleDoors implements IDrawingDoors{
|
||||
private CntOfDoors _cntOfDoors;
|
||||
public void SetCntOfDoors(int cnt) {
|
||||
if (cnt <= 3) {
|
||||
_cntOfDoors = CntOfDoors.Three;
|
||||
}
|
||||
if (cnt == 4) {
|
||||
_cntOfDoors = CntOfDoors.Four;
|
||||
}
|
||||
if (cnt >= 5) {
|
||||
_cntOfDoors = CntOfDoors.Five;
|
||||
}
|
||||
}
|
||||
public void DrawDoors(Graphics2D g, Color BodyColor, int _startPosX, int _startPosY) {
|
||||
switch (_cntOfDoors)
|
||||
{
|
||||
case Three:
|
||||
g.setColor(BodyColor);
|
||||
//Первая дверь
|
||||
g.drawLine(_startPosX + 2, _startPosY + 80, _startPosX + 10, _startPosY + 50);
|
||||
g.drawLine(_startPosX + 10, _startPosY + 50, _startPosX + 18, _startPosY + 80);
|
||||
//Вторая дверь
|
||||
g.drawLine(_startPosX + 67, _startPosY + 80, _startPosX + 75, _startPosY + 50);
|
||||
g.drawLine(_startPosX + 75, _startPosY + 50, _startPosX + 83, _startPosY + 80);
|
||||
//Третья дверь
|
||||
g.drawLine(_startPosX + 132, _startPosY + 80, _startPosX + 140, _startPosY + 50);
|
||||
g.drawLine(_startPosX + 140, _startPosY + 50, _startPosX + 148, _startPosY + 80);
|
||||
|
||||
g.setColor(Color.CYAN);
|
||||
g.drawOval(_startPosX + 22, _startPosY + 35, 16, 24);
|
||||
g.drawOval(_startPosX + 47, _startPosY + 35, 16, 24);
|
||||
g.drawOval(_startPosX + 87, _startPosY + 35, 16, 24);
|
||||
g.drawOval(_startPosX + 112, _startPosY + 35, 16, 24);
|
||||
break;
|
||||
case Four:
|
||||
g.setColor(BodyColor);
|
||||
//Первая дверь
|
||||
g.drawLine(_startPosX + 2, _startPosY + 80, _startPosX + 10, _startPosY + 50);
|
||||
g.drawLine(_startPosX + 10, _startPosY + 50, _startPosX + 18, _startPosY + 80);
|
||||
//Вторая дверь
|
||||
g.drawLine(_startPosX + 48, _startPosY + 80, _startPosX + 56, _startPosY + 50);
|
||||
g.drawLine(_startPosX + 56, _startPosY + 50, _startPosX + 64, _startPosY + 80);
|
||||
//Третья дверь
|
||||
g.drawLine(_startPosX + 86, _startPosY + 80, _startPosX + 94, _startPosY + 50);
|
||||
g.drawLine(_startPosX + 94, _startPosY + 50, _startPosX + 102, _startPosY + 80);
|
||||
//Четвёртая дверь
|
||||
g.drawLine(_startPosX + 132, _startPosY + 80, _startPosX + 140, _startPosY + 50);
|
||||
g.drawLine(_startPosX + 140, _startPosY + 50, _startPosX + 148, _startPosY + 80);
|
||||
|
||||
g.setColor(Color.CYAN);
|
||||
g.drawOval(_startPosX + 25, _startPosY + 35, 16, 24);
|
||||
g.drawOval(_startPosX + 67, _startPosY + 35, 16, 24);
|
||||
g.drawOval(_startPosX + 109, _startPosY + 35, 16, 24);
|
||||
break;
|
||||
case Five:
|
||||
g.setColor(BodyColor);
|
||||
//Первая дверь
|
||||
g.drawLine(_startPosX + 2, _startPosY + 80, _startPosX + 10, _startPosY + 50);
|
||||
g.drawLine(_startPosX + 10, _startPosY + 50, _startPosX + 18, _startPosY + 80);
|
||||
//Вторая дверь
|
||||
g.drawLine(_startPosX + 48, _startPosY + 80, _startPosX + 56, _startPosY + 50);
|
||||
g.drawLine(_startPosX + 56, _startPosY + 50, _startPosX + 64, _startPosY + 80);
|
||||
//Третья дверь
|
||||
g.drawLine(_startPosX + 67, _startPosY + 80, _startPosX + 75, _startPosY + 50);
|
||||
g.drawLine(_startPosX + 75, _startPosY + 50, _startPosX + 83, _startPosY + 80);
|
||||
//Четвёртая дверь
|
||||
g.drawLine(_startPosX + 86, _startPosY + 80, _startPosX + 94, _startPosY + 50);
|
||||
g.drawLine(_startPosX + 94, _startPosY + 50, _startPosX + 102, _startPosY + 80);
|
||||
//Пятая дверь
|
||||
g.drawLine(_startPosX + 132, _startPosY + 80, _startPosX + 140, _startPosY + 50);
|
||||
g.drawLine(_startPosX + 140, _startPosY + 50, _startPosX + 148, _startPosY + 80);
|
||||
|
||||
g.setColor(Color.CYAN);
|
||||
g.drawOval(_startPosX + 25, _startPosY + 35, 16, 24);
|
||||
g.drawOval(_startPosX + 109, _startPosY + 35, 16, 24);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
40
Trolleybus/DrawingTrolleybus.java
Normal file
40
Trolleybus/DrawingTrolleybus.java
Normal file
@ -0,0 +1,40 @@
|
||||
package Trolleybus;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawingTrolleybus extends DrawingBus{
|
||||
// Конструктор
|
||||
public DrawingTrolleybus(int speed, double weight, Color bodyColor, Color additionalColor, boolean horns, boolean batteries, int width, int height)
|
||||
{
|
||||
super(speed, weight, bodyColor, width, height, 150, 95);
|
||||
if (EntityBus != null)
|
||||
{
|
||||
EntityBus = new EntityTrolleybus(speed, weight, bodyColor, additionalColor, horns, batteries);
|
||||
}
|
||||
}
|
||||
// Прорисовка объекта
|
||||
@Override
|
||||
public void DrawTransport(Graphics g)
|
||||
{
|
||||
if (!(EntityBus instanceof EntityTrolleybus trolleybus))
|
||||
{
|
||||
return;
|
||||
}
|
||||
super.DrawTransport(g);
|
||||
Graphics2D g2d = (Graphics2D)g;
|
||||
//Опциональные "рога"
|
||||
g2d.setColor(trolleybus.getAdditionalColor());
|
||||
if (trolleybus.Horns)
|
||||
{
|
||||
g2d.drawLine(_startPosX + 70, _startPosY + 30, _startPosX + 40, _startPosY);
|
||||
g2d.drawLine(_startPosX + 70, _startPosY + 30, _startPosX + 60, _startPosY);
|
||||
}
|
||||
//Опциональный отсек для батареи
|
||||
if (trolleybus.Batteries)
|
||||
{
|
||||
int[] xOfBatteries = {_startPosX + 70, _startPosX + 70, _startPosX + 100, _startPosX + 110};
|
||||
int[] yOfBatteries = {_startPosY + 30, _startPosY + 25, _startPosY + 25, _startPosY + 30};
|
||||
g2d.fillPolygon(xOfBatteries, yOfBatteries, 4);
|
||||
}
|
||||
}
|
||||
}
|
29
Trolleybus/EntityBus.java
Normal file
29
Trolleybus/EntityBus.java
Normal file
@ -0,0 +1,29 @@
|
||||
package Trolleybus;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class EntityBus {
|
||||
public int Speed;
|
||||
public double Weight;
|
||||
public Color BodyColor;
|
||||
public double Step;
|
||||
//геттеры
|
||||
public int getSpeed() {
|
||||
return Speed;
|
||||
}
|
||||
public double getWeight() {
|
||||
return Weight;
|
||||
}
|
||||
public Color getBodyColor() {
|
||||
return BodyColor;
|
||||
}
|
||||
public double getStep() {
|
||||
return Step;
|
||||
}
|
||||
public EntityBus(int speed, double weight, Color bodyColor) {
|
||||
Speed = speed;
|
||||
Weight = weight;
|
||||
Step = (double) Speed * 100 / Weight;
|
||||
BodyColor = bodyColor;
|
||||
}
|
||||
}
|
29
Trolleybus/EntityTrolleybus.java
Normal file
29
Trolleybus/EntityTrolleybus.java
Normal file
@ -0,0 +1,29 @@
|
||||
package Trolleybus;
|
||||
|
||||
import java.awt.*;
|
||||
public class EntityTrolleybus extends EntityBus{
|
||||
// Дополнительный цвет (для опциональных элементов)
|
||||
public Color AdditionalColor;
|
||||
// Замена свойства
|
||||
public Color getAdditionalColor() {return AdditionalColor;}
|
||||
private void setAdditionalColor(Color additionalColor) {AdditionalColor = additionalColor;}
|
||||
|
||||
// Признак (опция) наличия "рогов"
|
||||
public boolean Horns;
|
||||
// Замена свойства
|
||||
public boolean getHorns() {return Horns;}
|
||||
private void setHorns(boolean horns) {Horns = horns;}
|
||||
|
||||
// Признак (опция) наличия отсека под электрические батареи
|
||||
public boolean Batteries;
|
||||
// Замена свойства
|
||||
public boolean getBatteries() {return Batteries;}
|
||||
private void setBatteries(boolean batteries) {Batteries = batteries;}
|
||||
|
||||
public EntityTrolleybus(int speed, double weight, Color bodyColor, Color additionalColor, boolean horns ,boolean batteries) {
|
||||
super(speed, weight, bodyColor);
|
||||
AdditionalColor = additionalColor;
|
||||
Horns = horns;
|
||||
Batteries = batteries;
|
||||
}
|
||||
}
|
125
Trolleybus/FormBusesCollection.java
Normal file
125
Trolleybus/FormBusesCollection.java
Normal file
@ -0,0 +1,125 @@
|
||||
package Trolleybus;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.*;
|
||||
|
||||
public class FormBusesCollection {
|
||||
private final BusesGenericCollection<DrawingBus, DrawingObjectBus> _buses;
|
||||
private JFrame frameBusesCollection;
|
||||
private JPanel panelBusesCollection, panelTools;
|
||||
private JButton buttonAddBus, buttonRemoveBus, buttonRefreshCollection;
|
||||
private JTextField positionTextField; //поле для ввода номера позиции
|
||||
public FormBusesCollection() {
|
||||
InitializeComponent();
|
||||
_buses = new BusesGenericCollection<>(panelBusesCollection.getWidth(), panelBusesCollection.getHeight());
|
||||
}
|
||||
|
||||
private void InitializeComponent() {
|
||||
//Само окно
|
||||
frameBusesCollection = new JFrame("Набор автобусов");
|
||||
frameBusesCollection.setLayout(new BorderLayout());
|
||||
frameBusesCollection.setSize(900, 600);
|
||||
frameBusesCollection.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
|
||||
//Панель, на которой отрисовывается набор автобусов/троллейбусов
|
||||
panelBusesCollection = new JPanel();
|
||||
|
||||
//Панель, с помощью которой изменяется набор
|
||||
panelTools = new JPanel();
|
||||
panelTools.setBorder(BorderFactory.createLineBorder(Color.BLACK));
|
||||
panelTools.setLayout(null);
|
||||
panelTools.setPreferredSize(new Dimension(170, 600));
|
||||
|
||||
//Кнопки панели panelTools
|
||||
buttonAddBus = new JButton("Добавить автобус");
|
||||
buttonAddBus.setBounds(10, 10, 150, 40);
|
||||
|
||||
buttonRemoveBus = new JButton("Удалить автобус");
|
||||
buttonRemoveBus.setBounds(10, 100, 150, 40);
|
||||
|
||||
buttonRefreshCollection = new JButton("Обновить коллекцию");
|
||||
buttonRefreshCollection.setBounds(10, 150, 150, 40);
|
||||
|
||||
//Добавление листенеров к кнопкам
|
||||
buttonAddBus.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
buttonAddBus_Click(e);
|
||||
}
|
||||
});
|
||||
buttonRemoveBus.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
buttonRemoveBus_Click(e);
|
||||
}
|
||||
});
|
||||
|
||||
buttonRefreshCollection.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
buttonRefreshCollection_Click(e);
|
||||
}
|
||||
});
|
||||
|
||||
//Поле ввода
|
||||
positionTextField = new JTextField();
|
||||
positionTextField.setBounds(10, 60, 150, 30);
|
||||
|
||||
//Добавление кнопок на панель panelTools
|
||||
panelTools.add(buttonAddBus);
|
||||
panelTools.add(positionTextField);
|
||||
panelTools.add(buttonRemoveBus);
|
||||
panelTools.add(buttonRefreshCollection);
|
||||
|
||||
frameBusesCollection.add(panelBusesCollection, BorderLayout.CENTER);
|
||||
frameBusesCollection.add(panelTools, BorderLayout.EAST);
|
||||
frameBusesCollection.setVisible(true);
|
||||
}
|
||||
|
||||
private void buttonAddBus_Click(ActionEvent e) {
|
||||
FormTrolleybus form = new FormTrolleybus();
|
||||
//Листенер для кнопки выбора автобуса на той форме
|
||||
form.buttonSelect.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
DrawingBus SelectedBus = form.getBus();
|
||||
if (_buses.Add(SelectedBus) != -1)
|
||||
{
|
||||
JOptionPane.showMessageDialog(null, "Объект добавлен");
|
||||
_buses.ShowBuses(panelBusesCollection);
|
||||
}
|
||||
else
|
||||
{
|
||||
JOptionPane.showMessageDialog(null, "Не удалось добавить объект");
|
||||
}
|
||||
form.getFrame().dispose();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void buttonRemoveBus_Click(ActionEvent e) {
|
||||
String pos_string = positionTextField.getText();
|
||||
//Если строка не заполнена (имеет то же значение, что и пустая строка), то считаем, что ввели 0
|
||||
if (pos_string.compareTo(" ") == 0) {
|
||||
pos_string = "0";
|
||||
}
|
||||
int pos;
|
||||
//Могли ввести не цифры
|
||||
try {
|
||||
pos = Integer.parseInt(pos_string);
|
||||
}
|
||||
//Если ввели не цифры, то тоже считаем, что ввели 0
|
||||
catch(Exception ex) {
|
||||
pos = 0;
|
||||
}
|
||||
|
||||
if (_buses.Remove(pos)) {
|
||||
JOptionPane.showMessageDialog(null, "Объект удален");
|
||||
_buses.ShowBuses(panelBusesCollection);
|
||||
}
|
||||
else {
|
||||
JOptionPane.showMessageDialog(null, "Не удалось удалить объект");
|
||||
}
|
||||
}
|
||||
|
||||
private void buttonRefreshCollection_Click(ActionEvent e) {
|
||||
_buses.ShowBuses(panelBusesCollection);
|
||||
}
|
||||
}
|
91
Trolleybus/FormDifficult.java
Normal file
91
Trolleybus/FormDifficult.java
Normal file
@ -0,0 +1,91 @@
|
||||
package Trolleybus;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.util.Random;
|
||||
|
||||
public class FormDifficult{
|
||||
private JFrame frameMain;
|
||||
private JPanel panelMain;
|
||||
private JButton buttonCreate;
|
||||
private GenericDifficult<EntityBus, IDrawingDoors> generator;
|
||||
|
||||
public FormDifficult() {
|
||||
InitializeComponent();
|
||||
Random rand = new Random();
|
||||
// макс. кол-во вариаций форм дверей/видов автобуса
|
||||
int maxCnt = rand.nextInt(5, 11);
|
||||
generator = new GenericDifficult<>(maxCnt, maxCnt, panelMain.getWidth(), panelMain.getHeight());
|
||||
// добавление в массивы с дверьми/сущностями рандомные варианты
|
||||
for (int i = 0; i < maxCnt; i++) {
|
||||
generator.Add(createRandomEntityBus());
|
||||
generator.Add(createRandomDoors());
|
||||
}
|
||||
}
|
||||
|
||||
private void InitializeComponent() {
|
||||
//Само окно
|
||||
frameMain = new JFrame("Усложнённая лаб 3");
|
||||
frameMain.setSize(900, 500);
|
||||
frameMain.setLayout(new BorderLayout());
|
||||
frameMain.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
|
||||
//Панель, на которой отрисовывается
|
||||
panelMain = new JPanel();
|
||||
panelMain.setLayout(null);
|
||||
|
||||
buttonCreate = new JButton("Создать");
|
||||
buttonCreate.setBounds(10,400,150,30);
|
||||
|
||||
buttonCreate.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
createRandomDrawingBus(e);
|
||||
}
|
||||
});
|
||||
|
||||
frameMain.add(panelMain, BorderLayout.CENTER);
|
||||
panelMain.add(buttonCreate);
|
||||
frameMain.setVisible(true);
|
||||
}
|
||||
|
||||
private void Draw(DrawingBus drawingBus){
|
||||
if (drawingBus == null) {
|
||||
return;
|
||||
}
|
||||
Graphics g = panelMain.getGraphics();
|
||||
// Очистка перед перерисовкой
|
||||
panelMain.paint(g);
|
||||
drawingBus.DrawTransport(g);
|
||||
}
|
||||
|
||||
private void createRandomDrawingBus(ActionEvent e) {
|
||||
DrawingBus drawingBus = generator.CreateObject();
|
||||
drawingBus.SetPosition(50, 50);
|
||||
Draw(drawingBus);
|
||||
}
|
||||
|
||||
private EntityBus createRandomEntityBus() {
|
||||
Random rand = new Random();
|
||||
Color color = new Color(rand.nextInt(0, 256), rand.nextInt(0, 256), rand.nextInt(0, 256));
|
||||
EntityBus entityBus = new EntityBus(rand.nextInt(100, 300), rand.nextDouble(1000, 3000), color);
|
||||
return entityBus;
|
||||
}
|
||||
private IDrawingDoors createRandomDoors() {
|
||||
IDrawingDoors doors;
|
||||
Random rand = new Random();
|
||||
int shape = rand.nextInt(1, 4);
|
||||
if (shape == 1) {
|
||||
doors = new DrawingDoors();
|
||||
}
|
||||
else if (shape == 2) {
|
||||
doors = new DrawingOvalDoors();
|
||||
}
|
||||
else {
|
||||
doors = new DrawingTriangleDoors();
|
||||
}
|
||||
doors.SetCntOfDoors(rand.nextInt(3, 6));
|
||||
return doors;
|
||||
}
|
||||
}
|
263
Trolleybus/FormTrolleybus.java
Normal file
263
Trolleybus/FormTrolleybus.java
Normal file
@ -0,0 +1,263 @@
|
||||
package Trolleybus;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.util.Random;
|
||||
|
||||
public class FormTrolleybus{
|
||||
private DrawingBus _drawingBus;
|
||||
private AbstractStrategy _abstractStrategy;
|
||||
private JFrame frameTrolleybus;
|
||||
private JPanel panelTrolleybus;
|
||||
private JButton buttonCreate, buttonCreateTrolleybus, buttonUp, buttonDown, buttonRight, buttonLeft, buttonStep;
|
||||
public JButton buttonSelect;
|
||||
private JComboBox comboBoxStrategy;
|
||||
|
||||
public FormTrolleybus(){
|
||||
//Само окно
|
||||
frameTrolleybus = new JFrame();
|
||||
frameTrolleybus.setLayout(new BorderLayout());
|
||||
frameTrolleybus.setSize(900, 500);
|
||||
frameTrolleybus.setTitle("Троллейбус");
|
||||
frameTrolleybus.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
|
||||
//Панель, на которую добавляю всё
|
||||
panelTrolleybus = new JPanel();
|
||||
panelTrolleybus.setLayout(null);
|
||||
|
||||
//ComboBox
|
||||
String[] strings = {"В центр", "В край"};
|
||||
comboBoxStrategy = new JComboBox(strings);
|
||||
comboBoxStrategy.setBounds(750, 20, 90, 30);
|
||||
|
||||
//Кнопка создания автобуса
|
||||
buttonCreate = new JButton("Создать автобус");
|
||||
buttonCreate.setToolTipText("buttonCreate");
|
||||
//Кнопка создания троллейбуса
|
||||
buttonCreateTrolleybus = new JButton("Создать троллейбус");
|
||||
buttonCreateTrolleybus.setToolTipText("buttonCreateTrolleybus");
|
||||
//Кнопка вверх
|
||||
buttonUp = new JButton();
|
||||
buttonUp.setIcon(new ImageIcon("Up.png"));
|
||||
buttonUp.setToolTipText("buttonUp");
|
||||
//Кнопка вниз
|
||||
buttonDown = new JButton();
|
||||
buttonDown.setIcon(new ImageIcon("Down.png"));
|
||||
buttonDown.setToolTipText("buttonDown");
|
||||
//Кнопка вправо
|
||||
buttonRight = new JButton();
|
||||
buttonRight.setIcon(new ImageIcon("Right.png"));
|
||||
buttonRight.setToolTipText("buttonRight");
|
||||
//Кнопка влево
|
||||
buttonLeft = new JButton();
|
||||
buttonLeft.setIcon(new ImageIcon("Left.png"));
|
||||
buttonLeft.setToolTipText("buttonLeft");
|
||||
//Кнопка шага
|
||||
buttonStep = new JButton("Шаг");
|
||||
//Кнопка выбора созданного автобуса/троллейбуса
|
||||
buttonSelect = new JButton("Выбрать");
|
||||
|
||||
//Размеры, позиция кнопок
|
||||
buttonCreate.setBounds(10,400,150,30);
|
||||
buttonCreateTrolleybus.setBounds(170, 400, 150, 30);
|
||||
buttonUp.setBounds(800,380,30,30);
|
||||
buttonDown.setBounds(800,420,30,30);
|
||||
buttonLeft.setBounds(760,420,30,30);
|
||||
buttonRight.setBounds(840,420,30,30);
|
||||
buttonStep.setBounds(750, 70, 90, 30);
|
||||
buttonSelect.setBounds(350, 400, 90, 30);
|
||||
|
||||
//Добавление листенеров к кнопкам
|
||||
buttonCreate.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
ButtonCreate_Click(e);
|
||||
}
|
||||
});
|
||||
|
||||
buttonCreateTrolleybus.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
ButtonCreate_Click(e);
|
||||
}
|
||||
});
|
||||
|
||||
buttonUp.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
ButtonMove_Click(buttonUp, e);
|
||||
}
|
||||
});
|
||||
|
||||
buttonDown.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
ButtonMove_Click(buttonDown, e);
|
||||
}
|
||||
});
|
||||
|
||||
buttonRight.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
ButtonMove_Click(buttonRight, e);
|
||||
}
|
||||
});
|
||||
|
||||
buttonLeft.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
ButtonMove_Click(buttonLeft, e);
|
||||
}
|
||||
});
|
||||
|
||||
buttonStep.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
ButtonStep_Click(buttonStep, e);
|
||||
}
|
||||
});
|
||||
|
||||
//Листенер для кнопки выбора автобуса создаётся при вызове этой формы в новой форме
|
||||
|
||||
panelTrolleybus.add(buttonCreate);
|
||||
panelTrolleybus.add(buttonCreateTrolleybus);
|
||||
panelTrolleybus.add(buttonUp);
|
||||
panelTrolleybus.add(buttonDown);
|
||||
panelTrolleybus.add(buttonLeft);
|
||||
panelTrolleybus.add(buttonRight);
|
||||
panelTrolleybus.add(buttonStep);
|
||||
panelTrolleybus.add(comboBoxStrategy);
|
||||
panelTrolleybus.add(buttonSelect);
|
||||
frameTrolleybus.add(panelTrolleybus, BorderLayout.CENTER);
|
||||
|
||||
frameTrolleybus.setVisible(true);
|
||||
}
|
||||
// Метод прорисовки троллейбуса
|
||||
private void Draw(){
|
||||
if (_drawingBus == null) {
|
||||
return;
|
||||
}
|
||||
Graphics g = panelTrolleybus.getGraphics();
|
||||
// Очистка перед перерисовкой
|
||||
panelTrolleybus.paint(g);
|
||||
_drawingBus.DrawTransport(g);
|
||||
}
|
||||
private void ButtonCreate_Click(ActionEvent e) {
|
||||
Random random = new Random();
|
||||
JButton info = (JButton)e.getSource();
|
||||
String name = info.getToolTipText();
|
||||
Random rand = new Random();
|
||||
//Для диалогов выбора цвета
|
||||
Color color;
|
||||
JColorChooser colorChooser;
|
||||
int result;
|
||||
switch (name) {
|
||||
case "buttonCreate":
|
||||
color = new Color(rand.nextInt(0, 256), rand.nextInt(0, 256), rand.nextInt(0, 256));
|
||||
// Диалог выбора цвета
|
||||
colorChooser = new JColorChooser(color);
|
||||
result = JOptionPane.showConfirmDialog(null, colorChooser, "Выберите цвет", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
|
||||
if (result == JOptionPane.OK_OPTION) {
|
||||
color = colorChooser.getColor();
|
||||
}
|
||||
_drawingBus = new DrawingBus(random.nextInt(100, 300),
|
||||
random.nextInt(1000, 3000),
|
||||
color,
|
||||
panelTrolleybus.getWidth(), panelTrolleybus.getHeight());
|
||||
break;
|
||||
case "buttonCreateTrolleybus":
|
||||
color = new Color(rand.nextInt(0, 256), rand.nextInt(0, 256), rand.nextInt(0, 256));
|
||||
Color additionalColor = new Color(rand.nextInt(0, 256), rand.nextInt(0, 256), rand.nextInt(0, 256));
|
||||
|
||||
// Диалог выбора основного цвета
|
||||
colorChooser = new JColorChooser(color);
|
||||
result = JOptionPane.showConfirmDialog(null, colorChooser, "Выберите основной цвет", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
|
||||
if (result == JOptionPane.OK_OPTION) {
|
||||
color = colorChooser.getColor();
|
||||
}
|
||||
|
||||
// Диалог выбора дополнительного цвета
|
||||
colorChooser = new JColorChooser(additionalColor);
|
||||
result = JOptionPane.showConfirmDialog(null, colorChooser, "Выберите дополнительный цвет", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
|
||||
if (result == JOptionPane.OK_OPTION) {
|
||||
additionalColor = colorChooser.getColor();
|
||||
}
|
||||
|
||||
_drawingBus = new DrawingTrolleybus(random.nextInt(100, 300),
|
||||
random.nextInt(1000, 3000),
|
||||
color,
|
||||
additionalColor,
|
||||
random.nextBoolean(),
|
||||
random.nextBoolean(),
|
||||
panelTrolleybus.getWidth(), panelTrolleybus.getHeight());
|
||||
break;
|
||||
}
|
||||
_drawingBus.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100));
|
||||
Draw();
|
||||
}
|
||||
protected void ButtonMove_Click(Object sender, ActionEvent e) {
|
||||
if (_drawingBus == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
JButton info = (JButton)e.getSource();
|
||||
String name = info.getToolTipText();
|
||||
switch (name)
|
||||
{
|
||||
case "buttonUp":
|
||||
_drawingBus.MoveTransport(DirectionType.Up);
|
||||
break;
|
||||
case "buttonDown":
|
||||
_drawingBus.MoveTransport(DirectionType.Down);
|
||||
break;
|
||||
case "buttonLeft":
|
||||
_drawingBus.MoveTransport(DirectionType.Left);
|
||||
break;
|
||||
case "buttonRight":
|
||||
_drawingBus.MoveTransport(DirectionType.Right);
|
||||
break;
|
||||
}
|
||||
Draw();
|
||||
}
|
||||
private void ButtonStep_Click(Object sender, ActionEvent e)
|
||||
{
|
||||
if (_drawingBus == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (comboBoxStrategy.isEnabled())
|
||||
{
|
||||
|
||||
if (comboBoxStrategy.getSelectedIndex() == 0) {
|
||||
_abstractStrategy = new MoveToCenter();
|
||||
}
|
||||
else if (comboBoxStrategy.getSelectedIndex() == 1) {
|
||||
_abstractStrategy = new MoveToBorder();
|
||||
}
|
||||
else {
|
||||
_abstractStrategy = null;
|
||||
}
|
||||
|
||||
if (_abstractStrategy == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
_abstractStrategy.SetData(new DrawingObjectBus(_drawingBus), panelTrolleybus.getWidth(), panelTrolleybus.getHeight());
|
||||
comboBoxStrategy.setEnabled(false);
|
||||
}
|
||||
if (_abstractStrategy == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
_abstractStrategy.MakeStep();
|
||||
Draw();
|
||||
if (_abstractStrategy.GetStatus() == Status.Finish)
|
||||
{
|
||||
comboBoxStrategy.setEnabled(true);
|
||||
_abstractStrategy = null;
|
||||
}
|
||||
}
|
||||
|
||||
public DrawingBus getBus() {
|
||||
return _drawingBus;
|
||||
}
|
||||
public Frame getFrame() {
|
||||
return frameTrolleybus;
|
||||
}
|
||||
}
|
57
Trolleybus/GenericDifficult.java
Normal file
57
Trolleybus/GenericDifficult.java
Normal file
@ -0,0 +1,57 @@
|
||||
package Trolleybus;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Random;
|
||||
|
||||
public class GenericDifficult <T extends EntityBus, U extends IDrawingDoors> {
|
||||
private ArrayList<T> Buses;
|
||||
private ArrayList<U> Doors;
|
||||
private int CntOfBuses;
|
||||
private int MaxCntOfBuses;
|
||||
private int CntOfDoors;
|
||||
private int MaxCntOfDoors;
|
||||
private int WidthOfPanel;
|
||||
private int HeightOfPanel;
|
||||
|
||||
public GenericDifficult(int maxCountOfBuses, int maxCountOfDoors, int width, int height) {
|
||||
MaxCntOfBuses = maxCountOfBuses;
|
||||
MaxCntOfDoors = maxCountOfDoors;
|
||||
Buses = new ArrayList<T>(MaxCntOfBuses);
|
||||
Doors = new ArrayList<U>(MaxCntOfDoors);
|
||||
CntOfBuses = 0;
|
||||
CntOfDoors = 0;
|
||||
WidthOfPanel = width;
|
||||
HeightOfPanel = height;
|
||||
}
|
||||
|
||||
//Полиморфные методы добавления в массив
|
||||
public boolean Add(T bus) {
|
||||
if (bus == null || CntOfBuses >= MaxCntOfBuses) {
|
||||
return false;
|
||||
}
|
||||
Buses.add(CntOfBuses++, bus);
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean Add(U door) {
|
||||
if (door == null || CntOfDoors >= MaxCntOfDoors) {
|
||||
return false;
|
||||
}
|
||||
Doors.add(CntOfDoors++, door);
|
||||
return true;
|
||||
}
|
||||
|
||||
public DrawingBus CreateObject() {
|
||||
if (CntOfBuses == 0 || CntOfDoors == 0) {
|
||||
return null;
|
||||
}
|
||||
// В массивах с дверьми и самими автобусами берём по 1 случайному элементу (необязательно с одинаковым индексом),
|
||||
// чтобы получить рандомное сочетание дверей и самого корпуса
|
||||
Random rand = new Random();
|
||||
int indexOfEntityBus = rand.nextInt(0, CntOfBuses);
|
||||
int indexOfDoor = rand.nextInt(0, CntOfDoors);
|
||||
T bus = Buses.get(indexOfEntityBus);
|
||||
U doors = Doors.get(indexOfDoor);
|
||||
return new DrawingBus(bus, doors, WidthOfPanel, HeightOfPanel);
|
||||
}
|
||||
}
|
8
Trolleybus/IDrawingDoors.java
Normal file
8
Trolleybus/IDrawingDoors.java
Normal file
@ -0,0 +1,8 @@
|
||||
package Trolleybus;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public interface IDrawingDoors {
|
||||
public void SetCntOfDoors(int cnt);
|
||||
public void DrawDoors(Graphics2D g, Color BodyColor, int _startPosX, int _startPosY);
|
||||
}
|
13
Trolleybus/IMoveableObject.java
Normal file
13
Trolleybus/IMoveableObject.java
Normal file
@ -0,0 +1,13 @@
|
||||
package Trolleybus;
|
||||
|
||||
// Интерфейс для работы с перемещаемым объектом
|
||||
public interface IMoveableObject {
|
||||
// Получение координаты X объекта
|
||||
public ObjectParameters GetObjectPosition();
|
||||
// Шаг объекта
|
||||
public int GetStep();
|
||||
// Проверка, можно ли переместиться по нужному направлению
|
||||
boolean CheckCanMove(DirectionType direction);
|
||||
// Изменение направления пермещения объекта
|
||||
void MoveObject(DirectionType direction);
|
||||
}
|
7
Trolleybus/Main.java
Normal file
7
Trolleybus/Main.java
Normal file
@ -0,0 +1,7 @@
|
||||
package Trolleybus;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
new FormBusesCollection();
|
||||
}
|
||||
}
|
50
Trolleybus/MoveToBorder.java
Normal file
50
Trolleybus/MoveToBorder.java
Normal file
@ -0,0 +1,50 @@
|
||||
package Trolleybus;
|
||||
|
||||
public class MoveToBorder extends AbstractStrategy{
|
||||
@Override
|
||||
protected boolean IsTargetDestination()
|
||||
{
|
||||
var objParams = GetObjectParameters();
|
||||
if (objParams == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return objParams.RightBorder() <= FieldWidth() &&
|
||||
objParams.RightBorder() + GetStep() >= FieldWidth() &&
|
||||
objParams.DownBorder() <= FieldHeight() &&
|
||||
objParams.DownBorder() + GetStep() >= FieldHeight();
|
||||
}
|
||||
@Override
|
||||
protected void MoveToTarget()
|
||||
{
|
||||
var objParams = GetObjectParameters();
|
||||
if (objParams == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
var diffX = objParams.RightBorder() - FieldWidth();
|
||||
if (Math.abs(diffX) >= GetStep())
|
||||
{
|
||||
if (diffX > 0)
|
||||
{
|
||||
MoveLeft();
|
||||
}
|
||||
else
|
||||
{
|
||||
MoveRight();
|
||||
}
|
||||
}
|
||||
var diffY = objParams.DownBorder() - FieldHeight();
|
||||
if (Math.abs(diffY) >= GetStep())
|
||||
{
|
||||
if (diffY > 0)
|
||||
{
|
||||
MoveUp();
|
||||
}
|
||||
else
|
||||
{
|
||||
MoveDown();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
50
Trolleybus/MoveToCenter.java
Normal file
50
Trolleybus/MoveToCenter.java
Normal file
@ -0,0 +1,50 @@
|
||||
package Trolleybus;
|
||||
|
||||
public class MoveToCenter extends AbstractStrategy{
|
||||
@Override
|
||||
protected boolean IsTargetDestination() {
|
||||
var objParams = GetObjectParameters();
|
||||
|
||||
if (objParams == null) {
|
||||
return false;
|
||||
}
|
||||
return objParams.ObjectMiddleHorizontal() <= FieldWidth() / 2 &&
|
||||
objParams.ObjectMiddleHorizontal() + GetStep() >= FieldWidth() / 2 &&
|
||||
objParams.ObjectMiddleVertical() <= FieldHeight() / 2 &&
|
||||
objParams.ObjectMiddleVertical() + GetStep() >= FieldHeight() / 2;
|
||||
}
|
||||
@Override
|
||||
protected void MoveToTarget()
|
||||
{
|
||||
var objParams = GetObjectParameters();
|
||||
if (objParams == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
var diffX = objParams.ObjectMiddleHorizontal() - FieldWidth() / 2;
|
||||
if (Math.abs(diffX) > GetStep())
|
||||
{
|
||||
if (diffX > 0)
|
||||
{
|
||||
MoveLeft();
|
||||
}
|
||||
else
|
||||
{
|
||||
MoveRight();
|
||||
}
|
||||
|
||||
}
|
||||
var diffY = objParams.ObjectMiddleVertical() - FieldHeight() / 2;
|
||||
if (Math.abs(diffY) > GetStep())
|
||||
{
|
||||
if (diffY > 0)
|
||||
{
|
||||
MoveUp();
|
||||
}
|
||||
else
|
||||
{
|
||||
MoveDown();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
46
Trolleybus/ObjectParameters.java
Normal file
46
Trolleybus/ObjectParameters.java
Normal file
@ -0,0 +1,46 @@
|
||||
package Trolleybus;
|
||||
|
||||
// Параметры-координаты объекта
|
||||
public class ObjectParameters {
|
||||
private final int _x;
|
||||
private final int _y;
|
||||
private final int _width;
|
||||
private final int _height;
|
||||
public int LeftBorder() {return _x;}
|
||||
public int TopBorder() {return _y;}
|
||||
public int RightBorder() {return _x + _width;}
|
||||
public int DownBorder() {return _y + _height;}
|
||||
public int ObjectMiddleHorizontal() { return _x + _width / 2;}
|
||||
public int ObjectMiddleVertical() {return _y + _height / 2;}
|
||||
public int getLeftBorder() {
|
||||
return _x;
|
||||
}
|
||||
|
||||
public int getTopBorder() {
|
||||
return _y;
|
||||
}
|
||||
|
||||
public int getRightBorder() {
|
||||
return _x + _width;
|
||||
}
|
||||
|
||||
public int getDownBorder() {
|
||||
return _y + _height;
|
||||
}
|
||||
|
||||
public int getObjectMiddleHorizontal() {
|
||||
return _x + _width / 2;
|
||||
}
|
||||
|
||||
public int getObjectMiddleVertical() {
|
||||
return _y + _height / 2;
|
||||
}
|
||||
// Конструктор
|
||||
public ObjectParameters(int x, int y, int width, int height)
|
||||
{
|
||||
_x = x;
|
||||
_y = y;
|
||||
_width = width;
|
||||
_height = height;
|
||||
}
|
||||
}
|
68
Trolleybus/SetGeneric.java
Normal file
68
Trolleybus/SetGeneric.java
Normal file
@ -0,0 +1,68 @@
|
||||
package Trolleybus;
|
||||
|
||||
public class SetGeneric <T extends Object>{
|
||||
private final Object[] _places;
|
||||
public int Count;
|
||||
public SetGeneric(int count){
|
||||
_places = new Object[count];
|
||||
Count = count;
|
||||
}
|
||||
//Вставка в начало
|
||||
public int Insert(T bus){
|
||||
return Insert(bus, 0);
|
||||
}
|
||||
//Вставка на какую-то позицию
|
||||
public int Insert(T bus, int position)
|
||||
{
|
||||
if (position >= Count || position < 0)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (_places[position] == null)
|
||||
{
|
||||
_places[position] = bus;
|
||||
}
|
||||
else
|
||||
{
|
||||
//проверка, что в массиве после вставляемого эл-а есть место
|
||||
int index = position;
|
||||
while (_places[index] != null)
|
||||
{
|
||||
index++;
|
||||
if (index >= Count)
|
||||
{
|
||||
//места в массиве нет, т.е. ни по какому индексу вставить нельзя
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
for (int i = index; i > position; i--)
|
||||
{
|
||||
_places[i] = _places[i - 1];
|
||||
|
||||
}
|
||||
//вставка по позиции
|
||||
_places[position] = bus;
|
||||
|
||||
}
|
||||
//индекс в массиве, по которому вставили, т.е. вставка прошла успешно
|
||||
return position;
|
||||
}
|
||||
public boolean Remove(int position)
|
||||
{
|
||||
if (position >= Count || position < 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
_places[position] = null;
|
||||
return true;
|
||||
}
|
||||
public T Get(int position)
|
||||
{
|
||||
if (position >= Count || position < 0)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return (T)_places[position];
|
||||
}
|
||||
}
|
7
Trolleybus/Status.java
Normal file
7
Trolleybus/Status.java
Normal file
@ -0,0 +1,7 @@
|
||||
package Trolleybus;
|
||||
|
||||
public enum Status {
|
||||
NotInit,
|
||||
InProgress,
|
||||
Finish
|
||||
}
|
Loading…
Reference in New Issue
Block a user