129 lines
4.6 KiB
Java
129 lines
4.6 KiB
Java
package ProjectTankHard;
|
||
|
||
import ProjectTankHard.DrawningObjects.DrawningTankBase;
|
||
import ProjectTankHard.Generics.TanksGenericCollection;
|
||
import ProjectTankHard.MovementStrategy.DrawningObjectTank;
|
||
|
||
import javax.swing.*;
|
||
import java.awt.*;
|
||
import java.awt.event.ActionEvent;
|
||
import java.awt.event.ActionListener;
|
||
|
||
|
||
public class FormTankCollection {
|
||
private final TanksGenericCollection<DrawningTankBase, DrawningObjectTank> _tanks;
|
||
private int pictureBoxWidth = 605;
|
||
private int pictureBoxHeight = 426;
|
||
CollectionCanvas canv;
|
||
void Draw(){
|
||
if(canv == null) return;
|
||
|
||
canv.repaint();
|
||
}
|
||
|
||
public FormTankCollection(){
|
||
_tanks = new TanksGenericCollection<>(pictureBoxWidth, pictureBoxHeight);
|
||
canv = new CollectionCanvas();
|
||
canv.setBounds(12,12, pictureBoxWidth, pictureBoxHeight);
|
||
|
||
JPanel toolBox = new JPanel();
|
||
JFrame collectionFrame = new JFrame();
|
||
|
||
JButton addButton = new JButton("Добавить");
|
||
JButton removeButton = new JButton("Удалить");
|
||
JButton refreshButton = new JButton("Обновить");
|
||
JTextField tankNumber = new JTextField();
|
||
GridLayout gridLayout = new GridLayout(4,1);
|
||
|
||
toolBox.setLayout(gridLayout);
|
||
toolBox.add(addButton);
|
||
toolBox.add(tankNumber);
|
||
toolBox.add(removeButton);
|
||
toolBox.add(refreshButton);
|
||
toolBox.setBounds(623, 12, 227, 426);
|
||
|
||
collectionFrame.add(toolBox);
|
||
collectionFrame.add(canv);
|
||
collectionFrame.setVisible(true);
|
||
collectionFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
|
||
collectionFrame.setSize(880,497);
|
||
|
||
addButton.addActionListener(new ActionListener() {
|
||
@Override
|
||
public void actionPerformed(ActionEvent e) {
|
||
if(_tanks == null) return;
|
||
|
||
FormTank form = new FormTank();
|
||
form.SelectButton.addActionListener(new ActionListener() {
|
||
@Override
|
||
public void actionPerformed(ActionEvent e) {
|
||
if (_tanks.Insert(form.SelectedTank()) != -1)
|
||
{
|
||
JOptionPane.showMessageDialog(null, "Объект добавлен", "Информация", JOptionPane.INFORMATION_MESSAGE);
|
||
form.SelectedTank()._pictureWidth = pictureBoxWidth;
|
||
form.SelectedTank()._pictureHeight = pictureBoxHeight;
|
||
Draw();
|
||
}
|
||
else
|
||
{
|
||
JOptionPane.showMessageDialog(null, "Не удалось добавить объект", "Информация", JOptionPane.INFORMATION_MESSAGE);
|
||
}
|
||
|
||
canv._tanks = _tanks;
|
||
form.TankFrame.dispose();
|
||
Draw();
|
||
}
|
||
});
|
||
}
|
||
});
|
||
|
||
removeButton.addActionListener(new ActionListener() {
|
||
@Override
|
||
public void actionPerformed(ActionEvent e) {
|
||
if(_tanks == null) return;
|
||
|
||
String tmp = tankNumber.getText();
|
||
int numb;
|
||
|
||
try {
|
||
numb = Integer.parseInt(tmp);
|
||
}
|
||
catch(Exception ex){
|
||
JOptionPane.showMessageDialog(null, "Введите число", "Информация", JOptionPane.INFORMATION_MESSAGE);
|
||
return;
|
||
}
|
||
|
||
_tanks.Remove(numb);
|
||
_tanks.ShowTanks();
|
||
JOptionPane.showMessageDialog(null, "Объект удален", "Информация", JOptionPane.INFORMATION_MESSAGE);
|
||
Draw();
|
||
}
|
||
});
|
||
|
||
refreshButton.addActionListener(new ActionListener() {
|
||
@Override
|
||
public void actionPerformed(ActionEvent e) {
|
||
if(_tanks == null) return;
|
||
|
||
_tanks.ShowTanks();
|
||
Draw();
|
||
}
|
||
});
|
||
}
|
||
}
|
||
|
||
class CollectionCanvas extends JComponent {
|
||
public TanksGenericCollection<DrawningTankBase, DrawningObjectTank> _tanks;
|
||
public CollectionCanvas() {}
|
||
|
||
@Override
|
||
public void paintComponent (Graphics g){
|
||
if (_tanks == null) return;
|
||
|
||
super.paintComponents (g) ;
|
||
Graphics2D g2d = (Graphics2D)g;
|
||
g2d.drawImage(_tanks.ShowTanks(), 0, 0, this);
|
||
super.repaint();
|
||
}
|
||
}
|