Этап 3. Доделанная работа 4

This commit is contained in:
2022-11-17 14:57:30 +04:00
parent 40ad8463b5
commit b3a51217f6
5 changed files with 215 additions and 68 deletions

View File

@@ -13,6 +13,9 @@ public class DrawingObjectShip implements IDrawingObject {
}
return 0;
}
public DrawingShip GetDrawingObjectShip() {
return _ship;
}
@Override
public void SetObject(int x, int y, int width, int height) {
_ship.SetPosition(x,y,width,height);

View File

@@ -1,16 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="FormMapWithSetShipsGeneric">
<grid id="27dc6" binding="Mainpanel" layout-manager="GridLayoutManager" row-count="2" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="27dc6" binding="Mainpanel" layout-manager="GridLayoutManager" row-count="4" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="649" height="400"/>
<xy x="20" y="20" width="798" height="526"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<grid id="9bb5a" binding="pictureBoxShip" layout-manager="BorderLayout" hgap="0" vgap="0">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
<grid row="0" column="0" row-span="3" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false">
<minimum-size width="600" height="400"/>
<preferred-size width="600" height="400"/>
<maximum-size width="600" height="400"/>
</grid>
</constraints>
<properties/>
<border type="none"/>
@@ -18,31 +22,20 @@
</grid>
<hspacer id="b305e">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
<grid row="3" column="0" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<grid id="4fa91" binding="GroupBoxTools" layout-manager="GridLayoutManager" row-count="8" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="1" row-span="2" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
<grid row="1" column="1" row-span="3" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="95ba3" class="javax.swing.JComboBox" binding="ComboBoxSelectorMap">
<constraints>
<grid row="0" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<model>
<item value="Простая карта"/>
<item value="Карта море"/>
</model>
</properties>
</component>
<component id="cdb80" class="javax.swing.JButton" binding="ButtonAddShip">
<constraints>
<grid row="1" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
<grid row="0" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Добавить корабль"/>
@@ -50,7 +43,7 @@
</component>
<component id="e5494" class="javax.swing.JTextField" binding="maskedTextBoxPosition">
<constraints>
<grid row="2" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<grid row="1" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
@@ -58,7 +51,7 @@
</component>
<component id="8bb89" class="javax.swing.JButton" binding="ButtonDeleteShip">
<constraints>
<grid row="3" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
<grid row="2" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Удалить корабль"/>
@@ -66,7 +59,7 @@
</component>
<component id="aa482" class="javax.swing.JButton" binding="ButtonShowStorage">
<constraints>
<grid row="4" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
<grid row="3" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Показать хранилище"/>
@@ -74,7 +67,7 @@
</component>
<component id="9ebdc" class="javax.swing.JButton" binding="ButtonShowMap">
<constraints>
<grid row="5" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
<grid row="4" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Показать карту"/>
@@ -128,6 +121,77 @@
<text value=""/>
</properties>
</component>
<component id="6734a" class="javax.swing.JButton" binding="ButtonCheckDel">
<constraints>
<grid row="5" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Удаленные"/>
</properties>
</component>
</children>
</grid>
<grid id="d087c" binding="GroupBoxMaps" layout-manager="GridLayoutManager" row-count="7" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="124b6" class="javax.swing.JTextField" binding="textBoxNewMapName">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
<properties/>
</component>
<vspacer id="3ed0f">
<constraints>
<grid row="6" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
<component id="95ba3" class="javax.swing.JComboBox" binding="ComboBoxSelectorMap">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<model>
<item value="Простая карта"/>
<item value="Карта море"/>
</model>
</properties>
</component>
<component id="fb777" class="javax.swing.JButton" binding="ButtonAddMap">
<constraints>
<grid row="3" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Добавить карту"/>
</properties>
</component>
<component id="a358a" class="javax.swing.JButton" binding="ButtonDeleteMap">
<constraints>
<grid row="5" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Удалить карту"/>
</properties>
</component>
<vspacer id="64e67">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
<component id="e221" class="javax.swing.JList" binding="ListBoxMaps" custom-create="true">
<constraints>
<grid row="4" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="2" anchor="0" fill="3" indent="0" use-parent-layout="false">
<preferred-size width="150" height="50"/>
</grid>
</constraints>
<properties/>
</component>
</children>
</grid>
</children>

View File

@@ -6,7 +6,9 @@ import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.util.HashMap;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
public class FormMapWithSetShipsGeneric extends JFrame{
public JPanel Mainpanel;
private JPanel pictureBoxShip;
@@ -21,7 +23,21 @@ public class FormMapWithSetShipsGeneric extends JFrame{
private JButton ButtonUp;
private JButton ButtonRight;
private JTextField maskedTextBoxPosition;
private JTextField textBoxNewMapName;
private int picWidth=600;
private int picHeight=400;
private JButton ButtonAddMap;
private JList ListBoxMaps;
private JButton ButtonDeleteMap;
private JPanel GroupBoxMaps;
private JButton ButtonCheckDel;
private MapWithSetShipsGeneric<DrawingObjectShip,AbstractMap> _mapShipsCollectionGeneric;
private final HashMap<String,AbstractMap> _mapsDict = new HashMap<>(){{
put("Простая карта",new SimpleMap());
put("Карта море",new SeaMap());
}};
private final MapsCollection _mapsCollection;
public void UpdateWindow(BufferedImage bmp)
{
pictureBoxShip.removeAll();
@@ -32,8 +48,30 @@ public class FormMapWithSetShipsGeneric extends JFrame{
pictureBoxShip.add(imageWithMapAndObject, BorderLayout.CENTER);
pictureBoxShip.revalidate();
}
private void ReloadMaps(){
int index = ListBoxMaps.getSelectedIndex();
DefaultListModel<String> model1 = (DefaultListModel<String>) ListBoxMaps.getModel();
model1.removeAllElements();
for(int i=0;i<_mapsCollection.Keys().size();i++)
{
model1.addElement(_mapsCollection.Keys().get(i));
}
if (ListBoxMaps.getModel().getSize() > 0 && (index == -1 || index >= ListBoxMaps.getModel().getSize()))
{
ListBoxMaps.setSelectedIndex(0);
}
else if (ListBoxMaps.getModel().getSize() > 0 && index > -1 && index < ListBoxMaps.getModel().getSize())
{
ListBoxMaps.setSelectedIndex(index);
}
}
public FormMapWithSetShipsGeneric()
{
_mapsCollection = new MapsCollection(picWidth, picHeight);
ComboBoxSelectorMap.removeAllItems();
for (String elem : _mapsDict.keySet()) {
ComboBoxSelectorMap.addItem(elem);
}
try {
Image img = ImageIO.read(FormShip.class.getResource("Images/4.png"));
ButtonUp.setIcon(new ImageIcon(img));
@@ -47,36 +85,10 @@ public class FormMapWithSetShipsGeneric extends JFrame{
System.out.println(ex);
}
ComboBoxSelectorMap.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
AbstractMap map = null;
ComboBoxSelectorMap = (JComboBox)e.getSource();
String item = (String)ComboBoxSelectorMap.getSelectedItem();
switch (item) {
case "Простая карта": {
map = new SimpleMap();
break;
}
case "Карта море": {
map = new SeaMap();
break;
}
}
if(map!=null)
{
_mapShipsCollectionGeneric = new MapWithSetShipsGeneric<DrawingObjectShip,AbstractMap>(pictureBoxShip.getWidth(),pictureBoxShip.getHeight(),map);
}
else
{
_mapShipsCollectionGeneric=null;
}
}
});
ButtonAddShip.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(_mapShipsCollectionGeneric==null)
if (ListBoxMaps.getSelectedIndex() == -1)
{
return;
}
@@ -85,10 +97,10 @@ public class FormMapWithSetShipsGeneric extends JFrame{
form.setVisible(true);
form.setModal(true);
DrawingObjectShip ship = new DrawingObjectShip(form.GetSelectedShip());
if(_mapShipsCollectionGeneric.Add(ship)!=-1)
if(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).Add(ship)!=-1)
{
JOptionPane.showMessageDialog(null,"Объект добавлен");
UpdateWindow(_mapShipsCollectionGeneric.ShowSet());
UpdateWindow(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).ShowSet());
}
else
{
@@ -96,7 +108,14 @@ public class FormMapWithSetShipsGeneric extends JFrame{
}
}
});
ListBoxMaps.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
if (ListBoxMaps.getSelectedIndex() == -1)
return;
UpdateWindow(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).ShowSet());
}
});
ButtonDeleteShip.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
@@ -110,10 +129,10 @@ public class FormMapWithSetShipsGeneric extends JFrame{
return;
}
int pos=Integer.parseInt(maskedTextBoxPosition.getText());
if(_mapShipsCollectionGeneric.Delete(pos)!=null)
if(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).Delete(pos)!=null)
{
JOptionPane.showMessageDialog(null, "Объект удален");
UpdateWindow(_mapShipsCollectionGeneric.ShowSet());
UpdateWindow(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).ShowSet());
}
else
{
@@ -124,27 +143,27 @@ public class FormMapWithSetShipsGeneric extends JFrame{
ButtonShowStorage.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_mapShipsCollectionGeneric == null)
if (ListBoxMaps.getSelectedIndex() == -1)
{
return;
}
UpdateWindow(_mapShipsCollectionGeneric.ShowSet());
UpdateWindow(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).ShowSet());
}
});
ButtonShowMap.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_mapShipsCollectionGeneric == null)
if (ListBoxMaps.getSelectedIndex() == -1)
{
return;
}
UpdateWindow(_mapShipsCollectionGeneric.ShowOnMap());
UpdateWindow(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).ShowOnMap());
}
});
ButtonUp.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_mapShipsCollectionGeneric == null)
if (ListBoxMaps.getSelectedIndex() == -1)
{
return;
}
@@ -152,7 +171,7 @@ public class FormMapWithSetShipsGeneric extends JFrame{
JLabel imageWithMapAndObject = new JLabel();
imageWithMapAndObject.setPreferredSize(pictureBoxShip.getSize());
imageWithMapAndObject.setMinimumSize(new Dimension(1, 1));
imageWithMapAndObject.setIcon(new ImageIcon(_mapShipsCollectionGeneric.MoveObject(Direction.Up)));
imageWithMapAndObject.setIcon(new ImageIcon(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).MoveObject(Direction.Up)));
pictureBoxShip.add(imageWithMapAndObject, BorderLayout.CENTER);
pictureBoxShip.revalidate();
pictureBoxShip.repaint();
@@ -161,7 +180,7 @@ public class FormMapWithSetShipsGeneric extends JFrame{
ButtonDown.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_mapShipsCollectionGeneric == null)
if (ListBoxMaps.getSelectedIndex() == -1)
{
return;
}
@@ -169,7 +188,7 @@ public class FormMapWithSetShipsGeneric extends JFrame{
JLabel imageWithMapAndObject = new JLabel();
imageWithMapAndObject.setPreferredSize(pictureBoxShip.getSize());
imageWithMapAndObject.setMinimumSize(new Dimension(1, 1));
imageWithMapAndObject.setIcon(new ImageIcon(_mapShipsCollectionGeneric.MoveObject(Direction.Down)));
imageWithMapAndObject.setIcon(new ImageIcon(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).MoveObject(Direction.Down)));
pictureBoxShip.add(imageWithMapAndObject, BorderLayout.CENTER);
pictureBoxShip.revalidate();
pictureBoxShip.repaint();
@@ -178,7 +197,7 @@ public class FormMapWithSetShipsGeneric extends JFrame{
ButtonRight.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_mapShipsCollectionGeneric == null)
if (ListBoxMaps.getSelectedIndex() == -1)
{
return;
}
@@ -186,7 +205,7 @@ public class FormMapWithSetShipsGeneric extends JFrame{
JLabel imageWithMapAndObject = new JLabel();
imageWithMapAndObject.setPreferredSize(pictureBoxShip.getSize());
imageWithMapAndObject.setMinimumSize(new Dimension(1, 1));
imageWithMapAndObject.setIcon(new ImageIcon(_mapShipsCollectionGeneric.MoveObject(Direction.Right)));
imageWithMapAndObject.setIcon(new ImageIcon(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).MoveObject(Direction.Right)));
pictureBoxShip.add(imageWithMapAndObject, BorderLayout.CENTER);
pictureBoxShip.revalidate();
pictureBoxShip.repaint();
@@ -195,7 +214,7 @@ public class FormMapWithSetShipsGeneric extends JFrame{
ButtonLeft.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_mapShipsCollectionGeneric == null)
if (ListBoxMaps.getSelectedIndex() == -1)
{
return;
}
@@ -203,7 +222,7 @@ public class FormMapWithSetShipsGeneric extends JFrame{
JLabel imageWithMapAndObject = new JLabel();
imageWithMapAndObject.setPreferredSize(pictureBoxShip.getSize());
imageWithMapAndObject.setMinimumSize(new Dimension(1, 1));
imageWithMapAndObject.setIcon(new ImageIcon(_mapShipsCollectionGeneric.MoveObject(Direction.Left)));
imageWithMapAndObject.setIcon(new ImageIcon(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).MoveObject(Direction.Left)));
pictureBoxShip.add(imageWithMapAndObject, BorderLayout.CENTER);
pictureBoxShip.revalidate();
pictureBoxShip.repaint();
@@ -215,9 +234,62 @@ public class FormMapWithSetShipsGeneric extends JFrame{
public void keyTyped(KeyEvent e) {
char c = e.getKeyChar();
if ( ((c < '0') || (c > '9')) || maskedTextBoxPosition.getText().length() >= 2) {
e.consume();
e.consume(); // игнорим введенные буквы и пробел
}
}
});
ButtonAddMap.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (ComboBoxSelectorMap.getSelectedIndex() == -1 || textBoxNewMapName.getText().isEmpty())
{
JOptionPane.showMessageDialog(null,"Не все данные заполнены","Ошибка",JOptionPane.ERROR_MESSAGE);
return;
}
if(!_mapsDict.containsKey(ComboBoxSelectorMap.getSelectedItem()))
{
JOptionPane.showMessageDialog(null,"Нет такой карты","Ошибка",JOptionPane.ERROR_MESSAGE);
}
_mapsCollection.AddMap(textBoxNewMapName.getText(),_mapsDict.get(ComboBoxSelectorMap.getSelectedItem().toString()));
ReloadMaps();
}
});
ButtonDeleteMap.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (ListBoxMaps.getSelectedIndex() == -1)
{
return;
}
if(JOptionPane.showConfirmDialog(null,"Удалить карту"+ListBoxMaps.getSelectedValue().toString()+"?","Удаление",JOptionPane.YES_NO_OPTION)==0)
{
_mapsCollection.DelMap(ListBoxMaps.getSelectedValue().toString());
ReloadMaps();
}
}
});
ButtonCheckDel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (ListBoxMaps.getSelectedIndex() == -1)
{
return;
}
DrawingObjectShip ship=_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).getShipsDeleted();
if(ship==null)
{
JOptionPane.showMessageDialog(null,"Коллекция пуста","Ошибка",JOptionPane.ERROR_MESSAGE);
return;
}
FormShip formShip=new FormShip(ship);
formShip.setSize(1000,800);
formShip.setVisible(true);
}
});
}
private void createUIComponents() {
DefaultListModel<String> dlm = new DefaultListModel<String>();
ListBoxMaps = new JList(dlm);
}
}

View File

@@ -41,6 +41,14 @@ public class FormShip extends JDialog{
}
validate();
}
public FormShip(DrawingObjectShip ship)
{
super(new Frame("Лайнер"));
CreateWindow();
setModal(true);
_ship=ship.GetDrawingObjectShip();
getContentPane().add(Mainpanel);
}
public FormShip()
{
super(new Frame("Лайнер"));

View File

@@ -18,7 +18,7 @@ public class SetShipsGeneric<T extends Object> implements Iterable<T>{
}
public int Insert(T ship, int position)
{
if (position < 0 || position >= _maxCount) return -1;
if (position < 0 || position > Count() || _maxCount == Count()) return -1;
_places.add(position,ship);
return position;
}