import java.awt.*; import javax.swing.*; import java.awt.event.*; public class FormShipCollection { class Canvas extends JComponent{ public ShipGenericCollection _ships; public Canvas(){} public void paintComponent(Graphics g){ super.paintComponent(g); if(_ships.ShowShips() != null){ g.drawImage(_ships.ShowShips(), 0, 0, this); } super.repaint(); } } Canvas canv; static int pictureBoxWidth = 860; static int pictureBoxHeight = 580; private ShipGenericCollection _ships; public void Draw(){ canv.repaint(); } FormShipCollection(){ canv = new Canvas(); JFrame w = new JFrame("FormShipCollection"); _ships = new ShipGenericCollection(pictureBoxWidth, pictureBoxHeight); canv._ships = _ships; JButton ButtonAddShip = new JButton("Добавить кораблик"); JButton ButtonDeleteShip = new JButton("Удалить кораблик"); JButton ButtonRefreshCollection = new JButton("Обновить коллекцию"); JButton forNewForm = new JButton("новая форма"); JTextField TextBoxNumber = new JTextField(); ButtonAddShip.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e){ FormContainerShip form = new FormContainerShip(); form.buttonSelectedShip.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent e){ if(_ships.Add(form._drawingShip) != -1){ JOptionPane.showMessageDialog(null, "Объект добавлен", "Информация", JOptionPane.INFORMATION_MESSAGE); Draw(); } else{ JOptionPane.showMessageDialog(null, "Объект не добавлен", "Информация", JOptionPane.INFORMATION_MESSAGE); } form.w.dispose(); } } ); } } ); forNewForm.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e){ NewFormRand newFormRand = new NewFormRand(); } } ); ButtonDeleteShip.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e){ if (JOptionPane.showConfirmDialog(null, "Удалить объект?", "Удаление", JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION) { return; } int pos = Integer.parseInt(TextBoxNumber.getText()); if (_ships.remove(pos) != null) { JOptionPane.showMessageDialog(null, "Объект удален", "Информация", JOptionPane.INFORMATION_MESSAGE); Draw(); } else { JOptionPane.showMessageDialog(null, "Не удалось удалить объект", "Информация", JOptionPane.INFORMATION_MESSAGE); } } } ); 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); ButtonAddShip.setBounds(pictureBoxWidth, 0, 120, 20); TextBoxNumber.setBounds(pictureBoxWidth, 50, 120, 20); ButtonDeleteShip.setBounds(pictureBoxWidth, 80, 120, 20); ButtonRefreshCollection.setBounds(pictureBoxWidth, 120, 120, 20); forNewForm.setBounds(pictureBoxWidth, 150, 120, 20); w.add(canv); w.add(ButtonAddShip); w.add(ButtonDeleteShip); w.add(ButtonRefreshCollection); w.add(forNewForm); w.add(TextBoxNumber); w.setVisible(true); } }