Compare commits

..

16 Commits
main ... Lab4

Author SHA1 Message Date
4efda8d15b Lab4 Done 2023-12-07 18:03:57 +04:00
4aedc22ade Lab4+ 2023-11-23 22:56:22 +04:00
a41aa83934 Lab4 2023-11-23 22:51:24 +04:00
68312fa3c3 Lab3 Done done+ 2023-11-18 02:36:55 +04:00
f7be8b6d55 Lab3 Done done 2023-11-18 02:26:55 +04:00
8cc4261ccb Lab3 Done+ 2023-11-10 00:21:29 +04:00
b02f4ac691 Lab2 Done+ 2023-11-10 00:20:05 +04:00
adf9a87f29 Lab2 Done+ 2023-11-09 16:23:16 +04:00
b76c220516 Lab2 Done 2023-11-04 02:38:48 +04:00
dc8923d850 Lab2+++ 2023-11-04 02:32:04 +04:00
43d4951e5d Lab2++ 2023-11-04 02:04:16 +04:00
f9487fc660 Lab2+ 2023-11-04 01:54:14 +04:00
e59a8e2cd2 Lab2 2023-11-04 01:46:55 +04:00
66bb3f0907 Done++ 2023-10-29 02:13:51 +04:00
2856775bab Done 2023-10-29 02:06:47 +04:00
a6fede83c9 Lab1 2023-10-29 02:01:32 +04:00
35 changed files with 1260 additions and 0 deletions

8
.idea/.gitignore vendored Normal file
View File

@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

View File

@ -0,0 +1,10 @@
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="DuplicatedCode" enabled="true" level="WEAK WARNING" enabled_by_default="true">
<Languages>
<language minSize="53" name="Java" />
</Languages>
</inspection_tool>
</profile>
</component>

5
.idea/misc.xml Normal file
View File

@ -0,0 +1,5 @@
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="openjdk-21" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

8
.idea/modules.xml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/PIbd-23_Yunusov.N.N_Trolleybus_Hard.iml" filepath="$PROJECT_DIR$/.idea/PIbd-23_Yunusov.N.N_Trolleybus_Hard.iml" />
</modules>
</component>
</project>

6
.idea/vcs.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

BIN
img/down.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 282 B

BIN
img/left.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 295 B

BIN
img/right.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 283 B

BIN
img/up.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 276 B

61
src/AbstractStrategy.java Normal file
View File

@ -0,0 +1,61 @@
public abstract class AbstractStrategy {
private IMoveableObject moveableObject;
private Status state = Status.NOT_INIT;
private int fieldWidth;
protected int getFieldWidth(){return fieldWidth;}
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)
{
state = Status.NOT_INIT;
return;
}
state = Status.IN_PROGRESS;
this.moveableObject = moveableObject;
fieldWidth = width;
fieldHeight = height;
}
public void makeStep(){
if (state != Status.IN_PROGRESS) {
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.IN_PROGRESS)
{
return null;
}
return moveableObject.getStep();
}
protected abstract void moveToTarget();
protected abstract boolean isTargetDestination();
private boolean moveTo(DirectionType directionType) {
if (state != Status.IN_PROGRESS)
{
return false;
}
if (moveableObject.checkCanMove(directionType))
{
moveableObject.moveObject(directionType);
return true;
}
return false;
}
}

View File

