Compare commits
7 Commits
Author | SHA1 | Date | |
---|---|---|---|
4b488b1a21 | |||
cadb01023b | |||
2accdec263 | |||
41866d9c5f | |||
0d7397b322 | |||
b84c7369ad | |||
febf171390 |
103
AbstractMap.java
Normal file
103
AbstractMap.java
Normal file
@ -0,0 +1,103 @@
|
||||
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 Image createMap(int width, int height, IDrawingObject drawingObject) {
|
||||
_width = width;
|
||||
_height = height;
|
||||
_drawingObject = drawingObject;
|
||||
do {
|
||||
generateMap();
|
||||
} while (!setObjectOnMap());
|
||||
return drawMapWithObject();
|
||||
}
|
||||
|
||||
public Image moveObject(Direction direction) {
|
||||
_drawingObject.moveObject(direction);
|
||||
if (objectIntersects()) {
|
||||
switch (direction) {
|
||||
case Left -> _drawingObject.moveObject(Direction.Right);
|
||||
case Right -> _drawingObject.moveObject(Direction.Left);
|
||||
case Up -> _drawingObject.moveObject(Direction.Down);
|
||||
case Down -> _drawingObject.moveObject(Direction.Up);
|
||||
}
|
||||
}
|
||||
return drawMapWithObject();
|
||||
}
|
||||
|
||||
private boolean setObjectOnMap() {
|
||||
if (_drawingObject == null || _map == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
for (int i = 2; i < _map.length; i++)
|
||||
{
|
||||
for (int j = 2; j < _map[i].length; j++)
|
||||
{
|
||||
_drawingObject.setObject((int) (i * _size_x), (int) (j * _size_y), _width, _height);
|
||||
if (!objectIntersects()) return true;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean objectIntersects() {
|
||||
float[] location = _drawingObject.getCurrentPosition();
|
||||
Rectangle self = new Rectangle((int) location[0], (int) location[2], (int) location[1] - (int) location[0], (int) location[3] - (int) location[2]);
|
||||
for (int i = 0; i < _map.length; i++)
|
||||
{
|
||||
for (int j = 0; j < _map[i].length; j++)
|
||||
{
|
||||
if (_map[i][j] == _barrier)
|
||||
{
|
||||
if (self.intersects(new Rectangle(j * (int) _size_x, i * (int) _size_y, (int) _size_x, (int) _size_y)))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private Image drawMapWithObject() {
|
||||
Image img = new BufferedImage(_width, _height, BufferedImage.TYPE_INT_ARGB);
|
||||
if (_drawingObject == null || _map == null) {
|
||||
return img;
|
||||
}
|
||||
Graphics2D g = (Graphics2D) img.getGraphics();
|
||||
for (int i = 0; i < _map.length; ++i)
|
||||
{
|
||||
for (int j = 0; j < _map[i].length; ++j)
|
||||
{
|
||||
if (_map[i][j] == _freeRoad)
|
||||
{
|
||||
drawRoadPart(g, i, j);
|
||||
} else if (_map[i][j] == _barrier)
|
||||
{
|
||||
drawBarrierPart(g, i, j);
|
||||
}
|
||||
}
|
||||
}
|
||||
_drawingObject.drawingObject(g);
|
||||
return img;
|
||||
}
|
||||
|
||||
protected abstract void generateMap();
|
||||
protected abstract void drawRoadPart(Graphics2D g, int i, int j);
|
||||
protected abstract void drawBarrierPart(Graphics2D g, int i, int j);
|
||||
}
|
5
DecksCount.java
Normal file
5
DecksCount.java
Normal file
@ -0,0 +1,5 @@
|
||||
public enum DecksCount {
|
||||
One,
|
||||
Two,
|
||||
Three
|
||||
}
|
17
DecksType.java
Normal file
17
DecksType.java
Normal file
@ -0,0 +1,17 @@
|
||||
import java.awt.*;
|
||||
import java.util.Random;
|
||||
|
||||
public enum DecksType {
|
||||
Standard,
|
||||
Round,
|
||||
Triangular;
|
||||
|
||||
public static IDrawingDecks random(int decksCount, Color bodyColor) {
|
||||
return switch (new Random().nextInt(DecksType.values().length)) {
|
||||
case 0 -> new DrawingDecks(decksCount, bodyColor);
|
||||
case 1 -> new DrawingRoundDecks(decksCount, bodyColor);
|
||||
case 2 -> new DrawingTriDecks(decksCount, bodyColor);
|
||||
default -> null;
|
||||
};
|
||||
}
|
||||
}
|
6
Direction.java
Normal file
6
Direction.java
Normal file
@ -0,0 +1,6 @@
|
||||
public enum Direction {
|
||||
Up,
|
||||
Down,
|
||||
Left,
|
||||
Right
|
||||
}
|
36
DrawingDecks.java
Normal file
36
DrawingDecks.java
Normal file
@ -0,0 +1,36 @@
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawingDecks implements IDrawingDecks {
|
||||
private DecksCount deckCount;
|
||||
private Color color;
|
||||
|
||||
public DrawingDecks(int decksCount, Color bodyColor) {
|
||||
setDecksCount(decksCount);
|
||||
color = bodyColor;
|
||||
}
|
||||
|
||||
public void setDecksCount(int num) {
|
||||
if (num <= 1) {
|
||||
deckCount = DecksCount.One;
|
||||
} else if (num >= 3) {
|
||||
deckCount = DecksCount.Three;
|
||||
}
|
||||
else {
|
||||
deckCount = DecksCount.Two;
|
||||
}
|
||||
}
|
||||
|
||||
public void draw(Graphics2D g, int x, int y, int shipWidth, int shipHeight) {
|
||||
g.setColor(color != null ? color : Color.BLACK);
|
||||
switch (deckCount) {
|
||||
case Two: {
|
||||
g.fillRect(x, y + 5, 15, 5);
|
||||
g.fillPolygon(new int[] {x, x, x + 5}, new int[] {y + 5, y + 10, y + 10}, 3);
|
||||
}
|
||||
case Three: {
|
||||
g.fillRect(x + shipWidth - 20, y, 20, 10);
|
||||
g.fillPolygon(new int[] {x + shipWidth - 20, x + shipWidth - 20, x + shipWidth - 25}, new int[] {y, y + 10, y + 10}, 3);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
34
DrawingMotorShip.java
Normal file
34
DrawingMotorShip.java
Normal file
@ -0,0 +1,34 @@
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawingMotorShip extends DrawingShip {
|
||||
public DrawingMotorShip(int speed, float weight, Color bodyColor, int decksCount, Color dopColor, boolean pipes, boolean fueltank) {
|
||||
super(speed, weight, bodyColor, decksCount, 80, 50);
|
||||
ship = new EntityMotorShip(speed, weight, bodyColor, dopColor, pipes, fueltank);
|
||||
}
|
||||
|
||||
public DrawingMotorShip(EntityMotorShip entity, IDrawingDecks decks) {
|
||||
super(entity, decks);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drawTransport(Graphics2D g) {
|
||||
if (!(ship instanceof EntityMotorShip motorShip)) {
|
||||
return;
|
||||
}
|
||||
|
||||
g.setColor(motorShip.getDopColor());
|
||||
g.setStroke(new BasicStroke(6));
|
||||
if (motorShip.getPipes()) {
|
||||
g.fillRect((int)_startPosX + 20, (int)_startPosY - 5, 5, 20);
|
||||
g.fillRect((int)_startPosX + 30, (int)_startPosY - 10, 5, 25);
|
||||
g.fillRect((int)_startPosX + 40, (int)_startPosY - 5, 5, 20);
|
||||
}
|
||||
|
||||
g.setColor(motorShip.getBodyColor());
|
||||
super.drawTransport(g);
|
||||
g.setColor(motorShip.getDopColor());
|
||||
if (motorShip.getFuelTank()) {
|
||||
g.fillOval((int)_startPosX + 30, (int)_startPosY + 20, 25, 10);
|
||||
}
|
||||
}
|
||||
}
|
37
DrawingObjectShip.java
Normal file
37
DrawingObjectShip.java
Normal file
@ -0,0 +1,37 @@
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawingObjectShip implements IDrawingObject {
|
||||
private DrawingShip _ship = null;
|
||||
|
||||
public DrawingObjectShip(DrawingShip ship) {
|
||||
_ship = ship;
|
||||
}
|
||||
|
||||
public float getStep() {
|
||||
if (_ship != null && _ship.ship != null) {
|
||||
return _ship.ship.getStep();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
public float[] getCurrentPosition() {
|
||||
if (_ship != null) {
|
||||
return _ship.getCurrentPosition();
|
||||
}
|
||||
return new float[] { 0, 0, 0, 0 };
|
||||
}
|
||||
|
||||
public void moveObject(Direction direction) {
|
||||
if (_ship != null) {
|
||||
_ship.moveTransport(direction);
|
||||
}
|
||||
}
|
||||
|
||||
public void setObject(int x, int y, int width, int height) {
|
||||
_ship.SetPosition(x, y, width, height);
|
||||
}
|
||||
|
||||
public void drawingObject(Graphics2D g) {
|
||||
_ship.drawTransport(g);
|
||||
}
|
||||
}
|
34
DrawingRoundDecks.java
Normal file
34
DrawingRoundDecks.java
Normal file
@ -0,0 +1,34 @@
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawingRoundDecks implements IDrawingDecks {
|
||||
private DecksCount decksCount;
|
||||
private Color color;
|
||||
|
||||
public DrawingRoundDecks(int decksCount, Color bodyColor) {
|
||||
setDecksCount(decksCount);
|
||||
color = bodyColor;
|
||||
}
|
||||
|
||||
public void setDecksCount(int num) {
|
||||
if (num <= 1) {
|
||||
decksCount = DecksCount.One;
|
||||
} else if (num >= 3) {
|
||||
decksCount = DecksCount.Three;
|
||||
}
|
||||
else {
|
||||
decksCount = DecksCount.Two;
|
||||
}
|
||||
}
|
||||
|
||||
public void draw(Graphics2D g, int x, int y, int shipWidth, int shipHeight) {
|
||||
g.setColor(color != null ? color : Color.BLACK);
|
||||
switch (decksCount) {
|
||||
case Two: {
|
||||
g.fillOval(x, y, 20, 20);
|
||||
}
|
||||
case Three: {
|
||||
g.fillOval(x + shipWidth - 20, y, 20, 20);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
124
DrawingShip.java
Normal file
124
DrawingShip.java
Normal file
@ -0,0 +1,124 @@
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawingShip {
|
||||
protected EntityShip ship;
|
||||
protected IDrawingDecks drawingDecks;
|
||||
protected float _startPosX;
|
||||
protected float _startPosY;
|
||||
private Integer _pictureWidth = null;
|
||||
private Integer _pictureHeight = null;
|
||||
protected int _shipWidth = 80;
|
||||
protected int _shipHeight = 30;
|
||||
|
||||
public EntityShip getShip() {
|
||||
return ship;
|
||||
}
|
||||
|
||||
public DrawingShip(int speed, float weight, Color bodyColor, int decksCount) {
|
||||
ship = new EntityShip(speed, weight, bodyColor);
|
||||
drawingDecks = DecksType.random(decksCount, bodyColor);
|
||||
}
|
||||
|
||||
public DrawingShip(EntityShip entity, IDrawingDecks decks) {
|
||||
ship = entity;
|
||||
drawingDecks = decks;
|
||||
}
|
||||
|
||||
protected DrawingShip(int speed, float weight, Color bodyColor, int decksCount, int shipWidth, int shipHeight) {
|
||||
this(speed, weight, bodyColor, decksCount);
|
||||
_shipWidth = shipWidth;
|
||||
_shipHeight = shipHeight;
|
||||
}
|
||||
|
||||
public void SetPosition(int x, int y, int width, int height) {
|
||||
if (x < 0 || x + _shipWidth >= width)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (y < 0 || y + _shipHeight >= height)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_startPosX = x;
|
||||
_startPosY = y;
|
||||
_pictureWidth = width;
|
||||
_pictureHeight = height;
|
||||
}
|
||||
|
||||
public void moveTransport(Direction direction)
|
||||
{
|
||||
if (_pictureWidth == null || _pictureHeight == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
switch (direction)
|
||||
{
|
||||
case Right:
|
||||
if (_startPosX + _shipWidth + ship.getStep() < _pictureWidth)
|
||||
{
|
||||
_startPosX += ship.getStep();
|
||||
}
|
||||
break;
|
||||
|
||||
case Left:
|
||||
if (_startPosX - ship.getStep() >= 0)
|
||||
{
|
||||
_startPosX -= ship.getStep();
|
||||
}
|
||||
break;
|
||||
|
||||
case Up:
|
||||
if (_startPosY - ship.getStep() >= 0)
|
||||
{
|
||||
_startPosY -= ship.getStep();
|
||||
}
|
||||
break;
|
||||
|
||||
case Down:
|
||||
if (_startPosY + _shipHeight + ship.getStep() < _pictureHeight)
|
||||
{
|
||||
_startPosY += ship.getStep();
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public void drawTransport(Graphics2D g) {
|
||||
if (_startPosX < 0 || _startPosY < 0 || _pictureHeight == null || _pictureWidth == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
g.setColor(ship.getBodyColor() != null ? ship.getBodyColor() : Color.BLACK);
|
||||
drawingDecks.draw(g, (int) _startPosX, (int) _startPosY, _shipWidth, _shipHeight);
|
||||
int xPoly[] = {(int)_startPosX, (int)_startPosX + 80, (int)_startPosX + 70, (int)_startPosX + 10};
|
||||
int yPoly[] = {(int)_startPosY + 10, (int)_startPosY + 10, (int)_startPosY + 30, (int)_startPosY + 30};
|
||||
g.fillPolygon(xPoly, yPoly, 4);
|
||||
g.fillRect((int)_startPosX + 30, (int)_startPosY, 20, 10);
|
||||
}
|
||||
|
||||
public void changeBorders(int width, int height)
|
||||
{
|
||||
_pictureWidth = width;
|
||||
_pictureHeight = height;
|
||||
if (_pictureWidth <= _shipWidth || _pictureHeight <= _shipHeight)
|
||||
{
|
||||
_pictureWidth = null;
|
||||
_pictureHeight = null;
|
||||
return;
|
||||
}
|
||||
if (_startPosX + _shipWidth > _pictureWidth)
|
||||
{
|
||||
_startPosX = _pictureWidth - _shipWidth;
|
||||
}
|
||||
if (_startPosY + _shipHeight > _pictureHeight)
|
||||
{
|
||||
_startPosY = _pictureHeight - _shipHeight;
|
||||
}
|
||||
}
|
||||
|
||||
public float[] getCurrentPosition() {
|
||||
return new float[] { _startPosX, _startPosX + _shipWidth - 1, _startPosY, _startPosY + _shipHeight -1 };
|
||||
}
|
||||
}
|
34
DrawingTriDecks.java
Normal file
34
DrawingTriDecks.java
Normal file
@ -0,0 +1,34 @@
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawingTriDecks implements IDrawingDecks {
|
||||
private DecksCount decksCount;
|
||||
private Color color;
|
||||
|
||||
public DrawingTriDecks(int decksCount, Color bodyColor) {
|
||||
setDecksCount(decksCount);
|
||||
color = bodyColor;
|
||||
}
|
||||
|
||||
public void setDecksCount(int num) {
|
||||
if (num <= 1) {
|
||||
decksCount = DecksCount.One;
|
||||
} else if (num >= 3) {
|
||||
decksCount = DecksCount.Three;
|
||||
}
|
||||
else {
|
||||
decksCount = DecksCount.Two;
|
||||
}
|
||||
}
|
||||
|
||||
public void draw(Graphics2D g, int x, int y, int shipWidth, int shipHeight) {
|
||||
g.setColor(color != null ? color : Color.BLACK);
|
||||
switch (decksCount) {
|
||||
case Two: {
|
||||
g.fillPolygon(new int[] {x, x, x + 5}, new int[] {y + 5, y + 10, y + 10}, 3);
|
||||
}
|
||||
case Three: {
|
||||
g.fillPolygon(new int[] {x + shipWidth - 20, x + shipWidth - 20, x + shipWidth}, new int[] {y, y + 10, y + 10}, 3);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
26
EntityMotorShip.java
Normal file
26
EntityMotorShip.java
Normal file
@ -0,0 +1,26 @@
|
||||
import java.awt.*;
|
||||
|
||||
public class EntityMotorShip extends EntityShip {
|
||||
private Color dopColor;
|
||||
private boolean pipes;
|
||||
private boolean fueltank;
|
||||
|
||||
public EntityMotorShip(int speed, float weight, Color bodyColor, Color dopColor, boolean pipes, boolean fueltank) {
|
||||
super(speed, weight, bodyColor);
|
||||
this.dopColor = dopColor;
|
||||
this.pipes = pipes;
|
||||
this.fueltank = fueltank;
|
||||
}
|
||||
|
||||
public Color getDopColor() {
|
||||
return dopColor;
|
||||
}
|
||||
|
||||
public boolean getPipes() {
|
||||
return pipes;
|
||||
}
|
||||
|
||||
public boolean getFuelTank() {
|
||||
return fueltank;
|
||||
}
|
||||
}
|
31
EntityShip.java
Normal file
31
EntityShip.java
Normal file
@ -0,0 +1,31 @@
|
||||
import java.awt.*;
|
||||
import java.util.Random;
|
||||
|
||||
public class EntityShip {
|
||||
private int speed;
|
||||
private float weight;
|
||||
private Color bodyColor;
|
||||
|
||||
public EntityShip(int speed, float weight, Color bodyColor) {
|
||||
Random rnd = new Random();
|
||||
this.speed = speed <= 0 ? rnd.nextInt(100) + 50 : speed;
|
||||
this.weight = weight <= 0 ? rnd.nextInt(30) + 40 : weight;
|
||||
this.bodyColor = bodyColor;
|
||||
}
|
||||
|
||||
public int getSpeed() {
|
||||
return speed;
|
||||
}
|
||||
|
||||
public float getWeight() {
|
||||
return weight;
|
||||
}
|
||||
|
||||
public Color getBodyColor() {
|
||||
return bodyColor;
|
||||
}
|
||||
|
||||
public float getStep() {
|
||||
return speed * 100 / weight;
|
||||
}
|
||||
}
|
43
EntityWithDecks.java
Normal file
43
EntityWithDecks.java
Normal file
@ -0,0 +1,43 @@
|
||||
import java.util.Random;
|
||||
|
||||
public class EntityWithDecks<T extends EntityShip, U extends IDrawingDecks> {
|
||||
static Random rnd = new Random();
|
||||
private final Object[] entities;
|
||||
public int entitiesCount = 0;
|
||||
private final Object[] decks;
|
||||
public int decksCount = 0;
|
||||
|
||||
public EntityWithDecks(int count) {
|
||||
entities = new Object[count];
|
||||
decks = new Object[count];
|
||||
}
|
||||
|
||||
public boolean add(EntityShip entity) {
|
||||
if (entitiesCount >= entities.length) {
|
||||
return false;
|
||||
}
|
||||
entities[entitiesCount++] = entity;
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean add(IDrawingDecks deck) {
|
||||
if (decksCount >= decks.length) {
|
||||
return false;
|
||||
}
|
||||
decks[decksCount++] = deck;
|
||||
return true;
|
||||
}
|
||||
|
||||
public IDrawingObject constructShip() {
|
||||
if (entitiesCount == 0 || decksCount == 0) {
|
||||
return null;
|
||||
}
|
||||
EntityShip entity = (EntityShip) entities[rnd.nextInt(0, entitiesCount)];
|
||||
IDrawingDecks deck = (IDrawingDecks) decks[rnd.nextInt(0, decksCount)];
|
||||
|
||||
if (entity instanceof EntityMotorShip advancedEntity) {
|
||||
return new DrawingObjectShip(new DrawingMotorShip(advancedEntity, deck));
|
||||
}
|
||||
return new DrawingObjectShip(new DrawingShip(entity, deck));
|
||||
}
|
||||
}
|
161
FormMapWithSetShips.form
Normal file
161
FormMapWithSetShips.form
Normal file
@ -0,0 +1,161 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="FormMapWithSetShips">
|
||||
<grid id="27dc6" binding="paneShips" 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="620" height="400"/>
|
||||
</constraints>
|
||||
<properties/>
|
||||
<border type="none"/>
|
||||
<children>
|
||||
<grid id="fcfe1" 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="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>
|
||||
<grid id="f232" binding="toolsGroup" layout-manager="GridLayoutManager" row-count="9" 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="0" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="0" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties/>
|
||||
<border type="none"/>
|
||||
<children>
|
||||
<component id="d7a62" class="javax.swing.JLabel" binding="toolsLabel">
|
||||
<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"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<text value="Инструменты"/>
|
||||
</properties>
|
||||
</component>
|
||||
<hspacer id="1c91f">
|
||||
<constraints>
|
||||
<grid row="0" column="1" row-span="1" col-span="3" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
</hspacer>
|
||||
<vspacer id="352bc">
|
||||
<constraints>
|
||||
<grid row="5" column="0" row-span="2" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
</vspacer>
|
||||
<component id="6a0f5" class="javax.swing.JComboBox" binding="comboBoxMapSelector">
|
||||
<constraints>
|
||||
<grid row="1" column="1" row-span="1" col-span="3" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<model>
|
||||
<item value="Простая карта"/>
|
||||
<item value="Водная карта"/>
|
||||
</model>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="52049" class="javax.swing.JButton" binding="buttonAddShip">
|
||||
<constraints>
|
||||
<grid row="2" column="1" row-span="1" col-span="3" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<text value="Добавить корабль"/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="2f9ed" class="javax.swing.JFormattedTextField" binding="textBoxPosition">
|
||||
<constraints>
|
||||
<grid row="3" column="1" 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="1dbb5" class="javax.swing.JButton" binding="buttonRemoveShip">
|
||||
<constraints>
|
||||
<grid row="4" column="1" row-span="1" col-span="3" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<actionCommand value=""/>
|
||||
<text value="Удалить корабль"/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="4e6ca" class="javax.swing.JButton" binding="buttonShowStorage">
|
||||
<constraints>
|
||||
<grid row="5" column="1" row-span="1" col-span="3" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<text value="Посмотреть хранилище"/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="51371" class="javax.swing.JButton" binding="buttonShowOnMap">
|
||||
<constraints>
|
||||
<grid row="6" column="1" row-span="1" col-span="3" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<text value="Посмотреть карту"/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="fb3e4" class="javax.swing.JButton" binding="buttonUp">
|
||||
<constraints>
|
||||
<grid row="7" column="1" row-span="1" col-span="3" vsize-policy="0" hsize-policy="0" anchor="0" fill="1" 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>
|
||||
<borderPainted value="true"/>
|
||||
<contentAreaFilled value="false"/>
|
||||
<icon value="Resources/ArrowUp.png"/>
|
||||
<text value=""/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="328f6" class="javax.swing.JButton" binding="buttonDown">
|
||||
<constraints>
|
||||
<grid row="8" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" 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>
|
||||
<contentAreaFilled value="false"/>
|
||||
<icon value="Resources/ArrowDown.png"/>
|
||||
<text value=""/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="4f2dc" class="javax.swing.JButton" binding="buttonLeft">
|
||||
<constraints>
|
||||
<grid row="8" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" 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>
|
||||
<contentAreaFilled value="false"/>
|
||||
<icon value="Resources/ArrowLeft.png"/>
|
||||
<label value=""/>
|
||||
<text value=""/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="bac06" class="javax.swing.JButton" binding="buttonRight">
|
||||
<constraints>
|
||||
<grid row="8" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" 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>
|
||||
<contentAreaFilled value="false"/>
|
||||
<icon value="Resources/ArrowRight.png"/>
|
||||
<label value=""/>
|
||||
<text value=""/>
|
||||
</properties>
|
||||
</component>
|
||||
</children>
|
||||
</grid>
|
||||
</children>
|
||||
</grid>
|
||||
</form>
|
150
FormMapWithSetShips.java
Normal file
150
FormMapWithSetShips.java
Normal file
@ -0,0 +1,150 @@
|
||||
import javax.swing.*;
|
||||
import javax.swing.text.DefaultFormatterFactory;
|
||||
import javax.swing.text.MaskFormatter;
|
||||
import java.awt.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.text.ParseException;
|
||||
|
||||
public class FormMapWithSetShips extends JFrame {
|
||||
private JPanel pictureBox;
|
||||
private JPanel toolsGroup;
|
||||
private JLabel toolsLabel;
|
||||
private JComboBox comboBoxMapSelector;
|
||||
private JButton buttonAddShip;
|
||||
private JPanel paneShips;
|
||||
private JFormattedTextField textBoxPosition;
|
||||
private JButton buttonRemoveShip;
|
||||
private JButton buttonShowStorage;
|
||||
private JButton buttonUp;
|
||||
private JButton buttonDown;
|
||||
private JButton buttonLeft;
|
||||
private JButton buttonRight;
|
||||
private JButton buttonShowOnMap;
|
||||
|
||||
private Image bufferedImage;
|
||||
private MapWithSetShipsGeneric<DrawingObjectShip, AbstractMap> _mapShipsCollectionGeneric;
|
||||
|
||||
public FormMapWithSetShips() {
|
||||
this.setTitle("Ship");
|
||||
this.setContentPane(paneShips);
|
||||
|
||||
try {
|
||||
textBoxPosition.setFormatterFactory(new DefaultFormatterFactory(new MaskFormatter("##")));
|
||||
} catch (ParseException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
comboBoxMapSelector.addActionListener(e -> {
|
||||
AbstractMap map = switch (((JComboBox) e.getSource()).getSelectedItem().toString()) {
|
||||
case "Простая карта" -> new SimpleMap();
|
||||
case "Водная карта" -> new WaterMap();
|
||||
default -> null;
|
||||
};
|
||||
|
||||
if (map != null) {
|
||||
_mapShipsCollectionGeneric = new MapWithSetShipsGeneric<>(pictureBox.getWidth(), pictureBox.getHeight(), map);
|
||||
} else {
|
||||
_mapShipsCollectionGeneric = null;
|
||||
}
|
||||
});
|
||||
|
||||
buttonAddShip.addActionListener(e -> {
|
||||
if (_mapShipsCollectionGeneric == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
FormShip dialog = new FormShip();
|
||||
dialog.setSize(800, 500);
|
||||
dialog.setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
|
||||
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
|
||||
|
||||
dialog.setVisible(true);
|
||||
|
||||
if (dialog.getSelectedShip() != null) {
|
||||
DrawingObjectShip ship = new DrawingObjectShip(dialog.getSelectedShip());
|
||||
if (_mapShipsCollectionGeneric.addShip(ship) != -1)
|
||||
{
|
||||
JOptionPane.showMessageDialog(this, "Объект добавлен", "Успех", JOptionPane.INFORMATION_MESSAGE);
|
||||
bufferedImage = _mapShipsCollectionGeneric.showSet();
|
||||
repaint();
|
||||
}
|
||||
else
|
||||
{
|
||||
JOptionPane.showMessageDialog(this, "Не удалось добавить объект", "Провал", JOptionPane.INFORMATION_MESSAGE);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
buttonRemoveShip.addActionListener(e -> {
|
||||
String text = textBoxPosition.getText();
|
||||
if (text == null || text.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (JOptionPane.showConfirmDialog(this, "Удалить объект?", "Удаление", JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION) {
|
||||
return;
|
||||
}
|
||||
|
||||
int position = Integer.parseInt(text);
|
||||
|
||||
if (_mapShipsCollectionGeneric.removeShipAt(position) != null) {
|
||||
JOptionPane.showMessageDialog(this, "Объект удалён", "Успех", JOptionPane.INFORMATION_MESSAGE);
|
||||
bufferedImage = _mapShipsCollectionGeneric.showSet();
|
||||
repaint();
|
||||
} else {
|
||||
JOptionPane.showMessageDialog(this, "Не удалось удалить объект", "Провал", JOptionPane.INFORMATION_MESSAGE);
|
||||
}
|
||||
});
|
||||
|
||||
buttonShowStorage.addActionListener(e -> {
|
||||
if (_mapShipsCollectionGeneric == null) {
|
||||
return;
|
||||
}
|
||||
bufferedImage = _mapShipsCollectionGeneric.showSet();
|
||||
repaint();
|
||||
});
|
||||
|
||||
buttonShowOnMap.addActionListener(e -> {
|
||||
if (_mapShipsCollectionGeneric == null) {
|
||||
return;
|
||||
}
|
||||
bufferedImage = _mapShipsCollectionGeneric.showOnMap();
|
||||
repaint();
|
||||
});
|
||||
|
||||
buttonLeft.addActionListener(e -> {
|
||||
if (_mapShipsCollectionGeneric != null) {
|
||||
bufferedImage = _mapShipsCollectionGeneric.moveObject(Direction.Left);
|
||||
repaint();
|
||||
}
|
||||
});
|
||||
buttonRight.addActionListener(e -> {
|
||||
if (_mapShipsCollectionGeneric != null) {
|
||||
bufferedImage = _mapShipsCollectionGeneric.moveObject(Direction.Right);
|
||||
repaint();
|
||||
}
|
||||
});
|
||||
buttonUp.addActionListener(e -> {
|
||||
if (_mapShipsCollectionGeneric != null) {
|
||||
bufferedImage = _mapShipsCollectionGeneric.moveObject(Direction.Up);
|
||||
repaint();
|
||||
}
|
||||
});
|
||||
buttonDown.addActionListener(e -> {
|
||||
if (_mapShipsCollectionGeneric != null) {
|
||||
bufferedImage = _mapShipsCollectionGeneric.moveObject(Direction.Down);
|
||||
repaint();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void paint(Graphics g) {
|
||||
super.paint(g);
|
||||
|
||||
if (bufferedImage != null) {
|
||||
pictureBox.paintComponents(bufferedImage.getGraphics());
|
||||
pictureBox.getGraphics().drawImage(bufferedImage, 0, 0, null);
|
||||
}
|
||||
}
|
||||
}
|
173
FormShip.form
Normal file
173
FormShip.form
Normal file
@ -0,0 +1,173 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="FormShip">
|
||||
<grid id="27dc6" binding="shipPane" 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>
|
||||
<xy x="20" y="20" width="664" height="436"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<background color="-4210753"/>
|
||||
<inheritsPopupMenu value="false"/>
|
||||
<maximumSize width="640" height="480"/>
|
||||
<minimumSize width="80" height="50"/>
|
||||
<preferredSize width="640" height="480"/>
|
||||
</properties>
|
||||
<border type="none">
|
||||
<font/>
|
||||
<title-color color="-1908517"/>
|
||||
</border>
|
||||
<children>
|
||||
<grid id="460f0" layout-manager="GridLayoutManager" row-count="1" 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="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false">
|
||||
<minimum-size width="-1" height="20"/>
|
||||
</grid>
|
||||
</constraints>
|
||||
<properties/>
|
||||
<border type="none"/>
|
||||
<children>
|
||||
<component id="ec1f2" class="javax.swing.JLabel" binding="speedLabel">
|
||||
<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"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<text value=""/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="1ffd0" class="javax.swing.JLabel" binding="colorLabel">
|
||||
<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"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<text value=""/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="64b86" class="javax.swing.JLabel" binding="weightLabel">
|
||||
<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"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<text value=""/>
|
||||
</properties>
|
||||
</component>
|
||||
</children>
|
||||
</grid>
|
||||
<grid id="fa947" binding="pictureBox" layout-manager="GridLayoutManager" row-count="3" column-count="8" 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>
|
||||
<vspacer id="402e3">
|
||||
<constraints>
|
||||
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
</vspacer>
|
||||
<vspacer id="bbd8c">
|
||||
<constraints>
|
||||
<grid row="0" column="3" 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="271ef" class="javax.swing.JButton" binding="buttonUp">
|
||||
<constraints>
|
||||
<grid row="1" column="6" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="1" 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>
|
||||
<borderPainted value="true"/>
|
||||
<contentAreaFilled value="false"/>
|
||||
<icon value="Resources/ArrowUp.png"/>
|
||||
<text value=""/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="4b967" class="javax.swing.JButton" binding="buttonDown">
|
||||
<constraints>
|
||||
<grid row="2" column="6" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" 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>
|
||||
<contentAreaFilled value="false"/>
|
||||
<icon value="Resources/ArrowDown.png"/>
|
||||
<text value=""/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="2e663" class="javax.swing.JButton" binding="buttonLeft">
|
||||
<constraints>
|
||||
<grid row="2" column="5" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" 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>
|
||||
<contentAreaFilled value="false"/>
|
||||
<icon value="Resources/ArrowLeft.png"/>
|
||||
<label value=""/>
|
||||
<text value=""/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="c2d76" class="javax.swing.JButton" binding="buttonRight">
|
||||
<constraints>
|
||||
<grid row="2" column="7" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" 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>
|
||||
<contentAreaFilled value="false"/>
|
||||
<icon value="Resources/ArrowRight.png"/>
|
||||
<label value=""/>
|
||||
<text value=""/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="7bc68" class="javax.swing.JButton" binding="createButton">
|
||||
<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>
|
||||
<alignmentX value="0.0"/>
|
||||
<margin top="0" left="0" bottom="0" right="0"/>
|
||||
<text value="Создать"/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="27408" class="javax.swing.JButton" binding="createAdvancedButton">
|
||||
<constraints>
|
||||
<grid row="2" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<text value="Модификация"/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="d034f" class="javax.swing.JButton" binding="buttonSelect">
|
||||
<constraints>
|
||||
<grid row="2" column="4" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<text value="Выбрать"/>
|
||||
</properties>
|
||||
</component>
|
||||
<hspacer id="f89d0">
|
||||
<constraints>
|
||||
<grid row="1" column="3" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
</hspacer>
|
||||
<hspacer id="f89d0">
|
||||
<constraints>
|
||||
<grid row="1" column="3" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
</hspacer>
|
||||
</children>
|
||||
</grid>
|
||||
</children>
|
||||
</grid>
|
||||
</form>
|
105
FormShip.java
Normal file
105
FormShip.java
Normal file
@ -0,0 +1,105 @@
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.*;
|
||||
import java.util.Random;
|
||||
|
||||
public class FormShip extends JDialog {
|
||||
private JPanel shipPane;
|
||||
private JLabel speedLabel;
|
||||
private JLabel weightLabel;
|
||||
private JLabel colorLabel;
|
||||
private JPanel pictureBox;
|
||||
private JButton createButton;
|
||||
private JButton buttonUp;
|
||||
private JButton buttonDown;
|
||||
private JButton buttonLeft;
|
||||
private JButton buttonRight;
|
||||
private JButton createAdvancedButton;
|
||||
public JButton buttonSelect;
|
||||
|
||||
private DrawingShip _ship;
|
||||
private DrawingShip selectedShip;
|
||||
|
||||
public FormShip() {
|
||||
this.setTitle("Ship");
|
||||
this.setContentPane(shipPane);
|
||||
createButton.addActionListener(e -> {
|
||||
Random rnd = new Random();
|
||||
Color color = JColorChooser.showDialog(this, "Цвет", new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)));
|
||||
if (color == null) {
|
||||
color = new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256));
|
||||
}
|
||||
_ship = new DrawingShip(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000), color, rnd.nextInt(1, 4));
|
||||
_ship.SetPosition(10 + rnd.nextInt(90), 10 + rnd.nextInt(90), pictureBox.getWidth(), pictureBox.getHeight());
|
||||
speedLabel.setText(String.format("Speed: %s", _ship.getShip().getSpeed()));
|
||||
weightLabel.setText(String.format("Weight: %s", _ship.getShip().getWeight()));
|
||||
colorLabel.setText(String.format("Color: %x", _ship.getShip().getBodyColor().getRGB()));
|
||||
repaint();
|
||||
});
|
||||
pictureBox.addComponentListener(new ComponentAdapter() {
|
||||
@Override
|
||||
public void componentResized(ComponentEvent e) {
|
||||
if (_ship != null) _ship.changeBorders(pictureBox.getWidth(), pictureBox.getHeight());
|
||||
repaint();
|
||||
}
|
||||
});
|
||||
buttonLeft.addActionListener(e -> {
|
||||
if (_ship != null) _ship.moveTransport(Direction.Left);
|
||||
repaint();
|
||||
});
|
||||
buttonRight.addActionListener(e -> {
|
||||
if (_ship != null) _ship.moveTransport(Direction.Right);
|
||||
repaint();
|
||||
});
|
||||
buttonUp.addActionListener(e -> {
|
||||
if (_ship != null) _ship.moveTransport(Direction.Up);
|
||||
repaint();
|
||||
});
|
||||
buttonDown.addActionListener(e -> {
|
||||
if (_ship != null) _ship.moveTransport(Direction.Down);
|
||||
repaint();
|
||||
});
|
||||
createAdvancedButton.addActionListener(e -> {
|
||||
Random rnd = new Random();
|
||||
Color color = JColorChooser.showDialog(this, "Основной цвет", Color.white);
|
||||
if (color == null) {
|
||||
color = new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256));
|
||||
}
|
||||
Color dopColor = JColorChooser.showDialog(this, "Дополнительный цвет", Color.white);
|
||||
if (dopColor == null) {
|
||||
dopColor = new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256));
|
||||
}
|
||||
_ship = new DrawingMotorShip(
|
||||
rnd.nextInt(100, 300),
|
||||
rnd.nextInt(1000, 2000),
|
||||
color,
|
||||
rnd.nextInt(1, 4),
|
||||
dopColor,
|
||||
rnd.nextBoolean(),
|
||||
rnd.nextBoolean()
|
||||
);
|
||||
_ship.SetPosition(10 + rnd.nextInt(90), 10 + rnd.nextInt(90), pictureBox.getWidth(), pictureBox.getHeight());
|
||||
speedLabel.setText(String.format("Скорость: %s", _ship.getShip().getSpeed()));
|
||||
weightLabel.setText(String.format("Вес: %s", _ship.getShip().getWeight()));
|
||||
colorLabel.setText(String.format("Цвет: %x", _ship.getShip().getBodyColor().getRGB()));
|
||||
repaint();
|
||||
});
|
||||
buttonSelect.addActionListener(e -> {
|
||||
selectedShip = _ship;
|
||||
dispose();
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void paint(Graphics g) {
|
||||
super.paint(g);
|
||||
Graphics2D g2d = (Graphics2D) pictureBox.getGraphics();
|
||||
if (_ship != null) {
|
||||
_ship.drawTransport(g2d);
|
||||
}
|
||||
}
|
||||
|
||||
public DrawingShip getSelectedShip() {
|
||||
return selectedShip;
|
||||
}
|
||||
}
|
30
FormShipDisplay.form
Normal file
30
FormShipDisplay.form
Normal file
@ -0,0 +1,30 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="FormShipDisplay">
|
||||
<grid id="27dc6" binding="contentPane" 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>
|
||||
<xy x="20" y="20" width="323" height="234"/>
|
||||
</constraints>
|
||||
<properties/>
|
||||
<border type="none"/>
|
||||
<children>
|
||||
<grid id="632d3" 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="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>
|
||||
<component id="7e5f8" class="javax.swing.JButton" binding="buttonRefresh">
|
||||
<constraints>
|
||||
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<text value="Обновить"/>
|
||||
</properties>
|
||||
</component>
|
||||
</children>
|
||||
</grid>
|
||||
</form>
|
68
FormShipDisplay.java
Normal file
68
FormShipDisplay.java
Normal file
@ -0,0 +1,68 @@
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.util.Random;
|
||||
|
||||
public class FormShipDisplay extends JFrame {
|
||||
private static final Random rnd = new Random();
|
||||
private JPanel contentPane;
|
||||
private JButton buttonRefresh;
|
||||
private JPanel pictureBox;
|
||||
private IDrawingObject first;
|
||||
private IDrawingObject second;
|
||||
private IDrawingObject third;
|
||||
|
||||
private final EntityWithDecks<EntityShip, IDrawingDecks> storage;
|
||||
|
||||
public FormShipDisplay() {
|
||||
setTitle("Корабли");
|
||||
setContentPane(contentPane);
|
||||
|
||||
storage = new EntityWithDecks<>(20);
|
||||
|
||||
for(int i = 0; i < 20; i++) {
|
||||
var base_color = new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256));
|
||||
var dop_color = new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256));
|
||||
if (rnd.nextBoolean()) {
|
||||
storage.add(new EntityMotorShip(
|
||||
rnd.nextInt(100, 300),
|
||||
rnd.nextInt(1000, 2000),
|
||||
base_color,
|
||||
dop_color,
|
||||
rnd.nextBoolean(),
|
||||
rnd.nextBoolean()
|
||||
));
|
||||
} else {
|
||||
storage.add(new EntityShip(
|
||||
rnd.nextInt(100, 300),
|
||||
rnd.nextInt(1000, 2000),
|
||||
base_color
|
||||
));
|
||||
}
|
||||
storage.add(DecksType.random(rnd.nextInt(0, 3), base_color));
|
||||
}
|
||||
|
||||
buttonRefresh.addActionListener(e -> {
|
||||
first = storage.constructShip();
|
||||
second = storage.constructShip();
|
||||
third = storage.constructShip();
|
||||
|
||||
first.setObject(0, 10, pictureBox.getWidth(), pictureBox.getHeight());
|
||||
second.setObject(90, 10, pictureBox.getWidth(), pictureBox.getHeight());
|
||||
third.setObject(190, 10, pictureBox.getWidth(), pictureBox.getHeight());
|
||||
|
||||
repaint();
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void paint(Graphics g) {
|
||||
super.paint(g);
|
||||
Graphics2D g2g = (Graphics2D) pictureBox.getGraphics();
|
||||
|
||||
if (first != null && second != null && third != null) {
|
||||
first.drawingObject(g2g);
|
||||
second.drawingObject(g2g);
|
||||
third.drawingObject(g2g);
|
||||
}
|
||||
}
|
||||
}
|
6
IDrawingDecks.java
Normal file
6
IDrawingDecks.java
Normal file
@ -0,0 +1,6 @@
|
||||
import java.awt.*;
|
||||
|
||||
public interface IDrawingDecks {
|
||||
void setDecksCount(int num);
|
||||
void draw(Graphics2D g, int x, int y, int shipWidth, int shipHeight);
|
||||
}
|
9
IDrawingObject.java
Normal file
9
IDrawingObject.java
Normal file
@ -0,0 +1,9 @@
|
||||
import java.awt.*;
|
||||
|
||||
public interface IDrawingObject {
|
||||
float getStep();
|
||||
void setObject(int x, int y, int width, int height);
|
||||
void moveObject(Direction direction);
|
||||
void drawingObject(Graphics2D g);
|
||||
float[] getCurrentPosition();
|
||||
}
|
111
MapWithSetShipsGeneric.java
Normal file
111
MapWithSetShipsGeneric.java
Normal file
@ -0,0 +1,111 @@
|
||||
import java.awt.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
|
||||
public class MapWithSetShipsGeneric<T extends IDrawingObject, U extends AbstractMap> {
|
||||
public final int _pictureWidth;
|
||||
public final int _pictureHeight;
|
||||
public final int _placeSizeWidth = 210;
|
||||
public final int _placeSizeHeight = 90;
|
||||
private final SetShipsGeneric<T> _setShips;
|
||||
private final U _map;
|
||||
|
||||
public MapWithSetShipsGeneric(int picWidth, int picHeight, U map) {
|
||||
int width = picWidth / _placeSizeWidth;
|
||||
int height = picHeight / _placeSizeHeight;
|
||||
_setShips = new SetShipsGeneric<T>(width * height);
|
||||
_pictureWidth = picWidth;
|
||||
_pictureHeight = picHeight;
|
||||
_map = map;
|
||||
}
|
||||
|
||||
public int addShip(T ship) {
|
||||
return _setShips.insert(ship);
|
||||
}
|
||||
|
||||
public T removeShipAt(int position) {
|
||||
return _setShips.remove(position);
|
||||
}
|
||||
|
||||
public Image showSet() {
|
||||
BufferedImage img = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_ARGB);
|
||||
Graphics2D g2d = (Graphics2D) img.getGraphics();
|
||||
drawBackground(g2d);
|
||||
drawShips(g2d);
|
||||
return img;
|
||||
}
|
||||
|
||||
public Image showOnMap() {
|
||||
shaking();
|
||||
for (int i = 0; i < _setShips.getCount(); i++)
|
||||
{
|
||||
var ship = _setShips.get(i);
|
||||
if (ship != null)
|
||||
{
|
||||
return _map.createMap(_pictureWidth, _pictureHeight, ship);
|
||||
}
|
||||
}
|
||||
return new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_ARGB);
|
||||
}
|
||||
|
||||
public Image moveObject(Direction direction) {
|
||||
if (_map != null) {
|
||||
return _map.moveObject(direction);
|
||||
}
|
||||
return new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_ARGB);
|
||||
}
|
||||
|
||||
private void shaking() {
|
||||
int j = _setShips.getCount() - 1;
|
||||
for (int i = 0; i < _setShips.getCount(); i++)
|
||||
{
|
||||
if (_setShips.get(i) == null)
|
||||
{
|
||||
for (; j > i; j--)
|
||||
{
|
||||
var ship = _setShips.get(j);
|
||||
if (ship != null)
|
||||
{
|
||||
_setShips.insert(ship, i);
|
||||
_setShips.remove(j);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (j <= i)
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void drawBackground(Graphics2D g) {
|
||||
Color pen = Color.black;
|
||||
Color box = new Color(0, 100, 0, 255);
|
||||
Color flag = Color.red;
|
||||
Stroke normalStroke = new BasicStroke(1);
|
||||
Stroke penStroke = new BasicStroke(3);
|
||||
Stroke thinPenStroke = new BasicStroke(2);
|
||||
|
||||
for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++)
|
||||
{
|
||||
for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j) {
|
||||
}
|
||||
}
|
||||
g.setStroke(normalStroke);
|
||||
}
|
||||
|
||||
private void drawShips(Graphics2D g) {
|
||||
int width = _pictureWidth / _placeSizeWidth;
|
||||
int height = _pictureHeight / _placeSizeHeight;
|
||||
|
||||
for (int i = 0; i < _setShips.getCount(); i++)
|
||||
{
|
||||
var ship = _setShips.get(i);
|
||||
if (ship != null)
|
||||
{
|
||||
ship.setObject(i % width * _placeSizeWidth + 10, (height - 1 - i / width) * _placeSizeHeight + 10, _pictureWidth, _pictureHeight);
|
||||
ship.drawingObject(g);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
10
Program.java
Normal file
10
Program.java
Normal file
@ -0,0 +1,10 @@
|
||||
import javax.swing.*;
|
||||
|
||||
public class Program {
|
||||
public static void main(String[] args) {
|
||||
FormShipDisplay form = new FormShipDisplay();
|
||||
form.setSize(640, 480);
|
||||
form.setVisible(true);
|
||||
form.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
|
||||
}
|
||||
}
|
BIN
Resources/ArrowDown.png
Normal file
BIN
Resources/ArrowDown.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 264 B |
BIN
Resources/ArrowLeft.png
Normal file
BIN
Resources/ArrowLeft.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 292 B |
BIN
Resources/ArrowRight.png
Normal file
BIN
Resources/ArrowRight.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 287 B |
BIN
Resources/ArrowUp.png
Normal file
BIN
Resources/ArrowUp.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 262 B |
70
SetShipsGeneric.java
Normal file
70
SetShipsGeneric.java
Normal file
@ -0,0 +1,70 @@
|
||||
public class SetShipsGeneric<T> {
|
||||
private final Object[] _places;
|
||||
|
||||
public int getCount() {
|
||||
return _places.length;
|
||||
}
|
||||
|
||||
public SetShipsGeneric(int count) {
|
||||
_places = new Object[count];
|
||||
}
|
||||
|
||||
public int insert(T ship) {
|
||||
return insert(ship, 0);
|
||||
}
|
||||
|
||||
public int insert(T ship, int position) {
|
||||
if (position < 0 || position >= getCount()) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (_places[position] == null) {
|
||||
_places[position] = ship;
|
||||
return position;
|
||||
}
|
||||
|
||||
int firstNull = -1;
|
||||
|
||||
for (int i = position + 1; i < getCount(); i++)
|
||||
{
|
||||
if (_places[i] == null)
|
||||
{
|
||||
firstNull = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (firstNull == -1)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
System.arraycopy(_places, position, _places, position + 1, firstNull - position);
|
||||
|
||||
_places[position] = ship;
|
||||
return position;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public T remove(int position) {
|
||||
if (position < 0 || position >= getCount())
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var result = _places[position];
|
||||
|
||||
_places[position] = null;
|
||||
|
||||
return (T) result;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public T get(int position) {
|
||||
if (position < 0 || position >= getCount()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return (T) _places[position];
|
||||
}
|
||||
}
|
40
SimpleMap.java
Normal file
40
SimpleMap.java
Normal file
@ -0,0 +1,40 @@
|
||||
import java.awt.*;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class SimpleMap extends AbstractMap {
|
||||
private final Color barrierColor = Color.black;
|
||||
private final Color roadColor = Color.gray;
|
||||
|
||||
@Override
|
||||
protected void drawBarrierPart(Graphics2D g, int i, int j) {
|
||||
g.setColor(barrierColor);
|
||||
g.fillRect(j * (int) _size_x, i * (int) _size_y, (int) _size_x, (int) _size_y);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void drawRoadPart(Graphics2D g, int i, int j) {
|
||||
g.setColor(roadColor);
|
||||
g.fillRect(j * (int) _size_x, i * (int) _size_y, (int) _size_x, (int) _size_y);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void generateMap() {
|
||||
_map = new int[100][100];
|
||||
_size_x = (float)_width / _map[0].length;
|
||||
_size_y = (float)_height / _map.length;
|
||||
int counter = 0;
|
||||
for (int[] row : _map) {
|
||||
Arrays.fill(row, _freeRoad);
|
||||
}
|
||||
while (counter < 50)
|
||||
{
|
||||
int x = _random.nextInt(0, 100);
|
||||
int y = _random.nextInt(0, 100);
|
||||
if (_map[y][x] == _freeRoad)
|
||||
{
|
||||
_map[y][x] = _barrier;
|
||||
counter++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
36
WaterMap.java
Normal file
36
WaterMap.java
Normal file
@ -0,0 +1,36 @@
|
||||
import java.awt.*;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class WaterMap extends AbstractMap {
|
||||
private final Color barrierColor = Color.green;
|
||||
private final Color roadColor = new Color(165, 42, 42, 255);
|
||||
|
||||
@Override
|
||||
protected void drawBarrierPart(Graphics2D g, int i, int j)
|
||||
{
|
||||
g.setColor(barrierColor);
|
||||
g.fillRect(j * (int) _size_x, i * (int) _size_y, (int) _size_x, (int) _size_y);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void drawRoadPart(Graphics2D g, int i, int j)
|
||||
{
|
||||
g.setColor(roadColor);
|
||||
g.fillRect(j * (int) _size_x, i * (int) _size_y, (int) _size_x, (int) _size_y);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void generateMap() {
|
||||
_map = new int[50][50];
|
||||
_size_x = (float)_width / _map[0].length;
|
||||
_size_y = (float)_height / _map.length;
|
||||
int counter = 0;
|
||||
for (int[] row : _map) {
|
||||
Arrays.fill(row, _freeRoad);
|
||||
}
|
||||
while (counter < 10)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user