Перенесена форма FormMapWithSetLocomotives, изменены некоторые классы для ее работы

This commit is contained in:
Данила Мочалов 2022-10-21 21:27:07 +04:00
parent 896285b4c5
commit d91e28ae4a
4 changed files with 265 additions and 46 deletions

View File

@ -1,6 +1,5 @@
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
public class FormLocomotive extends JComponent{
@ -9,26 +8,7 @@ public class FormLocomotive extends JComponent{
public DrawningLocomotive getSelectedLocomotive() {
return SelectedLocomotive;
}
public FormLocomotive() {
JFrame formFrame = new JFrame("Locomotive");
formFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
formFrame.setSize(800, 500);
formFrame.setVisible(true);
formFrame.setLocationRelativeTo(null);
formFrame.addComponentListener(new ComponentListener() {
@Override
public void componentResized(ComponentEvent e) {
if (_locomotive != null) _locomotive.ChangeBorders(formFrame.getWidth(), formFrame.getHeight());
repaint();
}
@Override
public void componentMoved(ComponentEvent e) {}
@Override
public void componentShown(ComponentEvent e) {}
@Override
public void componentHidden(ComponentEvent e) {}
});
public FormLocomotive(JDialog caller) {
Panel statusPanel = new Panel();
statusPanel.setBackground(Color.WHITE);
@ -44,7 +24,7 @@ public class FormLocomotive extends JComponent{
createButton.addActionListener(e -> {
Random rnd = new Random();
_locomotive = new DrawningLocomotive(rnd.nextInt(200) + 100, rnd.nextInt(1000) + 1000, new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)));
_locomotive.SetPosition(10 + rnd.nextInt(90), 10 + rnd.nextInt(90), formFrame.getWidth(), formFrame.getHeight() - 75);
_locomotive.SetPosition(10 + rnd.nextInt(90), 10 + rnd.nextInt(90), 800, 500-75);
speedLabel.setText("Speed: " + _locomotive.Locomotive.getSpeed());
weightLabel.setText("Weight: " + (int)_locomotive.Locomotive.getWeight());
colorLabel.setText("Color: " + _locomotive.Locomotive.getBodyColor().getRed() + " " + _locomotive.Locomotive.getBodyColor().getGreen() + " " + _locomotive.Locomotive.getBodyColor().getBlue() );
@ -59,7 +39,7 @@ public class FormLocomotive extends JComponent{
new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)),
rnd.nextBoolean(),
rnd.nextBoolean());
_locomotive.SetPosition(10 + rnd.nextInt(90), 10 + rnd.nextInt(90), formFrame.getWidth(), formFrame.getHeight() - 75);
_locomotive.SetPosition(10 + rnd.nextInt(90), 10 + rnd.nextInt(90), 800, 500 - 75);
speedLabel.setText("Speed: " + _locomotive.Locomotive.getSpeed());
weightLabel.setText("Weight: " + (int)_locomotive.Locomotive.getWeight());
colorLabel.setText("Color: " + _locomotive.Locomotive.getBodyColor().getRed() + " " + _locomotive.Locomotive.getBodyColor().getGreen() + " " + _locomotive.Locomotive.getBodyColor().getBlue() );
@ -69,7 +49,8 @@ public class FormLocomotive extends JComponent{
JButton selectLocomotiveButton = new JButton("Select");
selectLocomotiveButton.addActionListener(e -> {
SelectedLocomotive = _locomotive;
JOptionPane.showMessageDialog(formFrame, "OK");
//TODO
caller.dispose();
});
statusPanel.add(createButton);
@ -107,8 +88,6 @@ public class FormLocomotive extends JComponent{
statusPanel.add(moveDownButton);
statusPanel.add(moveLeftButton);
statusPanel.add(moveRightButton);
formFrame.getContentPane().add(this);
}
@Override
protected void paintComponent(Graphics g) {
@ -119,6 +98,6 @@ public class FormLocomotive extends JComponent{
}
public static void main(String[] args) {
new FormMap();
new FormMapWithSetLocomotives();
}
}

View File

@ -0,0 +1,209 @@
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);
formFrame.setVisible(true);
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;
}
// TODO
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());
//TODO
if (_mapLocomotivesCollectionGeneric.Plus(locomotive)) {
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)) {
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);
formFrame.getContentPane().add(this);
super.repaint();
}
@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();
}
}

