Compare commits

...

17 Commits

40 changed files with 2198 additions and 0 deletions

BIN
Resources/DownArrow.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 414 B

BIN
Resources/LeftArrow.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 439 B

BIN
Resources/RightArrow.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 377 B

BIN
Resources/UpArrow.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 439 B

View File

@ -0,0 +1,18 @@
package DoubleDeckerBus;
import javax.swing.*;
import java.awt.*;
import DoubleDeckerBus.DrawningObjects.*;
public class Canvas extends JComponent {
public DrawningBus DrawningBus;
public void paintComponent (Graphics g){
if (DrawningBus == null){
return;
}
super.paintComponents (g) ;
Graphics2D g2d = (Graphics2D)g;
DrawningBus.DrawTransport(g2d);
super.repaint();
}
}

View File

@ -0,0 +1,22 @@
package DoubleDeckerBus;
import DoubleDeckerBus.Generics.BusGenericStorage;
import javax.swing.*;
import java.awt.*;
public class CollectionCanvas extends JComponent {
public BusGenericStorage _storage;
public JList<String> listBoxStorages;
@Override
public void paintComponent (Graphics g){
if (listBoxStorages == null || listBoxStorages.getSelectedIndex() == -1) {
return;
}
super.paintComponents(g);
Graphics2D g2d = (Graphics2D)g;
g2d.drawImage(_storage.Get(listBoxStorages.getSelectedValue()).ShowTheBuses(), 0, 0, this);
super.repaint();
}
}

View File

@ -0,0 +1,8 @@
package DoubleDeckerBus;
public enum DirectionType {
Up,
Down,
Left,
Right
}

View File

@ -0,0 +1,7 @@
package DoubleDeckerBus;
public enum DoorNumberType {
Three,
Four,
Five
}

View File

@ -0,0 +1,35 @@
package DoubleDeckerBus.DragDrop;
import java.awt.*;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;
public class ColorTransferable implements Transferable {
private Color color;
public static final DataFlavor colorDataFlavor = new DataFlavor(Color.class, "Color");
public ColorTransferable(Color color) {
this.color = color;
}
@Override
public DataFlavor[] getTransferDataFlavors() {
return new DataFlavor[]{colorDataFlavor};
}
@Override
public boolean isDataFlavorSupported(DataFlavor flavor) {
return colorDataFlavor.equals(flavor);
}
@Override
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
if (isDataFlavorSupported(flavor)) {
return color;
} else {
throw new UnsupportedFlavorException(flavor);
}
}
}

View File

@ -0,0 +1,37 @@
package DoubleDeckerBus.DragDrop;
import DoubleDeckerBus.DrawningObjects.IDraw;
import javax.swing.*;
import java.awt.datatransfer.Transferable;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class IDrawComponent extends JComponent {
public IDraw obj;
public IDrawComponent(IDraw obj){
this.obj = obj;
this.addMouseListener(
new MouseAdapter(){
@Override
public void mousePressed(MouseEvent e) {
((IDrawComponent)e.getComponent()).getTransferHandler().exportAsDrag(((IDrawComponent)e.getComponent()), e, TransferHandler.COPY);
}
}
);
this.setTransferHandler(
new TransferHandler(){
@Override
public int getSourceActions(JComponent c) {
return TransferHandler.COPY;
}
@Override
protected Transferable createTransferable(JComponent c) {
return new iDrawTransferable(((IDrawComponent)c).obj);
}
}
);
}
}

View File

@ -0,0 +1,12 @@
package DoubleDeckerBus.DragDrop;
import javax.swing.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class LabelMouseAdapter extends MouseAdapter {
@Override
public void mousePressed(MouseEvent e) {
((JLabel)e.getComponent()).getTransferHandler().exportAsDrag(((JLabel)e.getComponent()), e, TransferHandler.COPY);
}
}

View File

@ -0,0 +1,17 @@
package DoubleDeckerBus.DragDrop;
import javax.swing.*;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
public class LabelTransferHandler extends TransferHandler {
@Override
public int getSourceActions(JComponent c) {
return TransferHandler.COPY;
}
@Override
protected Transferable createTransferable(JComponent c) {
return new StringSelection(((JLabel)c).getText());
}
}

View File

@ -0,0 +1,12 @@
package DoubleDeckerBus.DragDrop;
import javax.swing.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class PanelMouseAdapter extends MouseAdapter {
@Override
public void mousePressed(MouseEvent e) {
((JPanel)e.getComponent()).getTransferHandler().exportAsDrag(((JPanel)e.getComponent()), e, TransferHandler.COPY);
}
}

View File

@ -0,0 +1,16 @@
package DoubleDeckerBus.DragDrop;
import javax.swing.*;
import java.awt.datatransfer.Transferable;
public class PanelTransferHandler extends TransferHandler {
@Override
public int getSourceActions(JComponent c) {
return TransferHandler.COPY;
}
@Override
protected Transferable createTransferable(JComponent c) {
return new ColorTransferable(((JPanel)c).getBackground());
}
}

View File

@ -0,0 +1,36 @@
package DoubleDeckerBus.DragDrop;
import DoubleDeckerBus.DrawningObjects.IDraw;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;
public class iDrawTransferable implements Transferable {
private IDraw iDrawObject;
public static final DataFlavor iDrawDataFlavor = new DataFlavor(IDraw.class, "IDraw");
public iDrawTransferable(IDraw iDrawObject) {
this.iDrawObject = iDrawObject;
}
@Override
public DataFlavor[] getTransferDataFlavors() {
return new DataFlavor[]{iDrawDataFlavor};
}
@Override
public boolean isDataFlavorSupported(DataFlavor flavor) {
return iDrawDataFlavor.equals(flavor);
}
@Override
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
if (isDataFlavorSupported(flavor)) {
return iDrawObject;
} else {
throw new UnsupportedFlavorException(flavor);
}
}
}

View File

