Лабораторная работа №7

This commit is contained in:
ENDORFIT 2024-06-09 00:00:39 +04:00
parent 4de23d08a2
commit 6e40554fa2
29 changed files with 269 additions and 94 deletions

View File

@ -6,14 +6,7 @@
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="39e7c520-e505-4e53-a269-c949d7c9f5d1" name="Changes" comment=""> <list default="true" id="39e7c520-e505-4e53-a269-c949d7c9f5d1" name="Changes" comment="">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/CountWheels.java" beforeDir="false" /> <change beforePath="$PROJECT_DIR$/src/Scripts/CollectionGenericObjects/StorageCollection.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/Scripts/CollectionGenericObjects/StorageCollection.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/DirectionType.java" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/src/DrawingField.java" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/src/DrawingModernMonorail.java" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/src/DrawingWheels.java" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/src/EntityModernMonorail.java" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/src/FormModernMonorail.java" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/src/Program.java" beforeDir="false" />
</list> </list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" /> <option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -23,6 +16,7 @@
<component name="FileTemplateManagerImpl"> <component name="FileTemplateManagerImpl">
<option name="RECENT_TEMPLATES"> <option name="RECENT_TEMPLATES">
<list> <list>
<option value="Interface" />
<option value="Class" /> <option value="Class" />
</list> </list>
</option> </option>
@ -39,15 +33,16 @@
<option name="hideEmptyMiddlePackages" value="true" /> <option name="hideEmptyMiddlePackages" value="true" />
<option name="showLibraryContents" value="true" /> <option name="showLibraryContents" value="true" />
</component> </component>
<component name="PropertiesComponent"><![CDATA[{ <component name="PropertiesComponent">{
"keyToString": { &quot;keyToString&quot;: {
"Application.Program.executor": "Run", &quot;Application.Program.executor&quot;: &quot;Run&quot;,
"RunOnceActivity.OpenProjectViewOnStart": "true", &quot;RunOnceActivity.OpenProjectViewOnStart&quot;: &quot;true&quot;,
"RunOnceActivity.ShowReadmeOnStart": "true", &quot;RunOnceActivity.ShowReadmeOnStart&quot;: &quot;true&quot;,
"git-widget-placeholder": "LabWorking__2", &quot;git-widget-placeholder&quot;: &quot;LabWork6&quot;,
"kotlin-language-version-configured": "true" &quot;kotlin-language-version-configured&quot;: &quot;true&quot;,
&quot;last_opened_file_path&quot;: &quot;C:/Учеба/AgarioGame&quot;
} }
}]]></component> }</component>
<component name="RunManager"> <component name="RunManager">
<configuration name="Program" type="Application" factoryName="Application"> <configuration name="Program" type="Application" factoryName="Application">
<option name="MAIN_CLASS_NAME" value="Scripts.Program" /> <option name="MAIN_CLASS_NAME" value="Scripts.Program" />

View File

@ -0,0 +1,61 @@
package Scripts.CollectionAdditionalObjects;
import Scripts.Drawing.DrawingModernMonorail;
import Scripts.Drawing.DrawingMonorail;
import Scripts.Entities.EntityModernMonorail;
import Scripts.Entities.EntityMonorail;
import Scripts.Wheels.IDrawingWheels;
import java.lang.reflect.Array;
import java.util.Random;
public class AdditionalCollections <T extends EntityMonorail, U extends IDrawingWheels>{
public T[] _collectionEntity;
public U[] _collectionDecks;
public AdditionalCollections(int size, Class<T> type1, Class<T> type2) {
_collectionEntity = (T[]) Array.newInstance(type1, size);
_collectionDecks = (U[]) Array.newInstance(type2, size);
CountEntities = size;
CountDecks = size;
}
public int CountEntities;
public int CountDecks;
public int Insert(T entity) {
int index = 0;
while (index < CountEntities) {
if (_collectionEntity[index] == null)
{
_collectionEntity[index] = entity;
return index;
}
++index;
}
return -1;
}
public int Insert(U decks) {
int index = 0;
while (index < CountDecks) {
if (_collectionDecks[index] == null)
{
_collectionDecks[index] = decks;
return index;
}
++index;
}
return -1;
}
public DrawingMonorail CreateAdditionalCollectionMonorail() {
Random random = new Random();
if (_collectionEntity == null || _collectionDecks == null) return null;
T entity = _collectionEntity[random.nextInt(CountEntities)];
U wheels = _collectionDecks[random.nextInt(CountDecks)];
DrawingMonorail drawingMonorail = null;
if (entity instanceof EntityModernMonorail) {
drawingMonorail = new DrawingModernMonorail((EntityModernMonorail) entity, wheels);
}
else {
drawingMonorail = new DrawingMonorail(entity, wheels);
}
return drawingMonorail;
}
}

