Compare commits

...

15 Commits

33 changed files with 2851 additions and 219 deletions

4
.idea/misc.xml generated
View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_11" default="true" project-jdk-name="11.0.5" project-jdk-type="JavaSDK"> <component name="ProjectRootManager" version="2" languageLevel="JDK_19" project-jdk-name="openjdk-19" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/../../../ирино, не трогат/!/!/!/!/RPP/ProjectAircraftCarrierHard/out" /> <output url="file://$PROJECT_DIR$/out" />
</component> </component>
</project> </project>

150
src/AbstractMap.java Normal file
View File

@ -0,0 +1,150 @@
import java.awt.*;
import java.awt.image.BufferedImage;
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 BufferedImage CreateMap(int width, int height, IDrawingObject drawningObject)
{
_width = width;
_height = height;
_drawingObject = drawningObject;
GenerateMap();
while (!SetObjectOnMap())
{
GenerateMap();
}
return DrawMapWithObject();
}
public BufferedImage MoveObject(Direction direction)
{
if(_drawingObject!=null){
float[] cortege = _drawingObject.GetCurrentPosition();
if (Check(cortege[0],cortege[1],cortege[2],cortege[3]) != 0)
{
_drawingObject.MoveObject(GetOpositDirection(direction));
}
if(true){
_drawingObject.MoveObject(direction);
}
return DrawMapWithObject();
}
return null;
}
private Direction GetOpositDirection(Direction dir)
{
switch (dir)
{
case None:
return Direction.None;
case Up:
return Direction.Down;
case Down:
return Direction.Up;
case Left:
return Direction.Right;
case Right:
return Direction.Left;
}
return Direction.None;
}
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);
float[] cortege = _drawingObject.GetCurrentPosition();
while (Check(cortege[0], cortege[1], cortege[2], cortege[3]) != 2)
{
int result;
do
{
result = Check(cortege[0], cortege[1], cortege[2], cortege[3]);
if (result == 0)
{
_drawingObject.SetObject((int)cortege[0], (int)cortege[1], _width, _height);
return true;
}
else
{
cortege[0] += _size_x;
}
} while (result != 2);
cortege[0] = x;
cortege[1] += _size_y;
}
return false;
}
private int Check(float Left, float Right, float Top, float Bottom)
{
int startX = (int)(Left / _size_x);
int startY = (int)(Right / _size_y);
int endX = (int)(Top / _size_x);
int endY = (int)(Bottom / _size_y);
if (startX < 0 || startY < 0 || endX >= _map[0].length || endY >= _map.length)
{
return 2;
}
for (int i = startX; i <= endX; i++)
{
for (int j = startY; j <= endY; j++)
{
if (_map[i][j] == _barrier)
{
return 1;
}
}
}
return 0;
}
private BufferedImage DrawMapWithObject()
{
BufferedImage bmp = new BufferedImage(_width,_height,BufferedImage.TYPE_INT_RGB);
if (_drawingObject == null || _map == null)
{
return bmp;
}
Graphics gr = bmp.getGraphics();
for (int i = 0; i < _map.length; ++i)
{
for (int j = 0; j < _map.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 bmp;
}
protected abstract void GenerateMap();
protected abstract void DrawRoadPart(Graphics g, int i, int j);
protected abstract void DrawBarrierPart(Graphics g, int i, int j);
}

13
src/BlockCount.java Normal file
View File

@ -0,0 +1,13 @@
public enum BlockCount {
TwoBlocks(2),
FourBlocks(4),
SixBlocks(6);
private final int Value;
BlockCount(int count){
Value=count;
}
public int GetBlockCount(){
return Value;
}
}

View File

@ -1,5 +0,0 @@
public enum BlockDirection {
TwoBlocks,
FourBlocks,
SixBlocks;
}

9
src/BlockForm.java Normal file
View File

@ -0,0 +1,9 @@
public enum BlockForm {
Rect(0),
Round(1),
RoundRectangle(2);
public final int Value;
BlockForm(int i) {
Value=i;
}
}

View File

@ -1,4 +1,5 @@
public enum Direction { public enum Direction {
None,
Up, Up,
Down, Down,
Left, Left,

View File

@ -0,0 +1,102 @@
import java.awt.*;
public class DrawingAircraftCarrier extends DrawingWarship{
private Color dopColor;
public DrawingAircraftCarrier(int speed, float weight, Color bodyColor, Color dopColor, int blocks, boolean bodyKit, boolean cabin, boolean superEngine)
{
super(speed, weight, bodyColor, blocks, 114, 40);
Warship = new EntityAircraftCarrier(speed, weight, bodyColor, dopColor, bodyKit, cabin, superEngine);
this.dopColor = dopColor;
}
public DrawingAircraftCarrier(EntityWarship warship, IDrawingObjectBlock additionalObject) {
super(warship, additionalObject);
Warship = warship;
}
public void SetDopColor(Color color) {
var temp = (EntityAircraftCarrier) Warship;
Warship = new EntityAircraftCarrier(temp.GetSpeed(), temp.GetWeight(), temp.GetBodyColor(), color, temp.GetBodyKit(), temp.GetCabin(), temp.GetSuperEngine());
dopColor = color;
}
@Override
public void SetColor(Color color) {
var temp = (EntityAircraftCarrier) Warship;
dopColor = dopColor==null ? Color.WHITE : dopColor;
Warship = new EntityAircraftCarrier(temp.GetSpeed(), temp.GetWeight(), color, dopColor, temp.GetBodyKit(), temp.GetCabin(), temp.GetSuperEngine());
}
@Override
public void paintComponent(Graphics gr){
if(!(Warship instanceof EntityAircraftCarrier))
{
return;
}
Graphics2D g2 = (Graphics2D) gr;
EntityAircraftCarrier aircraftCarrier = (EntityAircraftCarrier) Warship;
if (aircraftCarrier.GetBodyKit())
{
//боковая площадка
int[] pointXArea = {_startPosX + 94, _startPosX + 74, _startPosX + 24, _startPosX + 4};
int[] pointYArea = {_startPosY + 40, _startPosY + 60, _startPosY + 60, _startPosY + 40};
g2.setColor(Warship.GetBodyColor());
g2.fillPolygon(pointXArea, pointYArea, 4);
g2.setColor(Color.BLACK);
g2.drawPolygon(pointXArea, pointYArea, 4);
//полоса
int[] pointXLine = {_startPosX + 4, _startPosX + 15, _startPosX + 74, _startPosX + 59};
int[] pointYLine = {_startPosY, _startPosY, _startPosY + 60, _startPosY + 60};
g2.setColor(Color.GRAY);
g2.fillPolygon(pointXLine, pointYLine, 4);
g2.setColor(Color.BLACK);
g2.drawPolygon(pointXLine, pointYLine, 4);
}
if (aircraftCarrier.GetSuperEngine())
{
g2.setColor(Color.RED);
g2.fillOval(_startPosX, _startPosY, 10, 10);
g2.setColor(Color.BLACK);
g2.drawOval(_startPosX, _startPosY, 10, 10);
g2.setColor(Color.RED);
g2.fillOval(_startPosX, _startPosY + 10, 10, 10);
g2.setColor(Color.BLACK);
g2.drawOval(_startPosX, _startPosY + 10, 10, 10);
g2.setColor(Color.RED);
g2.fillOval(_startPosX, _startPosY + 18, 10, 10);
g2.setColor(Color.BLACK);
g2.drawOval(_startPosX, _startPosY + 18, 10, 10);
g2.setColor(Color.RED);
g2.fillOval(_startPosX, _startPosY + 30, 10, 10);
g2.setColor(Color.BLACK);
g2.drawOval(_startPosX, _startPosY + 30, 10, 10);
}
super.paintComponent(gr);
if (aircraftCarrier.GetCabin())
{
g2.setColor(Color.GRAY);
g2.fillOval(_startPosX + 80, _startPosY + 13, 10, 14);
g2.setColor(Color.BLACK);
g2.drawOval(_startPosX + 80, _startPosY + 13, 10, 14);
g2.setColor(Color.GRAY);
g2.fillOval(_startPosX + 90, _startPosY + 13, 10, 14);
g2.setColor(Color.BLACK);
g2.drawOval(_startPosX + 90, _startPosY + 13, 10, 14);
g2.setColor(Color.GRAY);
g2.fillRect(_startPosX + 85, _startPosY + 13, 10, 14);
g2.setColor(Color.BLACK);
g2.drawRect(_startPosX + 85, _startPosY + 13, 10, 14);
}
super.repaint();
}
}

View File

@ -1,45 +1,44 @@
import java.awt.*; import java.awt.*;
public class DrawingBlocks { public class DrawingBlocks implements IDrawingObjectBlock{
BlockDirection blockDirection; private BlockCount _block;
public int GetBlockCount(){return _block.GetBlockCount();}
public DrawingBlocks(BlockCount block) {
_block=block;
}
@Override
public void SetBlockCount(int count){
for (BlockCount temp: BlockCount.values())
if (temp.GetBlockCount() == count){
_block=temp;
return;
}
}
@Override
public void DrawBlocks(Graphics2D g2, int _startPosX, int _startPosY){ public void DrawBlocks(Graphics2D g2, int _startPosX, int _startPosY){
switch(blockDirection){ if (_block.GetBlockCount() >= 2) {
case TwoBlocks: g2.setColor(Color.GRAY);
g2.setColor(Color.GRAY); g2.fillRect(_startPosX + 15, _startPosY + 10, 10, 10);
g2.fillRect(_startPosX + 15, _startPosY + 10, 10, 10); g2.fillRect(_startPosX + 15, _startPosY + 20, 10, 10);
g2.fillRect(_startPosX + 15, _startPosY + 20, 10, 10); g2.setColor(Color.BLACK);
g2.setColor(Color.BLACK); g2.drawRect(_startPosX + 15, _startPosY + 10, 10, 10);
g2.drawRect(_startPosX + 15, _startPosY + 10, 10, 10); g2.drawRect(_startPosX + 15, _startPosY + 20, 10, 10);
g2.drawRect(_startPosX + 15, _startPosY + 20, 10, 10); }
break; if (_block.GetBlockCount() >= 4) {
case FourBlocks: g2.setColor(Color.GRAY);
g2.setColor(Color.GRAY); g2.fillRect(_startPosX + 25, _startPosY + 10, 10, 10);
g2.fillRect(_startPosX + 15, _startPosY + 10, 10, 10); g2.fillRect(_startPosX + 25, _startPosY + 20, 10, 10);
g2.fillRect(_startPosX + 15, _startPosY + 20, 10, 10); g2.setColor(Color.BLACK);
g2.fillRect(_startPosX + 25, _startPosY + 10, 10, 10); g2.drawRect(_startPosX + 25, _startPosY + 10, 10, 10);
g2.fillRect(_startPosX + 25, _startPosY + 20, 10, 10); g2.drawRect(_startPosX + 25, _startPosY + 20, 10, 10);
g2.setColor(Color.BLACK); }
g2.drawRect(_startPosX + 15, _startPosY + 10, 10, 10); if (_block.GetBlockCount() >= 6) {
g2.drawRect(_startPosX + 15, _startPosY + 20, 10, 10); g2.setColor(Color.GRAY);
g2.drawRect(_startPosX + 25, _startPosY + 10, 10, 10); g2.fillRect(_startPosX + 35, _startPosY + 10, 10, 10);
g2.drawRect(_startPosX + 25, _startPosY + 20, 10, 10); g2.fillRect(_startPosX + 35, _startPosY + 20, 10, 10);
break; g2.setColor(Color.BLACK);
case SixBlocks: g2.drawRect(_startPosX + 35, _startPosY + 10, 10, 10);
g2.setColor(Color.GRAY); g2.drawRect(_startPosX + 35, _startPosY + 20, 10, 10);
g2.fillRect(_startPosX + 15, _startPosY + 10, 10, 10);
g2.fillRect(_startPosX + 15, _startPosY + 20, 10, 10);
g2.fillRect(_startPosX + 25, _startPosY + 10, 10, 10);
g2.fillRect(_startPosX + 25, _startPosY + 20, 10, 10);
g2.fillRect(_startPosX + 35, _startPosY + 10, 10, 10);
g2.fillRect(_startPosX + 35, _startPosY + 20, 10, 10);
g2.setColor(Color.BLACK);
g2.drawRect(_startPosX + 15, _startPosY + 10, 10, 10);
g2.drawRect(_startPosX + 15, _startPosY + 20, 10, 10);
g2.drawRect(_startPosX + 25, _startPosY + 10, 10, 10);
g2.drawRect(_startPosX + 25, _startPosY + 20, 10, 10);
g2.drawRect(_startPosX + 35, _startPosY + 10, 10, 10);
g2.drawRect(_startPosX + 35, _startPosY + 20, 10, 10);
break;
} }
} }
} }

View File

@ -1,22 +0,0 @@
import javax.swing.*;
import java.awt.*;
public class DrawingComponents extends JComponent {
public DrawingWarship warship;
public DrawingComponents(){
super();
}
public void SetDrawingWarship(DrawingWarship warship){
this.warship = warship;
}
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
if(warship != null){
warship.DrawTransport(g2);
}
repaint();
}
}

View File

@ -0,0 +1,53 @@
import java.awt.*;
public class DrawingObjectWarship implements IDrawingObject {
private DrawingWarship _warship = null;
public DrawingObjectWarship(DrawingWarship warship)
{
_warship = warship;
}
public float Step(){
if(_warship !=null && _warship.Warship != null)
return _warship.Warship.Step;
return 0;
}
public DrawingWarship GetWarship() {
return _warship;
}
@Override
public void SetObject(int x, int y, int width, int height) {
_warship.SetPosition(x, y, width, height);
}
@Override
public void MoveObject(Direction direction) {
_warship.MoveTransport(direction);
}
@Override
public void DrawingObject(Graphics g) {
_warship.paintComponent(g);
}
@Override
public float[] GetCurrentPosition() {
if(_warship != null) return _warship.GetCurrentPosition();
return null;
}
@Override
public String GetInfo() {
if (_warship != null){
return ExtentionWarship.GetDataForSave(_warship);
}
return null;
}
public static IDrawingObject Create(String data){
return new DrawingObjectWarship(ExtentionWarship.CreateDrawingWarship(data));
}
}

View File

@ -0,0 +1,44 @@
import java.awt.*;
public class DrawingRoundBlocks implements IDrawingObjectBlock{
private BlockCount _block;
public DrawingRoundBlocks(BlockCount block){
_block=block;
}
public int GetBlockCount(){return _block.GetBlockCount();}
@Override
public void SetBlockCount(int count){
for (BlockCount temp: BlockCount.values())
if (temp.GetBlockCount() == count){
_block=temp;
return;
}
}
@Override
public void DrawBlocks(Graphics2D g2, int _startPosX, int _startPosY){
if (_block.GetBlockCount() >= 2) {
g2.setColor(Color.GRAY);
g2.fillOval(_startPosX + 15, _startPosY + 10, 10, 10);
g2.fillOval(_startPosX + 15, _startPosY + 20, 10, 10);
g2.setColor(Color.BLACK);
g2.drawOval(_startPosX + 15, _startPosY + 10, 10, 10);
g2.drawOval(_startPosX + 15, _startPosY + 20, 10, 10);
}
if (_block.GetBlockCount() >= 4) {
g2.setColor(Color.GRAY);
g2.fillOval(_startPosX + 25, _startPosY + 10, 10, 10);
g2.fillOval(_startPosX + 25, _startPosY + 20, 10, 10);
g2.setColor(Color.BLACK);
g2.drawOval(_startPosX + 25, _startPosY + 10, 10, 10);
g2.drawOval(_startPosX + 25, _startPosY + 20, 10, 10);
}
if (_block.GetBlockCount() >= 6) {
g2.setColor(Color.GRAY);
g2.fillOval(_startPosX + 35, _startPosY + 10, 10, 10);
g2.fillOval(_startPosX + 35, _startPosY + 20, 10, 10);
g2.setColor(Color.BLACK);
g2.drawOval(_startPosX + 35, _startPosY + 10, 10, 10);
g2.drawOval(_startPosX + 35, _startPosY + 20, 10, 10);
}
}
}

View File

@ -0,0 +1,44 @@
import java.awt.*;
public class DrawingRoundRectangleBlocks implements IDrawingObjectBlock {
private BlockCount _block;
public DrawingRoundRectangleBlocks(BlockCount block) {
_block=block;
}
public int GetBlockCount(){return _block.GetBlockCount();}
@Override
public void SetBlockCount(int count){
for (BlockCount temp: BlockCount.values())
if (temp.GetBlockCount() == count){
_block=temp;
return;
}
}
@Override
public void DrawBlocks(Graphics2D g2, int _startPosX, int _startPosY){
if (_block.GetBlockCount() >= 2) {
g2.setColor(Color.GRAY);
g2.fillRoundRect(_startPosX + 15, _startPosY + 10, 10, 10, 7, 7);
g2.fillRoundRect(_startPosX + 15, _startPosY + 20, 10, 10, 7, 7);
g2.setColor(Color.BLACK);
g2.drawRoundRect(_startPosX + 15, _startPosY + 10, 10, 10, 7, 7);
g2.drawRoundRect(_startPosX + 15, _startPosY + 20, 10, 10, 7, 7);
}
if (_block.GetBlockCount() >= 4) {
g2.setColor(Color.GRAY);
g2.fillRoundRect(_startPosX + 25, _startPosY + 10, 10, 10, 7, 7);
g2.fillRoundRect(_startPosX + 25, _startPosY + 20, 10, 10, 7, 7);
g2.setColor(Color.BLACK);
g2.drawRoundRect(_startPosX + 25, _startPosY + 10, 10, 10, 7, 7);
g2.drawRoundRect(_startPosX + 25, _startPosY + 20, 10, 10, 7, 7);
}
if (_block.GetBlockCount() >= 6) {
g2.setColor(Color.GRAY);
g2.fillRoundRect(_startPosX + 35, _startPosY + 10, 10, 10, 7, 7);
g2.fillRoundRect(_startPosX + 35, _startPosY + 20, 10, 10, 7, 7);
g2.setColor(Color.BLACK);
g2.drawRoundRect(_startPosX + 35, _startPosY + 10, 10, 10, 7, 7);
g2.drawRoundRect(_startPosX + 35, _startPosY + 20, 10, 10, 7, 7);
}
}
}

View File

@ -1,25 +1,67 @@
import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.util.Random; import java.util.Random;
public class DrawingWarship { public class DrawingWarship extends JComponent {
private EntityWarship Warship; protected EntityWarship Warship;
public EntityWarship GetWarship(){return Warship;} public EntityWarship GetWarship(){return Warship;}
public DrawingBlocks Blocks; protected IDrawingObjectBlock Blocks;
public String BlockType(){return Blocks.getClass().getName();}
private BlockCount _block;
private int _startPosX; protected int _startPosX;
private int _startPosY; protected int _startPosY;
private Integer _pictureWidth = null; private Integer _pictureWidth = null;
private Integer _pictureHeight = null; private Integer _pictureHeight = null;
private final int _warshipWidth = 114; private int _warshipWidth = 114;
private final int _warshipHeight = 40; private int _warshipHeight = 40;
public void Init(int speed, float weight, Color bodyColor) public DrawingWarship(int speed, float weight, Color bodyColor, int blockCount)
{ {
Warship = new EntityWarship(); Warship = new EntityWarship(speed, weight, bodyColor);
Warship.Init(speed, weight, bodyColor); Random rnd = new Random();
Blocks = new DrawingBlocks(); int randomForm = rnd.nextInt(3);
Blocks.blockDirection = BlockRandom(); if(randomForm == 0){
Blocks = new DrawingBlocks(_block);
}
else if(randomForm == 1){
Blocks = new DrawingRoundBlocks(_block);
}
else Blocks = new DrawingRoundRectangleBlocks(_block);
Blocks.SetBlockCount(blockCount);
}
protected DrawingWarship(int speed, float weight, Color bodyColor, int blocksCount, int warshipWidth, int warshipHeight)
{
Warship = new EntityWarship(speed, weight, bodyColor);
Blocks = new DrawingBlocks(_block);
Blocks.SetBlockCount(blocksCount);
_warshipWidth = warshipWidth;
_warshipHeight = warshipHeight;
}
public DrawingWarship(EntityWarship warship,IDrawingObjectBlock block){
Warship = warship;
Blocks = block;
}
public void SetBlocks(int blocksCount, IDrawingObjectBlock block){
Blocks = block;
Blocks.SetBlockCount(blocksCount);
}
public void SetColor(Color color){
Warship = new EntityWarship(Warship.GetSpeed(), Warship.GetWeight(), color);
}
public int BlockRandom(){
Random rnd = new Random();
int count = rnd.nextInt(0,3);
if(count == 0) return 2;
if(count == 1) return 4;
if(count == 2) return 6;
return 0;
} }
public void SetPosition(int x, int y, int width, int height) public void SetPosition(int x, int y, int width, int height)
@ -32,6 +74,7 @@ public class DrawingWarship {
_pictureHeight = height; _pictureHeight = height;
} }
} }
public void MoveTransport(Direction direction) public void MoveTransport(Direction direction)
{ {
if (_pictureWidth == null || _pictureHeight == null) if (_pictureWidth == null || _pictureHeight == null)
@ -70,11 +113,14 @@ public class DrawingWarship {
break; break;
} }
} }
public void DrawTransport(Graphics2D g2){
public void paintComponent(Graphics gr){
super.paintComponent(gr);
if (_startPosX < 0 || _startPosY < 0 || _pictureHeight == null || _pictureWidth == null) if (_startPosX < 0 || _startPosY < 0 || _pictureHeight == null || _pictureWidth == null)
{ {
return; return;
} }
Graphics2D g2 = (Graphics2D) gr;
//главная палуба //главная палуба
int[] pointXWarship = {_startPosX + 4, _startPosX + 94, _startPosX + 114, _startPosX + 94, _startPosX + 4}; int[] pointXWarship = {_startPosX + 4, _startPosX + 94, _startPosX + 114, _startPosX + 94, _startPosX + 4};
int[] pointYWarship = {_startPosY, _startPosY, _startPosY + 20, _startPosY + 40, _startPosY + 40}; int[] pointYWarship = {_startPosY, _startPosY, _startPosY + 20, _startPosY + 40, _startPosY + 40};
@ -105,15 +151,7 @@ public class DrawingWarship {
//блоки //блоки
Blocks.DrawBlocks(g2,_startPosX, _startPosY); Blocks.DrawBlocks(g2,_startPosX, _startPosY);
} super.repaint();
public BlockDirection BlockRandom(){
Random rand = new Random();
int resRand = rand.nextInt(3);
if(resRand == 0) return BlockDirection.TwoBlocks;
if(resRand == 1) return BlockDirection.FourBlocks;
if(resRand == 2) return BlockDirection.SixBlocks;
return null;
} }
public void ChangeBorders(int width, int height) public void ChangeBorders(int width, int height)
@ -135,4 +173,13 @@ public class DrawingWarship {
_startPosY = _pictureHeight - _warshipHeight; _startPosY = _pictureHeight - _warshipHeight;
} }
} }
public float[] GetCurrentPosition(){
float[] cortege = new float[4];
cortege[0] = _startPosX;
cortege[1] =_startPosY;
cortege[2] = _startPosX + _warshipWidth;
cortege[3] = _startPosY + _warshipHeight;
return cortege;
}
} }