@ -0,0 +1,203 @@
package DoubleDeckerBus.DrawningObjects;
import DoubleDeckerBus.DirectionType;
import DoubleDeckerBus.Entities.EntityBus;
import DoubleDeckerBus.MovementStrategy.*;
import javax.swing.*;
import java.awt.*;
import java.util.Random;
public class DrawningBus {
protected EntityBus EntityBus;
public int _pictureWidth;
public int _pictureHeight;
protected int _startPosX;
protected int _startPosY;
public int _busWidth = 110;
public int _busHeight = 70;
public int _doorNumb;
protected IDraw DrawningDoor;
public EntityBus EntityBus() {
return EntityBus;
}
public DrawningBus(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 option = random.nextInt(1,4);
if (option == 1) {
DrawningDoor = new DrawningDoor(_busWidth, _busHeight, _startPosX, _startPosY);
} else if (option == 2) {
DrawningDoor = new DrawningDoorOval(_busWidth, _busHeight, _startPosX, _startPosY);
} else if (option == 3) {
DrawningDoor = new DrawningDoorTriangle(_busWidth, _busHeight, _startPosX, _startPosY);
}
DrawningDoor.ChangeDoorsNumber(random.nextInt(2, 6));
}
protected DrawningBus(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 option = random.nextInt(1,4);
if (option == 1) {
DrawningDoor = new DrawningDoor(_busWidth, _busHeight, _startPosX, _startPosY);
} else if (option == 2) {
DrawningDoor = new DrawningDoorOval(_busWidth, _busHeight, _startPosX, _startPosY);
} else if (option == 3) {
DrawningDoor = new DrawningDoorTriangle(_busWidth, _busHeight, _startPosX, _startPosY);
}
DrawningDoor.ChangeDoorsNumber(random.nextInt(2, 6));
}
public void ChangeIDraw(IDraw obj) {
DrawningDoor = obj;
obj.ChangeDoorsNumber(_doorNumb);
obj.ChangeX(_startPosX);
obj.ChangeY(_startPosY);
}
public void ChangeDoorsNumb(int numb) {
_doorNumb = numb;
DrawningDoor.ChangeDoorsNumber(numb);
}
public void SetPosition(int x, int y){
if (EntityBus == null) {
return;
}
_startPosX = x;
_startPosY = y;
DrawningDoor.ChangeX(_startPosX);
DrawningDoor.ChangeY(_startPosY);
if (x + _busWidth >= _pictureWidth || y + _busHeight >= _pictureHeight || x < 0 || y < 0) {
_startPosX = 0;
_startPosY = 0;
}
}
public int GetPosX() {
return _startPosX;
}
public int GetPosY() {
return _startPosY;
}
public int GetWidth() {
return _busWidth;
}
public int GetHeight() {
return _busHeight;
}
public IMoveableObject GetMoveableObject(){
return new DrawningObjectBus(this);
}
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;
}
DrawningDoor.ChangeX(_startPosX);
DrawningDoor.ChangeY(_startPosY);
}
public void ChangeColor(Color col){
EntityBus.BodyColor = col;
}
public void DrawTransport(Graphics2D g2d) {
if (EntityBus == null) {
return;
}
// Границы первого этажа автобуса
g2d.setColor(EntityBus.BodyColor());
g2d.fillRect(_startPosX, _startPosY + 30, 100, 30);
// Колеса
g2d.setColor(Color.black);
g2d.fillOval(_startPosX + 7, _startPosY + 55, 10, 10);
g2d.fillOval(_startPosX + 77, _startPosY + 55, 10, 10);
// Окна
g2d.setColor(Color.blue);
g2d.fillOval(_startPosX + 10, _startPosY + 35, 10, 15);
g2d.fillOval(_startPosX + 50, _startPosY + 35, 10, 15);
g2d.fillOval(_startPosX + 70, _startPosY + 35, 10, 15);
g2d.fillOval(_startPosX + 90, _startPosY + 35, 10, 15);
// двери
DrawningDoor.DrawDoors(g2d);
}
}

View File

@ -0,0 +1,68 @@
package DoubleDeckerBus.DrawningObjects;
import DoubleDeckerBus.DoorNumberType;
import javax.swing.*;
import java.awt.*;
public class DrawningDoor implements IDraw {
private DoorNumberType DoorNumberType;
private Color blackColor;
private int Width;
private int Height;
public int CurX;
public int CurY;
public DrawningDoor(int width, int height, int curX, int curY) {
Width = width;
Height = height;
CurX = curX;
CurY = curY;
blackColor = Color.BLACK;
}
public void ChangeX(int x){
CurX = x;
}
public void ChangeY(int y){
CurY = y;
}
public void ChangeDoorsNumber(int x) {
if (x <= 3) {
DoorNumberType = DoorNumberType.Three;
}
if (x == 4) {
DoorNumberType = DoorNumberType.Four;
}
if (x >= 5) {
DoorNumberType = DoorNumberType.Five;
}
}
public DoorNumberType DoorNumberType() {
return DoorNumberType;
}
public void DrawDoor(Graphics2D g2d, int x, int y){
g2d.fillRect(x, y, 10, 20);
}
public void DrawDoors(Graphics2D g2d) {
g2d.setColor(blackColor);
DrawDoor(g2d, CurX + 15, CurY + 40);
DrawDoor(g2d, CurX + 30, CurY + 40);
if (DoorNumberType() == DoorNumberType.Three || DoorNumberType() == DoorNumberType.Four || DoorNumberType() == DoorNumberType.Five) {
DrawDoor(g2d, CurX + 45, CurY + 40);
}
if (DoorNumberType() == DoorNumberType.Four || DoorNumberType() == DoorNumberType.Five) {
DrawDoor(g2d, CurX + 60, CurY + 40);
}
if (DoorNumberType() == DoorNumberType.Five) {
DrawDoor(g2d, CurX + 75, CurY + 40);
}
}
}

View File

@ -0,0 +1,68 @@
package DoubleDeckerBus.DrawningObjects;
import DoubleDeckerBus.DoorNumberType;
import javax.swing.*;
import java.awt.*;
public class DrawningDoorOval implements IDraw {
private DoorNumberType DoorNumberType;
private Color blackColor;
private int Width;
private int Height;
public int CurX;
public int CurY;
public DrawningDoorOval(int width, int height, int curX, int curY) {
Width = width;
Height = height;
CurX = curX;
CurY = curY;
blackColor = Color.BLACK;
}
public void ChangeX(int x){
CurX = x;
}
public void ChangeY(int y){
CurY = y;
}
public void ChangeDoorsNumber(int x) {
if (x <= 3) {
DoorNumberType = DoorNumberType.Three;
}
if (x == 4) {
DoorNumberType = DoorNumberType.Four;
}
if (x >= 5) {
DoorNumberType = DoorNumberType.Five;
}
}
public DoorNumberType DoorNumberType() {
return DoorNumberType;
}
public void DrawDoor(Graphics2D g2d, int x, int y){
g2d.fillOval(x, y, 10, 20);
}
public void DrawDoors(Graphics2D g2d) {
g2d.setColor(blackColor);
DrawDoor(g2d, CurX + 15, CurY + 40);
DrawDoor(g2d, CurX + 30, CurY + 40);
if (DoorNumberType() == DoorNumberType.Three || DoorNumberType() == DoorNumberType.Four || DoorNumberType() == DoorNumberType.Five) {
DrawDoor(g2d, CurX + 45, CurY + 40);
}
if (DoorNumberType() == DoorNumberType.Four || DoorNumberType() == DoorNumberType.Five) {
DrawDoor(g2d, CurX + 60, CurY + 40);
}
if (DoorNumberType() == DoorNumberType.Five) {
DrawDoor(g2d, CurX + 75, CurY + 40);
}
}
}

View File

@ -0,0 +1,70 @@
package DoubleDeckerBus.DrawningObjects;
import DoubleDeckerBus.DoorNumberType;
import javax.swing.*;
import java.awt.*;
public class DrawningDoorTriangle implements IDraw {
private DoubleDeckerBus.DoorNumberType DoorNumberType;
private Color blackColor;
private int Width;
private int Height;
public int CurX;
public int CurY;
public DrawningDoorTriangle(int width, int height, int curX, int curY) {
Width = width;
Height = height;
CurX = curX;
CurY = curY;
blackColor = Color.BLACK;
}
public void ChangeX(int x){
CurX = x;
}
public void ChangeY(int y){
CurY = y;
}
public void ChangeDoorsNumber(int x) {
if (x <= 3) {
DoorNumberType = DoorNumberType.Three;
}
if (x == 4) {
DoorNumberType = DoorNumberType.Four;
}
if (x >= 5) {
DoorNumberType = DoorNumberType.Five;
}
}
public DoorNumberType DoorNumberType() {
return DoorNumberType;
}
public void DrawDoor(Graphics2D g2d, int x, int y){
int[] xPoints = {x, x + 5, x + 10};
int[] yPoints = {y + 20, y, y + 20};
g2d.fillPolygon(xPoints, yPoints, 3);
}
public void DrawDoors(Graphics2D g2d) {
g2d.setColor(blackColor);
DrawDoor(g2d, CurX + 15, CurY + 40);
DrawDoor(g2d, CurX + 30, CurY + 40);
if (DoorNumberType() == DoorNumberType.Three || DoorNumberType() == DoorNumberType.Four || DoorNumberType() == DoorNumberType.Five) {
DrawDoor(g2d, CurX + 45, CurY + 40);
}
if (DoorNumberType() == DoorNumberType.Four || DoorNumberType() == DoorNumberType.Five) {
DrawDoor(g2d, CurX + 60, CurY + 40);
}
if (DoorNumberType() == DoorNumberType.Five) {
DrawDoor(g2d, CurX + 75, CurY + 40);
}
}
}

