lab#4
This commit is contained in:
parent
d59abaf008
commit
557d46293b
@ -13,6 +13,9 @@ public class DrawingObjectShip implements IDrawingObject {
|
|||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
public DrawingShip GetDrawingObjectShip() {
|
||||||
|
return _ship;
|
||||||
|
}
|
||||||
@Override
|
@Override
|
||||||
public void SetObject(int x, int y, int width, int height) {
|
public void SetObject(int x, int y, int width, int height) {
|
||||||
_ship.SetPosition(x,y,width,height);
|
_ship.SetPosition(x,y,width,height);
|
||||||
|
@ -1,16 +1,20 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="FormMapWithSetShipsGeneric">
|
<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"/>
|
<margin top="0" left="0" bottom="0" right="0"/>
|
||||||
<constraints>
|
<constraints>
|
||||||
<xy x="20" y="20" width="649" height="400"/>
|
<xy x="20" y="20" width="798" height="526"/>
|
||||||
</constraints>
|
</constraints>
|
||||||
<properties/>
|
<properties/>
|
||||||
<border type="none"/>
|
<border type="none"/>
|
||||||
<children>
|
<children>
|
||||||
<grid id="9bb5a" binding="pictureBoxShip" layout-manager="BorderLayout" hgap="0" vgap="0">
|
<grid id="9bb5a" binding="pictureBoxShip" layout-manager="BorderLayout" hgap="0" vgap="0">
|
||||||
<constraints>
|
<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>
|
</constraints>
|
||||||
<properties/>
|
<properties/>
|
||||||
<border type="none"/>
|
<border type="none"/>
|
||||||
@ -18,32 +22,20 @@
|
|||||||
</grid>
|
</grid>
|
||||||
<hspacer id="b305e">
|
<hspacer id="b305e">
|
||||||
<constraints>
|
<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>
|
</constraints>
|
||||||
</hspacer>
|
</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">
|
<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"/>
|
<margin top="0" left="0" bottom="0" right="0"/>
|
||||||
<constraints>
|
<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>
|
</constraints>
|
||||||
<properties/>
|
<properties/>
|
||||||
<border type="none"/>
|
<border type="none"/>
|
||||||
<children>
|
<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="Карта острова"/>
|
|
||||||
<item value="Карта скалы"/>
|
|
||||||
</model>
|
|
||||||
</properties>
|
|
||||||
</component>
|
|
||||||
<component id="cdb80" class="javax.swing.JButton" binding="ButtonAddShip">
|
<component id="cdb80" class="javax.swing.JButton" binding="ButtonAddShip">
|
||||||
<constraints>
|
<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>
|
</constraints>
|
||||||
<properties>
|
<properties>
|
||||||
<text value="Добавить корабль"/>
|
<text value="Добавить корабль"/>
|
||||||
@ -51,7 +43,7 @@
|
|||||||
</component>
|
</component>
|
||||||
<component id="e5494" class="javax.swing.JTextField" binding="maskedTextBoxPosition">
|
<component id="e5494" class="javax.swing.JTextField" binding="maskedTextBoxPosition">
|
||||||
<constraints>
|
<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"/>
|
<preferred-size width="150" height="-1"/>
|
||||||
</grid>
|
</grid>
|
||||||
</constraints>
|
</constraints>
|
||||||
@ -59,7 +51,7 @@
|
|||||||
</component>
|
</component>
|
||||||
<component id="8bb89" class="javax.swing.JButton" binding="ButtonDeleteShip">
|
<component id="8bb89" class="javax.swing.JButton" binding="ButtonDeleteShip">
|
||||||
<constraints>
|
<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>
|
</constraints>
|
||||||
<properties>
|
<properties>
|
||||||
<text value="Удалить корабль"/>
|
<text value="Удалить корабль"/>
|
||||||
@ -67,7 +59,7 @@
|
|||||||
</component>
|
</component>
|
||||||
<component id="aa482" class="javax.swing.JButton" binding="ButtonShowStorage">
|
<component id="aa482" class="javax.swing.JButton" binding="ButtonShowStorage">
|
||||||
<constraints>
|
<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>
|
</constraints>
|
||||||
<properties>
|
<properties>
|
||||||
<text value="Показать хранилище"/>
|
<text value="Показать хранилище"/>
|
||||||
@ -75,7 +67,7 @@
|
|||||||
</component>
|
</component>
|
||||||
<component id="9ebdc" class="javax.swing.JButton" binding="ButtonShowMap">
|
<component id="9ebdc" class="javax.swing.JButton" binding="ButtonShowMap">
|
||||||
<constraints>
|
<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>
|
</constraints>
|
||||||
<properties>
|
<properties>
|
||||||
<text value="Показать карту"/>
|
<text value="Показать карту"/>
|
||||||
@ -129,6 +121,78 @@
|
|||||||
<text value=""/>
|
<text value=""/>
|
||||||
</properties>
|
</properties>
|
||||||
</component>
|
</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="Карта острова"/>
|
||||||
|
<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>
|
</children>
|
||||||
</grid>
|
</grid>
|
||||||
</children>
|
</children>
|
||||||
|
@ -6,7 +6,9 @@ import java.awt.event.ActionListener;
|
|||||||
import java.awt.event.KeyAdapter;
|
import java.awt.event.KeyAdapter;
|
||||||
import java.awt.event.KeyEvent;
|
import java.awt.event.KeyEvent;
|
||||||
import java.awt.image.BufferedImage;
|
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 class FormMapWithSetShipsGeneric extends JFrame{
|
||||||
public JPanel Mainpanel;
|
public JPanel Mainpanel;
|
||||||
private JPanel pictureBoxShip;
|
private JPanel pictureBoxShip;
|
||||||
@ -21,7 +23,22 @@ public class FormMapWithSetShipsGeneric extends JFrame{
|
|||||||
private JButton ButtonUp;
|
private JButton ButtonUp;
|
||||||
private JButton ButtonRight;
|
private JButton ButtonRight;
|
||||||
private JTextField maskedTextBoxPosition;
|
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 MapWithSetShipsGeneric<DrawingObjectShip,AbstractMap> _mapShipsCollectionGeneric;
|
||||||
|
private final HashMap<String,AbstractMap> _mapsDict = new HashMap<>(){{
|
||||||
|
put("Простая карта",new SimpleMap());
|
||||||
|
put("Карта острова",new IslandsMap());
|
||||||
|
put("Карта скалы",new RocksMap());
|
||||||
|
}};
|
||||||
|
private final MapsCollection _mapsCollection;
|
||||||
public void UpdateWindow(BufferedImage bmp)
|
public void UpdateWindow(BufferedImage bmp)
|
||||||
{
|
{
|
||||||
pictureBoxShip.removeAll();
|
pictureBoxShip.removeAll();
|
||||||
@ -32,8 +49,30 @@ public class FormMapWithSetShipsGeneric extends JFrame{
|
|||||||
pictureBoxShip.add(imageWithMapAndObject, BorderLayout.CENTER);
|
pictureBoxShip.add(imageWithMapAndObject, BorderLayout.CENTER);
|
||||||
pictureBoxShip.revalidate();
|
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()
|
public FormMapWithSetShipsGeneric()
|
||||||
{
|
{
|
||||||
|
_mapsCollection = new MapsCollection(picWidth, picHeight);
|
||||||
|
ComboBoxSelectorMap.removeAllItems();
|
||||||
|
for (String elem : _mapsDict.keySet()) {
|
||||||
|
ComboBoxSelectorMap.addItem(elem);
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
Image img = ImageIO.read(FormShip.class.getResource("Images/4.png"));
|
Image img = ImageIO.read(FormShip.class.getResource("Images/4.png"));
|
||||||
ButtonUp.setIcon(new ImageIcon(img));
|
ButtonUp.setIcon(new ImageIcon(img));
|
||||||
@ -47,51 +86,22 @@ public class FormMapWithSetShipsGeneric extends JFrame{
|
|||||||
System.out.println(ex);
|
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 IslandsMap();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case "Карта скалы": {
|
|
||||||
map = new RocksMap();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if(map!=null)
|
|
||||||
{
|
|
||||||
_mapShipsCollectionGeneric = new MapWithSetShipsGeneric<DrawingObjectShip,AbstractMap>(pictureBoxShip.getWidth(),pictureBoxShip.getHeight(),map);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
_mapShipsCollectionGeneric=null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
ButtonAddShip.addActionListener(new ActionListener() {
|
ButtonAddShip.addActionListener(new ActionListener() {
|
||||||
@Override
|
@Override
|
||||||
public void actionPerformed(ActionEvent e) {
|
public void actionPerformed(ActionEvent e) {
|
||||||
if(_mapShipsCollectionGeneric==null)
|
if (ListBoxMaps.getSelectedIndex() == -1)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
FormShip form = new FormShip();
|
FormShip form = new FormShip();
|
||||||
form.setSize(1000,700);
|
form.setSize(1000,700);
|
||||||
form.setVisible(true);
|
form.setVisible(true);
|
||||||
|
form.setModal(true);
|
||||||
DrawingObjectShip ship = new DrawingObjectShip(form.GetSelectedShip());
|
DrawingObjectShip ship = new DrawingObjectShip(form.GetSelectedShip());
|
||||||
if(_mapShipsCollectionGeneric.Add(ship)!=-1)
|
if(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).Add(ship)!=-1)
|
||||||
{
|
{
|
||||||
JOptionPane.showMessageDialog(null,"Объект добавлен");
|
JOptionPane.showMessageDialog(null,"Объект добавлен");
|
||||||
UpdateWindow(_mapShipsCollectionGeneric.ShowSet());
|
UpdateWindow(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).ShowSet());
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -99,7 +109,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() {
|
ButtonDeleteShip.addActionListener(new ActionListener() {
|
||||||
@Override
|
@Override
|
||||||
public void actionPerformed(ActionEvent e) {
|
public void actionPerformed(ActionEvent e) {
|
||||||
@ -113,10 +130,10 @@ public class FormMapWithSetShipsGeneric extends JFrame{
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
int pos=Integer.parseInt(maskedTextBoxPosition.getText());
|
int pos=Integer.parseInt(maskedTextBoxPosition.getText());
|
||||||
if(_mapShipsCollectionGeneric.Delete(pos)!=null)
|
if(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).Delete(pos)!=null)
|
||||||
{
|
{
|
||||||
JOptionPane.showMessageDialog(null, "Объект удален");
|
JOptionPane.showMessageDialog(null, "Объект удален");
|
||||||
UpdateWindow(_mapShipsCollectionGeneric.ShowSet());
|
UpdateWindow(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).ShowSet());
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -127,27 +144,27 @@ public class FormMapWithSetShipsGeneric extends JFrame{
|
|||||||
ButtonShowStorage.addActionListener(new ActionListener() {
|
ButtonShowStorage.addActionListener(new ActionListener() {
|
||||||
@Override
|
@Override
|
||||||
public void actionPerformed(ActionEvent e) {
|
public void actionPerformed(ActionEvent e) {
|
||||||
if (_mapShipsCollectionGeneric == null)
|
if (ListBoxMaps.getSelectedIndex() == -1)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
UpdateWindow(_mapShipsCollectionGeneric.ShowSet());
|
UpdateWindow(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).ShowSet());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
ButtonShowMap.addActionListener(new ActionListener() {
|
ButtonShowMap.addActionListener(new ActionListener() {
|
||||||
@Override
|
@Override
|
||||||
public void actionPerformed(ActionEvent e) {
|
public void actionPerformed(ActionEvent e) {
|
||||||
if (_mapShipsCollectionGeneric == null)
|
if (ListBoxMaps.getSelectedIndex() == -1)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
UpdateWindow(_mapShipsCollectionGeneric.ShowOnMap());
|
UpdateWindow(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).ShowOnMap());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
ButtonUp.addActionListener(new ActionListener() {
|
ButtonUp.addActionListener(new ActionListener() {
|
||||||
@Override
|
@Override
|
||||||
public void actionPerformed(ActionEvent e) {
|
public void actionPerformed(ActionEvent e) {
|
||||||
if (_mapShipsCollectionGeneric == null)
|
if (ListBoxMaps.getSelectedIndex() == -1)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -155,7 +172,7 @@ public class FormMapWithSetShipsGeneric extends JFrame{
|
|||||||
JLabel imageWithMapAndObject = new JLabel();
|
JLabel imageWithMapAndObject = new JLabel();
|
||||||
imageWithMapAndObject.setPreferredSize(pictureBoxShip.getSize());
|
imageWithMapAndObject.setPreferredSize(pictureBoxShip.getSize());
|
||||||
imageWithMapAndObject.setMinimumSize(new Dimension(1, 1));
|
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.add(imageWithMapAndObject, BorderLayout.CENTER);
|
||||||
pictureBoxShip.revalidate();
|
pictureBoxShip.revalidate();
|
||||||
pictureBoxShip.repaint();
|
pictureBoxShip.repaint();
|
||||||
@ -164,7 +181,7 @@ public class FormMapWithSetShipsGeneric extends JFrame{
|
|||||||
ButtonDown.addActionListener(new ActionListener() {
|
ButtonDown.addActionListener(new ActionListener() {
|
||||||
@Override
|
@Override
|
||||||
public void actionPerformed(ActionEvent e) {
|
public void actionPerformed(ActionEvent e) {
|
||||||
if (_mapShipsCollectionGeneric == null)
|
if (ListBoxMaps.getSelectedIndex() == -1)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -172,7 +189,7 @@ public class FormMapWithSetShipsGeneric extends JFrame{
|
|||||||
JLabel imageWithMapAndObject = new JLabel();
|
JLabel imageWithMapAndObject = new JLabel();
|
||||||
imageWithMapAndObject.setPreferredSize(pictureBoxShip.getSize());
|
imageWithMapAndObject.setPreferredSize(pictureBoxShip.getSize());
|
||||||
imageWithMapAndObject.setMinimumSize(new Dimension(1, 1));
|
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.add(imageWithMapAndObject, BorderLayout.CENTER);
|
||||||
pictureBoxShip.revalidate();
|
pictureBoxShip.revalidate();
|
||||||
pictureBoxShip.repaint();
|
pictureBoxShip.repaint();
|
||||||
@ -181,7 +198,7 @@ public class FormMapWithSetShipsGeneric extends JFrame{
|
|||||||
ButtonRight.addActionListener(new ActionListener() {
|
ButtonRight.addActionListener(new ActionListener() {
|
||||||
@Override
|
@Override
|
||||||
public void actionPerformed(ActionEvent e) {
|
public void actionPerformed(ActionEvent e) {
|
||||||
if (_mapShipsCollectionGeneric == null)
|
if (ListBoxMaps.getSelectedIndex() == -1)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -189,7 +206,7 @@ public class FormMapWithSetShipsGeneric extends JFrame{
|
|||||||
JLabel imageWithMapAndObject = new JLabel();
|
JLabel imageWithMapAndObject = new JLabel();
|
||||||
imageWithMapAndObject.setPreferredSize(pictureBoxShip.getSize());
|
imageWithMapAndObject.setPreferredSize(pictureBoxShip.getSize());
|
||||||
imageWithMapAndObject.setMinimumSize(new Dimension(1, 1));
|
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.add(imageWithMapAndObject, BorderLayout.CENTER);
|
||||||
pictureBoxShip.revalidate();
|
pictureBoxShip.revalidate();
|
||||||
pictureBoxShip.repaint();
|
pictureBoxShip.repaint();
|
||||||
@ -198,7 +215,7 @@ public class FormMapWithSetShipsGeneric extends JFrame{
|
|||||||
ButtonLeft.addActionListener(new ActionListener() {
|
ButtonLeft.addActionListener(new ActionListener() {
|
||||||
@Override
|
@Override
|
||||||
public void actionPerformed(ActionEvent e) {
|
public void actionPerformed(ActionEvent e) {
|
||||||
if (_mapShipsCollectionGeneric == null)
|
if (ListBoxMaps.getSelectedIndex() == -1)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -206,7 +223,7 @@ public class FormMapWithSetShipsGeneric extends JFrame{
|
|||||||
JLabel imageWithMapAndObject = new JLabel();
|
JLabel imageWithMapAndObject = new JLabel();
|
||||||
imageWithMapAndObject.setPreferredSize(pictureBoxShip.getSize());
|
imageWithMapAndObject.setPreferredSize(pictureBoxShip.getSize());
|
||||||
imageWithMapAndObject.setMinimumSize(new Dimension(1, 1));
|
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.add(imageWithMapAndObject, BorderLayout.CENTER);
|
||||||
pictureBoxShip.revalidate();
|
pictureBoxShip.revalidate();
|
||||||
pictureBoxShip.repaint();
|
pictureBoxShip.repaint();
|
||||||
@ -222,5 +239,58 @@ public class FormMapWithSetShipsGeneric extends JFrame{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -44,8 +44,21 @@ public class FormShip extends JDialog{
|
|||||||
}
|
}
|
||||||
validate();
|
validate();
|
||||||
}
|
}
|
||||||
|
public FormShip(DrawingObjectShip ship)
|
||||||
|
{
|
||||||
|
super(new Frame("Корабль"));
|
||||||
|
CreateWindow();
|
||||||
|
setModal(true);
|
||||||
|
_ship=ship.GetDrawingObjectShip();
|
||||||
|
getContentPane().add(Mainpanel);
|
||||||
|
}
|
||||||
public FormShip() {
|
public FormShip() {
|
||||||
super(new Frame("Корабль"));
|
super(new Frame("Корабль"));
|
||||||
|
CreateWindow();
|
||||||
|
setModal(true);
|
||||||
|
getContentPane().add(Mainpanel);
|
||||||
|
}
|
||||||
|
public void CreateWindow(){
|
||||||
Box LabelBox = Box.createHorizontalBox();
|
Box LabelBox = Box.createHorizontalBox();
|
||||||
LabelBox.setMinimumSize(new Dimension(1, 20));
|
LabelBox.setMinimumSize(new Dimension(1, 20));
|
||||||
LabelBox.add(JLabelSpeed);
|
LabelBox.add(JLabelSpeed);
|
||||||
@ -143,7 +156,5 @@ public class FormShip extends JDialog{
|
|||||||
dispose();
|
dispose();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
setModal(true);
|
|
||||||
getContentPane().add(Mainpanel);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
import java.awt.image.BufferedImage;
|
import java.awt.image.BufferedImage;
|
||||||
|
import java.util.Stack;
|
||||||
|
|
||||||
public class MapWithSetShipsGeneric<T extends IDrawingObject, U extends AbstractMap> {
|
public class MapWithSetShipsGeneric<T extends IDrawingObject, U extends AbstractMap> {
|
||||||
private final int _pictureWidth;
|
private final int _pictureWidth;
|
||||||
@ -8,8 +9,10 @@ public class MapWithSetShipsGeneric<T extends IDrawingObject, U extends Abstrac
|
|||||||
private final int _placeSizeHeight = 90;
|
private final int _placeSizeHeight = 90;
|
||||||
private final SetShipGeneric<T> _setShips;
|
private final SetShipGeneric<T> _setShips;
|
||||||
private final U _map;
|
private final U _map;
|
||||||
|
private Stack<T> _deletedShips;
|
||||||
public MapWithSetShipsGeneric(int picWidth, int picHeight, U map)
|
public MapWithSetShipsGeneric(int picWidth, int picHeight, U map)
|
||||||
{
|
{
|
||||||
|
_deletedShips=new Stack<>();
|
||||||
int width = picWidth / _placeSizeWidth;
|
int width = picWidth / _placeSizeWidth;
|
||||||
int height = picHeight / _placeSizeHeight;
|
int height = picHeight / _placeSizeHeight;
|
||||||
_setShips = new SetShipGeneric<T>(width * height);
|
_setShips = new SetShipGeneric<T>(width * height);
|
||||||
@ -23,7 +26,9 @@ public class MapWithSetShipsGeneric<T extends IDrawingObject, U extends Abstrac
|
|||||||
}
|
}
|
||||||
public T Delete(int position)
|
public T Delete(int position)
|
||||||
{
|
{
|
||||||
return _setShips.Remove(position);
|
T ship=_setShips.Remove(position);
|
||||||
|
_deletedShips.push(ship);
|
||||||
|
return ship;
|
||||||
}
|
}
|
||||||
public BufferedImage ShowSet()
|
public BufferedImage ShowSet()
|
||||||
{
|
{
|
||||||
@ -36,16 +41,23 @@ public class MapWithSetShipsGeneric<T extends IDrawingObject, U extends Abstrac
|
|||||||
public BufferedImage ShowOnMap()
|
public BufferedImage ShowOnMap()
|
||||||
{
|
{
|
||||||
Shaking();
|
Shaking();
|
||||||
for (int i = 0; i < _setShips.Count(); i++)
|
for (var ship : _setShips)
|
||||||
{
|
{
|
||||||
var ship = _setShips.Get(i);
|
return _map.CreateMap(_pictureWidth,_pictureHeight,ship);
|
||||||
if (ship != null)
|
|
||||||
{
|
|
||||||
return _map.CreateMap(_pictureWidth, _pictureHeight, ship);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_RGB);
|
return new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_RGB);
|
||||||
}
|
}
|
||||||
|
public T GetSelectedShip(int ind){
|
||||||
|
return _setShips.Get(ind);
|
||||||
|
}
|
||||||
|
public T GetShipsDeleted()
|
||||||
|
{
|
||||||
|
if(_deletedShips.isEmpty())
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return _deletedShips.pop();
|
||||||
|
}
|
||||||
public BufferedImage MoveObject(Direction direction)
|
public BufferedImage MoveObject(Direction direction)
|
||||||
{
|
{
|
||||||
if (_map != null)
|
if (_map != null)
|
||||||
|
45
MapsCollection.java
Normal file
45
MapsCollection.java
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
import java.util.AbstractCollection;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
|
||||||
|
public class MapsCollection {
|
||||||
|
private final HashMap<String,MapWithSetShipsGeneric<DrawingObjectShip, AbstractMap>> _mapStorages;
|
||||||
|
public ArrayList<String> Keys()
|
||||||
|
{
|
||||||
|
return new ArrayList<>(_mapStorages.keySet());
|
||||||
|
}
|
||||||
|
private final int _pictureWidth;
|
||||||
|
private final int _pictureHeight;
|
||||||
|
public MapsCollection(int pictureWidth,int pictureHeight)
|
||||||
|
{
|
||||||
|
_mapStorages=new HashMap<>();
|
||||||
|
_pictureWidth=pictureWidth;
|
||||||
|
_pictureHeight=pictureHeight;
|
||||||
|
}
|
||||||
|
public void AddMap(String Name, AbstractMap Map)
|
||||||
|
{
|
||||||
|
if(!_mapStorages.containsKey(Name))
|
||||||
|
{
|
||||||
|
_mapStorages.put(Name,new MapWithSetShipsGeneric<>(_pictureWidth,_pictureHeight,Map));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public void DelMap(String name)
|
||||||
|
{
|
||||||
|
_mapStorages.remove(name);
|
||||||
|
}
|
||||||
|
public MapWithSetShipsGeneric<DrawingObjectShip,AbstractMap> Get(String ind)
|
||||||
|
{
|
||||||
|
if(_mapStorages.containsKey(ind))
|
||||||
|
{
|
||||||
|
return _mapStorages.get(ind);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
public DrawingObjectShip Get(String name, int ind) {
|
||||||
|
if (_mapStorages.containsKey(name))
|
||||||
|
{
|
||||||
|
return _mapStorages.get(name).GetSelectedShip(ind);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
@ -1,12 +1,17 @@
|
|||||||
public class SetShipGeneric<T extends Object> {
|
import java.util.ArrayList;
|
||||||
private final Object[] _places;
|
import java.util.Iterator;
|
||||||
|
public class SetShipGeneric<T extends Object> implements Iterable<T>{
|
||||||
|
private ArrayList<T> _places;
|
||||||
|
private final int _maxCount;
|
||||||
|
//private final Object[] _places;
|
||||||
public int Count()
|
public int Count()
|
||||||
{
|
{
|
||||||
return _places.length;
|
return _places.size();
|
||||||
}
|
}
|
||||||
public SetShipGeneric(int count)
|
public SetShipGeneric(int count)
|
||||||
{
|
{
|
||||||
_places = new Object[count];
|
_maxCount=count;
|
||||||
|
_places = new ArrayList<>();
|
||||||
}
|
}
|
||||||
public int Insert(T ship)
|
public int Insert(T ship)
|
||||||
{
|
{
|
||||||
@ -14,52 +19,38 @@ public class SetShipGeneric<T extends Object> {
|
|||||||
}
|
}
|
||||||
public int Insert(T ship, int position)
|
public int Insert(T ship, int position)
|
||||||
{
|
{
|
||||||
if (position >= _places.length || position < 0)
|
if (position < 0 || position > Count() || _maxCount == Count()) return -1;
|
||||||
{
|
_places.add(position,ship);
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
if (_places[position] == null)
|
|
||||||
{
|
|
||||||
_places[position] = ship;
|
|
||||||
return position;
|
|
||||||
}
|
|
||||||
int emptyIndex = -1;
|
|
||||||
for (int i = position + 1; i < Count(); i++)
|
|
||||||
{
|
|
||||||
if (_places[i] == null)
|
|
||||||
{
|
|
||||||
emptyIndex = i;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (emptyIndex < 0)
|
|
||||||
{
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
for (int i = emptyIndex; i > position; i--)
|
|
||||||
{
|
|
||||||
_places[i] = _places[i - 1];
|
|
||||||
}
|
|
||||||
_places[position] = ship;
|
|
||||||
return position;
|
return position;
|
||||||
}
|
}
|
||||||
public T Remove(int position)
|
public T Remove(int position)
|
||||||
{
|
{
|
||||||
if (position >= _places.length || position < 0)
|
if (position >= Count() || position < 0)
|
||||||
{
|
{
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
T ship = (T) _places[position];
|
T ship = (T) _places.get(position);
|
||||||
_places[position] = null;
|
_places.remove(ship);
|
||||||
return ship;
|
return ship;
|
||||||
}
|
}
|
||||||
public T Get(int position)
|
public T Get(int position)
|
||||||
{
|
{
|
||||||
if (position >= _places.length || position < 0)
|
if (position >= Count() || position<0)
|
||||||
{
|
{
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
return (T)_places[position];
|
return _places.get(position);
|
||||||
|
}
|
||||||
|
public void Set(int position,T value)
|
||||||
|
{
|
||||||
|
if (position < _maxCount || position >= 0)
|
||||||
|
{
|
||||||
|
Insert(value,position);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public Iterator<T> iterator() {
|
||||||
|
return _places.iterator();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user