@ -0,0 +1,64 @@
import java.awt.*;
public class BusesGenericCollection<T extends DrawingBus, U extends IMoveableObject> {
private int _pictureWidth;
private int _pictureHeight;
private final int _placeSizeWidth = 210;
private final int _placeSizeHeight = 135;
private SetGeneric<T> _collection;
public BusesGenericCollection(int picWidth, int picHeight) {
int width = picWidth / _placeSizeWidth;
int height = picHeight / _placeSizeHeight;
_pictureWidth = picWidth;
_pictureHeight = picHeight;
_collection = new SetGeneric<T>(width * height);
}
public void showBuses(Graphics2D g){
drawBackground(g);
drawObjects(g);
}
public boolean insert(T obj)
{
if (obj != null)
return _collection.insert(obj);
return false;
}
public boolean remove(int pos)
{
return _collection.remove(pos);
}
public U GetU(int pos) {
if(_collection.Get(pos) == null)
return null;
return (U)_collection.Get(pos).GetMoveableObject();
}
public T Get(int position){
if(position < 0 || position >= _collection.getCount())
return null;
return _collection.Get(position);
}
private void drawBackground(Graphics2D g) {
BasicStroke pen = new BasicStroke(3);
g.setStroke(pen);
for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++) {
for (int j = 0; j < _pictureHeight / _placeSizeHeight +
1; ++j) { g.drawLine(i * _placeSizeWidth, j *
_placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j *
_placeSizeHeight);
}
g.drawLine(i * _placeSizeWidth, 0, i *
_placeSizeWidth, _pictureHeight / _placeSizeHeight * _placeSizeHeight);
}
}
private void drawObjects(Graphics2D g) {
for(int i =0;i <_collection.getCount(); i++)
{
DrawingBus bus =_collection.Get(i);
if (bus != null) {
int inRow = _pictureWidth / _placeSizeWidth;
bus.setPosition(_placeSizeWidth * (inRow - 1) - (i % inRow * _placeSizeWidth), i / inRow * _placeSizeHeight);
bus.drawTransport(g);
}
}
}
}

View File

@ -0,0 +1,41 @@
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;
public class BusesGenericStorage {
final HashMap<String, BusesGenericCollection<DrawingBus, DrawingObjectBus>> _busStorages;
public List<String> Keys(){
if(_busStorages == null)
return null;
return _busStorages.keySet().stream().collect(Collectors.toList());
}
private final int _pictureWidth;
private final int _pictureHeight;
public BusesGenericStorage(int pictureWidth, int pictureHeight){
_busStorages = new HashMap<>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
public void addSet(String name){
if(_busStorages.containsKey(name))
return;
_busStorages.put(name, new BusesGenericCollection<>(_pictureWidth, _pictureHeight));
}
public void delSet(String name){
if(!_busStorages.containsKey(name))
return;
_busStorages.remove(name);
}
public BusesGenericCollection<DrawingBus, DrawingObjectBus> getCollection(String name){
if(!_busStorages.containsKey(name))
return null;
return _busStorages.get(name);
}
public DrawingBus Get(String collectionName, int position){
return _busStorages.get(collectionName).Get(position);
}
}

6
src/DirectionType.java Normal file
View File

@ -0,0 +1,6 @@
public enum DirectionType {
UP,
DOWN,
LEFT,
RIGHT
}

5
src/DoorsNumber.java Normal file
View File

@ -0,0 +1,5 @@
public enum DoorsNumber {
THREE,
FOUR,
FIVE
}

138
src/DrawingBus.java Normal file
View File

@ -0,0 +1,138 @@
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);
}
public IMoveableObject GetMoveableObject() { return new DrawingObjectBus(this);}
}

35
src/DrawingDoors.java Normal file
View File

@ -0,0 +1,35 @@
import java.awt.*;
public class DrawingDoors implements IDrawDoors{
private DoorsNumber number;
public int getNumber(){
int x = 0;
if(number == DoorsNumber.THREE)
x = 3;
if(number == DoorsNumber.FOUR)
x = 4;
if(number == DoorsNumber.FIVE)
x = 5;
return x;
}
public int getType(){return 0;}
public void setNumber(int x){
if(x <= 3)
number = DoorsNumber.THREE;
if(x == 4)
number = DoorsNumber.FOUR;
if(x >= 5)
number = DoorsNumber.FIVE;
}
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);
graphics2D.fillRect(_startX+118, _startY+81, 25, 40);
if (number == DoorsNumber.FOUR || number == DoorsNumber.FIVE){
graphics2D.fillRect(_startX+151, _startY+81, 25, 40);
}
if (number == DoorsNumber.FIVE){
graphics2D.fillRect(_startX+19, _startY+81, 25, 40);
}
}
}

