PIbd-23_Mochalov_D.V._Locom.../FormMapWithSetLocomotives.java

207 lines
8.0 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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 FormMapWithSetLocomotives extends JComponent {
private BufferedImage bufferImg = null;
/// Объект от класса карты с набором объектов
private MapWithSetLocomotivesGeneric<DrawningObjectLocomotive, AbstractMap> _mapLocomotivesCollectionGeneric;
public FormMapWithSetLocomotives() {
JFrame formFrame = new JFrame("Form Map With SetLocomotives");
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",
"Spike Map",
"Rail 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 "Spike Map":
map = new SpikeMap();
break;
case "Rail Map":
map = new RailMap();
break;
}
if (map != null)
{
_mapLocomotivesCollectionGeneric = new MapWithSetLocomotivesGeneric<DrawningObjectLocomotive, AbstractMap>
(600, 500, map);
}
else
{
_mapLocomotivesCollectionGeneric = null;
}
});
statusPanel.add(mapSelectComboBox);
// Кнопка добавления локомотива
JButton addLocomotiveButton = new JButton("Add Locomotive");
addLocomotiveButton.addActionListener(e -> {
// логика добавления
if (_mapLocomotivesCollectionGeneric == null)
{
return;
}
JDialog dialog = new JDialog(formFrame, "Dialog", true);
FormLocomotive formLocomotive = new FormLocomotive(dialog);
dialog.setSize(800, 500);
dialog.setContentPane(formLocomotive);
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
DrawningObjectLocomotive locomotive = new DrawningObjectLocomotive(formLocomotive.getSelectedLocomotive());
if (_mapLocomotivesCollectionGeneric.Plus(locomotive) != -1) {
JOptionPane.showMessageDialog(formFrame, "Object added", "Success", JOptionPane.OK_CANCEL_OPTION);
bufferImg = _mapLocomotivesCollectionGeneric.ShowSet();
repaint();
}
else {
JOptionPane.showMessageDialog(formFrame, "Object cannot be added", "Error", JOptionPane.OK_CANCEL_OPTION);
}
});
statusPanel.add(addLocomotiveButton);
// Текстовое поле для ввода позиции с маской
JFormattedTextField maskedTextFieldPosition = null;
try {
MaskFormatter positionMask = new MaskFormatter("##");
maskedTextFieldPosition = new JFormattedTextField(positionMask);
statusPanel.add(maskedTextFieldPosition);
}
catch (ParseException e) {
e.printStackTrace();
}
//Кнопка удаления локомотива
JButton deleteLocomotiveButton = new JButton("Delete Locomotive");
JFormattedTextField finalMaskedTextFieldPosition = maskedTextFieldPosition;
deleteLocomotiveButton.addActionListener(e -> {
// логика удаления
if ((String)mapSelectComboBox.getSelectedItem() == null) {
return;
}
//TODO
if (finalMaskedTextFieldPosition == null) {
return;
}
int position = Integer.parseInt(finalMaskedTextFieldPosition.getText());
if (_mapLocomotivesCollectionGeneric.Minus(position) != null) {
JOptionPane.showMessageDialog(formFrame, "Object removed", "Success", JOptionPane.OK_CANCEL_OPTION);
bufferImg = _mapLocomotivesCollectionGeneric.ShowSet();
repaint();
}
else{
JOptionPane.showMessageDialog(formFrame, "Object cannot be removed", "Error", JOptionPane.OK_CANCEL_OPTION);
}
});
statusPanel.add(deleteLocomotiveButton);
//Кнопка просмотра хранилища
JButton showStorageButton = new JButton("Show Storage");
showStorageButton.addActionListener(e -> {
// логика просмотра
if (_mapLocomotivesCollectionGeneric == null)
{
return;
}
bufferImg = _mapLocomotivesCollectionGeneric.ShowSet();
repaint();
});
statusPanel.add(showStorageButton);
//Кнопка просмотра карты
JButton showOnMapButton = new JButton("Show On Map");
showOnMapButton.addActionListener(e -> {
// логика просмотра
if (_mapLocomotivesCollectionGeneric == null)
{
return;
}
bufferImg = _mapLocomotivesCollectionGeneric.ShowOnMap();
});
statusPanel.add(showOnMapButton);
ActionListener moveButtonListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_mapLocomotivesCollectionGeneric == 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 = _mapLocomotivesCollectionGeneric.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();
}
}