Compare commits
10 Commits
Author | SHA1 | Date | |
---|---|---|---|
11444f958e | |||
ea3315b4a9 | |||
226955f1fe | |||
53230875c8 | |||
5523780ec8 | |||
d37d252d53 | |||
426b17a7f8 | |||
eabca5415f | |||
c01f35128c | |||
185268c2d2 |
BIN
images/down.png
Normal file
BIN
images/down.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 967 B |
BIN
images/left.png
Normal file
BIN
images/left.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 970 B |
BIN
images/right.png
Normal file
BIN
images/right.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 989 B |
BIN
images/up.png
Normal file
BIN
images/up.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 957 B |
56
src/AbstractStrategy.java
Normal file
56
src/AbstractStrategy.java
Normal file
@ -0,0 +1,56 @@
|
||||
import java.util.function.Supplier;
|
||||
public abstract class AbstractStrategy {
|
||||
private IMoveableObject moveableObject;
|
||||
private Status state = Status.NOTINIT;
|
||||
private int FieldWidth;
|
||||
protected int getFieldWidth(){return FieldWidth;}
|
||||
private int FieldHeight;
|
||||
protected int getFieldHeight(){return FieldHeight;}
|
||||
public Status GetStatus() {return state;}
|
||||
|
||||
public void SetData(IMoveableObject moveableObject, int width, int height){
|
||||
if(moveableObject == null){
|
||||
state = Status.NOTINIT;
|
||||
return;
|
||||
}
|
||||
state = Status.INPROGRESS;
|
||||
this.moveableObject = moveableObject;
|
||||
FieldWidth = width;
|
||||
FieldHeight = height;
|
||||
}
|
||||
|
||||
public void MakeStep(){
|
||||
if(state != Status.INPROGRESS)
|
||||
return;
|
||||
if(IsTargetDestination()){
|
||||
state = Status.FINISH;
|
||||
return;
|
||||
}
|
||||
MoveToTarget();
|
||||
}
|
||||
|
||||
protected boolean MoveLeft() {return MoveTo(DirectionType.LEFT);}
|
||||
protected boolean MoveRight() {return MoveTo(DirectionType.RIGHT);}
|
||||
protected boolean MoveUp() {return MoveTo(DirectionType.UP);}
|
||||
protected boolean MoveDown() {return MoveTo(DirectionType.DOWN);}
|
||||
protected Supplier<ObjectParameters> getObjectParameters = () -> moveableObject.getObjectPosition();
|
||||
|
||||
protected Integer GetStep(){
|
||||
if(state != Status.INPROGRESS)
|
||||
return null;
|
||||
return moveableObject.getStep();
|
||||
}
|
||||
|
||||
protected abstract void MoveToTarget();
|
||||
protected abstract boolean IsTargetDestination();
|
||||
|
||||
private boolean MoveTo(DirectionType direction){
|
||||
if(state != Status.INPROGRESS)
|
||||
return false;
|
||||
if(moveableObject.checkCanMove(direction)){
|
||||
moveableObject.moveObject(direction);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
158
src/BomberGenericCollection.java
Normal file
158
src/BomberGenericCollection.java
Normal file
@ -0,0 +1,158 @@
|
||||
import java.awt.*;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
public class BomberGenericCollection<T extends DrawingAir, U extends IMoveableObject> {
|
||||
public static char _separatorRecords = ';';
|
||||
public static char _separatorForObject = ':';
|
||||
/// <summary>
|
||||
/// Ширина окна прорисовки
|
||||
/// </summary>
|
||||
private final int pictureWidth;
|
||||
/// <summary>
|
||||
/// Высота окна прорисовки
|
||||
/// </summary>
|
||||
private final int _pictureHeight;
|
||||
/// <summary>
|
||||
/// Размер занимаемого объектом места (ширина)
|
||||
/// </summary>
|
||||
private final int _placeSizeWidth = 170;
|
||||
/// <summary>
|
||||
/// Размер занимаемого объектом места (высота)
|
||||
/// </summary>
|
||||
private final int _placeSizeHeight = 180;
|
||||
/// <summary>
|
||||
/// Набор объектов
|
||||
/// </summary>
|
||||
private SetGeneric<T> collection;
|
||||
public ArrayList<T> GetPlanes(){
|
||||
return collection.getPlanes(collection.getCount());
|
||||
}
|
||||
|
||||
public boolean SaveData(File f, String name) throws IOException {
|
||||
if(f.exists()) {
|
||||
f.delete();
|
||||
}
|
||||
f.createNewFile();
|
||||
StringBuilder data = new StringBuilder();
|
||||
data.append("PlaneCollection\n");
|
||||
data.append(String.format("%s\n", name));
|
||||
StringBuilder records = new StringBuilder();
|
||||
for(DrawingAir elem : GetPlanes())
|
||||
{
|
||||
records.append(String.format("%s%c", ExtentionDrawingBomber.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 void Clear(){
|
||||
collection = new SetGeneric<>(pictureWidth * _pictureHeight);
|
||||
}
|
||||
/// <summary>
|
||||
/// Конструктор
|
||||
/// </summary>
|
||||
/// <param name="picWidth"></param>
|
||||
/// <param name="picHeight"></param>
|
||||
public BomberGenericCollection(int picWidth, int picHeight)
|
||||
{
|
||||
int width = picWidth / _placeSizeWidth;
|
||||
int height = picHeight / _placeSizeHeight;
|
||||
pictureWidth = picWidth;
|
||||
_pictureHeight = picHeight;
|
||||
collection = new SetGeneric<T>(width * height);
|
||||
}
|
||||
public int size(){
|
||||
return collection.getCount();
|
||||
}
|
||||
/// <summary>
|
||||
/// Перегрузка оператора сложения
|
||||
/// </summary>
|
||||
/// <param name="collect"></param>
|
||||
/// <param name="obj"></param>
|
||||
/// <returns></returns>
|
||||
public boolean Insert(T obj)
|
||||
{
|
||||
if (obj == null )
|
||||
return false;
|
||||
return collection.insert(obj);
|
||||
}
|
||||
/// <summary>
|
||||
/// Перегрузка оператора вычитания
|
||||
/// </summary>
|
||||
/// <param name="collect"></param>
|
||||
/// <param name="pos"></param>
|
||||
/// <returns></returns>
|
||||
public boolean Remove(int pos) {
|
||||
return collection.remove(pos);
|
||||
}
|
||||
/// <summary>
|
||||
/// Получение объекта IMoveableObject
|
||||
/// </summary>
|
||||
/// <param name="pos"></param>
|
||||
/// <returns></returns>
|
||||
public U GetU(int pos)
|
||||
{
|
||||
if(collection.Get(pos) == null)
|
||||
return null;
|
||||
return (U)collection.Get(pos).GetMoveableObject();
|
||||
}
|
||||
public T Get(int position){
|
||||
if(position < 0 || position >= collection.getCount())
|
||||
return null;
|
||||
return collection.Get(position);
|
||||
}
|
||||
/// <summary>
|
||||
/// Вывод всего набора объектов
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public void ShowPlanes(Graphics2D gr)
|
||||
{
|
||||
DrawBackground(gr);
|
||||
DrawObjects(gr);
|
||||
}
|
||||
/// <summary>
|
||||
/// Метод отрисовки фона
|
||||
/// </summary>
|
||||
/// <param name="g"></param>
|
||||
private void DrawBackground(Graphics2D g)
|
||||
{
|
||||
BasicStroke pen = new BasicStroke(3);
|
||||
g.setStroke(pen);
|
||||
for (int i = 0; i < pictureWidth / _placeSizeWidth; i++)
|
||||
{
|
||||
for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j)
|
||||
{//линия разметки места
|
||||
g.drawLine(i * _placeSizeWidth + 10, j * _placeSizeHeight +5,
|
||||
i * _placeSizeWidth + _placeSizeWidth / 2 + 50, j * _placeSizeHeight +5);
|
||||
}
|
||||
g.drawLine(i * _placeSizeWidth + 10, 5, i * _placeSizeWidth + 10,
|
||||
_pictureHeight / _placeSizeHeight * _placeSizeHeight +5);
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Метод прорисовки объектов
|
||||
/// </summary>
|
||||
/// <param name="g"></param>
|
||||
private void DrawObjects(Graphics2D g)
|
||||
{
|
||||
for (int i = 0; i < collection.getCount(); i++)
|
||||
{
|
||||
DrawingAir bomber = collection.Get(i);
|
||||
if (bomber != null)
|
||||
{
|
||||
int inRow = pictureWidth / _placeSizeWidth;
|
||||
bomber.setPosition(pictureWidth - _placeSizeWidth - (i % inRow * _placeSizeWidth)-8, i / inRow * _placeSizeHeight + 20);
|
||||
bomber.drawTransport(g);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
158
src/BomberGenericStorage.java
Normal file
158
src/BomberGenericStorage.java
Normal file
@ -0,0 +1,158 @@
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
import java.util.Scanner;
|
||||
public class BomberGenericStorage {
|
||||
public HashMap<String, BomberGenericCollection<DrawingAir, DrawingObjectPlane>> planeStorages;
|
||||
private final int _pictureWidth;
|
||||
private final int _pictureHeight;
|
||||
|
||||
private static final char _separatorForKeyValue = '|';
|
||||
private final char _separatorRecords = ';';
|
||||
private static final char _separatorForObject = ':';
|
||||
|
||||
public void SaveData(File f) throws IOException {
|
||||
if(f.exists()) {
|
||||
f.delete();
|
||||
}
|
||||
f.createNewFile();
|
||||
StringBuilder data = new StringBuilder();
|
||||
data.append("PlaneStorage\n");
|
||||
for(Map.Entry<String, BomberGenericCollection<DrawingAir, DrawingObjectPlane>> record : planeStorages.entrySet()){
|
||||
StringBuilder records = new StringBuilder();
|
||||
for(DrawingAir elem : record.getValue().GetPlanes())
|
||||
{
|
||||
records.append(String.format("%s%c", ExtentionDrawingBomber.GetDataForSave(elem, _separatorForObject),
|
||||
_separatorRecords));
|
||||
}
|
||||
data.append(String.format("%s%c%s\n", record.getKey(), _separatorForKeyValue, records.toString()));
|
||||
}
|
||||
if(data.length() == 0)
|
||||
return;
|
||||
FileWriter writer = new FileWriter(f);
|
||||
writer.write(data.toString());
|
||||
writer.flush();
|
||||
writer.close();
|
||||
}
|
||||
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("PlaneStorage"))
|
||||
return false;
|
||||
planeStorages.clear();
|
||||
for(String data : strs){
|
||||
String st = new String("\\" + Character.toString( _separatorForKeyValue));
|
||||
String[]record = data.split(st);
|
||||
if (record.length != 2)
|
||||
continue;
|
||||
BomberGenericCollection<DrawingAir, DrawingObjectPlane> collection =
|
||||
new BomberGenericCollection<>(_pictureWidth, _pictureHeight);
|
||||
String[] set = record[1].split(Character.toString(_separatorRecords));
|
||||
|
||||
for(int i = set.length -1; i >=0; i--){
|
||||
String elem = set[i];
|
||||
DrawingAir plane = ExtentionDrawingBomber.CreateDrawingPlane(elem,
|
||||
_separatorForObject, _pictureWidth, _pictureHeight);
|
||||
if (plane != null)
|
||||
{
|
||||
if (!(collection.Insert(plane)))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
planeStorages.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("PlaneCollection"))
|
||||
return false;
|
||||
String collectionName = strs[1];
|
||||
BomberGenericCollection<DrawingAir, DrawingObjectPlane> collection = getCollection(collectionName);
|
||||
if(collection == null)
|
||||
collection = new BomberGenericCollection<>(_pictureWidth, _pictureHeight);
|
||||
else
|
||||
collection.Clear();
|
||||
String[] planesInfo = strs[2].split(Character.toString(BomberGenericCollection._separatorRecords));
|
||||
for(int i = planesInfo.length-1; i >= 0; i--){
|
||||
String data = planesInfo[i];
|
||||
DrawingAir plane = ExtentionDrawingBomber.CreateDrawingPlane(data,
|
||||
BomberGenericCollection._separatorForObject, _pictureWidth, _pictureHeight);
|
||||
if (plane != null)
|
||||
{
|
||||
if (!(collection.Insert(plane)))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
AddSetFromFile(collectionName, collection);
|
||||
return true;
|
||||
}
|
||||
public BomberGenericStorage(int pictureWidth, int pictureHeight){
|
||||
planeStorages = new HashMap<>();
|
||||
_pictureWidth = pictureWidth;
|
||||
_pictureHeight = pictureHeight;
|
||||
}
|
||||
public ArrayList<String> Keys(){
|
||||
return new ArrayList<>(planeStorages.keySet());
|
||||
}
|
||||
public void AddSet(String name){
|
||||
if(planeStorages.containsKey(name))
|
||||
return;
|
||||
planeStorages.put(name, new BomberGenericCollection<>(_pictureWidth, _pictureHeight));
|
||||
}
|
||||
|
||||
public void AddSetFromFile(String name, BomberGenericCollection<DrawingAir, DrawingObjectPlane> toAdd){
|
||||
if(planeStorages.containsKey(name)){
|
||||
planeStorages.remove(name);
|
||||
}
|
||||
planeStorages.put(name, toAdd);
|
||||
}
|
||||
|
||||
public void DelSet(String name, BomberTrashCollection<DrawingAir> trashBox){
|
||||
if(!planeStorages.containsKey(name))
|
||||
return;
|
||||
BomberGenericCollection<DrawingAir, DrawingObjectPlane> cur = planeStorages.get(name);
|
||||
for(int i = 0; i < cur.size(); i++)
|
||||
trashBox.Push(cur.Get(i));
|
||||
planeStorages.remove(name);
|
||||
}
|
||||
|
||||
public BomberGenericCollection<DrawingAir, DrawingObjectPlane> GetCollection(String collectionName){
|
||||
return planeStorages.get(collectionName);
|
||||
}
|
||||
public BomberGenericCollection<DrawingAir, DrawingObjectPlane> getCollection(String name){
|
||||
if(!planeStorages.containsKey(name))
|
||||
return null;
|
||||
return planeStorages.get(name);
|
||||
}
|
||||
|
||||
public DrawingAir getPlanes(String collectionName, int position){
|
||||
return planeStorages.get(collectionName).Get(position);
|
||||
}
|
||||
}
|
16
src/BomberTrashCollection.java
Normal file
16
src/BomberTrashCollection.java
Normal file
@ -0,0 +1,16 @@
|
||||
import java.util.LinkedList;
|
||||
public class BomberTrashCollection<T extends DrawingAir> {
|
||||
LinkedList<T> linkedList;
|
||||
public BomberTrashCollection(){
|
||||
linkedList = new LinkedList<>();
|
||||
}
|
||||
public void Push(T plane){
|
||||
linkedList.push(plane);
|
||||
}
|
||||
public T Pop(){
|
||||
return linkedList.pop();
|
||||
}
|
||||
public int GetSize(){
|
||||
return linkedList.size();
|
||||
}
|
||||
}
|
6
src/DirectionType.java
Normal file
6
src/DirectionType.java
Normal file
@ -0,0 +1,6 @@
|
||||
public enum DirectionType {
|
||||
UP,
|
||||
DOWN,
|
||||
LEFT,
|
||||
RIGHT
|
||||
}
|
146
src/DrawingAir.java
Normal file
146
src/DrawingAir.java
Normal file
@ -0,0 +1,146 @@
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawingAir {
|
||||
protected EntityAir entityAir;
|
||||
protected void setEntityAir(EntityAir entityAir){this.entityAir = entityAir;}
|
||||
public EntityAir getEntityAir() {
|
||||
return entityAir;
|
||||
}
|
||||
public IDrawEngines drawingEngines;
|
||||
public int _pictureWidth;
|
||||
public int _pictureHeight;
|
||||
protected int _startPosX;
|
||||
public int getPosX(){return _startPosX;}
|
||||
protected int _startPosY;
|
||||
public int getPosY(){return _startPosY;}
|
||||
private final int _PlaneWidth = 160;
|
||||
public int getWidth(){return _PlaneWidth;}
|
||||
private final int _PlaneHeight = 160;
|
||||
public int getHeight(){return _PlaneHeight;}
|
||||
public DrawingAir(int speed, double weight, Color bodyColor, int width, int height, int enginesType, int enginesNumber) {
|
||||
if (width < _PlaneWidth || height < _PlaneHeight)
|
||||
return;
|
||||
_pictureWidth = width;
|
||||
_pictureHeight = height;
|
||||
entityAir = new EntityAir(speed, weight, bodyColor);
|
||||
switch (enginesType){
|
||||
case 1:
|
||||
drawingEngines = new DrawingEnginesOval();
|
||||
break;
|
||||
case 2:
|
||||
drawingEngines = new DrawingEnginesRound();
|
||||
break;
|
||||
default:
|
||||
drawingEngines = new DrawingEnginesSquare();
|
||||
break;
|
||||
}
|
||||
drawingEngines.setNumber(enginesNumber);
|
||||
}
|
||||
|
||||
public IMoveableObject GetMoveableObject() {return new DrawingObjectPlane(this);}
|
||||
|
||||
public void setPosition(int x, int y) {
|
||||
if (x < 0 || y < 0 || x + _PlaneWidth > _pictureWidth || y + _PlaneHeight > _pictureHeight)
|
||||
x = y = 0;
|
||||
_startPosX = x;
|
||||
_startPosY = y;
|
||||
}
|
||||
public void drawTransport(Graphics2D g) {
|
||||
if (entityAir == null)
|
||||
return;
|
||||
BasicStroke pen = new BasicStroke(2);
|
||||
Color penColor = Color.BLACK;
|
||||
Color bodyColor = entityAir.getBodyColor();
|
||||
g.setStroke(pen);
|
||||
g.setColor(bodyColor);
|
||||
//фюзеляж
|
||||
g.fillRect(_startPosX + 20, _startPosY + 70, 140, 20);
|
||||
//кабина
|
||||
int[] pointX = new int[]{_startPosX, _startPosX + 20, _startPosX + 20};
|
||||
int[] pointY = new int[]{_startPosY + 80, _startPosY + 70, _startPosY + 90};
|
||||
g.setColor(Color.BLACK);
|
||||
g.fillPolygon(pointX, pointY, 3);
|
||||
//границы самолета
|
||||
g.setColor(penColor);
|
||||
g.drawPolygon(pointX, pointY, 3);
|
||||
g.drawRect(_startPosX + 20, _startPosY + 70, 140, 20);
|
||||
//Крылья
|
||||
pointX = new int[]{_startPosX + 70, _startPosX + 70, _startPosX + 90, _startPosX + 100};
|
||||
pointY = new int[]{_startPosY + 70, _startPosY, _startPosY, _startPosY + 70};
|
||||
g.setColor(bodyColor);
|
||||
g.fillPolygon(pointX, pointY, 4);
|
||||
g.setColor(penColor);
|
||||
g.drawPolygon(pointX, pointY, 4);
|
||||
pointX = new int[]{_startPosX + 70, _startPosX + 70, _startPosX + 90, _startPosX + 100};
|
||||
pointY = new int[]{_startPosY + 90, _startPosY + 160, _startPosY + 160, _startPosY + 90};
|
||||
g.setColor(bodyColor);
|
||||
g.fillPolygon(pointX, pointY, 4);
|
||||
g.setColor(penColor);
|
||||
g.drawPolygon(pointX, pointY, 4);
|
||||
pointX = new int[]{_startPosX + 130, _startPosX + 130, _startPosX + 160, _startPosX + 160};
|
||||
pointY = new int[]{_startPosY + 70, _startPosY + 50, _startPosY + 30, _startPosY + 70};
|
||||
g.setColor(bodyColor);
|
||||
g.fillPolygon(pointX, pointY, 4);
|
||||
g.setColor(penColor);
|
||||
g.drawPolygon(pointX, pointY, 4);
|
||||
pointX = new int[]{_startPosX + 130, _startPosX + 130, _startPosX + 160, _startPosX + 160};
|
||||
pointY = new int[]{_startPosY + 90, _startPosY + 110, _startPosY + 130, _startPosY + 90};
|
||||
g.setColor(bodyColor);
|
||||
g.fillPolygon(pointX, pointY, 4);
|
||||
g.setColor(penColor);
|
||||
g.drawPolygon(pointX, pointY, 4);
|
||||
drawingEngines.drawEngines(g, _startPosX, _startPosY);
|
||||
}
|
||||
public boolean canMove(DirectionType direction) {
|
||||
if (entityAir == null) {
|
||||
return false;
|
||||
}
|
||||
switch(direction) {
|
||||
case LEFT:
|
||||
return _startPosX - entityAir.step.get().intValue() > 0;
|
||||
case UP:
|
||||
return _startPosY - entityAir.step.get().intValue() > 0;
|
||||
case RIGHT:
|
||||
return _startPosX + entityAir.step.get().intValue() + _PlaneWidth < _pictureWidth;
|
||||
case DOWN:
|
||||
return _startPosY + entityAir.step.get().intValue() + _PlaneWidth < _pictureHeight;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
public void moveTransport(DirectionType direction)
|
||||
{
|
||||
if (!canMove(direction) || entityAir == null)
|
||||
return;
|
||||
switch (direction)
|
||||
{
|
||||
//влево
|
||||
case LEFT:
|
||||
_startPosX -= entityAir.step.get().intValue();
|
||||
break;
|
||||
//вверх
|
||||
case UP:
|
||||
_startPosY -= entityAir.step.get().intValue();
|
||||
break;
|
||||
// вправо
|
||||
case RIGHT:
|
||||
_startPosX += entityAir.step.get().intValue();
|
||||
break;
|
||||
//вниз
|
||||
case DOWN:
|
||||
_startPosY += entityAir.step.get().intValue();
|
||||
break;
|
||||
}
|
||||
}
|
||||
public void ChangeColor(Color col){
|
||||
entityAir.bodyColor = col;
|
||||
}
|
||||
|
||||
public void ChangeIDraw(IDrawEngines obj){
|
||||
drawingEngines = obj;
|
||||
}
|
||||
|
||||
public void ChangeEnginesCol(int col){
|
||||
drawingEngines.setNumber(col);
|
||||
}
|
||||
}
|
57
src/DrawingAirBomber.java
Normal file
57
src/DrawingAirBomber.java
Normal file
@ -0,0 +1,57 @@
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawingAirBomber extends DrawingAir {
|
||||
|
||||
public DrawingAirBomber(int speed, double weight, Color bodyColor,
|
||||
Color additionalColor, boolean rocket, boolean toplivo, int width, int height, int enginesType, int enginesNumber){
|
||||
super(speed, weight, bodyColor, width, height, enginesType, enginesNumber);
|
||||
if(entityAir != null)
|
||||
entityAir = new EntityAirBomber(speed, weight, bodyColor, additionalColor,rocket, toplivo);
|
||||
}
|
||||
public void ChangeAddColor(Color col){
|
||||
((EntityAirBomber)entityAir).additionalColor = col;
|
||||
}
|
||||
public void drawTransport(Graphics2D g)
|
||||
{
|
||||
if (!(entityAir instanceof EntityAirBomber))
|
||||
return;
|
||||
EntityAirBomber entityAirBomber = (EntityAirBomber) entityAir;
|
||||
BasicStroke pen = new BasicStroke(2);
|
||||
Color penColor = Color.BLACK;
|
||||
Color additionalColor = entityAirBomber.getAdditionalColor();
|
||||
g.setStroke(pen);
|
||||
super.drawTransport(g);
|
||||
// топливо
|
||||
if (entityAirBomber.getFuel())
|
||||
{
|
||||
int[] pointX = new int[]{_startPosX + 60, _startPosX + 60, _startPosX + 100, _startPosX + 100};
|
||||
int[] pointY = new int[]{_startPosY + 10, _startPosY, _startPosY, _startPosY + 10};
|
||||
g.setColor(penColor);
|
||||
g.fillPolygon(pointX, pointY, 4);
|
||||
pointX = new int[]{_startPosX + 60, _startPosX + 60, _startPosX + 100, _startPosX + 100};
|
||||
pointY = new int[]{_startPosY + 160, _startPosY + 150, _startPosY + 150, _startPosY + 160};
|
||||
g.setColor(penColor);
|
||||
g.fillPolygon(pointX, pointY, 4);
|
||||
}
|
||||
//ракеты
|
||||
if (entityAirBomber.getBombs())
|
||||
{
|
||||
int[] pointX = new int[]{_startPosX+70, _startPosX+55, _startPosX+70, _startPosX+70};
|
||||
int[] pointY = new int[]{_startPosY+30, _startPosY+20, _startPosY+10, _startPosY+30};
|
||||
g.setColor(additionalColor);
|
||||
g.fillPolygon(pointX, pointY, 4);
|
||||
pointX = new int[]{_startPosX+70, _startPosX+55, _startPosX+70, _startPosX+70};
|
||||
pointY = new int[]{_startPosY+60, _startPosY+50, _startPosY+40, _startPosY+60};
|
||||
g.setColor(additionalColor);
|
||||
g.fillPolygon(pointX, pointY, 4);
|
||||
pointX = new int[]{_startPosX+70, _startPosX+55, _startPosX+70, _startPosX+70};
|
||||
pointY = new int[]{_startPosY+120, _startPosY+110, _startPosY+100, _startPosY+120};
|
||||
g.setColor(additionalColor);
|
||||
g.fillPolygon(pointX, pointY, 4);
|
||||
pointX = new int[]{_startPosX+70, _startPosX+55, _startPosX+70, _startPosX+70};
|
||||
pointY = new int[]{_startPosY+150, _startPosY+140, _startPosY+130, _startPosY+150};
|
||||
g.setColor(additionalColor);
|
||||
g.fillPolygon(pointX, pointY, 4);
|
||||
}
|
||||
}
|
||||
}
|
41
src/DrawingEnginesOval.java
Normal file
41
src/DrawingEnginesOval.java
Normal file
@ -0,0 +1,41 @@
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawingEnginesOval implements IDrawEngines{
|
||||
private EngineNumber number;
|
||||
|
||||
@Override
|
||||
public int getType() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
public int getNumber(){
|
||||
int x = 0;
|
||||
if(number == EngineNumber.TWO)
|
||||
x = 2;
|
||||
if(number == EngineNumber.FOUR)
|
||||
x = 4;
|
||||
if(number == EngineNumber.SIX)
|
||||
x = 6;
|
||||
return x;
|
||||
}
|
||||
public void setNumber(int x){
|
||||
if(x <= 2)
|
||||
number = EngineNumber.TWO;
|
||||
if(x == 4)
|
||||
number = EngineNumber.FOUR;
|
||||
if(x >= 6)
|
||||
number = EngineNumber.SIX;
|
||||
}
|
||||
public void drawEngines(Graphics2D graphics2D, int _startX, int _startY){
|
||||
graphics2D.fillRect(_startX+75, _startY+20, 15, 15);
|
||||
graphics2D.fillRect(_startX+75, _startY+125, 15, 15);
|
||||
if (number == EngineNumber.FOUR || number == EngineNumber.SIX){
|
||||
graphics2D.fillRect(_startX+75, _startY+40, 15, 15);
|
||||
graphics2D.fillRect(_startX+75, _startY+105, 15, 15);
|
||||
}
|
||||
if (number == EngineNumber.SIX){
|
||||
graphics2D.fillRect(_startX+140, _startY+50, 15, 15);
|
||||
graphics2D.fillRect(_startX+140, _startY+95, 15, 15);
|
||||
}
|
||||
}
|
||||
}
|
41
src/DrawingEnginesRound.java
Normal file
41
src/DrawingEnginesRound.java
Normal file
@ -0,0 +1,41 @@
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawingEnginesRound implements IDrawEngines {
|
||||
private EngineNumber number;
|
||||
|
||||
@Override
|
||||
public int getType() {
|
||||
return 2;
|
||||
}
|
||||
|
||||
public int getNumber(){
|
||||
int x = 0;
|
||||
if(number == EngineNumber.TWO)
|
||||
x = 2;
|
||||
if(number == EngineNumber.FOUR)
|
||||
x = 4;
|
||||
if(number == EngineNumber.SIX)
|
||||
x = 6;
|
||||
return x;
|
||||
}
|
||||
public void setNumber(int x){
|
||||
if(x <= 2)
|
||||
number = EngineNumber.TWO;
|
||||
if(x == 4)
|
||||
number = EngineNumber.FOUR;
|
||||
if(x >= 6)
|
||||
number = EngineNumber.SIX;
|
||||
}
|
||||
public void drawEngines(Graphics2D graphics2D, int _startX, int _startY){
|
||||
graphics2D.fillOval(_startX+70, _startY+20, 20, 15);
|
||||
graphics2D.fillOval(_startX+70, _startY+125, 20, 15);
|
||||
if (number == EngineNumber.FOUR || number == EngineNumber.SIX){
|
||||
graphics2D.fillOval(_startX+70, _startY+40, 20, 15);
|
||||
graphics2D.fillOval(_startX+70, _startY+105, 20, 15);
|
||||
}
|
||||
if (number == EngineNumber.SIX){
|
||||
graphics2D.fillOval(_startX+135, _startY+50, 20, 15);
|
||||
graphics2D.fillOval(_startX+135, _startY+95, 20, 15);
|
||||
}
|
||||
}
|
||||
}
|
53
src/DrawingEnginesSquare.java
Normal file
53
src/DrawingEnginesSquare.java
Normal file
@ -0,0 +1,53 @@
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawingEnginesSquare implements IDrawEngines {
|
||||
private EngineNumber number;
|
||||
|
||||
@Override
|
||||
public int getType() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public int getNumber(){
|
||||
int x = 0;
|
||||
if(number == EngineNumber.TWO)
|
||||
x = 2;
|
||||
if(number == EngineNumber.FOUR)
|
||||
x = 4;
|
||||
if(number == EngineNumber.SIX)
|
||||
x = 6;
|
||||
return x;
|
||||
}
|
||||
public void setNumber(int x){
|
||||
if(x <= 2)
|
||||
number = EngineNumber.TWO;
|
||||
if(x == 4)
|
||||
number = EngineNumber.FOUR;
|
||||
if(x >= 6)
|
||||
number = EngineNumber.SIX;
|
||||
}
|
||||
public void drawEngines(Graphics2D graphics2D, int _startX, int _startY){
|
||||
graphics2D.fillOval(_startX+85, _startY+20, 20, 15);
|
||||
graphics2D.fillRect(_startX+75, _startY+20, 20, 15);
|
||||
graphics2D.fillOval(_startX+65, _startY+20, 20, 15);
|
||||
graphics2D.fillOval(_startX+85, _startY+125, 20, 15);
|
||||
graphics2D.fillRect(_startX+75, _startY+125, 20, 15);
|
||||
graphics2D.fillOval(_startX+65, _startY+125, 20, 15);
|
||||
if (number == EngineNumber.FOUR || number == EngineNumber.SIX){
|
||||
graphics2D.fillOval(_startX+85, _startY+40, 20, 15);
|
||||
graphics2D.fillRect(_startX+75, _startY+40, 20, 15);
|
||||
graphics2D.fillOval(_startX+65, _startY+40, 20, 15);
|
||||
graphics2D.fillOval(_startX+85, _startY+105, 20, 15);
|
||||
graphics2D.fillRect(_startX+75, _startY+105, 20, 15);
|
||||
graphics2D.fillOval(_startX+65, _startY+105, 20, 15);
|
||||
}
|
||||
if (number == EngineNumber.SIX){
|
||||
graphics2D.fillOval(_startX+145, _startY+50, 20, 15);
|
||||
graphics2D.fillRect(_startX+135, _startY+50, 25, 15);
|
||||
graphics2D.fillOval(_startX+125, _startY+50, 20, 15);
|
||||
graphics2D.fillOval(_startX+145, _startY+95, 20, 15);
|
||||
graphics2D.fillRect(_startX+135, _startY+95, 25, 15);
|
||||
graphics2D.fillOval(_startX+125, _startY+95, 20, 15);
|
||||
}
|
||||
}
|
||||
}
|
27
src/DrawingObjectAirBomber.java
Normal file
27
src/DrawingObjectAirBomber.java
Normal file
@ -0,0 +1,27 @@
|
||||
public class DrawingObjectAirBomber implements IMoveableObject{
|
||||
private final DrawingAir drawingAir;
|
||||
|
||||
public DrawingObjectAirBomber(DrawingAir drawingPlane){
|
||||
this.drawingAir = drawingPlane;
|
||||
}
|
||||
public ObjectParameters getObjectPosition(){
|
||||
if(drawingAir == null || drawingAir.getEntityAir() == null)
|
||||
return null;
|
||||
return new ObjectParameters(drawingAir.getPosX(), drawingAir.getPosY(),
|
||||
drawingAir.getWidth(), drawingAir.getHeight());
|
||||
}
|
||||
public int getStep(){
|
||||
if(drawingAir.getEntityAir() == null)
|
||||
return 0;
|
||||
return drawingAir.getEntityAir().step.get().intValue();
|
||||
}
|
||||
public boolean checkCanMove(DirectionType direction){
|
||||
if(drawingAir == null)
|
||||
return false;
|
||||
return drawingAir.canMove(direction);
|
||||
}
|
||||
public void moveObject(DirectionType direction){
|
||||
drawingAir.moveTransport(direction);
|
||||
}
|
||||
}
|
||||
|
25
src/DrawingObjectPlane.java
Normal file
25
src/DrawingObjectPlane.java
Normal file
@ -0,0 +1,25 @@
|
||||
public class DrawingObjectPlane implements IMoveableObject{
|
||||
private final DrawingAir drawingPlane;
|
||||
public DrawingObjectPlane(DrawingAir drawingPlane){
|
||||
this.drawingPlane = drawingPlane;
|
||||
}
|
||||
public ObjectParameters getObjectPosition(){
|
||||
if(drawingPlane == null || drawingPlane.getEntityAir() == null)
|
||||
return null;
|
||||
return new ObjectParameters(drawingPlane.getPosX(), drawingPlane.getPosY(),
|
||||
drawingPlane.getWidth(), drawingPlane.getHeight());
|
||||
}
|
||||
public int getStep(){
|
||||
if(drawingPlane.getEntityAir() == null)
|
||||
return 0;
|
||||
return drawingPlane.getEntityAir().step.get().intValue();
|
||||
}
|
||||
public boolean checkCanMove(DirectionType direction){
|
||||
if(drawingPlane == null)
|
||||
return false;
|
||||
return drawingPlane.canMove(direction);
|
||||
}
|
||||
public void moveObject(DirectionType direction){
|
||||
drawingPlane.moveTransport(direction);
|
||||
}
|
||||
}
|
5
src/EngineNumber.java
Normal file
5
src/EngineNumber.java
Normal file
@ -0,0 +1,5 @@
|
||||
public enum EngineNumber {
|
||||
TWO,
|
||||
FOUR,
|
||||
SIX
|
||||
}
|
22
src/EntityAir.java
Normal file
22
src/EntityAir.java
Normal file
@ -0,0 +1,22 @@
|
||||
import java.awt.*;
|
||||
import java.util.function.Supplier;
|
||||
public class EntityAir {
|
||||
private int speed;
|
||||
public int getSpeed() {
|
||||
return speed;
|
||||
}
|
||||
private double weight;
|
||||
public double getWeight() {
|
||||
return weight;
|
||||
}
|
||||
public Color bodyColor;
|
||||
public Color getBodyColor() {
|
||||
return bodyColor;
|
||||
}
|
||||
public Supplier<Double> step = () -> (double) speed * 100 / weight;
|
||||
public EntityAir(int speed, double weight, Color bodyColor){
|
||||
this.speed = speed;
|
||||
this.weight = weight;
|
||||
this.bodyColor = bodyColor;
|
||||
}
|
||||
}
|
23
src/EntityAirBomber.java
Normal file
23
src/EntityAirBomber.java
Normal file
@ -0,0 +1,23 @@
|
||||
import java.awt.*;
|
||||
|
||||
public class EntityAirBomber extends EntityAir {
|
||||
public Color additionalColor;
|
||||
public Color getAdditionalColor(){
|
||||
return additionalColor;
|
||||
}
|
||||
private boolean isFuel;
|
||||
public boolean getFuel() {
|
||||
return isFuel;
|
||||
}
|
||||
private boolean isBombs;
|
||||
public boolean getBombs() {
|
||||
return isBombs;
|
||||
}
|
||||
public EntityAirBomber(int speed, double weight, Color bodyColor, Color
|
||||
additionalColor, boolean isBombs, boolean isFuel) {
|
||||
super(speed, weight, bodyColor);
|
||||
this.additionalColor = additionalColor;
|
||||
this.isFuel = isFuel;
|
||||
this.isBombs = isBombs;
|
||||
}
|
||||
}
|
75
src/ExtentionDrawingBomber.java
Normal file
75
src/ExtentionDrawingBomber.java
Normal file
@ -0,0 +1,75 @@
|
||||
import java.awt.*;
|
||||
|
||||
public class ExtentionDrawingBomber {
|
||||
private static String getName(Color col){
|
||||
if(col.equals(Color.RED))
|
||||
return "RED";
|
||||
if(col.equals(Color.GREEN))
|
||||
return "GREEN";
|
||||
if(col.equals(Color.BLUE))
|
||||
return "BLUE";
|
||||
if(col.equals(Color.YELLOW))
|
||||
return "YELLOW";
|
||||
if(col.equals(Color.WHITE))
|
||||
return "WHITE";
|
||||
if(col.equals(Color.GRAY))
|
||||
return "GRAY";
|
||||
if(col.equals(Color.BLACK))
|
||||
return "BLACK";
|
||||
if(col.equals(Color.MAGENTA))
|
||||
return "MAGENTA";
|
||||
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("MAGENTA"))
|
||||
return Color.MAGENTA;
|
||||
return null;
|
||||
}
|
||||
public static DrawingAir CreateDrawingPlane(String info, char separatorForObject,
|
||||
int width, int height){
|
||||
String[] strs = info.split(Character.toString(separatorForObject));
|
||||
if(strs.length == 5){
|
||||
return new DrawingAir(Integer.parseInt(strs[0]),
|
||||
Integer.parseInt(strs[1]), getColor(strs[2]), width, height, Integer.parseInt(strs[3]), Integer.parseInt(strs[4]));
|
||||
}
|
||||
if(strs.length == 8){
|
||||
return new DrawingAirBomber(Integer.parseInt(strs[0]),
|
||||
Integer.parseInt(strs[1]), getColor(strs[2]),
|
||||
getColor(strs[5]), Boolean.parseBoolean(strs[6]),
|
||||
Boolean.parseBoolean(strs[7]), width, height,
|
||||
Integer.parseInt(strs[3]), Integer.parseInt(strs[4]));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
public static String GetDataForSave(DrawingAir drawingPlane, char separatorForObject){
|
||||
var plane = drawingPlane;
|
||||
var entity = plane.entityAir;
|
||||
if(entity == null)
|
||||
return null;
|
||||
var str = String.format("%d%c%d%c%s%c%d%c%d", entity.getSpeed(), separatorForObject, (int)entity.getWeight(),
|
||||
separatorForObject, getName(entity.bodyColor), separatorForObject, plane.drawingEngines.getType(), separatorForObject, plane.drawingEngines.getNumber());
|
||||
if(!(entity instanceof EntityAirBomber)){
|
||||
return str;
|
||||
}
|
||||
var nstr = String.format("%s%c%s%c%b%c%b", str, separatorForObject,
|
||||
getName(((EntityAirBomber) entity).additionalColor), separatorForObject,
|
||||
((EntityAirBomber) entity).getBombs(), separatorForObject,
|
||||
((EntityAirBomber) entity).getFuel());
|
||||
return nstr;
|
||||
}
|
||||
}
|
203
src/FrameAirBomber.java
Normal file
203
src/FrameAirBomber.java
Normal file
@ -0,0 +1,203 @@
|
||||
import javax.imageio.ImageIO;
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Random;
|
||||
public class FrameAirBomber extends JFrame {
|
||||
private DrawingAir drawingPlane;
|
||||
private AbstractStrategy abstractStrategy;
|
||||
public JButton selectPlaneButton;
|
||||
|
||||
private DrawingAir selectedPlane;
|
||||
public DrawingAir getSelectedPlane() {
|
||||
return selectedPlane;
|
||||
}
|
||||
private JComboBox comboBoxStrategy;
|
||||
private final JComponent pictureBox;
|
||||
public FrameAirBomber() throws IOException {
|
||||
super("Бомбардировщик");
|
||||
setSize(new Dimension(900,500));
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
//components initialisation
|
||||
pictureBox = new JComponent(){
|
||||
public void paintComponent(Graphics graphics){
|
||||
super.paintComponent(graphics);
|
||||
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
|
||||
Graphics2D graphics2D = (Graphics2D) graphics;
|
||||
if (drawingPlane != null) drawingPlane.drawTransport(graphics2D);
|
||||
super.repaint();
|
||||
}
|
||||
};
|
||||
comboBoxStrategy = new JComboBox<>(new String[]{"К центру", "К границе"});
|
||||
JButton stepButton = new JButton("Шаг");
|
||||
JButton createPlaneButton = new JButton("Создать самолет");
|
||||
JButton createAirBomberButton = new JButton("Создать бомбардировщик");
|
||||
JButton rightButton = new JButton(new ImageIcon(ImageIO.read(new File("images/right.png"))));
|
||||
JButton leftButton = new JButton(new ImageIcon(ImageIO.read(new File("images/left.png"))));
|
||||
JButton upButton = new JButton(new ImageIcon(ImageIO.read(new File("images/up.png"))));
|
||||
JButton downButton = new JButton(new ImageIcon(ImageIO.read(new File("images/down.png"))));
|
||||
selectPlaneButton = new JButton("Выбрать самолет");
|
||||
pictureBox.setBounds( 0, 0, getContentPane().getWidth(), getContentPane().getHeight());
|
||||
//ActionListeners and ActionCommand addition
|
||||
createPlaneButton.addActionListener(e -> buttonCreatePlaneClick());
|
||||
createAirBomberButton.addActionListener(e -> buttonCreateAirBomberClick());
|
||||
stepButton.addActionListener(e -> buttonStepClick());
|
||||
rightButton.setActionCommand("right");
|
||||
rightButton.addActionListener(this::buttonMoveClick);
|
||||
leftButton.setActionCommand("left");
|
||||
leftButton.addActionListener(this::buttonMoveClick);
|
||||
upButton.setActionCommand("up");
|
||||
upButton.addActionListener(this::buttonMoveClick);
|
||||
downButton.setActionCommand("down");
|
||||
downButton.addActionListener(this::buttonMoveClick);
|
||||
//component addition
|
||||
setLayout(new BorderLayout());
|
||||
JPanel panelAirBomber = new JPanel(new BorderLayout());
|
||||
JPanel rightPanel = new JPanel(new BorderLayout());
|
||||
JPanel leftPanel = new JPanel(new BorderLayout());
|
||||
GridBagConstraints constraints = new GridBagConstraints();
|
||||
constraints.insets.left = constraints.insets.top = constraints.insets.bottom = constraints.insets.right = 2;
|
||||
//createPanel
|
||||
JPanel createPanel = new JPanel(new GridBagLayout());
|
||||
constraints.gridx = 0;
|
||||
constraints.gridy = 0;
|
||||
createPanel.add(createPlaneButton, constraints);
|
||||
constraints.gridx = 1;
|
||||
constraints.gridy = 0;
|
||||
createPanel.add(createAirBomberButton, constraints);
|
||||
constraints.gridx = 2;
|
||||
constraints.gridy = 0;
|
||||
createPanel.add(selectPlaneButton, constraints);
|
||||
//movementPanel
|
||||
JPanel movementPanel = new JPanel(new GridBagLayout());
|
||||
rightButton.setPreferredSize(new Dimension(30,30));
|
||||
constraints.gridx = 2;
|
||||
constraints.gridy = 1;
|
||||
movementPanel.add(rightButton, constraints);
|
||||
leftButton.setPreferredSize(new Dimension(30,30));
|
||||
constraints.gridx = 0;
|
||||
constraints.gridy = 1;
|
||||
movementPanel.add(leftButton, constraints);
|
||||
upButton.setPreferredSize(new Dimension(30,30));
|
||||
constraints.gridx = 1;
|
||||
constraints.gridy = 0;
|
||||
movementPanel.add(upButton, constraints);
|
||||
downButton.setPreferredSize(new Dimension(30,30));
|
||||
constraints.gridx = 1;
|
||||
constraints.gridy = 1;
|
||||
movementPanel.add(downButton, constraints);
|
||||
//stepPanel
|
||||
JPanel stepPanel = new JPanel(new GridBagLayout());
|
||||
constraints.gridx = 0;
|
||||
constraints.gridy = 0;
|
||||
stepPanel.add(comboBoxStrategy, constraints);
|
||||
constraints.gridx = 0;
|
||||
constraints.gridy = 1;
|
||||
stepPanel.add(stepButton, constraints);
|
||||
//addition to frame
|
||||
add(pictureBox);
|
||||
rightPanel.add(movementPanel, BorderLayout.SOUTH);
|
||||
rightPanel.add(stepPanel, BorderLayout.NORTH);
|
||||
leftPanel.add(createPanel, BorderLayout.SOUTH);
|
||||
panelAirBomber.add(rightPanel, BorderLayout.EAST);
|
||||
panelAirBomber.add(leftPanel, BorderLayout.WEST);
|
||||
add(panelAirBomber,BorderLayout.CENTER);
|
||||
setVisible(true);
|
||||
}
|
||||
private void buttonCreateAirBomberClick() {
|
||||
Random random = new Random();
|
||||
pictureBox.setBounds(0,0,getContentPane().getWidth(),getContentPane().getHeight());
|
||||
Color bodyColor = JColorChooser.showDialog(null, "Choose a color", Color.RED);
|
||||
Color additionalColor = JColorChooser.showDialog(null, "Choose a color", Color.RED);
|
||||
drawingPlane = new DrawingAirBomber(random.nextInt(200) + 100, random.nextInt(2000) + 1000, bodyColor,
|
||||
additionalColor, random.nextBoolean(), random.nextBoolean(), pictureBox.getWidth(), pictureBox.getHeight(), random.nextInt(3),(random.nextInt(3)+1)*2);
|
||||
drawingPlane.setPosition(random.nextInt(90) + 10, random.nextInt(90) + 10);
|
||||
draw();
|
||||
}
|
||||
private void buttonCreatePlaneClick(){
|
||||
Random random = new Random();
|
||||
pictureBox.setBounds(0,0,getContentPane().getWidth(),getContentPane().getHeight());
|
||||
Color bodyColor = JColorChooser.showDialog(null, "Choose a color", Color.RED);
|
||||
drawingPlane = new DrawingAir(random.nextInt(200) + 100, random.nextInt(2000) + 1000, bodyColor,
|
||||
pictureBox.getWidth(), pictureBox.getHeight(), random.nextInt(3),(random.nextInt(3)+1)*2);
|
||||
drawingPlane.setPosition(random.nextInt(90) + 10, random.nextInt(90) + 10);
|
||||
draw();
|
||||
}
|
||||
private void buttonStepClick(){
|
||||
if (drawingPlane == null) {
|
||||
return;
|
||||
}
|
||||
if (comboBoxStrategy.isEnabled()) {
|
||||
;
|
||||
switch(comboBoxStrategy.getSelectedIndex()) {
|
||||
case 0:
|
||||
abstractStrategy = new MoveToCenter();
|
||||
break;
|
||||
case 1:
|
||||
abstractStrategy = new MoveToBorder();
|
||||
break;
|
||||
default:
|
||||
abstractStrategy = null;
|
||||
break;
|
||||
};
|
||||
if (abstractStrategy == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
abstractStrategy.SetData(new DrawingObjectPlane(drawingPlane), pictureBox.getWidth(),
|
||||
pictureBox.getHeight());
|
||||
comboBoxStrategy.setEnabled(false);
|
||||
}
|
||||
if (abstractStrategy == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
abstractStrategy.MakeStep();
|
||||
draw();
|
||||
if (abstractStrategy.GetStatus() == Status.FINISH)
|
||||
{
|
||||
comboBoxStrategy.setEnabled(true);
|
||||
abstractStrategy = null;
|
||||
}
|
||||
}
|
||||
private void buttonMoveClick(ActionEvent event) {
|
||||
if(drawingPlane == null || drawingPlane.getEntityAir() == null)
|
||||
return;
|
||||
switch (event.getActionCommand())
|
||||
{
|
||||
case "left":
|
||||
drawingPlane.moveTransport(DirectionType.LEFT);
|
||||
break;
|
||||
case "right":
|
||||
drawingPlane.moveTransport(DirectionType.RIGHT);
|
||||
break;
|
||||
case "up":
|
||||
drawingPlane.moveTransport(DirectionType.UP);
|
||||
break;
|
||||
case "down":
|
||||
drawingPlane.moveTransport(DirectionType.DOWN);
|
||||
break;
|
||||
}
|
||||
draw();
|
||||
}
|
||||
private void draw() {
|
||||
if (drawingPlane == null)
|
||||
return;
|
||||
pictureBox.repaint();
|
||||
}
|
||||
public void select(){
|
||||
if (drawingPlane == null) {
|
||||
return;
|
||||
}
|
||||
selectedPlane = drawingPlane;
|
||||
}
|
||||
|
||||
public void ChangePlane(DrawingAir newPlane){
|
||||
drawingPlane = newPlane;
|
||||
drawingPlane.setPosition(0,0);
|
||||
pictureBox.setBounds(0,0,getContentPane().getWidth(),getContentPane().getHeight());
|
||||
draw();
|
||||
}
|
||||
}
|
387
src/FrameBomberConfig.java
Normal file
387
src/FrameBomberConfig.java
Normal file
@ -0,0 +1,387 @@
|
||||
import javax.swing.*;
|
||||
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.awt.event.MouseAdapter;
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.io.IOException;
|
||||
|
||||
public class FrameBomberConfig extends JFrame{
|
||||
private static 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 record ColorTransferable(Color color) implements Transferable {
|
||||
private static final DataFlavor colorDataFlavor = new DataFlavor(Color.class, "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 {
|
||||
if (isDataFlavorSupported(flavor)) {
|
||||
return color;
|
||||
} else {
|
||||
throw new UnsupportedFlavorException(flavor);
|
||||
}
|
||||
}
|
||||
}
|
||||
private record IDrawEnginesTransferable(IDrawEngines IDrawEnginesObject) implements Transferable {
|
||||
private static final DataFlavor IDrawEnginesDataFlavor = new DataFlavor(IDrawEngines.class, "IDrawEngines");
|
||||
@Override
|
||||
public DataFlavor[] getTransferDataFlavors() {
|
||||
return new DataFlavor[]{IDrawEnginesDataFlavor};
|
||||
}
|
||||
@Override
|
||||
public boolean isDataFlavorSupported(DataFlavor flavor) {
|
||||
return IDrawEnginesDataFlavor.equals(flavor);
|
||||
}
|
||||
@Override
|
||||
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException {
|
||||
if (isDataFlavorSupported(flavor)) {
|
||||
return IDrawEnginesObject;
|
||||
} else {
|
||||
throw new UnsupportedFlavorException(flavor);
|
||||
}
|
||||
}
|
||||
}
|
||||
private static class PanelTransferHandler extends TransferHandler {
|
||||
@Override
|
||||
public int getSourceActions(JComponent c) {
|
||||
return TransferHandler.COPY;
|
||||
}
|
||||
@Override
|
||||
protected Transferable createTransferable(JComponent c) {
|
||||
return new ColorTransferable(c.getBackground());
|
||||
}
|
||||
}
|
||||
private static class LabelMouseAdapter extends MouseAdapter{
|
||||
@Override
|
||||
public void mousePressed(MouseEvent e) {
|
||||
((JLabel)e.getComponent()).getTransferHandler().exportAsDrag(((JLabel)e.getComponent()), e, TransferHandler.COPY);
|
||||
}
|
||||
}
|
||||
private static class PanelMouseAdapter extends MouseAdapter{
|
||||
@Override
|
||||
public void mousePressed(MouseEvent e) {
|
||||
((JPanel)e.getComponent()).getTransferHandler().exportAsDrag(((JPanel)e.getComponent()), e, TransferHandler.COPY);
|
||||
}
|
||||
}
|
||||
private static class IDrawEnginesComponent extends JComponent{
|
||||
public IDrawEngines obj;
|
||||
public IDrawEnginesComponent(IDrawEngines obj){
|
||||
this.obj = obj;
|
||||
this.addMouseListener(
|
||||
new MouseAdapter(){
|
||||
@Override
|
||||
public void mousePressed(MouseEvent e) {
|
||||
((IDrawEnginesComponent)e.getComponent()).getTransferHandler().exportAsDrag(((IDrawEnginesComponent)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 IDrawEnginesTransferable(((IDrawEnginesComponent) c).obj);
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
private final JComponent pictureBox = new JComponent(){
|
||||
public void paintComponent(Graphics graphics){
|
||||
super.paintComponent(graphics);
|
||||
Graphics2D graphics2D = (Graphics2D) graphics;
|
||||
if (drawingAir != null) drawingAir.drawTransport(graphics2D);
|
||||
super.repaint();
|
||||
}
|
||||
};
|
||||
public final JButton addButton = new JButton("Добавить");
|
||||
public final JButton cancelButton = new JButton("Отмена");
|
||||
public DrawingAir drawingAir;
|
||||
private final int pictureBoxWidth = 218;
|
||||
private final int pictureBoxHeight = 178;
|
||||
public FrameBomberConfig(){
|
||||
super("Создание объекта");
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
createGui();
|
||||
setVisible(true);
|
||||
}
|
||||
private void createGui(){
|
||||
pictureBox.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
|
||||
IDrawEnginesComponent engines = new IDrawEnginesComponent(new DrawingEnginesOval());
|
||||
IDrawEnginesComponent enginesCircle = new IDrawEnginesComponent(new DrawingEnginesRound());
|
||||
IDrawEnginesComponent enginesOval = new IDrawEnginesComponent(new DrawingEnginesSquare());
|
||||
JLabel squareLabel = new JLabel("Квадратные");
|
||||
squareLabel.setHorizontalAlignment(SwingConstants.CENTER);
|
||||
squareLabel.setVerticalAlignment(SwingConstants.CENTER);
|
||||
JLabel roundedBackLabel = new JLabel("Круглые");
|
||||
roundedBackLabel.setHorizontalAlignment(SwingConstants.CENTER);
|
||||
roundedBackLabel.setVerticalAlignment(SwingConstants.CENTER);
|
||||
JLabel roundedFrontLabel = new JLabel("Овальные");
|
||||
roundedFrontLabel.setHorizontalAlignment(SwingConstants.CENTER);
|
||||
roundedFrontLabel.setVerticalAlignment(SwingConstants.CENTER);
|
||||
engines.setLayout(new GridLayout(1,1));
|
||||
enginesCircle.setLayout(new GridLayout(1,1));
|
||||
enginesOval.setLayout(new GridLayout(1,1));
|
||||
engines.add(squareLabel);
|
||||
enginesCircle.add(roundedBackLabel);
|
||||
enginesOval.add(roundedFrontLabel);
|
||||
engines.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
|
||||
enginesCircle.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
|
||||
enginesOval.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
|
||||
JLabel colorLabel = new JLabel("Цвет");
|
||||
colorLabel.setHorizontalAlignment(SwingConstants.CENTER);
|
||||
colorLabel.setVerticalAlignment(SwingConstants.CENTER);
|
||||
colorLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
|
||||
JLabel additionalColorLabel = new JLabel("Доп цвет");
|
||||
additionalColorLabel.setHorizontalAlignment(SwingConstants.CENTER);
|
||||
additionalColorLabel.setVerticalAlignment(SwingConstants.CENTER);
|
||||
additionalColorLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
|
||||
JLabel IDrawEnginesLabel = new JLabel("Двигатели");
|
||||
IDrawEnginesLabel.setHorizontalAlignment(SwingConstants.CENTER);
|
||||
IDrawEnginesLabel.setVerticalAlignment(SwingConstants.CENTER);
|
||||
IDrawEnginesLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
|
||||
JCheckBox checkBoxBomb = new JCheckBox("Наличие бомб");
|
||||
JCheckBox checkBoxFuel = new JCheckBox("Наличие баков");
|
||||
JLabel simpleLabel = new JLabel("Простой");
|
||||
simpleLabel.setTransferHandler(new LabelTransferHandler());
|
||||
simpleLabel.addMouseListener(new LabelMouseAdapter());
|
||||
simpleLabel.setHorizontalAlignment(SwingConstants.CENTER);
|
||||
simpleLabel.setVerticalAlignment(SwingConstants.CENTER);
|
||||
simpleLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
|
||||
JLabel advancedLabel = new JLabel("Продвинутый");
|
||||
advancedLabel.setTransferHandler(new LabelTransferHandler());
|
||||
advancedLabel.addMouseListener(new LabelMouseAdapter());
|
||||
advancedLabel.setHorizontalAlignment(SwingConstants.CENTER);
|
||||
advancedLabel.setVerticalAlignment(SwingConstants.CENTER);
|
||||
advancedLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
|
||||
JLabel speedLabel = new JLabel ("Скорость");
|
||||
JLabel weightLabel = new JLabel ("Вес");
|
||||
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 enginesNumberSpinnerModel = new SpinnerNumberModel(2, 2, 6, 2.0);
|
||||
JSpinner speedSpinner = new JSpinner(speedSpinnerModel);
|
||||
JSpinner weightSpinner = new JSpinner(weightSpinnerModel);
|
||||
JSpinner enginesNumberSpinner = new JSpinner(enginesNumberSpinnerModel);
|
||||
JPanel colorPanel = new JPanel();
|
||||
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("Фиолетовый");
|
||||
pictureBox.setTransferHandler(
|
||||
new TransferHandler(){
|
||||
@Override
|
||||
public boolean canImport(TransferSupport support) {
|
||||
return support.isDataFlavorSupported(DataFlavor.stringFlavor);
|
||||
}
|
||||
@Override
|
||||
public boolean importData(TransferSupport support) {
|
||||
if (canImport(support)) {
|
||||
try {
|
||||
int speed = ((Number)speedSpinner.getValue()).intValue();
|
||||
int weight = ((Number)weightSpinner.getValue()).intValue();
|
||||
int enginesNumber = ((Number)enginesNumberSpinner.getValue()).intValue();
|
||||
switch ((String)support.getTransferable().getTransferData(DataFlavor.stringFlavor)) {
|
||||
case "Простой" -> {
|
||||
drawingAir = new DrawingAir(speed, weight, Color.WHITE,
|
||||
pictureBoxWidth, pictureBoxHeight, 0, enginesNumber);
|
||||
drawingAir.ChangeEnginesCol(enginesNumber);
|
||||
}
|
||||
case "Продвинутый" -> {
|
||||
drawingAir = new DrawingAirBomber(speed, weight, Color.WHITE, Color.BLACK,
|
||||
checkBoxBomb.isSelected(), checkBoxFuel.isSelected(),
|
||||
pictureBoxWidth, pictureBoxHeight, 0, enginesNumber);
|
||||
drawingAir.ChangeEnginesCol(enginesNumber);
|
||||
}
|
||||
}
|
||||
drawingAir.setPosition(pictureBoxWidth / 2 - drawingAir.getWidth() / 2,
|
||||
pictureBoxHeight / 2 - drawingAir.getHeight() / 2);
|
||||
pictureBox.repaint();
|
||||
return true;
|
||||
} catch (UnsupportedFlavorException | IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
);
|
||||
IDrawEnginesLabel.setTransferHandler(
|
||||
new TransferHandler(){
|
||||
@Override
|
||||
public boolean canImport(TransferSupport support) {
|
||||
return support.isDataFlavorSupported(IDrawEnginesTransferable.IDrawEnginesDataFlavor);
|
||||
}
|
||||
@Override
|
||||
public boolean importData(TransferSupport support) {
|
||||
if (canImport(support)) {
|
||||
try {
|
||||
IDrawEngines obj = (IDrawEngines) support.getTransferable().getTransferData(IDrawEnginesTransferable.IDrawEnginesDataFlavor);
|
||||
obj.setNumber(((Number)enginesNumberSpinner.getValue()).intValue());
|
||||
if (drawingAir == null)
|
||||
return false;
|
||||
drawingAir.ChangeIDraw(obj);
|
||||
pictureBox.repaint();
|
||||
return true;
|
||||
} catch (UnsupportedFlavorException | IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
);
|
||||
colorLabel.setTransferHandler(
|
||||
new TransferHandler(){
|
||||
@Override
|
||||
public boolean canImport(TransferSupport support) {
|
||||
return support.isDataFlavorSupported(ColorTransferable.colorDataFlavor);
|
||||
}
|
||||
@Override
|
||||
public boolean importData(TransferSupport support) {
|
||||
if (canImport(support)) {
|
||||
try {
|
||||
Color color = (Color) support.getTransferable().getTransferData(ColorTransferable.colorDataFlavor);
|
||||
if (drawingAir == null)
|
||||
return false;
|
||||
drawingAir.ChangeColor(color);
|
||||
pictureBox.repaint();
|
||||
return true;
|
||||
} catch (UnsupportedFlavorException | IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
);
|
||||
additionalColorLabel.setTransferHandler(
|
||||
new TransferHandler(){
|
||||
@Override
|
||||
public boolean canImport(TransferSupport support) {
|
||||
return support.isDataFlavorSupported(ColorTransferable.colorDataFlavor);
|
||||
}
|
||||
@Override
|
||||
public boolean importData(TransferSupport support) {
|
||||
if (canImport(support)) {
|
||||
try {
|
||||
Color color = (Color) support.getTransferable().getTransferData(ColorTransferable.colorDataFlavor);
|
||||
if (drawingAir == null || !(drawingAir instanceof DrawingAirBomber))
|
||||
return false;
|
||||
((DrawingAirBomber) drawingAir).ChangeAddColor(color);
|
||||
pictureBox.repaint();
|
||||
return true;
|
||||
} catch (UnsupportedFlavorException | IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
);
|
||||
addButton.setBounds(555,250,94,29);
|
||||
cancelButton.setBounds(679,250,94,29);
|
||||
pictureBox.setBounds(555, 65, pictureBoxWidth, pictureBoxHeight);
|
||||
colorLabel.setBounds(555, 20, 70, 33);
|
||||
additionalColorLabel.setBounds(629, 20, 70, 33);
|
||||
IDrawEnginesLabel.setBounds(703, 20, 70, 33);
|
||||
checkBoxBomb.setBounds(6, 132, 159, 24);
|
||||
checkBoxFuel.setBounds(6, 162, 145, 24);
|
||||
simpleLabel.setBounds(171,229, 120, 50);
|
||||
advancedLabel.setBounds(297,229, 120, 50);
|
||||
engines.setBounds(171,169, 120, 50);
|
||||
enginesCircle.setBounds(297,169, 120, 50);
|
||||
enginesOval.setBounds(423,169, 120, 50);
|
||||
colorPanel.setBounds(171, 23, 300,120);
|
||||
speedSpinner.setBounds(6, 46, 150, 27);
|
||||
speedLabel.setBounds(6, 23, 73, 20);
|
||||
weightSpinner.setBounds(6, 99, 150, 27);
|
||||
weightLabel.setBounds(6, 76, 33, 20);
|
||||
enginesNumberSpinner.setBounds(6, 200, 150, 27);
|
||||
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.MAGENTA);
|
||||
colorPanel.setLayout(new GridLayout(2, 4, 26, 10));
|
||||
colorPanel.add(redPanel);
|
||||
colorPanel.add(greenPanel);
|
||||
colorPanel.add(bluePanel);
|
||||
colorPanel.add(yellowPanel);
|
||||
colorPanel.add(whitePanel);
|
||||
colorPanel.add(grayPanel);
|
||||
colorPanel.add(blackPanel);
|
||||
colorPanel.add(purplePanel);
|
||||
add(colorLabel);
|
||||
add(additionalColorLabel);
|
||||
add(IDrawEnginesLabel);
|
||||
setLayout(null);
|
||||
setSize(818, 350);
|
||||
add(speedLabel);
|
||||
add(speedSpinner);
|
||||
add(weightLabel);
|
||||
add(weightSpinner);
|
||||
add(simpleLabel);
|
||||
add(advancedLabel);
|
||||
add(checkBoxBomb);
|
||||
add(checkBoxFuel);
|
||||
add(pictureBox);
|
||||
add(addButton);
|
||||
add(cancelButton);
|
||||
add(enginesNumberSpinner);
|
||||
add(colorPanel);
|
||||
add(engines);
|
||||
add(enginesCircle);
|
||||
add(enginesOval);
|
||||
}
|
||||
}
|
77
src/FrameHard.java
Normal file
77
src/FrameHard.java
Normal file
@ -0,0 +1,77 @@
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.util.Random;
|
||||
|
||||
public class FrameHard extends JFrame {
|
||||
HardGeneric<EntityAir, IDrawEngines> generic;
|
||||
DrawingAir drawing;
|
||||
private JComponent pictureBox;
|
||||
private final int pictureBoxWidth = 500;
|
||||
private final int pictureBoxHeight = 200;
|
||||
|
||||
public FrameHard() {
|
||||
setLocationRelativeTo(null);
|
||||
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
|
||||
pictureBox = new JComponent() {
|
||||
public void paintComponent(Graphics graphics) {
|
||||
super.paintComponent(graphics);
|
||||
Graphics2D graphics2D = (Graphics2D) graphics;
|
||||
if (drawing != null) drawing.drawTransport(graphics2D);
|
||||
super.repaint();
|
||||
}
|
||||
};
|
||||
pictureBox.setPreferredSize(new Dimension(pictureBoxWidth, pictureBoxHeight));
|
||||
JButton buttonMakeObject = new JButton("Создать новый объект");
|
||||
buttonMakeObject.setPreferredSize(new Dimension(150, 30));
|
||||
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
|
||||
buttonPanel.add(buttonMakeObject);
|
||||
Random rand = new Random();
|
||||
int size = rand.nextInt(1, 10);
|
||||
generic = new HardGeneric<>(size, size, pictureBoxWidth, pictureBoxHeight);
|
||||
for (int i = 0; i < size; i++) {
|
||||
generic.InsertPlanes(makeRandomPlane());
|
||||
generic.InsertEngine(makeRandomEngine());
|
||||
}
|
||||
buttonMakeObject.addActionListener(e -> {
|
||||
DrawingAir drawingPlane = generic.makeObject();
|
||||
drawingPlane.setPosition(pictureBoxWidth / 2 - drawingPlane.getWidth() / 2, pictureBoxHeight / 2 - drawingPlane.getHeight() / 2);
|
||||
drawing = drawingPlane;
|
||||
draw();
|
||||
});
|
||||
setLayout(new BorderLayout());
|
||||
add(pictureBox, BorderLayout.CENTER);
|
||||
add(buttonPanel, BorderLayout.SOUTH);
|
||||
pack();
|
||||
setVisible(true);
|
||||
}
|
||||
|
||||
public EntityAir makeRandomPlane() {
|
||||
Random rand = new Random();
|
||||
EntityAir plane;
|
||||
switch (rand.nextInt(2)){
|
||||
case 1 -> plane = new EntityAirBomber(rand.nextInt(100, 300), rand.nextDouble(1000, 3000),
|
||||
new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)),
|
||||
new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)),
|
||||
rand.nextBoolean(), rand.nextBoolean());
|
||||
default -> plane = new EntityAir(rand.nextInt(100, 300), rand.nextDouble(1000, 3000),
|
||||
new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)));
|
||||
}
|
||||
return plane;
|
||||
}
|
||||
|
||||
public IDrawEngines makeRandomEngine() {
|
||||
Random random = new Random();
|
||||
IDrawEngines engines;
|
||||
switch (random.nextInt(3)) {
|
||||
case 1 -> engines = new DrawingEnginesOval();
|
||||
case 2 -> engines = new DrawingEnginesRound();
|
||||
default -> engines = new DrawingEnginesSquare();
|
||||
}
|
||||
engines.setNumber((random.nextInt(3) + 1) * 2);
|
||||
return engines;
|
||||
}
|
||||
|
||||
void draw() {
|
||||
pictureBox.repaint();
|
||||
}
|
||||
}
|
271
src/FramePlaneCollection.java
Normal file
271
src/FramePlaneCollection.java
Normal file
@ -0,0 +1,271 @@
|
||||
import javax.swing.*;
|
||||
import javax.swing.border.StrokeBorder;
|
||||
|
||||
import java.awt.*;
|
||||
import java.io.IOException;
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Objects;
|
||||
|
||||
public class FramePlaneCollection extends JFrame {
|
||||
private BomberTrashCollection<DrawingAir> trashCollection = new BomberTrashCollection<>();
|
||||
private final BomberGenericStorage storage;
|
||||
private JComponent pictureBoxCollection;
|
||||
private TextField textFieldNumber;
|
||||
private TextField textFieldStorageName;
|
||||
private JList<String> listStorages;
|
||||
private DefaultListModel<String> listModel;
|
||||
public FramePlaneCollection(){
|
||||
super("Набор самолетов");
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
createGui();
|
||||
pack();
|
||||
storage = new BomberGenericStorage(pictureBoxCollection.getWidth(), pictureBoxCollection.getHeight());
|
||||
setVisible(true);
|
||||
}
|
||||
private void createGui(){
|
||||
//components initialisation
|
||||
pictureBoxCollection = new JComponent(){
|
||||
public void paintComponent(Graphics graphics){
|
||||
super.paintComponent(graphics);
|
||||
Graphics2D graphics2D = (Graphics2D) graphics;
|
||||
if (listStorages == null || storage == null)
|
||||
return;
|
||||
var collection = storage.getCollection(listStorages.getSelectedValue());
|
||||
if (collection == null)
|
||||
return;
|
||||
collection.ShowPlanes(graphics2D);
|
||||
super.repaint();
|
||||
}
|
||||
};
|
||||
JMenuBar menuFile = new JMenuBar();
|
||||
JMenu file = new JMenu("Файл");
|
||||
menuFile.add(file);
|
||||
JMenuItem saveFile = new JMenuItem("Сохранить");
|
||||
saveFile.addActionListener(e -> saveFile_Click());
|
||||
JMenuItem loadFile = new JMenuItem("Загрузить");
|
||||
loadFile.addActionListener(e -> loadFile_Click());
|
||||
JMenuItem saveCollection = new JMenuItem("Сохранить коллекцию");
|
||||
saveCollection.addActionListener(e -> saveCollection_Click());
|
||||
JMenuItem loadCollection = new JMenuItem("Загрузить коллекцию");
|
||||
loadCollection.addActionListener(e -> loadCollection_Click());
|
||||
file.add(saveCollection);
|
||||
file.add(loadCollection);
|
||||
file.add(saveFile);
|
||||
file.add(loadFile);
|
||||
super.setJMenuBar(menuFile);
|
||||
pictureBoxCollection.setPreferredSize(new Dimension(700, 600));
|
||||
JButton buttonAddPlane = new JButton("Добавить самолет");
|
||||
textFieldNumber = new TextField();
|
||||
JButton buttonRemovePlane = new JButton("Удалить самолет");
|
||||
JButton buttonRefreshCollection = new JButton("Обновить коллекцию");
|
||||
JButton buttonAddSet = new JButton("Добавить набор");
|
||||
JButton buttonDeleteSet = new JButton("Удалить набор");
|
||||
JButton buttonTrash = new JButton("Корзина");
|
||||
textFieldStorageName = new TextField();
|
||||
listModel = new DefaultListModel<>();
|
||||
JScrollPane scrollPane = new JScrollPane();
|
||||
listStorages= new JList<>(listModel);
|
||||
scrollPane.setViewportView(listStorages);
|
||||
//ActionListeners and ActionCommand addition
|
||||
buttonAddSet.addActionListener(e -> buttonAddSet_Click());
|
||||
buttonDeleteSet.addActionListener(e -> buttonDeleteSet_Click());
|
||||
buttonAddPlane.addActionListener(e -> buttonAddPlaneClick());
|
||||
buttonRemovePlane.addActionListener(e -> buttonRemovePlaneClick());
|
||||
buttonRefreshCollection.addActionListener(e -> buttonRefreshCollectionClick());
|
||||
buttonTrash.addActionListener(e->buttonTrashClick());
|
||||
//panels and constraints initialisation
|
||||
JPanel panelTools = new JPanel(new GridBagLayout());
|
||||
panelTools.setBorder(new StrokeBorder(new BasicStroke(3)));
|
||||
panelTools.setToolTipText("Инструменты");
|
||||
JPanel panelSets = new JPanel(new GridBagLayout());
|
||||
panelSets.setBorder(new StrokeBorder(new BasicStroke(3)));
|
||||
panelSets.setToolTipText("Наборы");
|
||||
GridBagConstraints constraints = new GridBagConstraints();
|
||||
constraints.insets.left = constraints.insets.right = 5;
|
||||
constraints.insets.top = constraints.insets.bottom = 5;
|
||||
constraints.fill = GridBagConstraints.BOTH;
|
||||
//addition to panelSets
|
||||
constraints.gridx = 0;
|
||||
constraints.gridy = 0;
|
||||
panelSets.add(textFieldStorageName, constraints);
|
||||
constraints.gridx = 0;
|
||||
constraints.gridy = 1;
|
||||
panelSets.add(buttonAddSet, constraints);
|
||||
constraints.gridx = 0;
|
||||
constraints.gridy = 2;
|
||||
panelSets.add(scrollPane, constraints);
|
||||
constraints.gridx = 0;
|
||||
constraints.gridy = 3;
|
||||
panelSets.add(buttonDeleteSet, constraints);
|
||||
//addition to panelTools
|
||||
constraints.gridx = 0;
|
||||
constraints.gridy = 0;
|
||||
panelTools.add(panelSets, constraints);
|
||||
constraints.gridx = 0;
|
||||
constraints.gridy = 1;
|
||||
panelTools.add(buttonAddPlane, constraints);
|
||||
constraints.gridx = 0;
|
||||
constraints.gridy = 2;
|
||||
panelTools.add(textFieldNumber, constraints);
|
||||
constraints.gridx = 0;
|
||||
constraints.gridy = 3;
|
||||
panelTools.add(buttonRemovePlane, constraints);
|
||||
constraints.gridx = 0;
|
||||
constraints.gridy = 4;
|
||||
panelTools.add(buttonRefreshCollection, constraints);
|
||||
constraints.gridx = 0;
|
||||
constraints.gridy = 5;
|
||||
panelTools.add(buttonTrash, constraints);
|
||||
//addition to frame
|
||||
setLayout(new BorderLayout());
|
||||
add(panelTools, BorderLayout.EAST);
|
||||
add(pictureBoxCollection, BorderLayout.CENTER);
|
||||
}
|
||||
private void saveFile_Click(){
|
||||
JFileChooser fc = new JFileChooser("C:\\Users\\salih\\OneDrive\\Рабочий стол\\HardSave");
|
||||
fc.addChoosableFileFilter(new TxtSaveFilter());
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
private void loadFile_Click() {
|
||||
JFileChooser fc = new JFileChooser("C:\\Users\\salih\\OneDrive\\Рабочий стол\\HardSave");
|
||||
fc.addChoosableFileFilter(new TxtSaveFilter());
|
||||
int ret = fc.showDialog(null, "Открыть файл");
|
||||
if(ret == JFileChooser.APPROVE_OPTION){
|
||||
File file = fc.getSelectedFile();
|
||||
try {
|
||||
storage.LoadData(file);
|
||||
reloadObjects();
|
||||
super.repaint();
|
||||
} catch (IOException ex) {
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void saveCollection_Click() {
|
||||
JFileChooser fc = new JFileChooser("C:\\Users\\salih\\OneDrive\\Рабочий стол\\HardSave");
|
||||
fc.addChoosableFileFilter(new TxtSaveFilter());
|
||||
int retrieval = fc.showSaveDialog(null);
|
||||
|
||||
if (retrieval == JFileChooser.APPROVE_OPTION) {
|
||||
File file = new File(fc.getSelectedFile() + "." + "txt");
|
||||
|
||||
try {
|
||||
if(listStorages.getSelectedIndex() == -1) {
|
||||
return;
|
||||
}
|
||||
storage.planeStorages.get(listStorages.getSelectedValue()).SaveData(file, listStorages.getSelectedValue());
|
||||
reloadObjects();
|
||||
super.repaint();
|
||||
} catch (IOException ex) {
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void loadCollection_Click() {
|
||||
JFileChooser fc = new JFileChooser("C:\\Users\\salih\\OneDrive\\Рабочий стол\\HardSave");
|
||||
fc.addChoosableFileFilter(new TxtSaveFilter());
|
||||
int ret = fc.showDialog(null, "Открыть файл");
|
||||
if(ret == JFileChooser.APPROVE_OPTION){
|
||||
File file = fc.getSelectedFile();
|
||||
try {
|
||||
storage.LoadCollection(file);
|
||||
reloadObjects();
|
||||
super.repaint();
|
||||
} catch (IOException ex) {
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
private void reloadObjects(){
|
||||
int index = listStorages.getSelectedIndex();
|
||||
listModel.clear();
|
||||
ArrayList<String> keys = storage.Keys();
|
||||
for (String key : keys) {
|
||||
listModel.addElement(key);
|
||||
}
|
||||
if(listModel.size() > 0 && (index == -1 || index >= listModel.size()))
|
||||
listStorages.setSelectedIndex(0);
|
||||
else if(listModel.size() > 0)
|
||||
listStorages.setSelectedIndex(index);
|
||||
}
|
||||
public void buttonAddSet_Click() {
|
||||
if(textFieldStorageName.getText() == null ) {
|
||||
JOptionPane.showMessageDialog(this, "Не все данные заполнены");
|
||||
return;
|
||||
}
|
||||
String name = textFieldStorageName.getText();
|
||||
if (Objects.equals(name, "")) {
|
||||
JOptionPane.showMessageDialog(this, "Не все данные заполнены");
|
||||
return;
|
||||
}
|
||||
storage.AddSet(name);
|
||||
reloadObjects();
|
||||
}
|
||||
public void buttonDeleteSet_Click() {
|
||||
if (listStorages.getSelectedIndex() == -1)
|
||||
return;
|
||||
storage.DelSet(listStorages.getSelectedValue(), trashCollection);
|
||||
reloadObjects();
|
||||
}
|
||||
public void buttonAddPlaneClick() {
|
||||
BomberGenericCollection<DrawingAir, DrawingObjectPlane> drawingPlanes = storage.getCollection(listStorages.getSelectedValue());
|
||||
FrameBomberConfig framePlaneConfig = new FrameBomberConfig();
|
||||
framePlaneConfig.addButton.addActionListener(e -> {
|
||||
if (drawingPlanes.Insert(framePlaneConfig.drawingAir)) {
|
||||
framePlaneConfig.dispose();
|
||||
framePlaneConfig.drawingAir._pictureWidth = pictureBoxCollection.getWidth();
|
||||
framePlaneConfig.drawingAir._pictureHeight = pictureBoxCollection.getHeight();
|
||||
JOptionPane.showMessageDialog(null, "Объект добавлен", "Информация", JOptionPane.INFORMATION_MESSAGE);
|
||||
pictureBoxCollection.repaint();
|
||||
}
|
||||
else {
|
||||
JOptionPane.showMessageDialog(null, "Не удалось добавить объект", "Информация", JOptionPane.INFORMATION_MESSAGE);
|
||||
}
|
||||
});
|
||||
framePlaneConfig.cancelButton.addActionListener(e -> framePlaneConfig.dispose());
|
||||
}
|
||||
private void buttonRemovePlaneClick(){
|
||||
if (listStorages.getSelectedIndex() == -1)
|
||||
return;
|
||||
var obj = storage.getCollection(listStorages.getSelectedValue());
|
||||
if (obj == null)
|
||||
return;
|
||||
int pos = Integer.parseInt(textFieldNumber.getText());
|
||||
var plane = obj.Get(pos);
|
||||
if (obj.Remove(pos)){
|
||||
JOptionPane.showMessageDialog(this, "Объект удален");
|
||||
trashCollection.Push(plane);
|
||||
pictureBoxCollection.repaint();
|
||||
}
|
||||
else{
|
||||
JOptionPane.showMessageDialog(this, "Не удалось удалить объект");
|
||||
}
|
||||
}
|
||||
private void buttonRefreshCollectionClick(){
|
||||
pictureBoxCollection.repaint();
|
||||
}
|
||||
private void buttonTrashClick(){
|
||||
if(trashCollection.GetSize() == 0)
|
||||
return;
|
||||
try {
|
||||
FrameAirBomber form = new FrameAirBomber();
|
||||
form.ChangePlane(trashCollection.Pop());
|
||||
} catch (Exception e){
|
||||
throw new RuntimeException();
|
||||
}
|
||||
}
|
||||
}
|
51
src/HardGeneric.java
Normal file
51
src/HardGeneric.java
Normal file
@ -0,0 +1,51 @@
|
||||
import java.util.Random;
|
||||
|
||||
public class HardGeneric<T extends EntityAir, U extends IDrawEngines> {
|
||||
T[] planes;
|
||||
U[] engines;
|
||||
private int planesNumber;
|
||||
private int enginesNumber;
|
||||
private int pictureBoxWidth;
|
||||
private int pictureBoxHeight;
|
||||
|
||||
public HardGeneric(int planesCount, int enginesCount, int width, int height) {
|
||||
planesNumber = 0;
|
||||
enginesNumber = 0;
|
||||
planes = (T[]) new EntityAir[planesCount];
|
||||
engines = (U[]) new IDrawEngines[enginesCount];
|
||||
pictureBoxHeight = height;
|
||||
pictureBoxWidth = width;
|
||||
}
|
||||
|
||||
public int InsertPlanes(T entityPlane) {
|
||||
if (planes[planes.length - 1] != null)
|
||||
return -1;
|
||||
for (int i = planesNumber - 1; i >= 0; i--) {
|
||||
planes[i + 1] = planes[i];
|
||||
}
|
||||
planesNumber++;
|
||||
planes[0] = entityPlane;
|
||||
return 0;
|
||||
}
|
||||
|
||||
public int InsertEngine(U engine) {
|
||||
if (engines[engines.length - 1] != null)
|
||||
return -1;
|
||||
for (int i = enginesNumber - 1; i >= 0; i--) {
|
||||
engines[i + 1] = engines[i];
|
||||
}
|
||||
enginesNumber++;
|
||||
engines[0] = engine;
|
||||
return 0;
|
||||
}
|
||||
|
||||
public DrawingAir makeObject() {
|
||||
Random rand = new Random();
|
||||
EntityAir entity = planes[rand.nextInt(0, planesNumber)];
|
||||
IDrawEngines engine = engines[rand.nextInt(0, enginesNumber)];
|
||||
if(entity instanceof EntityAirBomber)
|
||||
return new DrawingAirBomber(entity.getSpeed(), entity.getWeight(), entity.getBodyColor(), ((EntityAirBomber) entity).getAdditionalColor(),
|
||||
((EntityAirBomber) entity).getBombs(), ((EntityAirBomber) entity).getFuel(), pictureBoxWidth, pictureBoxHeight, engine.getType(), engine.getNumber());
|
||||
return new DrawingAir(entity.getSpeed(), entity.getWeight(), entity.getBodyColor(), pictureBoxWidth, pictureBoxHeight, engine.getType(), engine.getNumber());
|
||||
}
|
||||
}
|
8
src/IDrawEngines.java
Normal file
8
src/IDrawEngines.java
Normal file
@ -0,0 +1,8 @@
|
||||
import java.awt.*;
|
||||
|
||||
public interface IDrawEngines {
|
||||
public int getType();
|
||||
public int getNumber();
|
||||
public void setNumber(int x);
|
||||
public void drawEngines(Graphics2D graphics2D, int _startX, int _startY);
|
||||
}
|
6
src/IMoveableObject.java
Normal file
6
src/IMoveableObject.java
Normal file
@ -0,0 +1,6 @@
|
||||
public interface IMoveableObject {
|
||||
ObjectParameters getObjectPosition();
|
||||
int getStep();
|
||||
boolean checkCanMove(DirectionType direction);
|
||||
void moveObject(DirectionType direction);
|
||||
}
|
@ -1,5 +1,5 @@
|
||||
import java.io.IOException;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
System.out.println("dasa");
|
||||
}
|
||||
public static void main(String[] args) throws IOException { new FramePlaneCollection(); }
|
||||
}
|
34
src/MoveToBorder.java
Normal file
34
src/MoveToBorder.java
Normal file
@ -0,0 +1,34 @@
|
||||
public class MoveToBorder extends AbstractStrategy{
|
||||
@Override
|
||||
protected boolean IsTargetDestination() {
|
||||
var objParams = getObjectParameters.get();
|
||||
if(objParams == null)
|
||||
return false;
|
||||
return objParams.getRightBorder() + GetStep() >= getFieldWidth() &&
|
||||
objParams.getDownBorder() + GetStep() >= getFieldHeight();
|
||||
}
|
||||
@Override
|
||||
protected void MoveToTarget() {
|
||||
var objParams = getObjectParameters.get();
|
||||
if(objParams == null)
|
||||
return;
|
||||
var diffX = objParams.getRightBorder() - getFieldWidth();
|
||||
var diffY = objParams.getDownBorder() - getFieldHeight();
|
||||
if(diffX >= 0)
|
||||
{
|
||||
MoveDown();
|
||||
}
|
||||
else if(diffY >= 0)
|
||||
{
|
||||
MoveRight();
|
||||
}
|
||||
else if(Math.abs(diffX) > Math.abs(diffY))
|
||||
{
|
||||
MoveRight();
|
||||
}
|
||||
else
|
||||
{
|
||||
MoveDown();
|
||||
}
|
||||
}
|
||||
}
|
32
src/MoveToCenter.java
Normal file
32
src/MoveToCenter.java
Normal file
@ -0,0 +1,32 @@
|
||||
public class MoveToCenter extends AbstractStrategy{
|
||||
@Override
|
||||
protected boolean IsTargetDestination() {
|
||||
var objParams = getObjectParameters.get();
|
||||
if(objParams == null)
|
||||
return false;
|
||||
return objParams.getObjectMiddleHorizontal() <= getFieldWidth() / 2 &&
|
||||
objParams.getObjectMiddleHorizontal()+GetStep() >= getFieldWidth() / 2 &&
|
||||
objParams.getObjectMiddleVertical() <= getFieldHeight() / 2 &&
|
||||
objParams.getObjectMiddleVertical() + GetStep() >= getFieldHeight() / 2;
|
||||
}
|
||||
@Override
|
||||
protected void MoveToTarget() {
|
||||
var objParams = getObjectParameters.get();
|
||||
if(objParams == null)
|
||||
return;
|
||||
var diffX = objParams.getObjectMiddleHorizontal() - getFieldWidth() / 2;
|
||||
if(Math.abs(diffX) > GetStep()){
|
||||
if(diffX > 0)
|
||||
MoveLeft();
|
||||
else
|
||||
MoveRight();
|
||||
}
|
||||
var diffY = objParams.getObjectMiddleVertical() - getFieldHeight() / 2;
|
||||
if(Math.abs(diffY) > GetStep()){
|
||||
if(diffY > 0)
|
||||
MoveUp();
|
||||
else
|
||||
MoveDown();
|
||||
}
|
||||
}
|
||||
}
|
19
src/ObjectParameters.java
Normal file
19
src/ObjectParameters.java
Normal file
@ -0,0 +1,19 @@
|
||||
public class ObjectParameters {
|
||||
private final int POS_X;
|
||||
private final int POS_Y;
|
||||
private final int WIDTH;
|
||||
private final int HEIGHT;
|
||||
public int getLeftBorder() {return POS_X;}
|
||||
public int getTopBorder() {return POS_Y;}
|
||||
public int getRightBorder() {return POS_X + WIDTH;}
|
||||
public int getDownBorder() {return POS_Y + HEIGHT;}
|
||||
public int getObjectMiddleHorizontal() {return POS_X + this.WIDTH / 2;}
|
||||
public int getObjectMiddleVertical() {return POS_Y + this.HEIGHT / 2;}
|
||||
public ObjectParameters(int x, int y, int width, int height)
|
||||
{
|
||||
POS_X = x;
|
||||
POS_Y = y;
|
||||
WIDTH = width;
|
||||
HEIGHT = height;
|
||||
}
|
||||
}
|
42
src/SetGeneric.java
Normal file
42
src/SetGeneric.java
Normal file
@ -0,0 +1,42 @@
|
||||
import java.lang.reflect.Array;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class SetGeneric<T>{
|
||||
private final ArrayList<T> places;
|
||||
private final int maxCount;
|
||||
public int getCount() {return places.size();}
|
||||
public SetGeneric(int count){
|
||||
maxCount = count;
|
||||
places = new ArrayList<>();
|
||||
}
|
||||
public boolean insert(T plane){
|
||||
return insert(plane, 0);
|
||||
}
|
||||
public boolean insert(T plane, int position){
|
||||
if (!(position >= 0 && position <= places.size() && places.size() < maxCount))
|
||||
return false;
|
||||
places.add(position, plane);
|
||||
return true;
|
||||
}
|
||||
public boolean remove(int position){
|
||||
if(!(position >= 0 && position < places.size()))
|
||||
return false;
|
||||
places.remove(position);
|
||||
return true;
|
||||
}
|
||||
public T Get(int position){
|
||||
if(!(position >= 0 && position < getCount()))
|
||||
return null;
|
||||
return places.get(position);
|
||||
}
|
||||
public ArrayList<T> getPlanes(int maxPlanes){
|
||||
ArrayList<T> toRet = new ArrayList<>();
|
||||
for(int i = 0; i < places.size(); i++){
|
||||
toRet.add(places.get(i));
|
||||
if(i == maxPlanes)
|
||||
return toRet;
|
||||
}
|
||||
return toRet;
|
||||
}
|
||||
}
|
5
src/Status.java
Normal file
5
src/Status.java
Normal file
@ -0,0 +1,5 @@
|
||||
public enum Status {
|
||||
NOTINIT,
|
||||
INPROGRESS,
|
||||
FINISH
|
||||
}
|
18
src/TxtSaveFilter.java
Normal file
18
src/TxtSaveFilter.java
Normal file
@ -0,0 +1,18 @@
|
||||
import javax.swing.filechooser.FileFilter;
|
||||
import java.io.File;
|
||||
|
||||
public 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";
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user