View File

@ -0,0 +1,40 @@
import java.awt.*;
public class DrawingDoorsRoundedUp implements IDrawDoors{
private DoorsNumber number;
public int getNumber(){
int x = 0;
if(number == DoorsNumber.THREE)
x = 3;
if(number == DoorsNumber.FOUR)
x = 4;
if(number == DoorsNumber.FIVE)
x = 5;
return x;
}
public int getType(){return 1;}
public void setNumber(int x){
if(x <= 2)
number = DoorsNumber.THREE;
if(x == 4)
number = DoorsNumber.FOUR;
if(x >= 6)
number = DoorsNumber.FIVE;
}
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);
}
}
}

View File

@ -0,0 +1,45 @@
import java.awt.*;
public class DrawingDoorsRoundedUpAndDown implements IDrawDoors{
private DoorsNumber number;
public int getNumber(){
int x = 0;
if(number == DoorsNumber.THREE)
x = 3;
if(number == DoorsNumber.FOUR)
x = 4;
if(number == DoorsNumber.FIVE)
x = 5;
return x;
}
public int getType(){return 2;}
public void setNumber(int x){
if(x <= 2)
number = DoorsNumber.THREE;
if(x == 4)
number = DoorsNumber.FOUR;
if(x >= 6)
number = DoorsNumber.FIVE;
}
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);
}
}
}

29
src/DrawingObjectBus.java Normal file
View File

@ -0,0 +1,29 @@
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);
}
}

View File

@ -0,0 +1,38 @@
import java.awt.*;
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)
{
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 (!(entityBus instanceof EntityTrolleybus))
return;
EntityTrolleybus entityTrolleybus = (EntityTrolleybus) entityBus;
BasicStroke pen = new BasicStroke(2);
graphics2D.setStroke(pen);
Color additionalColor = entityTrolleybus.getAdditionalColor();
super.drawTransport(graphics2D);
//рога
graphics2D.setPaint(Color.BLACK);
if (entityTrolleybus.getRoga()) {
graphics2D.setPaint(Color.BLACK);
graphics2D.drawLine(_startPosX + 186, _startPosY + 31, _startPosX + 86, _startPosY + 1);
graphics2D.drawLine(_startPosX + 86, _startPosY + 1, _startPosX + 126, _startPosY + 31);
graphics2D.drawLine(_startPosX + 146, _startPosY + 31, _startPosX + 46, _startPosY + 1);
graphics2D.drawLine(_startPosX + 46, _startPosY + 1, _startPosX + 86, _startPosY + 31);
}
//батарея
if (entityTrolleybus.getBattery()) {
graphics2D.setPaint(additionalColor);
graphics2D.fillRect(_startPosX + 176, _startPosY + 101, 15, 20);
graphics2D.setPaint(Color.YELLOW);
graphics2D.drawLine(_startPosX + 183, _startPosY + 103, _startPosX + 178, _startPosY + 111);
graphics2D.drawLine(_startPosX + 178, _startPosY + 111, _startPosX + 189, _startPosY + 111);
graphics2D.drawLine(_startPosX + 189, _startPosY + 111, _startPosX + 183, _startPosY + 119);
}
}
}

23
src/EntityBus.java Normal file
View File

@ -0,0 +1,23 @@
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;
}
}

21
src/EntityTrolleybus.java Normal file
View File

@ -0,0 +1,21 @@
import java.awt.*;
public class EntityTrolleybus extends EntityBus{
private Color additionalColor;
public Color getAdditionalColor(){return additionalColor;}
private boolean roga;
public boolean getRoga() {
return roga;
}
private boolean battery;
public boolean getBattery() {
return battery;
}
public EntityTrolleybus(int speed, double weight, Color bodyColor, Color
additionalColor, boolean roga, boolean battery) {
super(speed, weight, bodyColor);
this.additionalColor = additionalColor;
this.roga = roga;
this.battery = battery;
}
}