View File

@ -44,7 +44,7 @@ public class AdditionalCollection <T extends EntityMonorail, U extends IDrawingW
} }
return -1; return -1;
} }
public DrawingMonorail CreateAdditionalCollectionMonorail() { public DrawingMonorail CreateAdditionalCollectionStormtrooper() {
Random random = new Random(); Random random = new Random();
if (CollectionEntity == null || CollectionWheels == null) return null; if (CollectionEntity == null || CollectionWheels == null) return null;
T entity = CollectionEntity[random.nextInt(CountEntities)]; T entity = CollectionEntity[random.nextInt(CountEntities)];

View File

@ -7,18 +7,13 @@ import java.util.NoSuchElementException;
public class ListGenericObjects<T> implements ICollectionGenericObjects<T> { public class ListGenericObjects<T> implements ICollectionGenericObjects<T> {
private List<T> _collection; private List<T> _collection;
private CollectionType collectionType = CollectionType.List;
private int _maxCount; private int _maxCount;
private CollectionType collectionType = CollectionType.List;
public int getCount() { public int getCount() {
return _collection.size(); return _collection.size();
} }
@Override
public CollectionType GetCollectionType() {
return collectionType;
}
@Override @Override
public void SetMaxCount(int size) { public void SetMaxCount(int size) {
if (size > 0) { if (size > 0) {
@ -30,6 +25,11 @@ public class ListGenericObjects<T> implements ICollectionGenericObjects<T> {
_collection = new ArrayList<T>(); _collection = new ArrayList<T>();
} }
@Override
public CollectionType GetCollectionType() {
return collectionType;
}
@Override @Override
public T Get(int position) public T Get(int position)
{ {

View File

@ -8,8 +8,8 @@ import java.util.NoSuchElementException;
public class MassiveGenericObjects<T> implements ICollectionGenericObjects<T> public class MassiveGenericObjects<T> implements ICollectionGenericObjects<T>
{ {
private T[] _collection;
private CollectionType collectionType = CollectionType.Massive; private CollectionType collectionType = CollectionType.Massive;
private T[] _collection;
private int Count; private int Count;
public void SetMaxCount(int size) { public void SetMaxCount(int size) {
if (size > 0) { if (size > 0) {
@ -24,7 +24,6 @@ public class MassiveGenericObjects<T> implements ICollectionGenericObjects<T>
public CollectionType GetCollectionType() { public CollectionType GetCollectionType() {
return collectionType; return collectionType;
} }
@Override @Override
public int getCount() { public int getCount() {
return Count; return Count;
@ -57,7 +56,6 @@ public class MassiveGenericObjects<T> implements ICollectionGenericObjects<T>
if (position >= getCount() || position < 0) return null; if (position >= getCount() || position < 0) return null;
return (T) _collection[position]; return (T) _collection[position];
} }
@Override @Override
public Iterable<T> GetItems() { public Iterable<T> GetItems() {
return new Iterable<T>() { return new Iterable<T>() {

View File

@ -66,7 +66,7 @@ public class StorageCollection<T extends DrawingMonorail> {
sb.append(value.getValue().getCount()); sb.append(value.getValue().getCount());
sb.append(_separatorForKeyValueS); sb.append(_separatorForKeyValueS);
for (T monorail : value.getValue().GetItems()) { for (T monorail : value.getValue().GetItems()) {
String data = ExtentionDrawningMonorail.GetDataForSave((DrawingMonorail) monorail); String data = ExtentionDrawningMonorail.GetDataForSave((DrawingMonorail)monorail);
if (data.isEmpty()) continue; if (data.isEmpty()) continue;
sb.append(data); sb.append(data);
sb.append(_separatorItemsS); sb.append(_separatorItemsS);
@ -132,8 +132,8 @@ public class StorageCollection<T extends DrawingMonorail> {
collection.SetMaxCount(Integer.parseInt(record[2])); collection.SetMaxCount(Integer.parseInt(record[2]));
String[] set = record[3].split(_separatorItems); String[] set = record[3].split(_separatorItems);
for (String elem : set) { for (String elem : set) {
DrawingMonorail ship = ExtentionDrawningMonorail.CreateDrawingShip(elem); DrawingMonorail drawingMonorail = ExtentionDrawningMonorail.CreateDrawingMonorail(elem);
if (collection.Insert((T) ship) == -1) if (collection.Insert((T) drawingMonorail) == -1)
{ {
return false; return false;
} }
@ -153,29 +153,35 @@ public class StorageCollection<T extends DrawingMonorail> {
String s = fs.readLine(); String s = fs.readLine();
if (s == null || s.isEmpty() || !s.startsWith(_collectionName)) if (s == null || s.isEmpty() || !s.startsWith(_collectionName))
return false; return false;
if (_storages.containsKey(s)) {
_storages.get(s).ClearCollection();
}
s = fs.readLine(); s = fs.readLine();
String[] record = s.split(_separatorForKeyValue); String[] record = s.split(_separatorForKeyValue);
if (record.length != 4) { if (record.length != 4) {
return false; return false;
} }
ICollectionGenericObjects<T> collection = CreateCollection(record[1]); ICollectionGenericObjects<T> collection;
if (collection == null)
{ if (_storages.containsKey(s)) {
return false; _storages.get(s).ClearCollection();
collection = _storages.get(s);
if (record[1] == "Massive" && collection instanceof ListGenericObjects<T> || record[1] == "List" && collection instanceof MassiveGenericObjects<T>) collection = CreateCollection(record[1]);
} }
else {
collection = CreateCollection(record[1]);
_storages.put(record[0], collection);
}
collection.SetMaxCount(Integer.parseInt(record[2])); collection.SetMaxCount(Integer.parseInt(record[2]));
String[] set = record[3].split(_separatorItems); String[] set = record[3].split(_separatorItems);
for (String elem : set) { for (String elem : set) {
DrawingMonorail monorail = ExtentionDrawningMonorail.CreateDrawingShip(elem); DrawingMonorail drawingMonorail = ExtentionDrawningMonorail.CreateDrawingMonorail(elem);
if (collection.Insert((T) monorail) == -1) if (collection.Insert((T) drawingMonorail) == -1)
{ {
return false; return false;
} }
} }
_storages.put(record[0], collection);
return true; return true;
} }
catch (IOException e) { catch (IOException e) {

View File

@ -1,8 +1,7 @@
package Scripts.Drawing; package Scripts.Drawing;
import Scripts.Entities.EntityMonorail; import Scripts.Entities.EntityMonorail;
import Scripts.Wheels.DrawingWheels; import Scripts.Wheels.*;
import Scripts.Wheels.IDrawingWheels;
import java.awt.*; import java.awt.*;
import java.util.Random; import java.util.Random;
@ -234,4 +233,17 @@ public class DrawingMonorail {
_wheels.DrawWheels(g2, coordinatesX, _startPositionY + 30, _entityMonorail.getBodyColor()); _wheels.DrawWheels(g2, coordinatesX, _startPositionY + 30, _entityMonorail.getBodyColor());
} }
public String[] GetStringRepresentationDecks() {
if (_wheels.GetOrnament() instanceof DrawOrnament) {
return new String[]{String.valueOf(_wheels.getCountWheels()), "DrawingDecksType1"};
}
else if (_wheels.GetOrnament() instanceof DrawOrnamentOval) {
return new String[]{String.valueOf(_wheels.getCountWheels()), "DrawingDecksType2"};
}
else if (_wheels.GetOrnament() instanceof DrawOrnamentTriangle) {
return new String[]{String.valueOf(_wheels.getCountWheels()), "DrawingDecksType3"};
}
return null;
}
} }

View File

@ -2,7 +2,7 @@ package Scripts.Drawing;
import Scripts.Entities.EntityModernMonorail; import Scripts.Entities.EntityModernMonorail;
import Scripts.Entities.EntityMonorail; import Scripts.Entities.EntityMonorail;
import Scripts.Wheels.IDrawingWheels; import Scripts.Wheels.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
@ -10,20 +10,20 @@ import java.util.Collections;
public class ExtentionDrawningMonorail { public class ExtentionDrawningMonorail {
private static String _separatorForObjectS = ":"; private static String _separatorForObjectS = ":";
private static String _separatorForObject = "\\:"; private static String _separatorForObject = "\\:";
public static DrawingMonorail CreateDrawingShip(String info) { public static DrawingMonorail CreateDrawingMonorail(String info) {
String[] strs = info.split(_separatorForObject); String[] strs = info.split(_separatorForObject);
EntityMonorail entityMonorail; EntityMonorail entityMonorail;
IDrawingWheels wheels = null; IDrawingWheels wheels = new DrawingWheels();
if (strs.length == 8) if (strs.length == 8)
{ {
String s = strs[8]; String s = strs[8];
switch (s) { switch (s) {
case "DrawingDecksType1": case "DrawingDecksType1":
wheels = new DrawingDecksType1(); wheels.SetOrnament(new DrawOrnament());
case "DrawingDecksType2": case "DrawingDecksType2":
wheels = new DrawingDecksType2(); wheels.SetOrnament(new DrawOrnamentOval());
case "DrawingDecksType3": case "DrawingDecksType3":
wheels = new DrawingDecksType3(); wheels.SetOrnament(new DrawOrnamentTriangle());
} }
if (wheels != null) wheels.SetCountWheels(Integer.parseInt(strs[7])); if (wheels != null) wheels.SetCountWheels(Integer.parseInt(strs[7]));
} }
@ -31,18 +31,18 @@ public class ExtentionDrawningMonorail {
String s = strs[5]; String s = strs[5];
switch (s) { switch (s) {
case "DrawingDecksType1": case "DrawingDecksType1":
wheels = new DrawingDecksType1(); wheels.SetOrnament(new DrawOrnament());
case "DrawingDecksType2": case "DrawingDecksType2":
wheels = new DrawingDecksType2(); wheels.SetOrnament(new DrawOrnamentOval());
case "DrawingDecksType3": case "DrawingDecksType3":
wheels = new DrawingDecksType3(); wheels.SetOrnament(new DrawOrnamentTriangle());
} }
if (wheels != null) wheels.SetCountWheels(Integer.parseInt(strs[4])); if (wheels != null) wheels.SetCountWheels(Integer.parseInt(strs[4]));
} }
entityMonorail = EntityModernMonorail.CreateEntityModernMonorail(strs); entityMonorail = EntityModernMonorail.CreateEntityModernMonorail(strs);
if (entityMonorail != null) if (entityMonorail != null)
{ {
return new DrawingModernMonorail((EntityModernMonorail) entityMonorail, wheels); return new DrawingModernMonorail((EntityModernMonorail)entityMonorail, wheels);
} }
entityMonorail = EntityMonorail.CreateEntityMonorail(strs); entityMonorail = EntityMonorail.CreateEntityMonorail(strs);
if (entityMonorail != null) if (entityMonorail != null)

View File

@ -13,7 +13,7 @@ public class EntityModernMonorail extends EntityMonorail {
public boolean getMonorailTrack() {return _monorailTrack;} public boolean getMonorailTrack() {return _monorailTrack;}
public boolean getCabin() {return _cabin;} public boolean getCabin() {return _cabin;}
public void setAdditionalColor(Color value) {_additionalColor = _additionalColor;} public void setAdditionalColor(Color value) {_additionalColor = value;}
public void setMonorailTrack(Boolean value) { _monorailTrack = value;} public void setMonorailTrack(Boolean value) { _monorailTrack = value;}
public void setCabin(Boolean value) { _cabin = value;} public void setCabin(Boolean value) { _cabin = value;}
@ -27,20 +27,23 @@ public class EntityModernMonorail extends EntityMonorail {
_monorailTrack = monorailTrack; _monorailTrack = monorailTrack;
_cabin = cabin; _cabin = cabin;
Step = _speed * 100/ (float)_weight; Step = _speed * 100/ (int)_weight;
} }
@Override @Override
public String[] GetStringRepresentation() public String[] GetStringRepresentation()
{ {
return new String[]{"EntityModernMonorail", _speed.toString(), _weight.toString(), Integer speed = _speed;
Float weight = _weight;
return new String[]{"EntityModernMonorail", speed.toString(), weight.toString(),
colorToHexString(getBodyColor()), colorToHexString(getAdditionalColor()), colorToHexString(getBodyColor()), colorToHexString(getAdditionalColor()),
String.valueOf(_monorailTrack), String.valueOf(_cabin)}; String.valueOf(_monorailTrack), String.valueOf(_cabin)};
} }
public static EntityModernMonorail CreateEntityModernMonorail(String[] strs) public static EntityModernMonorail CreateEntityModernMonorail(String[] strs)
{ {
if (strs.length != 10 || !Objects.equals(strs[0], "EntityModernMonorail")) if (!Objects.equals(strs[0], "EntityModernMonorail"))
{ {
return null; return null;
} }

View File

@ -6,8 +6,8 @@ import java.util.Random;
public class EntityMonorail { public class EntityMonorail {
public float Step; public float Step;
protected Integer _speed; protected int _speed;
protected Float _weight; protected float _weight;
protected Color _bodyColor; protected Color _bodyColor;
public int getSpeed() { public int getSpeed() {
@ -30,27 +30,27 @@ public class EntityMonorail {
_weight = weight <= 0 ? rnd.nextInt(100)+500 : weight; _weight = weight <= 0 ? rnd.nextInt(100)+500 : weight;
_bodyColor = bodyColor; _bodyColor = bodyColor;
Step = _speed * 100/ (float)_weight; Step = _speed * 100/ (int)_weight;
} }
public String[] GetStringRepresentation() public String[] GetStringRepresentation()
{ {
return new String[]{"EntityMonorail", _speed.toString(), _weight.toString(), colorToHexString(_bodyColor)}; Integer speed = _speed;
} Float weight = _weight;
return new String[]{"EntityMonorail", speed.toString(), weight.toString(), colorToHexString(_bodyColor)};
}
public static EntityMonorail CreateEntityMonorail(String[] strs) public static EntityMonorail CreateEntityMonorail(String[] strs)
{ {
if (strs.length != 6 || !Objects.equals(strs[0], "EntityMonorail")) if (!Objects.equals(strs[0], "EntityMonorail"))
{ {
return null; return null;
} }
return new EntityMonorail(Integer.parseInt(strs[1]), Float.parseFloat(strs[2]), hexStringToColor(strs[3])); return new EntityMonorail(Integer.parseInt(strs[1]), Float.parseFloat(strs[2]), hexStringToColor(strs[3]));
} }
public static String colorToHexString(Color color) { public static String colorToHexString(Color color) {
return String.format("#%02x%02x%02x", color.getRed(), color.getGreen(), color.getBlue()); return String.format("#%02x%02x%02x", color.getRed(), color.getGreen(), color.getBlue());
} }
public static Color hexStringToColor(String hexString) { public static Color hexStringToColor(String hexString) {
return Color.decode(hexString); return Color.decode(hexString);
} }

View File

@ -24,7 +24,7 @@ public class FormAdditionalCollection extends JFrame {
private JButton buttonGenerate = new JButton("Создать"); private JButton buttonGenerate = new JButton("Создать");
private JButton buttonGoToCollection = new JButton("В коллекцию"); private JButton buttonGoToCollection = new JButton("В коллекцию");
private JList<String> listEntity = new JList<String>(); private JList<String> listEntity = new JList<String>();
private JList<String> listWheels = new JList<String>(); private JList<String> listEngines = new JList<String>();
public FormAdditionalCollection() { public FormAdditionalCollection() {
setTitle("Случайные монорельсы"); setTitle("Случайные монорельсы");
setMinimumSize(new Dimension(970,310)); setMinimumSize(new Dimension(970,310));
@ -45,7 +45,7 @@ public class FormAdditionalCollection extends JFrame {
buttonGenerate.addActionListener(new ActionListener() { buttonGenerate.addActionListener(new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
_drawingMonorail = _additionalCollection.CreateAdditionalCollectionMonorail(); _drawingMonorail = _additionalCollection.CreateAdditionalCollectionStormtrooper();
_drawingMonorail.SetPictureSize(getWidth(), getHeight()); _drawingMonorail.SetPictureSize(getWidth(), getHeight());
_drawingMonorail.SetPosition(360,30); _drawingMonorail.SetPosition(360,30);
_canvasMonorail._drawingMonorail = _drawingMonorail; _canvasMonorail._drawingMonorail = _drawingMonorail;
@ -60,11 +60,11 @@ public class FormAdditionalCollection extends JFrame {
buttonGoToCollection.setBounds(830,200,120,60); buttonGoToCollection.setBounds(830,200,120,60);
buttonGenerate.setBounds(830, 130, 120, 60); buttonGenerate.setBounds(830, 130, 120, 60);
listEntity.setBounds(10,200,400,60); listEntity.setBounds(10,200,400,60);
listWheels.setBounds(420,200,400,60); listEngines.setBounds(420,200,400,60);
add(buttonGenerate); add(buttonGenerate);
add(buttonGoToCollection); add(buttonGoToCollection);
add(listEntity); add(listEntity);
add(listWheels); add(listEngines);
add(_canvasMonorail); add(_canvasMonorail);
setVisible(true); setVisible(true);
} }
@ -128,6 +128,6 @@ public class FormAdditionalCollection extends JFrame {
data2[i] = ToString(wheels); data2[i] = ToString(wheels);
} }
listEntity.setListData(data1); listEntity.setListData(data1);
listWheels.setListData(data2); listEngines.setListData(data2);
} }
} }

View File

@ -37,6 +37,14 @@ public class FormMonorailCollection extends JFrame{
private JButton RefreshButton = new JButton("Обновить"); private JButton RefreshButton = new JButton("Обновить");
private JComboBox ComboBoxCollections = new JComboBox(new String[]{"", "Хранилище"}); private JComboBox ComboBoxCollections = new JComboBox(new String[]{"", "Хранилище"});
private JFormattedTextField TextField; private JFormattedTextField TextField;
private JFormattedTextField MaskedTextField;
private JMenuBar menuBar = new JMenuBar();
private JMenu fileMenu = new JMenu("File");
private JMenuItem loadItem = new JMenuItem("Load");
private JMenuItem saveItem = new JMenuItem("Save");
private JMenuItem loadCollection = new JMenuItem("Load coll");
private JMenuItem saveCollection = new JMenuItem("Save coll");
public FormMonorailCollection(String title, Dimension dimension) { public FormMonorailCollection(String title, Dimension dimension) {
this.title = title; this.title = title;
this.dimension = dimension; this.dimension = dimension;
@ -217,6 +225,37 @@ public class FormMonorailCollection extends JFrame{
} }
}); });
saveItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
SaveFile();
}
});
loadItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
LoadFile();
}
});
saveCollection.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Save coll");
SaveCollection();
}
});
loadCollection.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Load coll");
LoadCollection();
}
});
buttonCreateCompany.addActionListener(new ActionListener() { buttonCreateCompany.addActionListener(new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
@ -257,6 +296,14 @@ public class FormMonorailCollection extends JFrame{
} }
}); });
fileMenu.add(loadItem);
fileMenu.add(saveItem);
fileMenu.add(loadCollection);
fileMenu.add(saveCollection);
fileMenu.addSeparator();
menuBar.add(fileMenu);
setJMenuBar(menuBar);
ButtonGroup radioButtonsGroup = new ButtonGroup(); ButtonGroup radioButtonsGroup = new ButtonGroup();
JLabel labelCollectionName = new JLabel("Название коллекции"); JLabel labelCollectionName = new JLabel("Название коллекции");
radioButtonsGroup.add(radioButtonMassive); radioButtonsGroup.add(radioButtonMassive);
@ -301,6 +348,60 @@ public class FormMonorailCollection extends JFrame{
add(RefreshButton); add(RefreshButton);
setVisible(true); setVisible(true);
} }
private String SaveWindow() {
FileDialog fileDialog = new FileDialog(this, "Save File", FileDialog.SAVE);
fileDialog.setVisible(true);
String directory = fileDialog.getDirectory();
String file = fileDialog.getFile();
if (directory == null || file == null) return null;
return directory + file;
}
private void SaveFile() {
String filename = SaveWindow();
if (_storageCollection.SaveData(filename)) {
JOptionPane.showMessageDialog(null, "Сохранено");
}
else JOptionPane.showMessageDialog(null, "Ошибка сохранения");
}
private void SaveCollection() {
String filename = SaveWindow();
if (filename == null) {
JOptionPane.showMessageDialog(null, "Файл не выбран");
return;
}
if (listBoxCollection.getSelectedIndex() < 0 || listBoxCollection.getSelectedValue() == null) {
JOptionPane.showMessageDialog(null, "Коллекция не выбрана");
}
if (_storageCollection.SaveOneCollection(filename, listBoxCollection.getSelectedValue().toString())) {
JOptionPane.showMessageDialog(null, "Коллекция сохранена");
}
else JOptionPane.showMessageDialog(null, "Ошибка сохранения");
}
private String LoadWindow() {
FileDialog fileDialog = new FileDialog(this, "Save File", FileDialog.LOAD);
fileDialog.setVisible(true);
String directory = fileDialog.getDirectory();
String file = fileDialog.getFile();
if (directory == null || file == null) return null;
return directory + file;
}
private void LoadFile() {
String filename = LoadWindow();
if (_storageCollection.LoadData(filename)) {
JOptionPane.showMessageDialog(null, "Загрузка прошла успешно");
RerfreshListBoxItems();
}
else JOptionPane.showMessageDialog(null, "Не загрузилось");
}
private void LoadCollection() {
String filename = LoadWindow();
if (_storageCollection.LoadOneCollection(filename)) {
JOptionPane.showMessageDialog(null, "Коллекция загружена");
RerfreshListBoxItems();
}
else JOptionPane.showMessageDialog(null, "Не загрузилось");
}
private void RerfreshListBoxItems() { private void RerfreshListBoxItems() {
DefaultListModel<String> list = new DefaultListModel<String>(); DefaultListModel<String> list = new DefaultListModel<String>();
for (String name : _storageCollection.Keys()) { for (String name : _storageCollection.Keys()) {

View File

@ -35,9 +35,9 @@ public class FormMonorailConfig extends JFrame {
private JLabel labelBodyColor = new JLabel("Основной цвет", SwingConstants.CENTER); private JLabel labelBodyColor = new JLabel("Основной цвет", SwingConstants.CENTER);
private JLabel labelAdditionalColor = new JLabel("Дополнительный цвет", SwingConstants.CENTER); private JLabel labelAdditionalColor = new JLabel("Дополнительный цвет", SwingConstants.CENTER);
private JLabel labelWheels = new JLabel("Тип двигателей",SwingConstants.CENTER); private JLabel labelWheels = new JLabel("Тип двигателей",SwingConstants.CENTER);
private JLabel labelDefaultEngines = new JLabel("Классические",SwingConstants.CENTER); private JLabel labelDefaultWheels = new JLabel("Классические",SwingConstants.CENTER);
private JLabel labelOvalEngines = new JLabel("Овальные", SwingConstants.CENTER); private JLabel labelOvalOrnament = new JLabel("Овальные", SwingConstants.CENTER);
private JLabel labelTriangleEngines = new JLabel("Треугольные", SwingConstants.CENTER); private JLabel labelTriangleOrnament = new JLabel("Треугольные", SwingConstants.CENTER);
private JSpinner spinnerSpeed = new JSpinner(); private JSpinner spinnerSpeed = new JSpinner();
private JSpinner spinnerWeight = new JSpinner(); private JSpinner spinnerWeight = new JSpinner();
@ -121,9 +121,9 @@ public class FormMonorailConfig extends JFrame {
labelModernMonorail.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2)); labelModernMonorail.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
labelBodyColor.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2)); labelBodyColor.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
labelAdditionalColor.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2)); labelAdditionalColor.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
labelDefaultEngines.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2)); labelDefaultWheels.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
labelOvalEngines.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2)); labelOvalOrnament.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
labelTriangleEngines.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2)); labelTriangleOrnament.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
MouseAdapter labelObjectsMouseDown = new MouseAdapter() { MouseAdapter labelObjectsMouseDown = new MouseAdapter() {
@Override @Override
public void mousePressed(MouseEvent e) { public void mousePressed(MouseEvent e) {
@ -146,17 +146,17 @@ public class FormMonorailConfig extends JFrame {
labelModernMonorail.addMouseListener(labelObjectsMouseDown); labelModernMonorail.addMouseListener(labelObjectsMouseDown);
labelModernMonorail.setTransferHandler(labelObjectsTransferHandler); labelModernMonorail.setTransferHandler(labelObjectsTransferHandler);
MouseAdapter labelEnginesMouseDown = new MouseAdapter() { MouseAdapter labelWheelsMouseDown = new MouseAdapter() {
@Override @Override
public void mousePressed(MouseEvent e) { public void mousePressed(MouseEvent e) {
((JLabel) e.getComponent()).getTransferHandler().exportAsDrag(((JLabel) e.getComponent()), e, TransferHandler.COPY); ((JLabel) e.getComponent()).getTransferHandler().exportAsDrag(((JLabel) e.getComponent()), e, TransferHandler.COPY);
} }
}; };
labelDefaultEngines.addMouseListener(labelEnginesMouseDown); labelDefaultWheels.addMouseListener(labelWheelsMouseDown);
labelOvalEngines.addMouseListener(labelEnginesMouseDown); labelOvalOrnament.addMouseListener(labelWheelsMouseDown);
labelTriangleEngines.addMouseListener(labelEnginesMouseDown); labelTriangleOrnament.addMouseListener(labelWheelsMouseDown);
labelDefaultEngines.setTransferHandler(new TransferHandler() { labelDefaultWheels.setTransferHandler(new TransferHandler() {
@Override @Override
public int getSourceActions(JComponent c) {return TransferHandler.COPY;} public int getSourceActions(JComponent c) {return TransferHandler.COPY;}
@ -165,7 +165,7 @@ public class FormMonorailConfig extends JFrame {
return new WheelsTransferable(new DrawingWheels()); return new WheelsTransferable(new DrawingWheels());
} }
}); });
labelOvalEngines.setTransferHandler(new TransferHandler() { labelOvalOrnament.setTransferHandler(new TransferHandler() {
@Override @Override
public int getSourceActions(JComponent c) {return TransferHandler.COPY;} public int getSourceActions(JComponent c) {return TransferHandler.COPY;}
@ -174,7 +174,7 @@ public class FormMonorailConfig extends JFrame {
return new WheelsTransferable(new DrawOrnamentOval()); return new WheelsTransferable(new DrawOrnamentOval());
} }
}); });
labelTriangleEngines.setTransferHandler(new TransferHandler() { labelTriangleOrnament.setTransferHandler(new TransferHandler() {
@Override @Override
public int getSourceActions(JComponent c) {return TransferHandler.COPY;} public int getSourceActions(JComponent c) {return TransferHandler.COPY;}
@ -300,12 +300,8 @@ public class FormMonorailConfig extends JFrame {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
if (_drawingMonorail == null) return; if (_drawingMonorail == null) return;
DrawingMonorail copyDrawingMonorail;
if (_drawingMonorail instanceof DrawingModernMonorail) company._collection.Insert(_drawingMonorail);
copyDrawingMonorail = new DrawingModernMonorail((EntityModernMonorail) _drawingMonorail.getMonorail(), _drawingMonorail.getWheels());
else
copyDrawingMonorail = new DrawingMonorail(_drawingMonorail.getMonorail(), _drawingMonorail.getWheels());
company._collection.Insert(copyDrawingMonorail);
FormMonorailCollection.canvasShow(); FormMonorailCollection.canvasShow();
dispose(); dispose();
} }
@ -330,9 +326,9 @@ public class FormMonorailConfig extends JFrame {
labelBodyColor.setBounds(500,5,100, 40); labelBodyColor.setBounds(500,5,100, 40);
labelAdditionalColor.setBounds(605,5,170,40); labelAdditionalColor.setBounds(605,5,170,40);
labelWheels.setBounds(225,190,150,15); labelWheels.setBounds(225,190,150,15);
labelDefaultEngines.setBounds(140, 210, 100, 40); labelDefaultWheels.setBounds(140, 210, 100, 40);
labelOvalEngines.setBounds(250, 210, 100,40); labelOvalOrnament.setBounds(250, 210, 100,40);
labelTriangleEngines.setBounds(360,210,100,40); labelTriangleOrnament.setBounds(360,210,100,40);
labelColor.setBounds(200,10,50,15); labelColor.setBounds(200,10,50,15);
panelColorRed.setBounds(200, 30, 40, 40); panelColorRed.setBounds(200, 30, 40, 40);
panelColorGreen.setBounds(250, 30, 40,40); panelColorGreen.setBounds(250, 30, 40,40);
@ -354,10 +350,10 @@ public class FormMonorailConfig extends JFrame {
add(labelColor); add(labelColor);
add(labelBodyColor); add(labelBodyColor);
add(labelAdditionalColor); add(labelAdditionalColor);
add(labelDefaultEngines); add(labelDefaultWheels);
add(labelDefaultEngines); add(labelDefaultWheels);
add(labelOvalEngines); add(labelOvalOrnament);
add(labelTriangleEngines); add(labelTriangleOrnament);
add(spinnerSpeed); add(spinnerSpeed);
add(spinnerWeight); add(spinnerWeight);
add(spinnerNumberOfWheels); add(spinnerNumberOfWheels);

View File

@ -13,6 +13,8 @@ public class DrawingWheels implements IDrawingWheels{
_ornament = ornament; _ornament = ornament;
} }
public IOrnament GetOrnament() {return _ornament;}
public void SetCountWheels(int Count){ public void SetCountWheels(int Count){
for (CountWheels temp: CountWheels.values()) for (CountWheels temp: CountWheels.values())
if (temp.getCountWheels() == Count){ if (temp.getCountWheels() == Count){

View File

@ -4,6 +4,7 @@ import java.awt.*;
public interface IDrawingWheels { public interface IDrawingWheels {
public void SetOrnament(IOrnament ornament); public void SetOrnament(IOrnament ornament);
IOrnament GetOrnament();
public void SetCountWheels(int Count); public void SetCountWheels(int Count);
public int getCountWheels(); public int getCountWheels();
public void DrawWheels(Graphics2D g, int startPosX[], int startPosY, Color color); public void DrawWheels(Graphics2D g, int startPosX[], int startPosY, Color color);