View File

@ -29,14 +29,15 @@ public class MapWithSetLocomotivesGeneric
}
/// Добавление
public boolean Plus(MapWithSetLocomotivesGeneric<T, U> map, T locomotive)
//TODO
public boolean Plus(T locomotive)
{
return map._setLocomotives.Insert(locomotive);
return this._setLocomotives.Insert(locomotive);
}
/// Удаление
public boolean Minus(MapWithSetLocomotivesGeneric<T, U> map, int position)
public boolean Minus(int position)
{
return map._setLocomotives.Remove(position);
return this._setLocomotives.Remove(position);
}
/// Вывод всего набора объектов
@ -44,8 +45,8 @@ public class MapWithSetLocomotivesGeneric
{
BufferedImage bmp = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_RGB);
Graphics gr = bmp.getGraphics();
DrawBackground(gr);
DrawLocomotives(gr);
DrawBackground((Graphics2D)gr);
DrawLocomotives((Graphics2D)gr);
return bmp;
}
@ -101,23 +102,50 @@ public class MapWithSetLocomotivesGeneric
}
/// Метод отрисовки фона
private void DrawBackground(Graphics g)
private void DrawBackground(Graphics2D g)
{
g.setColor(Color.WHITE);
g.fillRect(0,0,600, 500);
for (int j = _placeSizeHeight; j < _pictureHeight; j+= _placeSizeHeight)
{
//нижняя линия рельс
g.setColor(Color.BLACK);
g.setStroke(new BasicStroke(5));
g.drawLine(0, j, _pictureWidth, j);
for (int i = 0; i < _pictureWidth; i+=20)
{
g.drawLine(i, j, i, j + 10);
}
g.drawLine(0, j + 10, _pictureWidth, j + 10);
//верхняя линия рельс
g.setColor(Color.GRAY);
g.drawLine(0, j - 20, _pictureWidth, j - 20);
for (int i = 0; i < _pictureWidth; i += 20)
{
g.drawLine(i, j - 20, i, j - 10);
}
g.drawLine(0, j - 10, _pictureWidth, j - 10);
//фонари
for (int i = _placeSizeWidth; i < _pictureWidth; i += _placeSizeWidth)
{
g.setColor(Color.BLACK);
for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++)
{
for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j)
{ //линия разметки места
g.drawLine(i * _placeSizeWidth, j * _placeSizeHeight, i *
_placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight);
g.setStroke(new BasicStroke(10));
g.drawLine(i, j - _placeSizeHeight + 20, i, j);
g.setColor(Color.YELLOW);
g.setStroke(new BasicStroke(20));
g.drawLine(i, j - _placeSizeHeight + 18, i, j - _placeSizeHeight + 38);
}
g.drawLine(i * _placeSizeWidth, 0, i * _placeSizeWidth,
(_pictureHeight / _placeSizeHeight) * _placeSizeHeight);
}
g.setStroke(new BasicStroke(2));
}
/// Метод прорисовки объектов
private void DrawLocomotives(Graphics g)
private void DrawLocomotives(Graphics2D g)
{
int width = _pictureWidth / _placeSizeWidth;
int height = _pictureHeight / _placeSizeHeight;
@ -128,7 +156,7 @@ public class MapWithSetLocomotivesGeneric
for (int i = 0; i < _setLocomotives.Count(); i++)
{
// установка позиции
if (_setLocomotives.Get(i) != null) _setLocomotives.Get(i).SetObject(curWidth * _placeSizeWidth, curHeight * _placeSizeHeight, _pictureWidth, _pictureHeight);
if (_setLocomotives.Get(i) != null) _setLocomotives.Get(i).SetObject(curWidth * _placeSizeWidth + 10, curHeight * _placeSizeHeight + 18, _pictureWidth, _pictureHeight);
if (_setLocomotives.Get(i) != null) _setLocomotives.Get(i).DrawningObject(g);
if (curWidth < width) curWidth++;
else

View File

@ -24,9 +24,12 @@ public class SetLocomotivesGeneric <T>
int emptyEl = -1;
for (int i = position + 1; i < Count(); i++)
{
if (_places[i] == null) emptyEl = i;
if (_places[i] == null)
{
emptyEl = i;
break;
}
}
if (emptyEl == -1)
{
return false;