View File

@ -0,0 +1,24 @@
import java.awt.*;
public class EntityAircraftCarrier extends EntityWarship {
private Color DopColor;
public Color GetDopColor(){return DopColor;}
private boolean BodyKit;
public boolean GetBodyKit(){return BodyKit;}
private boolean Cabin;
public boolean GetCabin(){return Cabin;}
private boolean SuperEngine;
public boolean GetSuperEngine(){return SuperEngine;}
public EntityAircraftCarrier(int speed, float weight, Color bodyColor, Color dopColor, boolean bodyKit, boolean cabin, boolean superEngine)
{
super(speed, weight, bodyColor);
DopColor = dopColor;
BodyKit = bodyKit;
Cabin = cabin;
SuperEngine = superEngine;
}
}

View File

@ -11,11 +11,12 @@ public class EntityWarship {
public Color GetBodyColor (){return BodyColor;} public Color GetBodyColor (){return BodyColor;}
public float Step; public float Step;
public void Init(int speed, float weight, Color bodyColor)
public EntityWarship(int speed, float weight, Color bodyColor)
{ {
Random rnd = new Random(); Random rnd = new Random();
Speed = speed <= 0 ? rnd.nextInt(100) + 50 : speed; Speed = speed <= 0 ? rnd.nextInt(50, 150) : speed;
Weight = weight <= 0 ? rnd.nextInt(30)+40 : weight; Weight = weight <= 0 ? rnd.nextInt(40, 70) : weight;
BodyColor= bodyColor; BodyColor= bodyColor;
Step = Speed * 100 / Weight; Step = Speed * 100 / Weight;
} }

64
src/ExtentionWarship.java Normal file
View File

@ -0,0 +1,64 @@
import java.awt.*;
public class ExtentionWarship {
private static final char _separatorForObject = ':';
private static BlockCount _block;
public static DrawingWarship CreateDrawingWarship(String info)
{
//speed - 0, weight - 1, color - 2, blocktype - 3, blockcount - 4,
//dopcolor - 5, bodykit - 6, cabin - 7, superengine- 8
String[] strs = info.split(String.valueOf(_separatorForObject));
IDrawingObjectBlock blockType = null;
switch (strs[3])
{
case "DrawingBlocks":
blockType = new DrawingBlocks(_block);
break;
case "DrawingRoundBlocks":
blockType = new DrawingRoundBlocks(_block);
break;
case "DrawingRoundRectangleBlocks":
blockType = new DrawingRoundRectangleBlocks(_block);
break;
}
if (strs.length == 5)
{
var temp = new DrawingWarship(Integer.parseInt(strs[0]),
Float.parseFloat(strs[1]),
new Color(Integer.parseInt(strs[2])),
Integer.parseInt(strs[4]));
temp.SetBlocks(Integer.parseInt(strs[4]), blockType);
return temp;
}
if (strs.length == 9)
{
var temp = new DrawingAircraftCarrier(Integer.parseInt(strs[0]),
Float.parseFloat(strs[1]),
new Color(Integer.parseInt(strs[2])),
new Color(Integer.parseInt(strs[5])),
Integer.parseInt(strs[4]),
Boolean.parseBoolean(strs[6]),
Boolean.parseBoolean(strs[7]),
Boolean.parseBoolean(strs[8]));
temp.SetBlocks(Integer.parseInt(strs[4]), blockType);
return temp;
}
return null;
}
public static String GetDataForSave(DrawingWarship drawingWarship)
{
var warship = drawingWarship.Warship;
var str = "" + warship.GetSpeed() + _separatorForObject + warship.GetWeight()
+ _separatorForObject + warship.GetBodyColor().getRGB() + _separatorForObject +
drawingWarship.BlockType() + _separatorForObject + drawingWarship.Blocks.GetBlockCount();
if (warship instanceof EntityAircraftCarrier aircraftCarrier)
{
return str + _separatorForObject + aircraftCarrier.GetDopColor().getRGB()
+ _separatorForObject + aircraftCarrier.GetBodyKit() + _separatorForObject
+ aircraftCarrier.GetCabin() + _separatorForObject + aircraftCarrier.GetSuperEngine();
}
return str;
}
}

View File

@ -0,0 +1,255 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="FormMapWithSetWarships">
<grid id="27dc6" binding="mainPanel" 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>
<xy x="20" y="20" width="884" height="620"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<grid id="ae53a" binding="PictureBox" 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="3" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false">
<preferred-size width="727" height="24"/>
</grid>
</constraints>
<properties/>
<border type="none"/>
<children>
<hspacer id="815b1">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="1" hsize-policy="7" anchor="0" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="716" height="11"/>
</grid>
</constraints>
</hspacer>
</children>
</grid>
<grid id="16c60" binding="GroupBoxTools" layout-manager="GridLayoutManager" row-count="11" 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="1" row-span="2" col-span="1" vsize-policy="3" hsize-policy="0" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="bevel-lowered" title="Tools" title-justification="1" title-position="2">
<font/>
</border>
<children>
<component id="1fc5b" class="javax.swing.JButton" binding="ButtonShowStorage">
<constraints>
<grid row="6" 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="Show storage"/>
</properties>
</component>
<component id="3dd40" class="javax.swing.JButton" binding="ButtonShowOnMap">
<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="Show map"/>
</properties>
</component>
<component id="2ea34" class="javax.swing.JButton" binding="ButtonRight">
<constraints>
<grid row="10" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="3" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<text value=""/>
</properties>
</component>
<component id="daf62" class="javax.swing.JButton" binding="ButtonDown">
<constraints>
<grid row="10" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="3" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<text value=""/>
</properties>
</component>
<component id="76b8a" class="javax.swing.JButton" binding="ButtonLeft">
<constraints>
<grid row="10" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="3" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<text value=""/>
</properties>
</component>
<component id="675d4" class="javax.swing.JButton" binding="ButtonUp">
<constraints>
<grid row="9" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="3" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<text value=""/>
</properties>
</component>
<vspacer id="fa6fc">
<constraints>
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
<vspacer id="f6c4c">
<constraints>
<grid row="8" column="1" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
<component id="14e42" class="javax.swing.JButton" binding="deletedWarshipButtom">
<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="Deleted warship"/>
</properties>
</component>
<component id="16e2c" class="javax.swing.JButton" binding="ButtonRemoveWarship">
<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="Remove warship"/>
</properties>
</component>
<component id="608d7" class="javax.swing.JTextField" binding="TextBoxPosition">
<constraints>
<grid row="3" 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="9e5dd" class="javax.swing.JButton" binding="ButtonAddWarship">
<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="Add warship"/>
</properties>
</component>
<grid id="53593" binding="MapPanel" layout-manager="GridLayoutManager" row-count="5" 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="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<name value=""/>
</properties>
<border type="bevel-lowered" title="Maps" title-justification="1" title-position="2"/>
<children>
<component id="91aeb" class="javax.swing.JTextField" binding="TextFieldMap">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" 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="b2e9e" class="javax.swing.JButton" binding="DeleteMapButton">
<constraints>
<grid row="4" 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="Delete map"/>
</properties>
</component>
<component id="9cadb" class="javax.swing.JList" binding="ListBoxMaps">
<constraints>
<grid row="3" column="0" row-span="1" col-span="1" 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="ddbe9" class="javax.swing.JButton" binding="CreateMapButton">
<constraints>
<grid row="2" 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="Create map"/>
</properties>
</component>
<component id="8d7d6" class="javax.swing.JComboBox" binding="СomboBoxSelectorMap">
<constraints>
<grid row="1" 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="SimpleMap"/>
<item value="LineMap"/>
</model>
</properties>
</component>
</children>
</grid>
</children>
</grid>
<grid id="fed73" class="javax.swing.JMenuBar" binding="menuStrip" 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="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<grid id="1ed5d" class="javax.swing.JMenu" binding="menu" layout-manager="FlowLayout" hgap="5" vgap="5" flow-align="1">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="File"/>
</properties>
<border type="none"/>
<children>
<component id="ba181" class="javax.swing.JMenuItem" binding="saveToolStripMenuItem">
<constraints/>
<properties>
<text value="Save"/>
</properties>
</component>
<component id="601ab" class="javax.swing.JMenuItem" binding="loadToolStripMenuItem">
<constraints/>
<properties>
<text value="Load"/>
</properties>
</component>
<component id="62149" class="javax.swing.JMenuItem" binding="saveMapToolStripMenuItem">
<constraints/>
<properties>
<text value="SaveMap"/>
</properties>
</component>
<component id="5bcb" class="javax.swing.JMenuItem" binding="loadMapToolStripMenuItem">
<constraints/>
<properties>
<text value="LoadMap"/>
</properties>
</component>
</children>
</grid>
</children>
</grid>
<vspacer id="19c9">
<constraints>
<grid row="1" column="2" 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>

View File

@ -0,0 +1,333 @@
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.HashMap;
import java.util.function.Consumer;
public class FormMapWithSetWarships extends JFrame{
private JPanel mainPanel;
private JPanel PictureBox;
private JPanel GroupBoxTools;
private JComboBox СomboBoxSelectorMap;
private JButton ButtonAddWarship;
private JButton ButtonRemoveWarship;
private JButton ButtonShowStorage;
private JButton ButtonShowOnMap;
private JTextField TextBoxPosition;
private JButton ButtonRight;
private JButton ButtonUp;
private JButton ButtonDown;
private JButton ButtonLeft;
private JButton deletedWarshipButtom;
private JPanel MapPanel;
private JTextField TextFieldMap;
private JButton CreateMapButton;
private JButton DeleteMapButton;
private JList ListBoxMaps;
private JMenuItem saveToolStripMenuItem;
private JMenuItem loadToolStripMenuItem;
private JMenuItem saveMapToolStripMenuItem;
private JMenuItem loadMapToolStripMenuItem;
private JMenuBar menuStrip;
private JMenu menu;
private Image bufferedImage;
private MapWithSetWarshipsGeneric<DrawingObjectWarship, AbstractMap> _mapWarshipsCollectionGeneric;
private MapsCollection _mapsCollection;
private final HashMap<String, AbstractMap> _mapsDict = new HashMap<>() {{
put("SimpleMap", new SimpleMap());
put("LineMap", new LineMap());
}};
public FormMapWithSetWarships(){
InitializeComponent();
}
@Override
public void paint(Graphics g) {
super.paint(g);
if (bufferedImage != null) {
PictureBox.paintComponents(bufferedImage.getGraphics());
PictureBox.getGraphics().drawImage(bufferedImage, 0, 0, null);
}
}
private void ReloadMaps()
{
int index = ListBoxMaps.getSelectedIndex();
ListBoxMaps.setListData(_mapsCollection.Keys().toArray(new String[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);
}
repaint();
}
private void InitializeComponent(){
setContentPane(mainPanel);
setTitle("Warship");
setSize(935, 693);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
_mapsCollection = new MapsCollection(getWidth(), getHeight());
СomboBoxSelectorMap.removeAllItems();
for(String elem:_mapsDict.keySet()){
СomboBoxSelectorMap.addItem(elem);
}
Icon iconUp = new ImageIcon("src\\Images\\ArrowUp.jpg");
ButtonUp.setIcon(iconUp);
Icon iconDown = new ImageIcon("src\\Images\\ArrowDown.jpg");
ButtonDown.setIcon(iconDown);
Icon iconLeft = new ImageIcon("src\\Images\\ArrowLeft.jpg");
ButtonLeft.setIcon(iconLeft);
Icon iconRight = new ImageIcon("src\\Images\\ArrowRight.jpg");
ButtonRight.setIcon(iconRight);
File root = new File("D://2.1//RPP");
//сохранение
saveToolStripMenuItem.addActionListener(e -> {
JFileChooser fs = new JFileChooser();
fs.setCurrentDirectory(root);
fs.setAcceptAllFileFilterUsed(false);
FileNameExtensionFilter filter = new FileNameExtensionFilter(".txt file", "txt");
fs.addChoosableFileFilter(filter);
fs.setDialogTitle("Save");
int result = fs.showSaveDialog(null);
if (result == JFileChooser.APPROVE_OPTION) {
File selectedFile = fs.getSelectedFile();
if (_mapsCollection.SaveData(selectedFile.getPath()))
{
JOptionPane.showMessageDialog(null, "Сохранение прошло успешно", "Результат",JOptionPane.INFORMATION_MESSAGE);
}
else
{
JOptionPane.showMessageDialog(null, "Не сохранилось", "Результат",JOptionPane.ERROR_MESSAGE);
}
}
});
//загрузка
loadToolStripMenuItem.addActionListener(e -> {
JFileChooser fs = new JFileChooser();
fs.setCurrentDirectory(root);
fs.setAcceptAllFileFilterUsed(false);
FileNameExtensionFilter filter = new FileNameExtensionFilter(".txt file", "txt");
fs.addChoosableFileFilter(filter);
fs.setDialogTitle("Load");
int result = fs.showSaveDialog(null);
if (result == JFileChooser.APPROVE_OPTION) {
File selectedFile = fs.getSelectedFile();
if (_mapsCollection.LoadData(selectedFile.getPath()))
{
ReloadMaps();
JOptionPane.showMessageDialog(null, "Загрузка прошла успешно", "Результат",JOptionPane.INFORMATION_MESSAGE);
}
else
{
JOptionPane.showMessageDialog(null, "Не удалось загрузить", "Результат",JOptionPane.ERROR_MESSAGE);
}
}
});
//сохранение карты
saveMapToolStripMenuItem.addActionListener(e -> {
JFileChooser fs = new JFileChooser();
fs.setCurrentDirectory(root);
fs.setAcceptAllFileFilterUsed(false);
FileNameExtensionFilter filter = new FileNameExtensionFilter(".txt file", "txt");
fs.addChoosableFileFilter(filter);
fs.setDialogTitle("SaveMap");
int result = fs.showSaveDialog(null);
if (result == JFileChooser.APPROVE_OPTION) {
File selectedFile = fs.getSelectedFile();
if (_mapsCollection.SaveMap(selectedFile.getPath(), ListBoxMaps.getSelectedValue().toString()))
{
JOptionPane.showMessageDialog(null, "Сохранение карты прошло успешно", "Результат",JOptionPane.INFORMATION_MESSAGE);
}
else
{
JOptionPane.showMessageDialog(null, "Не сохранилась карта", "Результат",JOptionPane.ERROR_MESSAGE);
}
}
});
//загрузка карты
loadMapToolStripMenuItem.addActionListener(e -> {
JFileChooser fs = new JFileChooser();
fs.setCurrentDirectory(root);
fs.setAcceptAllFileFilterUsed(false);
FileNameExtensionFilter filter = new FileNameExtensionFilter(".txt file", "txt");
fs.addChoosableFileFilter(filter);
fs.setDialogTitle("LoadMap");
int result = fs.showSaveDialog(null);
if (result == JFileChooser.APPROVE_OPTION) {
File selectedFile = fs.getSelectedFile();
if (_mapsCollection.LoadMap(selectedFile.getPath()))
{
JOptionPane.showMessageDialog(null, "Загрузка карты прошла успешно", "Результат",JOptionPane.INFORMATION_MESSAGE);
ReloadMaps();
}
else
{
JOptionPane.showMessageDialog(null, "Не загрузилась карта", "Результат",JOptionPane.ERROR_MESSAGE);
}
}
});
ListBoxMaps.addListSelectionListener(e -> {
if(ListBoxMaps.getSelectedIndex() == -1)
return;
bufferedImage = _mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).ShowSet();
repaint();
});
DeleteMapButton.addActionListener(e -> {
if (ListBoxMaps.getSelectedIndex() == -1)
{
return;
}
if(JOptionPane.showConfirmDialog(this,"Удалить карту " + ListBoxMaps.getSelectedValue().toString() + "?",
"Удаление",JOptionPane.YES_NO_OPTION) == 0)
{
_mapsCollection.DelMap(ListBoxMaps.getSelectedValue().toString());
ReloadMaps();
}
});
CreateMapButton.addActionListener(e -> {
if (СomboBoxSelectorMap.getSelectedIndex() == -1 || TextFieldMap.getText() == null || TextFieldMap.getText().equals(""))
{
JOptionPane.showMessageDialog(this,"Не все данные заполнены","Ошибка",JOptionPane.ERROR_MESSAGE);
return;
}
if (!_mapsDict.containsKey(СomboBoxSelectorMap.getSelectedItem()))
{
JOptionPane.showMessageDialog(this,"Нет такой карты","Ошибка",JOptionPane.ERROR_MESSAGE);
return;
}
_mapsCollection.AddMap(TextFieldMap.getText(), _mapsDict.get(СomboBoxSelectorMap.getSelectedItem().toString()));
ReloadMaps();
});
deletedWarshipButtom.addActionListener(e -> {
if (ListBoxMaps.getSelectedIndex() == -1)
return;
DrawingObjectWarship warship = _mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).GetWarshipInDeleted();
if (warship == null) {
JOptionPane.showMessageDialog(null, "Стек пуст", "Ошибка", JOptionPane.ERROR_MESSAGE);
return;
}
FormWarship form = new FormWarship(warship);
form.setSize(1000, 700);
form.setVisible(true);
});
ButtonAddWarship.addActionListener(e -> {
FormWarshipConfig formWarshipConfig = new FormWarshipConfig();
formWarshipConfig.AddEvent(newWarship ->{
if(ListBoxMaps.getSelectedIndex() == -1){
return;
}
if(newWarship != null){
DrawingObjectWarship warship = new DrawingObjectWarship(newWarship);
if (_mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).Plus(warship) >= 0) {
JOptionPane.showMessageDialog(null, "Объект добавлен");
bufferedImage = _mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).ShowSet();
repaint();
}
else {
JOptionPane.showMessageDialog(null, "Не удалось добавить объект");
}
}
});
formWarshipConfig.setSize(1000, 450);
formWarshipConfig.setVisible(true);
});
ButtonRemoveWarship.addActionListener(e -> {
if (TextBoxPosition.getText().isEmpty())
{
return;
}
int result = JOptionPane.showConfirmDialog(null,
"Удалить объект?",
"Удаление",
JOptionPane.YES_NO_OPTION,
JOptionPane.WARNING_MESSAGE);
if (result == JOptionPane.NO_OPTION)
{
return;
}
int pos = Integer.parseInt(TextBoxPosition.getText());
if (_mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).Minus(pos)!=null)
{
JOptionPane.showMessageDialog(this,
"Объект удален","Успех",JOptionPane.INFORMATION_MESSAGE);
bufferedImage = _mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).ShowSet();
repaint();
}
else
{
JOptionPane.showMessageDialog(this,
"Не удалось удалить объект","Ошибка",JOptionPane.INFORMATION_MESSAGE);
}
});
ButtonShowStorage.addActionListener(e -> {
if(_mapWarshipsCollectionGeneric == null)
{
return;
}
bufferedImage = _mapWarshipsCollectionGeneric.ShowSet();
repaint();
});
ButtonShowOnMap.addActionListener(e -> {
if (_mapWarshipsCollectionGeneric == null)
{
return;
}
bufferedImage = _mapWarshipsCollectionGeneric.ShowOnMap();
repaint();
});
ButtonUp.addActionListener(e -> {
if (_mapWarshipsCollectionGeneric != null) {
bufferedImage = _mapWarshipsCollectionGeneric.MoveObject(Direction.Up);
repaint();
}
});
ButtonDown.addActionListener(e -> {
if (_mapWarshipsCollectionGeneric != null) {
bufferedImage = _mapWarshipsCollectionGeneric.MoveObject(Direction.Down);
repaint();
}
});
ButtonRight.addActionListener(e -> {
if (_mapWarshipsCollectionGeneric != null) {
bufferedImage = _mapWarshipsCollectionGeneric.MoveObject(Direction.Right);
repaint();
}
});
ButtonLeft.addActionListener(e -> {
if (_mapWarshipsCollectionGeneric != null) {
bufferedImage = _mapWarshipsCollectionGeneric.MoveObject(Direction.Left);
repaint();
}
});
}
}