View File

@ -0,0 +1,73 @@
package DoubleDeckerBus.DrawningObjects;
import DoubleDeckerBus.Entities.EntityDoubleDeckerBus;
import java.awt.*;
public class DrawningDoubleDeckerBus extends DrawningBus {
public DrawningDoubleDeckerBus(int speed, double weight, Color bodyColor, Color additionalColor, int doorNumber,
int width, int height, boolean secondFloor, boolean ladder, boolean lineBetweenFloor) {
super(speed, weight, bodyColor, width, height);
if (EntityBus != null) {
EntityBus = new EntityDoubleDeckerBus(speed, weight, bodyColor, additionalColor, doorNumber, secondFloor,
ladder, lineBetweenFloor);
}
}
public void ChangeAddColor(Color col) {
((EntityDoubleDeckerBus)EntityBus).AdditionalColor = col;
}
@Override
public void DrawTransport(Graphics2D g2d) {
if (!(EntityBus instanceof EntityDoubleDeckerBus)) {
return;
}
super.DrawTransport(g2d);
EntityDoubleDeckerBus _bus = (EntityDoubleDeckerBus) EntityBus;
// второй этаж
if (_bus.SecondFloor())
{
g2d.setColor(_bus.BodyColor());
// Границы второго этажа автобуса
g2d.setColor(_bus.AdditionalColor());
g2d.fillRect(_startPosX, _startPosY, 100, 30);
// Окна второго этажа
g2d.setColor(Color.BLUE);
g2d.fillOval(_startPosX + 12, _startPosY + 5, 10, 15);
g2d.fillOval(_startPosX + 30, _startPosY + 5, 10, 15);
g2d.fillOval(_startPosX + 50, _startPosY + 5, 10, 15);
g2d.fillOval(_startPosX + 70, _startPosY + 5, 10, 15);
g2d.fillOval(_startPosX + 90, _startPosY + 5, 10, 15);
}
// лестница
if (_bus.Ladder())
{
if (_bus.SecondFloor()) {
//Вертикальные прямые
g2d.setColor(Color.black);
g2d.drawLine(_startPosX, _startPosY + 55, _startPosX, _startPosY + 25);
g2d.drawLine(_startPosX + 10, _startPosY + 55, _startPosX + 10, _startPosY + 25);
//Горизонтальные прямые
g2d.drawLine(_startPosX, _startPosY + 35, _startPosX + 10, _startPosY + 35);
g2d.drawLine(_startPosX, _startPosY + 45, _startPosX + 10, _startPosY + 45);
g2d.drawLine(_startPosX, _startPosY + 55, _startPosX + 10, _startPosY + 55);
}
}
// полоса между этажами
if (_bus.LineBetweenFloor())
{
g2d.setColor(Color.black);
g2d.fillRect(_startPosX, _startPosY + 30, 100, 3);
}
}
}

View File

@ -0,0 +1,14 @@
package DoubleDeckerBus.DrawningObjects;
import DoubleDeckerBus.DoorNumberType;
import java.awt.*;
public interface IDraw {
public void ChangeDoorsNumber(int x);
public DoorNumberType DoorNumberType();
public void DrawDoor(Graphics2D g2d, int x, int y);
public void DrawDoors(Graphics2D g2d);
public void ChangeX(int x);
public void ChangeY(int y);
}

View File

@ -0,0 +1,33 @@
package DoubleDeckerBus.Entities;
import java.awt.*;
public class EntityBus {
public int Speed;
public double Weight;
public Color BodyColor;
public double Step;
public int Speed() {
return Speed;
}
public double Weight() {
return Weight;
}
public Color BodyColor() {
return BodyColor;
}
public double Step() {
return Step;
}
public EntityBus(int speed, double weight, Color bodyColor) {
Speed = speed;
Weight = weight;
Step = (double) Speed * 100 / Weight;
BodyColor = bodyColor;
}
}

View File

@ -0,0 +1,39 @@
package DoubleDeckerBus.Entities;
import java.awt.*;
public class EntityDoubleDeckerBus extends EntityBus {
public Color AdditionalColor;
public boolean SecondFloor;
public boolean Ladder;
public boolean LineBetweenFloor;
public Color AdditionalColor() {
return AdditionalColor;
}
public boolean SecondFloor() {
return SecondFloor;
}
public boolean Ladder() {
return Ladder;
}
public boolean LineBetweenFloor() {
return LineBetweenFloor;
}
public double Step() {
return Step;
}
public EntityDoubleDeckerBus(int speed, double weight, Color bodyColor, Color additionalColor,
int doorNumber, boolean secondFloor ,boolean ladder, boolean lineBetweenFloor) {
super(speed, weight, bodyColor);
AdditionalColor = additionalColor;
SecondFloor = secondFloor;
Ladder = ladder;
LineBetweenFloor = lineBetweenFloor;
}
}

View File