192
src/FrameBusCollection.java Normal file
View File

@ -0,0 +1,192 @@
import javax.swing.*;
import javax.swing.border.StrokeBorder;
import java.awt.*;
import java.io.IOException;
import java.util.List;
import java.util.Objects;
import java.util.ArrayDeque;
import java.util.Queue;
public class FrameBusCollection extends JFrame {
private BusesGenericStorage storage;
private JList<String> listBoxStorages;
private DefaultListModel<String> listBoxModel;
JComponent pictureBoxCollection;
TextField textFieldNumber;
TextField textFieldStorageName;
Queue <DrawingBus> queue = new ArrayDeque<>();
public FrameBusCollection(){
super("Набор автобусов");
setSize(new Dimension(1000,500));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
createGui();
storage = new BusesGenericStorage(pictureBoxCollection.getWidth(),pictureBoxCollection.getHeight());
setVisible(true);
}
private void createGui(){
pictureBoxCollection = new JComponent(){
public void paintComponent(Graphics graphics){
super.paintComponent(graphics);
Graphics2D graphics2D = (Graphics2D) graphics;
if (listBoxStorages == null || storage == null)
return;
var collection = storage.getCollection(listBoxStorages.getSelectedValue());
if (collection == null)
return;
collection.showBuses(graphics2D);
super.repaint();
}
};
pictureBoxCollection.setSize(new Dimension(700,450));
JButton buttonAddBus = new JButton("Добавить автобус");
textFieldNumber = new TextField();
textFieldStorageName = new TextField();
JScrollPane scrollPane = new JScrollPane();
JButton buttonRemoveBus = new JButton("Удалить автобус");
JButton buttonRefreshCollection = new JButton("Обновить коллекцию");
JButton buttonAddCollection = new JButton("Добавить набор");
listBoxModel = new DefaultListModel<>();
listBoxStorages= new JList<>(listBoxModel);
scrollPane.setViewportView(listBoxStorages);
JButton buttonDeleteCollection = new JButton("Удалить набор");
JButton buttonTrash = new JButton("Корзина");
buttonAddBus.addActionListener(e -> buttonAddBusClick());
buttonRemoveBus.addActionListener(e -> buttonRemoveBusClick());
buttonRefreshCollection.addActionListener(e -> buttonRefreshBusClick());
buttonAddCollection.addActionListener(e -> buttonAddCollectionClick());
buttonDeleteCollection.addActionListener(e -> buttonDeleteCollectionClick());
buttonTrash.addActionListener(e -> buttonTrashClick());
JPanel panelTools = new JPanel(new GridBagLayout());
panelTools.setBorder(new StrokeBorder(new BasicStroke(3)));
JPanel panelCollection = new JPanel(new GridBagLayout());
panelCollection.setBorder(new StrokeBorder(new BasicStroke(3)));
GridBagConstraints constraints = new GridBagConstraints();
constraints.insets.left = constraints.insets.right = 5;
constraints.insets.top = constraints.insets.bottom = 5;
constraints.fill = GridBagConstraints.BOTH;
constraints.gridx = 0;
constraints.gridy = 0;
panelCollection.add(textFieldStorageName, constraints);
constraints.gridx = 0;
constraints.gridy = 1;
panelCollection.add(buttonAddCollection, constraints);
constraints.gridx = 0;
constraints.gridy = 2;
panelCollection.add(scrollPane, constraints);
constraints.gridx = 0;
constraints.gridy = 3;
panelCollection.add(buttonDeleteCollection, constraints);
constraints.gridx = 0;
constraints.gridy = 0;
panelTools.add(panelCollection, constraints);
constraints.gridx = 0;
constraints.gridy = 1;
panelTools.add(buttonAddBus, constraints);
constraints.gridx = 0;
constraints.gridy = 2;
panelTools.add(textFieldNumber, constraints);
constraints.gridx = 0;
constraints.gridy = 3;
panelTools.add(buttonRemoveBus, constraints);
constraints.gridx = 0;
constraints.gridy = 4;
panelTools.add(buttonRefreshCollection, constraints);
constraints.gridx = 0;
constraints.gridy = 5;
panelTools.add(buttonTrash, constraints);
setLayout(new BorderLayout());
add(panelTools, BorderLayout.EAST);
add(pictureBoxCollection,BorderLayout.CENTER);
}
private void buttonAddBusClick(){
if (listBoxStorages.getSelectedIndex() == -1)
return;
var obj = storage.getCollection(listBoxStorages.getSelectedValue());
if (obj == null)
return;
FrameTrolleybus form;
try{
form = new FrameTrolleybus();
}
catch (IOException e){
throw new RuntimeException();
}
form.selectBusButton.addActionListener(e -> {
form.selectBus();
if (obj.insert(form.getSelectedBus())) {
JOptionPane.showMessageDialog(this, "Объект добавлен");
pictureBoxCollection.repaint();
} else {
JOptionPane.showMessageDialog(this, "Не удалось добавить объект");
}
form.dispose();
});
}
private void buttonRemoveBusClick() {
if (listBoxStorages.getSelectedIndex() == -1)
return;
var obj = storage.getCollection(listBoxStorages.getSelectedValue());
if (obj == null)
return;
int pos = Integer.parseInt(textFieldNumber.getText());
queue.add(obj.Get(pos));
if (obj.remove(pos))
{
JOptionPane.showMessageDialog(this,"Объект удалён");
pictureBoxCollection.repaint();
}
else
{
JOptionPane.showMessageDialog(this,"Не удалось удалить объект");
}
}
private void buttonRefreshBusClick(){
pictureBoxCollection.repaint();
}
private void reloadObjects(){
int index = listBoxStorages.getSelectedIndex();
listBoxModel.clear();
List<String> keys = storage.Keys();
for (String key : keys) {
listBoxModel.addElement(key);
}
if(listBoxModel.size() > 0 && (index == -1 || index >= listBoxModel.size()))
listBoxStorages.setSelectedIndex(0);
else if(listBoxModel.size() > 0)
listBoxStorages.setSelectedIndex(index);
}
private void buttonAddCollectionClick() {
if(textFieldStorageName.getText() == null ) {
JOptionPane.showMessageDialog(this, "Не все данные заполнены");
return;
}
String name = textFieldStorageName.getText();
if (Objects.equals(name, "")) {
JOptionPane.showMessageDialog(this, "Не все данные заполнены");
return;
}
storage.addSet(name);
reloadObjects();
}
private void buttonDeleteCollectionClick() {
if (listBoxStorages.getSelectedIndex() == -1)
return;
storage.delSet(listBoxStorages.getSelectedValue());
reloadObjects();
}
private void buttonTrashClick(){
if(queue.size() == 0)
return;
FrameTrolleybus form;
try{
form = new FrameTrolleybus();
form.ChangeTrolleybus(queue.peek());
queue.remove();
}
catch (IOException e){
throw new RuntimeException();
}
}
}