View File

@ -1,41 +1,109 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="FormWarship"> <form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="FormWarship">
<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"> <grid id="27dc6" binding="mainPanel" layout-manager="GridLayoutManager" row-count="4" column-count="6" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/> <margin top="0" left="0" bottom="0" right="0"/>
<constraints> <constraints>
<xy x="23" y="20" width="961" height="517"/> <xy x="49" y="20" width="935" height="482"/>
</constraints> </constraints>
<properties/> <properties/>
<border type="none"/> <border type="none"/>
<children> <children>
<component id="80b72" class="javax.swing.JButton" binding="buttonCreate"> <grid id="dfecd" binding="drawPanel" layout-manager="GridLayoutManager" row-count="3" column-count="7" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints> <constraints>
<grid row="2" column="0" 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="0" row-span="3" col-span="6" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints> </constraints>
<properties> <properties/>
<text value="Create"/> <border type="none"/>
</properties> <children>
</component> <component id="80b72" class="javax.swing.JButton" binding="buttonCreate">
<hspacer id="3f3da"> <constraints>
<constraints> <grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
<grid row="2" 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>
</constraints> <properties>
</hspacer> <text value="Create"/>
<component id="32714" class="javax.swing.JButton" binding="buttonLeft"> </properties>
<constraints> </component>
<grid row="2" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="3" indent="0" use-parent-layout="false"> <hspacer id="667bb">
<minimum-size width="30" height="30"/> <constraints>
<preferred-size width="30" height="30"/> <grid row="2" column="2" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
<maximum-size width="30" height="30"/> </constraints>
</grid> </hspacer>
</constraints> <vspacer id="e8c93">
<properties> <constraints>
<text value=""/> <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"/>
</properties> </constraints>
</component> </vspacer>
<component id="34645" class="javax.swing.JButton" binding="buttonCreateModif">
<constraints>
<grid row="2" 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="Modification"/>
</properties>
</component>
<component id="55933" class="javax.swing.JButton" binding="buttonRight">
<constraints>
<grid row="2" column="6" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="3" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<text value=""/>
</properties>
</component>
<component id="bb79e" class="javax.swing.JButton" binding="buttonDown">
<constraints>
<grid row="2" column="5" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="3" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<enabled value="true"/>
<text value=""/>
</properties>
</component>
<component id="82cc1" class="javax.swing.JButton" binding="buttonUp">
<constraints>
<grid row="1" column="5" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="3" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<text value=""/>
</properties>
</component>
<component id="32714" class="javax.swing.JButton" binding="buttonLeft">
<constraints>
<grid row="2" column="4" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="3" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<text value=""/>
</properties>
</component>
<component id="b9b11" class="javax.swing.JButton" binding="buttonSelect">
<constraints>
<grid row="2" 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>
<text value="Select"/>
</properties>
</component>
</children>
</grid>
<toolbar id="3697c" binding="toolBar"> <toolbar id="3697c" binding="toolBar">
<constraints> <constraints>
<grid row="3" column="0" row-span="1" col-span="5" vsize-policy="0" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"> <grid row="3" column="0" row-span="1" col-span="6" vsize-policy="0" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="-1" height="20"/> <preferred-size width="-1" height="20"/>
</grid> </grid>
</constraints> </constraints>
@ -62,51 +130,6 @@
</component> </component>
</children> </children>
</toolbar> </toolbar>
<component id="55933" class="javax.swing.JButton" binding="buttonRight">
<constraints>
<grid row="2" column="4" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="3" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<text value=""/>
</properties>
</component>
<component id="82cc1" class="javax.swing.JButton" binding="buttonUp">
<constraints>
<grid row="1" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="3" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<text value=""/>
</properties>
</component>
<component id="bb79e" class="javax.swing.JButton" binding="buttonDown">
<constraints>
<grid row="2" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="3" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<enabled value="true"/>
<text value=""/>
</properties>
</component>
<grid id="dfecd" binding="drawPanel" layout-manager="BorderLayout" hgap="0" vgap="0">
<constraints>
<grid row="0" 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/>
<border type="none"/>
<children/>
</grid>
</children> </children>
</grid> </grid>
</form> </form>

