Abazov A. A. LabWork#06 #6

Closed
Andrey_Abazov wants to merge 1 commits from Lab06 into Lab05
8 changed files with 435 additions and 11 deletions
Showing only changes of commit 11be96c351 - Show all commits

View File

@ -57,4 +57,14 @@ public class DrawingObjectAirBomber implements IDrawingObject {
_airBomber.DrawTransport(g);
}
}
@Override
public String GetInfo() {
if (_airBomber != null) return ExtentionAirBomber.ExtentionedAirBomber.GetDataForSave(_airBomber);
else return "";
}
public static IDrawingObject Create(String data){
return new DrawingObjectAirBomber(ExtentionAirBomber.ExtentionedAirBomber.CreateDrawningAirBomber(data));
}
}

View File

@ -0,0 +1,77 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package AirBomberPackage;
import java.awt.Color;
/**
*
* @author Андрей
*/
public class ExtentionAirBomber{
public static class ExtentionedAirBomber {
/// <summary>
/// Разделитель для записи информации по объекту в файл
/// </summary>
private static String _separatorForObject = ":";
/// <summary>
/// Создание объекта из строки
/// </summary>
/// <param name="info"></param>
/// <returns></returns>
public static DrawingAirBomber CreateDrawningAirBomber(String info)
{
String[] strs = info.split(_separatorForObject);
if (strs.length == 7) //заменить на передачу RGB
{
EnginesType enginesType = null;
switch (strs[6]) {
case "DrawingEngines":
enginesType = EnginesType.RECTANGLE;
break;
case "DrawingEnginesTriangle":
enginesType = EnginesType.TRIANGLE;
break;
case "DrawingEnginesOval":
enginesType = EnginesType.OVAL;
break;
}
return new DrawingAirBomber(Integer.parseInt(strs[0]),
Integer.parseInt(strs[1]), new Color(Integer.parseInt(strs[2]), Integer.parseInt(strs[3]), Integer.parseInt(strs[4])), Integer.parseInt(strs[5]), enginesType);
}
if (strs.length == 13)
{
EnginesType enginesType = null;
switch (strs[6]) {
case "DrawingEngines":
enginesType = EnginesType.RECTANGLE;
break;
case "DrawingEnginesTriangle":
enginesType = EnginesType.TRIANGLE;
break;
case "DrawingEnginesOval":
enginesType = EnginesType.OVAL;
break;
}
return new DrawingHeavyAirBomber(Integer.parseInt(strs[0]), Float.parseFloat(strs[1]),
new Color(Integer.parseInt(strs[2]), Integer.parseInt(strs[3]),Integer.parseInt(strs[4])),
Integer.parseInt(strs[5]), enginesType,
new Color(Integer.parseInt(strs[7]), Integer.parseInt(strs[8]), Integer.parseInt(strs[9])),
Boolean.parseBoolean(strs[10]),Boolean.parseBoolean(strs[11]), Boolean.parseBoolean(strs[12]));
}
return null;
}
public static String GetDataForSave(DrawingAirBomber drawningAirBomber)
{
var airBomber = drawningAirBomber.AirBomber;
var str = String.format("%s%s%s%s%s%s%s%s%s%s%s%s%s", airBomber.getSpeed(), _separatorForObject, Math.round(airBomber.getWeight()), _separatorForObject, airBomber.getBodyColor().getRed(), _separatorForObject, airBomber.getBodyColor().getGreen(), _separatorForObject, airBomber.getBodyColor().getBlue(), _separatorForObject, drawningAirBomber.drawingEngines.getNumberOfEngines(), _separatorForObject, drawningAirBomber.drawingEngines.getClass().getSimpleName());
if (airBomber instanceof EntityHeavyAirBomber heavyAirBomber)
{
return String.format("%s%s%s%s%s%s%s%s%s%s%s%s%s", str,_separatorForObject,heavyAirBomber.getDopColor().getRed(),_separatorForObject, heavyAirBomber.getDopColor().getGreen(), _separatorForObject, heavyAirBomber.getDopColor().getBlue(), _separatorForObject, heavyAirBomber.getBombs(), _separatorForObject, heavyAirBomber.getFuelTank(), _separatorForObject, heavyAirBomber.getTailLine());
}
else return str;
}
}
}

