From c1a5d23a3686fdbab0397ded4744544eccad59e4 Mon Sep 17 00:00:00 2001 From: Timourka Date: Sun, 22 Oct 2023 22:05:13 +0400 Subject: [PATCH 1/5] addGenerics --- laba1Loco/DrawingTrain.java | 2 + laba1Loco/SetGeneric.java | 90 +++++++++++++++++ laba1Loco/TrainsGenericCollection.java | 128 +++++++++++++++++++++++++ 3 files changed, 220 insertions(+) create mode 100644 laba1Loco/SetGeneric.java create mode 100644 laba1Loco/TrainsGenericCollection.java diff --git a/laba1Loco/DrawingTrain.java b/laba1Loco/DrawingTrain.java index 6fbf441..86c220a 100644 --- a/laba1Loco/DrawingTrain.java +++ b/laba1Loco/DrawingTrain.java @@ -7,6 +7,8 @@ import javax.swing.Timer; import java.awt.event.*; public class DrawingTrain { + public IMoveableObject GetMoveableObject() { return new DrawningObjectTrain(this);} + protected IWheelDrawing wheelDrawing; /// /// Класс-сущность diff --git a/laba1Loco/SetGeneric.java b/laba1Loco/SetGeneric.java new file mode 100644 index 0000000..04d1c55 --- /dev/null +++ b/laba1Loco/SetGeneric.java @@ -0,0 +1,90 @@ +package laba1Loco; + +public class SetGeneric { + /// + /// Массив объектов, которые храним + /// + private Object[] _places; + /// + /// Количество объектов в массиве + /// + public int Count; + /// + /// Конструктор + /// + /// + public SetGeneric(int count) + { + _places = new Object[count]; + Count = _places.length; + } + /// + /// Добавление объекта в набор + /// + /// Добавляемый поезд + /// + public int Insert(T train) + { + int i = 0; + for (;i < _places.length; i++) + { + if (_places[i] == null) + break; + } + if (i == _places.length) + return -1; + for (; i > 0; i--) + { + _places[i] = _places[i - 1]; + } + _places[i] = train; + return i; + } + /// + /// Добавление объекта в набор на конкретную позицию + /// + /// Добавляемый поезд + /// Позиция + /// + public boolean Insert(T train, int position) + { + if (position < 0 || position >= _places.length) + return false; + for (; position < _places.length; position++) + { + if (_places[position] == null) + break; + } + if (position == _places.length) + return false; + for (; position > 0; position--) + { + _places[position] = _places[position - 1]; + } + _places[position] = train; + return true; + } + /// + /// Удаление объекта из набора с конкретной позиции + /// + /// + /// + public boolean Remove(int position) + { + if (position < 0 || position >= _places.length) + return false; + _places[position] = null; + return true; + } + /// + /// Получение объекта из набора по позиции + /// + /// + /// + public T Get(int position) + { + if (position < 0 || position >= _places.length) + return null; + return (T)_places[position]; + } +} diff --git a/laba1Loco/TrainsGenericCollection.java b/laba1Loco/TrainsGenericCollection.java new file mode 100644 index 0000000..f703194 --- /dev/null +++ b/laba1Loco/TrainsGenericCollection.java @@ -0,0 +1,128 @@ +package laba1Loco; + +import java.awt.Color; +import java.awt.Graphics2D; +import java.awt.image.BufferedImage; + +public class TrainsGenericCollection{ + /// + /// Ширина окна прорисовки + /// + private int _pictureWidth; + /// + /// Высота окна прорисовки + /// + private int _pictureHeight; + /// + /// Размер занимаемого объектом места (ширина) + /// + private int _placeSizeWidth = 210; + /// + /// Размер занимаемого объектом места (высота) + /// + private int _placeSizeHeight = 90; + /// + /// Набор объектов + /// + private SetGeneric _collection; + /// + /// Конструктор + /// + /// + /// + public TrainsGenericCollection(int picWidth, int picHeight) + { + int width = picWidth / _placeSizeWidth; + int height = picHeight / _placeSizeHeight; + _pictureWidth = picWidth; + _pictureHeight = picHeight; + _collection = new SetGeneric(width * height); + } + /// + /// Перегрузка оператора сложения + /// + /// + /// + /// + public void Add(T obj) + { + if (obj == null) + { + return ; + } + _collection.Insert(obj); + } + /// + /// Перегрузка оператора вычитания + /// + /// + /// + /// + public T remove(int pos) + { + T obj = _collection.Get(pos); + if (obj != null) + { + _collection.Remove(pos); + } + return obj; + } + /// + /// Получение объекта IMoveableObject + /// + /// + /// + public U GetU(int pos) + { + return (U)_collection.Get(pos).GetMoveableObject(); + } + /// + /// Вывод всего набора объектов + /// + /// + public BufferedImage ShowTrains() + { + BufferedImage bmp = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_ARGB); + Graphics2D g = bmp.createGraphics(); + DrawBackground(g); + DrawObjects(g); + g.dispose(); + return bmp; + } + /// + /// Метод отрисовки фона + /// + /// + private void DrawBackground(Graphics2D 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(Graphics2D g) + { + for (int i = 0; i < _collection.Count; i++) + { + T t = _collection.Get(i); + if (t != null) + { + t.SetPosition((i % (_pictureWidth / _placeSizeWidth)) * _placeSizeWidth, (i / (_pictureWidth / _placeSizeWidth)) * _placeSizeHeight); + if (t instanceof DrawingLoco) + ((DrawingLoco) t).DrawTransport(g); + else + t.DrawTransport(g); + } + } + } +} -- 2.25.1 From 8562c126a23e8f37811d150025afa6bd83248f9b Mon Sep 17 00:00:00 2001 From: Timourka Date: Sun, 22 Oct 2023 23:41:58 +0400 Subject: [PATCH 2/5] Form1 --- laba1Loco/FormTrain.java | 48 +++++++++++++++++++++++++++++++++++----- 1 file changed, 43 insertions(+), 5 deletions(-) diff --git a/laba1Loco/FormTrain.java b/laba1Loco/FormTrain.java index bd1c295..795237f 100644 --- a/laba1Loco/FormTrain.java +++ b/laba1Loco/FormTrain.java @@ -7,6 +7,8 @@ import javax.swing.Timer; import java.awt.event.*; public class FormTrain{ + public DrawingTrain SelectedTrain; + public boolean DialogResult = false; private DrawingTrain _drawingTrain; private AbstractStrategy abstractStrategy; Canvas canv; @@ -18,11 +20,14 @@ public class FormTrain{ } public FormTrain(){ + SelectedTrain = null; JFrame w=new JFrame ("Loco"); JButton buttonCreate = new JButton("create"); - JButton buttonCreateLocomotive = new JButton("createLocomotive"); + JButton buttonCreateLocomotive = new JButton("create locomotive"); JButton buttonStrategysStep = new JButton("strategys step"); - JComboBox comboBoxStrategy = new JComboBox( + JButton buttonSelectTrain = new JButton("select train"); + + JComboBox comboBoxStrategy = new JComboBox( new String[]{ "к центру", "к краю", @@ -51,6 +56,15 @@ public class FormTrain{ right.setContentAreaFilled(false); right.setName("right"); right.setIcon(new ImageIcon("D:\\Coffee\\PIbd-21_Kouvshinoff_T._A._WarmlyLocomotive._Harder\\laba1Loco\\images\\arowR340x259.png")); + buttonSelectTrain.addActionListener( + new ActionListener() { + public void actionPerformed(ActionEvent e){ + SelectedTrain = _drawingTrain; + DialogResult = true; + w.dispose(); + } + } + ); buttonStrategysStep.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e){ @@ -100,12 +114,26 @@ public class FormTrain{ public void actionPerformed(ActionEvent e){ System.out.println(e.getActionCommand()); Random random = new Random(); + Color color = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)); + // Вызываем диалоговое окно выбора цвета + Color selectedColor = JColorChooser.showDialog(w, "Выберите цвет", Color.WHITE); + if (selectedColor != null) + { + color = selectedColor; + } + Color color2 = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)); + // Вызываем диалоговое окно выбора цвета + selectedColor = JColorChooser.showDialog(w, "Выберите цвет", Color.WHITE); + if (selectedColor != null) + { + color2 = selectedColor; + } _drawingTrain = new DrawingLoco( random.nextInt(100, 300), random.nextInt(1000, 3000), - new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)), + color, random.nextInt(2, 5), - new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)), + color2, random.nextInt(0, 2) == 1, random.nextInt(0, 2) == 1, random.nextInt(0, 2) == 1, @@ -122,10 +150,17 @@ public class FormTrain{ public void actionPerformed(ActionEvent e){ System.out.println(e.getActionCommand()); Random random = new Random(); + Color color = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)); + // Вызываем диалоговое окно выбора цвета + Color selectedColor = JColorChooser.showDialog(w, "Выберите цвет", Color.WHITE); + if (selectedColor != null) + { + color = selectedColor; + } _drawingTrain = new DrawingTrain( random.nextInt(100, 300), random.nextInt(1000, 3000), - new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)), + color, random.nextInt(2, 5), pictureBoxWidth, pictureBoxHeight); @@ -177,6 +212,8 @@ public class FormTrain{ right.setBounds(940, 520, 40, 40); comboBoxStrategy.setBounds(pictureBoxWidth - 150, 20, 150, 20); buttonStrategysStep.setBounds(pictureBoxWidth - 150, 45, 150, 20); + buttonSelectTrain.setBounds(pictureBoxWidth/2, 540, 150, 20); + w.add(buttonSelectTrain); w.add(canv); w.add(buttonCreate); w.add(buttonCreateLocomotive); @@ -186,6 +223,7 @@ public class FormTrain{ w.add(right); w.add(comboBoxStrategy); w.add(buttonStrategysStep); + w.setVisible(true); } } class Canvas extends JComponent{ -- 2.25.1 From d94c613ddfa0c637d37f622214ea5bc5eb4336a2 Mon Sep 17 00:00:00 2001 From: Timourka Date: Mon, 23 Oct 2023 18:05:55 +0400 Subject: [PATCH 3/5] base --- laba1Loco/FormTrain.java | 46 ++++----- laba1Loco/FormTrainCollecltion.java | 130 +++++++++++++++++++++++++ laba1Loco/Main.java | 2 +- laba1Loco/TrainsGenericCollection.java | 6 +- 4 files changed, 154 insertions(+), 30 deletions(-) create mode 100644 laba1Loco/FormTrainCollecltion.java diff --git a/laba1Loco/FormTrain.java b/laba1Loco/FormTrain.java index 795237f..4741612 100644 --- a/laba1Loco/FormTrain.java +++ b/laba1Loco/FormTrain.java @@ -7,13 +7,29 @@ import javax.swing.Timer; import java.awt.event.*; public class FormTrain{ + class Canvas extends JComponent{ + public DrawingTrain _drawingTrain; + public Canvas(){ + } + public void paintComponent (Graphics g){ + if (_drawingTrain == null){ + return; + } + super.paintComponents (g) ; + Graphics2D g2d = (Graphics2D)g; + _drawingTrain.DrawTransport(g2d); + super.repaint(); + } + } public DrawingTrain SelectedTrain; public boolean DialogResult = false; - private DrawingTrain _drawingTrain; + public DrawingTrain _drawingTrain; private AbstractStrategy abstractStrategy; Canvas canv; static int pictureBoxWidth = 980; static int pictureBoxHeight = 580; + public JButton buttonSelectTrain; + public JFrame w; public void Draw(){ canv.repaint(); @@ -21,11 +37,11 @@ public class FormTrain{ public FormTrain(){ SelectedTrain = null; - JFrame w=new JFrame ("Loco"); + w = new JFrame ("Loco"); JButton buttonCreate = new JButton("create"); JButton buttonCreateLocomotive = new JButton("create locomotive"); JButton buttonStrategysStep = new JButton("strategys step"); - JButton buttonSelectTrain = new JButton("select train"); + buttonSelectTrain = new JButton("select train"); JComboBox comboBoxStrategy = new JComboBox( new String[]{ @@ -56,15 +72,7 @@ public class FormTrain{ right.setContentAreaFilled(false); right.setName("right"); right.setIcon(new ImageIcon("D:\\Coffee\\PIbd-21_Kouvshinoff_T._A._WarmlyLocomotive._Harder\\laba1Loco\\images\\arowR340x259.png")); - buttonSelectTrain.addActionListener( - new ActionListener() { - public void actionPerformed(ActionEvent e){ - SelectedTrain = _drawingTrain; - DialogResult = true; - w.dispose(); - } - } - ); + buttonStrategysStep.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e){ @@ -225,18 +233,4 @@ public class FormTrain{ w.add(buttonStrategysStep); w.setVisible(true); } -} -class Canvas extends JComponent{ - public DrawingTrain _drawingTrain; - public Canvas(){ - } - public void paintComponent (Graphics g){ - if (_drawingTrain == null){ - return; - } - super.paintComponents (g) ; - Graphics2D g2d = (Graphics2D)g; - _drawingTrain.DrawTransport(g2d); - super.repaint(); - } } \ No newline at end of file diff --git a/laba1Loco/FormTrainCollecltion.java b/laba1Loco/FormTrainCollecltion.java new file mode 100644 index 0000000..e4ab05d --- /dev/null +++ b/laba1Loco/FormTrainCollecltion.java @@ -0,0 +1,130 @@ +package laba1Loco; + +import java.awt.Graphics; +import java.awt.Graphics2D; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +import javax.swing.JButton; +import javax.swing.JComponent; +import javax.swing.JFrame; +import javax.swing.JOptionPane; +import javax.swing.JTextField; + +public class FormTrainCollecltion { + class Canvas extends JComponent{ + public TrainsGenericCollection _trains; + public Canvas(){ + } + public void paintComponent (Graphics g){ + super.paintComponent(g); + if (_trains.ShowTrains() != null) { + g.drawImage(_trains.ShowTrains(), 0, 0, this); + } + super.repaint(); + } + } + Canvas canv; + static int pictureBoxWidth = 860; + static int pictureBoxHeight = 580; + private TrainsGenericCollection _trains; + public void Draw(){ + canv.repaint(); + } + FormTrainCollecltion(){ + canv = new Canvas(); + JFrame w = new JFrame ("TrainCollecltion"); + _trains = new TrainsGenericCollection(pictureBoxWidth, pictureBoxHeight); + canv._trains = _trains; + + JButton ButtonAddTrain = new JButton("ButtonAddTrain"); + ButtonAddTrain.addActionListener( + new ActionListener() { + public void actionPerformed(ActionEvent e){ + FormTrain form = new FormTrain(); + form.buttonSelectTrain.addActionListener( + new ActionListener() { + public void actionPerformed(ActionEvent e){ + if (_trains.Add(form._drawingTrain) != -1) + { + JOptionPane.showMessageDialog(null, "Объект добавлен", "Информация", JOptionPane.INFORMATION_MESSAGE); + System.out.println("Объект добавлен"); + Draw(); + } + else + { + JOptionPane.showMessageDialog(null, "Не удалось добавить объект", "Информация", JOptionPane.INFORMATION_MESSAGE); + System.out.println("Не удалось добавить объект"); + } + form.w.dispose(); + } + } + ); + } + } + ); + + JTextField TextBoxNumber = new JTextField(); + JButton ButtonRemoveTrain = new JButton("ButtonRemoveTrain"); + ButtonRemoveTrain.addActionListener( + new ActionListener() { + public void actionPerformed(ActionEvent e){ + if (JOptionPane.showConfirmDialog(null, "Удалить объект?", "Удаление", JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION) + { + return; + } + for (char it : TextBoxNumber.getText().toCharArray()) + if (it < '0' || it > '9') + { + JOptionPane.showMessageDialog(null, "Не удалось удалить объект", "Информация", JOptionPane.INFORMATION_MESSAGE); + System.out.println("Не удалось удалить объект"); + return; + } + if (TextBoxNumber.getText().length() == 0) + { + JOptionPane.showMessageDialog(null, "Не удалось удалить объект", "Информация", JOptionPane.INFORMATION_MESSAGE); + System.out.println("Не удалось удалить объект"); + return; + } + + int pos = Integer.parseInt(TextBoxNumber.getText()); + if (_trains.remove(pos) != null) + { + JOptionPane.showMessageDialog(null, "Объект удален", "Информация", JOptionPane.INFORMATION_MESSAGE); + System.out.println("Объект удален"); + Draw(); + } + else + { + JOptionPane.showMessageDialog(null, "Не удалось удалить объект", "Информация", JOptionPane.INFORMATION_MESSAGE); + System.out.println("Не удалось удалить объект"); + } + } + } + ); + + JButton ButtonRefreshCollection = new JButton("ButtonRefreshCollection"); + ButtonRefreshCollection.addActionListener( + new ActionListener() { + public void actionPerformed(ActionEvent e){ + Draw(); + } + } + ); + + w.setSize (1000, 600); + w.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); + w.setLayout(null); + canv.setBounds(0, 0, pictureBoxWidth, pictureBoxHeight); + ButtonAddTrain.setBounds(pictureBoxWidth, 0, 120, 20); + TextBoxNumber.setBounds(pictureBoxWidth, 30, 120, 20); + ButtonRemoveTrain.setBounds(pictureBoxWidth, 60, 120, 20); + ButtonRefreshCollection.setBounds(pictureBoxWidth, 90, 120, 20); + w.add(canv); + w.add(ButtonAddTrain); + w.add(ButtonRemoveTrain); + w.add(ButtonRefreshCollection); + w.add(TextBoxNumber); + w.setVisible(true); + } +} diff --git a/laba1Loco/Main.java b/laba1Loco/Main.java index 3367941..8b0bf6c 100644 --- a/laba1Loco/Main.java +++ b/laba1Loco/Main.java @@ -2,6 +2,6 @@ package laba1Loco; public class Main{ public static void main(String[] args) { - FormTrain formTrain = new FormTrain(); + FormTrainCollecltion formTrainCollecltion = new FormTrainCollecltion(); } } \ No newline at end of file diff --git a/laba1Loco/TrainsGenericCollection.java b/laba1Loco/TrainsGenericCollection.java index f703194..3f652ed 100644 --- a/laba1Loco/TrainsGenericCollection.java +++ b/laba1Loco/TrainsGenericCollection.java @@ -44,13 +44,13 @@ public class TrainsGenericCollection /// /// - public void Add(T obj) + public int Add(T obj) { if (obj == null) { - return ; + return -1; } - _collection.Insert(obj); + return _collection.Insert(obj); } /// /// Перегрузка оператора вычитания -- 2.25.1 From 9ae0458bb267b5e58f25188ea8188c8366c66775 Mon Sep 17 00:00:00 2001 From: Timourka Date: Mon, 23 Oct 2023 21:52:50 +0400 Subject: [PATCH 4/5] DopHarder --- laba1Loco/DrawingLoco.java | 6 +++ laba1Loco/DrawingTrain.java | 10 +++++ laba1Loco/Form4GenericDopClass.java | 62 ++++++++++++++++++++++++++ laba1Loco/FormTrain.java | 3 +- laba1Loco/FormTrainCollecltion.java | 13 +++++- laba1Loco/GenericDopClass.java | 67 +++++++++++++++++++++++++++++ 6 files changed, 158 insertions(+), 3 deletions(-) create mode 100644 laba1Loco/Form4GenericDopClass.java create mode 100644 laba1Loco/GenericDopClass.java diff --git a/laba1Loco/DrawingLoco.java b/laba1Loco/DrawingLoco.java index 4b9aea3..0a03b88 100644 --- a/laba1Loco/DrawingLoco.java +++ b/laba1Loco/DrawingLoco.java @@ -26,6 +26,12 @@ public class DrawingLoco extends DrawingTrain{ EntityTrain = new EntityLoco(speed, weight, bodyColor, _numWheel, additionalColor, tube, fuelTank, locoLine); _locoWidth = ((EntityLoco)EntityTrain).FuelTank ? 169 : 83; } + // конструктор для 3 сложной лабы + public DrawingLoco(EntityLoco train, IWheelDrawing _wheelDrawing, int width, int height ){ + super(train, _wheelDrawing, width, height); + if (height < _locoHeight || width < _locoWidth) + return; + } /// /// Установка позиции /// diff --git a/laba1Loco/DrawingTrain.java b/laba1Loco/DrawingTrain.java index 86c220a..db5143c 100644 --- a/laba1Loco/DrawingTrain.java +++ b/laba1Loco/DrawingTrain.java @@ -70,6 +70,16 @@ public class DrawingTrain { } wheelDrawing.setNumWheel(_numWheel); } + // конструктор для 3 сложной лабы + public DrawingTrain(EntityTrain train, IWheelDrawing _wheelDrawing, int width, int height ){ + if (height < _locoHeight || width < _locoWidth) + return; + _pictureWidth = width; + _pictureHeight = height; + EntityTrain = train; + wheelDrawing = _wheelDrawing; + wheelDrawing.setNumWheel(EntityTrain.numWheel); + } /// /// Установка позиции /// diff --git a/laba1Loco/Form4GenericDopClass.java b/laba1Loco/Form4GenericDopClass.java new file mode 100644 index 0000000..20979b3 --- /dev/null +++ b/laba1Loco/Form4GenericDopClass.java @@ -0,0 +1,62 @@ +package laba1Loco; + +import java.awt.Color; +import java.awt.Graphics; +import java.awt.Graphics2D; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +import javax.swing.JButton; +import javax.swing.JComponent; +import javax.swing.JFrame; + +public class Form4GenericDopClass extends JFrame { + static int pictureBoxWidth = 980; + static int pictureBoxHeight = 560; + public DrawingTrain _drawingTrain; + private class Canvas extends JComponent{ + public Canvas(){ + } + public void paintComponent (Graphics g){ + if (_drawingTrain == null){ + return; + } + super.paintComponents (g) ; + Graphics2D g2d = (Graphics2D)g; + _drawingTrain.SetPosition(50, 50); + _drawingTrain.DrawTransport(g2d); + super.repaint(); + } + } + GenericDopClass genericDopClass; + public Form4GenericDopClass(){ + _drawingTrain = null; + Canvas canv = new Canvas(); + setSize (1000, 600); + setLayout(null); + canv.setBounds(0,0,pictureBoxWidth, pictureBoxHeight); + + genericDopClass = new GenericDopClass<>(100, 100, pictureBoxWidth, pictureBoxHeight); + genericDopClass.addTrain(new EntityTrain(100, 100, Color.BLUE, 2)); + genericDopClass.addTrain(new EntityTrain(100, 100, Color.RED, 3)); + genericDopClass.addTrain(new EntityTrain(100, 100, Color.GRAY, 4)); + genericDopClass.addTrain(new EntityLoco(100, 100, Color.BLUE, 2, Color.ORANGE, true, true, true)); + genericDopClass.addWheel(new WheelDrawingDavidStar()); + genericDopClass.addWheel(new WheelDrawingBalls()); + + JButton creatButton = new JButton("createButton"); + creatButton.addActionListener( + new ActionListener() { + public void actionPerformed(ActionEvent e){ + _drawingTrain = genericDopClass.getRDrawingObject(); + canv.repaint(); + } + } + ); + creatButton.setBounds(pictureBoxWidth/2, pictureBoxHeight-20, 120, 20); + + add(canv); + add(creatButton); + setVisible(true); + } +} diff --git a/laba1Loco/FormTrain.java b/laba1Loco/FormTrain.java index 4741612..8786420 100644 --- a/laba1Loco/FormTrain.java +++ b/laba1Loco/FormTrain.java @@ -7,7 +7,7 @@ import javax.swing.Timer; import java.awt.event.*; public class FormTrain{ - class Canvas extends JComponent{ + private class Canvas extends JComponent{ public DrawingTrain _drawingTrain; public Canvas(){ } @@ -208,7 +208,6 @@ public class FormTrain{ right.addActionListener(actioListener); w.setSize (1000, 600); - w.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); w.setLayout(null); canv = new Canvas(); canv.setBounds(0, 0, pictureBoxWidth, pictureBoxHeight); diff --git a/laba1Loco/FormTrainCollecltion.java b/laba1Loco/FormTrainCollecltion.java index e4ab05d..b61cd46 100644 --- a/laba1Loco/FormTrainCollecltion.java +++ b/laba1Loco/FormTrainCollecltion.java @@ -12,7 +12,7 @@ import javax.swing.JOptionPane; import javax.swing.JTextField; public class FormTrainCollecltion { - class Canvas extends JComponent{ + private class Canvas extends JComponent{ public TrainsGenericCollection _trains; public Canvas(){ } @@ -112,6 +112,15 @@ public class FormTrainCollecltion { } ); + JButton toForm4GenericDopClass = new JButton("ToForm4GenericDopClass"); + toForm4GenericDopClass.addActionListener( + new ActionListener() { + public void actionPerformed(ActionEvent e){ + Form4GenericDopClass form4GenericDopClass = new Form4GenericDopClass(); + } + } + ); + w.setSize (1000, 600); w.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); w.setLayout(null); @@ -120,11 +129,13 @@ public class FormTrainCollecltion { TextBoxNumber.setBounds(pictureBoxWidth, 30, 120, 20); ButtonRemoveTrain.setBounds(pictureBoxWidth, 60, 120, 20); ButtonRefreshCollection.setBounds(pictureBoxWidth, 90, 120, 20); + toForm4GenericDopClass.setBounds(pictureBoxWidth, 120, 120, 20); w.add(canv); w.add(ButtonAddTrain); w.add(ButtonRemoveTrain); w.add(ButtonRefreshCollection); w.add(TextBoxNumber); + w.add(toForm4GenericDopClass); w.setVisible(true); } } diff --git a/laba1Loco/GenericDopClass.java b/laba1Loco/GenericDopClass.java new file mode 100644 index 0000000..65235c9 --- /dev/null +++ b/laba1Loco/GenericDopClass.java @@ -0,0 +1,67 @@ +package laba1Loco; + +import java.util.Random; + +public class GenericDopClass { + + private Object[] Trains; + private Object[] Wheels; + private int maxCountTrains; + private int countTrains; + private int maxCountWheels; + private int countWheels; + private Random random; + /// + /// Ширина окна + /// + private int _pictureWidth; + /// + /// Высота окна + /// + private int _pictureHeight; + + public GenericDopClass(int _maxCountTrains, int _maxCountWheels, int pictureWidth, int pictureHeight){ + maxCountTrains = _maxCountTrains; + maxCountWheels = _maxCountWheels; + Trains = new Object[maxCountTrains]; + Wheels = new Object[maxCountWheels]; + countTrains = 0; + countWheels = 0; + _pictureWidth = pictureWidth; + _pictureHeight = pictureHeight; + random = new Random(); + } + + public boolean addTrain(T train){ + if (train == null) + return false; + if (countTrains > maxCountTrains) + return false; + Trains[countTrains++] = train; + return true; + } + + public boolean addWheel(U wheel){ + if (wheel == null) + return false; + if (countWheels > maxCountWheels) + return false; + Wheels[countWheels++] = wheel; + return true; + } + + public DrawingTrain getRDrawingObject(){ + if (countTrains == 0 || countWheels == 0) + return null; + int i = random.nextInt(countTrains); + int j = random.nextInt(countWheels); + DrawingTrain drawingTrain; + if (Trains[i] instanceof EntityLoco){ + drawingTrain = new DrawingLoco((EntityLoco)Trains[i], (IWheelDrawing)Wheels[j], _pictureWidth, _pictureHeight); + } + else{ + drawingTrain = new DrawingTrain((EntityTrain)Trains[i], (IWheelDrawing)Wheels[j], _pictureWidth, _pictureHeight); + } + return drawingTrain; + } +} -- 2.25.1 From c4403447c70e7df1e41a5d5381e1469c87670bf2 Mon Sep 17 00:00:00 2001 From: Timourka Date: Tue, 24 Oct 2023 12:10:39 +0400 Subject: [PATCH 5/5] =?UTF-8?q?=D0=BF=D0=BE=D0=BC=D0=B5=D0=BD=D1=8F=D0=BB?= =?UTF-8?q?=20=D0=BD=D0=B0=20=D1=8D=D1=80=D1=8D=D0=B9=D0=9B=D0=B8=D1=81?= =?UTF-8?q?=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- laba1Loco/GenericDopClass.java | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/laba1Loco/GenericDopClass.java b/laba1Loco/GenericDopClass.java index 65235c9..b8c0681 100644 --- a/laba1Loco/GenericDopClass.java +++ b/laba1Loco/GenericDopClass.java @@ -1,11 +1,12 @@ package laba1Loco; +import java.util.ArrayList; import java.util.Random; public class GenericDopClass { - private Object[] Trains; - private Object[] Wheels; + private ArrayList Trains; + private ArrayList Wheels; private int maxCountTrains; private int countTrains; private int maxCountWheels; @@ -23,8 +24,8 @@ public class GenericDopClass { public GenericDopClass(int _maxCountTrains, int _maxCountWheels, int pictureWidth, int pictureHeight){ maxCountTrains = _maxCountTrains; maxCountWheels = _maxCountWheels; - Trains = new Object[maxCountTrains]; - Wheels = new Object[maxCountWheels]; + Trains = new ArrayList(maxCountTrains); + Wheels = new ArrayList(maxCountWheels); countTrains = 0; countWheels = 0; _pictureWidth = pictureWidth; @@ -37,7 +38,7 @@ public class GenericDopClass { return false; if (countTrains > maxCountTrains) return false; - Trains[countTrains++] = train; + Trains.add(countTrains++, train); return true; } @@ -46,7 +47,7 @@ public class GenericDopClass { return false; if (countWheels > maxCountWheels) return false; - Wheels[countWheels++] = wheel; + Wheels.add(countWheels++, wheel); return true; } @@ -56,11 +57,11 @@ public class GenericDopClass { int i = random.nextInt(countTrains); int j = random.nextInt(countWheels); DrawingTrain drawingTrain; - if (Trains[i] instanceof EntityLoco){ - drawingTrain = new DrawingLoco((EntityLoco)Trains[i], (IWheelDrawing)Wheels[j], _pictureWidth, _pictureHeight); + if (Trains.get(i) instanceof EntityLoco){ + drawingTrain = new DrawingLoco((EntityLoco)Trains.get(i), Wheels.get(j), _pictureWidth, _pictureHeight); } else{ - drawingTrain = new DrawingTrain((EntityTrain)Trains[i], (IWheelDrawing)Wheels[j], _pictureWidth, _pictureHeight); + drawingTrain = new DrawingTrain(Trains.get(i), Wheels.get(j), _pictureWidth, _pictureHeight); } return drawingTrain; } -- 2.25.1