View File

@ -1,15 +1,18 @@
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter; import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent; import java.awt.event.ComponentEvent;
import java.util.Random; import java.util.Random;
public class FormWarship extends JFrame { public class FormWarship extends JDialog {
private DrawingWarship _warship; private DrawingWarship _warship;
public DrawingComponents drawingComponents; DrawingWarship selectedWarship;
public DrawingWarship getSelectedCar(){
return selectedWarship;
}
public boolean DialogResult = false;
private JPanel mainPanel; private JPanel mainPanel;
private JPanel drawPanel;
private JButton buttonCreate; private JButton buttonCreate;
private JButton buttonLeft; private JButton buttonLeft;
private JButton buttonUp; private JButton buttonUp;
@ -19,20 +22,55 @@ public class FormWarship extends JFrame {
private JLabel toolBarLabelSpeed; private JLabel toolBarLabelSpeed;
private JLabel toolBarLabelWieght; private JLabel toolBarLabelWieght;
private JLabel toolBarLabelColor; private JLabel toolBarLabelColor;
private JPanel drawPanel; private JButton buttonCreateModif;
private JButton buttonSelect;
public FormWarship(){ public FormWarship(){
InitializeComponent(); InitializeComponent();
} }
private void Draw(){
drawingComponents.repaint(); public FormWarship(DrawingObjectWarship warship){
InitializeComponent();
_warship = warship.GetWarship();
SetData();
} }
private void InitializeComponent(){
private void Draw(){
Graphics2D graphics = (Graphics2D) drawPanel.getGraphics();
graphics.clearRect(0, 0, drawPanel.getWidth(), drawPanel.getHeight());
drawPanel.paintComponents(graphics);
_warship.paintComponent(graphics);
}
@Override
public void paint(Graphics g) {
super.paint(g);
if (_warship != null) {
mainPanel.paintComponents(mainPanel.getGraphics());
_warship.paintComponent(g);
}
}
private void SetData(){
Random rnd = new Random();
_warship.SetPosition(rnd.nextInt(10, 100), rnd.nextInt(10, 100), mainPanel.getWidth(), mainPanel.getHeight());
toolBarLabelSpeed.setText("Color: " + _warship.GetWarship().GetSpeed() + " ");
toolBarLabelWieght.setText("Weight: " + _warship.GetWarship().GetWeight() + " ");
toolBarLabelColor.setText("Color: " + _warship.GetWarship().GetBodyColor().getRed() + " " +
_warship.GetWarship().GetBodyColor().getGreen() + " " + _warship.GetWarship().GetBodyColor().getBlue());
}
private void resizeWindow() {
_warship.ChangeBorders(drawPanel.getWidth(), drawPanel.getHeight());
Draw();
}
private void InitializeComponent() {
setContentPane(mainPanel); setContentPane(mainPanel);
setTitle("Warship"); setTitle("Warship");
setSize(900,700); setSize(900, 700);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true); setVisible(true);
Icon iconUp = new ImageIcon("src\\Images\\ArrowUp.jpg"); Icon iconUp = new ImageIcon("src\\Images\\ArrowUp.jpg");
@ -44,63 +82,68 @@ public class FormWarship extends JFrame {
Icon iconRight = new ImageIcon("src\\Images\\ArrowRight.jpg"); Icon iconRight = new ImageIcon("src\\Images\\ArrowRight.jpg");
buttonRight.setIcon(iconRight); buttonRight.setIcon(iconRight);
drawingComponents = new DrawingComponents(); //кнопка добавления объекта
drawPanel.add(drawingComponents); buttonCreate.addActionListener(e -> {
buttonCreate.addActionListener(new ActionListener() { Random rnd = new Random();
@Override _warship = new DrawingWarship(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000),
public void actionPerformed(ActionEvent e) { new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)), rnd.nextInt(3));
Random rnd = new Random(); SetData();
_warship = new DrawingWarship(); Draw();
_warship.Init(rnd.nextInt(200) + 100, rnd.nextInt(1000) + 1000, });
new Color(rnd.nextInt(256),rnd.nextInt(256),rnd.nextInt(256)));
_warship.SetPosition(rnd.nextInt(90) + 10, rnd.nextInt(90) + 10, drawPanel.getWidth(), drawPanel.getHeight()); //добавление модифицированного объекта
toolBarLabelSpeed.setText("Color: " + _warship.GetWarship().GetSpeed() + " "); buttonCreateModif.addActionListener(e -> {
toolBarLabelWieght.setText("Weight: " + _warship.GetWarship().GetWeight() + " "); Random rnd = new Random();
toolBarLabelColor.setText("Color: " + _warship.GetWarship().GetBodyColor().getRed() + " " + _warship = new DrawingAircraftCarrier(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000),
_warship.GetWarship().GetBodyColor().getGreen() + " " + _warship.GetWarship().GetBodyColor().getBlue()); new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)),
drawingComponents.SetDrawingWarship(_warship); new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)), BlockRandom(),
Draw(); rnd.nextBoolean(), rnd.nextBoolean(), rnd.nextBoolean());
SetData();
Draw();
});
buttonSelect.addActionListener(e -> {
if(_warship != null){
selectedWarship =_warship;
DialogResult = true;
} }
}); });
buttonLeft.addActionListener(new ActionListener() { buttonLeft.addActionListener(e -> {
@Override if (_warship != null) _warship.MoveTransport(Direction.Left);
public void actionPerformed(ActionEvent e) { Draw();
if(_warship != null) _warship.MoveTransport(Direction.Left);
Draw();
}
}); });
buttonRight.addActionListener(new ActionListener() { buttonRight.addActionListener(e -> {
@Override if (_warship != null) _warship.MoveTransport(Direction.Right);
public void actionPerformed(ActionEvent e) { Draw();
if(_warship != null) _warship.MoveTransport(Direction.Right);
Draw();
}
}); });
buttonUp.addActionListener(new ActionListener() { buttonUp.addActionListener(e -> {
@Override if (_warship != null) _warship.MoveTransport(Direction.Up);
public void actionPerformed(ActionEvent e) { Draw();
if(_warship != null) _warship.MoveTransport(Direction.Up);
Draw();
}
}); });
buttonDown.addActionListener(new ActionListener() { buttonDown.addActionListener(e -> {
@Override if (_warship != null) _warship.MoveTransport(Direction.Down);
public void actionPerformed(ActionEvent e) { Draw();
if(_warship != null) _warship.MoveTransport(Direction.Down);
Draw();
}
}); });
drawPanel.addComponentListener(new ComponentAdapter() { mainPanel.addComponentListener(new ComponentAdapter() {
@Override @Override
public void componentResized(ComponentEvent e) { public void componentResized(ComponentEvent e) {
_warship.ChangeBorders(drawPanel.getWidth(),drawPanel.getHeight()); super.componentResized(e);
Draw(); resizeWindow();
} }
}); });
} }
public int BlockRandom(){
Random rnd = new Random();
int count = rnd.nextInt(0,3);
if(count == 0) return 2;
if(count == 1) return 4;
if(count == 2) return 6;
return 0;
}
} }

