153 lines
5.6 KiB
Java
153 lines
5.6 KiB
Java
import javax.swing.*;
|
||
import javax.swing.text.DefaultFormatterFactory;
|
||
import javax.swing.text.MaskFormatter;
|
||
import java.awt.*;
|
||
import java.text.ParseException;
|
||
|
||
public class FormMapWithSetTracktor extends JFrame {
|
||
private JPanel ContentPanel;
|
||
private JPanel pictureBox;
|
||
private JPanel toolsGroup;
|
||
private JLabel toolsLabel;
|
||
private JComboBox comboBoxMapSelector;
|
||
private JButton buttonAddTracktor;
|
||
private JFormattedTextField textBoxPosition;
|
||
private JButton buttonRemoveTracktor;
|
||
private JButton buttonShowStorage;
|
||
private JButton buttonShowOnMap;
|
||
private JButton buttonUp;
|
||
private JButton buttonLeft;
|
||
private JButton buttonRight;
|
||
private JButton buttonDown;
|
||
|
||
private Image bufferedImage;
|
||
private MapWithSetTracktorGeneric<DrawningObjectExcavator, AbstractMap> _mapTracktorCollectionGeneric;
|
||
|
||
public FormMapWithSetTracktor() {
|
||
this.setTitle("Трактор");
|
||
this.setContentPane(ContentPanel);
|
||
this.setSize(800,500);
|
||
|
||
try {
|
||
textBoxPosition.setFormatterFactory(new DefaultFormatterFactory(new MaskFormatter("##")));
|
||
} catch (ParseException e) {
|
||
e.printStackTrace();
|
||
}
|
||
|
||
comboBoxMapSelector.addActionListener(e -> {
|
||
AbstractMap map = switch (((JComboBox) e.getSource()).getSelectedItem().toString()) {
|
||
case "Простая карта" -> new SimpleMap();
|
||
case "Свалка карта" -> new DumpMap();
|
||
default -> null;
|
||
};
|
||
|
||
if (map != null) {
|
||
_mapTracktorCollectionGeneric = new MapWithSetTracktorGeneric<>(pictureBox.getWidth(), pictureBox.getHeight(), map);
|
||
} else {
|
||
_mapTracktorCollectionGeneric = null;
|
||
}
|
||
});
|
||
|
||
buttonAddTracktor.addActionListener(e -> {
|
||
if (_mapTracktorCollectionGeneric == null) {
|
||
return;
|
||
}
|
||
|
||
FormTracktor dialog = new FormTracktor();
|
||
dialog.setSize(800, 500);
|
||
dialog.setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
|
||
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
|
||
dialog.setVisible(true);
|
||
|
||
if (dialog.getSelectedTracktor() != null) {
|
||
DrawningObjectExcavator tracktor = new DrawningObjectExcavator(dialog.getSelectedTracktor());
|
||
if (_mapTracktorCollectionGeneric.addTracktor(tracktor) != -1)
|
||
{
|
||
JOptionPane.showMessageDialog(this, "Объект добавлен", "Успех", JOptionPane.INFORMATION_MESSAGE);
|
||
bufferedImage = _mapTracktorCollectionGeneric.showSet();
|
||
repaint();
|
||
}
|
||
else
|
||
{
|
||
JOptionPane.showMessageDialog(this, "Не удалось добавить объект", "Провал", JOptionPane.INFORMATION_MESSAGE);
|
||
}
|
||
}
|
||
});
|
||
|
||
buttonRemoveTracktor.addActionListener(e -> {
|
||
String text = textBoxPosition.getText();
|
||
if (text == null || text.isEmpty()) {
|
||
return;
|
||
}
|
||
|
||
if (JOptionPane.showConfirmDialog(this, "Удалить объект?", "Удаление", JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION) {
|
||
return;
|
||
}
|
||
|
||
int position = Integer.parseInt(text);
|
||
|
||
if (_mapTracktorCollectionGeneric.removeTracktorAt(position) != null) {
|
||
JOptionPane.showMessageDialog(this, "Объект удалён", "Успех", JOptionPane.INFORMATION_MESSAGE);
|
||
bufferedImage = _mapTracktorCollectionGeneric.showSet();
|
||
repaint();
|
||
} else {
|
||
JOptionPane.showMessageDialog(this, "Не удалось удалить объект", "Провал", JOptionPane.INFORMATION_MESSAGE);
|
||
}
|
||
});
|
||
|
||
buttonShowStorage.addActionListener(e -> {
|
||
if (_mapTracktorCollectionGeneric == null) {
|
||
return;
|
||
}
|
||
bufferedImage = _mapTracktorCollectionGeneric.showSet();
|
||
repaint();
|
||
});
|
||
|
||
buttonShowOnMap.addActionListener(e -> {
|
||
if (_mapTracktorCollectionGeneric == null) {
|
||
return;
|
||
}
|
||
bufferedImage = _mapTracktorCollectionGeneric.showOnMap();
|
||
repaint();
|
||
});
|
||
|
||
buttonLeft.addActionListener(e -> {
|
||
if (_mapTracktorCollectionGeneric != null) {
|
||
bufferedImage = _mapTracktorCollectionGeneric.moveObject(Direction.Left);
|
||
repaint();
|
||
}
|
||
});
|
||
|
||
buttonRight.addActionListener(e -> {
|
||
if (_mapTracktorCollectionGeneric != null) {
|
||
bufferedImage = _mapTracktorCollectionGeneric.moveObject(Direction.Right);
|
||
repaint();
|
||
}
|
||
});
|
||
|
||
buttonUp.addActionListener(e -> {
|
||
if (_mapTracktorCollectionGeneric != null) {
|
||
bufferedImage = _mapTracktorCollectionGeneric.moveObject(Direction.Up);
|
||
repaint();
|
||
}
|
||
});
|
||
|
||
buttonDown.addActionListener(e -> {
|
||
if (_mapTracktorCollectionGeneric != null) {
|
||
bufferedImage = _mapTracktorCollectionGeneric.moveObject(Direction.Down);
|
||
repaint();
|
||
}
|
||
});
|
||
}
|
||
|
||
@Override
|
||
public void paint(Graphics g) {
|
||
super.paint(g);
|
||
|
||
if (bufferedImage != null) {
|
||
pictureBox.paintComponents(bufferedImage.getGraphics());
|
||
pictureBox.getGraphics().drawImage(bufferedImage, 0, 0, null);
|
||
}
|
||
}
|
||
}
|