From b02f4ac691973fa49c67af0a4cbce144a493d453 Mon Sep 17 00:00:00 2001 From: Yunusov_Niyaz Date: Fri, 10 Nov 2023 00:20:05 +0400 Subject: [PATCH 1/4] Lab2 Done+ --- .idea/PIbd-23_Yunusov.N.N_Trolleybus_Hard.iml | 4 +- .idea/misc.xml | 1 - src/BusesGenericCollection.java | 64 +++++++++++++ src/DrawingBus.java | 1 + src/DrawingDoors.java | 12 +++ src/DrawingDoorsRoundedUp.java | 12 +++ src/DrawingDoorsRoundedUpAndDown.java | 12 +++ src/FrameBusCollection.java | 91 +++++++++++++++++++ src/FrameHard.java | 45 +++++++++ src/FrameTrolleybus.java | 23 ++++- src/HardGeneric.java | 81 +++++++++++++++++ src/IDrawDoors.java | 2 + src/Main.java | 2 +- src/SetGeneric.java | 39 ++++++++ 14 files changed, 382 insertions(+), 7 deletions(-) create mode 100644 src/BusesGenericCollection.java create mode 100644 src/FrameBusCollection.java create mode 100644 src/FrameHard.java create mode 100644 src/HardGeneric.java create mode 100644 src/SetGeneric.java 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]; + } +} -- 2.25.1 From 8cc4261ccb3bb0dc659faff08f71b7875e265fda Mon Sep 17 00:00:00 2001 From: Yunusov_Niyaz Date: Fri, 10 Nov 2023 00:21:29 +0400 Subject: [PATCH 2/4] Lab3 Done+ --- src/HardGeneric.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/HardGeneric.java b/src/HardGeneric.java index 1274008..6c27515 100644 --- a/src/HardGeneric.java +++ b/src/HardGeneric.java @@ -8,7 +8,6 @@ public class HardGeneric { private int doorsCount; private int pictureBoxWidth; private int pictureBoxHeight; - public HardGeneric(int busesCount, int doorsCount, int width, int height) { count = 0; this.busesCount = busesCount; -- 2.25.1 From f7be8b6d55a4d846df5d7d8d423cb10c3ff58810 Mon Sep 17 00:00:00 2001 From: Yunusov_Niyaz Date: Sat, 18 Nov 2023 02:26:55 +0400 Subject: [PATCH 3/4] Lab3 Done done --- .idea/inspectionProfiles/Project_Default.xml | 10 ++++++ src/BusesGenericCollection.java | 4 +-- src/DrawingBus.java | 2 +- src/FrameBusCollection.java | 2 +- src/FrameHard.java | 12 +++---- src/FrameTrolleybus.java | 3 +- src/HardGeneric.java | 38 ++++++++++---------- src/SetGeneric.java | 8 ++--- 8 files changed, 41 insertions(+), 38 deletions(-) create mode 100644 .idea/inspectionProfiles/Project_Default.xml diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..dfb9496 --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,10 @@ + + + + \ No newline at end of file diff --git a/src/BusesGenericCollection.java b/src/BusesGenericCollection.java index 1487d8c..e6d8793 100644 --- a/src/BusesGenericCollection.java +++ b/src/BusesGenericCollection.java @@ -5,7 +5,6 @@ public class BusesGenericCollection _collection; - public BusesGenericCollection(int picWidth, int picHeight) { int width = picWidth / _placeSizeWidth; int height = picHeight / _placeSizeHeight; @@ -33,7 +32,7 @@ public class BusesGenericCollection buses; JComponent pictureBoxCollection; TextField textFieldNumber; - public FrameBusCollection() throws IOException{ + public FrameBusCollection(){ super("Набор автобусов"); setSize(new Dimension(900,500)); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); diff --git a/src/FrameHard.java b/src/FrameHard.java index 9e83f7e..f5f9198 100644 --- a/src/FrameHard.java +++ b/src/FrameHard.java @@ -4,14 +4,14 @@ import java.util.Random; public class FrameHard extends JFrame{ HardGeneric generic; DrawingBus drawing; - private JComponent pictureBox; + private JComponent pictureBoxHard; private int pictureBoxWidth = 210; private int pictureBoxHeight = 165; public FrameHard(){ setSize(300, 300); setLocationRelativeTo(null); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); - pictureBox = new JComponent(){ + pictureBoxHard = new JComponent(){ public void paintComponent(Graphics graphics){ super.paintComponent(graphics); Graphics2D graphics2D = (Graphics2D) graphics; @@ -19,7 +19,7 @@ public class FrameHard extends JFrame{ super.repaint(); } }; - pictureBox.setSize(pictureBoxWidth,pictureBoxHeight); + pictureBoxHard.setSize(pictureBoxWidth,pictureBoxHeight); JButton buttonMakeObject = new JButton("Создать объект"); Random rand = new Random(); int size = rand.nextInt(1, 10); @@ -33,13 +33,11 @@ public class FrameHard extends JFrame{ drawingBus.setPosition(pictureBoxWidth / 2 - drawingBus.getWidth()/2, pictureBoxHeight / 2 - drawingBus.getHeight()/2); drawing = drawingBus; - draw(); + pictureBoxHard.repaint(); }); setLayout(new BorderLayout()); - add(pictureBox, BorderLayout.CENTER); + add(pictureBoxHard, BorderLayout.CENTER); add(buttonMakeObject, BorderLayout.SOUTH); setVisible(true); } - void draw(){ - pictureBox.repaint();} } diff --git a/src/FrameTrolleybus.java b/src/FrameTrolleybus.java index 16138d0..1c39920 100644 --- a/src/FrameTrolleybus.java +++ b/src/FrameTrolleybus.java @@ -6,7 +6,6 @@ import java.awt.event.ActionEvent; import java.io.File; import java.io.IOException; import java.util.Random; - public class FrameTrolleybus extends JFrame { private DrawingBus drawingBus; private AbstractStrategy abstractStrategy; @@ -101,7 +100,7 @@ public class FrameTrolleybus extends JFrame { private void buttonCreateTrolleybusClick() { Random random = new Random(); Color bodyColor = JColorChooser.showDialog(null,"Основной цвет", null); - Color additColor = JColorChooser.showDialog(null,"Основной цвет", null); + Color additColor = JColorChooser.showDialog(null,"Дополнительный цвет", null); pictureBoxTrolleybus.setBounds(0,0,getContentPane().getWidth(),getContentPane().getHeight()); drawingBus = new DrawingTrolleybus(random.nextInt(200) + 100, random.nextInt(2000) + 1000, bodyColor, additColor, random.nextBoolean(), random.nextBoolean(), pictureBoxTrolleybus.getWidth(), pictureBoxTrolleybus.getHeight(), diff --git a/src/HardGeneric.java b/src/HardGeneric.java index 6c27515..1620ad1 100644 --- a/src/HardGeneric.java +++ b/src/HardGeneric.java @@ -3,55 +3,53 @@ import java.util.Random; public class HardGeneric { T[] buses; U[] doors; - private int count; - private int busesCount; - private int doorsCount; + private int busesNumber; + private int doorsNumber; 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]; + busesNumber = 0; + doorsNumber = 0; + buses = (T[]) new EntityBus[busesCount]; + doors = (U[]) new IDrawDoors[doorsCount]; pictureBoxHeight = height; pictureBoxWidth = width; } public int insertBuses(T entityBus) { - if (buses[busesCount - 1] != null) + if (buses[buses.length - 1] != null) return -1; - for (int i = count - 1; i >= 0; i--) { + for (int i = busesNumber - 1; i >= 0; i--) { buses[i + 1] = buses[i]; - doors[i + 1] = doors[i]; } - count++; + busesNumber++; buses[0] = entityBus; - return 0; } public int insertDoors(U door) { - if (doors[doorsCount - 1] != null) + if (doors[doors.length - 1] != null) return -1; + for (int i = doorsNumber - 1; i >= 0; i--) { + doors[i + 1] = doors[i]; + } + doorsNumber++; doors[0] = door; return 0; } public DrawingBus makeObject() { Random rand = new Random(); - int indBuses = rand.nextInt(0, count); - int indDoors = rand.nextInt(0, count); - EntityBus entity = buses[indBuses]; - IDrawDoors doors = this.doors[indDoors]; + EntityBus entity = buses[rand.nextInt(0, busesNumber)]; + IDrawDoors door = doors[rand.nextInt(0, doorsNumber)]; if (entity instanceof EntityTrolleybus) { return new DrawingTrolleybus(entity.getSpeed(), entity.getWeight(), entity.getBodyColor(), ((EntityTrolleybus) entity).getAdditionalColor(), ((EntityTrolleybus) entity).getRoga(), - ((EntityTrolleybus) entity).getBattery(), pictureBoxWidth, pictureBoxHeight, doors.getNumber(), doors.getType()); + ((EntityTrolleybus) entity).getBattery(), pictureBoxWidth, pictureBoxHeight, door.getNumber(), door.getType()); } return new DrawingBus(entity.getSpeed(), entity.getWeight(), entity.getBodyColor(), - pictureBoxWidth, pictureBoxHeight, doors.getNumber(), doors.getType()); + pictureBoxWidth, pictureBoxHeight, door.getNumber(), door.getType()); } public EntityBus makeRandomBus() { Random random = new Random(); diff --git a/src/SetGeneric.java b/src/SetGeneric.java index 593e1c5..127715a 100644 --- a/src/SetGeneric.java +++ b/src/SetGeneric.java @@ -6,10 +6,10 @@ public class SetGeneric{ 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 bus){ + return insert(bus, 0); } - public boolean insert(T ship, int position){ + public boolean insert(T bus, int position){ if (!(position >= 0 && position < places.length)) return false; if (places[position] != null) @@ -22,7 +22,7 @@ public class SetGeneric{ for (int i = ind - 1; i >= position; i--) places[i + 1] = places[i]; } - places[position] = ship; + places[position] = bus; return true; } public boolean remove(int position){ -- 2.25.1 From 68312fa3c39f3b37ae33e7ab8a586ab4a3d07a5b Mon Sep 17 00:00:00 2001 From: Yunusov_Niyaz Date: Sat, 18 Nov 2023 02:36:55 +0400 Subject: [PATCH 4/4] Lab3 Done done+ --- src/DrawingDoors.java | 3 --- src/DrawingDoorsRoundedUp.java | 3 --- src/DrawingDoorsRoundedUpAndDown.java | 3 --- 3 files changed, 9 deletions(-) diff --git a/src/DrawingDoors.java b/src/DrawingDoors.java index 0f11d83..2b651e8 100644 --- a/src/DrawingDoors.java +++ b/src/DrawingDoors.java @@ -2,7 +2,6 @@ import java.awt.*; public class DrawingDoors implements IDrawDoors{ private DoorsNumber number; - @Override public int getNumber(){ int x = 0; if(number == DoorsNumber.THREE) @@ -14,7 +13,6 @@ public class DrawingDoors implements IDrawDoors{ return x; } public int getType(){return 0;} - @Override public void setNumber(int x){ if(x <= 3) number = DoorsNumber.THREE; @@ -23,7 +21,6 @@ public class DrawingDoors implements IDrawDoors{ if(x >= 5) number = DoorsNumber.FIVE; } - @Override public void drawDoors(Graphics2D graphics2D, int _startX, int _startY){ graphics2D.fillRect(_startX+52, _startY+81, 25, 40); graphics2D.fillRect(_startX+85, _startY+81, 25, 40); diff --git a/src/DrawingDoorsRoundedUp.java b/src/DrawingDoorsRoundedUp.java index f2dda50..d262909 100644 --- a/src/DrawingDoorsRoundedUp.java +++ b/src/DrawingDoorsRoundedUp.java @@ -2,7 +2,6 @@ import java.awt.*; public class DrawingDoorsRoundedUp implements IDrawDoors{ private DoorsNumber number; - @Override public int getNumber(){ int x = 0; if(number == DoorsNumber.THREE) @@ -14,7 +13,6 @@ public class DrawingDoorsRoundedUp implements IDrawDoors{ return x; } public int getType(){return 1;} - @Override public void setNumber(int x){ if(x <= 2) number = DoorsNumber.THREE; @@ -23,7 +21,6 @@ public class DrawingDoorsRoundedUp implements IDrawDoors{ if(x >= 6) number = DoorsNumber.FIVE; } - @Override public void drawDoors(Graphics2D graphics2D, int _startX, int _startY){ graphics2D.fillRect(_startX+52, _startY+86, 25, 35); graphics2D.fillOval(_startX+52, _startY+81, 25, 12); diff --git a/src/DrawingDoorsRoundedUpAndDown.java b/src/DrawingDoorsRoundedUpAndDown.java index be28379..fbc1d76 100644 --- a/src/DrawingDoorsRoundedUpAndDown.java +++ b/src/DrawingDoorsRoundedUpAndDown.java @@ -2,7 +2,6 @@ import java.awt.*; public class DrawingDoorsRoundedUpAndDown implements IDrawDoors{ private DoorsNumber number; - @Override public int getNumber(){ int x = 0; if(number == DoorsNumber.THREE) @@ -14,7 +13,6 @@ public class DrawingDoorsRoundedUpAndDown implements IDrawDoors{ return x; } public int getType(){return 2;} - @Override public void setNumber(int x){ if(x <= 2) number = DoorsNumber.THREE; @@ -23,7 +21,6 @@ public class DrawingDoorsRoundedUpAndDown implements IDrawDoors{ if(x >= 6) number = DoorsNumber.FIVE; } - @Override public void drawDoors(Graphics2D graphics2D, int _startX, int _startY){ graphics2D.fillRect(_startX+52, _startY+86, 25, 30); graphics2D.fillOval(_startX+52, _startY+81, 25, 12); -- 2.25.1