372
src/FormWarshipConfig.form Normal file
View File

@ -0,0 +1,372 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="FormWarshipConfig">
<grid id="27dc6" binding="mainPanel" 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>
<xy x="20" y="20" width="984" height="533"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<grid id="19d1e" binding="groupBoxConfig" layout-manager="GridLayoutManager" row-count="12" 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="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>
<component id="ada62" 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="Speed"/>
</properties>
</component>
<component id="be9ce" class="javax.swing.JLabel" binding="labelWeight">
<constraints>
<grid row="3" 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="Weight"/>
</properties>
</component>
<component id="9c57c" class="javax.swing.JCheckBox" binding="checkBoxBodyKit">
<constraints>
<grid row="4" column="0" row-span="3" col-span="2" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Sign of a side platform"/>
</properties>
</component>
<grid id="c9236" binding="groupBoxColors" layout-manager="GridLayoutManager" row-count="3" column-count="4" 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="2" row-span="7" 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="8803" binding="redPanel" 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="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<background color="-65536"/>
</properties>
<border type="none"/>
<children/>
</grid>
<grid id="474b9" binding="greenPanel" 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="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<background color="-16753150"/>
</properties>
<border type="none"/>
<children/>
</grid>
<grid id="cbdc6" binding="bluePanel" 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="2" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<background color="-16776999"/>
</properties>
<border type="none"/>
<children/>
</grid>
<grid id="8795d" binding="whitePanel" 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="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<background color="-1181441"/>
</properties>
<border type="none"/>
<children/>
</grid>
<grid id="8f241" binding="grayPanel" 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="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<background color="-11776175"/>
</properties>
<border type="none"/>
<children/>
</grid>
<grid id="a4633" binding="blackPanel" 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="2" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<background color="-16777216"/>
</properties>
<border type="none"/>
<children/>
</grid>
<grid id="d67c9" binding="yellowPanel" 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="3" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<background color="-1024"/>
</properties>
<border type="none"/>
<children/>
</grid>
<grid id="c75ea" binding="purplePanel" 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="3" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<background color="-11009884"/>
</properties>
<border type="none"/>
<children/>
</grid>
<component id="48949" class="javax.swing.JLabel" binding="labelSimpleObject">
<constraints>
<grid row="2" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="100" height="20"/>
<preferred-size width="100" height="20"/>
<maximum-size width="100" height="20"/>
</grid>
</constraints>
<properties>
<text value="Simple"/>
</properties>
</component>
<component id="27b7d" class="javax.swing.JLabel" binding="labelModifiedObject">
<constraints>
<grid row="2" column="2" row-span="1" col-span="2" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="100" height="20"/>
<preferred-size width="100" height="20"/>
<maximum-size width="100" height="20"/>
</grid>
</constraints>
<properties>
<text value="Modified"/>
</properties>
</component>
</children>
</grid>
<hspacer id="a1a59">
<constraints>
<grid row="9" column="2" row-span="1" col-span="3" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<component id="1f2e0" class="javax.swing.JCheckBox" binding="checkBoxSuperEngine">
<constraints>
<grid row="8" column="0" row-span="2" col-span="2" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Sign of super engine"/>
</properties>
</component>
<component id="73317" class="javax.swing.JSpinner" binding="speedSpinner">
<constraints>
<grid row="2" column="1" 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="a9d95" class="javax.swing.JSpinner" binding="weightSpinner">
<constraints>
<grid row="3" column="1" 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="fa2f1" class="javax.swing.JLabel" binding="paramLabel">
<constraints>
<grid row="1" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Settings"/>
</properties>
</component>
<component id="1792" class="javax.swing.JLabel" binding="colorLabel">
<constraints>
<grid row="1" column="2" row-span="1" col-span="3" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Colors"/>
</properties>
</component>
<component id="c7b6d" class="javax.swing.JSpinner" binding="blocksSpinner">
<constraints>
<grid row="10" column="1" 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="b107c" class="javax.swing.JLabel" binding="labelBlocks">
<constraints>
<grid row="10" 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="Blocks"/>
</properties>
</component>
<component id="48680" class="javax.swing.JLabel" binding="labelRectangleBlocks">
<constraints>
<grid row="10" column="2" 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="Rectangle"/>
</properties>
</component>
<component id="c6b0e" class="javax.swing.JLabel" binding="labelRoundRectangleBLocks">
<constraints>
<grid row="10" 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="RoundRectangle"/>
</properties>
</component>
<component id="fce3e" class="javax.swing.JLabel" binding="labelRoundBlocks">
<constraints>
<grid row="10" 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="Round"/>
</properties>
</component>
<vspacer id="a34d0">
<constraints>
<grid row="11" column="0" row-span="1" col-span="5" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
<vspacer id="4e2b9">
<constraints>
<grid row="0" column="0" row-span="1" col-span="5" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
<component id="4ecc1" class="javax.swing.JCheckBox" binding="checkBoxCabin">
<constraints>
<grid row="7" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Sign of cabin"/>
</properties>
</component>
</children>
</grid>
<grid id="b39ac" binding="panelObject" 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="0" column="1" 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="8cde0" binding="pictureBox" layout-manager="BorderLayout" hgap="0" vgap="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">
<minimum-size width="300" height="300"/>
<preferred-size width="300" height="300"/>
<maximum-size width="300" height="300"/>
</grid>
</constraints>
<properties/>
<border type="none"/>
<children/>
</grid>
<component id="81f02" class="javax.swing.JLabel" binding="labelBaseColor">
<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="70" height="30"/>
<preferred-size width="70" height="30"/>
<maximum-size width="70" height="30"/>
</grid>
</constraints>
<properties>
<text value="Color"/>
</properties>
</component>
<component id="d1a3b" class="javax.swing.JLabel" binding="labelDopColor">
<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="70" height="30"/>
<preferred-size width="70" height="30"/>
<maximum-size width="70" height="30"/>
</grid>
</constraints>
<properties>
<text value="DopColor"/>
</properties>
</component>
<component id="5b8b0" class="javax.swing.JLabel" binding="labelSetBlocks">
<constraints>
<grid row="0" 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="70" height="30"/>
<preferred-size width="70" height="30"/>
<maximum-size width="70" height="30"/>
</grid>
</constraints>
<properties>
<text value="Blocks"/>
</properties>
</component>
<component id="940fd" class="javax.swing.JButton" binding="buttonAdd">
<constraints>
<grid row="2" 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="Add"/>
</properties>
</component>
<component id="bb5c5" 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="Delete"/>
</properties>
</component>
</children>
</grid>
</children>
</grid>
</form>

