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); } /// /// Перегрузка оператора вычитания