@ -0,0 +1,199 @@
package DoubleDeckerBus;
import DoubleDeckerBus.DrawningObjects.DrawningBus;
import DoubleDeckerBus.Generics.BusGenericStorage;
import DoubleDeckerBus.Generics.BusTrashCollection;
import DoubleDeckerBus.Generics.TheBusesGenericCollection;
import DoubleDeckerBus.MovementStrategy.DrawningObjectBus;
import java.util.List;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class FormBusCollection {
private final BusGenericStorage _storage;
private JList<String> listBoxStorages;
private DefaultListModel<String> listBoxModel;
private int pictureBoxWidth = 605;
private int pictureBoxHeight = 426;
CollectionCanvas canvas;
public void Draw() {
if (canvas == null) {
return;
}
canvas.repaint();
}
private void ReloadObjects() {
int index = listBoxStorages.getSelectedIndex();
listBoxModel.clear();
List<String> keys = _storage.Keys();
for (int i = 0; i < keys.size(); i++) {
listBoxModel.addElement(keys.get(i));
}
if (listBoxModel.size() > 0 && (index == -1 || index >= listBoxModel.size())) {
listBoxStorages.setSelectedIndex(0);
} else if(listBoxModel.size() > 0) {
listBoxStorages.setSelectedIndex(index);
}
}
public FormBusCollection() {
BusTrashCollection<DrawningBus> _trashCollection = new BusTrashCollection<>();
JButton callTrashButton = new JButton("мусор");
_storage = new BusGenericStorage(pictureBoxWidth, pictureBoxHeight);
JScrollPane scrollPane = new JScrollPane();
canvas = new CollectionCanvas();
JPanel toolBox = new JPanel();
JTextField storageName = new JTextField();
JButton addStorageButton = new JButton("Добавить набор");
listBoxModel = new DefaultListModel<>();
listBoxStorages= new JList<>(listBoxModel);
scrollPane.setViewportView(listBoxStorages);
JButton delStorageButton = new JButton("Удалить набор");
toolBox.setBounds(623,12, 227, 80);
JFrame collectionFrame = new JFrame();
collectionFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
collectionFrame.setSize(880,497);
toolBox.setBounds(623, 12, 227, 426);
canvas.setBounds(12,12,pictureBoxWidth,pictureBoxHeight);
JButton addButton = new JButton("Добавить");
JButton removeButton = new JButton("Удалить");
JButton refreshButton = new JButton("Обновить");
JTextField busNumb = new JTextField();
GridLayout lay = new GridLayout(9,1);
toolBox.add(storageName);
toolBox.add(addStorageButton);
toolBox.add(scrollPane);
toolBox.add(delStorageButton);
toolBox.setLayout(lay);
toolBox.add(addButton);
toolBox.add(busNumb);
toolBox.add(removeButton);
toolBox.add(refreshButton);
toolBox.add(callTrashButton);
collectionFrame.add(toolBox);
collectionFrame.add(canvas);
collectionFrame.setVisible(true);
canvas._storage = _storage;
canvas.listBoxStorages = listBoxStorages;
addStorageButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (storageName.getText() == null) {
return;
}
_storage.AddSet(storageName.getText());
ReloadObjects();
}
});
delStorageButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (listBoxStorages.getSelectedIndex() == -1) {
return;
}
_storage.DelSet(listBoxStorages.getSelectedValue());
ReloadObjects();
}
});
callTrashButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_trashCollection.GetSize() == 0) {
return;
}
FormDoubleDeckerBus form = new FormDoubleDeckerBus();
form.ChangeBus(_trashCollection.GetTop());
_trashCollection.Pop();
}
});
addButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (listBoxStorages.getSelectedIndex() == -1) {
return;
}
TheBusesGenericCollection<DrawningBus, DrawningObjectBus> _bus = _storage.Get(listBoxStorages.getSelectedValue());
FormBusConfig form = new FormBusConfig();
form.addButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_bus.Insert(form._bus))
{
JOptionPane.showMessageDialog(null, "Объект добавлен", "Информация", JOptionPane.INFORMATION_MESSAGE);
form._bus._pictureWidth = pictureBoxWidth;
form._bus._pictureHeight = pictureBoxHeight;
Draw();
}
else
{
JOptionPane.showMessageDialog(null, "Не удалось добавить объект", "Информация", JOptionPane.INFORMATION_MESSAGE);
}
form.frameConfig.dispose();
Draw();
}
});
form.cancelButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
form.frameConfig.dispose();
Draw();
}
});
}
});
removeButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (listBoxStorages.getSelectedIndex() == -1) {
return;
}
TheBusesGenericCollection<DrawningBus, DrawningObjectBus> _bus = _storage.Get(listBoxStorages.getSelectedValue());
if (_bus == null) {
return;
}
String tmp = busNumb.getText();
int numb;
try {
numb = Integer.parseInt(tmp);
} catch (Exception ex){
JOptionPane.showMessageDialog(null, "Введите число", "Информация", JOptionPane.INFORMATION_MESSAGE);
return;
}
DrawningBus curBus = _bus.Get(numb);
_trashCollection.Push(curBus);
_bus.Remove(numb);
_bus.ShowTheBuses();
JOptionPane.showMessageDialog(null, "Объект удален", "Информация", JOptionPane.INFORMATION_MESSAGE);
Draw();
}
});
refreshButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (listBoxStorages.getSelectedIndex() == -1) {
return;
}
TheBusesGenericCollection<DrawningBus, DrawningObjectBus> _bus = _storage.Get(listBoxStorages.getSelectedValue());
if (_bus == null) {
return;
}
_bus.ShowTheBuses();
Draw();
}
});
}
}

View File