43
src/FrameHard.java Normal file
View File

@ -0,0 +1,43 @@
import javax.swing.*;
import java.awt.*;
import java.util.Random;
public class FrameHard extends JFrame{
HardGeneric<EntityBus, IDrawDoors> generic;
DrawingBus drawing;
private JComponent pictureBoxHard;
private int pictureBoxWidth = 210;
private int pictureBoxHeight = 165;
public FrameHard(){
setSize(300, 300);
setLocationRelativeTo(null);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
pictureBoxHard = new JComponent(){
public void paintComponent(Graphics graphics){
super.paintComponent(graphics);
Graphics2D graphics2D = (Graphics2D) graphics;
if (drawing != null) drawing.drawTransport(graphics2D);
super.repaint();
}
};
pictureBoxHard.setSize(pictureBoxWidth,pictureBoxHeight);
JButton buttonMakeObject = new JButton("Создать объект");
Random rand = new Random();
int size = rand.nextInt(1, 10);
generic = new HardGeneric<>(size, size, pictureBoxWidth, pictureBoxHeight);
for(int i = 0; i < size; i++){
generic.insertBuses(generic.makeRandomBus());
generic.insertDoors(generic.makeRandomDoor());
}
buttonMakeObject.addActionListener(e -> {
DrawingBus drawingBus = generic.makeObject();
drawingBus.setPosition(pictureBoxWidth / 2 - drawingBus.getWidth()/2,
pictureBoxHeight / 2 - drawingBus.getHeight()/2);
drawing = drawingBus;
pictureBoxHard.repaint();
});
setLayout(new BorderLayout());
add(pictureBoxHard, BorderLayout.CENTER);
add(buttonMakeObject, BorderLayout.SOUTH);
setVisible(true);
}
}