175
src/FormWarshipConfig.java Normal file
View File

@ -0,0 +1,175 @@
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.function.Consumer;
public class FormWarshipConfig extends JFrame {
private JPanel mainPanel;
private JPanel groupBoxConfig;
private JLabel labelSpeed;
private JLabel labelWeight;
private JCheckBox checkBoxBodyKit;
private JPanel groupBoxColors;
private JPanel redPanel;
private JPanel greenPanel;
private JPanel bluePanel;
private JPanel whitePanel;
private JPanel grayPanel;
private JPanel blackPanel;
private JPanel yellowPanel;
private JPanel purplePanel;
private JLabel labelSimpleObject;
private JLabel labelModifiedObject;
private JCheckBox checkBoxSuperEngine;
private JSpinner speedSpinner;
private JSpinner weightSpinner;
private JLabel paramLabel;
private JLabel colorLabel;
private JSpinner blocksSpinner;
private JLabel labelBlocks;
private JLabel labelRectangleBlocks;
private JLabel labelRoundRectangleBLocks;
private JLabel labelRoundBlocks;
private JPanel panelObject;
private JPanel pictureBox;
private JLabel labelBaseColor;
private JLabel labelDopColor;
private JLabel labelSetBlocks;
private JCheckBox checkBoxCabin;
private JButton buttonAdd;
private JButton buttonCancel;
Consumer<DrawingWarship> EventAddWarship;
DrawingWarship _warship;
BlockCount _blocks;
public FormWarshipConfig(){
InitializeComponent();
}
public void AddEvent(Consumer<DrawingWarship> ev) { EventAddWarship = ev; }
private void InitializeComponent(){
setTitle("Warship");
setSize(1000, 450);
getContentPane().add(mainPanel);
labelSimpleObject.setBorder(BorderFactory.createLineBorder(Color.BLACK));
labelModifiedObject.setBorder(BorderFactory.createLineBorder(Color.BLACK));
labelBaseColor.setBorder(BorderFactory.createLineBorder(Color.BLACK));
labelDopColor.setBorder(BorderFactory.createLineBorder(Color.BLACK));
labelRectangleBlocks.setBorder(BorderFactory.createLineBorder(Color.BLACK));
labelRoundBlocks.setBorder(BorderFactory.createLineBorder(Color.BLACK));
labelRoundRectangleBLocks.setBorder(BorderFactory.createLineBorder(Color.BLACK));
labelSetBlocks.setBorder(BorderFactory.createLineBorder(Color.BLACK));
pictureBox.setBorder(BorderFactory.createLineBorder(Color.BLACK));
speedSpinner.setModel(new SpinnerNumberModel(100, 100, 1000, 1));
weightSpinner.setModel(new SpinnerNumberModel(100, 100, 1000, 1));
blocksSpinner.setModel(new SpinnerNumberModel(2, 2, 6, 2));
MouseAdapter drag = new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
setCursor(new Cursor(Cursor.HAND_CURSOR));
}
@Override
public void mouseReleased(MouseEvent e) {
setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
Drop((JComponent) e.getSource());
}
};
MouseAdapter defCursor = new MouseAdapter() {
@Override
public void mouseExited(MouseEvent e) {
setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
}
};
pictureBox.addMouseListener(defCursor);
labelBaseColor.addMouseListener(defCursor);
labelDopColor.addMouseListener(defCursor);
labelSetBlocks.addMouseListener(defCursor);
redPanel.addMouseListener(drag);
greenPanel.addMouseListener(drag);
bluePanel.addMouseListener(drag);
yellowPanel.addMouseListener(drag);
whitePanel.addMouseListener(drag);
grayPanel.addMouseListener(drag);
blackPanel.addMouseListener(drag);
purplePanel.addMouseListener(drag);
labelSimpleObject.addMouseListener(drag);
labelModifiedObject.addMouseListener(drag);
labelRectangleBlocks.addMouseListener(drag);
labelRoundBlocks.addMouseListener(drag);
labelRoundRectangleBLocks.addMouseListener(drag);
buttonAdd.addActionListener(e -> {
EventAddWarship.accept(_warship);
dispose();
});
buttonCancel.addActionListener(e -> dispose());
}
public void Drop(JComponent droppedItem) {
if (droppedItem == null) {
return;
}
if (droppedItem instanceof JPanel panel) {
if (_warship == null)
return;
//Установка основного цвета
if (labelBaseColor.getMousePosition() != null) {
_warship.SetColor(panel.getBackground());
}
//Установка дополнительного цвета
if (labelDopColor.getMousePosition() != null && _warship instanceof DrawingAircraftCarrier aircraftCarrier) {
aircraftCarrier.SetDopColor(panel.getBackground());
}
}
if (droppedItem instanceof JLabel label && pictureBox.getMousePosition() != null) {
int speed = (int)speedSpinner.getValue();
int weight = (int)weightSpinner.getValue();
int blocks = (int)blocksSpinner.getValue();
boolean bodyKit = checkBoxBodyKit.isSelected();
boolean cabin = checkBoxCabin.isSelected();
boolean superEngine = checkBoxSuperEngine.isSelected();
//Обычный объект
if (label == labelSimpleObject) {
try { pictureBox.remove(_warship); } catch (Exception ex) { }
_warship = new DrawingWarship(speed, weight, Color.WHITE, blocks);
}
//Модифицированный объект
else if (label == labelModifiedObject) {
try { pictureBox.remove(_warship); } catch (Exception ex) { }
_warship = new DrawingAircraftCarrier(speed, weight, Color.WHITE, Color.WHITE, blocks, bodyKit, cabin, superEngine);
}
if (_warship != null) {
_warship.SetPosition(pictureBox.getWidth() - 200, pictureBox.getHeight() - 150, pictureBox.getWidth(), pictureBox.getHeight());
pictureBox.add(_warship, BorderLayout.CENTER);
revalidate();
}
} else if (droppedItem instanceof JLabel label && labelSetBlocks.getMousePosition() != null && _warship != null) {
//Квадратные блоки
if (label == labelRectangleBlocks) {
_warship.SetBlocks((int)blocksSpinner.getValue(), new DrawingBlocks(_blocks));
}
//Круглые блоки
else if (label == labelRoundBlocks) {
_warship.SetBlocks((int)blocksSpinner.getValue(), new DrawingRoundBlocks(_blocks));
}
//Закругленные блоки
else if (label == labelRoundRectangleBLocks) {
_warship.SetBlocks((int)blocksSpinner.getValue(), new DrawingRoundRectangleBlocks(_blocks));
}
}
}
}

