6 лабораторная

This commit is contained in:
Hells Hound 2022-12-13 22:22:14 +04:00
parent caec9976f4
commit 29d558449c
9 changed files with 171 additions and 53 deletions

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_16" project-jdk-name="openjdk-19" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_19" project-jdk-name="openjdk-19" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

View File

@ -46,4 +46,8 @@ public class DrawingObjectWarship implements IDrawingObject {
}
return null;
}
public static IDrawingObject Create(String data){
return new DrawingObjectWarship(ExtentionWarship.CreateDrawingWarship(data));
}
}

View File

@ -5,7 +5,7 @@ public class DrawingWarship extends JComponent {
protected EntityWarship Warship;
public EntityWarship GetWarship(){return Warship;}
protected IDrawingObjectBlock Blocks;
public String BlockType(){return _block.getClass().getName().substring(4);}
public String BlockType(){return Blocks.getClass().getName();}
private BlockCount _block;
protected int _startPosX;
@ -46,8 +46,8 @@ public class DrawingWarship extends JComponent {
Blocks = block;
}
public void SetBlocks(int blocksCount, IDrawingObjectBlock blocks){
Blocks = blocks;
public void SetBlocks(int blocksCount, IDrawingObjectBlock block){
Blocks = block;
Blocks.SetBlockCount(blocksCount);
}

View File

@ -5,28 +5,33 @@ public class ExtentionWarship {
private static BlockCount _block;
public static DrawingWarship CreateDrawingWarship(String info)
{
//speed - 0, weight - 1, color - 2, blocktype - 3, blockcount - 4,
//dopcolor - 5, bodykit - 6, cabin - 7, superengine- 8
String[] strs = info.split(String.valueOf(_separatorForObject));
IDrawingObjectBlock pattern = null;
IDrawingObjectBlock blockType = null;
switch (strs[3])
{
case "DrawingRectangleBlocks":
pattern = new DrawingBlocks(_block);
case "DrawingBlocks":
blockType = new DrawingBlocks(_block);
break;
case "DrawingRoundBlocks":
pattern = new DrawingRoundBlocks(_block);
blockType = new DrawingRoundBlocks(_block);
break;
case "DrawingPiePatternRoller":
pattern = new DrawingRoundRectangleBlocks(_block);
case "DrawingRoundRectangleBlocks":
blockType = new DrawingRoundRectangleBlocks(_block);
break;
}
if (strs.length == 5)
{
var temp = new DrawingWarship(Integer.parseInt(strs[0]),
Float.parseFloat(strs[1]), new Color(Integer.parseInt(strs[2])), Integer.parseInt(strs[4]));
temp.SetBlocks(Integer.parseInt(strs[4]), pattern);
Float.parseFloat(strs[1]),
new Color(Integer.parseInt(strs[2])),
Integer.parseInt(strs[4]));
temp.SetBlocks(Integer.parseInt(strs[4]), blockType);
return temp;
}
if (strs.length == 8)
if (strs.length == 9)
{
var temp = new DrawingAircraftCarrier(Integer.parseInt(strs[0]),
Float.parseFloat(strs[1]),
@ -34,8 +39,10 @@ public class ExtentionWarship {
new Color(Integer.parseInt(strs[5])),
Integer.parseInt(strs[4]),
Boolean.parseBoolean(strs[6]),
Boolean.parseBoolean(strs[7]));
temp.SetBlocks(Integer.parseInt(strs[4]), pattern);
Boolean.parseBoolean(strs[7]),
Boolean.parseBoolean(strs[8]));
temp.SetBlocks(Integer.parseInt(strs[4]), blockType);
return temp;
}
return null;
@ -50,7 +57,7 @@ public class ExtentionWarship {
{
return str + _separatorForObject + aircraftCarrier.GetDopColor().getRGB()
+ _separatorForObject + aircraftCarrier.GetBodyKit() + _separatorForObject
+ aircraftCarrier.GetCabin() + aircraftCarrier.GetSuperEngine();
+ aircraftCarrier.GetCabin() + _separatorForObject + aircraftCarrier.GetSuperEngine();
}
return str;
}

View File

@ -1,9 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="FormMapWithSetWarships">
<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="3" 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>
<xy x="20" y="20" width="884" height="676"/>
<xy x="20" y="20" width="884" height="620"/>
</constraints>
<properties/>
<border type="none"/>
@ -11,7 +11,7 @@
<grid id="ae53a" binding="PictureBox" layout-manager="GridLayoutManager" row-count="1" 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="1" 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">
<preferred-size width="727" height="24"/>
</grid>
</constraints>
@ -30,7 +30,7 @@
<grid id="16c60" binding="GroupBoxTools" layout-manager="GridLayoutManager" row-count="11" 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="1" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="0" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
<grid row="1" column="1" row-span="2" col-span="1" vsize-policy="3" hsize-policy="0" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="bevel-lowered" title="Tools" title-justification="1" title-position="2">
@ -161,14 +161,6 @@
</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"/>
@ -185,14 +177,22 @@
</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="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="Преграды-линии"/>
<item value="SimpleMap"/>
<item value="LineMap"/>
</model>
</properties>
</component>
@ -200,7 +200,7 @@
</grid>
</children>
</grid>
<grid id="e0d40" class="javax.swing.JMenuBar" binding="menuStrip" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="fed73" class="javax.swing.JMenuBar" binding="menuStrip" layout-manager="GridLayoutManager" row-count="1" 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="0" indent="0" use-parent-layout="false"/>
@ -208,7 +208,7 @@
<properties/>
<border type="none"/>
<children>
<grid id="2188b" class="javax.swing.JMenu" binding="menu" layout-manager="FlowLayout" hgap="5" vgap="5" flow-align="1">
<grid id="1ed5d" class="javax.swing.JMenu" binding="menu" layout-manager="FlowLayout" hgap="5" vgap="5" flow-align="1">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
@ -217,25 +217,25 @@
</properties>
<border type="none"/>
<children>
<component id="92a79" class="javax.swing.JMenuItem" binding="saveToolStripMenuItem">
<component id="ba181" class="javax.swing.JMenuItem" binding="saveToolStripMenuItem">
<constraints/>
<properties>
<text value="save"/>
<text value="Save"/>
</properties>
</component>
<component id="d2bb2" class="javax.swing.JMenuItem" binding="loadToolStripMenuItem">
<component id="601ab" class="javax.swing.JMenuItem" binding="loadToolStripMenuItem">
<constraints/>
<properties>
<text value="load"/>
<text value="Load"/>
</properties>
</component>
<component id="83ebf" class="javax.swing.JMenuItem" binding="saveMapToolStripMenuItem">
<component id="62149" class="javax.swing.JMenuItem" binding="saveMapToolStripMenuItem">
<constraints/>
<properties>
<text value="Сохранение карты"/>
</properties>
</component>
<component id="ea34c" class="javax.swing.JMenuItem" binding="loadMapToolStripMenuItem">
<component id="5bcb" class="javax.swing.JMenuItem" binding="loadMapToolStripMenuItem">
<constraints/>
<properties>
<text value="Загрузка карты"/>
@ -245,6 +245,11 @@
</grid>
</children>
</grid>
<vspacer id="19c9">
<constraints>
<grid row="1" column="2" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
</children>
</grid>
</form>

View File

@ -27,18 +27,18 @@ public class FormMapWithSetWarships extends JFrame{
private JButton CreateMapButton;
private JButton DeleteMapButton;
private JList ListBoxMaps;
private JMenuBar menuStrip;
private JMenu menu;
private JMenuItem saveToolStripMenuItem;
private JMenuItem loadToolStripMenuItem;
private JMenuItem saveMapToolStripMenuItem;
private JMenuItem loadMapToolStripMenuItem;
private JMenuBar menuStrip;
private JMenu menu;
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());
put("SimpleMap", new SimpleMap());
put("LineMap", new LineMap());
}};
public FormMapWithSetWarships(){
@ -92,12 +92,16 @@ public class FormMapWithSetWarships extends JFrame{
Icon iconRight = new ImageIcon("src\\Images\\ArrowRight.jpg");
ButtonRight.setIcon(iconRight);
File root = new File("D://2.1//RPP");
//сохранение
saveToolStripMenuItem.addActionListener(e -> {
JFileChooser fs = new JFileChooser();
fs.setCurrentDirectory(root);
fs.setAcceptAllFileFilterUsed(false);
FileNameExtensionFilter filter = new FileNameExtensionFilter(".txt file", "txt");
fs.addChoosableFileFilter(filter);
fs.setDialogTitle("Сохранение");
fs.setDialogTitle("Save");
int result = fs.showSaveDialog(null);
if (result == JFileChooser.APPROVE_OPTION) {
File selectedFile = fs.getSelectedFile();
@ -111,8 +115,11 @@ public class FormMapWithSetWarships extends JFrame{
}
}
});
//загрузка
loadToolStripMenuItem.addActionListener(e -> {
JFileChooser fs = new JFileChooser();
fs.setCurrentDirectory(root);
fs.setAcceptAllFileFilterUsed(false);
FileNameExtensionFilter filter = new FileNameExtensionFilter(".txt file", "txt");
fs.addChoosableFileFilter(filter);
@ -131,8 +138,11 @@ public class FormMapWithSetWarships extends JFrame{
}
}
});
//сохранение карты
saveMapToolStripMenuItem.addActionListener(e -> {
JFileChooser fs = new JFileChooser();
fs.setCurrentDirectory(root);
fs.setAcceptAllFileFilterUsed(false);
FileNameExtensionFilter filter = new FileNameExtensionFilter(".txt file", "txt");
fs.addChoosableFileFilter(filter);
@ -140,7 +150,7 @@ public class FormMapWithSetWarships extends JFrame{
int result = fs.showSaveDialog(null);
if (result == JFileChooser.APPROVE_OPTION) {
File selectedFile = fs.getSelectedFile();
if (_mapsCollection.SaveMap(selectedFile.getPath(), listBoxMaps.getSelectedValue().toString()))
if (_mapsCollection.SaveMap(selectedFile.getPath(), ListBoxMaps.getSelectedValue().toString()))
{
JOptionPane.showMessageDialog(null, "Сохранение карты прошло успешно", "Результат",JOptionPane.INFORMATION_MESSAGE);
}
@ -150,8 +160,11 @@ public class FormMapWithSetWarships extends JFrame{
}
}
});
//загрузка карты
loadMapToolStripMenuItem.addActionListener(e -> {
JFileChooser fs = new JFileChooser();
fs.setCurrentDirectory(root);
fs.setAcceptAllFileFilterUsed(false);
FileNameExtensionFilter filter = new FileNameExtensionFilter(".txt file", "txt");
fs.addChoosableFileFilter(filter);

View File

@ -101,7 +101,7 @@ public class MapWithSetWarshipsGeneric <T extends IDrawingObject,U extends Abstr
for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++)
{
for (int j = 0; j <= _pictureHeight / _placeSizeHeight ; ++j)
for (int j = 0; j < _pictureHeight / _placeSizeHeight ; ++j)
{
g.setColor(brush);
g.setStroke(penWide);
@ -111,6 +111,7 @@ public class MapWithSetWarshipsGeneric <T extends IDrawingObject,U extends Abstr
g.setStroke(penThin);
}
}
}
private void DrawWarship(Graphics gr)
@ -142,11 +143,34 @@ public class MapWithSetWarshipsGeneric <T extends IDrawingObject,U extends Abstr
public String GetData(char separatorType, char separatorData)
{
String data = _map.getClass().getName()+separatorType;
data = data.substring(4);
for (var warship : _setWarships)
{
data += warship.GetInfo()+separatorData;
data += warship.GetInfo() + separatorData;
}
return data;
}
public void LoadData(String[] records)
{
for (var rec : records)
{
_setWarships.Insert((T)DrawingObjectWarship.Create(rec));
}
}
public String GetDataMap(char separatorType, char separatorData)
{
String data = _map.getClass().getName() + separatorType + '\n';
for (var warship : _setWarships)
{
data += warship.GetInfo() + separatorData+'\n';
}
return data;
}
public void Clear() {
_setWarships.Clear();
}
}

View File

@ -49,7 +49,6 @@ public class MapsCollection {
return null;
}
//Сохранение отдельного объекта класса-хранилища
public boolean SaveData(String filename) {
File file = new File(filename);
@ -68,7 +67,6 @@ public class MapsCollection {
return true;
}
//Загрузка отдельного объекта класса-хранилища
public boolean LoadData(String filename)
{
if (!(new File(filename).exists()))
@ -80,7 +78,6 @@ public class MapsCollection {
String str = "";
if ((str = br.readLine()) == null || !str.contains("MapsCollection"))
{
//если нет такой записи, то это не те данные
return false;
}
//очищаем записи
@ -94,11 +91,11 @@ public class MapsCollection {
case "SimpleMap":
map = new SimpleMap();
break;
case "HardMap":
map = new HardMap();
case "LineMap":
map = new LineMap();
break;
}
_mapStorages.put(elem[0], new MapWithSetTractorsGeneric<IDrawingObject, AbstractMap>(_pictureWidth, _pictureHeight, map));
_mapStorages.put(elem[0], new MapWithSetWarshipsGeneric<>(_pictureWidth, _pictureHeight, map));
_mapStorages.get(elem[0]).LoadData(elem[2].split(separatorData + "\n?"));
}
} catch (IOException e) {
@ -106,4 +103,68 @@ public class MapsCollection {
}
return true;
}
//Сохранение отдельного объекта класса-хранилища
public boolean SaveMap(String filename, String index) {
File file = new File(filename);
if (file.exists()) {
file.delete();
}
var elem = _mapStorages.get(index);
if (elem==null)
return false;
try (BufferedWriter br = new BufferedWriter(new FileWriter(filename)))
{
br.write("MapsCollection\n");
br.write(index + separatorDict);
br.write(elem.GetDataMap(separatorDict, separatorData));
} catch (IOException e) {
throw new RuntimeException(e);
}
return true;
}
//Загрузка отдельного объекта класса-хранилища
public boolean LoadMap(String filename) {
if (!(new File(filename).exists()))
{
return false;
}
try (BufferedReader br = new BufferedReader(new FileReader(filename)))
{
String str = "";
if ((str = br.readLine()) == null || !str.contains("MapsCollection"))
{
//если нет такой записи, то это не те данные
return false;
}
if ((str = br.readLine()) != null)
{
var elem = str.split(String.format("\\%c",separatorDict));
AbstractMap map = null;
switch (elem[1])
{
case "SimpleMap":
map = new SimpleMap();
break;
case "LineMap":
map = new LineMap();
break;
}
if(_mapStorages.containsKey(elem[0])){
_mapStorages.get(elem[0]).Clear();
}
else{
_mapStorages.put(elem[0],new MapWithSetWarshipsGeneric<>(_pictureWidth,_pictureHeight,map));
}
while((str = br.readLine()) != null) {
_mapStorages.get(elem[0]).LoadData(str.split(separatorData + "\n?"));
}
}
} catch (IOException e) {
throw new RuntimeException(e);
}
return true;
}
}

View File

@ -47,4 +47,8 @@ public class SetWarshipsGeneric<T extends Object> implements Iterable<T>{
public Iterator<T> iterator() {
return _places.iterator();
}
public void Clear() {
_places.clear();
}
}