View File

@ -37,4 +37,6 @@ public interface IDrawingObject {
/// </summary>
/// <returns></returns>
float[] GetCurrentPosition();
String GetInfo();
}

View File

@ -1,10 +1,58 @@
<?xml version="1.0" encoding="UTF-8" ?>
<Form version="1.5" maxVersion="1.9" type="org.netbeans.modules.form.forminfo.JFrameFormInfo">
<NonVisualComponents>
<Component class="javax.swing.JFileChooser" name="jFileChooser1">
</Component>
<Menu class="javax.swing.JMenuBar" name="jMenuBar1">
<SubComponents>
<Menu class="javax.swing.JMenu" name="jMenuSaveLoad">
<Properties>
<Property name="text" type="java.lang.String" value="File"/>
</Properties>
<SubComponents>
<MenuItem class="javax.swing.JMenuItem" name="jMenuItemSave">
<Properties>
<Property name="text" type="java.lang.String" value="&#x421;&#x43e;&#x445;&#x440;&#x430;&#x43d;&#x438;&#x442;&#x44c;"/>
</Properties>
<Events>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="jMenuItemSaveActionPerformed"/>
</Events>
</MenuItem>
<MenuItem class="javax.swing.JMenuItem" name="jMenuItemLoad">
<Properties>
<Property name="text" type="java.lang.String" value="&#x417;&#x430;&#x433;&#x440;&#x443;&#x437;&#x438;&#x442;&#x44c;"/>
</Properties>
<Events>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="jMenuItemLoadActionPerformed"/>
</Events>
</MenuItem>
<MenuItem class="javax.swing.JMenuItem" name="jMenuItemSaveStorage">
<Properties>
<Property name="text" type="java.lang.String" value="&#x421;&#x43e;&#x445;&#x440;&#x430;&#x43d;&#x438;&#x442;&#x44c; &#x445;&#x440;&#x430;&#x43d;&#x438;&#x43b;&#x438;&#x449;&#x435;"/>
</Properties>
<Events>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="jMenuItemSaveStorageActionPerformed"/>
</Events>
</MenuItem>
<MenuItem class="javax.swing.JMenuItem" name="jMenuItemLoadStorage">
<Properties>
<Property name="text" type="java.lang.String" value="&#x417;&#x430;&#x433;&#x440;&#x443;&#x437;&#x438;&#x442;&#x44c; &#x445;&#x440;&#x430;&#x43d;&#x438;&#x43b;&#x438;&#x449;&#x435;"/>
</Properties>
<Events>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="jMenuItemLoadStorageActionPerformed"/>
</Events>
</MenuItem>
</SubComponents>
</Menu>
</SubComponents>
</Menu>
</NonVisualComponents>
<Properties>
<Property name="defaultCloseOperation" type="int" value="3"/>
</Properties>
<SyntheticProperties>
<SyntheticProperty name="menuBar" type="java.lang.String" value="jMenuBar1"/>
<SyntheticProperty name="formSizePolicy" type="int" value="1"/>
<SyntheticProperty name="generateCenter" type="boolean" value="false"/>
</SyntheticProperties>
@ -86,7 +134,7 @@
<Component id="jScrollPane1" min="-2" pref="78" max="-2" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
<Component id="buttonDeleteMap" min="-2" pref="34" max="-2" attributes="0"/>
<EmptySpace pref="45" max="32767" attributes="0"/>
<EmptySpace pref="22" max="32767" attributes="0"/>
<Component id="buttonShowDeleted" min="-2" pref="36" max="-2" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
<Component id="buttonAddAirBomber" min="-2" max="-2" attributes="0"/>

View File