205
src/FormWarshipCreator.form Normal file
View File

@ -0,0 +1,205 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="FormWarshipCreator">
<grid id="27dc6" binding="PictureBox" 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>
<xy x="20" y="20" width="838" height="633"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<grid id="b8cad" binding="SettingsPanel" layout-manager="GridLayoutManager" row-count="9" 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="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<opaque value="true"/>
</properties>
<border type="etched" title="Settings"/>
<children>
<grid id="41669" binding="BlocksTypePanel" layout-manager="GridLayoutManager" row-count="3" 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="6" 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="etched" title="Blocks type"/>
<children>
<component id="40e8a" class="javax.swing.JRadioButton" binding="RoundRectangleFormRadioButton">
<constraints>
<grid row="2" column="0" 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="Round Rectangle blocks"/>
</properties>
</component>
<component id="339d6" class="javax.swing.JRadioButton" binding="RoundFormRadioButton">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<label value="Round blocks"/>
<text value="Round blocks"/>
</properties>
</component>
<component id="ad164" class="javax.swing.JRadioButton" binding="RectangleFormRadioButton">
<constraints>
<grid row="0" column="0" 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="Rectangle blocks"/>
</properties>
</component>
</children>
</grid>
<grid id="15d71" layout-manager="GridLayoutManager" row-count="2" 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="8" 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>
<component id="a819c" class="javax.swing.JButton" binding="CreateButton">
<constraints>
<grid row="0" 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>
<label value="Create"/>
<text value="Create"/>
</properties>
</component>
<component id="821c8" class="javax.swing.JButton" binding="SelectButton">
<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>
<label value="Select"/>
<text value="Select"/>
</properties>
</component>
</children>
</grid>
<vspacer id="6c05e">
<constraints>
<grid row="7" 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>
<grid id="21a40" binding="CountOfBlocksPanel" layout-manager="GridLayoutManager" row-count="3" 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="4" 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="etched" title="Number of blocks"/>
<children>
<component id="e77e" class="javax.swing.JRadioButton" binding="SixRadioButton">
<constraints>
<grid row="2" column="0" 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="6"/>
</properties>
</component>
<component id="90578" class="javax.swing.JRadioButton" binding="FourRadioButton">
<constraints>
<grid row="1" column="0" 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="4"/>
</properties>
</component>
<component id="d9b90" class="javax.swing.JRadioButton" binding="TwoRadioButton">
<constraints>
<grid row="0" column="0" 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="2"/>
</properties>
</component>
</children>
</grid>
<vspacer id="f0649">
<constraints>
<grid row="1" 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>
<vspacer id="18811">
<constraints>
<grid row="3" 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>
<vspacer id="1084d">
<constraints>
<grid row="5" 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>
<grid id="7b5d7" binding="TypePanel" 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="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="etched" title="Type"/>
<children>
<component id="b0d54" class="javax.swing.JComboBox" binding="comboBoxType">
<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="Warship"/>
<item value="AircraftCarrier"/>
</model>
</properties>
</component>
</children>
</grid>
<grid id="5220b" binding="ModificationPanel" layout-manager="GridLayoutManager" row-count="3" 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="2" 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="etched" title="Modification"/>
<children>
<component id="c96ee" class="javax.swing.JCheckBox" binding="EngineCheckBox">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<label value="Super engine"/>
<text value="Super engine"/>
</properties>
</component>
<component id="385c6" class="javax.swing.JCheckBox" binding="CabinCheckBox">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<label value="Cabin"/>
<text value="Cabin"/>
</properties>
</component>
<component id="63fa9" class="javax.swing.JCheckBox" binding="AreaCheckBox">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<label value="Landing area"/>
<text value="Landing area"/>
</properties>
</component>
</children>
</grid>
</children>
</grid>
<hspacer id="2bb5c">
<constraints>
<grid row="0" 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>
</children>
</grid>
</form>

140
src/FormWarshipCreator.java Normal file
View File

@ -0,0 +1,140 @@
import javax.swing.*;
import java.awt.*;
import java.util.Random;
public class FormWarshipCreator extends JDialog{
private JPanel PictureBox;
private JPanel SettingsPanel;
private JPanel BlocksTypePanel;
private JPanel CountOfBlocksPanel;
private JPanel ModificationPanel;
private JPanel TypePanel;
private JRadioButton RoundRectangleFormRadioButton;
private JRadioButton RoundFormRadioButton;
private JRadioButton RectangleFormRadioButton;
private JRadioButton SixRadioButton;
private JRadioButton FourRadioButton;
private JRadioButton TwoRadioButton;
private JButton CreateButton;
private JButton SelectButton;
private JCheckBox EngineCheckBox;
private JCheckBox CabinCheckBox;
private JCheckBox AreaCheckBox;
private JComboBox comboBoxType;
private BlockCount _block = null;
private IDrawingObjectBlock objectBlock = null;
private final WarshipCreatorGeneric<EntityWarship,IDrawingObjectBlock> warshipCreator =new WarshipCreatorGeneric<>(40,40);
private DrawingWarship _warship;
private DrawingWarship selectedWarship;
private boolean isModified = false;
public FormWarshipCreator(){
InitializeComponent();
}
@Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) PictureBox.getGraphics();
if (_warship != null) {
_warship.paintComponents(g2d);
}
}
public DrawingWarship getSelectedWarship() {
return selectedWarship;
}
private void InitializeComponent(){
setContentPane(PictureBox);
setTitle("Warship");
setResizable(false);
setSize(1000, 600);
//Определение типа объекта
comboBoxType.addItemListener(e -> isModified = e.getItem().toString().equals("AircraftCarrier"));
//количество блоков
TwoRadioButton.addActionListener(e -> {
_block = BlockCount.TwoBlocks;
});
FourRadioButton.addActionListener(e -> {
_block = BlockCount.FourBlocks;
});
SixRadioButton.addActionListener(e -> {
_block = BlockCount.SixBlocks;
});
//Квадратная форма блоков
RectangleFormRadioButton.addActionListener(e -> {
if(_block == null) return;
objectBlock = new DrawingBlocks(_block);
objectBlock.SetBlockCount(_block.GetBlockCount());
warshipCreator.AddBlock(objectBlock);
});
//Круглая форма блоков
RoundFormRadioButton.addActionListener(e -> {
if(_block ==null){
return;
}
objectBlock = new DrawingRoundBlocks(_block);
objectBlock.SetBlockCount(_block.GetBlockCount());
warshipCreator.AddBlock(objectBlock);
});
//Закруглено-квадратная форма блоков
RoundRectangleFormRadioButton.addActionListener(e -> {
if(_block == null){
return;
}
objectBlock = new DrawingRoundBlocks(_block);
objectBlock.SetBlockCount(_block.GetBlockCount());
warshipCreator.AddBlock(objectBlock);
});
CreateButton.addActionListener(e -> {
Random rnd = new Random();
if (_block != null && objectBlock != null) {
Color color = JColorChooser.showDialog(this, "Choose color", Color.WHITE);
if (isModified) {
Color additionalColor = JColorChooser.showDialog(this, "Choose color", Color.GRAY);
warshipCreator.AddWarship(
new EntityAircraftCarrier(
rnd.nextInt(10, 300),
rnd.nextFloat(10, 3000),
color,
additionalColor,
AreaCheckBox.isSelected(),
CabinCheckBox.isSelected(),
EngineCheckBox.isSelected()
)
);
}
else {
warshipCreator.AddWarship(
new EntityWarship(
rnd.nextInt(10, 300),
rnd.nextFloat(10, 3000),
color)
);
}
_warship = warshipCreator.NewWarshipCreating();
_warship.SetPosition(
rnd.nextInt(20),
rnd.nextInt(20),
PictureBox.getWidth(),
PictureBox.getHeight());
repaint();
}
});
SelectButton.addActionListener(e -> {
selectedWarship = _warship;
dispose();
});
}
}

17
src/IDrawingObject.java Normal file
View File

@ -0,0 +1,17 @@
import java.awt.*;
public interface IDrawingObject {
// Шаг перемещения объекта
public float Step = 0;
// Установка позиции объекта
void SetObject(int x, int y, int width, int height);
// Изменение направления пермещения объекта
void MoveObject(Direction direction);
// Отрисовка объекта
void DrawingObject(Graphics g);
// Получение текущей позиции объекта
// /Left, Right, Top, Bottom)
float[] GetCurrentPosition();
//Получение информации по объекту
String GetInfo();
}

View File

@ -0,0 +1,7 @@
import java.awt.*;
public interface IDrawingObjectBlock {
void SetBlockCount(int count);
int GetBlockCount();
void DrawBlocks(Graphics2D g, int _startPosX, int _startPosY);
}

53
src/LineMap.java Normal file
View File

@ -0,0 +1,53 @@
import java.awt.*;
public class LineMap extends AbstractMap{
@Override
protected void DrawBarrierPart(Graphics g, int i, int j)
{
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.gray);
g2.fillRect((int) (i * _size_x), (int) (j * _size_y), (int) (i * (_size_x + 1)), (int) (j * (_size_y + 1)));
}
@Override
protected void DrawRoadPart(Graphics g, int i, int j)
{
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.BLUE);
g2.fillRect((int) (i * _size_x), (int) (j * _size_y), (int) (i * (_size_x + 1)), (int) (j * (_size_y + 1)));
}
@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[i].length; ++j)
{
_map[i][j] = _freeRoad;
}
}
boolean flag = true;
while (counter < 20)
{
int lineX = _random.nextInt(11, 89);
int lineY = _random.nextInt(11, 89);
if (flag)
{
for (int i = lineY; i <= lineY + 10; i++)
_map[lineX][i] = _barrier;
flag = false;
}
else
{
for (int i = lineX; i <= lineX + 10; i++)
_map[i][lineY] = _barrier;
flag = true;
}
counter++;
}
}
}

View File

@ -1,6 +1,5 @@
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
new FormWarship(); new FormMapWithSetWarships();
} }
} }

View File

