PIbd-23_Dolgov_D.A._Airbus..../FormMapWithSetAirbus.java

203 lines
7.7 KiB
Java
Raw Normal View History

2022-12-06 12:26:38 +04:00
import javax.swing.*;
import javax.swing.text.MaskFormatter;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.text.ParseException;
public class FormMapWithSetAirbus extends JComponent{
private BufferedImage bufferImg = null;
/// Объект от класса карты с набором объектов
private MapWithSetAirbusGeneric<DrawingObjectAirbus, AbstractMap> _mapAirbusCollectionGeneric;
public FormMapWithSetAirbus() {
JFrame formFrame = new JFrame("Form Map With SetAirbus");
formFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
formFrame.setSize(750, 500);
formFrame.setLocationRelativeTo(null);
Panel statusPanel = new Panel();
statusPanel.setBackground(Color.WHITE);
statusPanel.setLayout(new GridLayout(0, 1, 20, 20));
setLayout(new BorderLayout());
add(statusPanel, BorderLayout.EAST);
// КомбоБокс с картами
String[] maps = {
"Simple Map",
"Sky Map",
};
JComboBox mapSelectComboBox = new JComboBox(maps);
mapSelectComboBox.setEditable(true);
mapSelectComboBox.addActionListener(e -> {
AbstractMap map = null;
String item = (String)mapSelectComboBox.getSelectedItem();
switch (item) {
case "Simple Map":
map = new SimpleMap();
break;
case "Sky Map":
map = new SkyMap();
break;
}
if (map != null)
{
_mapAirbusCollectionGeneric = new MapWithSetAirbusGeneric<DrawingObjectAirbus, AbstractMap>
(600, 500, map);
}
else
{
_mapAirbusCollectionGeneric = null;
}
});
statusPanel.add(mapSelectComboBox);
// Кнопка добавления аэробуса
JButton addAirbusButton = new JButton("Add Airbus");
addAirbusButton.addActionListener(e -> {
// логика добавления
if (_mapAirbusCollectionGeneric == null)
{
return;
}
JDialog dialog = new JDialog(formFrame, "Dialog", true);
FormAirbus formAirbus = new FormAirbus(dialog);
dialog.setSize(800, 500);
dialog.setContentPane(formAirbus);
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
DrawingObjectAirbus airbus = new DrawingObjectAirbus(formAirbus.getSelectedAirbus());
if (_mapAirbusCollectionGeneric.Plus(airbus) != -1) {
JOptionPane.showMessageDialog(formFrame, "Object added", "Success", JOptionPane.OK_CANCEL_OPTION);
bufferImg = _mapAirbusCollectionGeneric.ShowSet();
repaint();
}
else {
JOptionPane.showMessageDialog(formFrame, "Object cannot be added", "Error", JOptionPane.OK_CANCEL_OPTION);
}
});
statusPanel.add(addAirbusButton);
// Текстовое поле для ввода позиции с маской
JFormattedTextField maskedTextFieldPosition = null;
try {
MaskFormatter positionMask = new MaskFormatter("##");
maskedTextFieldPosition = new JFormattedTextField(positionMask);
statusPanel.add(maskedTextFieldPosition);
}
catch (ParseException e) {
e.printStackTrace();
}
//Кнопка удаления аэробуса
JButton deleteAirbusButton = new JButton("Delete Airbus");
JFormattedTextField finalMaskedTextFieldPosition = maskedTextFieldPosition;
deleteAirbusButton.addActionListener(e -> {
// логика удаления
if ((String)mapSelectComboBox.getSelectedItem() == null) {
return;
}
if (finalMaskedTextFieldPosition == null) {
return;
}
int position = Integer.parseInt(finalMaskedTextFieldPosition.getText());
if (_mapAirbusCollectionGeneric.Minus(position) != null) {
JOptionPane.showMessageDialog(formFrame, "Object removed", "Success", JOptionPane.OK_CANCEL_OPTION);
bufferImg = _mapAirbusCollectionGeneric.ShowSet();
repaint();
}
else{
JOptionPane.showMessageDialog(formFrame, "Object cannot be removed", "Error", JOptionPane.OK_CANCEL_OPTION);
}
});
statusPanel.add(deleteAirbusButton);
//Кнопка просмотра хранилища
JButton showStorageButton = new JButton("Show Storage");
showStorageButton.addActionListener(e -> {
// логика просмотра
if (_mapAirbusCollectionGeneric == null)
{
return;
}
bufferImg = _mapAirbusCollectionGeneric.ShowSet();
repaint();
});
statusPanel.add(showStorageButton);
//Кнопка просмотра карты
JButton showOnMapButton = new JButton("Show On Map");
showOnMapButton.addActionListener(e -> {
// логика просмотра
if (_mapAirbusCollectionGeneric == null)
{
return;
}
bufferImg = _mapAirbusCollectionGeneric.ShowOnMap();
});
statusPanel.add(showOnMapButton);
ActionListener moveButtonListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_mapAirbusCollectionGeneric == null)
{
return;
}
String name = e.getActionCommand();
Direction dir = Direction.None;
switch (name)
{
case "Up":
dir = Direction.Up;
break;
case "Down":
dir = Direction.Down;
break;
case "Left":
dir = Direction.Left;
break;
case "Right":
dir = Direction.Right;
break;
}
bufferImg = _mapAirbusCollectionGeneric.MoveObject(dir);
}
};
//Кнопки управления
JButton moveDownButton = new JButton("Down");
moveDownButton.addActionListener(moveButtonListener);
JButton moveUpButton = new JButton("Up");
moveUpButton.addActionListener(moveButtonListener);
JButton moveLeftButton = new JButton("Left");
moveLeftButton.addActionListener(moveButtonListener);
JButton moveRightButton = new JButton("Right");
moveRightButton.addActionListener(moveButtonListener);
statusPanel.add(moveUpButton);
statusPanel.add(moveDownButton);
statusPanel.add(moveLeftButton);
statusPanel.add(moveRightButton);
JButton showGalleryButton = new JButton("Show Gallery");
showGalleryButton.addActionListener(e -> {
new FormEntityWithExtraGallery();
});
statusPanel.add(showGalleryButton);
formFrame.getContentPane().add(this);
formFrame.setVisible(true);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
if (bufferImg != null) g2.drawImage(bufferImg, 0,0,600,500,null);
super.repaint();
}
}