Presnyakova V.V Lab_4 #4

Closed
Victoria_Presnyakova wants to merge 4 commits from Lab_4 into Lab_3
8 changed files with 443 additions and 286 deletions

View File

@ -1,5 +1,4 @@
import java.awt.*;
import java.util.Enumeration;
public class DrawningObjectBoat implements IDrawningObject {
private DrawningBoat _boat = null;
@ -36,4 +35,8 @@ public class DrawningObjectBoat implements IDrawningObject {
return _boat.GetCurrentPosition();
return null;
}
public DrawningBoat GetDrawningObjectBoat() {
return _boat;
}
}

View File

@ -23,7 +23,6 @@ public class FormBoat extends JDialog{
private final JLabel JLabelWeight = new JLabel();
private final JLabel JLabelColor = new JLabel();
private DrawningBoat SelectedBoat;
public DrawningBoat GetSelectedBoat() {
return SelectedBoat;
}
@ -50,7 +49,7 @@ public class FormBoat extends JDialog{
JLabelWeight.setText("Вес: " + _boat.GetBoat().GetWeight() + " ");
JLabelColor.setText(("Цвет: " + _boat.GetBoat().GetBodyColor() + " "));
}
public FormBoat() {
private void CreateWindow(){
add(Mainpanel);
Box LabelBox = Box.createHorizontalBox();
LabelBox.setMinimumSize(new Dimension(1, 20));
@ -121,4 +120,18 @@ public class FormBoat extends JDialog{
dispose();
});
}
public FormBoat() {
super(new Frame("Лодка"));
CreateWindow();
setModal(true);
getContentPane().add(Mainpanel);
}
public FormBoat(DrawningObjectBoat boat){
super(new Frame("Корабль"));
CreateWindow();
setModal(true);
_boat = boat.GetDrawningObjectBoat();
getContentPane().add(Mainpanel);
}
}

View File

@ -1,164 +1,200 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="FormMapWithSetBoats">
<grid id="27dc6" binding="Mainpanel" layout-manager="GridLayoutManager" row-count="1" 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="785" height="400"/>
<xy x="20" y="20" width="798" height="526"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<grid id="afa3d" binding="PictureBox" layout-manager="FlowLayout" hgap="5" vgap="5" flow-align="1">
<grid id="9bb5a" binding="pictureBoxBoat" 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="500"/>
<preferred-size width="600" height="500"/>
<maximum-size width="600" height="500"/>
</grid>
</constraints>
<properties/>
<properties>
<enabled value="true"/>
</properties>
<border type="none"/>
<children/>
</grid>
<grid id="e9a63" binding="groupBox" layout-manager="GridLayoutManager" row-count="10" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<hspacer id="b305e">
<constraints>
<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="1" col-span="1" vsize-policy="3" hsize-policy="0" anchor="0" fill="3" indent="0" use-parent-layout="false">
<minimum-size width="150" height="-1"/>
</grid>
<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="f23ee" class="javax.swing.JButton" binding="buttonAdd">
<component id="cdb80" class="javax.swing.JButton" binding="ButtonAddBoat">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" 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="Добавить лодку"/>
</properties>
</component>
<component id="2e61e" class="javax.swing.JComboBox" binding="comboBoxSelectorMap">
<component id="e5494" class="javax.swing.JTextField" binding="maskedTextBoxPosition">
<constraints>
<grid row="0" 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/>
</properties>
</component>
<component id="5c428" class="javax.swing.JButton" binding="buttonRemove">
<constraints>
<grid row="4" 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="e24b9" class="javax.swing.JButton" binding="buttonShowStorage">
<constraints>
<grid row="6" 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="ac087" class="javax.swing.JButton" binding="buttonShowOnMap">
<constraints>
<grid row="7" 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>
<grid id="2821b" layout-manager="GridLayoutManager" row-count="2" column-count="5" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="9" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="0" anchor="0" fill="3" indent="0" use-parent-layout="false">
<minimum-size width="150" height="-1"/>
</grid>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="6d803" class="javax.swing.JButton" binding="buttonLeft">
<constraints>
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<text value=""/>
</properties>
</component>
<component id="c0db2" class="javax.swing.JButton" binding="buttonUp">
<constraints>
<grid row="0" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<text value=""/>
</properties>
</component>
<component id="ca0da" class="javax.swing.JButton" binding="buttonDown">
<constraints>
<grid row="1" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<text value=""/>
</properties>
</component>
<component id="c3e45" class="javax.swing.JButton" binding="buttonRight">
<constraints>
<grid row="1" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<text value=""/>
</properties>
</component>
<hspacer id="1f200">
<constraints>
<grid row="1" column="4" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<hspacer id="f8b3b">
<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"/>
</constraints>
</hspacer>
</children>
</grid>
<vspacer id="b833e">
<constraints>
<grid row="8" 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="181ac" class="javax.swing.JTextField" binding="textBoxPosition">
<constraints>
<grid row="3" column="0" row-span="1" col-span="1" 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>
<properties/>
</component>
<vspacer id="53ec0">
<component id="8bb89" class="javax.swing.JButton" binding="ButtonDeleteBoat">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" 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="Удалить лодку"/>
</properties>
</component>
<component id="aa482" class="javax.swing.JButton" binding="ButtonShowStorage">
<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"/>
</constraints>
<properties>
<text value="Показать хранилище"/>
</properties>
</component>
<component id="9ebdc" class="javax.swing.JButton" binding="ButtonShowMap">
<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"/>
</constraints>
<properties>
<text value="Показать карту"/>
</properties>
</component>
<component id="acbc" class="javax.swing.JButton" binding="ButtonUp">
<constraints>
<grid row="6" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="3" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<text value=""/>
</properties>
</component>
<component id="a584e" class="javax.swing.JButton" binding="ButtonDown">
<constraints>
<grid row="7" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="3" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<text value=""/>
</properties>
</component>
<component id="e9fe6" class="javax.swing.JButton" binding="ButtonLeft">
<constraints>
<grid row="7" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="3" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<text value=""/>
</properties>
</component>
<component id="25a22" class="javax.swing.JButton" binding="ButtonRight">
<constraints>
<grid row="7" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="3" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<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>
<vspacer id="a6919">
<component id="95ba3" class="javax.swing.JComboBox" binding="ComboBoxSelectorMap">
<constraints>
<grid row="5" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
<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>
</grid>
</children>

