Compare commits
42 Commits
Author | SHA1 | Date | |
---|---|---|---|
5a4b922514 | |||
bc5e326d4c | |||
b1d13cb621 | |||
90fda89057 | |||
|
748584bfde | ||
89342ecb23 | |||
c09d2a3764 | |||
2f6bbea7ba | |||
a051a7a3fb | |||
e0cb8ba7b4 | |||
37f392d98e | |||
b564eb8ff4 | |||
7796905b0e | |||
dfab5596fc | |||
|
0dcc62e3b2 | ||
660ee2abb3 | |||
0b528dee91 | |||
8aa6989be2 | |||
5fc726afe3 | |||
123fae95d6 | |||
202cf0a590 | |||
588661f027 | |||
|
bb46f1fc74 | ||
63425cea3d | |||
468e737fda | |||
018364f793 | |||
a88bc283c7 | |||
a19996af00 | |||
3db14138cd | |||
492f45eb92 | |||
7b254d18b9 | |||
b500486bd0 | |||
e2ac836193 | |||
4e3c6f6598 | |||
25dad76145 | |||
e720b435c5 | |||
cdb5b9f8fe | |||
107ff418db | |||
2fd750d255 | |||
5e6e077ef5 | |||
4d09e5187d | |||
b74e5b9fba |
188
src/main/java/AbstractMap.java
Normal file
188
src/main/java/AbstractMap.java
Normal file
@ -0,0 +1,188 @@
|
||||
import java.awt.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.util.HashMap;
|
||||
import java.util.Random;
|
||||
|
||||
public abstract class AbstractMap {
|
||||
private IDrawingObject drawingObject = null;
|
||||
protected int[][] map = null;
|
||||
protected int width;
|
||||
protected int height;
|
||||
protected float size_x;
|
||||
protected float size_y;
|
||||
protected final Random random = new Random();
|
||||
protected final int _freeRoad = 0;
|
||||
protected final int _barrier = 1;
|
||||
|
||||
public Image CreateMap(int width, int height, IDrawingObject drawingObject)
|
||||
{
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
this.drawingObject = drawingObject;
|
||||
GenerateMap();
|
||||
while (!SetObjectOnMap())
|
||||
{
|
||||
GenerateMap();
|
||||
}
|
||||
return DrawMapWithObject();
|
||||
}
|
||||
|
||||
public Image MoveObject(Direction direction)
|
||||
{
|
||||
if (drawingObject == null)
|
||||
return null;
|
||||
boolean flag = true;
|
||||
float step = drawingObject.getStep();
|
||||
|
||||
HashMap<String, Float> hashMap = drawingObject.GetCurrentPosition();
|
||||
float left = hashMap.get("Left");
|
||||
float right = hashMap.get("Right");
|
||||
float top = hashMap.get("Top");
|
||||
float bottom = hashMap.get("Bottom");
|
||||
|
||||
int x1_obj_next = (int)((left - step) / size_x);
|
||||
int y1_obj_next = (int)((top - step) / size_y);
|
||||
int x2_obj_next = (int)((right + step) / size_x);
|
||||
int y2_obj_next = (int)((bottom + step) / size_y);
|
||||
|
||||
int x1_obj_current = (int)(left / size_x);
|
||||
int y1_obj_current = (int)(top / size_y);
|
||||
int x2_obj_current = (int)(right / size_x);
|
||||
int y2_obj_current = (int)(bottom / size_y);
|
||||
|
||||
switch (direction)
|
||||
{
|
||||
case Left:
|
||||
{
|
||||
if (x1_obj_next < 0)
|
||||
flag = false;
|
||||
else
|
||||
{
|
||||
for (int i = x1_obj_next; i <= x1_obj_current; i++)
|
||||
{
|
||||
for (int j = y1_obj_current; j <= y2_obj_current; j++)
|
||||
{
|
||||
if (map[i][j] == _barrier)
|
||||
flag = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
case Right:
|
||||
{
|
||||
if (x2_obj_next >= map.length)
|
||||
flag = false;
|
||||
else
|
||||
{
|
||||
for (int i = x2_obj_current; i <= x2_obj_next; i++)
|
||||
{
|
||||
for (int j = y1_obj_current; j <= y2_obj_current; j++)
|
||||
{
|
||||
if (map[i][j] == _barrier)
|
||||
flag = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
case Up:
|
||||
{
|
||||
if (y1_obj_next < 0)
|
||||
flag = false;
|
||||
else
|
||||
{
|
||||
for (int i = x1_obj_current; i <= x2_obj_current; i++)
|
||||
{
|
||||
for (int j = y1_obj_next; j <= y1_obj_current; j++)
|
||||
{
|
||||
if (map[i][j] == _barrier)
|
||||
flag = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
case Down:
|
||||
{
|
||||
if (y2_obj_next >= map.length)
|
||||
flag = false;
|
||||
else
|
||||
{
|
||||
for (int i = x1_obj_current; i <= x2_obj_current; i++)
|
||||
{
|
||||
for (int j = y2_obj_current; j <= y2_obj_next; j++)
|
||||
{
|
||||
if (map[i][j] == _barrier)
|
||||
flag = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (flag)
|
||||
{
|
||||
drawingObject.MoveObject(direction);
|
||||
}
|
||||
return DrawMapWithObject();
|
||||
}
|
||||
private boolean SetObjectOnMap()
|
||||
{
|
||||
if (drawingObject == null || map == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
int x = random.nextInt(0, 10);
|
||||
int y = random.nextInt(0, 10);
|
||||
drawingObject.SetObject(x, y, width, height);
|
||||
|
||||
HashMap<String, Float> hashMap = drawingObject.GetCurrentPosition();
|
||||
float left = hashMap.get("Left");
|
||||
float right = hashMap.get("Right");
|
||||
float top = hashMap.get("Top");
|
||||
float bottom = hashMap.get("Bottom");
|
||||
|
||||
for (int i = (int)(x / size_x); i <= (int) (right / size_x); i++)
|
||||
{
|
||||
for (int j = (int)(y / size_y); j <= (int) (bottom / size_y); j++)
|
||||
{
|
||||
if (map[i][j] == _barrier)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
private Image DrawMapWithObject()
|
||||
{
|
||||
BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
|
||||
if (drawingObject == null || map == null)
|
||||
return img;
|
||||
|
||||
Graphics2D gr = img.createGraphics();
|
||||
|
||||
for (int i = 0; i < map.length; ++i)
|
||||
{
|
||||
for (int j = 0; j < map[0].length; ++j)
|
||||
{
|
||||
if (map[i][j] == _freeRoad)
|
||||
{
|
||||
DrawRoadPart(gr, i, j);
|
||||
}
|
||||
else if (map[i][j] == _barrier)
|
||||
{
|
||||
DrawBarrierPart(gr, i, j);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
drawingObject.DrawingObject(gr);
|
||||
return img;
|
||||
}
|
||||
protected abstract void GenerateMap();
|
||||
protected abstract void DrawRoadPart(Graphics2D g, int i, int j);
|
||||
protected abstract void DrawBarrierPart(Graphics2D g, int i, int j);
|
||||
}
|
43
src/main/java/Delegator.java
Normal file
43
src/main/java/Delegator.java
Normal file
@ -0,0 +1,43 @@
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedList;
|
||||
import java.util.Queue;
|
||||
|
||||
|
||||
public class Delegator<T> {
|
||||
private Queue<ArrayList<Object>> methods2 = new LinkedList<>();
|
||||
|
||||
public void add(Method method_ref, Object consumer) {
|
||||
ArrayList<Object> element = new ArrayList<>();
|
||||
element.add(method_ref);
|
||||
element.add(consumer);
|
||||
methods2.add(element);
|
||||
}
|
||||
|
||||
public void remove(Method method_ref) {
|
||||
var element = methods2.peek();
|
||||
while (element != null) {
|
||||
if (element.contains(method_ref)) {
|
||||
methods2.remove(element);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void invoke(T arg) {
|
||||
try {
|
||||
// должны вызывать все добавленные ранее методы по очереди
|
||||
var element = methods2.poll();
|
||||
while (element != null) {
|
||||
Method method = (Method) element.get(0);
|
||||
method.setAccessible(true);
|
||||
method.invoke(element.get(1), arg);
|
||||
element = methods2.poll();
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,33 +1,56 @@
|
||||
import java.awt.*;
|
||||
import java.util.HashMap;
|
||||
import java.util.Random;
|
||||
|
||||
public class DrawingArmoredCar {
|
||||
private EntityArmoredCar armoredCar;
|
||||
protected EntityArmoredCar armoredCar;
|
||||
|
||||
private float startPosX;
|
||||
protected float startPosX;
|
||||
|
||||
private float startPosY;
|
||||
protected float startPosY;
|
||||
|
||||
private int pictureWidth;
|
||||
|
||||
private int pictureHeight;
|
||||
|
||||
private static final int carWidth = 80;
|
||||
private int carWidth = 80;
|
||||
|
||||
private static final int carHeight = 50;
|
||||
private int carHeight = 50;
|
||||
|
||||
private DrawingCaterpillar drawingCaterpillar;
|
||||
protected IDrawingCaterpillar drawingCaterpillar;
|
||||
|
||||
public EntityArmoredCar getArmoredCar() {
|
||||
return armoredCar;
|
||||
}
|
||||
|
||||
public void Init(int speed, float weight, Color bodyColor) {
|
||||
this.armoredCar = new EntityArmoredCar();
|
||||
this.armoredCar.init(speed, weight, bodyColor);
|
||||
public DrawingArmoredCar(int speed, float weight, Color bodyColor) {
|
||||
this.armoredCar = new EntityArmoredCar(speed, weight, bodyColor);
|
||||
IDrawingCaterpillar[] realisations = new IDrawingCaterpillar[]{
|
||||
new DrawingCaterpillar(bodyColor),
|
||||
new DrawingCrossCaterpillar(bodyColor),
|
||||
new DrawingDoubleCaterpillar(bodyColor)};
|
||||
Random r = new Random();
|
||||
this.drawingCaterpillar = new DrawingCaterpillar();
|
||||
this.drawingCaterpillar.Init(r.nextInt(4, 7), bodyColor);
|
||||
int num = r.nextInt(4, 7);
|
||||
this.drawingCaterpillar = realisations[r.nextInt(3)];
|
||||
drawingCaterpillar.setNumRinks(num);
|
||||
}
|
||||
|
||||
protected DrawingArmoredCar(int speed, float weight, Color bodyColor, int carWidth, int carHeight) {
|
||||
this(speed, weight, bodyColor);
|
||||
this.carWidth = carWidth;
|
||||
this.carHeight = carHeight;
|
||||
}
|
||||
public IDrawingCaterpillar GetCaterpillars() {
|
||||
return drawingCaterpillar;
|
||||
}
|
||||
|
||||
public void SetCaterpillar(IDrawingCaterpillar caterpillar) {
|
||||
this.drawingCaterpillar = caterpillar;
|
||||
}
|
||||
|
||||
public void SetColor(Color color) {
|
||||
armoredCar = new EntityArmoredCar(armoredCar.getSpeed(), armoredCar.getWeight(), color);
|
||||
drawingCaterpillar.setColor(color);
|
||||
}
|
||||
|
||||
public void SetPosition(int x, int y, int width, int height) {
|
||||
@ -84,17 +107,19 @@ public class DrawingArmoredCar {
|
||||
{
|
||||
return;
|
||||
}
|
||||
// отрисовка корпуса и гусеницы
|
||||
g2d.setPaint(armoredCar.getBodyColor());
|
||||
g2d.fillRect((int ) startPosX + 20, (int) startPosY, 40, 20);
|
||||
// отрисовка корпуса и гусеницы
|
||||
int new_startPosX = Math.round(startPosX);
|
||||
int new_startPosY = Math.round(startPosY);
|
||||
g2d.fillRect(new_startPosX + 20, new_startPosY, 40, 20);
|
||||
g2d.setPaint(Color.LIGHT_GRAY);
|
||||
g2d.fillRect((int ) startPosX, (int ) startPosY + 20, 80, 20);
|
||||
g2d.fillRect(new_startPosX, new_startPosY + 20, 80, 20);
|
||||
|
||||
g2d.fillOval((int ) startPosX, (int ) startPosY + 30, 20, 20);
|
||||
g2d.fillOval((int ) startPosX + 80 - 20, (int ) startPosY + 30, 20, 20);
|
||||
g2d.fillRect((int ) startPosX + 15, (int ) startPosY + 20, 60, 30);
|
||||
g2d.fillOval(new_startPosX, new_startPosY + 30, 20, 20);
|
||||
g2d.fillOval(new_startPosX + 80 - 20, new_startPosY + 30, 20, 20);
|
||||
g2d.fillRect(new_startPosX + 15, new_startPosY + 20, 60, 30);
|
||||
// отрисовка катков в гусенице
|
||||
drawingCaterpillar.DrawCaterpillar(g2d, (int)startPosX, (int)startPosY);
|
||||
drawingCaterpillar.DrawCaterpillar(g2d, new_startPosX, new_startPosY);
|
||||
}
|
||||
|
||||
public void ChangeBorders(int width, int height)
|
||||
@ -116,4 +141,14 @@ public class DrawingArmoredCar {
|
||||
startPosY = pictureHeight - carHeight;
|
||||
}
|
||||
}
|
||||
|
||||
public HashMap<String, Float> GetCurrentPosition()
|
||||
{
|
||||
HashMap<String, Float> hashMap = new HashMap<String, Float>();
|
||||
hashMap.put("Left", startPosX);
|
||||
hashMap.put("Right", startPosX + carWidth);
|
||||
hashMap.put("Top", startPosY);
|
||||
hashMap.put("Bottom", startPosY + carHeight);
|
||||
return hashMap;
|
||||
}
|
||||
}
|
||||
|
@ -1,14 +1,13 @@
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawingCaterpillar {
|
||||
public class DrawingCaterpillar implements IDrawingCaterpillar {
|
||||
private NumRinks numRinks = NumRinks.Four;
|
||||
private Color color;
|
||||
|
||||
public void Init(int n, Color color) {
|
||||
setNumRinks(n);
|
||||
public DrawingCaterpillar(Color color) {
|
||||
this.color = color;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setNumRinks(int n) {
|
||||
switch (n) {
|
||||
case 4 -> numRinks = NumRinks.Four;
|
||||
@ -22,7 +21,7 @@ public class DrawingCaterpillar {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void DrawCaterpillar(Graphics2D g2d, int startPosX, int startPosY)
|
||||
{
|
||||
color = color != null ? color : Color.YELLOW;
|
||||
@ -34,4 +33,19 @@ public class DrawingCaterpillar {
|
||||
g2d.fillOval(startPosX + dist * i, startPosY + 30, size, size);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setColor(Color color) {
|
||||
this.color = color;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Color getColor() {
|
||||
return color;
|
||||
}
|
||||
|
||||
@Override
|
||||
public NumRinks getNumRinks() {
|
||||
return numRinks;
|
||||
}
|
||||
}
|
||||
|
57
src/main/java/DrawingCrossCaterpillar.java
Normal file
57
src/main/java/DrawingCrossCaterpillar.java
Normal file
@ -0,0 +1,57 @@
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawingCrossCaterpillar implements IDrawingCaterpillar{
|
||||
private NumRinks numRinks = NumRinks.Four;
|
||||
private Color color;
|
||||
|
||||
public DrawingCrossCaterpillar(Color color) {
|
||||
this.color = color;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setNumRinks(int n) {
|
||||
switch (n) {
|
||||
case 4 -> numRinks = NumRinks.Four;
|
||||
|
||||
case 5 -> numRinks = NumRinks.Five;
|
||||
|
||||
case 6 -> numRinks = NumRinks.Six;
|
||||
|
||||
default -> {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void DrawCaterpillar(Graphics2D g2d, int startPosX, int startPosY) {
|
||||
color = color != null ? color : Color.YELLOW;
|
||||
|
||||
int size = numRinks == NumRinks.Four ? 15 : 10;
|
||||
int dist = numRinks == NumRinks.Four ? 20 : 13;
|
||||
startPosX = numRinks == NumRinks.Five ? startPosX + 5 : startPosX;
|
||||
for (int i = 0; i < numRinks.val(); i++) {
|
||||
g2d.setPaint(color);
|
||||
g2d.fillOval(startPosX + dist * i, startPosY + 30, size, size);
|
||||
g2d.setColor(Color.BLACK);
|
||||
g2d.setStroke(new BasicStroke(3));
|
||||
g2d.drawLine(startPosX + dist * i, startPosY + 30, startPosX + dist * i + size, startPosY + 30 + size);
|
||||
g2d.drawLine(startPosX + dist * i + size, startPosY + 30, startPosX + dist * i, startPosY + 30 + size);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setColor(Color color) {
|
||||
this.color = color;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Color getColor() {
|
||||
return color;
|
||||
}
|
||||
|
||||
@Override
|
||||
public NumRinks getNumRinks() {
|
||||
return numRinks;
|
||||
}
|
||||
}
|
54
src/main/java/DrawingDoubleCaterpillar.java
Normal file
54
src/main/java/DrawingDoubleCaterpillar.java
Normal file
@ -0,0 +1,54 @@
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawingDoubleCaterpillar implements IDrawingCaterpillar{
|
||||
private NumRinks numRinks = NumRinks.Four;
|
||||
private Color color;
|
||||
|
||||
public DrawingDoubleCaterpillar(Color color) {
|
||||
this.color = color;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setNumRinks(int n) {
|
||||
switch (n) {
|
||||
case 4 -> numRinks = NumRinks.Four;
|
||||
|
||||
case 5 -> numRinks = NumRinks.Five;
|
||||
|
||||
case 6 -> numRinks = NumRinks.Six;
|
||||
|
||||
default -> {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void DrawCaterpillar(Graphics2D g2d, int startPosX, int startPosY) {
|
||||
color = color != null ? color : Color.YELLOW;
|
||||
int size = numRinks == NumRinks.Four ? 15 : 10;
|
||||
int dist = numRinks == NumRinks.Four ? 20 : 13;
|
||||
startPosX = numRinks == NumRinks.Five ? startPosX + 5 : startPosX;
|
||||
for (int i = 0; i < numRinks.val(); i++) {
|
||||
g2d.setPaint(color);
|
||||
g2d.fillOval(startPosX + dist * i, startPosY + 30, size, size);
|
||||
g2d.setPaint(Color.WHITE);
|
||||
g2d.fillOval(startPosX + dist * i + size/4, startPosY + 30 + size/4, size/2, size/2);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setColor(Color color) {
|
||||
this.color = color;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Color getColor() {
|
||||
return color;
|
||||
}
|
||||
|
||||
@Override
|
||||
public NumRinks getNumRinks() {
|
||||
return numRinks;
|
||||
}
|
||||
}
|
55
src/main/java/DrawingObjectArmoredCar.java
Normal file
55
src/main/java/DrawingObjectArmoredCar.java
Normal file
@ -0,0 +1,55 @@
|
||||
import java.awt.*;
|
||||
import java.util.HashMap;
|
||||
|
||||
public class DrawingObjectArmoredCar implements IDrawingObject{
|
||||
private DrawingArmoredCar armoredCar = null;
|
||||
|
||||
public DrawingObjectArmoredCar(DrawingArmoredCar armoredCar) {
|
||||
this.armoredCar = armoredCar;
|
||||
}
|
||||
|
||||
public DrawingArmoredCar getArmoredCar() {
|
||||
return armoredCar;
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getStep() {
|
||||
if (armoredCar != null)
|
||||
return armoredCar.armoredCar.step;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void SetObject(int x, int y, int width, int height) {
|
||||
if (armoredCar != null)
|
||||
armoredCar.SetPosition(x, y, width, height);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void MoveObject(Direction direction) {
|
||||
if (armoredCar != null)
|
||||
armoredCar.MoveTransport(direction);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void DrawingObject(Graphics2D g) {
|
||||
if (armoredCar != null)
|
||||
armoredCar.DrawTransport(g);
|
||||
}
|
||||
|
||||
@Override
|
||||
public HashMap<String, Float> GetCurrentPosition() {
|
||||
return armoredCar.GetCurrentPosition();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String GetInfo() {
|
||||
if (armoredCar != null)
|
||||
return ExtensionArmoredClass.GetDataForSave(armoredCar);
|
||||
return null;
|
||||
}
|
||||
|
||||
public static IDrawingObject Create(String data) {
|
||||
return new DrawingObjectArmoredCar(ExtensionArmoredClass.CreateDrawingArmoredCar(data));
|
||||
}
|
||||
}
|
43
src/main/java/DrawingTank.java
Normal file
43
src/main/java/DrawingTank.java
Normal file
@ -0,0 +1,43 @@
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawingTank extends DrawingArmoredCar{
|
||||
public DrawingTank(int speed, float weight, Color bodyColor, Color dopColor,
|
||||
boolean towerWeapon, boolean AMachineGun) {
|
||||
super(speed, weight, bodyColor, 80, 60);
|
||||
armoredCar = new EntityTank(speed, weight, bodyColor, dopColor, towerWeapon, AMachineGun);
|
||||
}
|
||||
@Override
|
||||
public void DrawTransport(Graphics2D g) {
|
||||
if (!(armoredCar instanceof EntityTank tank)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (tank.isTowerWeapon()) {
|
||||
g.setColor(tank.getDopColor());
|
||||
g.fillRect((int)startPosX + 40, (int)startPosY, 5, 20);
|
||||
g.fillRect((int)startPosX + 40, (int)startPosY, 40, 5);
|
||||
g.fillOval((int)startPosX + 27, (int)startPosY + 7, 30, 10);
|
||||
}
|
||||
if (tank.isAMachineGun()) {
|
||||
g.setColor(tank.getDopColor());
|
||||
g.fillRect((int)startPosX + 60, (int)startPosY + 15, 7, 10);
|
||||
g.fillRect((int)startPosX + 60, (int)startPosY + 17, 20, 5);
|
||||
}
|
||||
startPosY += 5;
|
||||
super.DrawTransport(g);
|
||||
startPosY -= 5;
|
||||
}
|
||||
@Override
|
||||
public void SetColor(Color color) {
|
||||
if (armoredCar instanceof EntityTank tank)
|
||||
armoredCar = new EntityTank(tank.getSpeed(), tank.getWeight(), color,
|
||||
tank.getDopColor(), tank.isTowerWeapon(), tank.isAMachineGun());
|
||||
drawingCaterpillar.setColor(color);
|
||||
}
|
||||
|
||||
public void SetDopColor(Color color) {
|
||||
if (armoredCar instanceof EntityTank tank)
|
||||
armoredCar = new EntityTank(tank.getSpeed(), tank.getWeight(), tank.getBodyColor(),
|
||||
color, tank.isTowerWeapon(), tank.isAMachineGun());
|
||||
}
|
||||
}
|
@ -7,7 +7,7 @@ public class EntityArmoredCar {
|
||||
private Color bodyColor;
|
||||
public float step;
|
||||
|
||||
public void init(int speed, float weight, Color bodyColor) {
|
||||
public EntityArmoredCar(int speed, float weight, Color bodyColor) {
|
||||
this.speed = speed;
|
||||
this.weight = weight;
|
||||
this.bodyColor = bodyColor;
|
||||
|
26
src/main/java/EntityTank.java
Normal file
26
src/main/java/EntityTank.java
Normal file
@ -0,0 +1,26 @@
|
||||
import java.awt.*;
|
||||
|
||||
public class EntityTank extends EntityArmoredCar{
|
||||
private Color dopColor;
|
||||
private boolean towerWeapon;
|
||||
private boolean AMachineGun;
|
||||
|
||||
public Color getDopColor() {
|
||||
return dopColor;
|
||||
}
|
||||
|
||||
public boolean isTowerWeapon() {
|
||||
return towerWeapon;
|
||||
}
|
||||
|
||||
public boolean isAMachineGun() {
|
||||
return AMachineGun;
|
||||
}
|
||||
|
||||
public EntityTank(int speed, float weight, Color bodyColor, Color dopColor, boolean towerWeapon, boolean AMachineGun) {
|
||||
super(speed, weight, bodyColor);
|
||||
this.dopColor = dopColor;
|
||||
this.towerWeapon = towerWeapon;
|
||||
this.AMachineGun = AMachineGun;
|
||||
}
|
||||
}
|
67
src/main/java/ExtensionArmoredClass.java
Normal file
67
src/main/java/ExtensionArmoredClass.java
Normal file
@ -0,0 +1,67 @@
|
||||
import java.awt.*;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class ExtensionArmoredClass {
|
||||
private static final String _separatorForObject = ":";
|
||||
|
||||
public static DrawingArmoredCar CreateDrawingArmoredCar(String info) {
|
||||
String[] strs = info.split(_separatorForObject);
|
||||
if (strs.length == 6)
|
||||
{
|
||||
var armoredCar = new DrawingArmoredCar(Integer.parseInt(strs[0]), Integer.parseInt(strs[1]), Color.decode(strs[2]));
|
||||
armoredCar.drawingCaterpillar = CreateIDrawingCaterpillar(String.join(_separatorForObject, Arrays.copyOfRange(strs, 3, strs.length)));
|
||||
return armoredCar;
|
||||
}
|
||||
if (strs.length == 9)
|
||||
{
|
||||
var armoredCar = new DrawingTank(Integer.parseInt(strs[0]),
|
||||
Integer.parseInt(strs[1]), Color.decode(strs[2]),
|
||||
Color.decode(strs[3]), Boolean.parseBoolean((strs[4])),
|
||||
Boolean.parseBoolean(strs[5]));
|
||||
armoredCar.drawingCaterpillar = CreateIDrawingCaterpillar(String.join(_separatorForObject, Arrays.copyOfRange(strs, 6, strs.length)));
|
||||
return armoredCar;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static IDrawingCaterpillar CreateIDrawingCaterpillar(String info) {
|
||||
String[] strs = info.split(_separatorForObject);
|
||||
IDrawingCaterpillar caterpillar = null;
|
||||
switch (strs[0]) {
|
||||
case "DrawingCaterpillar" -> {
|
||||
caterpillar = new DrawingCaterpillar(Color.decode(strs[1]));
|
||||
caterpillar.setNumRinks(Integer.parseInt(strs[2]));
|
||||
}
|
||||
case "DrawingCrossCaterpillar" -> {
|
||||
caterpillar = new DrawingCrossCaterpillar(Color.decode(strs[1]));
|
||||
caterpillar.setNumRinks(Integer.parseInt(strs[2]));
|
||||
}
|
||||
case "DrawingDoubleCaterpillar" -> {
|
||||
caterpillar = new DrawingDoubleCaterpillar((Color.decode(strs[1])));
|
||||
caterpillar.setNumRinks(Integer.parseInt(strs[2]));
|
||||
}
|
||||
}
|
||||
return caterpillar;
|
||||
}
|
||||
|
||||
public static String GetDataForSave(DrawingArmoredCar drawingArmoredCar)
|
||||
{
|
||||
var armoredCar = drawingArmoredCar.armoredCar;
|
||||
var str = String.format("%d%s%d%s%d", armoredCar.getSpeed(), _separatorForObject,
|
||||
(int)armoredCar.getWeight(), _separatorForObject, armoredCar.getBodyColor().getRGB());
|
||||
if (!(armoredCar instanceof EntityTank tank))
|
||||
{
|
||||
return String.format("%s%s%s", str, _separatorForObject, GetDataForSave(drawingArmoredCar.drawingCaterpillar));
|
||||
}
|
||||
return String.format("%s%s%d%s%s%s%s%s%s", str, _separatorForObject,
|
||||
tank.getDopColor().getRGB(), _separatorForObject, tank.isTowerWeapon(), _separatorForObject,
|
||||
tank.isAMachineGun(), _separatorForObject, GetDataForSave(drawingArmoredCar.drawingCaterpillar));
|
||||
}
|
||||
|
||||
public static String GetDataForSave(IDrawingCaterpillar drawingCaterpillar) {
|
||||
return String.format("%s%s%d%s%d", drawingCaterpillar.getClass().getName(), _separatorForObject,
|
||||
drawingCaterpillar.getColor().getRGB(), _separatorForObject, drawingCaterpillar.getNumRinks().val());
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -22,7 +22,7 @@
|
||||
<border type="none"/>
|
||||
<children/>
|
||||
</grid>
|
||||
<grid id="7c72e" layout-manager="GridLayoutManager" row-count="2" column-count="4" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
|
||||
<grid id="7c72e" layout-manager="GridLayoutManager" row-count="2" column-count="6" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
|
||||
<margin top="0" left="0" bottom="0" right="0"/>
|
||||
<constraints>
|
||||
<grid row="3" column="0" row-span="1" col-span="3" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="true"/>
|
||||
@ -40,7 +40,7 @@
|
||||
</component>
|
||||
<component id="f4d6e" class="javax.swing.JButton" binding="buttonRight">
|
||||
<constraints>
|
||||
<grid row="1" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
<grid row="1" column="5" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<icon value="Right.png"/>
|
||||
@ -49,7 +49,7 @@
|
||||
</component>
|
||||
<component id="12974" class="javax.swing.JButton" binding="buttonUp">
|
||||
<constraints>
|
||||
<grid row="0" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
<grid row="0" column="4" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<enabled value="true"/>
|
||||
@ -60,7 +60,7 @@
|
||||
</component>
|
||||
<component id="da478" class="javax.swing.JButton" binding="buttonDown">
|
||||
<constraints>
|
||||
<grid row="1" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
<grid row="1" column="4" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<icon value="Down.png"/>
|
||||
@ -69,7 +69,7 @@
|
||||
</component>
|
||||
<component id="43d8e" class="javax.swing.JButton" binding="buttonLeft">
|
||||
<constraints>
|
||||
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
<grid row="1" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<icon value="Left.png"/>
|
||||
@ -77,6 +77,22 @@
|
||||
<visible value="true"/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="64b34" class="javax.swing.JButton" binding="buttonCreate_Modif">
|
||||
<constraints>
|
||||
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<text value="Модификация"/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="7c1e4" class="javax.swing.JButton" binding="buttonSelectArmoredCar">
|
||||
<constraints>
|
||||
<grid row="1" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<text value="Выбрать"/>
|
||||
</properties>
|
||||
</component>
|
||||
</children>
|
||||
</grid>
|
||||
<component id="2cd31" class="javax.swing.JLabel" binding="labelSpeed">
|
||||
|
@ -17,13 +17,16 @@ public class FormArmoredCar extends JFrame{
|
||||
private JLabel labelSpeed;
|
||||
private JLabel labelWeight;
|
||||
private JLabel labelColor;
|
||||
private JButton buttonCreate_Modif;
|
||||
private JButton buttonSelectArmoredCar;
|
||||
|
||||
private DrawingArmoredCar armoredCar;
|
||||
|
||||
private DrawingArmoredCar selectedCar;
|
||||
public boolean DialogResult = false;
|
||||
public FormArmoredCar() {
|
||||
super("Бронированная машина");
|
||||
setBounds(100, 100, 700, 700);
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
|
||||
|
||||
addComponentListener(new ComponentAdapter() {
|
||||
public void componentResized(ComponentEvent componentEvent) {
|
||||
@ -42,16 +45,11 @@ public class FormArmoredCar extends JFrame{
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent actionEvent) {
|
||||
Random rnd = new Random();
|
||||
armoredCar = new DrawingArmoredCar();
|
||||
armoredCar.Init(rnd.nextInt(100,300), rnd.nextInt(1000, 2000),
|
||||
new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)));
|
||||
Color newColor = JColorChooser.showDialog(null, "Choose a color", Color.RED);
|
||||
armoredCar = new DrawingArmoredCar(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000),
|
||||
newColor);
|
||||
|
||||
armoredCar.SetPosition(rnd.nextInt(10, 100), rnd.nextInt(10, 100),
|
||||
drawPanel.getWidth(), drawPanel.getHeight());
|
||||
|
||||
labelSpeed.setText("Скорость: " + armoredCar.getArmoredCar().getSpeed());
|
||||
labelWeight.setText("Вес: " + armoredCar.getArmoredCar().getWeight());
|
||||
labelColor.setText("Цвет: " + armoredCar.getArmoredCar().getBodyColor().getRGB());
|
||||
setData();
|
||||
}
|
||||
});
|
||||
|
||||
@ -94,6 +92,30 @@ public class FormArmoredCar extends JFrame{
|
||||
|
||||
setContentPane(mainPanel);
|
||||
setVisible(true);
|
||||
buttonCreate_Modif.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
Random rnd = new Random();
|
||||
Color newColor = JColorChooser.showDialog(null, "Choose a color", Color.RED);
|
||||
Color newColorDop = JColorChooser.showDialog(null, "Choose a color", Color.RED);
|
||||
armoredCar = new DrawingTank(rnd.nextInt(100, 300), rnd.nextInt(1000,
|
||||
2000),
|
||||
newColor,
|
||||
newColorDop,
|
||||
1 == rnd.nextInt(0, 2), 1 == rnd.nextInt(0, 2));
|
||||
setData();
|
||||
}
|
||||
});
|
||||
buttonSelectArmoredCar.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if (armoredCar != null) {
|
||||
selectedCar = armoredCar;
|
||||
DialogResult = true;
|
||||
dispose();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void createUIComponents() {
|
||||
@ -109,4 +131,22 @@ public class FormArmoredCar extends JFrame{
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public DrawingArmoredCar getSelectedCar() {
|
||||
return selectedCar;
|
||||
}
|
||||
|
||||
private void setData()
|
||||
{
|
||||
Random rnd = new Random();
|
||||
armoredCar.SetPosition(rnd.nextInt(10, 100), rnd.nextInt(10, 100), drawPanel.getWidth(), drawPanel.getHeight());
|
||||
labelSpeed.setText("Скорость: " + armoredCar.getArmoredCar().getSpeed());
|
||||
labelWeight.setText("Вес: " + armoredCar.getArmoredCar().getWeight());
|
||||
labelColor.setText("Цвет: " + armoredCar.getArmoredCar().getBodyColor().getRGB());
|
||||
}
|
||||
|
||||
public void setArmoredCar(DrawingArmoredCar armoredCar) {
|
||||
this.armoredCar = armoredCar;
|
||||
setData();
|
||||
}
|
||||
}
|
256
src/main/java/FormArmoredCarConfig.form
Normal file
256
src/main/java/FormArmoredCarConfig.form
Normal file
@ -0,0 +1,256 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="FormArmoredCarConfig">
|
||||
<grid id="27dc6" binding="mainPanel" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
|
||||
<margin top="0" left="0" bottom="0" right="0"/>
|
||||
<constraints>
|
||||
<xy x="20" y="20" width="864" height="273"/>
|
||||
</constraints>
|
||||
<properties/>
|
||||
<border type="none"/>
|
||||
<children>
|
||||
<grid id="48e70" layout-manager="GridLayoutManager" row-count="7" column-count="10" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
|
||||
<margin top="0" left="0" bottom="0" right="0"/>
|
||||
<constraints>
|
||||
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties/>
|
||||
<border type="none"/>
|
||||
<children>
|
||||
<grid id="4c6f3" layout-manager="GridLayoutManager" row-count="3" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
|
||||
<margin top="0" left="0" bottom="0" right="0"/>
|
||||
<constraints>
|
||||
<grid row="1" column="8" row-span="6" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties/>
|
||||
<border type="none"/>
|
||||
<children>
|
||||
<component id="8a4cf" class="javax.swing.JButton" binding="buttonOk">
|
||||
<constraints>
|
||||
<grid row="2" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<text value="Добавить"/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="5498b" class="javax.swing.JButton" binding="buttonCancel">
|
||||
<constraints>
|
||||
<grid row="2" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<text value="Отмена"/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="ce0c6" class="javax.swing.JLabel" binding="labelBaseColor">
|
||||
<constraints>
|
||||
<grid row="0" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="0" anchor="0" fill="0" indent="0" use-parent-layout="false">
|
||||
<minimum-size width="60" height="30"/>
|
||||
</grid>
|
||||
</constraints>
|
||||
<properties>
|
||||
<horizontalAlignment value="0"/>
|
||||
<text value="Цвет"/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="e8476" class="javax.swing.JLabel" binding="labelDopColor">
|
||||
<constraints>
|
||||
<grid row="0" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="0" indent="0" use-parent-layout="false">
|
||||
<minimum-size width="60" height="30"/>
|
||||
</grid>
|
||||
</constraints>
|
||||
<properties>
|
||||
<horizontalAlignment value="0"/>
|
||||
<text value="Доп. Цвет"/>
|
||||
</properties>
|
||||
</component>
|
||||
<grid id="333f1" binding="drawPanel" custom-create="true" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
|
||||
<margin top="0" left="0" bottom="0" right="0"/>
|
||||
<constraints>
|
||||
<grid row="1" column="0" row-span="1" col-span="3" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<background color="-3291443"/>
|
||||
</properties>
|
||||
<border type="none"/>
|
||||
<children/>
|
||||
</grid>
|
||||
</children>
|
||||
</grid>
|
||||
<component id="b99f7" class="javax.swing.JSpinner" binding="numericUpDownSpeed" custom-create="true">
|
||||
<constraints>
|
||||
<grid row="1" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties/>
|
||||
</component>
|
||||
<component id="fd32c" class="javax.swing.JSpinner" binding="numericUpDownWeight" custom-create="true">
|
||||
<constraints>
|
||||
<grid row="2" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties/>
|
||||
</component>
|
||||
<component id="9d793" class="javax.swing.JCheckBox" binding="checkBoxTowerWeapon">
|
||||
<constraints>
|
||||
<grid row="4" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<text value="Признак наличия орудия на башне"/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="d2725" class="javax.swing.JCheckBox" binding="checkBoxAMachineGun">
|
||||
<constraints>
|
||||
<grid row="5" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<text value="Признак наличия зенитного пулемета"/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="a8425" class="javax.swing.JLabel" binding="labelSpeed">
|
||||
<constraints>
|
||||
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<horizontalAlignment value="11"/>
|
||||
<text value="Скорость:"/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="ba547" class="javax.swing.JLabel" binding="labelWeight">
|
||||
<constraints>
|
||||
<grid row="2" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<text value="Вес:"/>
|
||||
</properties>
|
||||
</component>
|
||||
<grid id="b50ae" layout-manager="GridLayoutManager" row-count="3" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
|
||||
<margin top="0" left="0" bottom="0" right="0"/>
|
||||
<constraints>
|
||||
<grid row="1" column="5" row-span="6" col-span="3" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties/>
|
||||
<border type="none"/>
|
||||
<children>
|
||||
<grid id="b4cbb" binding="colorsPanel" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
|
||||
<margin top="0" left="0" bottom="0" right="0"/>
|
||||
<constraints>
|
||||
<grid row="0" column="0" row-span="1" col-span="3" vsize-policy="2" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<background color="-1"/>
|
||||
</properties>
|
||||
<border type="none"/>
|
||||
<children/>
|
||||
</grid>
|
||||
<component id="486f2" class="javax.swing.JLabel" binding="labelDoubleCaterpillar">
|
||||
<constraints>
|
||||
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false">
|
||||
<minimum-size width="-1" height="30"/>
|
||||
</grid>
|
||||
</constraints>
|
||||
<properties>
|
||||
<horizontalAlignment value="0"/>
|
||||
<text value="Двойные"/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="287da" class="javax.swing.JLabel" binding="labelSimpleCaterpillar">
|
||||
<constraints>
|
||||
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false">
|
||||
<minimum-size width="-1" height="30"/>
|
||||
</grid>
|
||||
</constraints>
|
||||
<properties>
|
||||
<horizontalAlignment value="0"/>
|
||||
<text value="Обычные"/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="61c17" class="javax.swing.JLabel" binding="labelCrossCaterpillar">
|
||||
<constraints>
|
||||
<grid row="1" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false">
|
||||
<minimum-size width="-1" height="30"/>
|
||||
</grid>
|
||||
</constraints>
|
||||
<properties>
|
||||
<horizontalAlignment value="0"/>
|
||||
<text value="Крестики"/>
|
||||
</properties>
|
||||
</component>
|
||||
<grid id="7be45" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
|
||||
<margin top="0" left="0" bottom="0" right="0"/>
|
||||
<constraints>
|
||||
<grid row="2" column="0" row-span="1" col-span="3" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties/>
|
||||
<border type="none"/>
|
||||
<children>
|
||||
<component id="88d7e" class="javax.swing.JLabel" binding="labelModifiedObject">
|
||||
<constraints>
|
||||
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false">
|
||||
<minimum-size width="80" height="30"/>
|
||||
</grid>
|
||||
</constraints>
|
||||
<properties>
|
||||
<horizontalAlignment value="0"/>
|
||||
<text value="Продвинутый"/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="e9351" class="javax.swing.JLabel" binding="labelSimpleObject">
|
||||
<constraints>
|
||||
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false">
|
||||
<minimum-size width="80" height="30"/>
|
||||
</grid>
|
||||
</constraints>
|
||||
<properties>
|
||||
<horizontalAlignment value="0"/>
|
||||
<horizontalTextPosition value="11"/>
|
||||
<text value="Простой"/>
|
||||
</properties>
|
||||
</component>
|
||||
</children>
|
||||
</grid>
|
||||
</children>
|
||||
</grid>
|
||||
<component id="b4b99" class="javax.swing.JLabel" binding="labelParameters">
|
||||
<constraints>
|
||||
<grid row="0" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="0" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<text value="Параметры"/>
|
||||
</properties>
|
||||
</component>
|
||||
<hspacer id="893be">
|
||||
<constraints>
|
||||
<grid row="1" column="4" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
</hspacer>
|
||||
<hspacer id="c09a6">
|
||||
<constraints>
|
||||
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
</hspacer>
|
||||
<hspacer id="f3f03">
|
||||
<constraints>
|
||||
<grid row="1" column="3" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
</hspacer>
|
||||
<hspacer id="abacc">
|
||||
<constraints>
|
||||
<grid row="4" column="9" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
</hspacer>
|
||||
<component id="def80" class="javax.swing.JSpinner" binding="numericUpDownCount" custom-create="true">
|
||||
<constraints>
|
||||
<grid row="3" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties/>
|
||||
</component>
|
||||
<component id="39892" class="javax.swing.JLabel" binding="labelCatCounts">
|
||||
<constraints>
|
||||
<grid row="3" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<text value="Количество катков:"/>
|
||||
</properties>
|
||||
</component>
|
||||
</children>
|
||||
</grid>
|
||||
</children>
|
||||
</grid>
|
||||
</form>
|
253
src/main/java/FormArmoredCarConfig.java
Normal file
253
src/main/java/FormArmoredCarConfig.java
Normal file
@ -0,0 +1,253 @@
|
||||
import javax.swing.*;
|
||||
import javax.swing.border.LineBorder;
|
||||
import java.awt.*;
|
||||
import java.awt.dnd.*;
|
||||
import java.awt.event.MouseAdapter;
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
public class FormArmoredCarConfig extends JDialog{
|
||||
private JPanel mainPanel;
|
||||
private JButton buttonOk;
|
||||
private JButton buttonCancel;
|
||||
private JSpinner numericUpDownSpeed;
|
||||
private JSpinner numericUpDownWeight;
|
||||
private JCheckBox checkBoxAMachineGun;
|
||||
private JCheckBox checkBoxTowerWeapon;
|
||||
private JLabel labelSpeed;
|
||||
private JLabel labelWeight;
|
||||
private JLabel labelParameters;
|
||||
private JLabel labelSimpleObject;
|
||||
private JLabel labelModifiedObject;
|
||||
private JLabel labelBaseColor;
|
||||
private JLabel labelDopColor;
|
||||
private JPanel drawPanel;
|
||||
private JPanel colorsPanel;
|
||||
private JLabel labelSimpleCaterpillar;
|
||||
private JLabel labelDoubleCaterpillar;
|
||||
private JLabel labelCrossCaterpillar;
|
||||
private JSpinner numericUpDownCount;
|
||||
private JLabel labelCatCounts;
|
||||
|
||||
private DrawingArmoredCar armoredCar = null;
|
||||
private Delegator<DrawingArmoredCar> delegator;
|
||||
|
||||
public FormArmoredCarConfig() {
|
||||
setTitle("Создание объекта");
|
||||
setBounds(100, 100, 800, 300);
|
||||
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
|
||||
|
||||
setContentPane(mainPanel);
|
||||
setVisible(true);
|
||||
labelSimpleObject.setBorder(new LineBorder(Color.BLACK));
|
||||
labelModifiedObject.setBorder(new LineBorder(Color.BLACK));
|
||||
labelBaseColor.setBorder(new LineBorder(Color.BLACK));
|
||||
labelDopColor.setBorder(new LineBorder(Color.BLACK));
|
||||
labelSimpleCaterpillar.setBorder(new LineBorder(Color.BLACK));
|
||||
labelDoubleCaterpillar.setBorder(new LineBorder(Color.BLACK));
|
||||
labelCrossCaterpillar.setBorder(new LineBorder(Color.BLACK));
|
||||
|
||||
var mouseAdapter = new DragMouseAdapter();
|
||||
|
||||
labelSimpleObject.addMouseListener(mouseAdapter);
|
||||
labelSimpleObject.setTransferHandler(new TransferHandler("text"));
|
||||
|
||||
labelModifiedObject.addMouseListener(mouseAdapter);
|
||||
labelModifiedObject.setTransferHandler(new TransferHandler("text"));
|
||||
|
||||
labelSimpleCaterpillar.addMouseListener(mouseAdapter);
|
||||
labelDoubleCaterpillar.addMouseListener(mouseAdapter);
|
||||
labelCrossCaterpillar.addMouseListener(mouseAdapter);
|
||||
|
||||
labelSimpleCaterpillar.setTransferHandler(new TransferHandler("text"));
|
||||
labelDoubleCaterpillar.setTransferHandler(new TransferHandler("text"));
|
||||
labelCrossCaterpillar.setTransferHandler(new TransferHandler("text"));
|
||||
|
||||
drawPanel.setBackground(null);
|
||||
colorsPanel.setBorder(BorderFactory.createTitledBorder("Цвета"));
|
||||
colorsPanel.setLayout(new GridLayout(2, 4));
|
||||
colorsPanel.setBackground(null);
|
||||
setColors();
|
||||
buttonCancel.addActionListener((e) -> dispose());
|
||||
new DrawPanelDropTargetListener(drawPanel);
|
||||
new LabelColorDropTargetListener(labelBaseColor);
|
||||
new LabelColorDropTargetListener(labelDopColor);
|
||||
|
||||
|
||||
buttonOk.addActionListener(e -> {
|
||||
if (delegator != null)
|
||||
delegator.invoke(armoredCar);
|
||||
dispose();
|
||||
});
|
||||
}
|
||||
|
||||
private class DragMouseAdapter extends MouseAdapter {
|
||||
public void mousePressed(MouseEvent e) {
|
||||
var c = (JComponent) e.getSource();
|
||||
var handler = c.getTransferHandler();
|
||||
handler.exportAsDrag(c, e, TransferHandler.COPY);
|
||||
}
|
||||
}
|
||||
|
||||
private class LabelColorDropTargetListener extends DropTargetAdapter{
|
||||
private final DropTarget dropTarget;
|
||||
private final JLabel label;
|
||||
|
||||
public LabelColorDropTargetListener(JLabel label) {
|
||||
this.label = label;
|
||||
dropTarget = new DropTarget(label, DnDConstants.ACTION_COPY,
|
||||
this, true, null);
|
||||
}
|
||||
@Override
|
||||
public void drop(DropTargetDropEvent event) {
|
||||
try {
|
||||
var tr = event.getTransferable();
|
||||
var dataFlavors = tr.getTransferDataFlavors();
|
||||
String s = (String) tr.getTransferData(dataFlavors[0]);
|
||||
Color color = Color.decode(s);
|
||||
switch (label.getText()) {
|
||||
case "Цвет": {
|
||||
if (armoredCar != null) {
|
||||
armoredCar.SetColor(color);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case "Доп. Цвет": {
|
||||
if (armoredCar != null) {
|
||||
if (armoredCar instanceof DrawingTank tank) {
|
||||
tank.SetDopColor(color);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
drawPanel.repaint();
|
||||
} catch (Exception e) {
|
||||
event.rejectDrop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private class DrawPanelDropTargetListener extends DropTargetAdapter {
|
||||
|
||||
private final DropTarget dropTarget;
|
||||
private final JPanel panel;
|
||||
|
||||
public DrawPanelDropTargetListener(JPanel panel) {
|
||||
this.panel = panel;
|
||||
|
||||
dropTarget = new DropTarget(panel, DnDConstants.ACTION_COPY,
|
||||
this, true, null);
|
||||
}
|
||||
|
||||
|
||||
public void drop(DropTargetDropEvent event) {
|
||||
try {
|
||||
var tr = event.getTransferable();
|
||||
var dataFlavors = tr.getTransferDataFlavors();
|
||||
String drawing_type = (String) tr.getTransferData(dataFlavors[0]);
|
||||
event.acceptDrop(DnDConstants.ACTION_COPY);
|
||||
switch (drawing_type) {
|
||||
case "Простой": {
|
||||
armoredCar = new DrawingArmoredCar((int) numericUpDownSpeed.getValue(), (int) numericUpDownWeight.getValue(), Color.WHITE);
|
||||
IDrawingCaterpillar drawingCaterpillar = armoredCar.GetCaterpillars();
|
||||
drawingCaterpillar.setNumRinks((int)numericUpDownCount.getValue());
|
||||
break;
|
||||
}
|
||||
case "Продвинутый": {
|
||||
armoredCar = new DrawingTank((int) numericUpDownSpeed.getValue(), (int) numericUpDownWeight.getValue(), Color.WHITE, Color.BLACK,
|
||||
checkBoxTowerWeapon.isSelected(), checkBoxAMachineGun.isSelected());
|
||||
IDrawingCaterpillar drawingCaterpillar = armoredCar.GetCaterpillars();
|
||||
drawingCaterpillar.setNumRinks((int)numericUpDownCount.getValue());
|
||||
break;
|
||||
}
|
||||
case "Обычные": {
|
||||
if (armoredCar != null) {
|
||||
IDrawingCaterpillar drawingCaterpillar = new DrawingCaterpillar(armoredCar.armoredCar.getBodyColor());
|
||||
drawingCaterpillar.setNumRinks((int)numericUpDownCount.getValue());
|
||||
armoredCar.SetCaterpillar(drawingCaterpillar);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case "Двойные": {
|
||||
if (armoredCar != null) {
|
||||
IDrawingCaterpillar drawingCaterpillar = new DrawingDoubleCaterpillar(armoredCar.armoredCar.getBodyColor());
|
||||
drawingCaterpillar.setNumRinks((int)numericUpDownCount.getValue());
|
||||
armoredCar.SetCaterpillar(drawingCaterpillar);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case "Крестики": {
|
||||
if (armoredCar != null) {
|
||||
IDrawingCaterpillar drawingCaterpillar = new DrawingCrossCaterpillar(armoredCar.armoredCar.getBodyColor());
|
||||
drawingCaterpillar.setNumRinks((int)numericUpDownCount.getValue());
|
||||
armoredCar.SetCaterpillar(drawingCaterpillar);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
event.dropComplete(true);
|
||||
drawPanel.repaint();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
event.rejectDrop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void createUIComponents() {
|
||||
drawPanel = new JPanel() {
|
||||
@Override
|
||||
public void paintComponent(Graphics g) {
|
||||
super.paintComponent(g);
|
||||
if (armoredCar != null) {
|
||||
Graphics2D g2d = (Graphics2D) g;
|
||||
armoredCar.SetPosition(drawPanel.getWidth() / 4, drawPanel.getHeight() / 4, drawPanel.getWidth(), drawPanel.getHeight());
|
||||
armoredCar.DrawTransport(g2d);
|
||||
}
|
||||
super.repaint();
|
||||
}
|
||||
};
|
||||
numericUpDownCount = new JSpinner(new SpinnerNumberModel(4, 4, 6, 1));
|
||||
numericUpDownSpeed = new JSpinner(new SpinnerNumberModel(100, 100, 1000, 1));
|
||||
numericUpDownWeight = new JSpinner(new SpinnerNumberModel(100, 100, 1000, 1));
|
||||
}
|
||||
|
||||
private void setColors() {
|
||||
Color[] colors = new Color[8];
|
||||
colors[0] = Color.RED;
|
||||
colors[1] = Color.GREEN;
|
||||
colors[2] = Color.BLUE;
|
||||
colors[3] = Color.YELLOW;
|
||||
colors[4] = Color.WHITE;
|
||||
colors[5] = Color.GRAY;
|
||||
colors[6] = Color.BLACK;
|
||||
colors[7] = Color.MAGENTA;
|
||||
|
||||
int i = 0;
|
||||
int width = 40;
|
||||
for (Color color : colors) {
|
||||
JButton btn = new JButton();
|
||||
btn.setBackground(color);
|
||||
btn.setEnabled(false);
|
||||
int x = i % 4;
|
||||
int y = i / 4;
|
||||
|
||||
btn.setBounds(x * width + 10, y * width + 10, width, width);
|
||||
btn.setText(Integer.toString(color.getRGB()));
|
||||
btn.setFont(new Font("Arial", Font.PLAIN, 0));
|
||||
btn.setTransferHandler(new TransferHandler("text"));
|
||||
btn.addMouseListener(new DragMouseAdapter());
|
||||
colorsPanel.add(btn);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
public void addEvent(Method event, Object consumer) {
|
||||
if (delegator == null)
|
||||
{
|
||||
delegator = new Delegator<>();
|
||||
}
|
||||
delegator.add(event, consumer);
|
||||
}
|
||||
}
|
31
src/main/java/FormGenericArmoredCar.form
Normal file
31
src/main/java/FormGenericArmoredCar.form
Normal file
@ -0,0 +1,31 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="FormGenericArmoredCar">
|
||||
<grid id="27dc6" binding="mainPanel" custom-create="true" layout-manager="GridLayoutManager" row-count="2" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
|
||||
<margin top="0" left="0" bottom="0" right="0"/>
|
||||
<constraints>
|
||||
<xy x="20" y="20" width="833" height="459"/>
|
||||
</constraints>
|
||||
<properties/>
|
||||
<border type="none"/>
|
||||
<children>
|
||||
<component id="6837" class="javax.swing.JButton" binding="createButton">
|
||||
<constraints>
|
||||
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<text value="Создать объекты"/>
|
||||
</properties>
|
||||
</component>
|
||||
<hspacer id="8e062">
|
||||
<constraints>
|
||||
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
</hspacer>
|
||||
<vspacer id="31893">
|
||||
<constraints>
|
||||
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
</vspacer>
|
||||
</children>
|
||||
</grid>
|
||||
</form>
|
83
src/main/java/FormGenericArmoredCar.java
Normal file
83
src/main/java/FormGenericArmoredCar.java
Normal file
@ -0,0 +1,83 @@
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.util.Random;
|
||||
|
||||
public class FormGenericArmoredCar extends JFrame{
|
||||
private JButton createButton;
|
||||
private JPanel mainPanel;
|
||||
GenericEntityArmoredCar<EntityArmoredCar, IDrawingCaterpillar> genericEntityArmoredCar;
|
||||
DrawingArmoredCar[] cars = new DrawingArmoredCar[3];
|
||||
|
||||
public FormGenericArmoredCar() {
|
||||
super("Форма с новым объектом");
|
||||
|
||||
setBounds(100, 100, 1000, 700);
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
|
||||
fill();
|
||||
createButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
for (int i = 0; i < 3; i++) {
|
||||
cars[i] = genericEntityArmoredCar.getDrawingObject();
|
||||
}
|
||||
setData();
|
||||
|
||||
|
||||
}
|
||||
});
|
||||
setContentPane(mainPanel);
|
||||
setVisible(true);
|
||||
}
|
||||
private void fill() {
|
||||
genericEntityArmoredCar = new GenericEntityArmoredCar<>(10);
|
||||
for (int i = 0; i < 10; i++) {
|
||||
Random r = new Random();
|
||||
EntityArmoredCar armoredCar = new EntityArmoredCar(
|
||||
r.nextInt(1000, 2000),
|
||||
r.nextInt(1000, 2000),
|
||||
new Color(r.nextInt(256), r.nextInt(256), r.nextInt(256)));
|
||||
IDrawingCaterpillar[] realisations = new IDrawingCaterpillar[]{
|
||||
new DrawingCaterpillar(armoredCar.getBodyColor()),
|
||||
new DrawingCrossCaterpillar(armoredCar.getBodyColor()),
|
||||
new DrawingDoubleCaterpillar(armoredCar.getBodyColor())};
|
||||
IDrawingCaterpillar drawingCaterpillar = realisations[r.nextInt(3)];
|
||||
genericEntityArmoredCar.Insert(armoredCar);
|
||||
genericEntityArmoredCar.Insert(drawingCaterpillar);
|
||||
}
|
||||
}
|
||||
private void setData()
|
||||
{
|
||||
int x = mainPanel.getWidth() / 4;
|
||||
int y = mainPanel.getHeight() / 2;
|
||||
for (DrawingArmoredCar armoredCar :
|
||||
cars) {
|
||||
armoredCar.SetPosition(x, y, mainPanel.getWidth(), mainPanel.getHeight());
|
||||
x += 150;
|
||||
}
|
||||
}
|
||||
|
||||
private void DrawCars(Graphics2D g) {
|
||||
for (DrawingArmoredCar armoredCar : cars) {
|
||||
if (armoredCar == null)
|
||||
return;
|
||||
armoredCar.DrawTransport(g);
|
||||
}
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
new FormGenericArmoredCar();
|
||||
}
|
||||
|
||||
private void createUIComponents() {
|
||||
mainPanel = new JPanel() {
|
||||
@Override
|
||||
public void paintComponent(Graphics g) {
|
||||
super.paintComponent(g);
|
||||
DrawCars((Graphics2D) g);
|
||||
super.repaint();
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
129
src/main/java/FormMap.form
Normal file
129
src/main/java/FormMap.form
Normal file
@ -0,0 +1,129 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="FormMap">
|
||||
<grid id="27dc6" binding="mainPanel" layout-manager="GridLayoutManager" row-count="4" column-count="5" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
|
||||
<margin top="0" left="0" bottom="0" right="0"/>
|
||||
<constraints>
|
||||
<xy x="20" y="20" width="564" height="400"/>
|
||||
</constraints>
|
||||
<properties/>
|
||||
<border type="none"/>
|
||||
<children>
|
||||
<grid id="7cc9f" binding="drawPanel" custom-create="true" layout-manager="BorderLayout" hgap="0" vgap="0">
|
||||
<constraints>
|
||||
<grid row="1" column="0" row-span="1" col-span="5" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<background color="-1"/>
|
||||
</properties>
|
||||
<border type="none"/>
|
||||
<children/>
|
||||
</grid>
|
||||
<grid id="e4749" layout-manager="GridLayoutManager" row-count="2" column-count="5" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
|
||||
<margin top="0" left="0" bottom="0" right="0"/>
|
||||
<constraints>
|
||||
<grid row="3" column="0" row-span="1" col-span="5" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="true"/>
|
||||
</constraints>
|
||||
<properties/>
|
||||
<border type="none"/>
|
||||
<children>
|
||||
<component id="30a94" class="javax.swing.JButton" binding="buttonCreate">
|
||||
<constraints>
|
||||
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<text value="Создать"/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="e6875" class="javax.swing.JButton" binding="buttonRight">
|
||||
<constraints>
|
||||
<grid row="1" column="4" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<icon value="Right.png"/>
|
||||
<text value=""/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="81a7c" class="javax.swing.JButton" binding="buttonUp">
|
||||
<constraints>
|
||||
<grid row="0" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<enabled value="true"/>
|
||||
<hideActionText value="true"/>
|
||||
<icon value="Up.png"/>
|
||||
<text value=""/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="87a25" class="javax.swing.JButton" binding="buttonDown">
|
||||
<constraints>
|
||||
<grid row="1" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<icon value="Down.png"/>
|
||||
<text value=""/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="fee79" class="javax.swing.JButton" binding="buttonLeft">
|
||||
<constraints>
|
||||
<grid row="1" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<icon value="Left.png"/>
|
||||
<text value=""/>
|
||||
<visible value="true"/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="2d428" class="javax.swing.JButton" binding="buttonCreate_Modif">
|
||||
<constraints>
|
||||
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<text value="Модификация"/>
|
||||
</properties>
|
||||
</component>
|
||||
</children>
|
||||
</grid>
|
||||
<component id="54172" class="javax.swing.JLabel" binding="labelSpeed">
|
||||
<constraints>
|
||||
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<text value=""/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="725a5" class="javax.swing.JLabel" binding="labelWeight">
|
||||
<constraints>
|
||||
<grid row="2" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<text value=""/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="46832" class="javax.swing.JLabel" binding="labelColor">
|
||||
<constraints>
|
||||
<grid row="2" column="4" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<text value=""/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="625" class="javax.swing.JComboBox" binding="mapSelectorComboBox">
|
||||
<constraints>
|
||||
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<model>
|
||||
<item value="Простая карта"/>
|
||||
<item value="Карта 1"/>
|
||||
<item value="Карта 2"/>
|
||||
</model>
|
||||
</properties>
|
||||
</component>
|
||||
<hspacer id="9af30">
|
||||
<constraints>
|
||||
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
</hspacer>
|
||||
</children>
|
||||
</grid>
|
||||
</form>
|
149
src/main/java/FormMap.java
Normal file
149
src/main/java/FormMap.java
Normal file
@ -0,0 +1,149 @@
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.awt.event.ComponentAdapter;
|
||||
import java.awt.event.ComponentEvent;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.util.Objects;
|
||||
import java.util.Random;
|
||||
|
||||
public class FormMap extends JFrame {
|
||||
private JPanel drawPanel;
|
||||
private JButton buttonCreate;
|
||||
private JButton buttonRight;
|
||||
private JButton buttonUp;
|
||||
private JButton buttonDown;
|
||||
private JButton buttonLeft;
|
||||
private JButton buttonCreate_Modif;
|
||||
private JLabel labelSpeed;
|
||||
private JLabel labelWeight;
|
||||
private JLabel labelColor;
|
||||
private JComboBox mapSelectorComboBox;
|
||||
private JPanel mainPanel;
|
||||
|
||||
private AbstractMap abstractMap;
|
||||
private Image img;
|
||||
public FormMap() {
|
||||
super("Карта");
|
||||
abstractMap = new SimpleMap();
|
||||
|
||||
setBounds(100, 100, 700, 700);
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
|
||||
labelSpeed.setText("Скорость: ");
|
||||
labelWeight.setText("Вес: ");
|
||||
labelColor.setText("Цвет: ");
|
||||
|
||||
buttonCreate.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent actionEvent) {
|
||||
Random rnd = new Random();
|
||||
DrawingArmoredCar armoredCar = new DrawingArmoredCar(rnd.nextInt(100,300), rnd.nextInt(1000, 2000),
|
||||
new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)));
|
||||
setData(armoredCar);
|
||||
}
|
||||
});
|
||||
|
||||
buttonUp.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent actionEvent) {
|
||||
if (abstractMap != null) {
|
||||
img = abstractMap.MoveObject(Direction.Up);
|
||||
drawPanel.repaint();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
buttonDown.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent actionEvent) {
|
||||
if (abstractMap != null) {
|
||||
img = abstractMap.MoveObject(Direction.Down);
|
||||
drawPanel.repaint();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
buttonLeft.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent actionEvent) {
|
||||
if (abstractMap != null) {
|
||||
img = abstractMap.MoveObject(Direction.Left);
|
||||
drawPanel.repaint();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
buttonRight.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent actionEvent) {
|
||||
if (abstractMap != null) {
|
||||
img = abstractMap.MoveObject(Direction.Right);
|
||||
drawPanel.repaint();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
setContentPane(mainPanel);
|
||||
setVisible(true);
|
||||
buttonCreate_Modif.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
Random rnd = new Random();
|
||||
DrawingTank tank = new DrawingTank(rnd.nextInt(100, 300), rnd.nextInt(1000,
|
||||
2000),
|
||||
new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0,
|
||||
256)),
|
||||
new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0,
|
||||
256)),
|
||||
1==rnd.nextInt(0, 2), 1==rnd.nextInt(0, 2));
|
||||
setData(tank);
|
||||
}
|
||||
});
|
||||
mapSelectorComboBox.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
String name = (String) mapSelectorComboBox.getSelectedItem();
|
||||
switch (name)
|
||||
{
|
||||
case "Простая карта":
|
||||
abstractMap = new SimpleMap();
|
||||
break;
|
||||
case "Карта 1":
|
||||
abstractMap = new MyMapWooden();
|
||||
break;
|
||||
case "Карта 2":
|
||||
abstractMap = new MyMapLabyrinth();
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void createUIComponents() {
|
||||
drawPanel = new JPanel() {
|
||||
@Override
|
||||
public void paintComponent(Graphics g) {
|
||||
super.paintComponent(g);
|
||||
g.drawImage(img, 0, 0, this);
|
||||
super.repaint();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private void setData(DrawingArmoredCar drawingArmoredCar)
|
||||
{
|
||||
if (abstractMap != null)
|
||||
img = abstractMap.CreateMap(drawPanel.getWidth(), drawPanel.getHeight(), new DrawingObjectArmoredCar(drawingArmoredCar));
|
||||
|
||||
labelSpeed.setText("Скорость: " + drawingArmoredCar.getArmoredCar().getSpeed());
|
||||
labelWeight.setText("Вес: " + drawingArmoredCar.getArmoredCar().getWeight());
|
||||
labelColor.setText("Цвет: " + drawingArmoredCar.getArmoredCar().getBodyColor().getRGB());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
171
src/main/java/FormMapWithArmoredCars.form
Normal file
171
src/main/java/FormMapWithArmoredCars.form
Normal file
@ -0,0 +1,171 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="FormMapWithArmoredCars">
|
||||
<grid id="27dc6" binding="mainPanel" layout-manager="GridLayoutManager" row-count="2" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
|
||||
<margin top="0" left="0" bottom="0" right="0"/>
|
||||
<constraints>
|
||||
<xy x="20" y="20" width="947" height="629"/>
|
||||
</constraints>
|
||||
<properties/>
|
||||
<border type="none"/>
|
||||
<children>
|
||||
<grid id="d36a5" binding="btnPanel" layout-manager="GridLayoutManager" row-count="13" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
|
||||
<margin top="0" left="0" bottom="0" right="0"/>
|
||||
<constraints>
|
||||
<grid row="0" column="1" row-span="2" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<name value=""/>
|
||||
<toolTipText value=""/>
|
||||
</properties>
|
||||
<border type="none"/>
|
||||
<children>
|
||||
<component id="abbc4" class="javax.swing.JButton" binding="buttonAddArmoredCar">
|
||||
<constraints>
|
||||
<grid row="5" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<text value="Добавить бронемашину"/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="ab78e" class="javax.swing.JButton" binding="buttonRemoveArmoredCar">
|
||||
<constraints>
|
||||
<grid row="7" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<text value="Удалить бронемашину"/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="28eda" class="javax.swing.JButton" binding="buttonShowStorage">
|
||||
<constraints>
|
||||
<grid row="8" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<text value="Посмотреть хранилище"/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="633b9" class="javax.swing.JButton" binding="buttonShowOnMap">
|
||||
<constraints>
|
||||
<grid row="9" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<text value="Посмотреть карту"/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="30614" class="javax.swing.JTextField" binding="maskedTextBoxPosition">
|
||||
<constraints>
|
||||
<grid row="6" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
|
||||
<preferred-size width="150" height="-1"/>
|
||||
</grid>
|
||||
</constraints>
|
||||
<properties/>
|
||||
</component>
|
||||
<component id="4f2a5" class="javax.swing.JComboBox" binding="mapSelectorComboBox">
|
||||
<constraints>
|
||||
<grid row="1" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<model>
|
||||
<item value="Простая карта"/>
|
||||
<item value="Карта 1"/>
|
||||
<item value="Карта 2"/>
|
||||
</model>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="bf66e" class="javax.swing.JButton" binding="buttonUp">
|
||||
<constraints>
|
||||
<grid row="11" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<enabled value="true"/>
|
||||
<hideActionText value="true"/>
|
||||
<icon value="Up.png"/>
|
||||
<text value=""/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="56422" class="javax.swing.JButton" binding="buttonDown">
|
||||
<constraints>
|
||||
<grid row="12" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<icon value="Down.png"/>
|
||||
<text value=""/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="f47b8" class="javax.swing.JButton" binding="buttonRight">
|
||||
<constraints>
|
||||
<grid row="12" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<icon value="Right.png"/>
|
||||
<text value=""/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="a0cfb" class="javax.swing.JButton" binding="buttonLeft">
|
||||
<constraints>
|
||||
<grid row="12" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<icon value="Left.png"/>
|
||||
<text value=""/>
|
||||
<visible value="true"/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="b0b2e" class="javax.swing.JTextField" binding="textBoxNewMapName">
|
||||
<constraints>
|
||||
<grid row="0" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
|
||||
<preferred-size width="150" height="-1"/>
|
||||
</grid>
|
||||
</constraints>
|
||||
<properties/>
|
||||
</component>
|
||||
<component id="8adee" class="javax.swing.JButton" binding="AddMapButton">
|
||||
<constraints>
|
||||
<grid row="2" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<text value="Добавить карту"/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="e7216" class="javax.swing.JButton" binding="DeleteMapButton">
|
||||
<constraints>
|
||||
<grid row="4" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<text value="Удалить карту"/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="7099e" class="javax.swing.JList" binding="listBoxMaps">
|
||||
<constraints>
|
||||
<grid row="3" column="0" row-span="1" col-span="3" vsize-policy="6" hsize-policy="2" anchor="0" fill="3" indent="0" use-parent-layout="false">
|
||||
<preferred-size width="150" height="50"/>
|
||||
</grid>
|
||||
</constraints>
|
||||
<properties/>
|
||||
</component>
|
||||
<component id="ccba6" class="javax.swing.JButton" binding="lookDeletedButton">
|
||||
<constraints>
|
||||
<grid row="10" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<text value="Посмотреть удаленные объекты"/>
|
||||
</properties>
|
||||
</component>
|
||||
</children>
|
||||
</grid>
|
||||
<hspacer id="da837">
|
||||
<constraints>
|
||||
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
</hspacer>
|
||||
<grid id="e184c" binding="drawPanel" custom-create="true" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
|
||||
<margin top="0" left="0" bottom="0" right="0"/>
|
||||
<constraints>
|
||||
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties/>
|
||||
<border type="none"/>
|
||||
<children/>
|
||||
</grid>
|
||||
</children>
|
||||
</grid>
|
||||
</form>
|
310
src/main/java/FormMapWithArmoredCars.java
Normal file
310
src/main/java/FormMapWithArmoredCars.java
Normal file
@ -0,0 +1,310 @@
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.*;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.Queue;
|
||||
|
||||
public class FormMapWithArmoredCars extends JFrame{
|
||||
private JButton buttonAddArmoredCar;
|
||||
private JButton buttonRemoveArmoredCar;
|
||||
private JButton buttonShowStorage;
|
||||
private JButton buttonShowOnMap;
|
||||
private JTextField maskedTextBoxPosition;
|
||||
private JComboBox mapSelectorComboBox;
|
||||
private JPanel drawPanel;
|
||||
private JButton buttonUp;
|
||||
private JButton buttonDown;
|
||||
private JButton buttonRight;
|
||||
private JButton buttonLeft;
|
||||
private JPanel mainPanel;
|
||||
private JPanel btnPanel;
|
||||
private JTextField textBoxNewMapName;
|
||||
private JButton AddMapButton;
|
||||
private JButton DeleteMapButton;
|
||||
private JList listBoxMaps;
|
||||
private JButton lookDeletedButton;
|
||||
|
||||
private Image img;
|
||||
|
||||
private final HashMap<String, AbstractMap> _mapsDict = new HashMap<>() {{
|
||||
put("Простая карта", new SimpleMap());
|
||||
put("Карта 1", new MyMapLabyrinth());
|
||||
put("Карта 2", new MyMapWooden());
|
||||
}};
|
||||
|
||||
private final MapsCollection _mapsCollection;
|
||||
private final Queue<DrawingObjectArmoredCar> queue = new LinkedList<>();
|
||||
|
||||
public FormMapWithArmoredCars() {
|
||||
super("Карта с набором объектов");
|
||||
setBounds(100, 100, 1000, 725);
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
|
||||
JMenuBar jMenuBar = new JMenuBar();
|
||||
|
||||
JMenu jMenuMap = new JMenu("Файл");
|
||||
JMenuItem itemSave = new JMenuItem("Сохранение");
|
||||
itemSave.addActionListener(e -> saveActionMenuBar(jMenuMap.getText()));
|
||||
JMenuItem itemLoad = new JMenuItem("Загрузка");
|
||||
itemLoad.addActionListener(e -> loadActionMenuBar(jMenuMap.getText()));
|
||||
jMenuMap.add(itemSave);
|
||||
jMenuMap.add(itemLoad);
|
||||
|
||||
JMenu jMenuObject = new JMenu("Файл карты");
|
||||
JMenuItem itemSaveObject = new JMenuItem("Сохранение");
|
||||
itemSaveObject.addActionListener(e -> saveActionMenuBar(jMenuObject.getText()));
|
||||
JMenuItem itemLoadObject = new JMenuItem("Загрузка");
|
||||
itemLoadObject.addActionListener(e -> loadActionMenuBar(jMenuObject.getText()));
|
||||
jMenuObject.add(itemSaveObject);
|
||||
jMenuObject.add(itemLoadObject);
|
||||
|
||||
jMenuBar.add(jMenuMap);
|
||||
jMenuBar.add(jMenuObject);
|
||||
setJMenuBar(jMenuBar);
|
||||
|
||||
mapSelectorComboBox.removeAllItems();
|
||||
for (var elem : _mapsDict.keySet())
|
||||
{
|
||||
mapSelectorComboBox.addItem(elem);
|
||||
}
|
||||
|
||||
buttonAddArmoredCar.addActionListener(e -> {
|
||||
var form = new FormArmoredCarConfig();
|
||||
try {
|
||||
form.addEvent(FormMapWithArmoredCars.class.getDeclaredMethod("AddArmoredCar", DrawingArmoredCar.class), this);
|
||||
} catch (NoSuchMethodException noSuchMethodException) {
|
||||
noSuchMethodException.printStackTrace();
|
||||
}
|
||||
});
|
||||
buttonRemoveArmoredCar.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if (maskedTextBoxPosition.getText().equals(""))
|
||||
{
|
||||
return;
|
||||
}
|
||||
int res = JOptionPane.showConfirmDialog (null,"Удалить объект?",
|
||||
"Удаление", JOptionPane.YES_NO_OPTION);
|
||||
if (res == JOptionPane.YES_OPTION) {
|
||||
int pos = Integer.parseInt(maskedTextBoxPosition.getText());
|
||||
DrawingObjectArmoredCar deletedArmoredCar = (DrawingObjectArmoredCar) _mapsCollection.get((String) listBoxMaps.getSelectedValue(), pos);
|
||||
if (_mapsCollection.get((String) listBoxMaps.getSelectedValue()).remove(pos) != null)
|
||||
{
|
||||
queue.add(deletedArmoredCar);
|
||||
JOptionPane.showMessageDialog(null, "Объект удален");
|
||||
img = _mapsCollection.get((String) listBoxMaps.getSelectedValue()).ShowSet();
|
||||
}
|
||||
else
|
||||
{
|
||||
JOptionPane.showMessageDialog(null, "Не удалось удалить объект");
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
buttonShowStorage.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if (_mapsCollection.get((String) listBoxMaps.getSelectedValue()) == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
img = _mapsCollection.get((String) listBoxMaps.getSelectedValue()).ShowSet();
|
||||
repaint();
|
||||
}
|
||||
});
|
||||
|
||||
buttonShowOnMap.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if (_mapsCollection.get((String) listBoxMaps.getSelectedValue()) == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
img = _mapsCollection.get((String) listBoxMaps.getSelectedValue()).ShowOnMap();
|
||||
repaint();
|
||||
}
|
||||
});
|
||||
|
||||
setContentPane(mainPanel);
|
||||
setVisible(true);
|
||||
buttonLeft.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
img = _mapsCollection.get((String) listBoxMaps.getSelectedValue()).MoveObject(Direction.Left);
|
||||
repaint();
|
||||
}
|
||||
});
|
||||
buttonUp.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
img = _mapsCollection.get((String) listBoxMaps.getSelectedValue()).MoveObject(Direction.Up);
|
||||
repaint();
|
||||
}
|
||||
});
|
||||
buttonDown.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
img = _mapsCollection.get((String) listBoxMaps.getSelectedValue()).MoveObject(Direction.Down);
|
||||
repaint();
|
||||
}
|
||||
});
|
||||
buttonRight.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
img = _mapsCollection.get((String) listBoxMaps.getSelectedValue()).MoveObject(Direction.Right);
|
||||
repaint();
|
||||
}
|
||||
});
|
||||
AddMapButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if (mapSelectorComboBox.getSelectedIndex() == -1 || textBoxNewMapName.getText().equals(""))
|
||||
{
|
||||
JOptionPane.showMessageDialog(null, "Не все данные заполнены", "Ошибка", JOptionPane.ERROR_MESSAGE);
|
||||
return;
|
||||
}
|
||||
if (!_mapsDict.containsKey((String) mapSelectorComboBox.getSelectedItem()))
|
||||
{
|
||||
JOptionPane.showMessageDialog(null, "Нет такой карты", "Ошибка", JOptionPane.ERROR_MESSAGE);
|
||||
return;
|
||||
}
|
||||
_mapsCollection.AddMap(textBoxNewMapName.getText(), _mapsDict.get((String) mapSelectorComboBox.getSelectedItem()));
|
||||
ReloadMaps();
|
||||
}
|
||||
});
|
||||
_mapsCollection = new MapsCollection(drawPanel.getWidth(), drawPanel.getHeight());
|
||||
|
||||
listBoxMaps.addListSelectionListener(e -> {
|
||||
if (listBoxMaps.getSelectedValue() != null) {
|
||||
img = _mapsCollection.get((String) listBoxMaps.getSelectedValue()).ShowSet();
|
||||
repaint();
|
||||
}
|
||||
});
|
||||
|
||||
DeleteMapButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if (listBoxMaps.getSelectedIndex() == -1)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (JOptionPane.showConfirmDialog(null, "Удалить карту" +
|
||||
listBoxMaps.getSelectedValue()+ "?", "Удаление", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)
|
||||
{
|
||||
_mapsCollection.DelMap((String) listBoxMaps.getSelectedValue());
|
||||
ReloadMaps();
|
||||
}
|
||||
}
|
||||
});
|
||||
lookDeletedButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
|
||||
if (!queue.isEmpty()) {
|
||||
FormArmoredCar form = new FormArmoredCar();
|
||||
form.setArmoredCar(queue.poll().getArmoredCar());
|
||||
}
|
||||
else
|
||||
JOptionPane.showMessageDialog(null, "Нет удаленных объектов");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void createUIComponents() {
|
||||
drawPanel = new JPanel() {
|
||||
@Override
|
||||
public void paintComponent(Graphics g) {
|
||||
super.paintComponent(g);
|
||||
g.drawImage(img, 0, 0, this);
|
||||
super.repaint();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private void ReloadMaps() {
|
||||
int index = listBoxMaps.getSelectedIndex();
|
||||
listBoxMaps.setListData(_mapsCollection.getKeys().toArray(new Object[0]));
|
||||
if (listBoxMaps.getModel().getSize() > 0 && (index == -1 || index >=
|
||||
listBoxMaps.getModel().getSize()))
|
||||
{
|
||||
listBoxMaps.setSelectedIndex(0);
|
||||
}
|
||||
else if (listBoxMaps.getModel().getSize() > 0 && index > -1 && index <
|
||||
listBoxMaps.getModel().getSize())
|
||||
{
|
||||
listBoxMaps.setSelectedIndex(index);
|
||||
}
|
||||
}
|
||||
|
||||
public void AddArmoredCar(DrawingArmoredCar drawingArmoredCar) {
|
||||
if (listBoxMaps.getSelectedIndex() == -1)
|
||||
{
|
||||
return;
|
||||
}
|
||||
DrawingObjectArmoredCar armoredCar = new DrawingObjectArmoredCar(drawingArmoredCar);
|
||||
if (_mapsCollection.get((String)listBoxMaps.getSelectedValue()).add(armoredCar) > -1)
|
||||
{
|
||||
JOptionPane.showMessageDialog(null, "Объект добавлен");
|
||||
img = _mapsCollection.get((String) listBoxMaps.getSelectedValue()).ShowSet();
|
||||
repaint();
|
||||
}
|
||||
else
|
||||
{
|
||||
JOptionPane.showMessageDialog(null, "Не удалось добавить объект");
|
||||
}
|
||||
}
|
||||
|
||||
private void saveActionMenuBar(String type) {
|
||||
FileDialog fd = new FileDialog(this, "Выбор файла", FileDialog.LOAD);
|
||||
fd.setFile("*.txt");
|
||||
fd.setVisible(true);
|
||||
String filename = fd.getFile();
|
||||
if (filename != null) {
|
||||
if (type.equals("Файл")) {
|
||||
if (_mapsCollection.SaveData(fd.getDirectory() + filename))
|
||||
{
|
||||
JOptionPane.showMessageDialog(null, "Сохранение прошло успешно");
|
||||
}
|
||||
else {
|
||||
JOptionPane.showMessageDialog(null, "Не сохранилось");
|
||||
}
|
||||
} else if (type.equals("Файл карты")){
|
||||
if (_mapsCollection.SaveDataMap(fd.getDirectory() + filename, (String) listBoxMaps.getSelectedValue())) {
|
||||
JOptionPane.showMessageDialog(null, "Сохранение прошло успешно");
|
||||
} else {
|
||||
JOptionPane.showMessageDialog(null, "Не сохранилось");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void loadActionMenuBar(String type) {
|
||||
FileDialog fd = new FileDialog(this, "Выбор файла", FileDialog.LOAD);
|
||||
fd.setFile("*.txt");
|
||||
fd.setVisible(true);
|
||||
String filename = fd.getFile();
|
||||
if (filename != null) {
|
||||
if (type.equals("Файл")) {
|
||||
if (_mapsCollection.LoadData(fd.getDirectory() + filename))
|
||||
{
|
||||
JOptionPane.showMessageDialog(null, "Загрузка прошла успешно");
|
||||
ReloadMaps();
|
||||
}
|
||||
else {
|
||||
JOptionPane.showMessageDialog(null, "Не загрузилось");
|
||||
}
|
||||
} else if (type.equals("Файл карты")){
|
||||
if (_mapsCollection.LoadDataMap(fd.getDirectory() + filename)) {
|
||||
JOptionPane.showMessageDialog(null, "Загрузка прошла успешно");
|
||||
ReloadMaps();
|
||||
} else {
|
||||
JOptionPane.showMessageDialog(null, "Не загрузилось");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
36
src/main/java/GenericEntityArmoredCar.java
Normal file
36
src/main/java/GenericEntityArmoredCar.java
Normal file
@ -0,0 +1,36 @@
|
||||
import java.util.Random;
|
||||
|
||||
public class GenericEntityArmoredCar<T extends EntityArmoredCar, U extends IDrawingCaterpillar> {
|
||||
private T[] armoredCars;
|
||||
private int carsCount = 0;
|
||||
private U[] caterpillars;
|
||||
private int caterpillarsCount = 0;
|
||||
|
||||
public GenericEntityArmoredCar(int numberObjects) {
|
||||
this.armoredCars = (T[]) new EntityArmoredCar[numberObjects];
|
||||
this.caterpillars = (U[]) new IDrawingCaterpillar[numberObjects];
|
||||
}
|
||||
|
||||
public void Insert(T armoredCar)
|
||||
{
|
||||
if (carsCount >= armoredCars.length)
|
||||
return;
|
||||
armoredCars[carsCount++] = armoredCar;
|
||||
}
|
||||
|
||||
public void Insert(U caterpillar)
|
||||
{
|
||||
if (caterpillarsCount >= caterpillars.length)
|
||||
return;
|
||||
caterpillars[caterpillarsCount++] = caterpillar;
|
||||
}
|
||||
|
||||
public DrawingArmoredCar getDrawingObject() {
|
||||
Random r = new Random();
|
||||
EntityArmoredCar armoredCar = armoredCars[r.nextInt(carsCount)];
|
||||
IDrawingCaterpillar caterpillar = caterpillars[r.nextInt(caterpillarsCount)];
|
||||
DrawingArmoredCar o = new DrawingArmoredCar(armoredCar.getSpeed(), armoredCar.getWeight(), armoredCar.getBodyColor());
|
||||
o.SetCaterpillar(caterpillar);
|
||||
return o;
|
||||
}
|
||||
}
|
14
src/main/java/IDrawingCaterpillar.java
Normal file
14
src/main/java/IDrawingCaterpillar.java
Normal file
@ -0,0 +1,14 @@
|
||||
import java.awt.*;
|
||||
|
||||
public interface IDrawingCaterpillar {
|
||||
void setNumRinks(int n);
|
||||
|
||||
void DrawCaterpillar(Graphics2D g2d, int startPosX, int startPosY);
|
||||
|
||||
void setColor(Color color);
|
||||
|
||||
Color getColor();
|
||||
|
||||
NumRinks getNumRinks();
|
||||
|
||||
}
|
16
src/main/java/IDrawingObject.java
Normal file
16
src/main/java/IDrawingObject.java
Normal file
@ -0,0 +1,16 @@
|
||||
import java.awt.*;
|
||||
import java.util.HashMap;
|
||||
|
||||
public interface IDrawingObject {
|
||||
float getStep();
|
||||
|
||||
void SetObject(int x, int y, int width, int height);
|
||||
|
||||
void MoveObject(Direction direction);
|
||||
|
||||
void DrawingObject(Graphics2D g);
|
||||
|
||||
HashMap<String, Float> GetCurrentPosition();
|
||||
|
||||
String GetInfo();
|
||||
}
|
175
src/main/java/MapWithSetArmoredCarsGeneric.java
Normal file
175
src/main/java/MapWithSetArmoredCarsGeneric.java
Normal file
@ -0,0 +1,175 @@
|
||||
import java.awt.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
|
||||
public class MapWithSetArmoredCarsGeneric<T extends IDrawingObject, U extends AbstractMap> {
|
||||
|
||||
private int _pictureWidth;
|
||||
|
||||
private int _pictureHeight;
|
||||
|
||||
private int _placeSizeWidth = 210;
|
||||
|
||||
private int _placeSizeHeight = 110;
|
||||
|
||||
private SetArmoredCarsGeneric<T> _setCars;
|
||||
|
||||
private U _map;
|
||||
|
||||
public MapWithSetArmoredCarsGeneric(int picWidth, int picHeight, U map)
|
||||
{
|
||||
int width = picWidth / _placeSizeWidth;
|
||||
int height = picHeight / _placeSizeHeight;
|
||||
_setCars = new SetArmoredCarsGeneric<T>(width * height);
|
||||
_pictureWidth = picWidth;
|
||||
_pictureHeight = picHeight;
|
||||
_map = map;
|
||||
}
|
||||
|
||||
public int add(T car)
|
||||
{
|
||||
return _setCars.Insert(car);
|
||||
}
|
||||
|
||||
public T remove(int position)
|
||||
{
|
||||
return _setCars.Remove(position);
|
||||
}
|
||||
|
||||
public Image ShowSet()
|
||||
{
|
||||
Image bmp = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_BGR);
|
||||
Graphics gr = bmp.getGraphics();
|
||||
DrawBackground(gr);
|
||||
DrawArmoredCars(gr);
|
||||
return bmp;
|
||||
}
|
||||
|
||||
public Image ShowOnMap()
|
||||
{
|
||||
Shaking();
|
||||
for (var armoredCar : _setCars.GetArmoredCars())
|
||||
{
|
||||
return _map.CreateMap(_pictureWidth, _pictureHeight, armoredCar);
|
||||
}
|
||||
return new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_BGR);
|
||||
}
|
||||
|
||||
public Image MoveObject(Direction direction)
|
||||
{
|
||||
if (_map != null)
|
||||
{
|
||||
return _map.MoveObject(direction);
|
||||
}
|
||||
return new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_BGR);
|
||||
}
|
||||
|
||||
private void Shaking()
|
||||
{
|
||||
int j = _setCars.getCount() - 1;
|
||||
for (int i = 0; i < _setCars.getCount(); i++)
|
||||
{
|
||||
if (_setCars.Get(i) == null)
|
||||
{
|
||||
for (; j > i; j--)
|
||||
{
|
||||
var car = _setCars.Get(j);
|
||||
if (car != null)
|
||||
{
|
||||
_setCars.Insert(car, i);
|
||||
_setCars.Remove(j);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (j <= i)
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void DrawBackground(Graphics g)
|
||||
{
|
||||
Color colorRoad = Color.GRAY;
|
||||
Color colorBox = new Color(110, 69, 19, 255);
|
||||
for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++)
|
||||
{
|
||||
for (int j = 0; j < _pictureHeight / _placeSizeHeight; j++)
|
||||
{
|
||||
g.setColor(colorRoad);
|
||||
g.fillRect(i * _placeSizeWidth, j * _placeSizeHeight, _placeSizeWidth, _placeSizeHeight);
|
||||
|
||||
g.setColor(colorBox);
|
||||
int boxSize = _placeSizeWidth / 7;
|
||||
g.fillRect(i * _placeSizeWidth + _placeSizeWidth / 2,
|
||||
j * _placeSizeHeight + _placeSizeHeight / 5, boxSize, boxSize);
|
||||
|
||||
g.fillRect(i * _placeSizeWidth + _placeSizeWidth / 2 - boxSize / 2,
|
||||
j * _placeSizeHeight + _placeSizeHeight / 5 + boxSize, boxSize, boxSize);
|
||||
|
||||
g.fillRect(i * _placeSizeWidth + _placeSizeWidth / 2 + boxSize / 2,
|
||||
j * _placeSizeHeight + _placeSizeHeight / 5 + boxSize, boxSize, boxSize);
|
||||
|
||||
g.setColor(Color.BLACK);
|
||||
g.drawLine(i * _placeSizeWidth, j * _placeSizeHeight,
|
||||
i * _placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight);
|
||||
|
||||
g.drawRect(i * _placeSizeWidth + _placeSizeWidth / 2,
|
||||
j * _placeSizeHeight + _placeSizeHeight / 5, boxSize, boxSize);
|
||||
|
||||
g.drawRect( i * _placeSizeWidth + _placeSizeWidth / 2 - boxSize / 2,
|
||||
j * _placeSizeHeight + _placeSizeHeight / 5 + boxSize, boxSize, boxSize);
|
||||
|
||||
g.drawRect( i * _placeSizeWidth + _placeSizeWidth / 2 + boxSize / 2,
|
||||
j * _placeSizeHeight + _placeSizeHeight / 5 + boxSize, boxSize, boxSize);
|
||||
}
|
||||
g.drawLine(i * _placeSizeWidth, 0, i * _placeSizeWidth,
|
||||
(_pictureHeight / _placeSizeHeight) * _placeSizeHeight);
|
||||
}
|
||||
}
|
||||
|
||||
private void DrawArmoredCars(Graphics g)
|
||||
{
|
||||
int width = _pictureWidth / _placeSizeWidth;
|
||||
int i = 0;
|
||||
for (var armoredCar : _setCars.GetArmoredCars())
|
||||
{
|
||||
armoredCar.SetObject(i % width * _placeSizeWidth + 5, i / width * _placeSizeHeight + 10, _pictureWidth, _pictureHeight);
|
||||
armoredCar.DrawingObject((Graphics2D) g);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
public SetArmoredCarsGeneric<T> get_setCars() {
|
||||
return _setCars;
|
||||
}
|
||||
|
||||
public void removeAllCars() {
|
||||
_setCars.Clear();
|
||||
}
|
||||
|
||||
public String GetData(char separatorType, char separatorData)
|
||||
{
|
||||
String data = String.format("%s%s", _map.getClass().getName(), separatorType);
|
||||
ArrayList<T> reverse = new ArrayList<T>();
|
||||
_setCars.GetArmoredCars().forEach(reverse::add);
|
||||
Collections.reverse(reverse);
|
||||
for (var armoredCar : reverse)
|
||||
{
|
||||
data += String.format("%s%s", armoredCar.GetInfo(), separatorData);
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
public void LoadData(String[] records)
|
||||
{
|
||||
for(var rec : records)
|
||||
{
|
||||
_setCars.Insert((T)DrawingObjectArmoredCar.Create(rec));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
165
src/main/java/MapsCollection.java
Normal file
165
src/main/java/MapsCollection.java
Normal file
@ -0,0 +1,165 @@
|
||||
import java.io.*;
|
||||
import java.nio.file.Files;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
public class MapsCollection {
|
||||
public HashMap<String, MapWithSetArmoredCarsGeneric<IDrawingObject, AbstractMap>> _mapStorages;
|
||||
private int _pictureWidth;
|
||||
private int _pictureHeight;
|
||||
|
||||
private final char separatorDict = '#';
|
||||
private final char separatorData = ';';
|
||||
|
||||
public MapsCollection(int pictureWidth, int pictureHeight)
|
||||
{
|
||||
_mapStorages = new HashMap<>();
|
||||
_pictureWidth = pictureWidth;
|
||||
_pictureHeight = pictureHeight;
|
||||
}
|
||||
|
||||
public Set<String> getKeys() {
|
||||
return _mapStorages.keySet();
|
||||
}
|
||||
|
||||
public void AddMap(String name, AbstractMap map)
|
||||
{
|
||||
if (!getKeys().contains(name))
|
||||
_mapStorages.put(name, new MapWithSetArmoredCarsGeneric<>(_pictureWidth, _pictureHeight, map));
|
||||
}
|
||||
|
||||
public void DelMap(String name)
|
||||
{
|
||||
if (getKeys().contains(name))
|
||||
_mapStorages.remove(name);
|
||||
}
|
||||
|
||||
public MapWithSetArmoredCarsGeneric<IDrawingObject, AbstractMap> get(String ind)
|
||||
{
|
||||
if (getKeys().contains(ind))
|
||||
return _mapStorages.get(ind);
|
||||
return null;
|
||||
}
|
||||
// дополнительное задание: метод "индексатор" с двумя параметрами
|
||||
public IDrawingObject get(String ind1, int pos) {
|
||||
if (_mapStorages.containsKey(ind1))
|
||||
return _mapStorages.get(ind1).get_setCars().Get(pos);
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean SaveData(String filename)
|
||||
{
|
||||
File f = new File(filename);
|
||||
if (f.exists())
|
||||
{
|
||||
f.delete();
|
||||
}
|
||||
try (BufferedWriter writter = new BufferedWriter(new FileWriter(filename))) {
|
||||
writter.write("MapsCollection");
|
||||
writter.newLine();
|
||||
for (var key : _mapStorages.keySet())
|
||||
{
|
||||
writter.write(String.format("%s%s%s", key, separatorDict, _mapStorages.get(key).GetData(separatorDict, separatorData)));
|
||||
writter.newLine();
|
||||
}
|
||||
}
|
||||
catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean LoadData(String filename) {
|
||||
File f = new File(filename);
|
||||
if (!f.exists()) {
|
||||
return false;
|
||||
}
|
||||
try (BufferedReader reader = new BufferedReader(new FileReader(filename))) {
|
||||
if (!reader.readLine().contains("MapsCollection"))
|
||||
return false;
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
var elem = line.split(String.valueOf(separatorDict), -1);
|
||||
AbstractMap map = null;
|
||||
switch (elem[1]) {
|
||||
case "SimpleMap":
|
||||
map = new SimpleMap();
|
||||
break;
|
||||
case "MyMapLabyrinth":
|
||||
map = new MyMapLabyrinth();
|
||||
break;
|
||||
case "MyMapWooden":
|
||||
map = new MyMapWooden();
|
||||
break;
|
||||
}
|
||||
_mapStorages.put(elem[0], new MapWithSetArmoredCarsGeneric<>(_pictureWidth, _pictureHeight, map));
|
||||
_mapStorages.get(elem[0]).LoadData(elem[2].split(String.valueOf(separatorData)));
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean SaveDataMap(String filename, String map) {
|
||||
if (!_mapStorages.containsKey(map))
|
||||
return false;
|
||||
File f = new File(filename);
|
||||
if (f.exists())
|
||||
{
|
||||
f.delete();
|
||||
}
|
||||
try (BufferedWriter writter = new BufferedWriter(new FileWriter(filename))) {
|
||||
writter.write("One Map");
|
||||
writter.newLine();
|
||||
var key = _mapStorages.get(map);
|
||||
writter.write(String.format("%s%s%s", map, separatorDict, key.GetData(separatorDict, separatorData)));
|
||||
writter.newLine();
|
||||
|
||||
}
|
||||
catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean LoadDataMap(String filename) {
|
||||
File f = new File(filename);
|
||||
if (!f.exists()) {
|
||||
return false;
|
||||
}
|
||||
try (BufferedReader reader = new BufferedReader(new FileReader(filename))) {
|
||||
if (!reader.readLine().contains("One Map"))
|
||||
return false;
|
||||
String line = reader.readLine();
|
||||
var elem = line.split(String.valueOf(separatorDict), -1);
|
||||
AbstractMap map = null;
|
||||
switch (elem[1]) {
|
||||
case "SimpleMap":
|
||||
map = new SimpleMap();
|
||||
break;
|
||||
case "MyMapLabyrinth":
|
||||
map = new MyMapLabyrinth();
|
||||
break;
|
||||
case "MyMapWooden":
|
||||
map = new MyMapWooden();
|
||||
break;
|
||||
}
|
||||
if (_mapStorages.containsKey(elem[0])) {
|
||||
_mapStorages.get(elem[0]).removeAllCars();
|
||||
}
|
||||
else
|
||||
_mapStorages.put(elem[0], new MapWithSetArmoredCarsGeneric<>(_pictureWidth, _pictureHeight, map));
|
||||
_mapStorages.get(elem[0]).LoadData(elem[2].split(String.valueOf(separatorData)));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
65
src/main/java/MyMapLabyrinth.java
Normal file
65
src/main/java/MyMapLabyrinth.java
Normal file
@ -0,0 +1,65 @@
|
||||
import java.awt.*;
|
||||
|
||||
public class MyMapLabyrinth extends AbstractMap{
|
||||
private final Color barrierColor = Color.BLUE;
|
||||
private final Color roadColor = Color.BLACK;
|
||||
@Override
|
||||
protected void GenerateMap() {
|
||||
map = new int[50][50];
|
||||
size_x = (float)width / map.length;
|
||||
size_y = (float)height / map[0].length;
|
||||
int counter = 0;
|
||||
for (int i = 0; i < map.length; ++i)
|
||||
{
|
||||
for (int j = 0; j < map[0].length; ++j)
|
||||
{
|
||||
map[i][j] = _freeRoad;
|
||||
}
|
||||
}
|
||||
|
||||
while (counter < 20)
|
||||
{
|
||||
int x = random.nextInt(0, 50);
|
||||
int y = random.nextInt(0, 50);
|
||||
int number = random.nextInt(1, 16);
|
||||
if (counter < 5) {
|
||||
if (number + x > map.length)
|
||||
{
|
||||
number = map.length - x - 1;
|
||||
}
|
||||
for (int i = x; i < x + number; ++i)
|
||||
{
|
||||
map[i][y] = _barrier;
|
||||
}
|
||||
} else
|
||||
{
|
||||
if (number + y > map[0].length)
|
||||
{
|
||||
number = map[0].length - y - 1;
|
||||
}
|
||||
for (int i = y; i < y + number; ++i)
|
||||
{
|
||||
map[x][i] = _barrier;
|
||||
}
|
||||
}
|
||||
counter++;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void DrawRoadPart(Graphics2D g, int i, int j) {
|
||||
g.setColor(roadColor);
|
||||
int new_size_x = Math.round(size_x);
|
||||
int new_size_y = Math.round(size_y);
|
||||
g.fillRect(i * new_size_x, j * new_size_y, new_size_x, new_size_y);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void DrawBarrierPart(Graphics2D g, int i, int j) {
|
||||
g.setColor(barrierColor);
|
||||
int new_size_x = Math.round(size_x);
|
||||
int new_size_y = Math.round(size_y);
|
||||
g.fillRect(i * new_size_x, j * new_size_y, new_size_x, new_size_y);
|
||||
}
|
||||
}
|
46
src/main/java/MyMapWooden.java
Normal file
46
src/main/java/MyMapWooden.java
Normal file
@ -0,0 +1,46 @@
|
||||
import java.awt.*;
|
||||
|
||||
public class MyMapWooden extends AbstractMap{
|
||||
private final Color barrierColor = new Color(45, 77, 44);
|
||||
private final Color roadColor = new Color(105, 70, 35);
|
||||
@Override
|
||||
protected void GenerateMap() {
|
||||
map = new int[10][10];
|
||||
size_x = (float)width / map.length;
|
||||
size_y = (float)height / map[0].length;
|
||||
int counter = 0;
|
||||
for (int i = 0; i < map.length; ++i)
|
||||
{
|
||||
for (int j = 0; j < map[0].length; ++j)
|
||||
{
|
||||
map[i][j] = _freeRoad;
|
||||
}
|
||||
}
|
||||
while (counter < 10)
|
||||
{
|
||||
int x = random.nextInt(10);
|
||||
int y = random.nextInt(10);
|
||||
if (map[x][y] == _freeRoad)
|
||||
{
|
||||
map[x][y] = _barrier;
|
||||
counter++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void DrawRoadPart(Graphics2D g, int i, int j) {
|
||||
g.setColor(roadColor);
|
||||
int new_size_x = Math.round(size_x);
|
||||
int new_size_y = Math.round(size_y);
|
||||
g.fillRect(i * new_size_x, j * new_size_y, new_size_x, new_size_y);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void DrawBarrierPart(Graphics2D g, int i, int j) {
|
||||
g.setColor(barrierColor);
|
||||
int new_size_x = Math.round(size_x);
|
||||
int new_size_y = Math.round(size_y);
|
||||
g.fillRect(i * new_size_x, j * new_size_y, new_size_x, new_size_y);
|
||||
}
|
||||
}
|
@ -1,5 +1,5 @@
|
||||
public class Program {
|
||||
public static void main(String[] args) {
|
||||
new FormArmoredCar();
|
||||
new FormMapWithArmoredCars();
|
||||
}
|
||||
}
|
||||
|
55
src/main/java/SetArmoredCarsGeneric.java
Normal file
55
src/main/java/SetArmoredCarsGeneric.java
Normal file
@ -0,0 +1,55 @@
|
||||
import java.util.*;
|
||||
|
||||
public class SetArmoredCarsGeneric<T> {
|
||||
private ArrayList<T> _places;
|
||||
private int maxCount;
|
||||
|
||||
public SetArmoredCarsGeneric(int count)
|
||||
{
|
||||
_places = new ArrayList<T>();
|
||||
maxCount = count;
|
||||
}
|
||||
|
||||
public int getCount() {
|
||||
return _places != null ? _places.size() : 0;
|
||||
}
|
||||
|
||||
public int Insert(T armoredCar)
|
||||
{
|
||||
return Insert(armoredCar, 0);
|
||||
}
|
||||
|
||||
public int Insert(T armoredCar, int position)
|
||||
{
|
||||
if (getCount() >= maxCount || position < 0 || position >= maxCount)
|
||||
return -1;
|
||||
|
||||
_places.add(position, armoredCar);
|
||||
return position;
|
||||
}
|
||||
|
||||
public T Remove(int position)
|
||||
{
|
||||
if (position < 0 || position >= _places.size())
|
||||
return null;
|
||||
T armoredCar = _places.get(position);
|
||||
_places.remove(position);
|
||||
return armoredCar;
|
||||
}
|
||||
|
||||
public T Get(int position)
|
||||
{
|
||||
if (position < 0 || position >= maxCount)
|
||||
return null;
|
||||
return _places.get(position);
|
||||
}
|
||||
|
||||
public Iterable<T> GetArmoredCars()
|
||||
{
|
||||
return () -> _places.stream().filter(Objects::nonNull).iterator();
|
||||
}
|
||||
|
||||
public void Clear() {
|
||||
_places.clear();
|
||||
}
|
||||
}
|
46
src/main/java/SimpleMap.java
Normal file
46
src/main/java/SimpleMap.java
Normal file
@ -0,0 +1,46 @@
|
||||
import java.awt.*;
|
||||
|
||||
public class SimpleMap extends AbstractMap{
|
||||
private final Color barrierColor = Color.BLACK;
|
||||
private final Color roadColor = Color.DARK_GRAY;
|
||||
@Override
|
||||
protected void GenerateMap() {
|
||||
map = new int[100][100];
|
||||
size_x = (float)width / map.length;
|
||||
size_y = (float)height / map[0].length;
|
||||
int counter = 0;
|
||||
for (int i = 0; i < map.length; ++i)
|
||||
{
|
||||
for (int j = 0; j < map[0].length; ++j)
|
||||
{
|
||||
map[i][j] = _freeRoad;
|
||||
}
|
||||
}
|
||||
while (counter < 50)
|
||||
{
|
||||
int x = random.nextInt(100);
|
||||
int y = random.nextInt(100);
|
||||
if (map[x][y] == _freeRoad)
|
||||
{
|
||||
map[x][y] = _barrier;
|
||||
counter++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void DrawRoadPart(Graphics2D g, int i, int j) {
|
||||
g.setColor(roadColor);
|
||||
int new_size_x = Math.round(size_x);
|
||||
int new_size_y = Math.round(size_y);
|
||||
g.fillRect(i * new_size_x, j * new_size_y, new_size_x, new_size_y);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void DrawBarrierPart(Graphics2D g, int i, int j) {
|
||||
g.setColor(barrierColor);
|
||||
int new_size_x = Math.round(size_x);
|
||||
int new_size_y = Math.round(size_y);
|
||||
g.fillRect(i * new_size_x, j * new_size_y, new_size_x, new_size_y);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user