diff --git a/.idea/PIbd-23_Yunusov.N.N_Trolleybus_Hard.iml b/.idea/PIbd-23_Yunusov.N.N_Trolleybus_Hard.iml index d6ebd48..c90834f 100644 --- a/.idea/PIbd-23_Yunusov.N.N_Trolleybus_Hard.iml +++ b/.idea/PIbd-23_Yunusov.N.N_Trolleybus_Hard.iml @@ -2,7 +2,9 @@ - + + + diff --git a/.idea/misc.xml b/.idea/misc.xml index bcb5da6..30905c1 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,3 @@ - diff --git a/src/BusesGenericCollection.java b/src/BusesGenericCollection.java new file mode 100644 index 0000000..1487d8c --- /dev/null +++ b/src/BusesGenericCollection.java @@ -0,0 +1,64 @@ +import java.awt.*; +public class BusesGenericCollection { + private int _pictureWidth; + private int _pictureHeight; + private final int _placeSizeWidth = 210; + private final int _placeSizeHeight = 135; + private SetGeneric _collection; + + public BusesGenericCollection(int picWidth, int picHeight) { + int width = picWidth / _placeSizeWidth; + int height = picHeight / _placeSizeHeight; + _pictureWidth = picWidth; + _pictureHeight = picHeight; + _collection = new SetGeneric(width * height, (Class) DrawingBus.class); + } + public void showBuses(Graphics2D g){ + drawBackground(g); + drawObjects(g); + } + public boolean insert(T obj) + { + if (obj != null) + return _collection.insert(obj); + return false; + } + public boolean remove(int pos) + { + T obj = _collection.Get(pos); + if (obj != null) + return _collection.remove(pos); + return false; + } + public U GetU(int pos) { + if(_collection.Get(pos) == null) + return null; + return (U)_collection.Get(pos).GetMoveableObject; + } + private void drawBackground(Graphics2D g) { + BasicStroke pen = new BasicStroke(3); + g.setStroke(pen); + for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++) { + for (int j = 0; j < _pictureHeight / _placeSizeHeight + + 1; ++j) { g.drawLine(i * _placeSizeWidth, j * + _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j * + _placeSizeHeight); + } + g.drawLine(i * _placeSizeWidth, 0, i * + _placeSizeWidth, _pictureHeight / _placeSizeHeight * _placeSizeHeight); + } + } + private void drawObjects(Graphics2D g) { + for(int i =0;i <_collection.getCount(); i++) + { + DrawingBus bus =_collection.Get(i); + if (bus != null) { + int inRow = _pictureWidth / _placeSizeWidth; + bus.setPosition(_placeSizeWidth * (inRow - 1) - (i % inRow * _placeSizeWidth), i / inRow * _placeSizeHeight); + //bus.setPosition(_pictureWidth - _placeSizeWidth - (i % inRow * _placeSizeWidth)-8, i / inRow * _placeSizeHeight + 20); + bus.drawTransport(g); + } + } + } +} + diff --git a/src/DrawingBus.java b/src/DrawingBus.java index 67f9467..0209136 100644 --- a/src/DrawingBus.java +++ b/src/DrawingBus.java @@ -134,4 +134,5 @@ public class DrawingBus { graphics2D.setPaint(Color.BLACK); graphics2D.drawRect(_startPosX + 196, _startPosY + 91, 10, 20); } + public IMoveableObject GetMoveableObject = new DrawingObjectBus(this); } diff --git a/src/DrawingDoors.java b/src/DrawingDoors.java index b49182c..0f11d83 100644 --- a/src/DrawingDoors.java +++ b/src/DrawingDoors.java @@ -3,6 +3,18 @@ import java.awt.*; public class DrawingDoors implements IDrawDoors{ private DoorsNumber number; @Override + public int getNumber(){ + int x = 0; + if(number == DoorsNumber.THREE) + x = 3; + if(number == DoorsNumber.FOUR) + x = 4; + if(number == DoorsNumber.FIVE) + x = 5; + return x; + } + public int getType(){return 0;} + @Override public void setNumber(int x){ if(x <= 3) number = DoorsNumber.THREE; diff --git a/src/DrawingDoorsRoundedUp.java b/src/DrawingDoorsRoundedUp.java index 8f12b1f..f2dda50 100644 --- a/src/DrawingDoorsRoundedUp.java +++ b/src/DrawingDoorsRoundedUp.java @@ -3,6 +3,18 @@ import java.awt.*; public class DrawingDoorsRoundedUp implements IDrawDoors{ private DoorsNumber number; @Override + public int getNumber(){ + int x = 0; + if(number == DoorsNumber.THREE) + x = 3; + if(number == DoorsNumber.FOUR) + x = 4; + if(number == DoorsNumber.FIVE) + x = 5; + return x; + } + public int getType(){return 1;} + @Override public void setNumber(int x){ if(x <= 2) number = DoorsNumber.THREE; diff --git a/src/DrawingDoorsRoundedUpAndDown.java b/src/DrawingDoorsRoundedUpAndDown.java index c912f83..be28379 100644 --- a/src/DrawingDoorsRoundedUpAndDown.java +++ b/src/DrawingDoorsRoundedUpAndDown.java @@ -3,6 +3,18 @@ import java.awt.*; public class DrawingDoorsRoundedUpAndDown implements IDrawDoors{ private DoorsNumber number; @Override + public int getNumber(){ + int x = 0; + if(number == DoorsNumber.THREE) + x = 3; + if(number == DoorsNumber.FOUR) + x = 4; + if(number == DoorsNumber.FIVE) + x = 5; + return x; + } + public int getType(){return 2;} + @Override public void setNumber(int x){ if(x <= 2) number = DoorsNumber.THREE; diff --git a/src/FrameBusCollection.java b/src/FrameBusCollection.java new file mode 100644 index 0000000..7952509 --- /dev/null +++ b/src/FrameBusCollection.java @@ -0,0 +1,91 @@ +import javax.swing.*; +import java.awt.*; +import java.io.IOException; + +public class FrameBusCollection extends JFrame { + private BusesGenericCollection buses; + JComponent pictureBoxCollection; + TextField textFieldNumber; + public FrameBusCollection() throws IOException{ + super("Набор автобусов"); + setSize(new Dimension(900,500)); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + createGui(); + buses = new BusesGenericCollection<>(pictureBoxCollection.getWidth(),pictureBoxCollection.getHeight()); + setVisible(true); + } + private void createGui(){ + pictureBoxCollection = new JComponent(){ + public void paintComponent(Graphics graphics){ + super.paintComponent(graphics); + Graphics2D graphics2D = (Graphics2D) graphics; + if (buses != null) buses.showBuses(graphics2D); + super.repaint(); + } + }; + pictureBoxCollection.setSize(new Dimension(700,450)); + JButton buttonAddBus = new JButton("Добавить автобус"); + buttonAddBus.setPreferredSize(new Dimension(160,30)); + textFieldNumber = new TextField(); + JButton buttonRemoveBus = new JButton("Удалить автобус"); + buttonRemoveBus.setPreferredSize(new Dimension(160,30)); + JButton buttonRefreshCollection = new JButton("Обновить коллекцию"); + buttonAddBus.setPreferredSize(new Dimension(160,30)); + buttonAddBus.addActionListener(e -> buttonAddBusClick()); + buttonRemoveBus.addActionListener(e -> buttonRemoveBusClick()); + buttonRefreshCollection.addActionListener(e -> buttonRefreshBusClick()); + JPanel panelCollection = new JPanel(new GridBagLayout()); + GridBagConstraints constraints = new GridBagConstraints(); + constraints.insets.left = constraints.insets.right = 2; + constraints.insets.top = constraints.insets.bottom = 30; + constraints.fill = GridBagConstraints.BOTH; + constraints.gridx = 0; + constraints.gridy = 0; + panelCollection.add(buttonAddBus, constraints); + constraints.gridx = 0; + constraints.gridy = 1; + panelCollection.add(textFieldNumber, constraints); + constraints.gridx = 0; + constraints.gridy = 2; + panelCollection.add(buttonRemoveBus, constraints); + constraints.gridx = 0; + constraints.gridy = 3; + panelCollection.add(buttonRefreshCollection, constraints); + setLayout(new BorderLayout()); + add(panelCollection, BorderLayout.EAST); + add(pictureBoxCollection,BorderLayout.CENTER); + } + private void buttonAddBusClick(){ + FrameTrolleybus form; + try{ + form = new FrameTrolleybus(); + } + catch (IOException e){ + throw new RuntimeException(); + } + form.selectBusButton.addActionListener(e -> { + form.selectBus(); + if (buses.insert(form.getSelectedBus())) { + JOptionPane.showMessageDialog(this, "Объект добавлен"); + pictureBoxCollection.repaint(); + } else { + JOptionPane.showMessageDialog(this, "Не удалось добавить объект"); + } + form.dispose(); + }); + } + private void buttonRemoveBusClick() { + int pos = Integer.parseInt(textFieldNumber.getText()); + if (buses.remove(pos)) + { + JOptionPane.showMessageDialog(this,"Объект удалён"); + } + else + { + JOptionPane.showMessageDialog(this,"Не удалось удалить объект"); + } + } + private void buttonRefreshBusClick(){ + pictureBoxCollection.repaint(); + } +} diff --git a/src/FrameHard.java b/src/FrameHard.java new file mode 100644 index 0000000..9e83f7e --- /dev/null +++ b/src/FrameHard.java @@ -0,0 +1,45 @@ +import javax.swing.*; +import java.awt.*; +import java.util.Random; +public class FrameHard extends JFrame{ + HardGeneric generic; + DrawingBus drawing; + private JComponent pictureBox; + private int pictureBoxWidth = 210; + private int pictureBoxHeight = 165; + public FrameHard(){ + setSize(300, 300); + setLocationRelativeTo(null); + setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); + pictureBox = new JComponent(){ + public void paintComponent(Graphics graphics){ + super.paintComponent(graphics); + Graphics2D graphics2D = (Graphics2D) graphics; + if (drawing != null) drawing.drawTransport(graphics2D); + super.repaint(); + } + }; + pictureBox.setSize(pictureBoxWidth,pictureBoxHeight); + JButton buttonMakeObject = new JButton("Создать объект"); + Random rand = new Random(); + int size = rand.nextInt(1, 10); + generic = new HardGeneric<>(size, size, pictureBoxWidth, pictureBoxHeight); + for(int i = 0; i < size; i++){ + generic.insertBuses(generic.makeRandomBus()); + generic.insertDoors(generic.makeRandomDoor()); + } + buttonMakeObject.addActionListener(e -> { + DrawingBus drawingBus = generic.makeObject(); + drawingBus.setPosition(pictureBoxWidth / 2 - drawingBus.getWidth()/2, + pictureBoxHeight / 2 - drawingBus.getHeight()/2); + drawing = drawingBus; + draw(); + }); + setLayout(new BorderLayout()); + add(pictureBox, BorderLayout.CENTER); + add(buttonMakeObject, BorderLayout.SOUTH); + setVisible(true); + } + void draw(){ + pictureBox.repaint();} +} diff --git a/src/FrameTrolleybus.java b/src/FrameTrolleybus.java index b754968..16138d0 100644 --- a/src/FrameTrolleybus.java +++ b/src/FrameTrolleybus.java @@ -1,5 +1,6 @@ import javax.imageio.ImageIO; import javax.swing.*; +import javax.swing.colorchooser.ColorChooserComponentFactory; import java.awt.*; import java.awt.event.ActionEvent; import java.io.File; @@ -10,6 +11,9 @@ public class FrameTrolleybus extends JFrame { private DrawingBus drawingBus; private AbstractStrategy abstractStrategy; private JComboBox comboBoxStrategy; + public JButton selectBusButton; + private DrawingBus selectedBus; + public DrawingBus getSelectedBus(){ return selectedBus;} private final JComponent pictureBoxTrolleybus; public FrameTrolleybus() throws IOException { super("Троллейбус"); @@ -27,6 +31,7 @@ public class FrameTrolleybus extends JFrame { JButton stepButton = new JButton("Шаг"); JButton createBusButton = new JButton("Создать автобус"); JButton createTrolleybusButton = new JButton("Создать троллейбус"); + selectBusButton = new JButton("Создание"); JButton rightButton = new JButton(new ImageIcon(ImageIO.read(new File("img/right.png")))); JButton leftButton = new JButton(new ImageIcon(ImageIO.read(new File("img/left.png")))); JButton upButton = new JButton(new ImageIcon(ImageIO.read(new File("img/up.png")))); @@ -53,6 +58,9 @@ public class FrameTrolleybus extends JFrame { constraints.gridx = 1; constraints.gridy = 0; createPanel.add(createTrolleybusButton, constraints); + constraints.gridx = 2; + constraints.gridy = 0; + createPanel.add(selectBusButton, constraints); JPanel movementPanel = new JPanel(new GridBagLayout()); JPanel rightPanel = new JPanel(new BorderLayout()); JPanel leftPanel = new JPanel(new BorderLayout()); @@ -81,6 +89,7 @@ public class FrameTrolleybus extends JFrame { constraints.gridx = 0; constraints.gridy = 1; stepPanel.add(stepButton, constraints); + add(pictureBoxTrolleybus); rightPanel.add(stepPanel, BorderLayout.NORTH); leftPanel.add(createPanel, BorderLayout.SOUTH); @@ -91,22 +100,23 @@ public class FrameTrolleybus extends JFrame { } private void buttonCreateTrolleybusClick() { Random random = new Random(); + Color bodyColor = JColorChooser.showDialog(null,"Основной цвет", null); + Color additColor = JColorChooser.showDialog(null,"Основной цвет", null); pictureBoxTrolleybus.setBounds(0,0,getContentPane().getWidth(),getContentPane().getHeight()); drawingBus = new DrawingTrolleybus(random.nextInt(200) + 100, random.nextInt(2000) + 1000, - new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)), - new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)), - random.nextBoolean(), random.nextBoolean(), pictureBoxTrolleybus.getWidth(), pictureBoxTrolleybus.getHeight(), + bodyColor, additColor, random.nextBoolean(), random.nextBoolean(), pictureBoxTrolleybus.getWidth(), pictureBoxTrolleybus.getHeight(), (random.nextInt(3)+1)*2, random.nextInt(3)); drawingBus.setPosition(random.nextInt(90) + 10, random.nextInt(90) + 10); draw(); } private void buttonCreateBusClick(){ Random random = new Random(); + Color bodyColor = JColorChooser.showDialog(null,"Основной цвет", null); pictureBoxTrolleybus.setBounds(0,0,getContentPane().getWidth(),getContentPane().getHeight()); drawingBus = new DrawingBus( random.nextInt(200) + 100, random.nextInt(2000) + 1000, - new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)), + bodyColor, pictureBoxTrolleybus.getWidth(), pictureBoxTrolleybus.getHeight(), (random.nextInt(3)+1)*2, @@ -174,4 +184,9 @@ public class FrameTrolleybus extends JFrame { } pictureBoxTrolleybus.repaint(); } + public void selectBus() { + if (drawingBus == null) { + return; + } + selectedBus = drawingBus;} } \ No newline at end of file diff --git a/src/HardGeneric.java b/src/HardGeneric.java new file mode 100644 index 0000000..1274008 --- /dev/null +++ b/src/HardGeneric.java @@ -0,0 +1,81 @@ +import java.awt.*; +import java.util.Random; +public class HardGeneric { + T[] buses; + U[] doors; + private int count; + private int busesCount; + private int doorsCount; + private int pictureBoxWidth; + private int pictureBoxHeight; + + public HardGeneric(int busesCount, int doorsCount, int width, int height) { + count = 0; + this.busesCount = busesCount; + this.doorsCount = doorsCount; + buses = (T[]) new EntityBus[this.busesCount]; + doors = (U[]) new IDrawDoors[this.doorsCount]; + pictureBoxHeight = height; + pictureBoxWidth = width; + } + + public int insertBuses(T entityBus) { + if (buses[busesCount - 1] != null) + return -1; + for (int i = count - 1; i >= 0; i--) { + buses[i + 1] = buses[i]; + doors[i + 1] = doors[i]; + } + count++; + buses[0] = entityBus; + + return 0; + } + + public int insertDoors(U door) { + if (doors[doorsCount - 1] != null) + return -1; + doors[0] = door; + return 0; + } + + public DrawingBus makeObject() { + Random rand = new Random(); + int indBuses = rand.nextInt(0, count); + int indDoors = rand.nextInt(0, count); + EntityBus entity = buses[indBuses]; + IDrawDoors doors = this.doors[indDoors]; + if (entity instanceof EntityTrolleybus) { + return new DrawingTrolleybus(entity.getSpeed(), entity.getWeight(), entity.getBodyColor(), + ((EntityTrolleybus) entity).getAdditionalColor(), ((EntityTrolleybus) entity).getRoga(), + ((EntityTrolleybus) entity).getBattery(), pictureBoxWidth, pictureBoxHeight, doors.getNumber(), doors.getType()); + + } + return new DrawingBus(entity.getSpeed(), entity.getWeight(), entity.getBodyColor(), + pictureBoxWidth, pictureBoxHeight, doors.getNumber(), doors.getType()); + } + public EntityBus makeRandomBus() { + Random random = new Random(); + EntityBus bus; + switch (random.nextInt(2)) { + case 0 -> bus = new EntityBus(random.nextInt(100, 300), random.nextDouble(1000, 3000), + new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256))); + default -> bus = new EntityTrolleybus(random.nextInt(100, 300), random.nextDouble(1000, 3000), + new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)), + new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)), + random.nextBoolean(), random.nextBoolean()); + } + return bus; + } + public IDrawDoors makeRandomDoor(){ + Random random = new Random(); + IDrawDoors door; + switch (random.nextInt(3)){ + case 1 -> door = new DrawingDoorsRoundedUp(); + case 2 -> door = new DrawingDoorsRoundedUpAndDown(); + default -> door = new DrawingDoors(); + } + door.setNumber((random.nextInt(3) + 1) * 2); + return door; + } +} diff --git a/src/IDrawDoors.java b/src/IDrawDoors.java index 17b7798..8514d67 100644 --- a/src/IDrawDoors.java +++ b/src/IDrawDoors.java @@ -1,6 +1,8 @@ import java.awt.*; public interface IDrawDoors { + int getNumber(); + int getType(); void setNumber(int x); void drawDoors(Graphics2D graphics2D, int _startX, int _startY); } diff --git a/src/Main.java b/src/Main.java index dabb840..8002bc5 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,4 +1,4 @@ import java.io.IOException; public class Main { - public static void main(String[] args) throws IOException { new FrameTrolleybus(); } + public static void main(String[] args) throws IOException { new FrameHard(); } } diff --git a/src/SetGeneric.java b/src/SetGeneric.java new file mode 100644 index 0000000..593e1c5 --- /dev/null +++ b/src/SetGeneric.java @@ -0,0 +1,39 @@ +import java.lang.reflect.Array; + +public class SetGeneric{ + private final T[] places; + public int getCount() {return places.length;} + public SetGeneric(int count, Class type){ + places = (T[]) Array.newInstance(type, count); + } + public boolean insert(T ship){ + return insert(ship, 0); + } + public boolean insert(T ship, int position){ + if (!(position >= 0 && position < places.length)) + return false; + if (places[position] != null) + { + int ind = position; + while (ind < places.length && places[ind] != null) + ind++; + if (ind == places.length) + return false; + for (int i = ind - 1; i >= position; i--) + places[i + 1] = places[i]; + } + places[position] = ship; + return true; + } + public boolean remove(int position){ + if(!(position >= 0 && position < getCount())) + return false; + places[position] = null; + return true; + } + public T Get(int position){ + if(!(position >= 0 && position < getCount())) + return null; + return places[position]; + } +}