199
src/FrameTrolleybus.java Normal file
View File

@ -0,0 +1,199 @@
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.colorchooser.ColorChooserComponentFactory;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.IOException;
import java.util.Random;
public class FrameTrolleybus extends JFrame {
private DrawingBus drawingBus;
private AbstractStrategy abstractStrategy;
private JComboBox<String> comboBoxStrategy;
public JButton selectBusButton;
private DrawingBus selectedBus;
public DrawingBus getSelectedBus(){ return selectedBus;}
private final JComponent pictureBoxTrolleybus;
public FrameTrolleybus() throws IOException {
super("Троллейбус");
setSize(new Dimension(900,500));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pictureBoxTrolleybus = new JComponent(){
public void paintComponent(Graphics graphics){
super.paintComponent(graphics);
Graphics2D graphics2D = (Graphics2D) graphics;
if (drawingBus != null) drawingBus.drawTransport(graphics2D);
super.repaint();
}
};
comboBoxStrategy = new JComboBox<>(new String[]{"к центру", "к границе"});
JButton stepButton = new JButton("Шаг");
JButton createBusButton = new JButton("Создать автобус");
JButton createTrolleybusButton = new JButton("Создать троллейбус");
selectBusButton = 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());
createBusButton.addActionListener(e -> buttonCreateBusClick());
createTrolleybusButton.addActionListener(e -> buttonCreateTrolleybusClick());
selectBusButton.addActionListener(e -> dispose());
stepButton.addActionListener(e -> buttonStepClick());
rightButton.setActionCommand("right");
rightButton.addActionListener(this::buttonMoveClick);
leftButton.setActionCommand("left");
leftButton.addActionListener(this::buttonMoveClick);
upButton.setActionCommand("up");
upButton.addActionListener(this::buttonMoveClick);
downButton.setActionCommand("down");
downButton.addActionListener(this::buttonMoveClick);
setLayout(new BorderLayout());
JPanel panelTrolleybus = new JPanel(new BorderLayout());
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);
constraints.gridx = 2;
constraints.gridy = 0;
createPanel.add(selectBusButton, 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));
constraints.gridx = 2;
constraints.gridy = 1;
constraints.insets.left = constraints.insets.top = constraints.insets.bottom = constraints.insets.right = 2;
movementPanel.add(rightButton, constraints);
leftButton.setPreferredSize(new Dimension(30,30));
constraints.gridx = 0;
constraints.gridy = 1;
movementPanel.add(leftButton, constraints);
upButton.setPreferredSize(new Dimension(30,30));
constraints.gridx = 1;
constraints.gridy = 0;
movementPanel.add(upButton, constraints);
downButton.setPreferredSize(new Dimension(30,30));
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(leftPanel, BorderLayout.WEST);
add(panelTrolleybus,BorderLayout.CENTER);
setVisible(true);
}
private void buttonCreateTrolleybusClick() {
Random random = new Random();
Color bodyColor = JColorChooser.showDialog(null,"Основной цвет", null);
Color additColor = JColorChooser.showDialog(null,"Дополнительный цвет", null);
pictureBoxTrolleybus.setBounds(0,0,getContentPane().getWidth(),getContentPane().getHeight());
drawingBus = new DrawingTrolleybus(random.nextInt(200) + 100, random.nextInt(2000) + 1000,
bodyColor, additColor, random.nextBoolean(), random.nextBoolean(), 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 buttonCreateBusClick(){
Random random = new Random();
Color bodyColor = JColorChooser.showDialog(null,"Основной цвет", null);
pictureBoxTrolleybus.setBounds(0,0,getContentPane().getWidth(),getContentPane().getHeight());
drawingBus = new DrawingBus(
random.nextInt(200) + 100,
random.nextInt(2000) + 1000,
bodyColor,
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(drawingBus == null || drawingBus.getEntityBus() == null)
return;
switch (event.getActionCommand())
{
case "left":
drawingBus.moveTransport(DirectionType.LEFT);
break;
case "right":
drawingBus.moveTransport(DirectionType.RIGHT);
break;
case "up":
drawingBus.moveTransport(DirectionType.UP);
break;
case "down":
drawingBus.moveTransport(DirectionType.DOWN);
break;
}
draw();
}
private void draw() {
if (drawingBus == null)
{
return;
}
pictureBoxTrolleybus.repaint();
}
public void selectBus() {
if (drawingBus == null) {
return;
}
selectedBus = drawingBus;
}
public void ChangeTrolleybus(DrawingBus bus){
bus.setPosition(0,0);
drawingBus = bus;
pictureBoxTrolleybus.setBounds(0,0,getContentPane().getWidth(),getContentPane().getHeight());
pictureBoxTrolleybus.repaint();
}
}

78
src/HardGeneric.java Normal file
View File

@ -0,0 +1,78 @@
import java.awt.*;
import java.util.Random;
public class HardGeneric <T extends EntityBus,U extends IDrawDoors> {
T[] buses;
U[] doors;
private int busesNumber;
private int doorsNumber;
private int pictureBoxWidth;
private int pictureBoxHeight;
public HardGeneric(int busesCount, int doorsCount, int width, int height) {
busesNumber = 0;
doorsNumber = 0;
buses = (T[]) new EntityBus[busesCount];
doors = (U[]) new IDrawDoors[doorsCount];
pictureBoxHeight = height;
pictureBoxWidth = width;
}
public int insertBuses(T entityBus) {
if (buses[buses.length - 1] != null)
return -1;
for (int i = busesNumber - 1; i >= 0; i--) {
buses[i + 1] = buses[i];
}
busesNumber++;
buses[0] = entityBus;
return 0;
}
public int insertDoors(U door) {
if (doors[doors.length - 1] != null)
return -1;
for (int i = doorsNumber - 1; i >= 0; i--) {
doors[i + 1] = doors[i];
}
doorsNumber++;
doors[0] = door;
return 0;
}
public DrawingBus makeObject() {
Random rand = new Random();
EntityBus entity = buses[rand.nextInt(0, busesNumber)];
IDrawDoors door = doors[rand.nextInt(0, doorsNumber)];
if (entity instanceof EntityTrolleybus) {
return new DrawingTrolleybus(entity.getSpeed(), entity.getWeight(), entity.getBodyColor(),
((EntityTrolleybus) entity).getAdditionalColor(), ((EntityTrolleybus) entity).getRoga(),
((EntityTrolleybus) entity).getBattery(), pictureBoxWidth, pictureBoxHeight, door.getNumber(), door.getType());
}
return new DrawingBus(entity.getSpeed(), entity.getWeight(), entity.getBodyColor(),
pictureBoxWidth, pictureBoxHeight, door.getNumber(), door.getType());
}
public EntityBus makeRandomBus() {
Random random = new Random();
EntityBus bus;
switch (random.nextInt(2)) {
case 0 -> bus = new EntityBus(random.nextInt(100, 300), random.nextDouble(1000, 3000),
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)));
default -> bus = new EntityTrolleybus(random.nextInt(100, 300), random.nextDouble(1000, 3000),
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());
}
return bus;
}
public IDrawDoors makeRandomDoor(){
Random random = new Random();
IDrawDoors door;
switch (random.nextInt(3)){
case 1 -> door = new DrawingDoorsRoundedUp();
case 2 -> door = new DrawingDoorsRoundedUpAndDown();
default -> door = new DrawingDoors();
}
door.setNumber((random.nextInt(3) + 1) * 2);
return door;
}
}

