Tsukanova I.V. HardLabWork4 #6

Closed
Inohara wants to merge 2 commits from LabWork04 into LabWork03
7 changed files with 292 additions and 98 deletions

View File

@ -14,6 +14,10 @@ public class DrawingObjectWarship implements IDrawingObject {
return 0;
}
public DrawingWarship GetWarship() {
return _warship;
}
@Override
public void SetObject(int x, int y, int width, int height) {
_warship.SetPosition(x, y, width, height);

View File

@ -3,7 +3,7 @@
<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">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="884" height="479"/>
<xy x="20" y="20" width="884" height="676"/>
</constraints>
<properties/>
<border type="none"/>
@ -37,33 +37,6 @@
<font/>
</border>
<children>
<component id="d5ebd" class="javax.swing.JComboBox" binding="СomboBoxSelectorMap">
<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="9e5dd" class="javax.swing.JButton" binding="ButtonAddWarship">
<constraints>
<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="Add warship"/>
</properties>
</component>
<component id="16e2c" class="javax.swing.JButton" binding="ButtonRemoveWarship">
<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="Remove warship"/>
</properties>
</component>
<component id="1fc5b" class="javax.swing.JButton" binding="ButtonShowStorage">
<constraints>
<grid row="6" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
@ -80,14 +53,6 @@
<text value="Show map"/>
</properties>
</component>
<component id="608d7" class="javax.swing.JTextField" binding="TextBoxPosition">
<constraints>
<grid row="3" 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>
<component id="2ea34" class="javax.swing.JButton" binding="ButtonRight">
<constraints>
<grid row="10" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="3" indent="0" use-parent-layout="false">
@ -136,14 +101,9 @@
<text value=""/>
</properties>
</component>
<vspacer id="aa195">
<constraints>
<grid row="1" column="1" 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="fa6fc">
<constraints>
<grid row="5" column="1" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
<grid row="1" column="1" 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="f6c4c">
@ -151,6 +111,93 @@
<grid row="8" column="1" 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="14e42" class="javax.swing.JButton" binding="deletedWarshipButtom">
<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="Deleted warship"/>
</properties>
</component>
<component id="16e2c" class="javax.swing.JButton" binding="ButtonRemoveWarship">
<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="Remove warship"/>
</properties>
</component>
<component id="608d7" class="javax.swing.JTextField" binding="TextBoxPosition">
<constraints>
<grid row="3" 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>
<component id="9e5dd" class="javax.swing.JButton" binding="ButtonAddWarship">
<constraints>
<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="Add warship"/>
</properties>
</component>
<grid id="53593" binding="MapPanel" layout-manager="GridLayoutManager" row-count="5" 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="0" row-span="1" col-span="3" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<name value=""/>
</properties>
<border type="bevel-lowered" title="Maps" title-justification="1" title-position="2"/>
<children>
<component id="91aeb" class="javax.swing.JTextField" binding="TextFieldMap">
<constraints>
<grid row="0" 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>
<component id="ddbe9" class="javax.swing.JButton" binding="CreateMapButton">
<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"/>
</constraints>
<properties>
<text value="Create map"/>
</properties>
</component>
<component id="b2e9e" class="javax.swing.JButton" binding="DeleteMapButton">
<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="Delete map"/>
</properties>
</component>
<component id="9cadb" class="javax.swing.JList" binding="ListBoxMaps">
<constraints>
<grid row="3" 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>
<component id="8d7d6" class="javax.swing.JComboBox" binding="СomboBoxSelectorMap">
<constraints>
<grid row="1" 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>
</children>
</grid>
</children>
</grid>
</children>

View File

@ -1,5 +1,8 @@
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
public class FormMapWithSetWarships extends JFrame{
private JPanel mainPanel;
@ -15,8 +18,19 @@ public class FormMapWithSetWarships extends JFrame{
private JButton ButtonUp;
private JButton ButtonDown;
private JButton ButtonLeft;
private JButton deletedWarshipButtom;
private JPanel MapPanel;
private JTextField TextFieldMap;
private JButton CreateMapButton;
private JButton DeleteMapButton;
private JList ListBoxMaps;
private Image bufferedImage;
private MapWithSetWarshipsGeneric<DrawingObjectWarship, AbstractMap> _mapWarshipsCollectionGeneric;
private MapsCollection _mapsCollection;
private final HashMap<String, AbstractMap> _mapsDict = new HashMap<>() {{
put("Простая карта", new SimpleMap());
put("Преграды-линии", new LineMap());
}};
public FormMapWithSetWarships(){
InitializeComponent();
@ -31,13 +45,35 @@ public class FormMapWithSetWarships extends JFrame{
}
}
public void InitializeComponent(){
private void ReloadMaps()
{
int index = ListBoxMaps.getSelectedIndex();
ListBoxMaps.setListData(_mapsCollection.Keys().toArray(new String[0]));
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);
}
repaint();
}
private void InitializeComponent(){
setContentPane(mainPanel);
setTitle("Warship");
setSize(1000, 693);
setSize(935, 693);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
_mapsCollection = new MapsCollection(getWidth(), getHeight());
СomboBoxSelectorMap.removeAllItems();
for(String elem:_mapsDict.keySet()){
СomboBoxSelectorMap.addItem(elem);
}
Icon iconUp = new ImageIcon("src\\Images\\ArrowUp.jpg");
ButtonUp.setIcon(iconUp);
Icon iconDown = new ImageIcon("src\\Images\\ArrowDown.jpg");
@ -47,24 +83,56 @@ public class FormMapWithSetWarships extends JFrame{
Icon iconRight = new ImageIcon("src\\Images\\ArrowRight.jpg");
ButtonRight.setIcon(iconRight);
СomboBoxSelectorMap.addActionListener(e -> {
AbstractMap map = switch (СomboBoxSelectorMap.getSelectedItem().toString()) {
case "Простая карта" -> new SimpleMap();
case "Преграды-линии" -> new LineMap();
default -> null;
};
if( map != null){
_mapWarshipsCollectionGeneric = new MapWithSetWarshipsGeneric<DrawingObjectWarship, AbstractMap>(
PictureBox.getWidth(), PictureBox.getHeight(), map);
}
else
ListBoxMaps.addListSelectionListener(e -> {
if(ListBoxMaps.getSelectedIndex() == -1)
return;
bufferedImage = _mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).ShowSet();
repaint();
});
DeleteMapButton.addActionListener(e -> {
if (ListBoxMaps.getSelectedIndex() == -1)
{
_mapWarshipsCollectionGeneric = null;
return;
}
if(JOptionPane.showConfirmDialog(this,"Удалить карту " + ListBoxMaps.getSelectedValue().toString() + "?",
"Удаление",JOptionPane.YES_NO_OPTION) == 0)
{
_mapsCollection.DelMap(ListBoxMaps.getSelectedValue().toString());
ReloadMaps();
}
});
CreateMapButton.addActionListener(e -> {
if (СomboBoxSelectorMap.getSelectedIndex() == -1 || TextFieldMap.getText() == null || TextFieldMap.getText().equals(""))
{
JOptionPane.showMessageDialog(this,"Не все данные заполнены","Ошибка",JOptionPane.ERROR_MESSAGE);
return;
}
if (!_mapsDict.containsKey(СomboBoxSelectorMap.getSelectedItem()))
{
JOptionPane.showMessageDialog(this,"Нет такой карты","Ошибка",JOptionPane.ERROR_MESSAGE);
return;
}
_mapsCollection.AddMap(TextFieldMap.getText(), _mapsDict.get(СomboBoxSelectorMap.getSelectedItem().toString()));
ReloadMaps();
});
deletedWarshipButtom.addActionListener(e -> {
if (ListBoxMaps.getSelectedIndex() == -1)
return;
DrawingObjectWarship warship = _mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).GetWarshipInDeleted();
if (warship == null) {
JOptionPane.showMessageDialog(null, "Стек пуст", "Ошибка", JOptionPane.ERROR_MESSAGE);
return;
}
FormWarship form = new FormWarship(warship);
form.setSize(1000, 700);
form.setVisible(true);
});
ButtonAddWarship.addActionListener(e -> {
if(_mapWarshipsCollectionGeneric == null){
if(ListBoxMaps.getSelectedIndex() == -1){
return;
}
FormWarshipCreator warshipCreator = new FormWarshipCreator();
@ -75,10 +143,10 @@ public class FormMapWithSetWarships extends JFrame{
if (warshipCreator.getSelectedWarship() != null) {
DrawingObjectWarship warship = new DrawingObjectWarship(warshipCreator.getSelectedWarship());
if (_mapWarshipsCollectionGeneric.Plus(warship) >= 0) {
if (_mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).Plus(warship)>=0) {
JOptionPane.showMessageDialog(this,
"Объект добавлен", "Успех", JOptionPane.INFORMATION_MESSAGE);
bufferedImage = _mapWarshipsCollectionGeneric.ShowSet();
bufferedImage = _mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).ShowSet();
repaint();
} else {
JOptionPane.showMessageDialog(this,
@ -88,26 +156,26 @@ public class FormMapWithSetWarships extends JFrame{
});
ButtonRemoveWarship.addActionListener(e -> {
String text = TextBoxPosition.getText();
if (text == null || _mapWarshipsCollectionGeneric == null || text.isEmpty())
if (TextBoxPosition.getText().isEmpty())
{
return;
}
int result = JOptionPane.showConfirmDialog(
null,
int result = JOptionPane.showConfirmDialog(null,
"Удалить объект?",
"Удаление",
JOptionPane.YES_NO_CANCEL_OPTION);
JOptionPane.YES_NO_OPTION,
JOptionPane.WARNING_MESSAGE);
if (result == JOptionPane.NO_OPTION)
{
return;
}
int pos = Integer.parseInt(text);
if (_mapWarshipsCollectionGeneric.Minus(pos) != null)
int pos = Integer.parseInt(TextBoxPosition.getText());
if (_mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).Minus(pos)!=null)
{
JOptionPane.showMessageDialog(this,
"Объект удален","Успех",JOptionPane.INFORMATION_MESSAGE);
bufferedImage = _mapWarshipsCollectionGeneric.ShowSet();
bufferedImage = _mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).ShowSet();
repaint();
}
else

View File

@ -4,9 +4,9 @@ import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.util.Random;
public class FormWarship extends JFrame {
public class FormWarship extends JDialog {
private DrawingWarship _warship;
public DrawingWarship selectedWarship;
DrawingWarship selectedWarship;
public DrawingWarship getSelectedCar(){
return selectedWarship;
}
@ -29,6 +29,13 @@ public class FormWarship extends JFrame {
InitializeComponent();
}
public FormWarship(DrawingObjectWarship warship){
InitializeComponent();
_warship = warship.GetWarship();
SetData();
}
private void Draw(){
Graphics2D graphics = (Graphics2D) drawPanel.getGraphics();
graphics.clearRect(0, 0, drawPanel.getWidth(), drawPanel.getHeight());
@ -36,6 +43,16 @@ public class FormWarship extends JFrame {
_warship.DrawTransport(graphics);
}
@Override
public void paint(Graphics g) {
super.paint(g);
if (_warship != null) {
mainPanel.paintComponents(mainPanel.getGraphics());
_warship.DrawTransport(g);
}
}
private void SetData(){
Random rnd = new Random();
_warship.SetPosition(rnd.nextInt(10, 100), rnd.nextInt(10, 100), mainPanel.getWidth(), mainPanel.getHeight());
@ -54,7 +71,6 @@ public class FormWarship extends JFrame {
setContentPane(mainPanel);
setTitle("Warship");
setSize(900, 700);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
Icon iconUp = new ImageIcon("src\\Images\\ArrowUp.jpg");

View File

@ -1,5 +1,6 @@
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Stack;
public class MapWithSetWarshipsGeneric <T extends IDrawingObject,U extends AbstractMap> {
private final int _pictureWidth;
@ -7,6 +8,7 @@ public class MapWithSetWarshipsGeneric <T extends IDrawingObject,U extends Abstr
private final int _placeSizeWidth = 120;
private final int _placeSizeHeight = 50;
private final SetWarshipsGeneric<T> _setWarships;
private Stack<T> _deletedWarship;
private final U _map;
public MapWithSetWarshipsGeneric(int picWidth, int picHeight, U map)
@ -17,6 +19,7 @@ public class MapWithSetWarshipsGeneric <T extends IDrawingObject,U extends Abstr
_pictureWidth = picWidth;
_pictureHeight = picHeight;
_map = map;
_deletedWarship = new Stack<>();
}
public int Plus(T warship)
@ -26,7 +29,9 @@ public class MapWithSetWarshipsGeneric <T extends IDrawingObject,U extends Abstr
public T Minus(int position)
{
return _setWarships.Remove(position);
T warship=_setWarships.Remove(position);
_deletedWarship.push(warship);
return warship;
}
public Image ShowSet()
@ -92,6 +97,7 @@ public class MapWithSetWarshipsGeneric <T extends IDrawingObject,U extends Abstr
Color brush=Color.BLACK;
Stroke penWide = new BasicStroke(3);
Stroke penThin = new BasicStroke(1);
for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++)
{
@ -102,6 +108,7 @@ public class MapWithSetWarshipsGeneric <T extends IDrawingObject,U extends Abstr
g.drawLine(i * _placeSizeWidth + 20, j * _placeSizeHeight+2, i * _placeSizeWidth + (int)(_placeSizeWidth*0.8), j * _placeSizeHeight+2);
g.drawLine(i * _placeSizeWidth + (int)(_placeSizeWidth * 0.8), j * _placeSizeHeight + 2, i * _placeSizeWidth + _placeSizeWidth, j * _placeSizeHeight + _placeSizeHeight/2);
g.drawLine(i * _placeSizeWidth + _placeSizeWidth, j * _placeSizeHeight + _placeSizeHeight / 2, i * _placeSizeWidth+ (int)(_placeSizeWidth * 0.8), j * _placeSizeHeight + _placeSizeHeight);
g.setStroke(penThin);
}
}
}
@ -120,4 +127,15 @@ public class MapWithSetWarshipsGeneric <T extends IDrawingObject,U extends Abstr
}
}
}
public T GetWarshipInList(int ind){
return _setWarships.Get(ind);
}
public T GetWarshipInDeleted()
{
if(_deletedWarship.empty())
return null;
return _deletedWarship.pop();
}
}

48
src/MapsCollection.java Normal file
View File

@ -0,0 +1,48 @@
import java.util.ArrayList;
import java.util.HashMap;
public class MapsCollection {
private final HashMap<String, MapWithSetWarshipsGeneric<DrawingObjectWarship, 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))
{
return;
}
_mapStorages.put(name, new MapWithSetWarshipsGeneric<>(_pictureWidth,_pictureHeight,map));
}
public void DelMap(String name)
{
_mapStorages.remove(name);
}
public MapWithSetWarshipsGeneric<DrawingObjectWarship, AbstractMap> get(String ind){
if (_mapStorages.containsKey(ind))
{
return _mapStorages.get(ind);
}
return null;
}
public DrawingObjectWarship Get(String name, int ind) {
if (_mapStorages.containsKey(name))
{
return _mapStorages.get(name).GetWarshipInList(ind);
}
return null;
}
}

View File

@ -1,13 +1,17 @@
public class SetWarshipsGeneric<T extends Object>{
private final Object[] _places;
import java.util.ArrayList;
import java.util.Iterator;
public class SetWarshipsGeneric<T extends Object> implements Iterable<T>{
private ArrayList<T> _places;
public int getCount() {
return _places.length;
return _places.size();
}
private final int _maxCount;
public SetWarshipsGeneric(int count)
{
_places = new Object[count];
_places = new ArrayList<>();
_maxCount = count;
}
public int Insert(T warship)
@ -17,41 +21,30 @@ public class SetWarshipsGeneric<T extends Object>{
public int Insert(T warship, int position)
{
if (position < 0 || position >= getCount())
if (position < 0 || position >= _maxCount)
return -1;
int empty = -1;
for (int i = position + 1; i < getCount(); i++)
{
if (_places[i] == null)
empty = i;
}
if (empty == -1)
return 0;
else
{
for (int i = empty; i > position; i--)
_places[i] = _places[i - 1];
}
_places[position] = warship;
return 1;
_places.add(position, warship);
return position;
}
public T Remove(int position)
{
if (position >= getCount() || position < 0 || _places[position] == null)
if (position >= _maxCount || position < 0)
return null;
T deleted =(T) _places[position];
_places[position] = null;
T deleted = _places.get(position);
_places.remove(position);
return deleted;
}
public T Get(int position)
{
if (position >= getCount() || position < 0)
if (position > getCount() || position < 0)
return null;
return _places.get(position);
}
return (T) _places[position];
@Override
public Iterator<T> iterator() {
return _places.iterator();
}
}