Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4a7d61a167 | ||
|
|
baf8f83855 | ||
|
|
27b81ee5a9 | ||
|
|
a398df5403 | ||
|
|
0d85275050 |
11
.idea/PIbd-23-Salin-O.A.-Monorail-Hard.iml
generated
Normal file
11
.idea/PIbd-23-Salin-O.A.-Monorail-Hard.iml
generated
Normal file
@@ -0,0 +1,11 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
||||
3
.idea/misc.xml
generated
3
.idea/misc.xml
generated
@@ -1,5 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectRootManager" version="2" default="true">
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="17" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
</project>
|
||||
8
.idea/modules.xml
generated
Normal file
8
.idea/modules.xml
generated
Normal file
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/PIbd-23-Salin-O.A.-Monorail-Hard.iml" filepath="$PROJECT_DIR$/.idea/PIbd-23-Salin-O.A.-Monorail-Hard.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
||||
6
.idea/vcs.xml
generated
Normal file
6
.idea/vcs.xml
generated
Normal file
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
||||
@@ -6,6 +6,7 @@ import javax.swing.*;
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawningLocomotive extends DrawningMonorail{
|
||||
|
||||
public DrawningLocomotive(int speed, double weight, Color bodyColor, Color wheelColor, Color tireColor, int wheelNumb,
|
||||
int width, int height, boolean secondCabine, boolean magniteRail, Color additionalColor){
|
||||
super(speed, weight, bodyColor, wheelColor, tireColor, width, height);
|
||||
@@ -15,6 +16,10 @@ public class DrawningLocomotive extends DrawningMonorail{
|
||||
}
|
||||
}
|
||||
|
||||
public void ChangeAddColor(Color col){
|
||||
((EntityLocomotive)EntityMonorail).AdditionalColor = col;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void DrawMonorail(Graphics2D g2d){
|
||||
if (!(EntityMonorail instanceof EntityLocomotive))
|
||||
|
||||
@@ -17,8 +17,9 @@ public class DrawningMonorail {
|
||||
public int _pictureHeight;
|
||||
protected int _startPosX;
|
||||
protected int _startPosY;
|
||||
protected int _monorailWidth = 133;
|
||||
protected int _monorailHeight = 50;
|
||||
public int _monorailWidth = 133;
|
||||
public int _wheelNumb;
|
||||
public int _monorailHeight = 50;
|
||||
protected IDraw DrawningWheels;
|
||||
|
||||
protected int wheelSz;
|
||||
@@ -45,9 +46,42 @@ public class DrawningMonorail {
|
||||
DrawningWheels = new DrawningWheelsCart(_monorailWidth - dif, _monorailHeight,_startPosX,_startPosY,wheelColor,tireColor);
|
||||
else
|
||||
DrawningWheels = new DrawningWheelsOrn(_monorailWidth - dif, _monorailHeight,_startPosX,_startPosY,wheelColor,tireColor);
|
||||
DrawningWheels.ChangeWheelsNumb(rand.nextInt(1, 6));
|
||||
DrawningWheels.ChangeWheelsNumb(rand.nextInt(4));
|
||||
}
|
||||
|
||||
public int GetIDrawType(){
|
||||
if(DrawningWheels instanceof DrawningWheels)
|
||||
return 0;
|
||||
if(DrawningWheels instanceof DrawningWheelsCart)
|
||||
return 1;
|
||||
if(DrawningWheels instanceof DrawningWheelsOrn)
|
||||
return 2;
|
||||
return -1;
|
||||
}
|
||||
|
||||
public void ChangeIDraw(int variant){
|
||||
IDraw cur;
|
||||
int dif = _monorailWidth / 10;
|
||||
if(variant == 0)
|
||||
cur = new DrawningWheels(_monorailWidth - dif, _monorailHeight,_startPosX,_startPosY,Color.GRAY,Color.BLACK);
|
||||
else if(variant == 1)
|
||||
cur = new DrawningWheelsCart(_monorailWidth - dif, _monorailHeight,_startPosX,_startPosY, Color.GRAY,Color.BLACK);
|
||||
else
|
||||
cur = new DrawningWheelsOrn(_monorailWidth - dif, _monorailHeight,_startPosX,_startPosY,Color.GRAY, Color.BLACK);
|
||||
ChangeIDraw(cur);
|
||||
|
||||
}
|
||||
public void ChangeIDraw(IDraw obj){
|
||||
DrawningWheels = obj;
|
||||
obj.ChangeWheelsNumb(_wheelNumb);
|
||||
obj.ChangeWheelsNumb(_wheelNumb);
|
||||
obj.ChangeX(_startPosX);
|
||||
obj.ChangeY(_startPosY);
|
||||
}
|
||||
public void ChangeWheelsNumb(int numb){
|
||||
_wheelNumb = numb;
|
||||
DrawningWheels.ChangeWheelsNumb(numb);
|
||||
}
|
||||
protected DrawningMonorail(int speed, double weight, Color bodyColor, Color wheelColor, Color tireColor,
|
||||
int width, int height, int monorailWidth, int monorailHeight){
|
||||
if(width <= _monorailWidth || height <= _monorailHeight)
|
||||
@@ -144,6 +178,10 @@ public class DrawningMonorail {
|
||||
DrawningWheels.ChangeY(_startPosY);
|
||||
}
|
||||
|
||||
public void ChangeColor(Color col){
|
||||
EntityMonorail.BodyColor = col;
|
||||
}
|
||||
|
||||
public void DrawMonorail(Graphics2D g2d){
|
||||
if (EntityMonorail == null)
|
||||
return;
|
||||
|
||||
@@ -61,7 +61,7 @@ public class DrawningWheelsCart implements IDraw{
|
||||
CurY + Height / 10 * 7};
|
||||
g2d.setColor(Color.BLACK);
|
||||
g2d.fillPolygon(xPointsArrFrontCart, yPointsArrFrontCart, xPointsArrFrontCart.length);
|
||||
g2d.setColor(TireColor);
|
||||
g2d.setColor(Color.WHITE);
|
||||
g2d.drawLine(CurX + Width / 10 * 2, CurY + Height / 10 * 7,CurX + Width / 10 * 4, CurY + Height / 10 * 9);
|
||||
g2d.drawLine(CurX + Width / 10 * 4, CurY + Height / 10 * 7,CurX + Width / 10 * 2, CurY + Height / 10 * 9);
|
||||
|
||||
@@ -72,7 +72,7 @@ public class DrawningWheelsCart implements IDraw{
|
||||
CurY + Height / 10 * 9, CurY + Height / 10 * 9};
|
||||
g2d.setColor(Color.BLACK);
|
||||
g2d.fillPolygon(xPointsArrBackCart, yPointsArrBackCart, xPointsArrBackCart.length);
|
||||
g2d.setColor(TireColor);
|
||||
g2d.setColor(Color.WHITE);
|
||||
g2d.drawLine(CurX + Width / 10 * 6, CurY + Height / 10 * 7,CurX + Width / 10 * 9, CurY + Height / 10 * 9);
|
||||
g2d.drawLine(CurX + Width / 10 * 9, CurY + Height / 10 * 7,CurX + Width / 10 * 6, CurY + Height / 10 * 9);
|
||||
|
||||
|
||||
@@ -0,0 +1,88 @@
|
||||
package MonorailHard.DrawningObjects;
|
||||
|
||||
import MonorailHard.Entities.EntityLocomotive;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class ExtentionDrawningMonorail {
|
||||
private static String getName(Color col){
|
||||
if(col.equals(Color.RED))
|
||||
return new String("RED");
|
||||
if(col.equals(Color.GREEN))
|
||||
return new String("GREEN");
|
||||
if(col.equals(Color.BLUE))
|
||||
return new String("BLUE");
|
||||
if(col.equals(Color.YELLOW))
|
||||
return new String("YELLOW");
|
||||
if(col.equals(Color.WHITE))
|
||||
return new String("WHITE");
|
||||
if(col.equals(Color.GRAY))
|
||||
return new String("GRAY");
|
||||
if(col.equals(Color.BLACK))
|
||||
return new String("BLACK");
|
||||
if(col.equals(Color.PINK))
|
||||
return new String("PINK");
|
||||
return null;
|
||||
}
|
||||
|
||||
private static Color getColor(String col){
|
||||
if(col.equals("RED"))
|
||||
return Color.RED;
|
||||
if(col.equals("GREEN"))
|
||||
return Color.GREEN;
|
||||
if(col.equals("BLUE"))
|
||||
return Color.BLUE;
|
||||
if(col.equals("YELLOW"))
|
||||
return Color.YELLOW;
|
||||
if(col.equals("WHITE"))
|
||||
return Color.WHITE;
|
||||
if(col.equals("GRAY"))
|
||||
return Color.GRAY;
|
||||
if(col.equals("BLACK"))
|
||||
return Color.BLACK;
|
||||
if(col.equals("PINK"))
|
||||
return Color.PINK;
|
||||
return null;
|
||||
}
|
||||
public static DrawningMonorail CreateDrawningMonorail(String info, char separatorForObject,
|
||||
int width, int height){
|
||||
String[] strs = info.split(Character.toString(separatorForObject));
|
||||
if(strs.length == 7){
|
||||
DrawningMonorail toRet = new DrawningMonorail(Integer.parseInt(strs[0]),
|
||||
Integer.parseInt(strs[1]), getColor(strs[2]),
|
||||
getColor(strs[3]), getColor(strs[4]), width, height);
|
||||
toRet.ChangeWheelsNumb(Integer.parseInt(strs[5]));
|
||||
toRet.ChangeIDraw(Integer.parseInt(strs[6]));
|
||||
return toRet;
|
||||
}
|
||||
if(strs.length == 10){
|
||||
DrawningMonorail toRet = new DrawningLocomotive(Integer.parseInt(strs[0]),
|
||||
Integer.parseInt(strs[1]), getColor(strs[2]),
|
||||
getColor(strs[3]), getColor(strs[4]),
|
||||
4, width, height, Boolean.parseBoolean(strs[7]),
|
||||
Boolean.parseBoolean(strs[8]), getColor(strs[9]));
|
||||
toRet.ChangeWheelsNumb(Integer.parseInt(strs[5]));
|
||||
toRet.ChangeIDraw(Integer.parseInt(strs[6]));
|
||||
return toRet;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
public static String GetDataForSave(DrawningMonorail drawningMonorail, char separatorForObject){
|
||||
var monorail = drawningMonorail.EntityMonorail;
|
||||
if(monorail == null)
|
||||
return null;
|
||||
String colName = getName(monorail.BodyColor);
|
||||
var str = String.format("%d%c%d%c%s%c%s%c%s%c%d%c%d", monorail.Speed(), separatorForObject, (int)monorail.Weight(),
|
||||
separatorForObject, getName(monorail.BodyColor), separatorForObject,
|
||||
getName(monorail.WheelColor), separatorForObject, getName(monorail.TireColor), separatorForObject,
|
||||
drawningMonorail._wheelNumb, separatorForObject, drawningMonorail.GetIDrawType());
|
||||
if(!(monorail instanceof EntityLocomotive)){
|
||||
return str;
|
||||
}
|
||||
var nstr = String.format("%s%c%b%c%b%c%s", str, separatorForObject,
|
||||
((EntityLocomotive) monorail).SecondCabine(), separatorForObject,
|
||||
((EntityLocomotive) monorail).MagniteRail(), separatorForObject,
|
||||
getName(((EntityLocomotive) monorail).AdditionalColor), separatorForObject);
|
||||
return nstr;
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,7 @@ package MonorailHard.Entities;
|
||||
import java.awt.*;
|
||||
|
||||
public class EntityLocomotive extends EntityMonorail{
|
||||
private Color AdditionalColor;
|
||||
public Color AdditionalColor;
|
||||
private boolean SecondCabine;
|
||||
private boolean MagniteRail;
|
||||
public Color AdditionalColor(){return AdditionalColor;};
|
||||
|
||||
@@ -4,7 +4,7 @@ import java.awt.*;
|
||||
public class EntityMonorail {
|
||||
private int Speed;
|
||||
private double Weight, Step;
|
||||
private Color BodyColor, WheelColor, TireColor;
|
||||
public Color BodyColor, WheelColor, TireColor;
|
||||
|
||||
public int Speed(){
|
||||
return Speed;
|
||||
|
||||
@@ -1,18 +1,44 @@
|
||||
package MonorailHard;
|
||||
|
||||
import MonorailHard.DrawningObjects.DrawningMonorail;
|
||||
import MonorailHard.DrawningObjects.ExtentionDrawningMonorail;
|
||||
import MonorailHard.Generics.MonorailGenericCollection;
|
||||
import MonorailHard.Generics.MonorailGenericStorage;
|
||||
import MonorailHard.Generics.MonorailTrashCollection;
|
||||
import MonorailHard.MovementStrategy.DrawningObjectMonorail;
|
||||
|
||||
import javax.swing.*;
|
||||
import javax.swing.filechooser.FileFilter;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.text.Normalizer;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Scanner;
|
||||
|
||||
class TxtSaveFilter extends FileFilter {
|
||||
@Override
|
||||
public boolean accept(File f) {
|
||||
if (f.isDirectory()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
String s = f.getName().toLowerCase();
|
||||
|
||||
return s.endsWith(".txt");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return "*.txt";
|
||||
}
|
||||
}
|
||||
|
||||
public class FormMonorailCollection {
|
||||
private final MonorailGenericStorage _storage;
|
||||
@@ -43,7 +69,20 @@ public class FormMonorailCollection {
|
||||
listBoxStorages.setSelectedIndex(index);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public FormMonorailCollection(){
|
||||
JMenuBar menuFile = new JMenuBar();
|
||||
JMenu file = new JMenu("Файл");
|
||||
menuFile.add(file);
|
||||
JMenuItem saveFile = new JMenuItem("Сохранить");
|
||||
JMenuItem loadFile = new JMenuItem("Загрузить");
|
||||
JMenuItem saveCollection = new JMenuItem("Сохранить коллекцию");
|
||||
JMenuItem loadCollection = new JMenuItem("Загрузить коллекцию");
|
||||
file.add(saveCollection);
|
||||
file.add(loadCollection);
|
||||
file.add(saveFile);
|
||||
file.add(loadFile);
|
||||
MonorailTrashCollection<DrawningMonorail> _trashCollection = new MonorailTrashCollection<>();
|
||||
JButton callTrashButton = new JButton("мусор");
|
||||
_storage = new MonorailGenericStorage(pictureBoxWidth, pictureBoxHeight);
|
||||
@@ -78,11 +117,93 @@ public class FormMonorailCollection {
|
||||
toolBox.add(refreshButton);
|
||||
toolBox.add(callTrashButton);
|
||||
collectionFrame.add(toolBox);
|
||||
collectionFrame.setJMenuBar(menuFile);
|
||||
collectionFrame.add(canv);
|
||||
collectionFrame.setVisible(true);
|
||||
canv._storage = _storage;
|
||||
canv.listBoxStorages = listBoxStorages;
|
||||
|
||||
saveFile.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
JFileChooser fc = new JFileChooser("C:\\Users\\frenk\\OneDrive\\Рабочий стол\\lab6saves");
|
||||
fc.addChoosableFileFilter(new TxtSaveFilter());
|
||||
int retrieval = fc.showSaveDialog(null);
|
||||
|
||||
if (retrieval == JFileChooser.APPROVE_OPTION) {
|
||||
File file = new File(fc.getSelectedFile() + "." + "txt");
|
||||
|
||||
try {
|
||||
_storage.SaveData(file);
|
||||
} catch (IOException ex) {
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
saveCollection.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
JFileChooser fc = new JFileChooser("C:\\Users\\frenk\\OneDrive\\Рабочий стол\\lab6saves");
|
||||
fc.addChoosableFileFilter(new TxtSaveFilter());
|
||||
int retrieval = fc.showSaveDialog(null);
|
||||
|
||||
if (retrieval == JFileChooser.APPROVE_OPTION) {
|
||||
File file = new File(fc.getSelectedFile() + "." + "txt");
|
||||
|
||||
try {
|
||||
if(listBoxStorages.getSelectedIndex() == -1) {
|
||||
return;
|
||||
}
|
||||
_storage._monorailStorages.get(listBoxStorages.getSelectedValue()).SaveData(file, listBoxStorages.getSelectedValue());
|
||||
ReloadObjects();
|
||||
} catch (IOException ex) {
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
loadFile.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
JFileChooser fc = new JFileChooser("C:\\Users\\frenk\\OneDrive\\Рабочий стол\\lab6saves");
|
||||
int ret = fc.showDialog(null, "Открыть файл");
|
||||
if(ret == JFileChooser.APPROVE_OPTION){
|
||||
File file = fc.getSelectedFile();
|
||||
try {
|
||||
_storage.LoadData(file);
|
||||
canv._storage =_storage;
|
||||
ReloadObjects();
|
||||
canv.repaint();
|
||||
} catch (IOException ex) {
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
loadCollection.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
JFileChooser fc = new JFileChooser("C:\\Users\\frenk\\OneDrive\\Рабочий стол\\lab6saves");
|
||||
int ret = fc.showDialog(null, "Открыть файл");
|
||||
if(ret == JFileChooser.APPROVE_OPTION){
|
||||
File file = fc.getSelectedFile();
|
||||
try {
|
||||
_storage.LoadCollection(file);
|
||||
ReloadObjects();
|
||||
canv.repaint();
|
||||
} catch (IOException ex) {
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
addStorageButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
@@ -99,7 +220,7 @@ public class FormMonorailCollection {
|
||||
if(listBoxStorages.getSelectedIndex() == -1) {
|
||||
return;
|
||||
}
|
||||
_storage.DelSet(listBoxStorages.getSelectedValue());
|
||||
_storage.DelSet(listBoxStorages.getSelectedValue(), _trashCollection);
|
||||
ReloadObjects();
|
||||
}
|
||||
});
|
||||
@@ -122,22 +243,29 @@ public class FormMonorailCollection {
|
||||
return;
|
||||
}
|
||||
MonorailGenericCollection<DrawningMonorail, DrawningObjectMonorail> _monorails = _storage.Get(listBoxStorages.getSelectedValue());
|
||||
FormMonorail form = new FormMonorail();
|
||||
form.buttonSelect.addActionListener(new ActionListener() {
|
||||
FormMonorailConfig form = new FormMonorailConfig();
|
||||
form.addButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if (_monorails.Insert(form.SelectedMonorail()))
|
||||
if (_monorails.Insert(form._monorail))
|
||||
{
|
||||
JOptionPane.showMessageDialog(null, "Объект добавлен", "Информация", JOptionPane.INFORMATION_MESSAGE);
|
||||
form.SelectedMonorail()._pictureWidth = pictureBoxWidth;
|
||||
form.SelectedMonorail()._pictureHeight = pictureBoxHeight;
|
||||
form._monorail._pictureWidth = pictureBoxWidth;
|
||||
form._monorail._pictureHeight = pictureBoxHeight;
|
||||
Draw();
|
||||
}
|
||||
else
|
||||
{
|
||||
JOptionPane.showMessageDialog(null, "Не удалось добавить объект", "Информация", JOptionPane.INFORMATION_MESSAGE);
|
||||
}
|
||||
form.MonorailFrame.dispose();
|
||||
form.frameConfig.dispose();
|
||||
Draw();
|
||||
}
|
||||
});
|
||||
form.cancelButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
form.frameConfig.dispose();
|
||||
Draw();
|
||||
}
|
||||
});
|
||||
@@ -206,4 +334,5 @@ class CollectionCanvas extends JComponent {
|
||||
g2d.drawImage(_storage.Get(listBoxStorages.getSelectedValue()).ShowMonorails(), 0, 0, this);
|
||||
super.repaint();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
471
src/MonorailHard/FormMonorailConfig.java
Normal file
471
src/MonorailHard/FormMonorailConfig.java
Normal file
@@ -0,0 +1,471 @@
|
||||
package MonorailHard;
|
||||
|
||||
import MonorailHard.DrawningObjects.*;
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.datatransfer.DataFlavor;
|
||||
import java.awt.datatransfer.StringSelection;
|
||||
import java.awt.datatransfer.Transferable;
|
||||
import java.awt.datatransfer.UnsupportedFlavorException;
|
||||
import java.util.*;
|
||||
import javax.swing.*;
|
||||
import javax.swing.border.Border;
|
||||
|
||||
import java.awt.event.*;
|
||||
import java.io.IOException;
|
||||
|
||||
import static java.lang.Integer.parseInt;
|
||||
|
||||
|
||||
public class FormMonorailConfig {
|
||||
private class LabelTransferHandler extends TransferHandler {
|
||||
@Override
|
||||
public int getSourceActions(JComponent c) {
|
||||
return TransferHandler.COPY;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Transferable createTransferable(JComponent c) {
|
||||
return new StringSelection(((JLabel)c).getText());
|
||||
}
|
||||
}
|
||||
|
||||
private class ColorTransferable implements Transferable {
|
||||
private Color color;
|
||||
private static final DataFlavor colorDataFlavor = new DataFlavor(Color.class, "Color");
|
||||
|
||||
public ColorTransferable(Color color) {
|
||||
this.color = color;
|
||||
}
|
||||
|
||||
@Override
|
||||
public DataFlavor[] getTransferDataFlavors() {
|
||||
return new DataFlavor[]{colorDataFlavor};
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isDataFlavorSupported(DataFlavor flavor) {
|
||||
return colorDataFlavor.equals(flavor);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
|
||||
if (isDataFlavorSupported(flavor)) {
|
||||
return color;
|
||||
} else {
|
||||
throw new UnsupportedFlavorException(flavor);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private class iDrawTransferable implements Transferable {
|
||||
private IDraw iDrawObject;
|
||||
private static final DataFlavor iDrawDataFlavor = new DataFlavor(IDraw.class, "IDraw");
|
||||
|
||||
|
||||
|
||||
public iDrawTransferable(IDraw iDrawObject) {
|
||||
this.iDrawObject = iDrawObject;
|
||||
}
|
||||
|
||||
@Override
|
||||
public DataFlavor[] getTransferDataFlavors() {
|
||||
return new DataFlavor[]{iDrawDataFlavor};
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isDataFlavorSupported(DataFlavor flavor) {
|
||||
return iDrawDataFlavor.equals(flavor);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
|
||||
if (isDataFlavorSupported(flavor)) {
|
||||
return iDrawObject;
|
||||
} else {
|
||||
throw new UnsupportedFlavorException(flavor);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private class PanelTransferHandler extends TransferHandler {
|
||||
@Override
|
||||
public int getSourceActions(JComponent c) {
|
||||
return TransferHandler.COPY;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Transferable createTransferable(JComponent c) {
|
||||
return new ColorTransferable(((JPanel)c).getBackground());
|
||||
}
|
||||
}
|
||||
|
||||
private class LabelMouseAdapter extends MouseAdapter{
|
||||
@Override
|
||||
public void mousePressed(MouseEvent e) {
|
||||
((JLabel)e.getComponent()).getTransferHandler().exportAsDrag(((JLabel)e.getComponent()), e, TransferHandler.COPY);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private class PanelMouseAdapter extends MouseAdapter{
|
||||
@Override
|
||||
public void mousePressed(MouseEvent e) {
|
||||
((JPanel)e.getComponent()).getTransferHandler().exportAsDrag(((JPanel)e.getComponent()), e, TransferHandler.COPY);
|
||||
}
|
||||
}
|
||||
|
||||
private class IDrawComponent extends JComponent{
|
||||
public IDraw obj;
|
||||
|
||||
public IDrawComponent(IDraw obj){
|
||||
this.obj = obj;
|
||||
this.addMouseListener(
|
||||
new MouseAdapter(){
|
||||
@Override
|
||||
public void mousePressed(MouseEvent e) {
|
||||
((IDrawComponent)e.getComponent()).getTransferHandler().exportAsDrag(((IDrawComponent)e.getComponent()), e, TransferHandler.COPY);
|
||||
}
|
||||
}
|
||||
);
|
||||
this.setTransferHandler(
|
||||
new TransferHandler(){
|
||||
@Override
|
||||
public int getSourceActions(JComponent c) {
|
||||
return TransferHandler.COPY;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Transferable createTransferable(JComponent c) {
|
||||
return new iDrawTransferable(((IDrawComponent)c).obj);
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public JFrame frameConfig;
|
||||
|
||||
public JButton addButton;
|
||||
public JButton cancelButton;
|
||||
public DrawningMonorail _monorail;
|
||||
int _pictureBoxWidth = 218;
|
||||
int _pictureBoxHeight = 190;
|
||||
|
||||
public FormMonorailConfig(){
|
||||
frameConfig = new JFrame();
|
||||
addButton = new JButton("Добавить");
|
||||
cancelButton = new JButton("Отмена");
|
||||
addButton.setBounds(555,262,94,29);
|
||||
cancelButton.setBounds(679,262,94,29);
|
||||
Canvas canv = new Canvas();
|
||||
canv.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
|
||||
canv.setBounds(555, 65, _pictureBoxWidth, _pictureBoxHeight);
|
||||
JLabel iDrawLabel = new JLabel("Орнамент");
|
||||
JLabel colorLabel = new JLabel("Цвет");
|
||||
DrawningMonorail toGetSize = new DrawningMonorail(0,0, null, null, null, 0, 0);
|
||||
IDrawComponent cartComp = new IDrawComponent(new DrawningWheelsCart(toGetSize._monorailWidth - toGetSize._monorailWidth / 10,
|
||||
toGetSize._monorailHeight,
|
||||
_pictureBoxWidth / 2 -toGetSize._monorailWidth / 2,
|
||||
_pictureBoxHeight / 2 -toGetSize._monorailHeight / 2, Color.GRAY, Color.BLACK));
|
||||
IDrawComponent ornComp = new IDrawComponent(new DrawningWheelsOrn(toGetSize._monorailWidth - toGetSize._monorailWidth / 10,
|
||||
toGetSize._monorailHeight,
|
||||
_pictureBoxWidth / 2 -toGetSize._monorailWidth / 2,
|
||||
_pictureBoxHeight / 2 -toGetSize._monorailHeight / 2, Color.GRAY, Color.BLACK));
|
||||
IDrawComponent baseComp = new IDrawComponent(new DrawningWheels(toGetSize._monorailWidth - toGetSize._monorailWidth / 10,
|
||||
toGetSize._monorailHeight,
|
||||
_pictureBoxWidth / 2 -toGetSize._monorailWidth / 2,
|
||||
_pictureBoxHeight / 2 -toGetSize._monorailHeight / 2, Color.GRAY, Color.BLACK));
|
||||
cartComp.setLayout(new GridLayout(1,1));
|
||||
ornComp.setLayout(new GridLayout(1,1));
|
||||
baseComp.setLayout(new GridLayout(1,1));
|
||||
iDrawLabel.setHorizontalAlignment(SwingConstants.CENTER);
|
||||
iDrawLabel.setVerticalAlignment(SwingConstants.CENTER);
|
||||
colorLabel.setHorizontalAlignment(SwingConstants.CENTER);
|
||||
colorLabel.setVerticalAlignment(SwingConstants.CENTER);
|
||||
JLabel cartLabel = new JLabel("На тележке");
|
||||
JLabel ornLabel = new JLabel("На колесах");
|
||||
JLabel baseLabel = new JLabel("Без");
|
||||
cartLabel.setHorizontalAlignment(SwingConstants.CENTER);
|
||||
cartLabel.setVerticalAlignment(SwingConstants.CENTER);
|
||||
ornLabel.setHorizontalAlignment(SwingConstants.CENTER);
|
||||
ornLabel.setVerticalAlignment(SwingConstants.CENTER);
|
||||
baseLabel.setHorizontalAlignment(SwingConstants.CENTER);
|
||||
baseLabel.setVerticalAlignment(SwingConstants.CENTER);
|
||||
|
||||
cartComp.add(cartLabel);
|
||||
ornComp.add(ornLabel);
|
||||
baseComp.add(baseLabel);
|
||||
cartComp.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
|
||||
ornComp.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
|
||||
baseComp.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
|
||||
JLabel addColorLabel = new JLabel("Доп цвет");
|
||||
addColorLabel.setHorizontalAlignment(SwingConstants.CENTER);
|
||||
addColorLabel.setVerticalAlignment(SwingConstants.CENTER);
|
||||
colorLabel.setBounds(555, 20, 70, 33);
|
||||
addColorLabel.setBounds(629, 20, 70, 33);
|
||||
iDrawLabel.setBounds(703, 20, 70, 33);
|
||||
colorLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
|
||||
addColorLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
|
||||
iDrawLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
|
||||
JCheckBox checkSecondCabine = new JCheckBox("Наличие 2 кабины");
|
||||
JCheckBox checkMagniteRail = new JCheckBox("Наличие магнитной рельсы");
|
||||
checkSecondCabine.setBounds(6, 132, 159, 24);
|
||||
checkMagniteRail.setBounds(6, 162, 145, 24);
|
||||
JLabel simpleLabel = new JLabel("Простой");
|
||||
JLabel advancedLabel = new JLabel("Продвинутый");
|
||||
simpleLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
|
||||
advancedLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
|
||||
simpleLabel.setBounds(171,169, 120, 50);
|
||||
simpleLabel.setHorizontalAlignment(SwingConstants.CENTER);
|
||||
simpleLabel.setVerticalAlignment(SwingConstants.CENTER);
|
||||
advancedLabel.setBounds(297,169, 120, 50);
|
||||
advancedLabel.setHorizontalAlignment(SwingConstants.CENTER);
|
||||
advancedLabel.setVerticalAlignment(SwingConstants.CENTER);
|
||||
cartComp.setBounds(171,229, 120, 50);
|
||||
ornComp.setBounds(297,229, 120, 50);
|
||||
baseComp.setBounds(423,229, 120, 50);
|
||||
JLabel speedLabel = new JLabel ("Скорость");
|
||||
JLabel weightLabel = new JLabel ("Вес");
|
||||
JPanel colorPanel = new JPanel();
|
||||
colorPanel.setBounds(171, 23, 234,143);
|
||||
SpinnerNumberModel speedSpinnerModel = new SpinnerNumberModel(100.0, 100.0, 1000.0, 1.0);
|
||||
SpinnerNumberModel weightSpinnerModel = new SpinnerNumberModel(100.0, 100.0, 1000.0, 1.0);
|
||||
SpinnerNumberModel wheelNumbSpinnerModel = new SpinnerNumberModel(2, 2, 4, 1.0);
|
||||
JSpinner wheelNumbSpinner = new JSpinner(wheelNumbSpinnerModel);
|
||||
JSpinner speedSpinner = new JSpinner(speedSpinnerModel);
|
||||
JSpinner weightSpinner = new JSpinner(weightSpinnerModel);
|
||||
speedSpinner.setBounds(6, 46, 150, 27);
|
||||
speedLabel.setBounds(6, 23, 73, 20);
|
||||
weightSpinner.setBounds(6, 99, 150, 27);
|
||||
weightLabel.setBounds(6, 76, 33, 20);
|
||||
wheelNumbSpinner.setBounds(6, 200, 150, 27);
|
||||
|
||||
JPanel redPanel = new JPanel();
|
||||
JPanel greenPanel = new JPanel();
|
||||
JPanel bluePanel = new JPanel();
|
||||
JPanel yellowPanel = new JPanel();
|
||||
JPanel whitePanel = new JPanel();
|
||||
JPanel grayPanel = new JPanel();
|
||||
JPanel blackPanel = new JPanel();
|
||||
JPanel purplePanel = new JPanel();
|
||||
|
||||
redPanel.setTransferHandler(new PanelTransferHandler());
|
||||
greenPanel.setTransferHandler(new PanelTransferHandler());
|
||||
bluePanel.setTransferHandler(new PanelTransferHandler());
|
||||
yellowPanel.setTransferHandler(new PanelTransferHandler());
|
||||
whitePanel.setTransferHandler(new PanelTransferHandler());
|
||||
grayPanel.setTransferHandler(new PanelTransferHandler());
|
||||
blackPanel.setTransferHandler(new PanelTransferHandler());
|
||||
purplePanel.setTransferHandler(new PanelTransferHandler());
|
||||
|
||||
redPanel.addMouseListener(new PanelMouseAdapter());
|
||||
greenPanel.addMouseListener(new PanelMouseAdapter());
|
||||
bluePanel.addMouseListener(new PanelMouseAdapter());
|
||||
yellowPanel.addMouseListener(new PanelMouseAdapter());
|
||||
whitePanel.addMouseListener(new PanelMouseAdapter());
|
||||
grayPanel.addMouseListener(new PanelMouseAdapter());
|
||||
blackPanel.addMouseListener(new PanelMouseAdapter());
|
||||
purplePanel.addMouseListener(new PanelMouseAdapter());
|
||||
|
||||
redPanel.setName("Красный");
|
||||
greenPanel.setName("Зелёный");
|
||||
bluePanel.setName("Синий");
|
||||
yellowPanel.setName("Жёлтый");
|
||||
whitePanel.setName("Белый");
|
||||
grayPanel.setName("Серый");
|
||||
blackPanel.setName("Чёрный");
|
||||
purplePanel.setName("Фиолетовый");
|
||||
|
||||
|
||||
simpleLabel.setTransferHandler(new LabelTransferHandler());
|
||||
simpleLabel.addMouseListener(new LabelMouseAdapter());
|
||||
|
||||
advancedLabel.setTransferHandler(new LabelTransferHandler());
|
||||
advancedLabel.addMouseListener(new LabelMouseAdapter());
|
||||
|
||||
|
||||
|
||||
redPanel.setBackground(Color.RED);
|
||||
greenPanel.setBackground(Color.GREEN);
|
||||
bluePanel.setBackground(Color.BLUE);
|
||||
yellowPanel.setBackground(Color.YELLOW);
|
||||
whitePanel.setBackground(Color.WHITE);
|
||||
grayPanel.setBackground(Color.GRAY);
|
||||
blackPanel.setBackground(Color.BLACK);
|
||||
purplePanel.setBackground(Color.PINK);
|
||||
|
||||
colorPanel.setLayout(new GridLayout(2, 4));
|
||||
|
||||
colorPanel.add(redPanel);
|
||||
colorPanel.add(greenPanel);
|
||||
colorPanel.add(bluePanel);
|
||||
colorPanel.add(yellowPanel);
|
||||
colorPanel.add(whitePanel);
|
||||
colorPanel.add(grayPanel);
|
||||
colorPanel.add(blackPanel);
|
||||
colorPanel.add(purplePanel);
|
||||
|
||||
|
||||
frameConfig.add(colorLabel);
|
||||
frameConfig.add(addColorLabel);
|
||||
frameConfig.add(iDrawLabel);
|
||||
frameConfig.setLayout(null);
|
||||
frameConfig.setSize(818, 350);
|
||||
frameConfig.add(speedLabel);
|
||||
frameConfig.add(speedSpinner);
|
||||
frameConfig.add(weightLabel);
|
||||
frameConfig.add(weightSpinner);
|
||||
frameConfig.add(simpleLabel);
|
||||
frameConfig.add(advancedLabel);
|
||||
frameConfig.add(checkSecondCabine);
|
||||
frameConfig.add(checkMagniteRail);
|
||||
frameConfig.add(canv);
|
||||
frameConfig.add(addButton);
|
||||
frameConfig.add(cancelButton);
|
||||
frameConfig.add(wheelNumbSpinner);
|
||||
|
||||
frameConfig.setVisible(true);
|
||||
frameConfig.add(colorPanel);
|
||||
frameConfig.add(cartComp);
|
||||
frameConfig.add(ornComp);
|
||||
frameConfig.add(baseComp);
|
||||
colorPanel.setVisible(true);
|
||||
cartComp.setVisible(true);
|
||||
ornComp.setVisible(true);
|
||||
baseComp.setVisible(true);
|
||||
|
||||
canv.setTransferHandler(
|
||||
new TransferHandler(){
|
||||
@Override
|
||||
public boolean canImport(TransferHandler.TransferSupport support) {
|
||||
return support.isDataFlavorSupported(DataFlavor.stringFlavor);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean importData(TransferHandler.TransferSupport support) {
|
||||
if (canImport(support)) {
|
||||
try {
|
||||
Object speedObj = speedSpinner.getValue();
|
||||
Number speedNumb = (Number) speedObj;
|
||||
int speed = speedNumb.intValue();
|
||||
|
||||
Object weightObj = weightSpinner.getValue();
|
||||
Number weightNumb = (Number) weightObj;
|
||||
int weight = weightNumb.intValue();
|
||||
|
||||
Object wheelNumbObj = wheelNumbSpinner.getValue();
|
||||
Number wheelNumbNumb = (Number) wheelNumbObj;
|
||||
int wheelNumb = wheelNumbNumb.intValue();
|
||||
|
||||
String data = (String) support.getTransferable().getTransferData(DataFlavor.stringFlavor);
|
||||
switch (data) {
|
||||
case "Простой":
|
||||
_monorail = new DrawningMonorail(speed, weight, Color.WHITE,
|
||||
Color.GRAY, Color.BLACK, _pictureBoxWidth, _pictureBoxHeight);
|
||||
_monorail.ChangeWheelsNumb(wheelNumb);
|
||||
break;
|
||||
case "Продвинутый":
|
||||
_monorail = new DrawningLocomotive(speed, weight, Color.WHITE, Color.GRAY, Color.BLACK,
|
||||
wheelNumb, _pictureBoxWidth, _pictureBoxHeight,
|
||||
checkSecondCabine.isSelected(), checkMagniteRail.isSelected(), Color.GRAY);
|
||||
_monorail.ChangeWheelsNumb(wheelNumb);
|
||||
break;
|
||||
}
|
||||
_monorail.SetPosition(_pictureBoxWidth / 2 -_monorail._monorailWidth / 2,
|
||||
_pictureBoxHeight / 2 -_monorail._monorailHeight / 2);
|
||||
|
||||
canv.DrawningMonorail = _monorail;
|
||||
canv.repaint();
|
||||
return true;
|
||||
} catch (UnsupportedFlavorException | IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
iDrawLabel.setTransferHandler(
|
||||
new TransferHandler(){
|
||||
@Override
|
||||
public boolean canImport(TransferHandler.TransferSupport support) {
|
||||
return support.isDataFlavorSupported(iDrawTransferable.iDrawDataFlavor);
|
||||
}
|
||||
@Override
|
||||
public boolean importData(TransferHandler.TransferSupport support) {
|
||||
if (canImport(support)) {
|
||||
try {
|
||||
IDraw obj = (IDraw) support.getTransferable().getTransferData(iDrawTransferable.iDrawDataFlavor);
|
||||
if (_monorail == null)
|
||||
return false;
|
||||
_monorail.ChangeIDraw(obj);
|
||||
canv.repaint();
|
||||
return true;
|
||||
} catch (UnsupportedFlavorException | IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
colorLabel.setTransferHandler(
|
||||
new TransferHandler(){
|
||||
@Override
|
||||
public boolean canImport(TransferHandler.TransferSupport support) {
|
||||
return support.isDataFlavorSupported(ColorTransferable.colorDataFlavor);
|
||||
}
|
||||
@Override
|
||||
public boolean importData(TransferHandler.TransferSupport support) {
|
||||
if (canImport(support)) {
|
||||
try {
|
||||
Color color = (Color) support.getTransferable().getTransferData(ColorTransferable.colorDataFlavor);
|
||||
if (_monorail == null)
|
||||
return false;
|
||||
_monorail.ChangeColor(color);
|
||||
canv.repaint();
|
||||
return true;
|
||||
} catch (UnsupportedFlavorException | IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
addColorLabel.setTransferHandler(
|
||||
new TransferHandler(){
|
||||
@Override
|
||||
public boolean canImport(TransferHandler.TransferSupport support) {
|
||||
return support.isDataFlavorSupported(ColorTransferable.colorDataFlavor);
|
||||
}
|
||||
@Override
|
||||
public boolean importData(TransferHandler.TransferSupport support) {
|
||||
if (canImport(support)) {
|
||||
try {
|
||||
Color color = (Color) support.getTransferable().getTransferData(ColorTransferable.colorDataFlavor);
|
||||
if (_monorail == null || !(_monorail instanceof DrawningLocomotive))
|
||||
return false;
|
||||
((DrawningLocomotive)_monorail).ChangeAddColor(color);
|
||||
canv.repaint();
|
||||
return true;
|
||||
} catch (UnsupportedFlavorException | IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,21 +1,58 @@
|
||||
package MonorailHard.Generics;
|
||||
|
||||
import MonorailHard.DrawningObjects.DrawningMonorail;
|
||||
import MonorailHard.DrawningObjects.ExtentionDrawningMonorail;
|
||||
import MonorailHard.MovementStrategy.DrawningObjectMonorail;
|
||||
import MonorailHard.MovementStrategy.IMoveableObject;
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Map;
|
||||
|
||||
public class MonorailGenericCollection<T extends DrawningMonorail, U extends IMoveableObject> {
|
||||
private final int _pictureWidth;
|
||||
|
||||
public static char _separatorRecords = ';';
|
||||
public static char _separatorForObject = ':';
|
||||
|
||||
private final int _pictureHeight;
|
||||
|
||||
private final int _placeSizeWidth = 133;
|
||||
|
||||
private final int _placeSizeHeight = 50;
|
||||
|
||||
private final SetGeneric<T> _collection;
|
||||
private SetGeneric<T> _collection;
|
||||
public ArrayList<T> GetMonorails(){
|
||||
return _collection.GetMonorails(_collection.Count);
|
||||
}
|
||||
|
||||
public boolean SaveData(File f, String name) throws IOException {
|
||||
if(f.exists()) {
|
||||
f.delete();
|
||||
}
|
||||
f.createNewFile();
|
||||
StringBuilder data = new StringBuilder();
|
||||
data.append("MonorailCollection\n");
|
||||
data.append(String.format("%s\n", name));
|
||||
StringBuilder records = new StringBuilder();
|
||||
for(DrawningMonorail elem : GetMonorails())
|
||||
{
|
||||
records.append(String.format("%s%c", ExtentionDrawningMonorail.GetDataForSave(elem, _separatorForObject),
|
||||
_separatorRecords));
|
||||
}
|
||||
data.append(records);
|
||||
if(data.length() == 0)
|
||||
return false;
|
||||
FileWriter writer = new FileWriter(f);
|
||||
writer.write(data.toString());
|
||||
writer.flush();
|
||||
writer.close();
|
||||
return true;
|
||||
}
|
||||
|
||||
public MonorailGenericCollection(int picWidth, int picHeight){
|
||||
int width = picWidth / _placeSizeWidth;
|
||||
@@ -89,4 +126,8 @@ public class MonorailGenericCollection<T extends DrawningMonorail, U extends IMo
|
||||
DrawObjects(gr);
|
||||
return bmp;
|
||||
}
|
||||
|
||||
public void Clear(){
|
||||
_collection = new SetGeneric<>(_pictureWidth * _pictureHeight);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,16 +1,119 @@
|
||||
package MonorailHard.Generics;
|
||||
|
||||
import MonorailHard.DrawningObjects.DrawningMonorail;
|
||||
import MonorailHard.DrawningObjects.ExtentionDrawningMonorail;
|
||||
import MonorailHard.MovementStrategy.DrawningObjectMonorail;
|
||||
|
||||
import java.util.Dictionary;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Queue;
|
||||
import javax.xml.crypto.dsig.keyinfo.KeyValue;
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class MonorailGenericStorage {
|
||||
final HashMap<String, MonorailGenericCollection<DrawningMonorail, DrawningObjectMonorail>> _monorailStorages;
|
||||
public final HashMap<String, MonorailGenericCollection<DrawningMonorail, DrawningObjectMonorail>> _monorailStorages;
|
||||
private static final char _separatorForKeyValue = '|';
|
||||
private final char _separatorRecords = ';';
|
||||
private static final char _separatorForObject = ':';
|
||||
|
||||
public boolean SaveData(File f) throws IOException {
|
||||
if(f.exists()) {
|
||||
f.delete();
|
||||
}
|
||||
f.createNewFile();
|
||||
StringBuilder data = new StringBuilder();
|
||||
data.append("MonorailStorage\n");
|
||||
for(Map.Entry<String, MonorailGenericCollection<DrawningMonorail, DrawningObjectMonorail>> record : _monorailStorages.entrySet()){
|
||||
StringBuilder records = new StringBuilder();
|
||||
for(DrawningMonorail elem : record.getValue().GetMonorails())
|
||||
{
|
||||
records.append(String.format("%s%c", ExtentionDrawningMonorail.GetDataForSave(elem, _separatorForObject),
|
||||
_separatorRecords));
|
||||
}
|
||||
data.append(String.format("%s%c%s\n", record.getKey(), _separatorForKeyValue, records.toString()));
|
||||
}
|
||||
if(data.length() == 0)
|
||||
return false;
|
||||
FileWriter writer = new FileWriter(f);
|
||||
writer.write(data.toString());
|
||||
writer.flush();
|
||||
writer.close();
|
||||
return true;
|
||||
}
|
||||
public boolean LoadData(File f) throws FileNotFoundException {
|
||||
if(!f.exists())
|
||||
return false;
|
||||
StringBuilder bufferTextFromFile =new StringBuilder();
|
||||
Scanner s = new Scanner(f);
|
||||
while(s.hasNext())
|
||||
bufferTextFromFile.append(s.next() + "\n");
|
||||
s.close();
|
||||
var strs = bufferTextFromFile.toString().split("\n");
|
||||
if(strs == null || strs.length == 0)
|
||||
return false;
|
||||
if (!strs[0].startsWith("MonorailStorage"))
|
||||
return false;
|
||||
_monorailStorages.clear();
|
||||
for(String data : strs){
|
||||
String st = new String("\\" + Character.toString( _separatorForKeyValue));
|
||||
String[]record = data.split(st);
|
||||
if (record.length != 2)
|
||||
continue;
|
||||
MonorailGenericCollection<DrawningMonorail, DrawningObjectMonorail> collection =
|
||||
new MonorailGenericCollection<>(_pictureWidth, _pictureHeight);
|
||||
String[] set = record[1].split(Character.toString(_separatorRecords));
|
||||
|
||||
for(int i = set.length -1; i >=0; i--){
|
||||
String elem = set[i];
|
||||
DrawningMonorail monorail = ExtentionDrawningMonorail.CreateDrawningMonorail(elem,
|
||||
_separatorForObject, _pictureWidth, _pictureHeight);
|
||||
if (monorail != null)
|
||||
{
|
||||
if (!(collection.Insert(monorail)))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
_monorailStorages.put(record[0], collection);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean LoadCollection(File f) throws FileNotFoundException {
|
||||
if(!f.exists())
|
||||
return false;
|
||||
StringBuilder bufferTextFromFile =new StringBuilder();
|
||||
Scanner s = new Scanner(f);
|
||||
while(s.hasNext())
|
||||
bufferTextFromFile.append(s.next() + "\n");
|
||||
s.close();
|
||||
var strs = bufferTextFromFile.toString().split("\n");
|
||||
if(strs == null || strs.length == 0)
|
||||
return false;
|
||||
if (!strs[0].startsWith("MonorailCollection"))
|
||||
return false;
|
||||
String collectionName = strs[1];
|
||||
MonorailGenericCollection<DrawningMonorail, DrawningObjectMonorail> collection = GetCollection(collectionName);
|
||||
if(collection == null)
|
||||
collection = new MonorailGenericCollection<>(_pictureWidth, _pictureHeight);
|
||||
else
|
||||
collection.Clear();
|
||||
String[] monorailsInfo = strs[2].split(Character.toString(MonorailGenericCollection._separatorRecords));
|
||||
for(int i = monorailsInfo.length-1; i >= 0; i--){
|
||||
String data = monorailsInfo[i];
|
||||
DrawningMonorail monorail = ExtentionDrawningMonorail.CreateDrawningMonorail(data,
|
||||
MonorailGenericCollection._separatorForObject, _pictureWidth, _pictureHeight);
|
||||
if (monorail != null)
|
||||
{
|
||||
if (!(collection.Insert(monorail)))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
AddSetFromFile(collectionName, collection);
|
||||
return true;
|
||||
}
|
||||
public List<String> Keys(){
|
||||
if(_monorailStorages == null)
|
||||
return null;
|
||||
@@ -31,9 +134,20 @@ public class MonorailGenericStorage {
|
||||
_monorailStorages.put(name, new MonorailGenericCollection<>(_pictureWidth, _pictureHeight));
|
||||
}
|
||||
|
||||
public void DelSet(String name){
|
||||
public void AddSetFromFile(String name, MonorailGenericCollection<DrawningMonorail, DrawningObjectMonorail> toAdd){
|
||||
if(_monorailStorages.containsKey(name)){
|
||||
_monorailStorages.remove(name);
|
||||
}
|
||||
_monorailStorages.put(name, toAdd);
|
||||
}
|
||||
|
||||
public void DelSet(String name, MonorailTrashCollection<DrawningMonorail> trashBox){
|
||||
if(!_monorailStorages.containsKey(name))
|
||||
return;
|
||||
MonorailGenericCollection<DrawningMonorail, DrawningObjectMonorail> cur = _monorailStorages.get(name);
|
||||
for(int i = 0; i < cur.Size(); i++)
|
||||
trashBox.Push(cur.Get(i));
|
||||
|
||||
_monorailStorages.remove(name);
|
||||
}
|
||||
|
||||
@@ -46,4 +160,8 @@ public class MonorailGenericStorage {
|
||||
public DrawningMonorail Get(String collectionName, int position){
|
||||
return _monorailStorages.get(collectionName).Get(position);
|
||||
}
|
||||
|
||||
public MonorailGenericCollection<DrawningMonorail, DrawningObjectMonorail> GetCollection(String collectionName){
|
||||
return _monorailStorages.get(collectionName);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user