@ -0,0 +1,327 @@
package DoubleDeckerBus;
import DoubleDeckerBus.DragDrop.*;
import DoubleDeckerBus.DrawningObjects.*;
import javax.swing.*;
import java.awt.*;
import java.awt.datatransfer.*;
import java.io.IOException;
public class FormBusConfig {
public JFrame frameConfig;
public JButton addButton;
public JButton cancelButton;
public DrawningBus _bus;
int _pictureBoxWidth = 218;
int _pictureBoxHeight = 190;
public FormBusConfig(){
frameConfig = new JFrame();
addButton = new JButton("Добавить");
cancelButton = new JButton("Отмена");
addButton.setBounds(555,262,94,29);
cancelButton.setBounds(679,262,94,29);
Canvas canvas = new Canvas();
canvas.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
canvas.setBounds(555, 65, _pictureBoxWidth, _pictureBoxHeight);
JLabel iDrawLabel = new JLabel("Двери");
JLabel colorLabel = new JLabel("Цвет");
DrawningBus toGetSize = new DrawningBus(0,0, null, 0, 0);
IDrawComponent baseDoor = new IDrawComponent(new DrawningDoor(toGetSize._busWidth - toGetSize._busWidth / 10,
toGetSize._busHeight,
_pictureBoxWidth / 2 -toGetSize._busWidth / 2,
_pictureBoxHeight / 2 -toGetSize._busHeight / 2));
IDrawComponent ovalDoor = new IDrawComponent(new DrawningDoorOval(toGetSize._busWidth - toGetSize._busWidth / 10,
toGetSize._busHeight,
_pictureBoxWidth / 2 -toGetSize._busWidth / 2,
_pictureBoxHeight / 2 -toGetSize._busHeight / 2));
IDrawComponent triangleDoor = new IDrawComponent(new DrawningDoorTriangle(toGetSize._busWidth - toGetSize._busWidth / 10,
toGetSize._busHeight,
_pictureBoxWidth / 2 -toGetSize._busWidth / 2,
_pictureBoxHeight / 2 -toGetSize._busHeight / 2));
baseDoor.setLayout(new GridLayout(1,1));
ovalDoor.setLayout(new GridLayout(1,1));
triangleDoor.setLayout(new GridLayout(1,1));
iDrawLabel.setHorizontalAlignment(SwingConstants.CENTER);
iDrawLabel.setVerticalAlignment(SwingConstants.CENTER);
colorLabel.setHorizontalAlignment(SwingConstants.CENTER);
colorLabel.setVerticalAlignment(SwingConstants.CENTER);
JLabel baseLabel = new JLabel("Прямоугольные");
JLabel ovalLabel = new JLabel("Овальные");
JLabel triangleLabel = new JLabel("Треугольные");
baseLabel.setHorizontalAlignment(SwingConstants.CENTER);
baseLabel.setVerticalAlignment(SwingConstants.CENTER);
ovalLabel.setHorizontalAlignment(SwingConstants.CENTER);
ovalLabel.setVerticalAlignment(SwingConstants.CENTER);
triangleLabel.setHorizontalAlignment(SwingConstants.CENTER);
triangleLabel.setVerticalAlignment(SwingConstants.CENTER);
baseDoor.add(baseLabel);
ovalDoor.add(ovalLabel);
triangleDoor.add(triangleLabel);
baseDoor.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
ovalDoor.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
triangleDoor.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
JLabel addColorLabel = new JLabel("Доп цвет");
addColorLabel.setHorizontalAlignment(SwingConstants.CENTER);
addColorLabel.setVerticalAlignment(SwingConstants.CENTER);
colorLabel.setBounds(555, 20, 70, 33);
addColorLabel.setBounds(629, 20, 70, 33);
iDrawLabel.setBounds(703, 20, 70, 33);
colorLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
addColorLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
iDrawLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
JCheckBox checkSecondFloor = new JCheckBox("Наличие 2 этажа");
JCheckBox checkLadder = new JCheckBox("Наличие лестницы");
JCheckBox checkLineBetweenFloor = new JCheckBox("Наличие разделителя");
checkSecondFloor.setBounds(6, 122, 159, 24);
checkLadder.setBounds(6, 152, 145, 24);
checkLineBetweenFloor.setBounds(6, 182, 162, 24);
JLabel simpleLabel = new JLabel("Простой");
JLabel advancedLabel = new JLabel("Продвинутый");
simpleLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
advancedLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
simpleLabel.setBounds(171,169, 120, 50);
simpleLabel.setHorizontalAlignment(SwingConstants.CENTER);
simpleLabel.setVerticalAlignment(SwingConstants.CENTER);
advancedLabel.setBounds(297,169, 120, 50);
advancedLabel.setHorizontalAlignment(SwingConstants.CENTER);
advancedLabel.setVerticalAlignment(SwingConstants.CENTER);
baseDoor.setBounds(171,229, 120, 50);
ovalDoor.setBounds(297,229, 120, 50);
triangleDoor.setBounds(423,229, 120, 50);
JLabel speedLabel = new JLabel ("Скорость");
JLabel weightLabel = new JLabel ("Вес");
JPanel colorPanel = new JPanel();
colorPanel.setBounds(171, 23, 234,143);
SpinnerNumberModel speedSpinnerModel = new SpinnerNumberModel(100.0, 100.0, 1000.0, 1.0);
SpinnerNumberModel weightSpinnerModel = new SpinnerNumberModel(100.0, 100.0, 1000.0, 1.0);
SpinnerNumberModel doorNumbSpinnerModel = new SpinnerNumberModel(3, 3, 5, 1.0);
JSpinner doorNumbSpinner = new JSpinner(doorNumbSpinnerModel);
JSpinner speedSpinner = new JSpinner(speedSpinnerModel);
JSpinner weightSpinner = new JSpinner(weightSpinnerModel);
speedSpinner.setBounds(6, 46, 150, 27);
speedLabel.setBounds(6, 23, 73, 20);
weightSpinner.setBounds(6, 99, 150, 27);
weightLabel.setBounds(6, 76, 33, 20);
doorNumbSpinner.setBounds(6, 220, 150, 27);
JPanel redPanel = new JPanel();
JPanel greenPanel = new JPanel();
JPanel bluePanel = new JPanel();
JPanel yellowPanel = new JPanel();
JPanel whitePanel = new JPanel();
JPanel grayPanel = new JPanel();
JPanel blackPanel = new JPanel();
JPanel purplePanel = new JPanel();
redPanel.setTransferHandler(new PanelTransferHandler());
greenPanel.setTransferHandler(new PanelTransferHandler());
bluePanel.setTransferHandler(new PanelTransferHandler());
yellowPanel.setTransferHandler(new PanelTransferHandler());
whitePanel.setTransferHandler(new PanelTransferHandler());
grayPanel.setTransferHandler(new PanelTransferHandler());
blackPanel.setTransferHandler(new PanelTransferHandler());
purplePanel.setTransferHandler(new PanelTransferHandler());
redPanel.addMouseListener(new PanelMouseAdapter());
greenPanel.addMouseListener(new PanelMouseAdapter());
bluePanel.addMouseListener(new PanelMouseAdapter());
yellowPanel.addMouseListener(new PanelMouseAdapter());
whitePanel.addMouseListener(new PanelMouseAdapter());
grayPanel.addMouseListener(new PanelMouseAdapter());
blackPanel.addMouseListener(new PanelMouseAdapter());
purplePanel.addMouseListener(new PanelMouseAdapter());
redPanel.setName("Красный");
greenPanel.setName("Зелёный");
bluePanel.setName("Синий");
yellowPanel.setName("Жёлтый");
whitePanel.setName("Белый");
grayPanel.setName("Серый");
blackPanel.setName("Чёрный");
purplePanel.setName("Фиолетовый");
simpleLabel.setTransferHandler(new LabelTransferHandler());
simpleLabel.addMouseListener(new LabelMouseAdapter());
advancedLabel.setTransferHandler(new LabelTransferHandler());
advancedLabel.addMouseListener(new LabelMouseAdapter());
redPanel.setBackground(Color.RED);
greenPanel.setBackground(Color.GREEN);
bluePanel.setBackground(Color.BLUE);
yellowPanel.setBackground(Color.YELLOW);
whitePanel.setBackground(Color.WHITE);
grayPanel.setBackground(Color.GRAY);
blackPanel.setBackground(Color.BLACK);
purplePanel.setBackground(Color.PINK);
colorPanel.setLayout(new GridLayout(2, 4));
colorPanel.add(redPanel);
colorPanel.add(greenPanel);
colorPanel.add(bluePanel);
colorPanel.add(yellowPanel);
colorPanel.add(whitePanel);
colorPanel.add(grayPanel);
colorPanel.add(blackPanel);
colorPanel.add(purplePanel);
frameConfig.add(colorLabel);
frameConfig.add(addColorLabel);
frameConfig.add(iDrawLabel);
frameConfig.setLayout(null);
frameConfig.setSize(818, 350);
frameConfig.add(speedLabel);
frameConfig.add(speedSpinner);
frameConfig.add(weightLabel);
frameConfig.add(weightSpinner);
frameConfig.add(simpleLabel);
frameConfig.add(advancedLabel);
frameConfig.add(checkSecondFloor);
frameConfig.add(checkLadder);
frameConfig.add(checkLineBetweenFloor);
frameConfig.add(canvas);
frameConfig.add(addButton);
frameConfig.add(cancelButton);
frameConfig.add(doorNumbSpinner);
frameConfig.setVisible(true);
frameConfig.add(colorPanel);
frameConfig.add(baseDoor);
frameConfig.add(ovalDoor);
frameConfig.add(triangleDoor);
colorPanel.setVisible(true);
baseDoor.setVisible(true);
ovalDoor.setVisible(true);
triangleDoor.setVisible(true);
canvas.setTransferHandler(
new TransferHandler(){
@Override
public boolean canImport(TransferHandler.TransferSupport support) {
return support.isDataFlavorSupported(DataFlavor.stringFlavor);
}
@Override
public boolean importData(TransferHandler.TransferSupport support) {
if (canImport(support)) {
try {
Object speedObj = speedSpinner.getValue();
Number speedNumb = (Number) speedObj;
int speed = speedNumb.intValue();
Object weightObj = weightSpinner.getValue();
Number weightNumb = (Number) weightObj;
int weight = weightNumb.intValue();
Object doorNumbObj = doorNumbSpinner.getValue();
Number doorNumb = (Number) doorNumbObj;
int doorsNumb = doorNumb.intValue();
String data = (String) support.getTransferable().getTransferData(DataFlavor.stringFlavor);
switch (data) {
case "Простой":
_bus = new DrawningBus(speed, weight, Color.WHITE, _pictureBoxWidth, _pictureBoxHeight);
_bus.ChangeDoorsNumb(doorsNumb);
break;
case "Продвинутый":
_bus = new DrawningDoubleDeckerBus(speed, weight, Color.WHITE, Color.BLACK,
doorsNumb, _pictureBoxWidth, _pictureBoxHeight,
checkSecondFloor.isSelected(), checkLadder.isSelected(), checkLineBetweenFloor.isSelected());
_bus.ChangeDoorsNumb(doorsNumb);
break;
}
_bus.SetPosition(_pictureBoxWidth / 2 -_bus._busWidth / 2,
_pictureBoxHeight / 2 -_bus._busHeight / 2);
canvas.DrawningBus = _bus;
canvas.repaint();
return true;
} catch (UnsupportedFlavorException | IOException e) {
e.printStackTrace();
}
}
return false;
}
}
);
iDrawLabel.setTransferHandler(
new TransferHandler(){
@Override
public boolean canImport(TransferHandler.TransferSupport support) {
return support.isDataFlavorSupported(iDrawTransferable.iDrawDataFlavor);
}
@Override
public boolean importData(TransferHandler.TransferSupport support) {
if (canImport(support)) {
try {
IDraw obj = (IDraw) support.getTransferable().getTransferData(iDrawTransferable.iDrawDataFlavor);
if (_bus == null)
return false;
_bus.ChangeIDraw(obj);
canvas.repaint();
return true;
} catch (UnsupportedFlavorException | IOException e) {
e.printStackTrace();
}
}
return false;
}
}
);
colorLabel.setTransferHandler(
new TransferHandler(){
@Override
public boolean canImport(TransferHandler.TransferSupport support) {
return support.isDataFlavorSupported(ColorTransferable.colorDataFlavor);
}
@Override
public boolean importData(TransferHandler.TransferSupport support) {
if (canImport(support)) {
try {
Color color = (Color) support.getTransferable().getTransferData(ColorTransferable.colorDataFlavor);
if (_bus == null)
return false;
_bus.ChangeColor(color);
canvas.repaint();
return true;
} catch (UnsupportedFlavorException | IOException e) {
e.printStackTrace();
}
}
return false;
}
}
);
addColorLabel.setTransferHandler(
new TransferHandler(){
@Override
public boolean canImport(TransferHandler.TransferSupport support) {
return support.isDataFlavorSupported(ColorTransferable.colorDataFlavor);
}
@Override
public boolean importData(TransferHandler.TransferSupport support) {
if (canImport(support)) {
try {
Color color = (Color) support.getTransferable().getTransferData(ColorTransferable.colorDataFlavor);
if (_bus == null || !(_bus instanceof DrawningDoubleDeckerBus))
return false;
((DrawningDoubleDeckerBus)_bus).ChangeAddColor(color);
canvas.repaint();
return true;
} catch (UnsupportedFlavorException | IOException e) {
e.printStackTrace();
}
}
return false;
}
}
);
}
}

