diff --git a/src/DoubleDeckerBus/Canvas.java b/src/DoubleDeckerBus/Canvas.java new file mode 100644 index 0000000..bff8378 --- /dev/null +++ b/src/DoubleDeckerBus/Canvas.java @@ -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(); + } +} \ No newline at end of file diff --git a/src/DoubleDeckerBus/CollectionCanvas.java b/src/DoubleDeckerBus/CollectionCanvas.java new file mode 100644 index 0000000..afd7948 --- /dev/null +++ b/src/DoubleDeckerBus/CollectionCanvas.java @@ -0,0 +1,23 @@ +package DoubleDeckerBus; + +import DoubleDeckerBus.DrawningObjects.DrawningBus; +import DoubleDeckerBus.Generics.TheBusesGenericCollection; +import DoubleDeckerBus.MovementStrategy.DrawningObjectBus; + +import javax.swing.*; +import java.awt.*; + +public class CollectionCanvas extends JComponent { + public TheBusesGenericCollection _bus; + + @Override + public void paintComponent (Graphics g){ + if (_bus == null) { + return; + } + super.paintComponents(g); + Graphics2D g2d = (Graphics2D)g; + g2d.drawImage(_bus.ShowTheBuses(), 0, 0, this); + super.repaint(); + } +} diff --git a/src/DoubleDeckerBus/DrawningObjects/DrawningBus.java b/src/DoubleDeckerBus/DrawningObjects/DrawningBus.java index 9da9393..be8d928 100644 --- a/src/DoubleDeckerBus/DrawningObjects/DrawningBus.java +++ b/src/DoubleDeckerBus/DrawningObjects/DrawningBus.java @@ -2,62 +2,56 @@ 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 JPanel BusPanel; protected EntityBus EntityBus; - private int _pictureWidth; - private int _pictureHeight; + public int _pictureWidth; + public int _pictureHeight; protected int _startPosX; protected int _startPosY; protected int _busWidth = 110; protected int _busHeight = 70; - private IDraw DrawningDoor; + protected IDraw DrawningDoor; public EntityBus EntityBus() { return EntityBus; } - public DrawningBus(int speed, double weight, Color bodyColor, int width, int height, JPanel busPanel) { + public DrawningBus(int speed, double weight, Color bodyColor, int width, int height) { if (width <= _busWidth || height <= _busHeight) { return; } _startPosX = 0; _startPosY = 0; - busPanel.setSize(width, height); - BusPanel = busPanel; - busPanel.paint(BusPanel.getGraphics()); _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, busPanel); + DrawningDoor = new DrawningDoor(_busWidth, _busHeight, _startPosX, _startPosY); } else if (option == 2) { - DrawningDoor = new DrawningDoorOval(_busWidth, _busHeight, _startPosX, _startPosY, busPanel); + DrawningDoor = new DrawningDoorOval(_busWidth, _busHeight, _startPosX, _startPosY); } else if (option == 3) { - DrawningDoor = new DrawningDoorTriangle(_busWidth, _busHeight, _startPosX, _startPosY, busPanel); + 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, JPanel busPanel) { + int width, int height, int busWidth, int busHeight) { if (width <= _busWidth || height <= _busHeight) { return; } _startPosX = 0; _startPosY = 0; - busPanel.setSize(width, height); - BusPanel = busPanel; - busPanel.paint(BusPanel.getGraphics()); _pictureWidth = width; _pictureHeight = height; _busWidth = busWidth; @@ -66,26 +60,29 @@ public class DrawningBus { Random random = new Random(); int option = random.nextInt(1,4); if (option == 1) { - DrawningDoor = new DrawningDoor(_busWidth, _busHeight, _startPosX, _startPosY, busPanel); + DrawningDoor = new DrawningDoor(_busWidth, _busHeight, _startPosX, _startPosY); } else if (option == 2) { - DrawningDoor = new DrawningDoorOval(_busWidth, _busHeight, _startPosX, _startPosY, busPanel); + DrawningDoor = new DrawningDoorOval(_busWidth, _busHeight, _startPosX, _startPosY); } else if (option == 3) { - DrawningDoor = new DrawningDoorTriangle(_busWidth, _busHeight, _startPosX, _startPosY, busPanel); + DrawningDoor = new DrawningDoorTriangle(_busWidth, _busHeight, _startPosX, _startPosY); } DrawningDoor.ChangeDoorsNumber(random.nextInt(2, 6)); } public void SetPosition(int x, int y){ - if (x < 0 || x + _busWidth > _pictureWidth) - { - x = Math.max(0, _pictureWidth - _busWidth); - } - if (y < 0 || y + _busHeight > _pictureHeight) - { - y = Math.max(0, _pictureHeight - _busHeight); + 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() { @@ -104,6 +101,10 @@ public class DrawningBus { return _busHeight; } + public IMoveableObject GetMoveableObject(){ + return new DrawningObjectBus(this); + } + public boolean CanMove(DirectionType direction) { if (EntityBus == null) @@ -131,7 +132,6 @@ public class DrawningBus { if (!CanMove(direction) || EntityBus == null) { return; } - BusPanel.paint(BusPanel.getGraphics()); switch (direction) { case Left: @@ -159,9 +159,7 @@ public class DrawningBus { DrawningDoor.ChangeY(_startPosY); } - public void DrawTransport() { - Graphics2D g2d = (Graphics2D)BusPanel.getGraphics(); - + public void DrawTransport(Graphics2D g2d) { if (EntityBus == null) { return; } @@ -183,6 +181,6 @@ public class DrawningBus { g2d.fillOval(_startPosX + 90, _startPosY + 35, 10, 15); // двери - DrawningDoor.DrawDoors(); + DrawningDoor.DrawDoors(g2d); } } \ No newline at end of file diff --git a/src/DoubleDeckerBus/DrawningObjects/DrawningDoor.java b/src/DoubleDeckerBus/DrawningObjects/DrawningDoor.java index 846fafd..031abcf 100644 --- a/src/DoubleDeckerBus/DrawningObjects/DrawningDoor.java +++ b/src/DoubleDeckerBus/DrawningObjects/DrawningDoor.java @@ -6,7 +6,6 @@ import javax.swing.*; import java.awt.*; public class DrawningDoor implements IDraw { - JPanel BusPanel; private DoorNumberType DoorNumberType; private Color blackColor; private int Width; @@ -14,13 +13,12 @@ public class DrawningDoor implements IDraw { public int CurX; public int CurY; - public DrawningDoor(int width, int height, int curX, int curY, JPanel busPanel) { + public DrawningDoor(int width, int height, int curX, int curY) { Width = width; Height = height; CurX = curX; CurY = curY; blackColor = Color.BLACK; - BusPanel = busPanel; } public void ChangeX(int x){ @@ -46,27 +44,25 @@ public class DrawningDoor implements IDraw { return DoorNumberType; } - public void DrawDoor(int x, int y){ - Graphics2D g2d = (Graphics2D)BusPanel.getGraphics(); + public void DrawDoor(Graphics2D g2d, int x, int y){ g2d.fillRect(x, y, 10, 20); } - public void DrawDoors() { - Graphics2D g2d = (Graphics2D) BusPanel.getGraphics(); + public void DrawDoors(Graphics2D g2d) { g2d.setColor(blackColor); - DrawDoor(CurX + 15, CurY + 40); - DrawDoor(CurX + 30, CurY + 40); + DrawDoor(g2d, CurX + 15, CurY + 40); + DrawDoor(g2d, CurX + 30, CurY + 40); if (DoorNumberType() == DoorNumberType.Three || DoorNumberType() == DoorNumberType.Four || DoorNumberType() == DoorNumberType.Five) { - DrawDoor(CurX + 45, CurY + 40); + DrawDoor(g2d, CurX + 45, CurY + 40); } if (DoorNumberType() == DoorNumberType.Four || DoorNumberType() == DoorNumberType.Five) { - DrawDoor(CurX + 60, CurY + 40); + DrawDoor(g2d, CurX + 60, CurY + 40); } if (DoorNumberType() == DoorNumberType.Five) { - DrawDoor(CurX + 75, CurY + 40); + DrawDoor(g2d, CurX + 75, CurY + 40); } } } diff --git a/src/DoubleDeckerBus/DrawningObjects/DrawningDoorOval.java b/src/DoubleDeckerBus/DrawningObjects/DrawningDoorOval.java index 77f7ba0..40ee074 100644 --- a/src/DoubleDeckerBus/DrawningObjects/DrawningDoorOval.java +++ b/src/DoubleDeckerBus/DrawningObjects/DrawningDoorOval.java @@ -6,7 +6,6 @@ import javax.swing.*; import java.awt.*; public class DrawningDoorOval implements IDraw { - JPanel BusPanel; private DoorNumberType DoorNumberType; private Color blackColor; private int Width; @@ -14,13 +13,12 @@ public class DrawningDoorOval implements IDraw { public int CurX; public int CurY; - public DrawningDoorOval(int width, int height, int curX, int curY, JPanel busPanel) { + public DrawningDoorOval(int width, int height, int curX, int curY) { Width = width; Height = height; CurX = curX; CurY = curY; blackColor = Color.BLACK; - BusPanel = busPanel; } public void ChangeX(int x){ @@ -46,27 +44,25 @@ public class DrawningDoorOval implements IDraw { return DoorNumberType; } - public void DrawDoor(int x, int y){ - Graphics2D g2d = (Graphics2D)BusPanel.getGraphics(); + public void DrawDoor(Graphics2D g2d, int x, int y){ g2d.fillOval(x, y, 10, 20); } - public void DrawDoors() { - Graphics2D g2d = (Graphics2D) BusPanel.getGraphics(); + public void DrawDoors(Graphics2D g2d) { g2d.setColor(blackColor); - DrawDoor(CurX + 15, CurY + 40); - DrawDoor(CurX + 30, CurY + 40); + DrawDoor(g2d, CurX + 15, CurY + 40); + DrawDoor(g2d, CurX + 30, CurY + 40); if (DoorNumberType() == DoorNumberType.Three || DoorNumberType() == DoorNumberType.Four || DoorNumberType() == DoorNumberType.Five) { - DrawDoor(CurX + 45, CurY + 40); + DrawDoor(g2d, CurX + 45, CurY + 40); } if (DoorNumberType() == DoorNumberType.Four || DoorNumberType() == DoorNumberType.Five) { - DrawDoor(CurX + 60, CurY + 40); + DrawDoor(g2d, CurX + 60, CurY + 40); } if (DoorNumberType() == DoorNumberType.Five) { - DrawDoor(CurX + 75, CurY + 40); + DrawDoor(g2d, CurX + 75, CurY + 40); } } } diff --git a/src/DoubleDeckerBus/DrawningObjects/DrawningDoorTriangle.java b/src/DoubleDeckerBus/DrawningObjects/DrawningDoorTriangle.java index 5ed797b..bdc6e49 100644 --- a/src/DoubleDeckerBus/DrawningObjects/DrawningDoorTriangle.java +++ b/src/DoubleDeckerBus/DrawningObjects/DrawningDoorTriangle.java @@ -6,7 +6,6 @@ import javax.swing.*; import java.awt.*; public class DrawningDoorTriangle implements IDraw { - JPanel BusPanel; private DoubleDeckerBus.DoorNumberType DoorNumberType; private Color blackColor; private int Width; @@ -14,13 +13,12 @@ public class DrawningDoorTriangle implements IDraw { public int CurX; public int CurY; - public DrawningDoorTriangle(int width, int height, int curX, int curY, JPanel busPanel) { + public DrawningDoorTriangle(int width, int height, int curX, int curY) { Width = width; Height = height; CurX = curX; CurY = curY; blackColor = Color.BLACK; - BusPanel = busPanel; } public void ChangeX(int x){ @@ -46,29 +44,27 @@ public class DrawningDoorTriangle implements IDraw { return DoorNumberType; } - public void DrawDoor(int x, int y){ - Graphics2D g2d = (Graphics2D)BusPanel.getGraphics(); + 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 = (Graphics2D) BusPanel.getGraphics(); + public void DrawDoors(Graphics2D g2d) { g2d.setColor(blackColor); - DrawDoor(CurX + 15, CurY + 40); - DrawDoor(CurX + 30, CurY + 40); + DrawDoor(g2d, CurX + 15, CurY + 40); + DrawDoor(g2d, CurX + 30, CurY + 40); if (DoorNumberType() == DoorNumberType.Three || DoorNumberType() == DoorNumberType.Four || DoorNumberType() == DoorNumberType.Five) { - DrawDoor(CurX + 45, CurY + 40); + DrawDoor(g2d, CurX + 45, CurY + 40); } if (DoorNumberType() == DoorNumberType.Four || DoorNumberType() == DoorNumberType.Five) { - DrawDoor(CurX + 60, CurY + 40); + DrawDoor(g2d, CurX + 60, CurY + 40); } if (DoorNumberType() == DoorNumberType.Five) { - DrawDoor(CurX + 75, CurY + 40); + DrawDoor(g2d, CurX + 75, CurY + 40); } } } diff --git a/src/DoubleDeckerBus/DrawningObjects/DrawningDoubleDeckerBus.java b/src/DoubleDeckerBus/DrawningObjects/DrawningDoubleDeckerBus.java index 04b134c..da850a5 100644 --- a/src/DoubleDeckerBus/DrawningObjects/DrawningDoubleDeckerBus.java +++ b/src/DoubleDeckerBus/DrawningObjects/DrawningDoubleDeckerBus.java @@ -2,14 +2,13 @@ package DoubleDeckerBus.DrawningObjects; import DoubleDeckerBus.Entities.EntityDoubleDeckerBus; -import javax.swing.*; 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, JPanel busPanel) { - super(speed, weight, bodyColor, width, height, busPanel); + 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); @@ -17,16 +16,15 @@ public class DrawningDoubleDeckerBus extends DrawningBus { } @Override - public void DrawTransport() { + public void DrawTransport(Graphics2D g2d) { if (!(EntityBus instanceof EntityDoubleDeckerBus)) { return; } - super.DrawTransport(); + super.DrawTransport(g2d); EntityDoubleDeckerBus _bus = (EntityDoubleDeckerBus) EntityBus; - Graphics2D g2d = (Graphics2D)BusPanel.getGraphics(); // второй этаж if (_bus.SecondFloor()) diff --git a/src/DoubleDeckerBus/DrawningObjects/IDraw.java b/src/DoubleDeckerBus/DrawningObjects/IDraw.java index 7719a89..e9ef999 100644 --- a/src/DoubleDeckerBus/DrawningObjects/IDraw.java +++ b/src/DoubleDeckerBus/DrawningObjects/IDraw.java @@ -2,11 +2,13 @@ package DoubleDeckerBus.DrawningObjects; import DoubleDeckerBus.DoorNumberType; +import java.awt.*; + public interface IDraw { public void ChangeDoorsNumber(int x); public DoorNumberType DoorNumberType(); - public void DrawDoor(int x, int y); - public void DrawDoors(); + public void DrawDoor(Graphics2D g2d, int x, int y); + public void DrawDoors(Graphics2D g2d); public void ChangeX(int x); public void ChangeY(int y); } diff --git a/src/DoubleDeckerBus/FormBusCollection.java b/src/DoubleDeckerBus/FormBusCollection.java new file mode 100644 index 0000000..c805d99 --- /dev/null +++ b/src/DoubleDeckerBus/FormBusCollection.java @@ -0,0 +1,109 @@ +package DoubleDeckerBus; + +import DoubleDeckerBus.DrawningObjects.DrawningBus; +import DoubleDeckerBus.Generics.TheBusesGenericCollection; +import DoubleDeckerBus.MovementStrategy.DrawningObjectBus; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.*; + +public class FormBusCollection { + private final TheBusesGenericCollection _bus; + private int pictureBoxWidth = 605; + private int pictureBoxHeight = 426; + + CollectionCanvas canvas; + + public void Draw() { + if (canvas == null) { + return; + } + canvas.repaint(); + } + + public FormBusCollection() { + _bus = new TheBusesGenericCollection<>(pictureBoxWidth, pictureBoxHeight); + canvas = new CollectionCanvas(); + JPanel toolBox = new JPanel(); + 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(4,1); + toolBox.setLayout(lay); + toolBox.add(addButton); + toolBox.add(busNumb); + toolBox.add(removeButton); + toolBox.add(refreshButton); + collectionFrame.add(toolBox); + collectionFrame.add(canvas); + collectionFrame.setVisible(true); + + addButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if(_bus == null) { + return; + } + FormDoubleDeckerBus form = new FormDoubleDeckerBus(); + form.buttonSelect.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (_bus.Insert(form.SelectedBus()) != -1) + { + JOptionPane.showMessageDialog(null, "Объект добавлен", "Информация", JOptionPane.INFORMATION_MESSAGE); + form.SelectedBus()._pictureWidth = pictureBoxWidth; + form.SelectedBus()._pictureHeight = pictureBoxHeight; + Draw(); + } + else + { + JOptionPane.showMessageDialog(null, "Не удалось добавить объект", "Информация", JOptionPane.INFORMATION_MESSAGE); + } + canvas._bus = _bus; + form.BusFrame.dispose(); + Draw(); + } + }); + } + }); + removeButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + 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; + } + _bus.Remove(numb); + _bus.ShowTheBuses(); + JOptionPane.showMessageDialog(null, "Объект удален", "Информация", JOptionPane.INFORMATION_MESSAGE); + Draw(); + } + }); + + refreshButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if(_bus == null) { + return; + } + _bus.ShowTheBuses(); + Draw(); + } + }); + } +} diff --git a/src/DoubleDeckerBus/FormDoubleDeckerBus.java b/src/DoubleDeckerBus/FormDoubleDeckerBus.java index 404513a..08bcac1 100644 --- a/src/DoubleDeckerBus/FormDoubleDeckerBus.java +++ b/src/DoubleDeckerBus/FormDoubleDeckerBus.java @@ -2,6 +2,7 @@ package DoubleDeckerBus; import DoubleDeckerBus.DrawningObjects.DrawningDoubleDeckerBus; import DoubleDeckerBus.DrawningObjects.DrawningBus; +import DoubleDeckerBus.Entities.EntityBus; import DoubleDeckerBus.MovementStrategy.*; import java.awt.*; @@ -17,150 +18,201 @@ import javax.swing.*; public class FormDoubleDeckerBus { static DrawningBus DrawningBus; static AbstractStrategy _abstractStrategy; - public static void main(String[] args) throws IOException { - String[] items = {"Довести до центра", "Довести до края"}; - JComboBox comboBoxStrategy = new JComboBox(items); - comboBoxStrategy.setBounds(562,12,151,28); - JFrame BusFrame = new JFrame(); - BusFrame.setResizable(false); - JPanel BusPanel = new JPanel(); - BusFrame.setLayout(new BorderLayout()); - BusFrame.setSize(743, 576); - BusFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - BusFrame.setLayout(new BorderLayout(1,1)); - BusPanel.setLayout(null); - BufferedImage RightIcon = ImageIO.read(new File("Resources/RightArrow.png")); - BufferedImage LeftIcon = ImageIO.read(new File("Resources/LeftArrow.png")); - BufferedImage UpIcon = ImageIO.read(new File("Resources/UpArrow.png")); - BufferedImage DownIcon = ImageIO.read(new File("Resources/DownArrow.png")); + public JButton buttonSelect; + public JFrame BusFrame; - JButton RightButton = new JButton(new ImageIcon(RightIcon)); - JButton LeftButton = new JButton(new ImageIcon(LeftIcon)); - JButton UpButton = new JButton(new ImageIcon(UpIcon)); - JButton DownButton = new JButton(new ImageIcon(DownIcon)); - JButton CreateButton = new JButton(); - JButton CreateDoubleDeckerBusButton = new JButton(); - JButton buttonStep = new JButton(); + Canvas canvas; + static int pictureBoxWidth = 882; + static int pictureBoxHeight = 453; - CreateDoubleDeckerBusButton.setBounds(198,477,180, 40); - CreateDoubleDeckerBusButton.setText("Создать двухэтажный автобус"); - CreateButton.setText("Создать"); - buttonStep.setBounds(619, 46, 94, 29); - buttonStep.setText("шаг"); - CreateButton.setBounds(12, 477, 180, 40); - RightButton.setBounds(683,487,30,30); - LeftButton.setBounds(611,487,30,30); - UpButton.setBounds(647,451,30,30); - DownButton.setBounds(647,487,30,30); + public Color ChooseColor(JFrame BusFrame){ + JColorChooser dialog = new JColorChooser(); + Color res = JColorChooser.showDialog(BusFrame, "Выберите цвет", Color.WHITE); + return res; + } - BusPanel.add(CreateButton); - BusPanel.add(CreateDoubleDeckerBusButton); - BusPanel.add(RightButton); - BusPanel.add(LeftButton); - BusPanel.add(UpButton); - BusPanel.add(DownButton); - BusPanel.add(comboBoxStrategy); - BusPanel.add(buttonStep); - comboBoxStrategy.setSelectedIndex(-1); + public DrawningBus SelectedBus(){ + return DrawningBus; + } - BusFrame.add(BusPanel, BorderLayout.CENTER); - Random random = new Random(); - CreateButton.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - DrawningBus = new DrawningBus(random.nextInt(100, 300), random.nextDouble(1000, 3000), - Color.getHSBColor(random.nextInt(0, 301), random.nextInt(0, 301), random.nextInt(0, 301)), - BusPanel.getWidth(), BusPanel.getHeight(), BusPanel); - DrawningBus.DrawTransport(); - comboBoxStrategy.enable(true); - } - }); + public void Draw() { + if (DrawningBus == null) { + return; + } + canvas.repaint(); + } - CreateDoubleDeckerBusButton.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - DrawningBus = new DrawningDoubleDeckerBus(random.nextInt(100, 300), random.nextDouble(1000, 3000), - Color.getHSBColor(random.nextInt(0, 301), random.nextInt(0, 301), random.nextInt(0, 301)), - Color.getHSBColor(random.nextInt(0, 301), random.nextInt(0, 301), random.nextInt(0, 301)), - random.nextInt(1, 6), BusPanel.getWidth(), BusPanel.getHeight(), random.nextBoolean(), random.nextBoolean(), random.nextBoolean(), - BusPanel); - DrawningBus.DrawTransport(); - comboBoxStrategy.enable(true); - } - }); + 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() { - @Override - public void actionPerformed(ActionEvent e) { - if (DrawningBus == null) { - return; - } - if (comboBoxStrategy.isEnabled()) { - if (comboBoxStrategy.getSelectedIndex() == 0) { - _abstractStrategy = new MoveToCenter(); - } - else if (comboBoxStrategy.getSelectedIndex() == 1) { - _abstractStrategy = new MoveToBorder(); - } else { - _abstractStrategy = null; - } - if (_abstractStrategy == null) { + buttonStep.addActionListener( + new ActionListener() { + public void actionPerformed(ActionEvent e){ + if (DrawningBus == null) + { return; } - _abstractStrategy.SetData(new DrawningObjectBus(DrawningBus), BusPanel.getWidth(), - BusPanel.getHeight()); - comboBoxStrategy.enable(false); - } - if (_abstractStrategy == null) { - return; - } - _abstractStrategy.MakeStep(); - DrawningBus.DrawTransport(); - if (_abstractStrategy.GetStatus() == Status.Finish) { - comboBoxStrategy.enable(true); - _abstractStrategy = null; + 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) { + if (DrawningBus.EntityBus() == null) { return; } DrawningBus.MoveTransport(DirectionType.Right); - DrawningBus.DrawTransport(); - } - }); - LeftButton.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - if(DrawningBus.EntityBus() == null) - return; - DrawningBus.MoveTransport(DirectionType.Left); - DrawningBus.DrawTransport(); - } - }); - UpButton.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - if(DrawningBus.EntityBus() == null) - return; - DrawningBus.MoveTransport(DirectionType.Up); - DrawningBus.DrawTransport(); - } - }); - DownButton.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - if(DrawningBus.EntityBus() == null) - return; - DrawningBus.MoveTransport(DirectionType.Down); - DrawningBus.DrawTransport(); + 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); } } \ No newline at end of file diff --git a/src/DoubleDeckerBus/Generics/HardGeneric.java b/src/DoubleDeckerBus/Generics/HardGeneric.java new file mode 100644 index 0000000..b9a88fd --- /dev/null +++ b/src/DoubleDeckerBus/Generics/HardGeneric.java @@ -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 { + ArrayList Buses; + ArrayList 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(MaxCountBuses); + Doors = new ArrayList(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); + } +} diff --git a/src/DoubleDeckerBus/Generics/SetGeneric.java b/src/DoubleDeckerBus/Generics/SetGeneric.java new file mode 100644 index 0000000..ef887ee --- /dev/null +++ b/src/DoubleDeckerBus/Generics/SetGeneric.java @@ -0,0 +1,60 @@ +package DoubleDeckerBus.Generics; + +public class SetGeneric{ + private final Object[] _places; + + public int Count; + + public SetGeneric(int count){ + _places = new Object[count]; + Count = count; + } + + public int Insert(T bus){ + return Insert(bus, 0); + } + + public int Insert(T bus, int position){ + if (!(position >= 0 && position < Count)) { + return -1; + } + + if (_places[position] == null){ + _places[position] = bus; + } else { + int place = -1; + + for (int i = position; i < Count; i++) { + if (_places[i] == null) { + place = i; + break; + } + } + + if (place == -1) { + return -1; + } + + for (int i = place - 1; i >= position; i--) { + _places[i + 1] = _places[i]; + } + _places[position] = bus; + } + return position; + } + + public boolean Remove(int position){ + if (!(position >= 0 && position < Count)) { + return false; + } + _places[position] = null; + return true; + } + + public T Get(int position){ + if (!(position >= 0 && position < Count)) { + return null; + } + return (T)_places[position]; + } +} diff --git a/src/DoubleDeckerBus/Generics/TheBusesGenericCollection.java b/src/DoubleDeckerBus/Generics/TheBusesGenericCollection.java new file mode 100644 index 0000000..03da8ee --- /dev/null +++ b/src/DoubleDeckerBus/Generics/TheBusesGenericCollection.java @@ -0,0 +1,77 @@ +package DoubleDeckerBus.Generics; + +import DoubleDeckerBus.DrawningObjects.DrawningBus; +import DoubleDeckerBus.MovementStrategy.IMoveableObject; + +import java.awt.*; +import java.awt.image.BufferedImage; + +public class TheBusesGenericCollection { + private final int _pictureWidth; + + private final int _pictureHeight; + + private final int _placeSizeWidth = 133; + + private final int _placeSizeHeight = 50; + + private final SetGeneric _collection; + + public TheBusesGenericCollection(int picWidth, int picHeight){ + int width = picWidth / _placeSizeWidth; + int height = picHeight / _placeSizeHeight; + _pictureWidth = picWidth; + _pictureHeight = picHeight; + _collection = new SetGeneric(width * height); + } + + public int Insert(T obj){ + if (obj == null) { + return -1; + } + 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 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 + 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(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); + bus.DrawTransport((Graphics2D) g); + } + } + } +} diff --git a/src/DoubleDeckerBus/HardForm.java b/src/DoubleDeckerBus/HardForm.java new file mode 100644 index 0000000..5a84e42 --- /dev/null +++ b/src/DoubleDeckerBus/HardForm.java @@ -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 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(); + } + } + ); + } +} \ No newline at end of file diff --git a/src/DoubleDeckerBus/Main.java b/src/DoubleDeckerBus/Main.java new file mode 100644 index 0000000..2d3022d --- /dev/null +++ b/src/DoubleDeckerBus/Main.java @@ -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(); + } +} \ No newline at end of file diff --git a/src/DoubleDeckerBus/MovementStrategy/MoveToCenter.java b/src/DoubleDeckerBus/MovementStrategy/MoveToCenter.java index bf72659..d45d9a9 100644 --- a/src/DoubleDeckerBus/MovementStrategy/MoveToCenter.java +++ b/src/DoubleDeckerBus/MovementStrategy/MoveToCenter.java @@ -15,9 +15,9 @@ public class MoveToCenter extends AbstractStrategy{ objParams.ObjectMiddleHorizontal - GetStep() <= FieldWidth() / 2)) && ((objParams.ObjectMiddleVertical <= FieldHeight() / 2 && - objParams.ObjectMiddleVertical + GetStep() >= FieldHeight() / 2) || - (objParams.ObjectMiddleVertical >= FieldHeight() / 2 && - objParams.ObjectMiddleVertical - GetStep() <= FieldHeight() / 2)); + objParams.ObjectMiddleVertical + GetStep() >= FieldHeight() / 2) || + (objParams.ObjectMiddleVertical >= FieldHeight() / 2 && + objParams.ObjectMiddleVertical - GetStep() <= FieldHeight() / 2)); } @Override