8
src/IDrawDoors.java Normal file
View File

@ -0,0 +1,8 @@
import java.awt.*;
public interface IDrawDoors {
int getNumber();
int getType();
void setNumber(int x);
void drawDoors(Graphics2D graphics2D, int _startX, int _startY);
}

6
src/IMoveableObject.java Normal file
View File

@ -0,0 +1,6 @@
public interface IMoveableObject {
ObjectParameters getObjectPosition();
int getStep();
boolean checkCanMove(DirectionType direction);
void moveObject(DirectionType direction);
}

4
src/Main.java Normal file
View File

@ -0,0 +1,4 @@
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException { new FrameBusCollection(); }
}

31
src/MoveToBorder.java Normal file
View File

@ -0,0 +1,31 @@
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();
}
}
}
}

38
src/MoveToCenter.java Normal file
View File

@ -0,0 +1,38 @@
public class MoveToCenter extends AbstractStrategy{
@Override
protected boolean isTargetDestination(){
var objParams = getObjectParameters();
if(objParams == null)
return false;
return objParams.getObjectMiddleHorizontal() <= getFieldWidth() / 2 &&
objParams.getObjectMiddleHorizontal() + getStep() >= getFieldWidth() / 2 &&
objParams.getObjectMiddleVertical() <= getFieldHeight() / 2 &&
objParams.getObjectMiddleVertical() + getStep() >= getFieldHeight() / 2;
}
@Override
protected void moveToTarget() {
ObjectParameters objParams = getObjectParameters();
if (objParams == null) {
return;
}
var diffX = objParams.getObjectMiddleHorizontal() - getFieldWidth() / 2;
if (Math.abs(diffX) > getStep()) {
if (diffX > 0) {
moveLeft();
}
else {
moveRight();
}
}
var diffY = objParams.getObjectMiddleVertical() - getFieldHeight() / 2;
if (Math.abs(diffY) > getStep()) {
if (diffY > 0) {
moveUp();
}
else {
moveDown();
}
}
}
}