@ -0,0 +1,176 @@
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Stack;
public class MapWithSetWarshipsGeneric <T extends IDrawingObject,U extends AbstractMap> {
private final int _pictureWidth;
private final int _pictureHeight;
private final int _placeSizeWidth = 120;
private final int _placeSizeHeight = 50;
private final SetWarshipsGeneric<T> _setWarships;
private Stack<T> _deletedWarship;
private final U _map;
public MapWithSetWarshipsGeneric(int picWidth, int picHeight, U map)
{
int width = picWidth / _placeSizeWidth;
int height = picHeight/_placeSizeHeight;
_setWarships = new SetWarshipsGeneric<T>(width * height);
_pictureWidth = picWidth;
_pictureHeight = picHeight;
_map = map;
_deletedWarship = new Stack<>();
}
public int Plus(T warship)
{
return _setWarships.Insert(warship);
}
public T Minus(int position)
{
T warship=_setWarships.Remove(position);
_deletedWarship.push(warship);
return warship;
}
public Image ShowSet()
{
BufferedImage bmp = new BufferedImage(_pictureWidth, _pictureWidth,BufferedImage.TYPE_INT_ARGB);
Graphics gr = bmp.getGraphics();
DrawBackground(gr);
DrawWarship(gr);
return bmp;
}
public Image ShowOnMap()
{
Shaking();
for (int i = 0; i < _setWarships.getCount(); i++)
{
T warship = _setWarships.Get(i);
if (warship != null)
{
return _map.CreateMap(_pictureWidth, _pictureHeight, warship);
}
}
return new BufferedImage(_pictureWidth, _pictureHeight, 1);
}
public Image MoveObject(Direction direction)
{
if (_map != null)
{
return _map.MoveObject(direction);
}
return new BufferedImage(_pictureWidth, _pictureHeight, 1);
}
public void Shaking()
{
int j = _setWarships.getCount() - 1;
for (int i = 0; i < _setWarships.getCount(); i++)
{
if (_setWarships.Get(i) == null)
{
for (; j > i; j--)
{
var warship = _setWarships.Get(j);
if (warship != null)
{
_setWarships.Insert(warship, i);
_setWarships.Remove(j);
break;
}
}
if (j <= i)
{
return;
}
}
}
}
private void DrawBackground(Graphics gr)
{
Graphics2D g = (Graphics2D)gr;
Color brush=Color.BLACK;
Stroke penWide = new BasicStroke(3);
Stroke penThin = new BasicStroke(1);
for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++)
{
for (int j = 0; j < _pictureHeight / _placeSizeHeight ; ++j)
{
g.setColor(brush);
g.setStroke(penWide);
g.drawLine(i * _placeSizeWidth + 20, j * _placeSizeHeight+2, i * _placeSizeWidth + (int)(_placeSizeWidth*0.8), j * _placeSizeHeight+2);
g.drawLine(i * _placeSizeWidth + (int)(_placeSizeWidth * 0.8), j * _placeSizeHeight + 2, i * _placeSizeWidth + _placeSizeWidth, j * _placeSizeHeight + _placeSizeHeight/2);
g.drawLine(i * _placeSizeWidth + _placeSizeWidth, j * _placeSizeHeight + _placeSizeHeight / 2, i * _placeSizeWidth+ (int)(_placeSizeWidth * 0.8), j * _placeSizeHeight + _placeSizeHeight);
g.setStroke(penThin);
}
}
}
private void DrawWarship(Graphics gr)
{
int width = _pictureWidth / _placeSizeWidth;
int height = _pictureHeight / _placeSizeHeight;
for (int i = 0; i < _setWarships.getCount(); i++)
{
if (_setWarships.Get(i) != null)
{
_setWarships.Get(i).SetObject(i % width * _placeSizeWidth, ((height - 1 - i / width) * _placeSizeHeight) + 6, _pictureWidth, _pictureHeight);
_setWarships.Get(i).DrawingObject(gr);
}
}
}
public T GetWarshipInList(int ind){
return _setWarships.Get(ind);
}
public T GetWarshipInDeleted()
{
if(_deletedWarship.empty())
return null;
return _deletedWarship.pop();
}
public String GetData(char separatorType, char separatorData)
{
String data = _map.getClass().getName()+separatorType;
for (var warship : _setWarships)
{
data += warship.GetInfo() + separatorData;
}
return data;
}
public void LoadData(String[] records)
{
for (var rec : records)
{
_setWarships.Insert((T)DrawingObjectWarship.Create(rec));
}
}
public String GetDataMap(char separatorType, char separatorData)
{
String data = _map.getClass().getName() + separatorType + '\n';
for (var warship : _setWarships)
{
data += warship.GetInfo() + separatorData+'\n';
}
return data;
}
public void Clear() {
_setWarships.Clear();
}
}

170
src/MapsCollection.java Normal file
View File

@ -0,0 +1,170 @@
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
public class MapsCollection {
private final HashMap<String, MapWithSetWarshipsGeneric<DrawingObjectWarship, AbstractMap>> _mapStorages;
public ArrayList<String> Keys() {
return new ArrayList<>(_mapStorages.keySet());
}
private final int _pictureWidth;
private final int _pictureHeight;
private final char separatorDict = '|';
private final char separatorData = ';';
public MapsCollection(int pictureWidth, int pictureHeight)
{
_mapStorages = new HashMap<>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
public void AddMap(String name, AbstractMap map)
{
if (_mapStorages.containsKey(name))
{
return;
}
_mapStorages.put(name, new MapWithSetWarshipsGeneric<>(_pictureWidth,_pictureHeight,map));
}
public void DelMap(String name)
{
_mapStorages.remove(name);
}
public MapWithSetWarshipsGeneric<DrawingObjectWarship, AbstractMap> get(String ind){
if (_mapStorages.containsKey(ind))
{
return _mapStorages.get(ind);
}
return null;
}
public DrawingObjectWarship Get(String name, int ind) {
if (_mapStorages.containsKey(name))
{
return _mapStorages.get(name).GetWarshipInList(ind);
}
return null;
}
public boolean SaveData(String filename) {
File file = new File(filename);
if (file.exists()) {
file.delete();
}
try (BufferedWriter br = new BufferedWriter(new FileWriter(filename)))
{
br.write("MapsCollection\n");
for (var storage : _mapStorages.entrySet()) {
br.write(storage.getKey() + separatorDict + storage.getValue().GetData(separatorDict, separatorData) + "\n");
}
} catch (IOException e) {
throw new RuntimeException(e);
}
return true;
}
public boolean LoadData(String filename)
{
if (!(new File(filename).exists()))
{
return false;
}
try (BufferedReader br = new BufferedReader(new FileReader(filename)))
{
String str = "";
if ((str = br.readLine()) == null || !str.contains("MapsCollection"))
{
return false;
}
//очищаем записи
_mapStorages.clear();
while ((str = br.readLine()) != null)
{
var elem = str.split(String.format("\\%c",separatorDict));
AbstractMap map = null;
switch (elem[1])
{
case "SimpleMap":
map = new SimpleMap();
break;
case "LineMap":
map = new LineMap();
break;
}
_mapStorages.put(elem[0], new MapWithSetWarshipsGeneric<>(_pictureWidth, _pictureHeight, map));
_mapStorages.get(elem[0]).LoadData(elem[2].split(separatorData + "\n?"));
}
} catch (IOException e) {
throw new RuntimeException(e);
}
return true;
}
//Сохранение отдельного объекта класса-хранилища
public boolean SaveMap(String filename, String index) {
File file = new File(filename);
if (file.exists()) {
file.delete();
}
var elem = _mapStorages.get(index);
if (elem==null)
return false;
try (BufferedWriter br = new BufferedWriter(new FileWriter(filename)))
{
br.write("MapsCollection\n");
br.write(index + separatorDict);
br.write(elem.GetDataMap(separatorDict, separatorData));
} catch (IOException e) {
throw new RuntimeException(e);
}
return true;
}
//Загрузка отдельного объекта класса-хранилища
public boolean LoadMap(String filename) {
if (!(new File(filename).exists()))
{
return false;
}
try (BufferedReader br = new BufferedReader(new FileReader(filename)))
{
String str = "";
if ((str = br.readLine()) == null || !str.contains("MapsCollection"))
{
//если нет такой записи, то это не те данные
return false;
}
if ((str = br.readLine()) != null)
{
var elem = str.split(String.format("\\%c",separatorDict));
AbstractMap map = null;
switch (elem[1])
{
case "SimpleMap":
map = new SimpleMap();
break;
case "LineMap":
map = new LineMap();
break;
}
if(_mapStorages.containsKey(elem[0])){
_mapStorages.get(elem[0]).Clear();
}
else{
_mapStorages.put(elem[0],new MapWithSetWarshipsGeneric<>(_pictureWidth,_pictureHeight,map));
}
while((str = br.readLine()) != null) {
_mapStorages.get(elem[0]).LoadData(str.split(separatorData + "\n?"));
}
}
} catch (IOException e) {
throw new RuntimeException(e);
}
return true;
}
}

View File

@ -0,0 +1,54 @@
import java.util.ArrayList;
import java.util.Iterator;
public class SetWarshipsGeneric<T extends Object> implements Iterable<T>{
private ArrayList<T> _places;
public int getCount() {
return _places.size();
}
private final int _maxCount;
public SetWarshipsGeneric(int count)
{
_places = new ArrayList<>();
_maxCount = count;
}
public int Insert(T warship)
{
return Insert(warship,0);
}
public int Insert(T warship, int position)
{
if (position < 0 || position >= _maxCount)
return -1;
_places.add(position, warship);
return position;
}
public T Remove(int position)
{
if (position >= _maxCount || position < 0)
return null;
T deleted = _places.get(position);
_places.remove(position);
return deleted;
}
public T Get(int position)
{
if (position > getCount() || position < 0)
return null;
return _places.get(position);
}
@Override
public Iterator<T> iterator() {
return _places.iterator();
}
public void Clear() {
_places.clear();
}
}

43
src/SimpleMap.java Normal file
View File

@ -0,0 +1,43 @@
import java.awt.*;
public class SimpleMap extends AbstractMap{
@Override
protected void DrawBarrierPart(Graphics g, int i, int j)
{
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.gray);
g2.fillRect((int) (i * _size_x), (int) (j * _size_y), (int) (i * (_size_x + 1)), (int) (j * (_size_y + 1)));
}
@Override
protected void DrawRoadPart(Graphics g, int i, int j)
{
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.BLUE);
g2.fillRect((int) (i * _size_x), (int) (j * _size_y), (int) (i * (_size_x + 1)), (int) (j * (_size_y + 1)));
}
@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[i].length; ++j)
{
_map[i][j] = _freeRoad;
}
}
while (counter < 50)
{
int x = _random.nextInt(0, 100);
int y = _random.nextInt(0, 100);
if (_map[x][y] == _freeRoad)
{
_map[x][y] = _barrier;
counter++;
}
}
}
}

View File

@ -0,0 +1,43 @@
import java.util.ArrayList;
import java.util.Random;
public class WarshipCreatorGeneric <T extends EntityWarship, U extends IDrawingObjectBlock>{
private final ArrayList<T> Warships;
private final ArrayList<U> Blocks;
private int WarshipsCount = 0;
private int BlocksCount = 0;
public WarshipCreatorGeneric(int warshipsCount, int blocksCount){
Warships = new ArrayList<>(warshipsCount);
Blocks = new ArrayList<>(blocksCount);
}
public int AddWarship(T warship){
if(WarshipsCount <= Warships.size()){
Warships.add(warship);
WarshipsCount++;
return WarshipsCount - 1;
}
return -1;
}
public int AddBlock(U block){
if(BlocksCount <= Blocks.size()){
Blocks.add(block);
BlocksCount++;
return BlocksCount - 1;
}
return -1;
}
public DrawingWarship NewWarshipCreating()
{
Random rand=new Random();
T warship = (T)Warships.get(rand.nextInt(WarshipsCount));
U block = (U)Blocks.get(rand.nextInt(BlocksCount));
if(warship instanceof EntityAircraftCarrier){
return new DrawingAircraftCarrier(warship, block);
}
return new DrawingWarship(warship,block);
}
}