View File

@ -1,39 +1,68 @@
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.util.HashMap;
public class FormMapWithSetBoats extends JFrame {
private MapWithSetBoatsGeneric<DrawningObjectBoat, AbstractMap> _mapBoatsCollectionGeneric;
public class FormMapWithSetBoats extends JFrame{
public JPanel Mainpanel;
private JButton buttonAdd;
private JComboBox comboBoxSelectorMap;
private JTextField textBoxPosition;
private JButton buttonRemove;
private JButton buttonShowStorage;
private JButton buttonShowOnMap;
private JButton buttonUp;
private JButton buttonLeft;
private JButton buttonRight;
private JButton buttonDown;
private JPanel PictureBox;
private JPanel groupBox;
AbstractMap _abstractMap;
private JFrame getFrame() {
JFrame frame = new JFrame();
frame.setVisible(false);
frame.setBounds(300, 100, 800, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
return frame;
private JPanel pictureBoxBoat;
private JPanel GroupBoxTools;
private JComboBox ComboBoxSelectorMap;
private JButton ButtonAddBoat;
private JButton ButtonDeleteBoat;
private JButton ButtonShowStorage;
private JButton ButtonShowMap;
private JButton ButtonLeft;
private JButton ButtonDown;
private JButton ButtonUp;
private JButton ButtonRight;
private JTextField maskedTextBoxPosition;
private JTextField textBoxNewMapName;
private int picWidth=600;
private int picHeight=500;
private JButton ButtonAddMap;
private JList ListBoxMaps;
private JButton ButtonDeleteMap;
private JPanel GroupBoxMaps;
private JButton ButtonCheckDel;
private MapWithSetBoatsGeneric<DrawningObjectBoat,AbstractMap> _mapBoatsCollectionGeneric;
private final HashMap<String,AbstractMap> _mapsDict = new HashMap<>(){{
put("Простая карта",new SimpleMap());
put("Вторая карта",new MyMap());
}};
private final MapsCollection _mapsCollection;
public void UpdateWindow(BufferedImage bmp)
{
pictureBoxBoat.removeAll();
JLabel imageWithMapAndObject = new JLabel();
imageWithMapAndObject.setPreferredSize(pictureBoxBoat.getSize());
imageWithMapAndObject.setMinimumSize(new Dimension(1, 1));
imageWithMapAndObject.setIcon(new ImageIcon(bmp));
pictureBoxBoat.add(imageWithMapAndObject, BorderLayout.CENTER);
pictureBoxBoat.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);
}
}
JFrame jFrame = getFrame();
private void ButtonMove_Click(String name)
{
if (_mapBoatsCollectionGeneric == null) return;
Direction direction = Direction.None;
switch (name)
{
@ -50,54 +79,30 @@ public class FormMapWithSetBoats extends JFrame {
direction = Direction.Down;
break;
}
PictureBox.removeAll();
JLabel imageWithMapAndObject = new JLabel();
imageWithMapAndObject.setPreferredSize(PictureBox.getSize());
imageWithMapAndObject.setMinimumSize(new Dimension(1, 1));
imageWithMapAndObject.setIcon(new ImageIcon(_mapBoatsCollectionGeneric.MoveObject(direction)));
PictureBox.add(imageWithMapAndObject, BorderLayout.CENTER);
PictureBox.revalidate();
PictureBox.repaint();
UpdateWindow(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).MoveObject(direction));
}
public FormMapWithSetBoats() {
comboBoxSelectorMap.addItem("Простая карта");
comboBoxSelectorMap.addItem("Вторая карта");
public FormMapWithSetBoats()
{
_mapsCollection = new MapsCollection(picWidth, picHeight);
ComboBoxSelectorMap.removeAllItems();
for (String elem : _mapsDict.keySet()) {
ComboBoxSelectorMap.addItem(elem);
}
try {
Image img = ImageIO.read(FormMapWithSetBoats.class.getResource("/Resource/up.jpg"));
buttonUp.setIcon(new ImageIcon(img));
img = ImageIO.read(FormMapWithSetBoats.class.getResource("/Resource/down.jpg"));
buttonDown.setIcon(new ImageIcon(img));
img = ImageIO.read(FormMapWithSetBoats.class.getResource("/Resource/left.jpg"));
buttonLeft.setIcon(new ImageIcon(img));
img = ImageIO.read(FormMapWithSetBoats.class.getResource("/Resource/right.jpg"));
buttonRight.setIcon(new ImageIcon(img));
Image img = ImageIO.read(FormBoat.class.getResource("/Resource/up.jpg"));
ButtonUp.setIcon(new ImageIcon(img));
img = ImageIO.read(FormBoat.class.getResource("/Resource/down.jpg"));
ButtonDown.setIcon(new ImageIcon(img));
img = ImageIO.read(FormBoat.class.getResource("/Resource/left.jpg"));
ButtonLeft.setIcon(new ImageIcon(img));
img = ImageIO.read(FormBoat.class.getResource("/Resource/right.jpg"));
ButtonRight.setIcon(new ImageIcon(img));
} catch (Exception ex) {
System.out.println(ex);
}
comboBoxSelectorMap.addActionListener(e -> {
AbstractMap map = null;
switch (comboBoxSelectorMap.getSelectedItem().toString())
{
case "Простая карта":
map = new SimpleMap();
break;
case "Вторая карта":
map = new MyMap();
break;
}
if (map != null)
{
_mapBoatsCollectionGeneric = new MapWithSetBoatsGeneric(PictureBox.getWidth(), PictureBox.getHeight(), map);
}
else
{
_mapBoatsCollectionGeneric = null;
}
});
buttonAdd.addActionListener(e -> {
if (_mapBoatsCollectionGeneric == null)
ButtonAddBoat.addActionListener(e -> {
if (ListBoxMaps.getSelectedIndex() == -1)
{
return;
}
@ -109,84 +114,113 @@ public class FormMapWithSetBoats extends JFrame {
dialog.setVisible(true);
if (dialog.GetSelectedBoat() == null) return;
DrawningObjectBoat boat = new DrawningObjectBoat(dialog.GetSelectedBoat());
if (_mapBoatsCollectionGeneric.addBoat(boat) != -1)
DrawningObjectBoat Boat = new DrawningObjectBoat(dialog.GetSelectedBoat());
if(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).addBoat(Boat)!=-1)
{
JOptionPane.showMessageDialog(jFrame, "Объект добавлен");
PictureBox.removeAll();
JLabel imageOfBoat = new JLabel();
imageOfBoat.setPreferredSize(PictureBox.getSize());
imageOfBoat.setMinimumSize(new Dimension(1, 1));
imageOfBoat.setIcon(new ImageIcon(_mapBoatsCollectionGeneric.ShowSet()));
PictureBox.add(imageOfBoat,BorderLayout.CENTER);
PictureBox.revalidate();
PictureBox.repaint();
JOptionPane.showMessageDialog(null,"Объект добавлен");
UpdateWindow(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).ShowSet());
}
else
{
JOptionPane.showMessageDialog(jFrame, "Не удалось добавить объект");
JOptionPane.showMessageDialog(null, "Не удалось добавить объект");
}
});
buttonRemove.addActionListener(e -> {
if(_mapBoatsCollectionGeneric == null) return;
String text = textBoxPosition.getText();
if(text.isEmpty()) return;
if(JOptionPane.showConfirmDialog(jFrame, "Вы действительно хотите удалить объект?", "Удаление", JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION) return;
try {
Integer.parseInt(text);
}
catch (Exception ex){
ListBoxMaps.addListSelectionListener(e -> {
if (ListBoxMaps.getSelectedIndex() == -1)
return;
UpdateWindow(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).ShowSet());
});
ButtonDeleteBoat.addActionListener(e -> {
if (maskedTextBoxPosition.getText().isEmpty())
{
return;
}
int pos = Integer.parseInt(text);
if (_mapBoatsCollectionGeneric.removeBoat(pos) != null)
int result = JOptionPane.showConfirmDialog(null,"Удалить объект?","Удаление",JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE);
if(result==JOptionPane.NO_OPTION)
{
JOptionPane.showMessageDialog(jFrame, "Объект удален");
PictureBox.removeAll();
JLabel imageOfShip = new JLabel();
imageOfShip.setPreferredSize(PictureBox.getSize());
imageOfShip.setMinimumSize(new Dimension(1, 1));
imageOfShip.setIcon(new ImageIcon(_mapBoatsCollectionGeneric.ShowSet()));
PictureBox.add(imageOfShip,BorderLayout.CENTER);
PictureBox.revalidate();
PictureBox.repaint();
return;
}
int pos=Integer.parseInt(maskedTextBoxPosition.getText());
if(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).removeBoat(pos)!=null)
{
JOptionPane.showMessageDialog(null, "Объект удален");
UpdateWindow(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).ShowSet());
}
else
{
JOptionPane.showMessageDialog(jFrame, "Не удалось удалить объект");
JOptionPane.showMessageDialog(null, "Не удалось удалить объект");
}
});
buttonShowStorage.addActionListener(e -> {
if (_mapBoatsCollectionGeneric == null) return;
PictureBox.removeAll();
JLabel imageOfShip = new JLabel();
imageOfShip.setPreferredSize(PictureBox.getSize());
imageOfShip.setMinimumSize(new Dimension(1, 1));
imageOfShip.setIcon(new ImageIcon(_mapBoatsCollectionGeneric.ShowSet()));
PictureBox.add(imageOfShip,BorderLayout.CENTER);
PictureBox.revalidate();
PictureBox.repaint();
ButtonShowStorage.addActionListener(e -> {
if (ListBoxMaps.getSelectedIndex() == -1)
{
return;
}
UpdateWindow(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).ShowSet());
});
buttonShowOnMap.addActionListener(e -> {
if (_mapBoatsCollectionGeneric == null) return;
PictureBox.removeAll();
JLabel imageOfShip = new JLabel();
imageOfShip.setPreferredSize(PictureBox.getSize());
imageOfShip.setMinimumSize(new Dimension(1, 1));
imageOfShip.setIcon(new ImageIcon(_mapBoatsCollectionGeneric.ShowOnMap()));
PictureBox.add(imageOfShip,BorderLayout.CENTER);
PictureBox.revalidate();
PictureBox.repaint();
ButtonShowMap.addActionListener(e -> {
if (ListBoxMaps.getSelectedIndex() == -1)
{
return;
}
UpdateWindow(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).ShowOnMap());
});
buttonUp.addActionListener(e -> ButtonMove_Click("buttonUp"));
buttonLeft.addActionListener(e -> ButtonMove_Click("buttonLeft"));
buttonDown.addActionListener(e -> ButtonMove_Click("buttonDown"));
buttonRight.addActionListener(e -> ButtonMove_Click("buttonRight"));
ButtonUp.addActionListener(e -> ButtonMove_Click("buttonUp"));
ButtonLeft.addActionListener(e -> ButtonMove_Click("buttonLeft"));
ButtonDown.addActionListener(e -> ButtonMove_Click("buttonDown"));
ButtonRight.addActionListener(e -> ButtonMove_Click("buttonRight"));
maskedTextBoxPosition.addKeyListener(new KeyAdapter() {
@Override
public void keyTyped(KeyEvent e) {
char c = e.getKeyChar();
if ( ((c < '0') || (c > '9')) || maskedTextBoxPosition.getText().length() >= 2) {
e.consume();
}
}
});
ButtonAddMap.addActionListener(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(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(e -> {
if (ListBoxMaps.getSelectedIndex() == -1)
{
return;
}
DrawningObjectBoat Boat=_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).GetBoatsDeleted();
if(Boat==null)
{
JOptionPane.showMessageDialog(null,"Коллекция пуста","Ошибка",JOptionPane.ERROR_MESSAGE);
return;
}
FormBoat formBoat=new FormBoat(Boat);
formBoat.setSize(1000,800);
formBoat.setVisible(true);
});
}
private void createUIComponents() {
DefaultListModel<String> dlm = new DefaultListModel<String>();
ListBoxMaps = new JList(dlm);
}
}

View File

@ -4,7 +4,7 @@ public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame("Лодка");
frame.setContentPane(new FormParam().MainPanel);
frame.setContentPane(new FormMapWithSetBoats().Mainpanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocation(500, 200);
frame.pack();

View File

@ -1,5 +1,6 @@
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.LinkedList;
public class MapWithSetBoatsGeneric <T extends IDrawningObject, U extends AbstractMap>
{
@ -9,9 +10,12 @@ public class MapWithSetBoatsGeneric <T extends IDrawningObject, U extends Abstra
private int _placeSizeHeight = 90;
private SetBoatsGeneric<T> _setBoats;
private U _map;
private LinkedList<T> _deletedBoats;
public MapWithSetBoatsGeneric(int picWidth, int picHeight, U map)
{
_deletedBoats = new LinkedList<>();
int width = picWidth / _placeSizeWidth;
int height = picHeight / _placeSizeHeight;
_setBoats = new SetBoatsGeneric<T>(width * height);
@ -27,7 +31,9 @@ public class MapWithSetBoatsGeneric <T extends IDrawningObject, U extends Abstra
public T removeBoat(int position)
{
return _setBoats.Remove(position);
T boat = _setBoats.Remove(position);
_deletedBoats.offerFirst(boat);
return boat;
}
public BufferedImage ShowSet()
@ -129,4 +135,16 @@ public class MapWithSetBoatsGeneric <T extends IDrawningObject, U extends Abstra
}
}
}
public T GetSelectedBoat(int ind){
return _setBoats.Get(ind);
}
public T GetBoatsDeleted()
{
if(_deletedBoats.isEmpty())
{
return null;
}
return _deletedBoats.removeFirst();
}
}

44
MapsCollection.java Normal file
View File

@ -0,0 +1,44 @@
import java.util.ArrayList;
import java.util.HashMap;
public class MapsCollection {
private final HashMap<String,MapWithSetBoatsGeneric<DrawningObjectBoat, 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 MapWithSetBoatsGeneric<>(_pictureWidth,_pictureHeight,Map));
}
}
public void DelMap(String name)
{
_mapStorages.remove(name);
}
public MapWithSetBoatsGeneric<DrawningObjectBoat,AbstractMap> Get(String ind)
{
if(_mapStorages.containsKey(ind))
{
return _mapStorages.get(ind);
}
return null;
}
public DrawningObjectBoat Get(String name, int ind) {
if (_mapStorages.containsKey(name))
{
return _mapStorages.get(name).GetSelectedBoat(ind);
}
return null;
}
}

View File

@ -1,44 +1,53 @@
import java.util.ArrayList;
import java.util.Iterator;
public class SetBoatsGeneric<T> {
private T[] _places;
private ArrayList<T> _places;
private final int _maxCount;
public int Count() {
return _places.length;
return _places.size();
}
public SetBoatsGeneric(int count) {
_places = (T[]) (new Object[count]);
_maxCount=count;
_places = new ArrayList<>();
}
public int Insert(T boat) {
for (int i = 0; i < _places.length; i++) {
if (_places[i] == null) {
_places[i] = boat;
return i;
}
}
return -1;
return Insert(boat, 0);
}
public int Insert(T boat, int position) {
int index = position;
while (_places[index] != null && index < _places.length) index++;
if (index == _places.length) return -1;
for (int i = index; i > position; --i) _places[i] = _places[i - 1];
_places[position] = boat;
if (position < 0 || position > Count() || _maxCount == Count()) return -1;
_places.add(position,boat);
return position;
}
public T Remove(int position) {
if (position >= _places.length) return null;
T res = _places[position];
_places[position] = null;
return res;
if (position >= Count() || position < 0)
{
return null;
}
T ship = (T) _places.get(position);
_places.remove(ship);
return ship;
}
public T Get(int position) {
return _places[position];
if (position >= Count() || position<0)
{
return null;
}
return _places.get(position);
}
public void Set(int position,T value)
{
if (position < _maxCount || position >= 0)
{
Insert(value,position);
}
}
public Iterator<T> iterator() {
return _places.iterator();
}
}