View File

@ -0,0 +1,224 @@
package DoubleDeckerBus;
import DoubleDeckerBus.DrawningObjects.DrawningDoubleDeckerBus;
import DoubleDeckerBus.DrawningObjects.DrawningBus;
import DoubleDeckerBus.Entities.EntityBus;
import DoubleDeckerBus.MovementStrategy.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.swing.*;
public class FormDoubleDeckerBus {
static DrawningBus DrawningBus;
static AbstractStrategy _abstractStrategy;
public JButton buttonSelect;
public JFrame BusFrame;
Canvas canvas;
static int pictureBoxWidth = 882;
static int pictureBoxHeight = 453;
public Color ChooseColor(JFrame BusFrame){
JColorChooser dialog = new JColorChooser();
Color res = JColorChooser.showDialog(BusFrame, "Выберите цвет", Color.WHITE);
return res;
}
public DrawningBus SelectedBus(){
return DrawningBus;
}
public void Draw() {
if (DrawningBus == null) {
return;
}
canvas.repaint();
}
public void ChangeBus(DrawningBus newBus){
newBus.SetPosition(0,0);
DrawningBus = newBus;
canvas.DrawningBus = DrawningBus;
}
public FormDoubleDeckerBus() {
BusFrame = new JFrame();
JButton buttonCreate = new JButton("Создать");
JButton buttonCreateDoubleDeckerBus = new JButton("Создать двухэтажный автобус");
JButton buttonStep = new JButton("Шаг");
buttonSelect = new JButton ("Выбрать");
JComboBox comboBoxStrategy = new JComboBox(new String[]{"Довести до центра", "Довести до края"});
JButton UpButton = new JButton();
UpButton.setIcon(new ImageIcon("D:\\Файлы\\УлГТУ\\3 семестр\\РПП\\DoubleDeckerBus\\Resources\\UpArrow.png"));
JButton DownButton = new JButton();
DownButton.setIcon(new ImageIcon("D:\\Файлы\\УлГТУ\\3 семестр\\РПП\\DoubleDeckerBus\\Resources\\DownArrow.png"));
JButton LeftButton = new JButton();
LeftButton.setIcon(new ImageIcon("D:\\Файлы\\УлГТУ\\3 семестр\\РПП\\DoubleDeckerBus\\Resources\\LeftArrow.png"));
JButton RightButton = new JButton();
RightButton.setIcon(new ImageIcon("D:\\Файлы\\УлГТУ\\3 семестр\\РПП\\DoubleDeckerBus\\Resources\\RightArrow.png"));
buttonStep.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e){
if (DrawningBus == 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 DrawningObjectBus(DrawningBus), pictureBoxWidth, pictureBoxHeight);
comboBoxStrategy.setEnabled(false);
}
if (_abstractStrategy == null)
{
return;
}
_abstractStrategy.MakeStep();
Draw();
if (_abstractStrategy.GetStatus() == Status.Finish)
{
comboBoxStrategy.setEnabled(true);
_abstractStrategy = null;
}
}
}
);
buttonCreate.addActionListener(
new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Random random = new Random();
Color color = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256));
Color choosen = ChooseColor(BusFrame);
if (choosen != null){
color = choosen;
}
DrawningBus = new DrawningBus(random.nextInt(100, 300), random.nextDouble(1000, 3000),
color, pictureBoxWidth, pictureBoxHeight);
canvas.DrawningBus = DrawningBus;
comboBoxStrategy.enable(true);
Draw();
}
}
);
buttonCreateDoubleDeckerBus.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e){
Random random = new Random();
Color color = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256));
Color additionalColor = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256));
Color choosen = ChooseColor(BusFrame);
if (choosen != null){
color = choosen;
}
choosen = ChooseColor(BusFrame);
if (choosen != null){
additionalColor = choosen;
}
DrawningBus = new DrawningDoubleDeckerBus(random.nextInt(100, 300), random.nextDouble(1000, 3000),
color, additionalColor, random.nextInt(1, 6), pictureBoxWidth, pictureBoxHeight,
random.nextBoolean(), random.nextBoolean(), random.nextBoolean());
canvas.DrawningBus = DrawningBus;
comboBoxStrategy.enable(true);
Draw();
}
}
);
RightButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (DrawningBus.EntityBus() == null) {
return;
}
DrawningBus.MoveTransport(DirectionType.Right);
Draw();
}
});
LeftButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (DrawningBus.EntityBus() == null) {
return;
}
DrawningBus.MoveTransport(DirectionType.Left);
Draw();
}
});
UpButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (DrawningBus.EntityBus() == null) {
return;
}
DrawningBus.MoveTransport(DirectionType.Up);
Draw();
}
});
DownButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (DrawningBus.EntityBus() == null) {
return;
}
DrawningBus.MoveTransport(DirectionType.Down);
Draw();
}
});
BusFrame.setSize(900, 500);
BusFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
BusFrame.setLayout(null);
canvas = new Canvas();
canvas.setSize(pictureBoxWidth, pictureBoxHeight);
buttonSelect.setBounds(453,401, 180, 40);
buttonCreateDoubleDeckerBus.setBounds(198, 401, 250, 40);
buttonCreate.setBounds(12, 401, 180, 40);
RightButton.setBounds(840,411,30,30);
LeftButton.setBounds(768,411,30,30);
UpButton.setBounds(804,375,30,30);
DownButton.setBounds(804,411,30,30);
comboBoxStrategy.setBounds(719,12,151,28);
buttonStep.setBounds(768, 46, 94, 29);
BusFrame.add(canvas);
BusFrame.add(buttonCreate);
BusFrame.add(buttonCreateDoubleDeckerBus);
BusFrame.add(UpButton);
BusFrame.add(DownButton);
BusFrame.add(LeftButton);
BusFrame.add(RightButton);
BusFrame.add(comboBoxStrategy);
BusFrame.add(buttonStep);
BusFrame.add(buttonSelect);
BusFrame.setVisible(true);
}
}