@ -46,6 +46,7 @@ public class JFrameMapWithSetAirBombers extends javax.swing.JFrame {
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jFileChooser1 = new javax.swing.JFileChooser();
airBomberCanvas = new AirBomberPackage.CanvasMy();
comboBoxSelectorMap = new javax.swing.JComboBox<>();
buttonAddAirBomber = new javax.swing.JButton();
@ -65,6 +66,12 @@ public class JFrameMapWithSetAirBombers extends javax.swing.JFrame {
listBoxMaps = new javax.swing.JList<>();
buttonDeleteMap = new javax.swing.JButton();
buttonShowDeleted = new javax.swing.JButton();
jMenuBar1 = new javax.swing.JMenuBar();
jMenuSaveLoad = new javax.swing.JMenu();
jMenuItemSave = new javax.swing.JMenuItem();
jMenuItemLoad = new javax.swing.JMenuItem();
jMenuItemSaveStorage = new javax.swing.JMenuItem();
jMenuItemLoadStorage = new javax.swing.JMenuItem();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
@ -182,6 +189,44 @@ public class JFrameMapWithSetAirBombers extends javax.swing.JFrame {
}
});
jMenuSaveLoad.setText("File");
jMenuItemSave.setText("Сохранить");
jMenuItemSave.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItemSaveActionPerformed(evt);
}
});
jMenuSaveLoad.add(jMenuItemSave);
jMenuItemLoad.setText("Загрузить");
jMenuItemLoad.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItemLoadActionPerformed(evt);
}
});
jMenuSaveLoad.add(jMenuItemLoad);
jMenuItemSaveStorage.setText("Сохранить хранилище");
jMenuItemSaveStorage.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItemSaveStorageActionPerformed(evt);
}
});
jMenuSaveLoad.add(jMenuItemSaveStorage);
jMenuItemLoadStorage.setText("Загрузить хранилище");
jMenuItemLoadStorage.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItemLoadStorageActionPerformed(evt);
}
});
jMenuSaveLoad.add(jMenuItemLoadStorage);
jMenuBar1.add(jMenuSaveLoad);
setJMenuBar(jMenuBar1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
@ -238,7 +283,7 @@ public class JFrameMapWithSetAirBombers extends javax.swing.JFrame {
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(buttonDeleteMap, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 45, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 22, Short.MAX_VALUE)
.addComponent(buttonShowDeleted, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(buttonAddAirBomber)
@ -308,7 +353,7 @@ public class JFrameMapWithSetAirBombers extends javax.swing.JFrame {
return;
}
int pos = Integer.parseInt(maskedTextBoxPosition.getText());
DrawingObjectAirBomber deletedAirBomber = _mapsCollection.Get(listBoxMaps.getSelectedValue()).remove(pos);
DrawingObjectAirBomber deletedAirBomber = (DrawingObjectAirBomber) _mapsCollection.Get(listBoxMaps.getSelectedValue()).remove(pos);
if (deletedAirBomber != null)
{
deletedAirBombers.add(deletedAirBomber);
@ -372,6 +417,52 @@ public class JFrameMapWithSetAirBombers extends javax.swing.JFrame {
}
airBomberCanvas.getGraphics().drawImage(_mapsCollection.Get(listBoxMaps.getSelectedValue()).ShowSet(), 0, 0, null);
}//GEN-LAST:event_listBoxMapsValueChanged
private void jMenuItemSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemSaveActionPerformed
if (jFileChooser1.showDialog(this, "Сохранить") == JFileChooser.APPROVE_OPTION){
if (_mapsCollection.SaveData(jFileChooser1.getSelectedFile().getPath())){
JOptionPane.showMessageDialog(this, "Cохранено");
}
else{
JOptionPane.showMessageDialog(this, "Не сохранилось");
}
}
}//GEN-LAST:event_jMenuItemSaveActionPerformed
private void jMenuItemLoadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemLoadActionPerformed
if (jFileChooser1.showDialog(this, "Загрузить") == JFileChooser.APPROVE_OPTION){
if (_mapsCollection.LoadData(jFileChooser1.getSelectedFile().getPath())){
JOptionPane.showMessageDialog(this, "Загрузилось");
ReloadMaps();
}
else{
JOptionPane.showMessageDialog(this, "Не загрузилось");
}
}
}//GEN-LAST:event_jMenuItemLoadActionPerformed
private void jMenuItemSaveStorageActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemSaveStorageActionPerformed
if (jFileChooser1.showDialog(this, "Сохранить") == JFileChooser.APPROVE_OPTION){
if (_mapsCollection.SaveStorage(jFileChooser1.getSelectedFile().getPath(), listBoxMaps.getSelectedValue())){
JOptionPane.showMessageDialog(this, "Cохранено");
}
else{
JOptionPane.showMessageDialog(this, "Не сохранилось");
}
}
}//GEN-LAST:event_jMenuItemSaveStorageActionPerformed
private void jMenuItemLoadStorageActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemLoadStorageActionPerformed
if (jFileChooser1.showDialog(this, "Загрузить") == JFileChooser.APPROVE_OPTION){
if (_mapsCollection.LoadStorage(jFileChooser1.getSelectedFile().getPath())){
JOptionPane.showMessageDialog(this, "Загрузилось");
ReloadMaps();
}
else{
JOptionPane.showMessageDialog(this, "Не загрузилось");
}
}
}//GEN-LAST:event_jMenuItemLoadStorageActionPerformed
private void ReloadMaps()
{
@ -440,8 +531,15 @@ public class JFrameMapWithSetAirBombers extends javax.swing.JFrame {
private javax.swing.JButton buttonShowStorage;
private javax.swing.JComboBox<String> comboBoxSelectorMap;
private javax.swing.JButton downButton;
private javax.swing.JFileChooser jFileChooser1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JMenuItem jMenuItemLoad;
private javax.swing.JMenuItem jMenuItemLoadStorage;
private javax.swing.JMenuItem jMenuItemSave;
private javax.swing.JMenuItem jMenuItemSaveStorage;
private javax.swing.JMenu jMenuSaveLoad;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JButton leftButton;
private javax.swing.JList<String> listBoxMaps;

View File

@ -70,6 +70,32 @@ public class MapWithSetAirBombersGeneric <T extends IDrawingObject, U extends Ab
return new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_ARGB);
}
/// <summary>
/// Получение данных в виде строки
/// </summary>
/// <param name="sep"></param>
/// <returns></returns>
public String GetData(String separatorType, String separatorData)
{
String data = _map.getClass().getSimpleName() + separatorType;
for (var airBomber : _setAirBombers.GetAirBombers())
{
data += airBomber.GetInfo() + separatorData;
}
return data;
}
public void LoadData(String[] records)
{
for (var rec : records)
{
_setAirBombers.Insert((T) DrawingObjectAirBomber.Create(rec));
}
}
public void clearStorage(){
_setAirBombers.clear();
}
private void Shaking()
{
int j = _setAirBombers.getCount() - 1;

View File

@ -5,6 +5,8 @@
package AirBomberPackage;
import java.util.HashMap;
import java.util.ArrayList;
import java.io.*;
import javax.swing.*;
/**
*
* @author Андрей
@ -13,7 +15,11 @@ public class MapsCollection {
/// <summary>
/// Словарь (хранилище) с картами
/// </summary>
HashMap<String, MapWithSetAirBombersGeneric<DrawingObjectAirBomber, AbstractMap>> _mapStorages;
HashMap<String, MapWithSetAirBombersGeneric<IDrawingObject, AbstractMap>> _mapStorages;
private String separatorDict = "-";
private String separatorData = ";";
/// <summary>
/// Возвращение списка названий карт
/// </summary>
@ -36,7 +42,7 @@ public class MapsCollection {
/// <param name="pictureHeight"></param>
public MapsCollection(int pictureWidth, int pictureHeight)
{
_mapStorages = new HashMap<String, MapWithSetAirBombersGeneric<DrawingObjectAirBomber, AbstractMap>>();
_mapStorages = new HashMap<String, MapWithSetAirBombersGeneric<IDrawingObject, AbstractMap>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
@ -51,7 +57,7 @@ public class MapsCollection {
{
return false;
}
_mapStorages.put(name, new MapWithSetAirBombersGeneric<DrawingObjectAirBomber, AbstractMap>(_pictureWidth, _pictureHeight, map));
_mapStorages.put(name, new MapWithSetAirBombersGeneric<IDrawingObject, AbstractMap>(_pictureWidth, _pictureHeight, map));
return true;
}
/// <summary>
@ -66,14 +72,154 @@ public class MapsCollection {
}
}
public MapWithSetAirBombersGeneric<DrawingObjectAirBomber, AbstractMap> Get(String ind){
public MapWithSetAirBombersGeneric<IDrawingObject, AbstractMap> Get(String ind){
if (_mapStorages.containsKey(ind)) return _mapStorages.get(ind);
else return null;
}
public DrawingObjectAirBomber Get(String ind, int index){
if (!_mapStorages.containsKey(ind)) return null;
MapWithSetAirBombersGeneric<DrawingObjectAirBomber, AbstractMap> map = _mapStorages.get(ind);
return map.getSetAirBombers().Get(index);
public boolean SaveData(String filename)
{
File saveFile = new File(filename);
if (saveFile.exists())
{
saveFile.delete();
}
try{
BufferedWriter bw = new BufferedWriter(new FileWriter(saveFile));
bw.write("MapsCollection\n");
for (var storage : _mapStorages.keySet())
{
bw.write(storage + separatorDict + _mapStorages.get(storage).GetData(separatorDict, separatorData) + "\n");
}
bw.close();
}
catch (IOException ex){
return false;
}
return true;
}
/// <summary>
/// Загрузка нформации по автомобилям на парковках из файла
/// </summary>
/// <param name="filename"></param>
/// <returns></returns>
public boolean LoadData(String filename)
{
File loadFile = new File(filename);
if (!loadFile.exists())
{
return false;
}
try{
BufferedReader br = new BufferedReader(new FileReader(loadFile));
String line;
line = br.readLine();
line.trim();
if (!line.equals("MapsCollection")) return false;
_mapStorages.clear();
while ((line = br.readLine()) != null)
{
line = line.trim();
var elem = line.split(separatorDict);
AbstractMap map = null;
switch (elem[1])
{
case "SimpleMap":
map = new SimpleMap();
break;
case "CityMap":
map = new CityMap();
break;
case "LineMap":
map = new LineMap();
break;
}
_mapStorages.put(elem[0], new MapWithSetAirBombersGeneric<IDrawingObject, AbstractMap>(_pictureWidth, _pictureHeight, map));
_mapStorages.get(elem[0]).LoadData(elem[2].split(separatorData));
}
br.close();
return true;
}
catch (IOException ex){
return false;
}
}
public boolean SaveStorage(String filename, String storageKey)
{
File saveFile = new File(filename);
if (saveFile.exists())
{
saveFile.delete();
}
try{
BufferedWriter bw = new BufferedWriter(new FileWriter(saveFile));
bw.write("Storage\n");
bw.write(storageKey + "\n");
String[] storageStrings = _mapStorages.get(storageKey).GetData(separatorDict, separatorData).split(separatorDict);
bw.write(storageStrings[0] + "\n");
String[] bombers = storageStrings[1].split(separatorData);
bw.write(bombers.length + "\n");
for (String bomber : bombers ){
bw.write(bomber + "\n");
}
bw.close();
}
catch (IOException ex){
return false;
}
return true;
}
public boolean LoadStorage(String filename){
File loadFile = new File(filename);
if (!loadFile.exists())
{
return false;
}
try{
BufferedReader br = new BufferedReader(new FileReader(loadFile));
String line;
line = br.readLine();
line.trim();
if (!line.equals("Storage")) return false;
String key = br.readLine();
String mapType = br.readLine();
if (_mapStorages.containsKey(key)){
_mapStorages.get(key).clearStorage();
}
else{
AbstractMap map = null;
switch (mapType)
{
case "SimpleMap":
map = new SimpleMap();
break;
case "CityMap":
map = new CityMap();
break;
case "LineMap":
map = new LineMap();
break;
}
_mapStorages.put(key, new MapWithSetAirBombersGeneric<IDrawingObject, AbstractMap>(_pictureWidth, _pictureHeight, map));
}
int numberOfBombers = Integer.parseInt( br.readLine());
for (int i =0; i< numberOfBombers;i++){
String bomber = br.readLine();
int res = _mapStorages.get(key).add(new DrawingObjectAirBomber(ExtentionAirBomber.ExtentionedAirBomber.CreateDrawningAirBomber(bomber)));
}
br.close();
return true;
}
catch (IOException ex){
return false;
}
}
}

View File

@ -92,4 +92,21 @@ public class SetAirBombersGeneric<T> {
}
_places.set(position, value);
}
public ArrayList<T> GetAirBombers(){
ArrayList<T> notNullAirBombers = new ArrayList<>();
for(var airBomber : _places){
if (airBomber != null){
notNullAirBombers.add(airBomber);
}
else{
break;
}
}
return notNullAirBombers;
}
public void clear(){
_places.clear();
}
}