93 lines
4.0 KiB
Java
93 lines
4.0 KiB
Java
package laba1Loco;
|
||
|
||
import java.awt.Color;
|
||
import java.lang.reflect.InvocationTargetException;
|
||
|
||
public class ExtentionDrawingTrain {
|
||
/// <summary>
|
||
/// Создание объекта из строки
|
||
/// </summary>
|
||
/// <param name="info">Строка с данными для создания объекта</param>
|
||
/// <param name="separatorForObject">Разделитель даннных</param>
|
||
/// <param name="width">Ширина</param>
|
||
/// <param name="height">Высота</param>
|
||
/// <returns>Объект</returns>
|
||
public static DrawingTrain CreateDrawingTrain(String info, String separatorForObject, int width, int height)
|
||
{
|
||
String[] strs = info.split(separatorForObject);
|
||
if (strs.length == 5)
|
||
{
|
||
String[] colorValues = strs[2].split(",");
|
||
|
||
DrawingTrain drawingTrain = new DrawingTrain(Integer.parseInt(strs[0]), (double)Integer.parseInt(strs[1]),
|
||
new Color(
|
||
Integer.parseInt(colorValues[0].replaceAll("\\D", "")),
|
||
Integer.parseInt(colorValues[1].replaceAll("\\D", "")),
|
||
Integer.parseInt(colorValues[2].replaceAll("\\D", ""))
|
||
),
|
||
Integer.parseInt(strs[3]),
|
||
width, height);
|
||
try{
|
||
drawingTrain.wheelDrawing = (IWheelDrawing)Class.forName(strs[4]).getDeclaredConstructor().newInstance();
|
||
drawingTrain.wheelDrawing.setNumWheel(drawingTrain.EntityTrain.numWheel);
|
||
}
|
||
catch(Exception e){
|
||
return null;
|
||
}
|
||
return drawingTrain;
|
||
}
|
||
if (strs.length == 9)
|
||
{
|
||
String[] colorValues = strs[2].split(",");
|
||
String[] colorValues2 = strs[5].split(",");
|
||
DrawingLoco drawingLoco = new DrawingLoco(
|
||
Integer.parseInt(strs[0]),
|
||
(double)Integer.parseInt(strs[1]),
|
||
new Color(
|
||
Integer.parseInt(colorValues[0].replaceAll("\\D", "")),
|
||
Integer.parseInt(colorValues[1].replaceAll("\\D", "")),
|
||
Integer.parseInt(colorValues[2].replaceAll("\\D", ""))
|
||
),
|
||
Integer.parseInt(strs[3]),
|
||
new Color(
|
||
Integer.parseInt(colorValues2[0].replaceAll("\\D", "")),
|
||
Integer.parseInt(colorValues2[1].replaceAll("\\D", "")),
|
||
Integer.parseInt(colorValues2[2].replaceAll("\\D", ""))
|
||
),
|
||
strs[6].equals("true"),
|
||
strs[7].equals("true"),
|
||
strs[8].equals("true"),
|
||
width, height);
|
||
try{
|
||
drawingLoco.wheelDrawing = (IWheelDrawing)Class.forName(strs[4]).getDeclaredConstructor().newInstance();
|
||
drawingLoco.wheelDrawing.setNumWheel(drawingLoco.EntityTrain.numWheel);
|
||
}
|
||
catch(Exception e){
|
||
return null;
|
||
}
|
||
return drawingLoco;
|
||
}
|
||
return null;
|
||
}
|
||
/// <summary>
|
||
/// Получение данных для сохранения в файл
|
||
/// </summary>
|
||
/// <param name="drawningtrain">Сохраняемый объект</param>
|
||
/// <param name="separatorForObject">Разделитель даннных</param>
|
||
/// <returns>Строка с данными по объекту</returns>
|
||
public static String GetDataForSave(DrawingTrain drawningTrain, String separatorForObject)
|
||
{
|
||
EntityTrain train = drawningTrain.EntityTrain;
|
||
if (train == null)
|
||
{
|
||
return null;
|
||
}
|
||
String str = "" + train.Speed + separatorForObject + (int)train.Weight + separatorForObject + train.BodyColor + separatorForObject + train.numWheel + separatorForObject + drawningTrain.wheelDrawing.getClass().getName();
|
||
if (!(train instanceof EntityLoco))
|
||
{
|
||
return str;
|
||
}
|
||
return str+separatorForObject+((EntityLoco)train).AdditionalColor.toString()+separatorForObject+((EntityLoco)train).Tube+separatorForObject+((EntityLoco)train).FuelTank+separatorForObject+((EntityLoco)train).LocoLine;
|
||
}
|
||
}
|