View File

@ -0,0 +1,51 @@
package DoubleDeckerBus.Generics;
import DoubleDeckerBus.DrawningObjects.DrawningBus;
import DoubleDeckerBus.MovementStrategy.DrawningObjectBus;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;
public class BusGenericStorage {
final HashMap<String, TheBusesGenericCollection<DrawningBus, DrawningObjectBus>> _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 BusGenericStorage(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 TheBusesGenericCollection<>(_pictureWidth, _pictureHeight));
}
public void DelSet(String name) {
if (!_busStorages.containsKey(name)) {
return;
}
_busStorages.remove(name);
}
public TheBusesGenericCollection<DrawningBus, DrawningObjectBus> Get(String name){
if (!_busStorages.containsKey(name)) {
return null;
}
return _busStorages.get(name);
}
public DrawningBus Get(String collectionName, int position) {
return _busStorages.get(collectionName).Get(position);
}
}

View File

@ -0,0 +1,35 @@
package DoubleDeckerBus.Generics;
import DoubleDeckerBus.DrawningObjects.DrawningBus;
import java.util.Stack;
public class BusTrashCollection<T extends DrawningBus> {
Stack<T> _stack;
public BusTrashCollection(){
_stack = new Stack<>();
}
public void Push(T bus){
_stack.push(bus);
}
public int GetSize(){
return _stack.size();
}
public void Pop(){
if (_stack.size() == 0) {
return;
}
_stack.pop();
}
public T GetTop() {
if (_stack.isEmpty()) {
return null;
}
return _stack.peek();
}
}

View File

@ -0,0 +1,67 @@
package DoubleDeckerBus.Generics;
import DoubleDeckerBus.DrawningObjects.DrawningBus;
import DoubleDeckerBus.DrawningObjects.IDraw;
import DoubleDeckerBus.Entities.EntityBus;
import java.util.ArrayList;
import java.util.Random;
public class HardGeneric <T extends EntityBus,U extends IDraw> {
ArrayList<T> Buses;
ArrayList<U> Doors;
private int CountBuses;
private int MaxCountBuses;
private int CountDoors;
private int MaxCountDoors;
private int pictureBoxWidth;
private int pictureBoxHeight;
public HardGeneric(int maxCountBuses, int maxCountDoors, int width, int height) {
MaxCountBuses = maxCountBuses;
MaxCountDoors = maxCountDoors;
Buses = new ArrayList<T>(MaxCountBuses);
Doors = new ArrayList<U>(MaxCountDoors);
CountBuses = 0;
CountDoors = 0;
pictureBoxHeight = height;
pictureBoxWidth = width;
}
public boolean Add(T bus) {
if (bus == null) {
return false;
}
if (CountBuses > MaxCountBuses) {
return false;
}
Buses.add(CountBuses++, bus);
return true;
}
public boolean Add(U door) {
if (door == null) {
return false;
}
if (CountDoors > MaxCountDoors) {
return false;
}
Doors.add(CountDoors++, door);
return true;
}
public DrawningBus MakeObject() {
if (CountBuses == 0 || CountDoors == 0) {
return null;
}
Random rand = new Random();
int indBus = rand.nextInt(0, CountBuses);
int indDoors = rand.nextInt(0,CountDoors);
T entity = Buses.get(indBus);
U door = Doors.get(indDoors);
return new DrawningBus(entity.Speed(), entity.Weight(), entity.BodyColor(), pictureBoxWidth, pictureBoxHeight);
}
}

View File

@ -0,0 +1,60 @@
package DoubleDeckerBus.Generics;
import java.util.ArrayList;
import java.util.List;
public class SetGeneric<T extends Object>{
private final List<T> _places;
public int Count;
private final int _maxCount;
public SetGeneric(int count){
_maxCount = count;
_places = new ArrayList<>();
}
public boolean Insert(T bus){
if (_places.size() == _maxCount) {
return false;
}
Insert(bus, 0);
return true;
}
public boolean Insert(T bus, int position){
if (!(position >= 0 && position <= _places.size() && _places.size() < _maxCount)) {
return false;
}
_places.add(position, bus);
Count++;
return true;
}
public boolean Remove(int position){
if (!(position >= 0 && position < _places.size())) {
return false;
}
_places.remove(position);
Count--;
return true;
}
public T Get(int position){
if (!(position >= 0 && position < Count)) {
return null;
}
return (T)_places.get(position);
}
public ArrayList<T> GetBus(int maxBus){
ArrayList<T> toRet = new ArrayList<>();
for (int i = 0; i < _places.size(); i++) {
toRet.add(_places.get(i));
if (i == maxBus) {
return toRet;
}
}
return toRet;
}
}

View File