19
src/ObjectParameters.java Normal file
View File

@ -0,0 +1,19 @@
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;
}
}

43
src/SetGeneric.java Normal file
View File

@ -0,0 +1,43 @@
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
public class SetGeneric<T extends Object>{
private final List<T> places;
private final int maxCount;
public int getCount() {return places.size();}
public SetGeneric(int count){
maxCount = count;
places = new ArrayList<>();
}
public boolean insert(T bus){
if(places.size() == maxCount)
return false;
return insert(bus, 0);
}
public boolean insert(T bus, int position){
if (!(position >= 0 && position <= places.size() && places.size() < maxCount))
return false;
places.add(position, bus);
return true;
}
public boolean remove(int position){
if(!(position >= 0 && position < getCount()))
return false;
places.remove(position);
return true;
}
public T Get(int position){
if(!(position >= 0 && position < getCount()))
return null;
return (T)places.get(position);
}
public ArrayList<T> GetBuses(int maxBuses){
ArrayList<T> toRet = new ArrayList<>();
for(int i = 0; i < places.size(); i++){
toRet.add(places.get(i));
if(i == maxBuses)
return toRet;
}
return toRet;
}
}

5
src/Status.java Normal file
View File

@ -0,0 +1,5 @@
public enum Status {
NOT_INIT,
IN_PROGRESS,
FINISH
}