Compare commits
30 Commits
Author | SHA1 | Date | |
---|---|---|---|
99da8dad3d | |||
a8d164c663 | |||
3047272489 | |||
afaed7e316 | |||
74284dde17 | |||
a7d0e609db | |||
7fb34d7482 | |||
3e82235fc8 | |||
49c7245c3b | |||
234aa190c8 | |||
22c6e38b92 | |||
d210db6966 | |||
c526d2cfc1 | |||
150d41e1a5 | |||
0190f57a9c | |||
9549cdbfa5 | |||
766efc216d | |||
957775120d | |||
42e878848f | |||
96a9477a8f | |||
b0b474077f | |||
b9c29806b0 | |||
be5dba9a9a | |||
3124dd9292 | |||
3f53243841 | |||
ee5ebc2b81 | |||
0ea7fea345 | |||
4ff33ecdf2 | |||
5c97eb3db4 | |||
afef85366f |
@ -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_1_8" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
|
<component name="ProjectRootManager" version="2" languageLevel="JDK_19" project-jdk-name="openjdk-21" project-jdk-type="JavaSDK">
|
||||||
<output url="file://$PROJECT_DIR$/out" />
|
<output url="file://$PROJECT_DIR$/out" />
|
||||||
</component>
|
</component>
|
||||||
</project>
|
</project>
|
BIN
images/down.png
Normal file
BIN
images/down.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 256 B |
BIN
images/left.png
Normal file
BIN
images/left.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 269 B |
BIN
images/right.png
Normal file
BIN
images/right.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 257 B |
BIN
images/up.png
Normal file
BIN
images/up.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 250 B |
@ -1,5 +1,7 @@
|
|||||||
|
import frames.*;
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args){
|
||||||
System.out.println("Hello world!");
|
new FrameShipsCollection();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
7
src/drawing_objects/BlocksNumber.java
Normal file
7
src/drawing_objects/BlocksNumber.java
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
package drawing_objects;
|
||||||
|
|
||||||
|
public enum BlocksNumber {
|
||||||
|
TWO,
|
||||||
|
FOUR,
|
||||||
|
SIX
|
||||||
|
}
|
8
src/drawing_objects/DirectionType.java
Normal file
8
src/drawing_objects/DirectionType.java
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
package drawing_objects;
|
||||||
|
|
||||||
|
public enum DirectionType {
|
||||||
|
UP,
|
||||||
|
DOWN,
|
||||||
|
LEFT,
|
||||||
|
RIGHT
|
||||||
|
}
|
50
src/drawing_objects/DrawingBattleship.java
Normal file
50
src/drawing_objects/DrawingBattleship.java
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
package drawing_objects;
|
||||||
|
|
||||||
|
import entities.EntityBattleship;
|
||||||
|
|
||||||
|
import java.awt.*;
|
||||||
|
|
||||||
|
public class DrawingBattleship extends DrawingShip {
|
||||||
|
public DrawingBattleship(int speed, double weight, Color bodyColor, Color additionalColor, boolean turret,
|
||||||
|
boolean rocketLauncher, int width, int height, int blocksType, int blocksNumber) {
|
||||||
|
super(speed, weight, bodyColor, width, height, blocksType, blocksNumber);
|
||||||
|
if (entityShip != null)
|
||||||
|
entityShip = new EntityBattleship(speed, weight, bodyColor, additionalColor, turret, rocketLauncher);
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public void drawTransport(Graphics2D graphics2D) {
|
||||||
|
if (!(entityShip instanceof EntityBattleship))
|
||||||
|
return;
|
||||||
|
BasicStroke pen = new BasicStroke(2);
|
||||||
|
graphics2D.setStroke(pen);
|
||||||
|
Color additionalColor = ((EntityBattleship)entityShip).getAdditionalColor();
|
||||||
|
super.drawTransport(graphics2D);
|
||||||
|
//орудийная башня
|
||||||
|
if (((EntityBattleship)entityShip).getTurret()) {
|
||||||
|
int[] shieldX = new int[] {startPosX + 112, startPosX + 112, startPosX + 119, startPosX + 119, };
|
||||||
|
int[] shieldY = new int[] {startPosY + 19, startPosY + 31, startPosY + 28, startPosY + 22};
|
||||||
|
graphics2D.setPaint(additionalColor);
|
||||||
|
graphics2D.fillPolygon(shieldX, shieldY, 4);
|
||||||
|
graphics2D.fillRect(startPosX + 119, startPosY + 24, 12, 2);
|
||||||
|
graphics2D.setPaint(Color.BLACK);
|
||||||
|
graphics2D.drawPolygon(shieldX, shieldY, 4);
|
||||||
|
graphics2D.drawRect(startPosX + 119, startPosY + 24, 12, 2);
|
||||||
|
}
|
||||||
|
//ячейки для ракет
|
||||||
|
if (((EntityBattleship)entityShip).getRocketLauncher()) {
|
||||||
|
graphics2D.setPaint(additionalColor);
|
||||||
|
graphics2D.fillRect(startPosX + 14, startPosY + 14, 10, 10);
|
||||||
|
graphics2D.fillRect(startPosX + 26, startPosY + 14, 10, 10);
|
||||||
|
graphics2D.fillRect(startPosX + 14, startPosY + 26, 10, 10);
|
||||||
|
graphics2D.fillRect(startPosX + 26, startPosY + 26, 10, 10);
|
||||||
|
graphics2D.setPaint(Color.BLACK);
|
||||||
|
graphics2D.drawRect(startPosX + 14, startPosY + 14, 10, 10);
|
||||||
|
graphics2D.drawRect(startPosX + 26, startPosY + 14, 10, 10);
|
||||||
|
graphics2D.drawRect(startPosX + 14, startPosY + 26, 10, 10);
|
||||||
|
graphics2D.drawRect(startPosX + 26, startPosY + 26, 10, 10);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public void setAdditionalColor(Color color){
|
||||||
|
((EntityBattleship)entityShip).additionalColor = color;
|
||||||
|
}
|
||||||
|
}
|
44
src/drawing_objects/DrawingBlocks.java
Normal file
44
src/drawing_objects/DrawingBlocks.java
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
package drawing_objects;
|
||||||
|
|
||||||
|
import java.awt.*;
|
||||||
|
|
||||||
|
public class DrawingBlocks implements IDrawBlocks{
|
||||||
|
private BlocksNumber number;
|
||||||
|
@Override
|
||||||
|
public int getNumber() {
|
||||||
|
int x = 0;
|
||||||
|
if(number == BlocksNumber.TWO)
|
||||||
|
x = 2;
|
||||||
|
else if(number == BlocksNumber.FOUR)
|
||||||
|
x = 4;
|
||||||
|
else if(number == BlocksNumber.SIX)
|
||||||
|
x = 6;
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public void setNumber(int x){
|
||||||
|
if(x <= 2)
|
||||||
|
number = BlocksNumber.TWO;
|
||||||
|
if(x == 4)
|
||||||
|
number = BlocksNumber.FOUR;
|
||||||
|
if(x >= 6)
|
||||||
|
number = BlocksNumber.SIX;
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public int getType() {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public void drawBlocks(Graphics2D graphics2D, int _startX, int _startY){
|
||||||
|
graphics2D.fillRect(_startX+52, _startY+12, 6, 6);
|
||||||
|
graphics2D.fillRect(_startX+52, _startY+32, 6, 6);
|
||||||
|
if (number == BlocksNumber.FOUR || number == BlocksNumber.SIX){
|
||||||
|
graphics2D.fillRect(_startX+62, _startY+12, 6, 6);
|
||||||
|
graphics2D.fillRect(_startX+62, _startY+32, 6, 6);
|
||||||
|
if (number == BlocksNumber.SIX){
|
||||||
|
graphics2D.fillRect(_startX+42, _startY+12, 6, 6);
|
||||||
|
graphics2D.fillRect(_startX+42, _startY+32, 6, 6);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
50
src/drawing_objects/DrawingCrossBlocks.java
Normal file
50
src/drawing_objects/DrawingCrossBlocks.java
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
package drawing_objects;
|
||||||
|
|
||||||
|
import java.awt.*;
|
||||||
|
|
||||||
|
public class DrawingCrossBlocks implements IDrawBlocks{
|
||||||
|
private BlocksNumber number;
|
||||||
|
@Override
|
||||||
|
public int getNumber() {
|
||||||
|
int x = 0;
|
||||||
|
if(number == BlocksNumber.TWO)
|
||||||
|
x = 2;
|
||||||
|
else if(number == BlocksNumber.FOUR)
|
||||||
|
x = 4;
|
||||||
|
else if(number == BlocksNumber.SIX)
|
||||||
|
x = 6;
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public void setNumber(int x){
|
||||||
|
if(x <= 2)
|
||||||
|
number = BlocksNumber.TWO;
|
||||||
|
if(x == 4)
|
||||||
|
number = BlocksNumber.FOUR;
|
||||||
|
if(x >= 6)
|
||||||
|
number = BlocksNumber.SIX;
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public int getType() {
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public void drawBlocks(Graphics2D graphics2D, int _startX, int _startY){
|
||||||
|
graphics2D.fillRect(_startX+54, _startY+12, 2, 6);
|
||||||
|
graphics2D.fillRect(_startX+52, _startY+14, 6, 2);
|
||||||
|
graphics2D.fillRect(_startX+54, _startY+32, 2, 6);
|
||||||
|
graphics2D.fillRect(_startX+52, _startY+34, 6, 2);
|
||||||
|
if (number == BlocksNumber.FOUR || number == BlocksNumber.SIX){
|
||||||
|
graphics2D.fillRect(_startX+64, _startY+12, 2, 6);
|
||||||
|
graphics2D.fillRect(_startX+62, _startY+14, 6, 2);
|
||||||
|
graphics2D.fillRect(_startX+64, _startY+32, 2, 6);
|
||||||
|
graphics2D.fillRect(_startX+62, _startY+34, 6, 2);
|
||||||
|
if (number == BlocksNumber.SIX){
|
||||||
|
graphics2D.fillRect(_startX+44, _startY+12, 2, 6);
|
||||||
|
graphics2D.fillRect(_startX+42, _startY+14, 6, 2);
|
||||||
|
graphics2D.fillRect(_startX+44, _startY+32, 2, 6);
|
||||||
|
graphics2D.fillRect(_startX+42, _startY+34, 6, 2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
44
src/drawing_objects/DrawingRoundBlocks.java
Normal file
44
src/drawing_objects/DrawingRoundBlocks.java
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
package drawing_objects;
|
||||||
|
|
||||||
|
import java.awt.*;
|
||||||
|
|
||||||
|
public class DrawingRoundBlocks implements IDrawBlocks{
|
||||||
|
private BlocksNumber number;
|
||||||
|
@Override
|
||||||
|
public int getNumber() {
|
||||||
|
int x = 0;
|
||||||
|
if(number == BlocksNumber.TWO)
|
||||||
|
x = 2;
|
||||||
|
else if(number == BlocksNumber.FOUR)
|
||||||
|
x = 4;
|
||||||
|
else if(number == BlocksNumber.SIX)
|
||||||
|
x = 6;
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public void setNumber(int x){
|
||||||
|
if(x <= 2)
|
||||||
|
number = BlocksNumber.TWO;
|
||||||
|
if(x == 4)
|
||||||
|
number = BlocksNumber.FOUR;
|
||||||
|
if(x >= 6)
|
||||||
|
number = BlocksNumber.SIX;
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public int getType() {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public void drawBlocks(Graphics2D graphics2D, int _startX, int _startY){
|
||||||
|
graphics2D.fillOval(_startX+50, _startY+11, 8, 8);
|
||||||
|
graphics2D.fillOval(_startX+50, _startY+31, 8, 8);
|
||||||
|
if (number == BlocksNumber.FOUR || number == BlocksNumber.SIX){
|
||||||
|
graphics2D.fillOval(_startX+60, _startY+11, 8, 8);
|
||||||
|
graphics2D.fillOval(_startX+60, _startY+31, 8, 8);
|
||||||
|
if (number == BlocksNumber.SIX){
|
||||||
|
graphics2D.fillOval(_startX+40, _startY+11, 8, 8);
|
||||||
|
graphics2D.fillOval(_startX+40, _startY+31, 8, 8);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
137
src/drawing_objects/DrawingShip.java
Normal file
137
src/drawing_objects/DrawingShip.java
Normal file
@ -0,0 +1,137 @@
|
|||||||
|
package drawing_objects;
|
||||||
|
|
||||||
|
import entities.EntityShip;
|
||||||
|
import movement_strategy.*;
|
||||||
|
|
||||||
|
import java.awt.*;
|
||||||
|
|
||||||
|
public class DrawingShip {
|
||||||
|
protected EntityShip entityShip;
|
||||||
|
public EntityShip getEntityShip(){
|
||||||
|
return entityShip;
|
||||||
|
}
|
||||||
|
public IDrawBlocks drawingBlocks;
|
||||||
|
public void setDrawingBlocks(IDrawBlocks blocks){
|
||||||
|
drawingBlocks = blocks;
|
||||||
|
}
|
||||||
|
public int pictureWidth;
|
||||||
|
public int pictureHeight;
|
||||||
|
protected int startPosX;
|
||||||
|
public int getPosX() {
|
||||||
|
return startPosX;
|
||||||
|
}
|
||||||
|
protected int startPosY;
|
||||||
|
public int getPosY() {
|
||||||
|
return startPosY;
|
||||||
|
}
|
||||||
|
private int shipWidth = 150;
|
||||||
|
public int getWidth() {
|
||||||
|
return shipWidth;
|
||||||
|
}
|
||||||
|
private int shipHeight = 50;
|
||||||
|
public int getHeight() {
|
||||||
|
return shipHeight;
|
||||||
|
}
|
||||||
|
public IMoveableObject getMoveableObject() {return new DrawingObjectShip(this);}
|
||||||
|
public DrawingShip(int speed, double weight, Color bodyColor, int width, int height, int blocksType, int blocksNumber) {
|
||||||
|
if (width < shipWidth || height < shipHeight)
|
||||||
|
return;
|
||||||
|
pictureWidth = width;
|
||||||
|
pictureHeight = height;
|
||||||
|
entityShip = new EntityShip(speed, weight, bodyColor);
|
||||||
|
switch (blocksType) {
|
||||||
|
case 1 -> drawingBlocks = new DrawingRoundBlocks();
|
||||||
|
case 2 -> drawingBlocks = new DrawingCrossBlocks();
|
||||||
|
default -> drawingBlocks = new DrawingBlocks();
|
||||||
|
}
|
||||||
|
drawingBlocks.setNumber(blocksNumber);
|
||||||
|
}
|
||||||
|
protected DrawingShip(int speed, double weight, Color bodyColor, int width, int height, int shipWidth,
|
||||||
|
int shipHeight, int blocksType, int blocksNumber) {
|
||||||
|
if (width < shipWidth || height < shipHeight)
|
||||||
|
return;
|
||||||
|
pictureWidth = width;
|
||||||
|
pictureHeight = height;
|
||||||
|
this.shipWidth = shipWidth;
|
||||||
|
this.shipHeight = shipHeight;
|
||||||
|
entityShip = new EntityShip(speed, weight, bodyColor);
|
||||||
|
switch (blocksType) {
|
||||||
|
case 1 -> drawingBlocks = new DrawingRoundBlocks();
|
||||||
|
case 2 -> drawingBlocks = new DrawingCrossBlocks();
|
||||||
|
default -> drawingBlocks = new DrawingBlocks();
|
||||||
|
}
|
||||||
|
drawingBlocks.setNumber(blocksNumber);
|
||||||
|
}
|
||||||
|
public void setPosition(int x, int y) {
|
||||||
|
if (x < 0 || y < 0 || x + shipWidth > pictureWidth || y + shipHeight > pictureHeight) {
|
||||||
|
x = 0;
|
||||||
|
y = 0;
|
||||||
|
}
|
||||||
|
startPosX = x;
|
||||||
|
startPosY = y;
|
||||||
|
}
|
||||||
|
public void drawTransport(Graphics2D graphics2D) {
|
||||||
|
if (entityShip == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
BasicStroke pen = new BasicStroke(2);
|
||||||
|
graphics2D.setStroke(pen);
|
||||||
|
Color bodyColor = entityShip.getBodyColor();
|
||||||
|
//корпус
|
||||||
|
int[] hullX = new int[] {startPosX + 5, startPosX + 100, startPosX + 150, startPosX + 100, startPosX + 5};
|
||||||
|
int[] hullY = new int[] {startPosY, startPosY, startPosY + 25, startPosY + 50, startPosY + 50};
|
||||||
|
graphics2D.setPaint(bodyColor);
|
||||||
|
graphics2D.fillPolygon(hullX, hullY, 5);
|
||||||
|
graphics2D.setPaint(Color.BLACK);
|
||||||
|
graphics2D.drawPolygon(hullX, hullY, 5);
|
||||||
|
graphics2D.fillRect(startPosX, startPosY + 6, 5, 13);
|
||||||
|
graphics2D.fillRect(startPosX, startPosY + 31, 5, 13);
|
||||||
|
//надстройки
|
||||||
|
graphics2D.setPaint(Color.DARK_GRAY);
|
||||||
|
graphics2D.fillRect(startPosX + 40, startPosY + 20, 30, 10);
|
||||||
|
graphics2D.fillRect(startPosX + 70, startPosY + 12, 18, 26);
|
||||||
|
graphics2D.fillOval(startPosX + 94, startPosY + 19, 12, 12);
|
||||||
|
graphics2D.setPaint(Color.BLACK);
|
||||||
|
graphics2D.drawRect(startPosX + 40, startPosY + 20, 30, 10);
|
||||||
|
graphics2D.drawRect(startPosX + 70, startPosY + 12, 18, 26);
|
||||||
|
graphics2D.drawOval(startPosX + 94, startPosY + 19, 12, 12);
|
||||||
|
//блоки
|
||||||
|
if (drawingBlocks != null){
|
||||||
|
drawingBlocks.drawBlocks(graphics2D, startPosX, startPosY);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public boolean canMove(DirectionType direction) {
|
||||||
|
if (entityShip == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return switch (direction) {
|
||||||
|
case LEFT -> startPosX - entityShip.step.get().intValue() > 0;
|
||||||
|
case UP -> startPosY - entityShip.step.get().intValue() > 0;
|
||||||
|
case RIGHT -> startPosX + entityShip.step.get().intValue() + shipWidth < pictureWidth;
|
||||||
|
case DOWN -> startPosY + entityShip.step.get().intValue() + shipHeight < pictureHeight;
|
||||||
|
default -> false;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
public void moveTransport(DirectionType direction) {
|
||||||
|
if (!canMove(direction) || entityShip == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
switch (direction) {
|
||||||
|
//влево
|
||||||
|
case LEFT -> startPosX -= entityShip.step.get().intValue();
|
||||||
|
//вверх
|
||||||
|
case UP -> startPosY -= entityShip.step.get().intValue();
|
||||||
|
// вправо
|
||||||
|
case RIGHT -> startPosX += entityShip.step.get().intValue();
|
||||||
|
//вниз
|
||||||
|
case DOWN -> startPosY += entityShip.step.get().intValue();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public void setBlocksNumber(int number){
|
||||||
|
drawingBlocks.setNumber(number);
|
||||||
|
}
|
||||||
|
public void setBodyColor(Color color){
|
||||||
|
entityShip.bodyColor = color;
|
||||||
|
}
|
||||||
|
}
|
79
src/drawing_objects/ExtensionDrawingShip.java
Normal file
79
src/drawing_objects/ExtensionDrawingShip.java
Normal file
@ -0,0 +1,79 @@
|
|||||||
|
package drawing_objects;
|
||||||
|
|
||||||
|
import entities.EntityBattleship;
|
||||||
|
|
||||||
|
import java.awt.*;
|
||||||
|
|
||||||
|
public class ExtensionDrawingShip {
|
||||||
|
private static String getColorName(Color col){
|
||||||
|
if(col.equals(Color.RED))
|
||||||
|
return "RED";
|
||||||
|
if(col.equals(Color.GREEN))
|
||||||
|
return "GREEN";
|
||||||
|
if(col.equals(Color.BLUE))
|
||||||
|
return "BLUE";
|
||||||
|
if(col.equals(Color.YELLOW))
|
||||||
|
return "YELLOW";
|
||||||
|
if(col.equals(Color.WHITE))
|
||||||
|
return "WHITE";
|
||||||
|
if(col.equals(Color.GRAY))
|
||||||
|
return "GRAY";
|
||||||
|
if(col.equals(Color.BLACK))
|
||||||
|
return "BLACK";
|
||||||
|
if(col.equals(Color.MAGENTA))
|
||||||
|
return "MAGENTA";
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
private static Color getColor(String col){
|
||||||
|
if(col.equals("RED"))
|
||||||
|
return Color.RED;
|
||||||
|
if(col.equals("GREEN"))
|
||||||
|
return Color.GREEN;
|
||||||
|
if(col.equals("BLUE"))
|
||||||
|
return Color.BLUE;
|
||||||
|
if(col.equals("YELLOW"))
|
||||||
|
return Color.YELLOW;
|
||||||
|
if(col.equals("WHITE"))
|
||||||
|
return Color.WHITE;
|
||||||
|
if(col.equals("GRAY"))
|
||||||
|
return Color.GRAY;
|
||||||
|
if(col.equals("BLACK"))
|
||||||
|
return Color.BLACK;
|
||||||
|
if(col.equals("MAGENTA"))
|
||||||
|
return Color.MAGENTA;
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
public static DrawingShip createDrawingShip(String info, char separatorForObject, int width, int height){
|
||||||
|
String[] strs = info.split(Character.toString(separatorForObject));
|
||||||
|
if(strs.length == 5){
|
||||||
|
return new DrawingShip(Integer.parseInt(strs[0]),
|
||||||
|
Integer.parseInt(strs[1]), getColor(strs[2]), width, height,
|
||||||
|
Integer.parseInt(strs[3]), Integer.parseInt(strs[4]));
|
||||||
|
}
|
||||||
|
if(strs.length == 8){
|
||||||
|
return new DrawingBattleship(Integer.parseInt(strs[0]),
|
||||||
|
Integer.parseInt(strs[1]), getColor(strs[2]), getColor(strs[5]),
|
||||||
|
Boolean.parseBoolean(strs[6]), Boolean.parseBoolean(strs[7]),
|
||||||
|
width, height, Integer.parseInt(strs[3]), Integer.parseInt(strs[4]));
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
public static String getDataForSave(DrawingShip DrawingShip, char separatorForObject){
|
||||||
|
var ship = DrawingShip.getEntityShip();
|
||||||
|
if(ship == null)
|
||||||
|
return null;
|
||||||
|
var str = String.format("%d%c%d%c%s%c%d%c%d",
|
||||||
|
ship.getSpeed(), separatorForObject,
|
||||||
|
(int)ship.getWeight(), separatorForObject,
|
||||||
|
getColorName(ship.getBodyColor()), separatorForObject,
|
||||||
|
DrawingShip.drawingBlocks.getType(), separatorForObject,
|
||||||
|
DrawingShip.drawingBlocks.getNumber());
|
||||||
|
if(!(ship instanceof EntityBattleship)){
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
return String.format("%s%c%s%c%b%c%b", str, separatorForObject,
|
||||||
|
getColorName(((EntityBattleship) ship).getAdditionalColor()), separatorForObject,
|
||||||
|
((EntityBattleship) ship).getTurret(), separatorForObject,
|
||||||
|
((EntityBattleship) ship).getRocketLauncher());
|
||||||
|
}
|
||||||
|
}
|
10
src/drawing_objects/IDrawBlocks.java
Normal file
10
src/drawing_objects/IDrawBlocks.java
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
package drawing_objects;
|
||||||
|
|
||||||
|
import java.awt.*;
|
||||||
|
|
||||||
|
public interface IDrawBlocks {
|
||||||
|
int getNumber();
|
||||||
|
void setNumber(int x);
|
||||||
|
int getType();
|
||||||
|
void drawBlocks(Graphics2D graphics2D, int _startX, int _startY);
|
||||||
|
}
|
20
src/entities/EntityBattleship.java
Normal file
20
src/entities/EntityBattleship.java
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
package entities;
|
||||||
|
|
||||||
|
import java.awt.*;
|
||||||
|
|
||||||
|
public class EntityBattleship extends EntityShip {
|
||||||
|
public Color additionalColor;
|
||||||
|
public Color getAdditionalColor(){
|
||||||
|
return additionalColor;
|
||||||
|
}
|
||||||
|
private boolean turret;
|
||||||
|
public boolean getTurret() {return turret;}
|
||||||
|
private boolean rocketLauncher;
|
||||||
|
public boolean getRocketLauncher() {return rocketLauncher;}
|
||||||
|
public EntityBattleship(int speed, double weight, Color bodyColor, Color additionalColor, boolean turret, boolean rocketLauncher) {
|
||||||
|
super(speed, weight, bodyColor);
|
||||||
|
this.additionalColor = additionalColor;
|
||||||
|
this.turret = turret;
|
||||||
|
this.rocketLauncher = rocketLauncher;
|
||||||
|
}
|
||||||
|
}
|
25
src/entities/EntityShip.java
Normal file
25
src/entities/EntityShip.java
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
package entities;
|
||||||
|
|
||||||
|
import java.awt.*;
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
|
public class EntityShip{
|
||||||
|
private int speed;
|
||||||
|
public int getSpeed(){
|
||||||
|
return speed;
|
||||||
|
}
|
||||||
|
private double weight;
|
||||||
|
public double getWeight(){
|
||||||
|
return weight;
|
||||||
|
}
|
||||||
|
public Color bodyColor;
|
||||||
|
public Color getBodyColor(){
|
||||||
|
return bodyColor;
|
||||||
|
}
|
||||||
|
public Supplier<Double> step = () -> (double) speed * 100 / weight;
|
||||||
|
public EntityShip(int speed, double weight, Color bodyColor){
|
||||||
|
this.speed = speed;
|
||||||
|
this.weight = weight;
|
||||||
|
this.bodyColor = bodyColor;
|
||||||
|
}
|
||||||
|
}
|
194
src/frames/FrameBattleship.java
Normal file
194
src/frames/FrameBattleship.java
Normal file
@ -0,0 +1,194 @@
|
|||||||
|
package frames;
|
||||||
|
|
||||||
|
import drawing_objects.DirectionType;
|
||||||
|
import drawing_objects.DrawingBattleship;
|
||||||
|
import drawing_objects.DrawingShip;
|
||||||
|
import movement_strategy.AbstractStrategy;
|
||||||
|
import movement_strategy.MoveToCenter;
|
||||||
|
import movement_strategy.MoveToBorder;
|
||||||
|
import movement_strategy.DrawingObjectShip;
|
||||||
|
import movement_strategy.Status;
|
||||||
|
|
||||||
|
import javax.imageio.ImageIO;
|
||||||
|
import javax.swing.*;
|
||||||
|
import java.awt.*;
|
||||||
|
import java.awt.event.ActionEvent;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
public class FrameBattleship extends JFrame {
|
||||||
|
private DrawingShip drawingShip;
|
||||||
|
private AbstractStrategy abstractStrategy;
|
||||||
|
public JButton selectShipButton;
|
||||||
|
private DrawingShip selectedShip;
|
||||||
|
public DrawingShip getSelectedShip() {return selectedShip;}
|
||||||
|
private JComboBox<String> comboBoxStrategy;
|
||||||
|
private JComponent pictureBoxBattleship;
|
||||||
|
public FrameBattleship() throws IOException {
|
||||||
|
super("Линкор");
|
||||||
|
setSize(new Dimension(900,500));
|
||||||
|
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
|
||||||
|
//components initialisation
|
||||||
|
pictureBoxBattleship = new JComponent(){
|
||||||
|
public void paintComponent(Graphics graphics){
|
||||||
|
super.paintComponent(graphics);
|
||||||
|
Graphics2D graphics2D = (Graphics2D) graphics;
|
||||||
|
if (drawingShip != null) drawingShip.drawTransport(graphics2D);
|
||||||
|
super.repaint();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
pictureBoxBattleship.setBounds( 0, 0, getContentPane().getWidth(), getContentPane().getHeight());
|
||||||
|
comboBoxStrategy = new JComboBox<>(new String[]{"к центру", "к границе"});
|
||||||
|
JButton stepButton = new JButton("Шаг");
|
||||||
|
JButton createShipButton = new JButton("Создать корабль");
|
||||||
|
JButton createBattleshipButton = new JButton("Создать линкор");
|
||||||
|
selectShipButton = new JButton("Выбрать корабль");
|
||||||
|
JButton rightButton = new JButton(new ImageIcon(ImageIO.read(new File("images/right.png"))));
|
||||||
|
rightButton.setPreferredSize(new Dimension(30,30));
|
||||||
|
JButton leftButton = new JButton(new ImageIcon(ImageIO.read(new File("images/left.png"))));
|
||||||
|
leftButton.setPreferredSize(new Dimension(30,30));
|
||||||
|
JButton upButton = new JButton(new ImageIcon(ImageIO.read(new File("images/up.png"))));
|
||||||
|
upButton.setPreferredSize(new Dimension(30,30));
|
||||||
|
JButton downButton = new JButton(new ImageIcon(ImageIO.read(new File("images/down.png"))));
|
||||||
|
downButton.setPreferredSize(new Dimension(30,30));
|
||||||
|
//ActionListeners and ActionCommand addition
|
||||||
|
createShipButton.addActionListener(e -> buttonCreateShipClick());
|
||||||
|
createBattleshipButton.addActionListener(e -> buttonCreateBattleshipClick());
|
||||||
|
stepButton.addActionListener(e -> buttonStepClick());
|
||||||
|
rightButton.setActionCommand("right");
|
||||||
|
rightButton.addActionListener(this::buttonMoveClick);
|
||||||
|
leftButton.setActionCommand("left");
|
||||||
|
leftButton.addActionListener(this::buttonMoveClick);
|
||||||
|
upButton.setActionCommand("up");
|
||||||
|
upButton.addActionListener(this::buttonMoveClick);
|
||||||
|
downButton.setActionCommand("down");
|
||||||
|
downButton.addActionListener(this::buttonMoveClick);
|
||||||
|
//panels and constraints initialisation
|
||||||
|
JPanel panelBattleship = new JPanel(new BorderLayout());
|
||||||
|
JPanel rightPanel = new JPanel(new BorderLayout());
|
||||||
|
JPanel leftPanel = new JPanel(new BorderLayout());
|
||||||
|
JPanel createPanel = new JPanel(new GridBagLayout());
|
||||||
|
JPanel movementPanel = new JPanel(new GridBagLayout());
|
||||||
|
GridBagConstraints constraints = new GridBagConstraints();
|
||||||
|
constraints.insets.left = constraints.insets.top = constraints.insets.bottom = constraints.insets.right = 2;
|
||||||
|
//addition to createPanel
|
||||||
|
constraints.gridx = 0;
|
||||||
|
constraints.gridy = 0;
|
||||||
|
createPanel.add(createShipButton, constraints);
|
||||||
|
constraints.gridx = 1;
|
||||||
|
constraints.gridy = 0;
|
||||||
|
createPanel.add(createBattleshipButton, constraints);
|
||||||
|
constraints.gridx = 2;
|
||||||
|
constraints.gridy = 0;
|
||||||
|
createPanel.add(selectShipButton, constraints);
|
||||||
|
//addition to movementPanel
|
||||||
|
constraints.gridx = 2;
|
||||||
|
constraints.gridy = 1;
|
||||||
|
movementPanel.add(rightButton, constraints);
|
||||||
|
constraints.gridx = 0;
|
||||||
|
constraints.gridy = 1;
|
||||||
|
movementPanel.add(leftButton, constraints);
|
||||||
|
constraints.gridx = 1;
|
||||||
|
constraints.gridy = 0;
|
||||||
|
movementPanel.add(upButton, constraints);
|
||||||
|
constraints.gridx = 1;
|
||||||
|
constraints.gridy = 1;
|
||||||
|
movementPanel.add(downButton, constraints);
|
||||||
|
//addition to stepPanel
|
||||||
|
JPanel stepPanel = new JPanel(new GridBagLayout());
|
||||||
|
constraints.gridx = 0;
|
||||||
|
constraints.gridy = 0;
|
||||||
|
stepPanel.add(comboBoxStrategy, constraints);
|
||||||
|
constraints.gridx = 0;
|
||||||
|
constraints.gridy = 1;
|
||||||
|
stepPanel.add(stepButton, constraints);
|
||||||
|
//addition to frame
|
||||||
|
setLayout(new BorderLayout());
|
||||||
|
add(pictureBoxBattleship);
|
||||||
|
rightPanel.add(movementPanel, BorderLayout.SOUTH);
|
||||||
|
rightPanel.add(stepPanel, BorderLayout.NORTH);
|
||||||
|
leftPanel.add(createPanel, BorderLayout.SOUTH);
|
||||||
|
panelBattleship.add(rightPanel, BorderLayout.EAST);
|
||||||
|
panelBattleship.add(leftPanel, BorderLayout.WEST);
|
||||||
|
add(panelBattleship,BorderLayout.CENTER);
|
||||||
|
setVisible(true);
|
||||||
|
}
|
||||||
|
private void buttonCreateBattleshipClick() {
|
||||||
|
Random random = new Random();
|
||||||
|
pictureBoxBattleship.setBounds(0,0,getContentPane().getWidth(),getContentPane().getHeight());
|
||||||
|
Color bodyColor = JColorChooser.showDialog(this,"Выбор базового цвета", null);
|
||||||
|
Color additColor = JColorChooser.showDialog(this,"Выбор дополнительного цвета", null);
|
||||||
|
drawingShip = new DrawingBattleship(random.nextInt(200) + 100, random.nextInt(2000) + 1000, bodyColor, additColor, random.nextBoolean(),
|
||||||
|
random.nextBoolean(), pictureBoxBattleship.getWidth(), pictureBoxBattleship.getHeight(), random.nextInt(3),(random.nextInt(3)+1)*2);
|
||||||
|
drawingShip.setPosition(random.nextInt(90) + 10, random.nextInt(90) + 10);
|
||||||
|
draw();
|
||||||
|
}
|
||||||
|
private void buttonCreateShipClick(){
|
||||||
|
Random random = new Random();
|
||||||
|
pictureBoxBattleship.setBounds(0,0,getContentPane().getWidth(),getContentPane().getHeight());
|
||||||
|
Color bodyColor = JColorChooser.showDialog(this,"Выбор цвета", null);
|
||||||
|
drawingShip = new DrawingShip(random.nextInt(200) + 100, random.nextInt(2000) + 1000, bodyColor, pictureBoxBattleship.getWidth(),
|
||||||
|
pictureBoxBattleship.getHeight(), random.nextInt(3),(random.nextInt(3)+1)*2);
|
||||||
|
drawingShip.setPosition(random.nextInt(90) + 10, random.nextInt(90) + 10);
|
||||||
|
draw();
|
||||||
|
}
|
||||||
|
private void buttonStepClick(){
|
||||||
|
if (drawingShip == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (comboBoxStrategy.isEnabled()) {
|
||||||
|
switch (comboBoxStrategy.getSelectedIndex()) {
|
||||||
|
case 0 -> abstractStrategy = new MoveToCenter();
|
||||||
|
case 1 -> abstractStrategy = new MoveToBorder();
|
||||||
|
default -> abstractStrategy = null;
|
||||||
|
}
|
||||||
|
if (abstractStrategy == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
abstractStrategy.setData(new DrawingObjectShip(drawingShip), pictureBoxBattleship.getWidth(), pictureBoxBattleship.getHeight());
|
||||||
|
comboBoxStrategy.setEnabled(false);
|
||||||
|
}
|
||||||
|
if (abstractStrategy == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
abstractStrategy.makeStep();
|
||||||
|
draw();
|
||||||
|
if (abstractStrategy.getStatus() == Status.FINISH)
|
||||||
|
{
|
||||||
|
comboBoxStrategy.setEnabled(true);
|
||||||
|
abstractStrategy = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
private void buttonMoveClick(ActionEvent event) {
|
||||||
|
if(drawingShip == null || drawingShip.getEntityShip() == null)
|
||||||
|
return;
|
||||||
|
switch (event.getActionCommand()) {
|
||||||
|
case "left" -> drawingShip.moveTransport(DirectionType.LEFT);
|
||||||
|
case "right" -> drawingShip.moveTransport(DirectionType.RIGHT);
|
||||||
|
case "up" -> drawingShip.moveTransport(DirectionType.UP);
|
||||||
|
case "down" -> drawingShip.moveTransport(DirectionType.DOWN);
|
||||||
|
}
|
||||||
|
draw();
|
||||||
|
}
|
||||||
|
private void draw() {
|
||||||
|
if (drawingShip == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
pictureBoxBattleship.repaint();
|
||||||
|
}
|
||||||
|
public void select(){
|
||||||
|
if (drawingShip == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
selectedShip = drawingShip;
|
||||||
|
}
|
||||||
|
public void setShip(DrawingShip ship){
|
||||||
|
ship.setPosition(0,0);
|
||||||
|
drawingShip = ship;
|
||||||
|
pictureBoxBattleship.setBounds(0,0,getContentPane().getWidth(),getContentPane().getHeight());
|
||||||
|
drawingShip.drawTransport((Graphics2D) pictureBoxBattleship.getGraphics());
|
||||||
|
draw();
|
||||||
|
}
|
||||||
|
}
|
390
src/frames/FrameShipConfig.java
Normal file
390
src/frames/FrameShipConfig.java
Normal file
@ -0,0 +1,390 @@
|
|||||||
|
package frames;
|
||||||
|
|
||||||
|
import java.awt.*;
|
||||||
|
import java.awt.datatransfer.DataFlavor;
|
||||||
|
import java.awt.datatransfer.StringSelection;
|
||||||
|
import java.awt.datatransfer.Transferable;
|
||||||
|
import java.awt.datatransfer.UnsupportedFlavorException;
|
||||||
|
import javax.swing.*;
|
||||||
|
import java.awt.event.*;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import drawing_objects.*;
|
||||||
|
|
||||||
|
public class FrameShipConfig extends JFrame {
|
||||||
|
private static class LabelTransferHandler extends TransferHandler {
|
||||||
|
@Override
|
||||||
|
public int getSourceActions(JComponent c) {
|
||||||
|
return TransferHandler.COPY;
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
protected Transferable createTransferable(JComponent c) {
|
||||||
|
return new StringSelection(((JLabel)c).getText());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
private record ColorTransferable(Color color) implements Transferable {
|
||||||
|
private static final DataFlavor colorDataFlavor = new DataFlavor(Color.class, "Color");
|
||||||
|
@Override
|
||||||
|
public DataFlavor[] getTransferDataFlavors() {
|
||||||
|
return new DataFlavor[]{colorDataFlavor};
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public boolean isDataFlavorSupported(DataFlavor flavor) {
|
||||||
|
return colorDataFlavor.equals(flavor);
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException {
|
||||||
|
if (isDataFlavorSupported(flavor)) {
|
||||||
|
return color;
|
||||||
|
} else {
|
||||||
|
throw new UnsupportedFlavorException(flavor);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
private record IDrawBlocksTransferable(IDrawBlocks IDrawBlocksObject) implements Transferable {
|
||||||
|
private static final DataFlavor IDrawBlocksDataFlavor = new DataFlavor(IDrawBlocks.class, "IDrawBlocks");
|
||||||
|
@Override
|
||||||
|
public DataFlavor[] getTransferDataFlavors() {
|
||||||
|
return new DataFlavor[]{IDrawBlocksDataFlavor};
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public boolean isDataFlavorSupported(DataFlavor flavor) {
|
||||||
|
return IDrawBlocksDataFlavor.equals(flavor);
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException {
|
||||||
|
if (isDataFlavorSupported(flavor)) {
|
||||||
|
return IDrawBlocksObject;
|
||||||
|
} else {
|
||||||
|
throw new UnsupportedFlavorException(flavor);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
private static class PanelTransferHandler extends TransferHandler {
|
||||||
|
@Override
|
||||||
|
public int getSourceActions(JComponent c) {
|
||||||
|
return TransferHandler.COPY;
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
protected Transferable createTransferable(JComponent c) {
|
||||||
|
return new ColorTransferable(c.getBackground());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
private static class LabelMouseAdapter extends MouseAdapter{
|
||||||
|
@Override
|
||||||
|
public void mousePressed(MouseEvent e) {
|
||||||
|
((JLabel)e.getComponent()).getTransferHandler().exportAsDrag(((JLabel)e.getComponent()), e, TransferHandler.COPY);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
private static class PanelMouseAdapter extends MouseAdapter{
|
||||||
|
@Override
|
||||||
|
public void mousePressed(MouseEvent e) {
|
||||||
|
((JPanel)e.getComponent()).getTransferHandler().exportAsDrag(((JPanel)e.getComponent()), e, TransferHandler.COPY);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
private static class IDrawBlocksComponent extends JComponent{
|
||||||
|
public IDrawBlocks obj;
|
||||||
|
public IDrawBlocksComponent(IDrawBlocks obj){
|
||||||
|
this.obj = obj;
|
||||||
|
this.addMouseListener(
|
||||||
|
new MouseAdapter(){
|
||||||
|
@Override
|
||||||
|
public void mousePressed(MouseEvent e) {
|
||||||
|
((IDrawBlocksComponent)e.getComponent()).getTransferHandler().exportAsDrag(((IDrawBlocksComponent)e.getComponent()), e, TransferHandler.COPY);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
this.setTransferHandler(
|
||||||
|
new TransferHandler(){
|
||||||
|
@Override
|
||||||
|
public int getSourceActions(JComponent c) {
|
||||||
|
return TransferHandler.COPY;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Transferable createTransferable(JComponent c) {
|
||||||
|
return new IDrawBlocksTransferable(((IDrawBlocksComponent) c).obj);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
private final JComponent pictureBox = new JComponent(){
|
||||||
|
public void paintComponent(Graphics graphics){
|
||||||
|
super.paintComponent(graphics);
|
||||||
|
Graphics2D graphics2D = (Graphics2D) graphics;
|
||||||
|
if (drawingShip != null) drawingShip.drawTransport(graphics2D);
|
||||||
|
super.repaint();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
public final JButton addButton = new JButton("Добавить");
|
||||||
|
public final JButton cancelButton = new JButton("Отмена");
|
||||||
|
public DrawingShip drawingShip;
|
||||||
|
private final int pictureBoxWidth = 218;
|
||||||
|
private final int pictureBoxHeight = 178;
|
||||||
|
public FrameShipConfig(){
|
||||||
|
super("Создание объекта");
|
||||||
|
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
|
createGui();
|
||||||
|
setVisible(true);
|
||||||
|
}
|
||||||
|
private void createGui(){
|
||||||
|
pictureBox.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
|
||||||
|
IDrawBlocksComponent blocks = new IDrawBlocksComponent(new DrawingBlocks());
|
||||||
|
IDrawBlocksComponent roundBlocks = new IDrawBlocksComponent(new DrawingRoundBlocks());
|
||||||
|
IDrawBlocksComponent crossBlocks = new IDrawBlocksComponent(new DrawingCrossBlocks());
|
||||||
|
JLabel squareLabel = new JLabel("Квадратные");
|
||||||
|
squareLabel.setHorizontalAlignment(SwingConstants.CENTER);
|
||||||
|
squareLabel.setVerticalAlignment(SwingConstants.CENTER);
|
||||||
|
JLabel roundLabel = new JLabel("Круглые");
|
||||||
|
roundLabel.setHorizontalAlignment(SwingConstants.CENTER);
|
||||||
|
roundLabel.setVerticalAlignment(SwingConstants.CENTER);
|
||||||
|
JLabel crossLabel = new JLabel("Крестовидные");
|
||||||
|
crossLabel.setHorizontalAlignment(SwingConstants.CENTER);
|
||||||
|
crossLabel.setVerticalAlignment(SwingConstants.CENTER);
|
||||||
|
blocks.setLayout(new GridLayout(1,1));
|
||||||
|
roundBlocks.setLayout(new GridLayout(1,1));
|
||||||
|
crossBlocks.setLayout(new GridLayout(1,1));
|
||||||
|
blocks.add(squareLabel);
|
||||||
|
roundBlocks.add(roundLabel);
|
||||||
|
crossBlocks.add(crossLabel);
|
||||||
|
blocks.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
|
||||||
|
roundBlocks.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
|
||||||
|
crossBlocks.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
|
||||||
|
JLabel colorLabel = new JLabel("Цвет");
|
||||||
|
colorLabel.setHorizontalAlignment(SwingConstants.CENTER);
|
||||||
|
colorLabel.setVerticalAlignment(SwingConstants.CENTER);
|
||||||
|
colorLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
|
||||||
|
JLabel additionalColorLabel = new JLabel("Доп цвет");
|
||||||
|
additionalColorLabel.setHorizontalAlignment(SwingConstants.CENTER);
|
||||||
|
additionalColorLabel.setVerticalAlignment(SwingConstants.CENTER);
|
||||||
|
additionalColorLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
|
||||||
|
JLabel IDrawBlocksLabel = new JLabel("Блоки");
|
||||||
|
IDrawBlocksLabel.setHorizontalAlignment(SwingConstants.CENTER);
|
||||||
|
IDrawBlocksLabel.setVerticalAlignment(SwingConstants.CENTER);
|
||||||
|
IDrawBlocksLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
|
||||||
|
JCheckBox checkBoxTurret = new JCheckBox("Наличие башни");
|
||||||
|
JCheckBox checkBoxRocketLauncher = new JCheckBox("Наличие ракет");
|
||||||
|
JLabel simpleLabel = new JLabel("Простой");
|
||||||
|
simpleLabel.setTransferHandler(new LabelTransferHandler());
|
||||||
|
simpleLabel.addMouseListener(new LabelMouseAdapter());
|
||||||
|
simpleLabel.setHorizontalAlignment(SwingConstants.CENTER);
|
||||||
|
simpleLabel.setVerticalAlignment(SwingConstants.CENTER);
|
||||||
|
simpleLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
|
||||||
|
JLabel advancedLabel = new JLabel("Продвинутый");
|
||||||
|
advancedLabel.setTransferHandler(new LabelTransferHandler());
|
||||||
|
advancedLabel.addMouseListener(new LabelMouseAdapter());
|
||||||
|
advancedLabel.setHorizontalAlignment(SwingConstants.CENTER);
|
||||||
|
advancedLabel.setVerticalAlignment(SwingConstants.CENTER);
|
||||||
|
advancedLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
|
||||||
|
JLabel speedLabel = new JLabel ("Скорость");
|
||||||
|
JLabel weightLabel = new JLabel ("Вес");
|
||||||
|
SpinnerNumberModel speedSpinnerModel = new SpinnerNumberModel(100.0, 100.0, 1000.0, 1.0);
|
||||||
|
SpinnerNumberModel weightSpinnerModel = new SpinnerNumberModel(100.0, 100.0, 1000.0, 1.0);
|
||||||
|
SpinnerNumberModel blocksNumberSpinnerModel = new SpinnerNumberModel(2, 2, 6, 2.0);
|
||||||
|
JSpinner speedSpinner = new JSpinner(speedSpinnerModel);
|
||||||
|
JSpinner weightSpinner = new JSpinner(weightSpinnerModel);
|
||||||
|
JSpinner blocksNumberSpinner = new JSpinner(blocksNumberSpinnerModel);
|
||||||
|
JPanel colorPanel = new JPanel();
|
||||||
|
JPanel redPanel = new JPanel();
|
||||||
|
JPanel greenPanel = new JPanel();
|
||||||
|
JPanel bluePanel = new JPanel();
|
||||||
|
JPanel yellowPanel = new JPanel();
|
||||||
|
JPanel whitePanel = new JPanel();
|
||||||
|
JPanel grayPanel = new JPanel();
|
||||||
|
JPanel blackPanel = new JPanel();
|
||||||
|
JPanel purplePanel = new JPanel();
|
||||||
|
redPanel.setTransferHandler(new PanelTransferHandler());
|
||||||
|
greenPanel.setTransferHandler(new PanelTransferHandler());
|
||||||
|
bluePanel.setTransferHandler(new PanelTransferHandler());
|
||||||
|
yellowPanel.setTransferHandler(new PanelTransferHandler());
|
||||||
|
whitePanel.setTransferHandler(new PanelTransferHandler());
|
||||||
|
grayPanel.setTransferHandler(new PanelTransferHandler());
|
||||||
|
blackPanel.setTransferHandler(new PanelTransferHandler());
|
||||||
|
purplePanel.setTransferHandler(new PanelTransferHandler());
|
||||||
|
redPanel.addMouseListener(new PanelMouseAdapter());
|
||||||
|
greenPanel.addMouseListener(new PanelMouseAdapter());
|
||||||
|
bluePanel.addMouseListener(new PanelMouseAdapter());
|
||||||
|
yellowPanel.addMouseListener(new PanelMouseAdapter());
|
||||||
|
whitePanel.addMouseListener(new PanelMouseAdapter());
|
||||||
|
grayPanel.addMouseListener(new PanelMouseAdapter());
|
||||||
|
blackPanel.addMouseListener(new PanelMouseAdapter());
|
||||||
|
purplePanel.addMouseListener(new PanelMouseAdapter());
|
||||||
|
redPanel.setName("Красный");
|
||||||
|
greenPanel.setName("Зелёный");
|
||||||
|
bluePanel.setName("Синий");
|
||||||
|
yellowPanel.setName("Жёлтый");
|
||||||
|
whitePanel.setName("Белый");
|
||||||
|
grayPanel.setName("Серый");
|
||||||
|
blackPanel.setName("Чёрный");
|
||||||
|
purplePanel.setName("Фиолетовый");
|
||||||
|
pictureBox.setTransferHandler(
|
||||||
|
new TransferHandler(){
|
||||||
|
@Override
|
||||||
|
public boolean canImport(TransferHandler.TransferSupport support) {
|
||||||
|
return support.isDataFlavorSupported(DataFlavor.stringFlavor);
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public boolean importData(TransferHandler.TransferSupport support) {
|
||||||
|
if (canImport(support)) {
|
||||||
|
try {
|
||||||
|
int speed = ((Number)speedSpinner.getValue()).intValue();
|
||||||
|
int weight = ((Number)weightSpinner.getValue()).intValue();
|
||||||
|
int blocksNumber = ((Number)blocksNumberSpinner.getValue()).intValue();
|
||||||
|
switch ((String)support.getTransferable().getTransferData(DataFlavor.stringFlavor)) {
|
||||||
|
case "Простой" -> {
|
||||||
|
drawingShip = new DrawingShip(speed, weight, Color.WHITE,
|
||||||
|
pictureBoxWidth, pictureBoxHeight, 0, blocksNumber);
|
||||||
|
drawingShip.setBlocksNumber(blocksNumber);
|
||||||
|
}
|
||||||
|
case "Продвинутый" -> {
|
||||||
|
drawingShip = new DrawingBattleship(speed, weight, Color.WHITE, Color.BLACK,
|
||||||
|
checkBoxTurret.isSelected(), checkBoxRocketLauncher.isSelected(),
|
||||||
|
pictureBoxWidth, pictureBoxHeight, 0, blocksNumber);
|
||||||
|
drawingShip.setBlocksNumber(blocksNumber);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
drawingShip.setPosition(pictureBoxWidth / 2 - drawingShip.getWidth() / 2,
|
||||||
|
pictureBoxHeight / 2 - drawingShip.getHeight() / 2);
|
||||||
|
pictureBox.repaint();
|
||||||
|
return true;
|
||||||
|
} catch (UnsupportedFlavorException | IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
IDrawBlocksLabel.setTransferHandler(
|
||||||
|
new TransferHandler(){
|
||||||
|
@Override
|
||||||
|
public boolean canImport(TransferHandler.TransferSupport support) {
|
||||||
|
return support.isDataFlavorSupported(IDrawBlocksTransferable.IDrawBlocksDataFlavor);
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public boolean importData(TransferHandler.TransferSupport support) {
|
||||||
|
if (canImport(support)) {
|
||||||
|
try {
|
||||||
|
IDrawBlocks obj = (IDrawBlocks) support.getTransferable().getTransferData(IDrawBlocksTransferable.IDrawBlocksDataFlavor);
|
||||||
|
obj.setNumber(((Number)blocksNumberSpinner.getValue()).intValue());
|
||||||
|
if (drawingShip == null)
|
||||||
|
return false;
|
||||||
|
drawingShip.setDrawingBlocks(obj);
|
||||||
|
pictureBox.repaint();
|
||||||
|
return true;
|
||||||
|
} catch (UnsupportedFlavorException | IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
colorLabel.setTransferHandler(
|
||||||
|
new TransferHandler(){
|
||||||
|
@Override
|
||||||
|
public boolean canImport(TransferHandler.TransferSupport support) {
|
||||||
|
return support.isDataFlavorSupported(ColorTransferable.colorDataFlavor);
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public boolean importData(TransferHandler.TransferSupport support) {
|
||||||
|
if (canImport(support)) {
|
||||||
|
try {
|
||||||
|
Color color = (Color) support.getTransferable().getTransferData(ColorTransferable.colorDataFlavor);
|
||||||
|
if (drawingShip == null)
|
||||||
|
return false;
|
||||||
|
drawingShip.setBodyColor(color);
|
||||||
|
pictureBox.repaint();
|
||||||
|
return true;
|
||||||
|
} catch (UnsupportedFlavorException | IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
additionalColorLabel.setTransferHandler(
|
||||||
|
new TransferHandler(){
|
||||||
|
@Override
|
||||||
|
public boolean canImport(TransferHandler.TransferSupport support) {
|
||||||
|
return support.isDataFlavorSupported(ColorTransferable.colorDataFlavor);
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public boolean importData(TransferHandler.TransferSupport support) {
|
||||||
|
if (canImport(support)) {
|
||||||
|
try {
|
||||||
|
Color color = (Color) support.getTransferable().getTransferData(ColorTransferable.colorDataFlavor);
|
||||||
|
if (drawingShip == null || !(drawingShip instanceof DrawingBattleship))
|
||||||
|
return false;
|
||||||
|
((DrawingBattleship) drawingShip).setAdditionalColor(color);
|
||||||
|
pictureBox.repaint();
|
||||||
|
return true;
|
||||||
|
} catch (UnsupportedFlavorException | IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
addButton.setBounds(555,250,94,29);
|
||||||
|
cancelButton.setBounds(679,250,94,29);
|
||||||
|
pictureBox.setBounds(555, 65, pictureBoxWidth, pictureBoxHeight);
|
||||||
|
colorLabel.setBounds(555, 20, 70, 33);
|
||||||
|
additionalColorLabel.setBounds(629, 20, 70, 33);
|
||||||
|
IDrawBlocksLabel.setBounds(703, 20, 70, 33);
|
||||||
|
checkBoxTurret.setBounds(6, 132, 159, 24);
|
||||||
|
checkBoxRocketLauncher.setBounds(6, 162, 145, 24);
|
||||||
|
simpleLabel.setBounds(171,169, 120, 50);
|
||||||
|
advancedLabel.setBounds(297,169, 120, 50);
|
||||||
|
blocks.setBounds(171,229, 120, 50);
|
||||||
|
roundBlocks.setBounds(297,229, 120, 50);
|
||||||
|
crossBlocks.setBounds(423,229, 120, 50);
|
||||||
|
colorPanel.setBounds(171, 23, 372,143);
|
||||||
|
speedSpinner.setBounds(6, 46, 150, 27);
|
||||||
|
speedLabel.setBounds(6, 23, 73, 20);
|
||||||
|
weightSpinner.setBounds(6, 99, 150, 27);
|
||||||
|
weightLabel.setBounds(6, 76, 33, 20);
|
||||||
|
blocksNumberSpinner.setBounds(6, 200, 150, 27);
|
||||||
|
redPanel.setBackground(Color.RED);
|
||||||
|
greenPanel.setBackground(Color.GREEN);
|
||||||
|
bluePanel.setBackground(Color.BLUE);
|
||||||
|
yellowPanel.setBackground(Color.YELLOW);
|
||||||
|
whitePanel.setBackground(Color.WHITE);
|
||||||
|
grayPanel.setBackground(Color.GRAY);
|
||||||
|
blackPanel.setBackground(Color.BLACK);
|
||||||
|
purplePanel.setBackground(Color.MAGENTA);
|
||||||
|
colorPanel.setLayout(new GridLayout(2, 4, 26, 10));
|
||||||
|
colorPanel.add(redPanel);
|
||||||
|
colorPanel.add(greenPanel);
|
||||||
|
colorPanel.add(bluePanel);
|
||||||
|
colorPanel.add(yellowPanel);
|
||||||
|
colorPanel.add(whitePanel);
|
||||||
|
colorPanel.add(grayPanel);
|
||||||
|
colorPanel.add(blackPanel);
|
||||||
|
colorPanel.add(purplePanel);
|
||||||
|
add(colorLabel);
|
||||||
|
add(additionalColorLabel);
|
||||||
|
add(IDrawBlocksLabel);
|
||||||
|
setLayout(null);
|
||||||
|
setSize(818, 350);
|
||||||
|
add(speedLabel);
|
||||||
|
add(speedSpinner);
|
||||||
|
add(weightLabel);
|
||||||
|
add(weightSpinner);
|
||||||
|
add(simpleLabel);
|
||||||
|
add(advancedLabel);
|
||||||
|
add(checkBoxTurret);
|
||||||
|
add(checkBoxRocketLauncher);
|
||||||
|
add(pictureBox);
|
||||||
|
add(addButton);
|
||||||
|
add(cancelButton);
|
||||||
|
add(blocksNumberSpinner);
|
||||||
|
add(colorPanel);
|
||||||
|
add(blocks);
|
||||||
|
add(roundBlocks);
|
||||||
|
add(crossBlocks);
|
||||||
|
}
|
||||||
|
}
|
289
src/frames/FrameShipsCollection.java
Normal file
289
src/frames/FrameShipsCollection.java
Normal file
@ -0,0 +1,289 @@
|
|||||||
|
package frames;
|
||||||
|
|
||||||
|
import javax.swing.*;
|
||||||
|
import javax.swing.border.StrokeBorder;
|
||||||
|
import javax.swing.filechooser.FileFilter;
|
||||||
|
import java.awt.*;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
import drawing_objects.*;
|
||||||
|
import generics.*;
|
||||||
|
import movement_strategy.*;
|
||||||
|
|
||||||
|
class TxtFilter extends FileFilter {
|
||||||
|
@Override
|
||||||
|
public boolean accept(File f) {
|
||||||
|
if (f.isDirectory()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
String s = f.getName().toLowerCase();
|
||||||
|
return s.endsWith(".txt");
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public String getDescription() {
|
||||||
|
return "*.txt";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public class FrameShipsCollection extends JFrame {
|
||||||
|
private final ShipsGenericStorage storage;
|
||||||
|
LinkedList<DrawingShip> trashCollection = new LinkedList<>();
|
||||||
|
private JComponent pictureBoxCollection;
|
||||||
|
private TextField textFieldNumber;
|
||||||
|
private TextField textFieldStorageName;
|
||||||
|
private JList<String> listStorages;
|
||||||
|
private DefaultListModel<String> listModel;
|
||||||
|
public FrameShipsCollection(){
|
||||||
|
super("Набор кораблей");
|
||||||
|
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
|
createGui();
|
||||||
|
pack();
|
||||||
|
setVisible(true);
|
||||||
|
storage = new ShipsGenericStorage(pictureBoxCollection.getWidth(), pictureBoxCollection.getHeight());
|
||||||
|
}
|
||||||
|
private void createGui(){
|
||||||
|
//components initialisation
|
||||||
|
pictureBoxCollection = new JComponent(){
|
||||||
|
public void paintComponent(Graphics graphics){
|
||||||
|
super.paintComponent(graphics);
|
||||||
|
Graphics2D graphics2D = (Graphics2D) graphics;
|
||||||
|
if (listStorages == null || storage == null)
|
||||||
|
return;
|
||||||
|
var collection = storage.getSet(listStorages.getSelectedValue());
|
||||||
|
if (collection == null)
|
||||||
|
return;
|
||||||
|
collection.showShips(graphics2D);
|
||||||
|
super.repaint();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
JMenuBar menuFile = new JMenuBar();
|
||||||
|
JMenu file = new JMenu("Файл");
|
||||||
|
menuFile.add(file);
|
||||||
|
JMenuItem saveFile = new JMenuItem("Сохранить");
|
||||||
|
JMenuItem loadFile = new JMenuItem("Загрузить");
|
||||||
|
JMenuItem saveSet = new JMenuItem("Сохранить коллекцию");
|
||||||
|
JMenuItem loadSet = new JMenuItem("Загрузить коллекцию");
|
||||||
|
file.add(saveSet);
|
||||||
|
file.add(loadSet);
|
||||||
|
file.add(saveFile);
|
||||||
|
file.add(loadFile);
|
||||||
|
setJMenuBar(menuFile);
|
||||||
|
pictureBoxCollection.setPreferredSize(new Dimension(700, 450));
|
||||||
|
JButton buttonAddShip = new JButton("Добавить корабль");
|
||||||
|
textFieldNumber = new TextField();
|
||||||
|
textFieldNumber.setText("0");
|
||||||
|
JButton buttonRemoveShip = new JButton("Удалить корабль");
|
||||||
|
JButton buttonRefreshCollection = new JButton("Обновить коллекцию");
|
||||||
|
JButton buttonAddSet = new JButton("Добавить набор");
|
||||||
|
JButton buttonDeleteSet = new JButton("Удалить набор");
|
||||||
|
JButton buttonTrash = new JButton("Корзина");
|
||||||
|
textFieldStorageName = new TextField();
|
||||||
|
listModel = new DefaultListModel<>();
|
||||||
|
JScrollPane scrollPane = new JScrollPane();
|
||||||
|
listStorages= new JList<>(listModel);
|
||||||
|
scrollPane.setViewportView(listStorages);
|
||||||
|
//ActionListeners and ActionCommand addition
|
||||||
|
buttonAddSet.addActionListener(e -> buttonAddSet_Click());
|
||||||
|
buttonDeleteSet.addActionListener(e -> buttonDeleteSet_Click());
|
||||||
|
buttonAddShip.addActionListener(e -> buttonAddShipClick());
|
||||||
|
buttonRemoveShip.addActionListener(e -> buttonRemoveShipClick());
|
||||||
|
buttonRefreshCollection.addActionListener(e -> buttonRefreshCollectionClick());
|
||||||
|
buttonTrash.addActionListener(e -> buttonTrashClick());
|
||||||
|
saveFile.addActionListener(e -> saveFile());
|
||||||
|
saveSet.addActionListener(e -> saveSet());
|
||||||
|
loadFile.addActionListener(e -> loadFile());
|
||||||
|
loadSet.addActionListener(e -> loadSet());
|
||||||
|
//panels and constraints initialisation
|
||||||
|
JPanel panelTools = new JPanel(new GridBagLayout());
|
||||||
|
panelTools.setBorder(new StrokeBorder(new BasicStroke(3)));
|
||||||
|
panelTools.setToolTipText("Инструменты");
|
||||||
|
JPanel panelSets = new JPanel(new GridBagLayout());
|
||||||
|
panelSets.setBorder(new StrokeBorder(new BasicStroke(3)));
|
||||||
|
panelSets.setToolTipText("Наборы");
|
||||||
|
GridBagConstraints constraints = new GridBagConstraints();
|
||||||
|
constraints.insets.left = constraints.insets.right = 5;
|
||||||
|
constraints.insets.top = constraints.insets.bottom = 5;
|
||||||
|
constraints.fill = GridBagConstraints.BOTH;
|
||||||
|
//addition to panelSets
|
||||||
|
constraints.gridx = 0;
|
||||||
|
constraints.gridy = 0;
|
||||||
|
panelSets.add(textFieldStorageName, constraints);
|
||||||
|
constraints.gridx = 0;
|
||||||
|
constraints.gridy = 1;
|
||||||
|
panelSets.add(buttonAddSet, constraints);
|
||||||
|
constraints.gridx = 0;
|
||||||
|
constraints.gridy = 2;
|
||||||
|
panelSets.add(scrollPane, constraints);
|
||||||
|
constraints.gridx = 0;
|
||||||
|
constraints.gridy = 3;
|
||||||
|
panelSets.add(buttonDeleteSet, constraints);
|
||||||
|
//addition to panelTools
|
||||||
|
constraints.gridx = 0;
|
||||||
|
constraints.gridy = 0;
|
||||||
|
panelTools.add(panelSets, constraints);
|
||||||
|
constraints.gridx = 0;
|
||||||
|
constraints.gridy = 1;
|
||||||
|
panelTools.add(buttonAddShip, constraints);
|
||||||
|
constraints.gridx = 0;
|
||||||
|
constraints.gridy = 2;
|
||||||
|
panelTools.add(textFieldNumber, constraints);
|
||||||
|
constraints.gridx = 0;
|
||||||
|
constraints.gridy = 3;
|
||||||
|
panelTools.add(buttonRemoveShip, constraints);
|
||||||
|
constraints.gridx = 0;
|
||||||
|
constraints.gridy = 4;
|
||||||
|
panelTools.add(buttonRefreshCollection, constraints);
|
||||||
|
constraints.gridx = 0;
|
||||||
|
constraints.gridy = 5;
|
||||||
|
panelTools.add(buttonTrash, constraints);
|
||||||
|
//addition to frame
|
||||||
|
setLayout(new BorderLayout());
|
||||||
|
add(panelTools, BorderLayout.EAST);
|
||||||
|
add(pictureBoxCollection, BorderLayout.CENTER);
|
||||||
|
}
|
||||||
|
private void reloadObjects(){
|
||||||
|
int index = listStorages.getSelectedIndex();
|
||||||
|
listModel.clear();
|
||||||
|
ArrayList<String> keys = storage.getKeys();
|
||||||
|
for (String key : keys) {
|
||||||
|
listModel.addElement(key);
|
||||||
|
}
|
||||||
|
if(listModel.size() > 0 && (index == -1 || index >= listModel.size()))
|
||||||
|
listStorages.setSelectedIndex(0);
|
||||||
|
else if(listModel.size() > 0)
|
||||||
|
listStorages.setSelectedIndex(index);
|
||||||
|
}
|
||||||
|
private void buttonAddSet_Click() {
|
||||||
|
if(textFieldStorageName.getText() == null ) {
|
||||||
|
JOptionPane.showMessageDialog(this, "Не все данные заполнены");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
String name = textFieldStorageName.getText();
|
||||||
|
if (Objects.equals(name, "")) {
|
||||||
|
JOptionPane.showMessageDialog(this, "Не все данные заполнены");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
storage.addSet(name);
|
||||||
|
reloadObjects();
|
||||||
|
}
|
||||||
|
private void buttonDeleteSet_Click() {
|
||||||
|
if (listStorages.getSelectedIndex() == -1)
|
||||||
|
return;
|
||||||
|
storage.delSet(listStorages.getSelectedValue(), trashCollection);
|
||||||
|
reloadObjects();
|
||||||
|
}
|
||||||
|
private void buttonAddShipClick() {
|
||||||
|
if(listStorages.getSelectedIndex() == -1) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
ShipsGenericCollection<DrawingShip, DrawingObjectShip> drawingShips = storage.getSet(listStorages.getSelectedValue());
|
||||||
|
FrameShipConfig frameShipConfig = new FrameShipConfig();
|
||||||
|
frameShipConfig.addButton.addActionListener(e -> {
|
||||||
|
if (drawingShips.insert(frameShipConfig.drawingShip)) {
|
||||||
|
frameShipConfig.dispose();
|
||||||
|
frameShipConfig.drawingShip.pictureWidth = pictureBoxCollection.getWidth();
|
||||||
|
frameShipConfig.drawingShip.pictureHeight = pictureBoxCollection.getHeight();
|
||||||
|
JOptionPane.showMessageDialog(null, "Объект добавлен", "Информация", JOptionPane.INFORMATION_MESSAGE);
|
||||||
|
pictureBoxCollection.repaint();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
JOptionPane.showMessageDialog(null, "Не удалось добавить объект", "Информация", JOptionPane.INFORMATION_MESSAGE);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
frameShipConfig.cancelButton.addActionListener(e -> frameShipConfig.dispose());
|
||||||
|
}
|
||||||
|
private void buttonRemoveShipClick(){
|
||||||
|
if (listStorages.getSelectedIndex() == -1 || Objects.equals(textFieldNumber.getText(), "") || textFieldNumber.getText() == null)
|
||||||
|
return;
|
||||||
|
var obj = storage.getSet(listStorages.getSelectedValue());
|
||||||
|
if (obj == null)
|
||||||
|
return;
|
||||||
|
int pos = Integer.parseInt(textFieldNumber.getText());
|
||||||
|
var ship = obj.get(pos);
|
||||||
|
if (obj.remove(pos)){
|
||||||
|
JOptionPane.showMessageDialog(this, "Объект удален");
|
||||||
|
trashCollection.add(ship);
|
||||||
|
pictureBoxCollection.repaint();
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
JOptionPane.showMessageDialog(this, "Не удалось удалить объект");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
private void buttonRefreshCollectionClick(){
|
||||||
|
pictureBoxCollection.repaint();
|
||||||
|
}
|
||||||
|
private void buttonTrashClick(){
|
||||||
|
if (trashCollection.size() == 0)
|
||||||
|
return;
|
||||||
|
FrameBattleship frame;
|
||||||
|
try {
|
||||||
|
frame = new FrameBattleship();
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
frame.setShip(trashCollection.pop());
|
||||||
|
}
|
||||||
|
private void saveFile(){
|
||||||
|
JFileChooser fc = new JFileChooser("C:\\Users\\user\\Documents");
|
||||||
|
fc.addChoosableFileFilter(new TxtFilter());
|
||||||
|
int retrieval = fc.showSaveDialog(null);
|
||||||
|
if (retrieval == JFileChooser.APPROVE_OPTION) {
|
||||||
|
File file = new File(fc.getSelectedFile() + "." + "txt");
|
||||||
|
try {
|
||||||
|
storage.saveData(file);
|
||||||
|
} catch (IOException ex) {
|
||||||
|
throw new RuntimeException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
private void saveSet(){
|
||||||
|
JFileChooser fc = new JFileChooser("C:\\Users\\user\\Documents");
|
||||||
|
fc.addChoosableFileFilter(new TxtFilter());
|
||||||
|
int retrieval = fc.showSaveDialog(null);
|
||||||
|
if (retrieval == JFileChooser.APPROVE_OPTION) {
|
||||||
|
File file = new File(fc.getSelectedFile() + "." + "txt");
|
||||||
|
try {
|
||||||
|
if(listStorages.getSelectedIndex() == -1) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
storage.shipsStorages.get(listStorages.getSelectedValue()).saveData(file, listStorages.getSelectedValue());
|
||||||
|
reloadObjects();
|
||||||
|
} catch (IOException ex) {
|
||||||
|
throw new RuntimeException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
private void loadFile(){
|
||||||
|
JFileChooser fc = new JFileChooser("C:\\Users\\user\\Documents");
|
||||||
|
fc.addChoosableFileFilter(new TxtFilter());
|
||||||
|
int ret = fc.showDialog(null, "Открыть файл");
|
||||||
|
if(ret == JFileChooser.APPROVE_OPTION){
|
||||||
|
File file = fc.getSelectedFile();
|
||||||
|
try {
|
||||||
|
storage.loadData(file);
|
||||||
|
reloadObjects();
|
||||||
|
pictureBoxCollection.repaint();
|
||||||
|
} catch (IOException ex) {
|
||||||
|
throw new RuntimeException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
private void loadSet(){
|
||||||
|
JFileChooser fc = new JFileChooser("C:\\Users\\user\\Documents");
|
||||||
|
int ret = fc.showDialog(null, "Открыть файл");
|
||||||
|
fc.addChoosableFileFilter(new TxtFilter());
|
||||||
|
if(ret == JFileChooser.APPROVE_OPTION){
|
||||||
|
File file = fc.getSelectedFile();
|
||||||
|
try {
|
||||||
|
storage.loadCollection(file);
|
||||||
|
reloadObjects();
|
||||||
|
pictureBoxCollection.repaint();
|
||||||
|
} catch (IOException ex) {
|
||||||
|
throw new RuntimeException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
51
src/frames/HardFrame.java
Normal file
51
src/frames/HardFrame.java
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
package frames;
|
||||||
|
|
||||||
|
import drawing_objects.DrawingShip;
|
||||||
|
import drawing_objects.IDrawBlocks;
|
||||||
|
import entities.EntityShip;
|
||||||
|
import generics.HardGeneric;
|
||||||
|
|
||||||
|
import javax.swing.*;
|
||||||
|
import java.awt.*;
|
||||||
|
|
||||||
|
public class HardFrame extends JFrame {
|
||||||
|
HardGeneric<EntityShip, IDrawBlocks> generic;
|
||||||
|
DrawingShip drawing;
|
||||||
|
private final JComponent pictureBox;
|
||||||
|
private final int pictureBoxWidth = 200;
|
||||||
|
private final int pictureBoxHeight = 100;
|
||||||
|
public HardFrame(){
|
||||||
|
setLocationRelativeTo(null);
|
||||||
|
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
|
||||||
|
pictureBox = new JComponent(){
|
||||||
|
public void paintComponent(Graphics graphics){
|
||||||
|
super.paintComponent(graphics);
|
||||||
|
Graphics2D graphics2D = (Graphics2D) graphics;
|
||||||
|
if (drawing != null) drawing.drawTransport(graphics2D);
|
||||||
|
super.repaint();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
pictureBox.setPreferredSize(new Dimension(pictureBoxWidth, pictureBoxHeight));
|
||||||
|
JButton buttonMakeObject = new JButton("Создать новый объект");
|
||||||
|
generic = new HardGeneric<>(10, 25, pictureBoxWidth, pictureBoxHeight);
|
||||||
|
boolean check;
|
||||||
|
do{
|
||||||
|
check = generic.insertShip(generic.makeRandomShip());
|
||||||
|
}while(check);
|
||||||
|
do {
|
||||||
|
check = generic.insertBlock(generic.makeRandomBlock());
|
||||||
|
}while(check);
|
||||||
|
buttonMakeObject.addActionListener(e -> {
|
||||||
|
DrawingShip drawingShip = generic.makeObject();
|
||||||
|
drawingShip.setPosition(pictureBoxWidth / 2 - drawingShip.getWidth()/2, pictureBoxHeight / 2 - drawingShip.getHeight()/2);
|
||||||
|
drawing = drawingShip;
|
||||||
|
draw();
|
||||||
|
});
|
||||||
|
setLayout(new BorderLayout());
|
||||||
|
add(pictureBox, BorderLayout.CENTER);
|
||||||
|
add(buttonMakeObject, BorderLayout.SOUTH);
|
||||||
|
pack();
|
||||||
|
setVisible(true);
|
||||||
|
}
|
||||||
|
void draw(){pictureBox.repaint();}
|
||||||
|
}
|
77
src/generics/HardGeneric.java
Normal file
77
src/generics/HardGeneric.java
Normal file
@ -0,0 +1,77 @@
|
|||||||
|
package generics;
|
||||||
|
|
||||||
|
import drawing_objects.*;
|
||||||
|
import entities.EntityBattleship;
|
||||||
|
import entities.EntityShip;
|
||||||
|
|
||||||
|
import java.awt.*;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
public class HardGeneric<T extends EntityShip, U extends IDrawBlocks>{
|
||||||
|
T[] ships;
|
||||||
|
U[] blocks;
|
||||||
|
private int shipsNumber;
|
||||||
|
private int blocksNumber;
|
||||||
|
private final int PICTURE_WIDTH;
|
||||||
|
private final int PICTURE_HEIGHT;
|
||||||
|
public HardGeneric(int shipsCount, int blocksCount, int width, int height){
|
||||||
|
shipsNumber = 0;
|
||||||
|
blocksNumber = 0;
|
||||||
|
ships = (T[]) new EntityShip[shipsCount];
|
||||||
|
blocks = (U[]) new IDrawBlocks[blocksCount];
|
||||||
|
PICTURE_HEIGHT = height;
|
||||||
|
PICTURE_WIDTH = width;
|
||||||
|
}
|
||||||
|
public boolean insertShip(T entityShip){
|
||||||
|
if(ships[ships.length-1] != null)
|
||||||
|
return false;
|
||||||
|
for(int i = shipsNumber -1; i>= 0; i--) {
|
||||||
|
ships[i + 1] = ships[i];
|
||||||
|
}
|
||||||
|
shipsNumber++;
|
||||||
|
ships[0] = entityShip;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
public boolean insertBlock(U block){
|
||||||
|
if(blocks[blocks.length-1] != null)
|
||||||
|
return false;
|
||||||
|
for(int i = blocksNumber -1; i>= 0; i--) {
|
||||||
|
blocks[i + 1] = blocks[i];
|
||||||
|
}
|
||||||
|
blocksNumber++;
|
||||||
|
blocks[0] = block;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
public DrawingShip makeObject(){
|
||||||
|
Random rand = new Random();
|
||||||
|
EntityShip entity = ships[rand.nextInt(0, shipsNumber)];
|
||||||
|
IDrawBlocks block = blocks[rand.nextInt(0, blocksNumber)];
|
||||||
|
if (entity instanceof EntityBattleship)
|
||||||
|
return new DrawingBattleship(entity.getSpeed(), entity.getWeight(), entity.getBodyColor(), ((EntityBattleship) entity).getAdditionalColor(),
|
||||||
|
((EntityBattleship) entity).getTurret(), ((EntityBattleship) entity).getRocketLauncher(), PICTURE_WIDTH, PICTURE_HEIGHT, block.getType(), block.getNumber());
|
||||||
|
return new DrawingShip(entity.getSpeed(), entity.getWeight(), entity.getBodyColor(), PICTURE_WIDTH, PICTURE_HEIGHT, block.getType(), block.getNumber());
|
||||||
|
}
|
||||||
|
public EntityShip makeRandomShip(){
|
||||||
|
Random random = new Random();
|
||||||
|
if (random.nextInt(2) == 0) {
|
||||||
|
return new EntityShip(random.nextInt(100, 300), random.nextDouble(1000, 3000),
|
||||||
|
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)));
|
||||||
|
} else {
|
||||||
|
return new EntityBattleship(random.nextInt(100, 300), random.nextDouble(1000, 3000),
|
||||||
|
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)),
|
||||||
|
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)),
|
||||||
|
random.nextBoolean(), random.nextBoolean());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public IDrawBlocks makeRandomBlock(){
|
||||||
|
Random random = new Random();
|
||||||
|
IDrawBlocks block;
|
||||||
|
switch (random.nextInt(3)){
|
||||||
|
case 1 -> block = new DrawingRoundBlocks();
|
||||||
|
case 2 -> block = new DrawingCrossBlocks();
|
||||||
|
default -> block = new DrawingBlocks();
|
||||||
|
}
|
||||||
|
block.setNumber((random.nextInt(3) + 1) * 2);
|
||||||
|
return block;
|
||||||
|
}
|
||||||
|
}
|
40
src/generics/SetGeneric.java
Normal file
40
src/generics/SetGeneric.java
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
package generics;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
public class SetGeneric<T extends Object>{
|
||||||
|
private final ArrayList<T> places;
|
||||||
|
public int getCount() {
|
||||||
|
return places.size();
|
||||||
|
}
|
||||||
|
private final int maxCount;
|
||||||
|
public SetGeneric(int count){
|
||||||
|
maxCount = count;
|
||||||
|
places = new ArrayList<>();
|
||||||
|
}
|
||||||
|
public boolean insert(T ship){
|
||||||
|
if(places.size() == maxCount)
|
||||||
|
return false;
|
||||||
|
return insert(ship, 0);
|
||||||
|
}
|
||||||
|
public boolean insert(T ship, int position){
|
||||||
|
if (!(position >= 0 && position <= places.size() && places.size() < maxCount))
|
||||||
|
return false;
|
||||||
|
places.add(position, ship);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
public boolean remove(int position){
|
||||||
|
if(!(position >= 0 && position < places.size()))
|
||||||
|
return false;
|
||||||
|
places.remove(position);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
public T get(int position){
|
||||||
|
if(!(position >= 0 && position < places.size()))
|
||||||
|
return null;
|
||||||
|
return places.get(position);
|
||||||
|
}
|
||||||
|
public ArrayList<T> getShips(){
|
||||||
|
return new ArrayList<>(places);
|
||||||
|
}
|
||||||
|
}
|
102
src/generics/ShipsGenericCollection.java
Normal file
102
src/generics/ShipsGenericCollection.java
Normal file
@ -0,0 +1,102 @@
|
|||||||
|
package generics;
|
||||||
|
|
||||||
|
import drawing_objects.*;
|
||||||
|
import movement_strategy.*;
|
||||||
|
|
||||||
|
import java.awt.*;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileWriter;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
public class ShipsGenericCollection<T extends DrawingShip, U extends IMoveableObject>{
|
||||||
|
private final int pictureWidth;
|
||||||
|
private final int pictureHeight;
|
||||||
|
private final int placeSizeWidth = 220;
|
||||||
|
private final int placeSizeHeight = 60;
|
||||||
|
public static char separatorRecords = ';';
|
||||||
|
public static char separatorForObject = ':';
|
||||||
|
private SetGeneric<T> collection;
|
||||||
|
public ShipsGenericCollection(int picWidth, int picHeight){
|
||||||
|
int width = picWidth / placeSizeWidth;
|
||||||
|
int height = picHeight / placeSizeHeight;
|
||||||
|
pictureWidth = picWidth;
|
||||||
|
pictureHeight = picHeight;
|
||||||
|
collection = new SetGeneric<>(width * height);
|
||||||
|
}
|
||||||
|
public boolean insert ( T obj) {
|
||||||
|
if (obj != null)
|
||||||
|
return collection.insert(obj);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
public boolean remove (int pos){
|
||||||
|
return collection.remove(pos);
|
||||||
|
}
|
||||||
|
public U getU(int pos){
|
||||||
|
if(collection.get(pos) == null)
|
||||||
|
return null;
|
||||||
|
return (U)collection.get(pos).getMoveableObject();
|
||||||
|
}
|
||||||
|
public T get(int position){
|
||||||
|
if(position < 0 || position >= collection.getCount())
|
||||||
|
return null;
|
||||||
|
return collection.get(position);
|
||||||
|
}
|
||||||
|
public int getCount(){
|
||||||
|
return collection.getCount();
|
||||||
|
}
|
||||||
|
public void showShips(Graphics2D graphics2D){
|
||||||
|
drawBackground(graphics2D);
|
||||||
|
drawObjects(graphics2D);
|
||||||
|
}
|
||||||
|
private void drawBackground(Graphics2D g){
|
||||||
|
BasicStroke stroke = new BasicStroke(3);
|
||||||
|
g.setStroke(stroke);
|
||||||
|
for (int i = 0; i < pictureWidth / placeSizeWidth; i++){
|
||||||
|
for (int j = 0; j < pictureHeight / placeSizeHeight + 1; ++j){
|
||||||
|
g.drawLine(i * placeSizeWidth, j * placeSizeHeight, i *
|
||||||
|
placeSizeWidth + placeSizeWidth / 2, j * placeSizeHeight);
|
||||||
|
}
|
||||||
|
g.drawLine(i * placeSizeWidth, 0, i * placeSizeWidth,
|
||||||
|
pictureHeight / placeSizeHeight * placeSizeHeight);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
private void drawObjects(Graphics2D g){
|
||||||
|
for (int i = 0; i < collection.getCount(); i++){
|
||||||
|
DrawingShip ship = collection.get(i);
|
||||||
|
if (ship != null)
|
||||||
|
{
|
||||||
|
int inRow = pictureWidth / placeSizeWidth;
|
||||||
|
ship.setPosition(i % inRow * placeSizeWidth, pictureHeight - pictureHeight % placeSizeHeight - (i / inRow + 1) * placeSizeHeight + 5);
|
||||||
|
ship.drawTransport(g);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public boolean saveData(File f, String name) throws IOException {
|
||||||
|
if(f.exists()) {
|
||||||
|
f.delete();
|
||||||
|
}
|
||||||
|
f.createNewFile();
|
||||||
|
StringBuilder data = new StringBuilder();
|
||||||
|
data.append("ShipsCollection\n");
|
||||||
|
data.append(String.format("%s\n", name));
|
||||||
|
StringBuilder records = new StringBuilder();
|
||||||
|
for(DrawingShip elem : getShips()) {
|
||||||
|
records.append(String.format("%s%c", ExtensionDrawingShip.getDataForSave(elem, separatorForObject), separatorRecords));
|
||||||
|
}
|
||||||
|
data.append(records);
|
||||||
|
if(data.length() == 0)
|
||||||
|
return false;
|
||||||
|
FileWriter writer = new FileWriter(f);
|
||||||
|
writer.write(data.toString());
|
||||||
|
writer.flush();
|
||||||
|
writer.close();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
public ArrayList<T> getShips(){
|
||||||
|
return collection.getShips();
|
||||||
|
}
|
||||||
|
public void clear(){
|
||||||
|
collection = new SetGeneric<>(pictureWidth * pictureHeight);
|
||||||
|
}
|
||||||
|
}
|
152
src/generics/ShipsGenericStorage.java
Normal file
152
src/generics/ShipsGenericStorage.java
Normal file
@ -0,0 +1,152 @@
|
|||||||
|
package generics;
|
||||||
|
|
||||||
|
import drawing_objects.DrawingShip;
|
||||||
|
import drawing_objects.ExtensionDrawingShip;
|
||||||
|
import movement_strategy.DrawingObjectShip;
|
||||||
|
|
||||||
|
import java.io.*;
|
||||||
|
import java.util.*;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class ShipsGenericStorage {
|
||||||
|
private final int pictureWidth;
|
||||||
|
private final int pictureHeight;
|
||||||
|
public final HashMap<String, ShipsGenericCollection<DrawingShip, DrawingObjectShip>> shipsStorages;
|
||||||
|
private static final char separatorForKeyValue = '|';
|
||||||
|
private final char separatorRecords = ';';
|
||||||
|
private static final char separatorForObject = ':';
|
||||||
|
public ShipsGenericStorage(int pictureWidth, int pictureHeight){
|
||||||
|
shipsStorages = new HashMap<>();
|
||||||
|
this.pictureWidth = pictureWidth;
|
||||||
|
this.pictureHeight = pictureHeight;
|
||||||
|
}
|
||||||
|
public ArrayList<String> getKeys(){
|
||||||
|
return new ArrayList<>(shipsStorages.keySet());
|
||||||
|
}
|
||||||
|
public void addSet(String name){
|
||||||
|
if(shipsStorages.containsKey(name))
|
||||||
|
return;
|
||||||
|
shipsStorages.put(name, new ShipsGenericCollection<>(pictureWidth, pictureHeight));
|
||||||
|
}
|
||||||
|
public ShipsGenericCollection<DrawingShip, DrawingObjectShip> getSet(String name){
|
||||||
|
if(!shipsStorages.containsKey(name))
|
||||||
|
return null;
|
||||||
|
return shipsStorages.get(name);
|
||||||
|
}
|
||||||
|
public DrawingShip getShip(String collectionName, int position){
|
||||||
|
return shipsStorages.get(collectionName).get(position);
|
||||||
|
}
|
||||||
|
public boolean saveData(File f) throws IOException {
|
||||||
|
if(f.exists()) {
|
||||||
|
f.delete();
|
||||||
|
}
|
||||||
|
f.createNewFile();
|
||||||
|
StringBuilder data = new StringBuilder();
|
||||||
|
data.append("ShipStorage\n");
|
||||||
|
for(Map.Entry<String, ShipsGenericCollection<DrawingShip, DrawingObjectShip>> record : shipsStorages.entrySet()){
|
||||||
|
StringBuilder records = new StringBuilder();
|
||||||
|
for(DrawingShip elem : record.getValue().getShips()) {
|
||||||
|
records.append(String.format("%s%c", ExtensionDrawingShip.getDataForSave(elem, separatorForObject), separatorRecords));
|
||||||
|
}
|
||||||
|
data.append(String.format("%s%c%s\n", record.getKey(), separatorForKeyValue, records));
|
||||||
|
}
|
||||||
|
if(data.length() == 0)
|
||||||
|
return false;
|
||||||
|
FileWriter writer = new FileWriter(f);
|
||||||
|
writer.write(data.toString());
|
||||||
|
writer.flush();
|
||||||
|
writer.close();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
public boolean loadData(File f) throws FileNotFoundException {
|
||||||
|
if(!f.exists())
|
||||||
|
return false;
|
||||||
|
StringBuilder bufferTextFromFile =new StringBuilder();
|
||||||
|
Scanner s = new Scanner(f);
|
||||||
|
while(s.hasNext())
|
||||||
|
bufferTextFromFile.append(s.next()).append("\n");
|
||||||
|
s.close();
|
||||||
|
var strs = bufferTextFromFile.toString().split("\n");
|
||||||
|
if(strs.length == 0)
|
||||||
|
return false;
|
||||||
|
if (!strs[0].startsWith("ShipStorage"))
|
||||||
|
return false;
|
||||||
|
shipsStorages.clear();
|
||||||
|
for(String data : strs){
|
||||||
|
String st = "\\" + separatorForKeyValue;
|
||||||
|
String[]record = data.split(st);
|
||||||
|
if (record.length != 2)
|
||||||
|
continue;
|
||||||
|
ShipsGenericCollection<DrawingShip, DrawingObjectShip> collection = new ShipsGenericCollection<>(pictureWidth, pictureHeight);
|
||||||
|
String[] set = record[1].split(Character.toString(separatorRecords));
|
||||||
|
for(int i = set.length -1; i >=0; i--){
|
||||||
|
String elem = set[i];
|
||||||
|
DrawingShip ship = ExtensionDrawingShip.createDrawingShip(elem, separatorForObject, pictureWidth, pictureHeight);
|
||||||
|
if (ship != null)
|
||||||
|
{
|
||||||
|
if (!(collection.insert(ship)))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
shipsStorages.put(record[0], collection);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
public boolean loadCollection(File f) throws FileNotFoundException {
|
||||||
|
if(!f.exists())
|
||||||
|
return false;
|
||||||
|
StringBuilder bufferTextFromFile =new StringBuilder();
|
||||||
|
Scanner s = new Scanner(f);
|
||||||
|
while(s.hasNext())
|
||||||
|
bufferTextFromFile.append(s.next()).append("\n");
|
||||||
|
s.close();
|
||||||
|
var strs = bufferTextFromFile.toString().split("\n");
|
||||||
|
if(strs.length == 0)
|
||||||
|
return false;
|
||||||
|
if (!strs[0].startsWith("ShipsCollection"))
|
||||||
|
return false;
|
||||||
|
String collectionName = strs[1];
|
||||||
|
ShipsGenericCollection<DrawingShip, DrawingObjectShip> collection = getCollection(collectionName);
|
||||||
|
if(collection == null)
|
||||||
|
collection = new ShipsGenericCollection<>(pictureWidth, pictureHeight);
|
||||||
|
else
|
||||||
|
collection.clear();
|
||||||
|
String[] shipsInfo = strs[2].split(Character.toString(ShipsGenericCollection.separatorRecords));
|
||||||
|
for(int i = shipsInfo.length-1; i >= 0; i--){
|
||||||
|
String data = shipsInfo[i];
|
||||||
|
DrawingShip ship = ExtensionDrawingShip.createDrawingShip(data, ShipsGenericCollection.separatorForObject, pictureWidth, pictureHeight);
|
||||||
|
if (ship != null)
|
||||||
|
{
|
||||||
|
if (!(collection.insert(ship)))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
addSetFromFile(collectionName, collection);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
public void addSetFromFile(String name, ShipsGenericCollection<DrawingShip, DrawingObjectShip> toAdd){
|
||||||
|
if(shipsStorages.containsKey(name)){
|
||||||
|
shipsStorages.remove(name);
|
||||||
|
}
|
||||||
|
shipsStorages.put(name, toAdd);
|
||||||
|
}
|
||||||
|
public void delSet(String name, LinkedList<DrawingShip> trashBox){
|
||||||
|
if(!shipsStorages.containsKey(name))
|
||||||
|
return;
|
||||||
|
ShipsGenericCollection<DrawingShip, DrawingObjectShip> cur = shipsStorages.get(name);
|
||||||
|
for(int i = 0; i < cur.getCount(); i++)
|
||||||
|
trashBox.push(cur.get(i));
|
||||||
|
shipsStorages.remove(name);
|
||||||
|
}
|
||||||
|
public ShipsGenericCollection<DrawingShip, DrawingObjectShip> getCollection(String collectionName){
|
||||||
|
return shipsStorages.get(collectionName);
|
||||||
|
}
|
||||||
|
}
|
64
src/movement_strategy/AbstractStrategy.java
Normal file
64
src/movement_strategy/AbstractStrategy.java
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
package movement_strategy;
|
||||||
|
|
||||||
|
import drawing_objects.DirectionType;
|
||||||
|
|
||||||
|
public abstract class AbstractStrategy {
|
||||||
|
private IMoveableObject moveableObject;
|
||||||
|
private Status state = Status.NOT_INIT;
|
||||||
|
private int fieldWidth;
|
||||||
|
protected int getFieldWidth(){return fieldWidth;}
|
||||||
|
private int fieldHeight;
|
||||||
|
protected int getFieldHeight(){return fieldHeight;}
|
||||||
|
public Status getStatus() {return state;}
|
||||||
|
public void setData(IMoveableObject moveableObject, int width, int height){
|
||||||
|
if (moveableObject == null)
|
||||||
|
{
|
||||||
|
state = Status.NOT_INIT;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
state = Status.IN_PROGRESS;
|
||||||
|
this.moveableObject = moveableObject;
|
||||||
|
fieldWidth = width;
|
||||||
|
fieldHeight = height;
|
||||||
|
}
|
||||||
|
public void makeStep(){
|
||||||
|
if (state != Status.IN_PROGRESS) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (isTargetDestination()) {
|
||||||
|
state = Status.FINISH;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
moveToTarget();
|
||||||
|
}
|
||||||
|
protected boolean moveLeft() {return moveTo(DirectionType.LEFT);}
|
||||||
|
protected boolean moveRight() {return moveTo(DirectionType.RIGHT);}
|
||||||
|
protected boolean moveUp() {return moveTo(DirectionType.UP);}
|
||||||
|
protected boolean moveDown() {return moveTo(DirectionType.DOWN);}
|
||||||
|
protected ObjectParameters getObjectParameters(){
|
||||||
|
if(moveableObject != null)
|
||||||
|
return moveableObject.getObjectPosition();
|
||||||
|
else return null;
|
||||||
|
}
|
||||||
|
protected Integer getStep() {
|
||||||
|
if (state != Status.IN_PROGRESS)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return moveableObject.getStep();
|
||||||
|
}
|
||||||
|
protected abstract void moveToTarget();
|
||||||
|
protected abstract boolean isTargetDestination();
|
||||||
|
private boolean moveTo(DirectionType directionType) {
|
||||||
|
if (state != Status.IN_PROGRESS)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (moveableObject.checkCanMove(directionType))
|
||||||
|
{
|
||||||
|
moveableObject.moveObject(directionType);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
36
src/movement_strategy/DrawingObjectShip.java
Normal file
36
src/movement_strategy/DrawingObjectShip.java
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
package movement_strategy;
|
||||||
|
|
||||||
|
import drawing_objects.DrawingShip;
|
||||||
|
import drawing_objects.DirectionType;
|
||||||
|
|
||||||
|
public class DrawingObjectShip implements IMoveableObject{
|
||||||
|
private final DrawingShip drawingShip;
|
||||||
|
public DrawingObjectShip(DrawingShip drawingShip){
|
||||||
|
this.drawingShip = drawingShip;
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public ObjectParameters getObjectPosition(){
|
||||||
|
if(drawingShip == null || drawingShip.getEntityShip() == null)
|
||||||
|
return null;
|
||||||
|
return new ObjectParameters(drawingShip.getPosX(), drawingShip.getPosY(),
|
||||||
|
drawingShip.getWidth(), drawingShip.getHeight());
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public int getStep(){
|
||||||
|
if(drawingShip.getEntityShip() == null)
|
||||||
|
return 0;
|
||||||
|
return drawingShip.getEntityShip().step.get().intValue();
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public boolean checkCanMove(DirectionType direction){
|
||||||
|
if(drawingShip == null)
|
||||||
|
return false;
|
||||||
|
return drawingShip.canMove(direction);
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public void moveObject(DirectionType direction){
|
||||||
|
if(drawingShip == null)
|
||||||
|
return;
|
||||||
|
drawingShip.moveTransport(direction);
|
||||||
|
}
|
||||||
|
}
|
10
src/movement_strategy/IMoveableObject.java
Normal file
10
src/movement_strategy/IMoveableObject.java
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
package movement_strategy;
|
||||||
|
|
||||||
|
import drawing_objects.DirectionType;
|
||||||
|
|
||||||
|
public interface IMoveableObject {
|
||||||
|
ObjectParameters getObjectPosition();
|
||||||
|
int getStep();
|
||||||
|
boolean checkCanMove(DirectionType direction);
|
||||||
|
void moveObject(DirectionType direction);
|
||||||
|
}
|
32
src/movement_strategy/MoveToBorder.java
Normal file
32
src/movement_strategy/MoveToBorder.java
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
package movement_strategy;
|
||||||
|
|
||||||
|
public class MoveToBorder extends AbstractStrategy {
|
||||||
|
@Override
|
||||||
|
protected boolean isTargetDestination() {
|
||||||
|
var objParams = getObjectParameters();
|
||||||
|
if (objParams == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return objParams.getRightBorder() + getStep() >= getFieldWidth() &&
|
||||||
|
objParams.getDownBorder() + getStep() >= getFieldHeight();
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
protected void moveToTarget() {
|
||||||
|
var objParams = getObjectParameters();
|
||||||
|
if (objParams == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
var diffX = objParams.getRightBorder() - getFieldWidth();
|
||||||
|
if (Math.abs(diffX) >= getStep()) {
|
||||||
|
if (diffX < 0) {
|
||||||
|
moveRight();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var diffY = objParams.getDownBorder() - getFieldHeight();
|
||||||
|
if (Math.abs(diffY) >= getStep()) {
|
||||||
|
if (diffY < 0) {
|
||||||
|
moveDown();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
39
src/movement_strategy/MoveToCenter.java
Normal file
39
src/movement_strategy/MoveToCenter.java
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
package movement_strategy;
|
||||||
|
|
||||||
|
public class MoveToCenter extends AbstractStrategy{
|
||||||
|
@Override
|
||||||
|
protected boolean isTargetDestination(){
|
||||||
|
var objParams = getObjectParameters();
|
||||||
|
if(objParams == null)
|
||||||
|
return false;
|
||||||
|
return objParams.getObjectMiddleHorizontal() <= getFieldWidth() / 2 &&
|
||||||
|
objParams.getObjectMiddleHorizontal() + getStep() >= getFieldWidth() / 2 &&
|
||||||
|
objParams.getObjectMiddleVertical() <= getFieldHeight() / 2 &&
|
||||||
|
objParams.getObjectMiddleVertical() + getStep() >= getFieldHeight() / 2;
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
protected void moveToTarget() {
|
||||||
|
ObjectParameters objParams = getObjectParameters();
|
||||||
|
if (objParams == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
var diffX = objParams.getObjectMiddleHorizontal() - getFieldWidth() / 2;
|
||||||
|
if (Math.abs(diffX) > getStep()) {
|
||||||
|
if (diffX > 0) {
|
||||||
|
moveLeft();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
moveRight();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var diffY = objParams.getObjectMiddleVertical() - getFieldHeight() / 2;
|
||||||
|
if (Math.abs(diffY) > getStep()) {
|
||||||
|
if (diffY > 0) {
|
||||||
|
moveUp();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
moveDown();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
20
src/movement_strategy/ObjectParameters.java
Normal file
20
src/movement_strategy/ObjectParameters.java
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
package movement_strategy;
|
||||||
|
|
||||||
|
public class ObjectParameters {
|
||||||
|
private final int POS_X;
|
||||||
|
private final int POS_Y;
|
||||||
|
private final int WIDTH;
|
||||||
|
private final int HEIGHT;
|
||||||
|
public int getLeftBorder() {return POS_X;}
|
||||||
|
public int getTopBorder() {return POS_Y;}
|
||||||
|
public int getRightBorder() {return POS_X + WIDTH;}
|
||||||
|
public int getDownBorder() {return POS_Y + HEIGHT;}
|
||||||
|
public int getObjectMiddleHorizontal() {return POS_X + this.WIDTH / 2;}
|
||||||
|
public int getObjectMiddleVertical() {return POS_Y + this.HEIGHT / 2;}
|
||||||
|
public ObjectParameters(int x, int y, int width, int height) {
|
||||||
|
POS_X = x;
|
||||||
|
POS_Y = y;
|
||||||
|
WIDTH = width;
|
||||||
|
HEIGHT = height;
|
||||||
|
}
|
||||||
|
}
|
7
src/movement_strategy/Status.java
Normal file
7
src/movement_strategy/Status.java
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
package movement_strategy;
|
||||||
|
|
||||||
|
public enum Status {
|
||||||
|
NOT_INIT,
|
||||||
|
IN_PROGRESS,
|
||||||
|
FINISH
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user