@ -0,0 +1,88 @@
package DoubleDeckerBus.Generics;
import DoubleDeckerBus.DrawningObjects.DrawningBus;
import DoubleDeckerBus.MovementStrategy.IMoveableObject;
import java.awt.*;
import java.awt.image.BufferedImage;
public class TheBusesGenericCollection<T extends DrawningBus, U extends IMoveableObject> {
private final int _pictureWidth;
private final int _pictureHeight;
private final int _placeSizeWidth = 133;
private final int _placeSizeHeight = 50;
private final SetGeneric<T> _collection;
public TheBusesGenericCollection(int picWidth, int picHeight){
int width = picWidth / _placeSizeWidth;
int height = picHeight / _placeSizeHeight;
_pictureWidth = picWidth;
_pictureHeight = picHeight;
_collection = new SetGeneric<T>(width * height);
}
public int Size() {
return _collection.Count;
}
public boolean Insert(T obj) {
if (obj == null) {
return false;
}
return _collection.Insert(obj);
}
public boolean Remove(int position) {
return _collection.Remove(position);
}
public U GetU(int pos) {
T ans = _collection.Get(pos);
if (ans == null) {
return null;
}
return (U)ans.GetMoveableObject();
}
public T Get(int position) {
if (position < 0 || position >= _collection.Count) {
return null;
}
return _collection.Get(position);
}
public BufferedImage ShowTheBuses() {
BufferedImage bmp = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_4BYTE_ABGR);
Graphics gr = bmp.createGraphics();
DrawBackground(gr);
DrawObjects(gr);
return bmp;
}
private void DrawBackground(Graphics g) {
g.setColor(Color.BLACK);
for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++) {
for (int j = 0; j < _pictureHeight / (_placeSizeHeight * 2) + 1; ++j) {
g.drawLine(i * _placeSizeWidth, j * _placeSizeHeight * 2,
i * _placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight * 2);
}
g.drawLine(i * _placeSizeWidth, 0, i * _placeSizeWidth,
_pictureHeight / (_placeSizeHeight * 2) * _placeSizeHeight * 2);
}
}
private void DrawObjects(Graphics g) {
for (int i = 0; i < _collection.Count; i++) {
DrawningBus bus = _collection.Get(i);
if (bus != null) {
int inRow = _pictureWidth / _placeSizeWidth;
bus.SetPosition((inRow - 1 - (i % inRow)) * _placeSizeWidth, i / inRow * _placeSizeHeight * 2);
bus.DrawTransport((Graphics2D) g);
}
}
}
}

View File

@ -0,0 +1,78 @@
package DoubleDeckerBus;
import DoubleDeckerBus.DrawningObjects.*;
import DoubleDeckerBus.Entities.EntityBus;
import DoubleDeckerBus.Generics.HardGeneric;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
public class HardForm extends JFrame {
private Canvas canvas;
private int pictureBoxWidth;
private int pictureBoxHeight;
private EntityBus createRandomEntityBus() {
Random rand = new Random();
return new EntityBus(
rand.nextInt(100, 300),
rand.nextDouble(1000, 3000),
Color.getHSBColor(
rand.nextInt(0, 301),
rand.nextInt(0, 301),
rand.nextInt(0, 301)
)
);
}
void redrawCanvas() {
if (canvas == null) {
return;
}
canvas.repaint();
}
private IDraw createDrawningDoor() {
return new DrawningDoor(pictureBoxWidth, pictureBoxHeight, 0, 0);
}
public HardForm() {
Random rand = new Random();
int size = rand.nextInt(1, 10);
JFrame HardFrame = new JFrame();
HardFrame.setSize(700, 400);
HardFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
canvas = new Canvas();
canvas.setBounds(0,0,pictureBoxWidth,pictureBoxHeight);
JButton createObjectButton = new JButton("Создать");
createObjectButton.setBounds(0, 0, 100, 40);
canvas.add(createObjectButton);
HardFrame.setContentPane(canvas);
HardFrame.setVisible(true);
pictureBoxHeight = canvas.getHeight();
pictureBoxWidth = canvas.getWidth();
HardGeneric<EntityBus, IDraw> objectGenerator = new HardGeneric<>(size, size, pictureBoxWidth, pictureBoxHeight);
for (int i = 0; i < size; i++) {
EntityBus ent = createRandomEntityBus();
objectGenerator.Add(ent);
objectGenerator.Add(createDrawningDoor());
}
createObjectButton.addActionListener(
new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
DrawningBus DrawningBus = objectGenerator.MakeObject();
DrawningBus.SetPosition(pictureBoxWidth / 2 - DrawningBus.GetWidth()/2,
pictureBoxHeight / 2 - DrawningBus.GetHeight()/2);
canvas.DrawningBus = DrawningBus;
redrawCanvas();
}
}
);
}
}

View File

@ -0,0 +1,9 @@
package DoubleDeckerBus;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
FormBusCollection formBusCollection = new FormBusCollection();
}
}

View File

@ -0,0 +1,98 @@
package DoubleDeckerBus.MovementStrategy;
import DoubleDeckerBus.DirectionType;
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.GetObjectParameters();
}
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;
}
}

View File

@ -0,0 +1,41 @@
package DoubleDeckerBus.MovementStrategy;
import DoubleDeckerBus.DrawningObjects.DrawningBus;
import DoubleDeckerBus.DirectionType;
public class DrawningObjectBus implements IMoveableObject {
private final DrawningBus _drawningBus;
public DrawningObjectBus(DrawningBus drawningBus){
_drawningBus = drawningBus;
}
public ObjectParameters GetObjectParameters(){
if (_drawningBus == null || _drawningBus.EntityBus() == null) {
return null;
}
return new ObjectParameters(_drawningBus.GetPosX(), _drawningBus.GetPosY(),
_drawningBus.GetWidth(), _drawningBus.GetHeight());
}
public int GetStep(){
if (_drawningBus.EntityBus() == null) {
return 0;
}
return (int)_drawningBus.EntityBus().Step();
}
public boolean CheckCanMove(DirectionType direction){
if (_drawningBus == null) {
return false;
}
return _drawningBus.CanMove(direction);
}
public void MoveObject(DirectionType direction){
if (_drawningBus == null) {
return;
}
_drawningBus.MoveTransport(direction);
}
}

View File

@ -0,0 +1,10 @@
package DoubleDeckerBus.MovementStrategy;
import DoubleDeckerBus.DirectionType;
public interface IMoveableObject {
public ObjectParameters GetObjectParameters();
public int GetStep();
boolean CheckCanMove(DirectionType direction);
void MoveObject(DirectionType direction);
}

View File

@ -0,0 +1,37 @@
package DoubleDeckerBus.MovementStrategy;
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) {
MoveRight();
}
}
var diffY = objParams.DownBorder - FieldHeight();
if (Math.abs(diffY) >= GetStep()) {
if (diffY < 0) {
MoveDown();
}
}
}
}

View File

@ -0,0 +1,50 @@
package DoubleDeckerBus.MovementStrategy;
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.ObjectMiddleHorizontal >= FieldWidth() / 2 &&
objParams.ObjectMiddleHorizontal - GetStep() <= FieldWidth() / 2)) &&
((objParams.ObjectMiddleVertical <= FieldHeight() / 2 &&
objParams.ObjectMiddleVertical + GetStep() >= FieldHeight() / 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();
}
}
}
}

View File

@ -0,0 +1,29 @@
package DoubleDeckerBus.MovementStrategy;
public class ObjectParameters {
private final int _x;
private final int _y;
private final int _width;
private final int _height;
public int LeftBorder;
public int TopBorder;
public int RightBorder;
public int DownBorder;
public int ObjectMiddleHorizontal;
public int ObjectMiddleVertical;
public ObjectParameters(int x, int y, int width, int height)
{
_x = x;
_y = y;
_width = width;
_height = height;
LeftBorder = _x;
TopBorder = _y;
RightBorder = _x + width;
DownBorder = _y + height;
ObjectMiddleHorizontal = _x + _width / 2;
ObjectMiddleVertical = _y + _height / 2;
}
}

View File

@ -0,0 +1,7 @@
package DoubleDeckerBus.MovementStrategy;
public enum Status {
NotInit,
InProgress,
Finish
}