Compare commits
9 Commits
Author | SHA1 | Date | |
---|---|---|---|
d59abaf008 | |||
ae5072c054 | |||
7f7aaaa294 | |||
028093f591 | |||
8e943c67b0 | |||
1a429fadb5 | |||
cd54300b22 | |||
af5540a048 | |||
521ae8d927 |
4
.idea/hard-repos.iml
generated
4
.idea/hard-repos.iml
generated
@ -2,7 +2,9 @@
|
|||||||
<module type="JAVA_MODULE" version="4">
|
<module type="JAVA_MODULE" version="4">
|
||||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||||
<exclude-output />
|
<exclude-output />
|
||||||
<content url="file://$MODULE_DIR$" />
|
<content url="file://$MODULE_DIR$">
|
||||||
|
<sourceFolder url="file://$MODULE_DIR$" isTestSource="false" />
|
||||||
|
</content>
|
||||||
<orderEntry type="inheritedJdk" />
|
<orderEntry type="inheritedJdk" />
|
||||||
<orderEntry type="sourceFolder" forTests="false" />
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
</component>
|
</component>
|
||||||
|
2
.idea/misc.xml
generated
2
.idea/misc.xml
generated
@ -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">
|
<component name="ProjectRootManager" version="2" project-jdk-name="openjdk-19" project-jdk-type="JavaSDK">
|
||||||
<output url="file://$PROJECT_DIR$/out" />
|
<output url="file://$PROJECT_DIR$/out" />
|
||||||
</component>
|
</component>
|
||||||
</project>
|
</project>
|
213
AbstractMap.java
Normal file
213
AbstractMap.java
Normal file
@ -0,0 +1,213 @@
|
|||||||
|
import java.awt.*;
|
||||||
|
import java.awt.image.BufferedImage;
|
||||||
|
import java.util.Random;
|
||||||
|
public abstract class AbstractMap {
|
||||||
|
private IDrawingObject _drawningObject = null;
|
||||||
|
protected int[][] _map = null;
|
||||||
|
protected int _width;
|
||||||
|
protected int _height;
|
||||||
|
protected float[] position;
|
||||||
|
protected float _size_x;
|
||||||
|
protected float _size_y;
|
||||||
|
protected Random _random = new Random();
|
||||||
|
protected int _water = 0;
|
||||||
|
protected int _barrier = 1;
|
||||||
|
public BufferedImage CreateMap(int width, int height, IDrawingObject drawningObject)
|
||||||
|
{
|
||||||
|
_width = width;
|
||||||
|
_height = height;
|
||||||
|
_drawningObject = drawningObject;
|
||||||
|
GenerateMap();
|
||||||
|
while (!SetObjectOnMap())
|
||||||
|
{
|
||||||
|
GenerateMap();
|
||||||
|
}
|
||||||
|
return DrawMapWithObject();
|
||||||
|
}
|
||||||
|
public BufferedImage MoveObject(Direction direction)
|
||||||
|
{
|
||||||
|
boolean moveAccept = true;
|
||||||
|
|
||||||
|
float[] position = _drawningObject.GetCurrentPosition();
|
||||||
|
int xObjWidth = (int)Math.ceil((position[2] - position[0]) / _size_x);
|
||||||
|
int yObjHeight = (int)Math.ceil((position[3] - position[1]) / _size_y);
|
||||||
|
int vertStep = (int)Math.ceil(_drawningObject.Step() / _size_y);
|
||||||
|
int horizStep = (int)Math.ceil(_drawningObject.Step() / _size_x);
|
||||||
|
int xObjLeftBorder = (int)Math.floor(position[0] / _size_x);
|
||||||
|
int xObjRightBorder = (int)Math.ceil(position[2] / _size_x);
|
||||||
|
int yObjTopBorder = (int)Math.floor(position[1] / _size_y);
|
||||||
|
int yObjBottomBorder = (int)Math.ceil(position[3] / _size_y);
|
||||||
|
|
||||||
|
switch (direction)
|
||||||
|
{
|
||||||
|
case Up:
|
||||||
|
for (int i = 0; i < vertStep; i++)
|
||||||
|
{
|
||||||
|
if (!moveAccept)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
for (int j = 0; j < xObjWidth; j++)
|
||||||
|
{
|
||||||
|
if (yObjTopBorder - i < 0 || xObjLeftBorder + j >= _map[0].length)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (_map[xObjLeftBorder + j][yObjTopBorder - i] == _barrier)
|
||||||
|
{
|
||||||
|
moveAccept = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case Down:
|
||||||
|
for (int i = 0; i < vertStep; i++)
|
||||||
|
{
|
||||||
|
if (!moveAccept)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
for (int j = 0; j < xObjWidth; j++)
|
||||||
|
{
|
||||||
|
if (yObjBottomBorder + i >= _map.length || xObjLeftBorder + j >= _map[0].length)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (_map[xObjLeftBorder + j][yObjBottomBorder + i] == _barrier)
|
||||||
|
{
|
||||||
|
moveAccept = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case Left:
|
||||||
|
for (int i = 0; i < yObjHeight; i++)
|
||||||
|
{
|
||||||
|
if (!moveAccept)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
for (int j = 0; j < horizStep; j++)
|
||||||
|
{
|
||||||
|
if (yObjTopBorder + i >= _map.length || xObjLeftBorder - j < 0)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (_map[xObjLeftBorder - j][yObjTopBorder + i] == _barrier)
|
||||||
|
{
|
||||||
|
moveAccept = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case Right:
|
||||||
|
for (int i = 0; i < yObjHeight; i++)
|
||||||
|
{
|
||||||
|
if (!moveAccept)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
for (int j = 0; j < horizStep; j++)
|
||||||
|
{
|
||||||
|
if (yObjTopBorder + i >= _map.length || xObjRightBorder + j >= _map[0].length)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (_map[xObjRightBorder + j][yObjTopBorder + i] == _barrier)
|
||||||
|
{
|
||||||
|
moveAccept = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (moveAccept)
|
||||||
|
{
|
||||||
|
_drawningObject.MoveObject(direction);
|
||||||
|
}
|
||||||
|
return DrawMapWithObject();
|
||||||
|
}
|
||||||
|
private boolean SetObjectOnMap()
|
||||||
|
{
|
||||||
|
if (_drawningObject == null || _map == null)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
int x = _random.nextInt(0, 10);
|
||||||
|
int y = _random.nextInt(0, 10);
|
||||||
|
|
||||||
|
float[] position = _drawningObject.GetCurrentPosition();
|
||||||
|
int xObjWidth = (int)Math.ceil((position[2] - position[0]) / _size_x);
|
||||||
|
int yObjHeight = (int)Math.ceil((position[3] - position[1]) / _size_y);
|
||||||
|
int xObjLeftBorder = (int)Math.floor(position[0] / _size_x);
|
||||||
|
int yObjTopBorder = (int)Math.floor(position[1] / _size_y);
|
||||||
|
|
||||||
|
while (y < _height - (position[3] - position[1]))
|
||||||
|
{
|
||||||
|
while (x < _width - (position[2] - position[0]))
|
||||||
|
{
|
||||||
|
if (CheckSpawnArea(xObjWidth, yObjHeight, xObjLeftBorder, yObjTopBorder))
|
||||||
|
{
|
||||||
|
_drawningObject.SetObject(x, y, _width, _height);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
x += (int)_size_x;
|
||||||
|
xObjLeftBorder = (int)(x / _size_x);
|
||||||
|
}
|
||||||
|
x = 0;
|
||||||
|
y += (int)_size_y;
|
||||||
|
yObjTopBorder = (int)(y / _size_y);
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
private boolean CheckSpawnArea(int xObjWidth, int yObjHeight, int xObjLeftBorder, int yObjTopBorder)
|
||||||
|
{
|
||||||
|
for (int i = 0; i <= yObjHeight; i++)
|
||||||
|
{
|
||||||
|
for (int j = 0; j <= xObjWidth; j++)
|
||||||
|
{
|
||||||
|
if (yObjTopBorder + i >= _map.length || xObjLeftBorder + j >= _map[0].length || _map[xObjLeftBorder + j][yObjTopBorder + i] == _barrier)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
private BufferedImage DrawMapWithObject()
|
||||||
|
{
|
||||||
|
BufferedImage bmp = new BufferedImage(_width, _height, BufferedImage.TYPE_INT_RGB);
|
||||||
|
if (_drawningObject == null || _map == null)
|
||||||
|
{
|
||||||
|
return bmp;
|
||||||
|
}
|
||||||
|
Graphics gr = bmp.getGraphics();
|
||||||
|
for (int i = 0; i < _map.length; ++i)
|
||||||
|
{
|
||||||
|
for (int j = 0; j < _map[i].length; ++j)
|
||||||
|
{
|
||||||
|
if (_map[i][j] == _water)
|
||||||
|
{
|
||||||
|
DrawRoadPart(gr, i, j);
|
||||||
|
}
|
||||||
|
else if (_map[i][j] == _barrier)
|
||||||
|
{
|
||||||
|
DrawBarrierPart(gr, i, j);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_drawningObject.DrawingObject(gr);
|
||||||
|
return bmp;
|
||||||
|
}
|
||||||
|
protected abstract void GenerateMap();
|
||||||
|
protected abstract void DrawRoadPart(Graphics g, int i, int j);
|
||||||
|
protected abstract void DrawBarrierPart(Graphics g, int i, int j);
|
||||||
|
}
|
24
AdditionalEnum.java
Normal file
24
AdditionalEnum.java
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
public enum AdditionalEnum {
|
||||||
|
One,
|
||||||
|
Two,
|
||||||
|
Three;
|
||||||
|
|
||||||
|
public static AdditionalEnum FromInteger(int intValue)
|
||||||
|
{
|
||||||
|
switch(intValue)
|
||||||
|
{
|
||||||
|
case 1:
|
||||||
|
return One;
|
||||||
|
|
||||||
|
case 2:
|
||||||
|
return Two;
|
||||||
|
|
||||||
|
case 3:
|
||||||
|
return Three;
|
||||||
|
|
||||||
|
default:
|
||||||
|
System.out.println("Error: incorrect value for enum");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
24
Direction.java
Normal file
24
Direction.java
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
public enum Direction {
|
||||||
|
Up,
|
||||||
|
Down,
|
||||||
|
Left,
|
||||||
|
Right;
|
||||||
|
|
||||||
|
public static Direction FromInteger(int intValue)
|
||||||
|
{
|
||||||
|
switch(intValue)
|
||||||
|
{
|
||||||
|
case 1:
|
||||||
|
return Up;
|
||||||
|
case 2:
|
||||||
|
return Down;
|
||||||
|
case 3:
|
||||||
|
return Left;
|
||||||
|
case 4:
|
||||||
|
return Right;
|
||||||
|
default:
|
||||||
|
System.out.println("Error: incorrect value for enum");
|
||||||
|
return Up;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
76
DrawingContainerShip.java
Normal file
76
DrawingContainerShip.java
Normal file
@ -0,0 +1,76 @@
|
|||||||
|
import java.awt.*;
|
||||||
|
|
||||||
|
public class DrawingContainerShip extends DrawingShip
|
||||||
|
{
|
||||||
|
public DrawingContainerShip(int speed, float weight, Color bodyColor, Color dopColor, boolean crane, boolean containers)
|
||||||
|
{
|
||||||
|
super(speed,weight,bodyColor,130,45);
|
||||||
|
Ship = new EntityContainerShip(speed, weight, bodyColor, dopColor, crane, containers);
|
||||||
|
}
|
||||||
|
protected DrawingContainerShip(EntityShip ship,IAdditionalDrawingObject deck)
|
||||||
|
{
|
||||||
|
super(ship,deck);
|
||||||
|
Ship=ship;
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public void DrawTransport(Graphics g) {
|
||||||
|
if (!(GetShip() instanceof EntityContainerShip containerShip))
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
super.DrawTransport(g);
|
||||||
|
Graphics2D g2d = (Graphics2D) g;
|
||||||
|
Deck.DrawDeck(Ship.GetBodyColor(), g, _startPosX, _startPosY);
|
||||||
|
if (containerShip.GetContainers())
|
||||||
|
{
|
||||||
|
g2d.setPaint(containerShip.GetDopColor());
|
||||||
|
//Заливка контейнеров
|
||||||
|
int[] xValuesFirstContainer = {(int) _startPosX + 16, (int) _startPosX + 16 + 29, (int) _startPosX + 16 + 29, (int) _startPosX + 16};
|
||||||
|
int[] yValuesFirstContainer = {(int) _startPosY + 16, (int) _startPosY + 16, (int) _startPosY + 16 + 14, (int) _startPosY + 16 + 14};
|
||||||
|
g2d.fillPolygon(xValuesFirstContainer, yValuesFirstContainer, 4);
|
||||||
|
int[] xValuesSecondContainer = {(int) _startPosX + 56, (int) _startPosX + 56 + 29, (int) _startPosX + 56 + 29, (int) _startPosX + 56};
|
||||||
|
int[] yValuesSecondContainer = {(int) _startPosY + 16, (int) _startPosY + 16, (int) _startPosY + 16 + 14, (int) _startPosY + 16 + 14};
|
||||||
|
g2d.fillPolygon(xValuesSecondContainer, yValuesSecondContainer, 4);
|
||||||
|
//Заливка центральных полос на контейнерах
|
||||||
|
g2d.setPaint(Color.ORANGE);
|
||||||
|
int[] xValuesFirstContainerLine = {(int) _startPosX + 16, (int) _startPosX + 16 + 29, (int) _startPosX + 16 + 29, (int) _startPosX + 16};
|
||||||
|
int[] yValuesFirstContainerLine = {(int) _startPosY + 20, (int) _startPosY + 20, (int) _startPosY + 25, (int) _startPosY + 25};
|
||||||
|
g2d.fillPolygon(xValuesFirstContainerLine, yValuesFirstContainerLine, 4);
|
||||||
|
|
||||||
|
int[] xValuesSecondContainerLine = {(int) _startPosX + 56, (int) _startPosX + 56 + 29, (int) _startPosX + 56 + 29, (int) _startPosX + 56};
|
||||||
|
int[] yValuesSecondContainerLine = {(int) _startPosY + 20, (int) _startPosY + 20, (int) _startPosY + 25, (int) _startPosY + 25};
|
||||||
|
g2d.fillPolygon(xValuesSecondContainerLine, yValuesSecondContainerLine, 4);
|
||||||
|
//Границы контейнеров
|
||||||
|
g2d.setPaint(Color.BLACK);
|
||||||
|
g2d.drawPolygon(xValuesFirstContainer, yValuesFirstContainer, 4);
|
||||||
|
g2d.drawPolygon(xValuesSecondContainer, yValuesSecondContainer, 4);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (containerShip.GetCrane())
|
||||||
|
{
|
||||||
|
int[] xValuesBorderTown = {(int) _startPosX + 45, (int) _startPosX + 55, (int) _startPosX + 55, (int) _startPosX + 45};
|
||||||
|
int[] yValuesBorderTown = {(int) _startPosY, (int) _startPosY, (int) _startPosY + 30, (int) _startPosY + 30};
|
||||||
|
g.drawPolygon(xValuesBorderTown, yValuesBorderTown, 4);
|
||||||
|
g.fillPolygon(xValuesBorderTown, yValuesBorderTown, 4);
|
||||||
|
//Граница заливки стрелы крана
|
||||||
|
int[] xValuesBorderCrane = {(int) _startPosX + 50, (int) _startPosX + 90, (int) _startPosX + 50};
|
||||||
|
int[] yValuesBorderCrane = {(int) _startPosY + 10, (int) _startPosY + 13, (int) _startPosY + 16};
|
||||||
|
g2d.fillPolygon(xValuesBorderCrane,yValuesBorderCrane,3);
|
||||||
|
|
||||||
|
//Трос и крепление
|
||||||
|
g2d.setColor(Color.BLACK);
|
||||||
|
g2d.drawLine((int)_startPosX + 90, (int)_startPosY + 14, (int)_startPosX + 90, (int)_startPosY + 40);
|
||||||
|
g2d.drawLine((int)_startPosX + 90, (int)_startPosY + 40, (int)_startPosX + 85, (int)_startPosY + 43);
|
||||||
|
g2d.drawLine((int)_startPosX + 90, (int)_startPosY + 40, (int)_startPosX + 90, (int)_startPosY + 45);
|
||||||
|
g2d.drawLine((int)_startPosX + 90, (int)_startPosY + 40, (int)_startPosX + 95, (int)_startPosY + 43);
|
||||||
|
//Граница стрелы крана
|
||||||
|
g2d.drawPolygon(xValuesBorderCrane, yValuesBorderCrane, 3);
|
||||||
|
}
|
||||||
|
g2d.setPaint(Ship.GetBodyColor());
|
||||||
|
int xValues[]={(int) _startPosX,(int) _startPosX + 100,(int) _startPosX + 80,(int) _startPosX + 20};
|
||||||
|
int yValues[]={(int) _startPosY + 30,(int) _startPosY + 30,(int) _startPosY + 60,(int) _startPosY + 60};
|
||||||
|
g2d.fillPolygon(xValues,yValues,4);
|
||||||
|
g2d.setPaint(Color.BLACK);
|
||||||
|
g2d.drawPolygon(xValues,yValues,4);
|
||||||
|
}
|
||||||
|
}
|
34
DrawingDeck.java
Normal file
34
DrawingDeck.java
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
import javax.swing.*;
|
||||||
|
import java.awt.*;
|
||||||
|
public class DrawingDeck extends JComponent implements IAdditionalDrawingObject{
|
||||||
|
private AdditionalEnum _decksEnum;
|
||||||
|
@Override
|
||||||
|
public void SetAddEnum(int decksAmount) {
|
||||||
|
_decksEnum = AdditionalEnum.FromInteger(decksAmount);
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public void DrawDeck(Color colorDeck, Graphics g,float _startPosX,float _startPosY)
|
||||||
|
{
|
||||||
|
super.paintComponent(g);
|
||||||
|
Graphics2D g2d = (Graphics2D) g;
|
||||||
|
int numOfDecks = 0;
|
||||||
|
switch (_decksEnum)
|
||||||
|
{
|
||||||
|
case One:
|
||||||
|
numOfDecks = 1;
|
||||||
|
break;
|
||||||
|
case Two:
|
||||||
|
numOfDecks = 2;
|
||||||
|
break;
|
||||||
|
case Three:
|
||||||
|
numOfDecks = 3;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
for(int i = 0; i < numOfDecks; ++i){
|
||||||
|
g2d.setPaint(colorDeck);
|
||||||
|
g2d.fillRect((int)_startPosX+30 + 5*i, (int)_startPosY+25 - 5*i, 30*2 - 5*i, 5);
|
||||||
|
g2d.setPaint(Color.BLACK);
|
||||||
|
g2d.drawRect((int)_startPosX+30 + 5*i, (int)_startPosY+25 - 5*i, 30*2 - 5*i, 5);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
57
DrawingEntities.java
Normal file
57
DrawingEntities.java
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
public class DrawingEntities <T extends EntityShip,U extends IAdditionalDrawingObject>{
|
||||||
|
public T[] _entities;
|
||||||
|
public U[] _decks;
|
||||||
|
int entitiesCount = 0;
|
||||||
|
int decksCount = 0;
|
||||||
|
String indx;
|
||||||
|
String indy;
|
||||||
|
public DrawingEntities(int countE,int countD)
|
||||||
|
{
|
||||||
|
_entities = (T[]) new EntityShip[countE];
|
||||||
|
_decks = (U[]) new IAdditionalDrawingObject[countD];
|
||||||
|
}
|
||||||
|
public int Insert(T ship)
|
||||||
|
{
|
||||||
|
if(entitiesCount<_entities.length){
|
||||||
|
_entities[entitiesCount] = ship;
|
||||||
|
entitiesCount++;
|
||||||
|
return entitiesCount-1;
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
public int Insert(U deck)
|
||||||
|
{
|
||||||
|
if(decksCount<_decks.length){
|
||||||
|
_decks[decksCount] = deck;
|
||||||
|
decksCount++;
|
||||||
|
return decksCount-1;
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
public void SetIndexs(int ind1, int ind2)
|
||||||
|
{
|
||||||
|
indx=Integer.toString(ind1);
|
||||||
|
indy=Integer.toString(ind2);
|
||||||
|
}
|
||||||
|
public DrawingShip CreateShip()
|
||||||
|
{
|
||||||
|
Random rnd = new Random();
|
||||||
|
int indEnt=0;
|
||||||
|
int indDeck=0;
|
||||||
|
if(entitiesCount-1!=0 & decksCount-1!=0)
|
||||||
|
{
|
||||||
|
indDeck=rnd.nextInt(0,decksCount-1);
|
||||||
|
indEnt=rnd.nextInt(0,entitiesCount-1);
|
||||||
|
}
|
||||||
|
T ship=(T)_entities[indEnt];
|
||||||
|
U deck=(U)_decks[indDeck];
|
||||||
|
SetIndexs(indEnt,indDeck);
|
||||||
|
if(ship instanceof EntityContainerShip)
|
||||||
|
{
|
||||||
|
return new DrawingContainerShip(ship,deck);
|
||||||
|
}
|
||||||
|
return new DrawingShip(ship,deck);
|
||||||
|
}
|
||||||
|
}
|
35
DrawingObjectShip.java
Normal file
35
DrawingObjectShip.java
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
import java.awt.*;
|
||||||
|
|
||||||
|
public class DrawingObjectShip implements IDrawingObject {
|
||||||
|
private DrawingShip _ship = null;
|
||||||
|
public DrawingObjectShip(DrawingShip ship)
|
||||||
|
{
|
||||||
|
_ship=ship;
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public float Step() {
|
||||||
|
if (_ship!=null && _ship.Ship != null) {
|
||||||
|
return _ship.Ship.GetStep();
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public void SetObject(int x, int y, int width, int height) {
|
||||||
|
_ship.SetPosition(x,y,width,height);
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public void MoveObject(Direction direction) {
|
||||||
|
_ship.MoveTransport(direction);
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public void DrawingObject(Graphics g) {
|
||||||
|
_ship.DrawTransport(g);
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public float[] GetCurrentPosition() {
|
||||||
|
if(_ship!=null)
|
||||||
|
return _ship.GetCurrentPosition();
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
39
DrawingOvalDeck.java
Normal file
39
DrawingOvalDeck.java
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
import javax.swing.*;
|
||||||
|
import java.awt.*;
|
||||||
|
|
||||||
|
public class DrawingOvalDeck extends JComponent implements IAdditionalDrawingObject{
|
||||||
|
private AdditionalEnum _decksEnum;
|
||||||
|
@Override
|
||||||
|
public void SetAddEnum(int decksAmount) {
|
||||||
|
_decksEnum = AdditionalEnum.FromInteger(decksAmount);
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public void DrawDeck(Color colorDeck, Graphics g, float _startPosX, float _startPosY)
|
||||||
|
{
|
||||||
|
if (_decksEnum == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
super.paintComponent(g);
|
||||||
|
Graphics2D g2d = (Graphics2D) g;
|
||||||
|
int numOfDecks = 0;
|
||||||
|
switch (_decksEnum)
|
||||||
|
{
|
||||||
|
case One:
|
||||||
|
numOfDecks = 1;
|
||||||
|
break;
|
||||||
|
case Two:
|
||||||
|
numOfDecks = 2;
|
||||||
|
break;
|
||||||
|
case Three:
|
||||||
|
numOfDecks = 3;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
for(int i = 0; i < numOfDecks; ++i){
|
||||||
|
g2d.setPaint(colorDeck);
|
||||||
|
g2d.fillOval((int)_startPosX+30 + 5*i, (int)_startPosY+25 - 5*i, 30*2 - 5*i, 5);
|
||||||
|
g2d.setPaint(Color.BLACK);
|
||||||
|
g2d.drawOval((int)_startPosX+30 + 5*i, (int)_startPosY+25 - 5*i, 30*2 - 5*i, 5);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
141
DrawingShip.java
Normal file
141
DrawingShip.java
Normal file
@ -0,0 +1,141 @@
|
|||||||
|
import javax.swing.*;
|
||||||
|
import java.awt.*;
|
||||||
|
import java.util.Random;
|
||||||
|
public class DrawingShip extends JPanel {
|
||||||
|
protected EntityShip Ship;
|
||||||
|
public IAdditionalDrawingObject Deck;
|
||||||
|
public void SetEnum() {
|
||||||
|
Random r = new Random();
|
||||||
|
int numbEnum = r.nextInt(1, 4);
|
||||||
|
Deck.SetAddEnum(numbEnum);
|
||||||
|
}
|
||||||
|
public void SetFormEnum()
|
||||||
|
{
|
||||||
|
Random r = new Random();
|
||||||
|
int numbEnum = r.nextInt(1, 4);
|
||||||
|
if (numbEnum == 1) {
|
||||||
|
Deck = new DrawingDeck();
|
||||||
|
}
|
||||||
|
if (numbEnum == 2) {
|
||||||
|
Deck = new DrawingOvalDeck();
|
||||||
|
}
|
||||||
|
if (numbEnum == 3) {
|
||||||
|
Deck=new DrawingTrapezDeck();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public EntityShip GetShip() {
|
||||||
|
return Ship;
|
||||||
|
}
|
||||||
|
protected float _startPosX;
|
||||||
|
protected float _startPosY;
|
||||||
|
private Integer _pictureWidth = null;
|
||||||
|
private Integer _pictureHeight = null;
|
||||||
|
private int _shipWidth = 100;
|
||||||
|
private int _shipHeight = 60;
|
||||||
|
public DrawingShip(int speed, float weight, Color bodycolor) {
|
||||||
|
Ship = new EntityShip(speed, weight, bodycolor);
|
||||||
|
SetFormEnum();
|
||||||
|
SetEnum();
|
||||||
|
}
|
||||||
|
public DrawingShip(int speed,float weight,Color bodyColor,int shipWidth,int shipHeight)
|
||||||
|
{
|
||||||
|
this(speed,weight,bodyColor);
|
||||||
|
_shipWidth = shipWidth;
|
||||||
|
_shipHeight = shipHeight;
|
||||||
|
}
|
||||||
|
protected DrawingShip(EntityShip ship,IAdditionalDrawingObject deck)
|
||||||
|
{
|
||||||
|
Ship=ship;
|
||||||
|
Deck=deck;
|
||||||
|
}
|
||||||
|
public void SetPosition(int x, int y, int width, int height) {
|
||||||
|
if (x < 0 || y < 0)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (x + _shipWidth > width || 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(Graphics g) {
|
||||||
|
if (_startPosX < 0 || _startPosY < 0 || _pictureWidth == null || _pictureHeight == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (GetShip() == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Graphics2D g2d = (Graphics2D) g;
|
||||||
|
g2d.setPaint(Ship.GetBodyColor());
|
||||||
|
int xValues[]={(int) _startPosX,(int) _startPosX + 100,(int) _startPosX + 80,(int) _startPosX + 20};
|
||||||
|
int yValues[]={(int) _startPosY + 30,(int) _startPosY + 30,(int) _startPosY + 60,(int) _startPosY + 60};
|
||||||
|
g2d.fillPolygon(xValues,yValues,4);
|
||||||
|
g2d.setPaint(Color.BLACK);
|
||||||
|
g2d.drawPolygon(xValues,yValues,4);
|
||||||
|
Deck.DrawDeck(Ship.GetBodyColor(), g, _startPosX, _startPosY);
|
||||||
|
}
|
||||||
|
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()
|
||||||
|
{
|
||||||
|
float[] position = new float[4];
|
||||||
|
position[0] = _startPosX;
|
||||||
|
position[1] = _startPosY;
|
||||||
|
position[2] = _startPosX + _shipWidth;
|
||||||
|
position[3] = _startPosY + _shipHeight;
|
||||||
|
return position;
|
||||||
|
}
|
||||||
|
}
|
41
DrawingTrapezDeck.java
Normal file
41
DrawingTrapezDeck.java
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
import javax.swing.*;
|
||||||
|
import java.awt.*;
|
||||||
|
public class DrawingTrapezDeck extends JComponent implements IAdditionalDrawingObject{
|
||||||
|
private AdditionalEnum _decksEnum;
|
||||||
|
@Override
|
||||||
|
public void SetAddEnum(int decksAmount) {
|
||||||
|
_decksEnum = AdditionalEnum.FromInteger(decksAmount);
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public void DrawDeck(Color colorDeck, Graphics g,float _startPosX,float _startPosY)
|
||||||
|
{
|
||||||
|
if (_decksEnum == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
super.paintComponent(g);
|
||||||
|
Graphics2D g2d = (Graphics2D) g;
|
||||||
|
int numOfDecks = 0;
|
||||||
|
switch (_decksEnum)
|
||||||
|
{
|
||||||
|
case One:
|
||||||
|
numOfDecks = 1;
|
||||||
|
break;
|
||||||
|
case Two:
|
||||||
|
numOfDecks = 2;
|
||||||
|
break;
|
||||||
|
case Three:
|
||||||
|
numOfDecks = 3;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
for(int i = 0; i < numOfDecks; ++i){
|
||||||
|
g2d.setPaint(colorDeck);
|
||||||
|
int[] xValues={(int)_startPosX+40 + 5*i,(int)_startPosX+40+15 + 5*i,(int)_startPosX+40+45,(int)_startPosX+40+60};
|
||||||
|
int[] yValues={(int)_startPosY+25 -5*i,(int)_startPosY+30 - 5*i,(int)_startPosY+30 - 5*i,(int)_startPosY+25 - 5*i};
|
||||||
|
g2d.fillPolygon(xValues,yValues,4);
|
||||||
|
g2d.setPaint(Color.BLACK);
|
||||||
|
g2d.drawPolygon(xValues,yValues,4);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
30
EntityContainerShip.java
Normal file
30
EntityContainerShip.java
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
import java.awt.*;
|
||||||
|
|
||||||
|
public class EntityContainerShip extends EntityShip {
|
||||||
|
private Color _DopColor;
|
||||||
|
private boolean _Crane;
|
||||||
|
private boolean _Containers;
|
||||||
|
|
||||||
|
public Color GetDopColor()
|
||||||
|
{
|
||||||
|
return _DopColor;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean GetCrane()
|
||||||
|
{
|
||||||
|
return _Crane;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean GetContainers()
|
||||||
|
{
|
||||||
|
return _Containers;
|
||||||
|
}
|
||||||
|
public EntityContainerShip(int speed, float weight, Color bodyColor, Color dopColor, boolean crane, boolean containers)
|
||||||
|
{
|
||||||
|
super(speed,weight,bodyColor);
|
||||||
|
_DopColor = dopColor;
|
||||||
|
_Crane = crane;
|
||||||
|
_Containers = containers;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
28
EntityShip.java
Normal file
28
EntityShip.java
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
import java.awt.*;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
public class EntityShip {
|
||||||
|
private int Speed;
|
||||||
|
public int GetSpeed() {
|
||||||
|
return Speed;
|
||||||
|
}
|
||||||
|
private float Weight;
|
||||||
|
public float GetWeight() {
|
||||||
|
return Weight;
|
||||||
|
}
|
||||||
|
private Color BodyColor;
|
||||||
|
public Color GetBodyColor() {
|
||||||
|
return BodyColor;
|
||||||
|
}
|
||||||
|
public float GetStep()
|
||||||
|
{
|
||||||
|
return Speed*100/Weight;
|
||||||
|
}
|
||||||
|
public EntityShip(int speed,float weight, Color bodyColor)
|
||||||
|
{
|
||||||
|
Random random = new Random();
|
||||||
|
Speed = speed <= 0 ? random.nextInt(50, 150) : speed;
|
||||||
|
Weight = weight <= 0 ? random.nextInt(50, 150) : weight;
|
||||||
|
BodyColor=bodyColor;
|
||||||
|
}
|
||||||
|
}
|
136
FormMapWithSetShipsGeneric.form
Normal file
136
FormMapWithSetShipsGeneric.form
Normal file
@ -0,0 +1,136 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="FormMapWithSetShipsGeneric">
|
||||||
|
<grid id="27dc6" binding="Mainpanel" layout-manager="GridLayoutManager" row-count="2" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
|
||||||
|
<margin top="0" left="0" bottom="0" right="0"/>
|
||||||
|
<constraints>
|
||||||
|
<xy x="20" y="20" width="649" height="400"/>
|
||||||
|
</constraints>
|
||||||
|
<properties/>
|
||||||
|
<border type="none"/>
|
||||||
|
<children>
|
||||||
|
<grid id="9bb5a" binding="pictureBoxShip" layout-manager="BorderLayout" hgap="0" vgap="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>
|
||||||
|
<hspacer id="b305e">
|
||||||
|
<constraints>
|
||||||
|
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||||
|
</constraints>
|
||||||
|
</hspacer>
|
||||||
|
<grid id="4fa91" binding="GroupBoxTools" layout-manager="GridLayoutManager" row-count="8" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
|
||||||
|
<margin top="0" left="0" bottom="0" right="0"/>
|
||||||
|
<constraints>
|
||||||
|
<grid row="0" column="1" row-span="2" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
|
||||||
|
</constraints>
|
||||||
|
<properties/>
|
||||||
|
<border type="none"/>
|
||||||
|
<children>
|
||||||
|
<component id="95ba3" class="javax.swing.JComboBox" binding="ComboBoxSelectorMap">
|
||||||
|
<constraints>
|
||||||
|
<grid row="0" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
|
||||||
|
</constraints>
|
||||||
|
<properties>
|
||||||
|
<model>
|
||||||
|
<item value="Простая карта"/>
|
||||||
|
<item value="Карта острова"/>
|
||||||
|
<item value="Карта скалы"/>
|
||||||
|
</model>
|
||||||
|
</properties>
|
||||||
|
</component>
|
||||||
|
<component id="cdb80" class="javax.swing.JButton" binding="ButtonAddShip">
|
||||||
|
<constraints>
|
||||||
|
<grid row="1" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||||
|
</constraints>
|
||||||
|
<properties>
|
||||||
|
<text value="Добавить корабль"/>
|
||||||
|
</properties>
|
||||||
|
</component>
|
||||||
|
<component id="e5494" class="javax.swing.JTextField" binding="maskedTextBoxPosition">
|
||||||
|
<constraints>
|
||||||
|
<grid row="2" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
|
||||||
|
<preferred-size width="150" height="-1"/>
|
||||||
|
</grid>
|
||||||
|
</constraints>
|
||||||
|
<properties/>
|
||||||
|
</component>
|
||||||
|
<component id="8bb89" class="javax.swing.JButton" binding="ButtonDeleteShip">
|
||||||
|
<constraints>
|
||||||
|
<grid row="3" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||||
|
</constraints>
|
||||||
|
<properties>
|
||||||
|
<text value="Удалить корабль"/>
|
||||||
|
</properties>
|
||||||
|
</component>
|
||||||
|
<component id="aa482" class="javax.swing.JButton" binding="ButtonShowStorage">
|
||||||
|
<constraints>
|
||||||
|
<grid row="4" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||||
|
</constraints>
|
||||||
|
<properties>
|
||||||
|
<text value="Показать хранилище"/>
|
||||||
|
</properties>
|
||||||
|
</component>
|
||||||
|
<component id="9ebdc" class="javax.swing.JButton" binding="ButtonShowMap">
|
||||||
|
<constraints>
|
||||||
|
<grid row="5" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||||
|
</constraints>
|
||||||
|
<properties>
|
||||||
|
<text value="Показать карту"/>
|
||||||
|
</properties>
|
||||||
|
</component>
|
||||||
|
<component id="acbc" class="javax.swing.JButton" binding="ButtonUp">
|
||||||
|
<constraints>
|
||||||
|
<grid row="6" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="3" indent="0" use-parent-layout="false">
|
||||||
|
<minimum-size width="30" height="30"/>
|
||||||
|
<preferred-size width="30" height="30"/>
|
||||||
|
<maximum-size width="30" height="30"/>
|
||||||
|
</grid>
|
||||||
|
</constraints>
|
||||||
|
<properties>
|
||||||
|
<text value=""/>
|
||||||
|
</properties>
|
||||||
|
</component>
|
||||||
|
<component id="a584e" class="javax.swing.JButton" binding="ButtonDown">
|
||||||
|
<constraints>
|
||||||
|
<grid row="7" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="3" indent="0" use-parent-layout="false">
|
||||||
|
<minimum-size width="30" height="30"/>
|
||||||
|
<preferred-size width="30" height="30"/>
|
||||||
|
<maximum-size width="30" height="30"/>
|
||||||
|
</grid>
|
||||||
|
</constraints>
|
||||||
|
<properties>
|
||||||
|
<text value=""/>
|
||||||
|
</properties>
|
||||||
|
</component>
|
||||||
|
<component id="e9fe6" class="javax.swing.JButton" binding="ButtonLeft">
|
||||||
|
<constraints>
|
||||||
|
<grid row="7" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="3" indent="0" use-parent-layout="false">
|
||||||
|
<minimum-size width="30" height="30"/>
|
||||||
|
<preferred-size width="30" height="30"/>
|
||||||
|
<maximum-size width="30" height="30"/>
|
||||||
|
</grid>
|
||||||
|
</constraints>
|
||||||
|
<properties>
|
||||||
|
<text value=""/>
|
||||||
|
</properties>
|
||||||
|
</component>
|
||||||
|
<component id="25a22" class="javax.swing.JButton" binding="ButtonRight">
|
||||||
|
<constraints>
|
||||||
|
<grid row="7" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="3" indent="0" use-parent-layout="false">
|
||||||
|
<minimum-size width="30" height="30"/>
|
||||||
|
<preferred-size width="30" height="30"/>
|
||||||
|
<maximum-size width="30" height="30"/>
|
||||||
|
</grid>
|
||||||
|
</constraints>
|
||||||
|
<properties>
|
||||||
|
<text value=""/>
|
||||||
|
</properties>
|
||||||
|
</component>
|
||||||
|
</children>
|
||||||
|
</grid>
|
||||||
|
</children>
|
||||||
|
</grid>
|
||||||
|
</form>
|
226
FormMapWithSetShipsGeneric.java
Normal file
226
FormMapWithSetShipsGeneric.java
Normal file
@ -0,0 +1,226 @@
|
|||||||
|
import javax.imageio.ImageIO;
|
||||||
|
import javax.swing.*;
|
||||||
|
import java.awt.*;
|
||||||
|
import java.awt.event.ActionEvent;
|
||||||
|
import java.awt.event.ActionListener;
|
||||||
|
import java.awt.event.KeyAdapter;
|
||||||
|
import java.awt.event.KeyEvent;
|
||||||
|
import java.awt.image.BufferedImage;
|
||||||
|
|
||||||
|
public class FormMapWithSetShipsGeneric extends JFrame{
|
||||||
|
public JPanel Mainpanel;
|
||||||
|
private JPanel pictureBoxShip;
|
||||||
|
private JPanel GroupBoxTools;
|
||||||
|
private JComboBox ComboBoxSelectorMap;
|
||||||
|
private JButton ButtonAddShip;
|
||||||
|
private JButton ButtonDeleteShip;
|
||||||
|
private JButton ButtonShowStorage;
|
||||||
|
private JButton ButtonShowMap;
|
||||||
|
private JButton ButtonLeft;
|
||||||
|
private JButton ButtonDown;
|
||||||
|
private JButton ButtonUp;
|
||||||
|
private JButton ButtonRight;
|
||||||
|
private JTextField maskedTextBoxPosition;
|
||||||
|
private MapWithSetShipsGeneric<DrawingObjectShip,AbstractMap> _mapShipsCollectionGeneric;
|
||||||
|
public void UpdateWindow(BufferedImage bmp)
|
||||||
|
{
|
||||||
|
pictureBoxShip.removeAll();
|
||||||
|
JLabel imageWithMapAndObject = new JLabel();
|
||||||
|
imageWithMapAndObject.setPreferredSize(pictureBoxShip.getSize());
|
||||||
|
imageWithMapAndObject.setMinimumSize(new Dimension(1, 1));
|
||||||
|
imageWithMapAndObject.setIcon(new ImageIcon(bmp));
|
||||||
|
pictureBoxShip.add(imageWithMapAndObject, BorderLayout.CENTER);
|
||||||
|
pictureBoxShip.revalidate();
|
||||||
|
}
|
||||||
|
public FormMapWithSetShipsGeneric()
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
Image img = ImageIO.read(FormShip.class.getResource("Images/4.png"));
|
||||||
|
ButtonUp.setIcon(new ImageIcon(img));
|
||||||
|
img = ImageIO.read(FormShip.class.getResource("Images/2.png"));
|
||||||
|
ButtonDown.setIcon(new ImageIcon(img));
|
||||||
|
img = ImageIO.read(FormShip.class.getResource("Images/1.png"));
|
||||||
|
ButtonLeft.setIcon(new ImageIcon(img));
|
||||||
|
img = ImageIO.read(FormShip.class.getResource("Images/3.png"));
|
||||||
|
ButtonRight.setIcon(new ImageIcon(img));
|
||||||
|
} catch (Exception ex) {
|
||||||
|
System.out.println(ex);
|
||||||
|
}
|
||||||
|
|
||||||
|
ComboBoxSelectorMap.addActionListener(new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
AbstractMap map = null;
|
||||||
|
ComboBoxSelectorMap = (JComboBox)e.getSource();
|
||||||
|
String item = (String)ComboBoxSelectorMap.getSelectedItem();
|
||||||
|
switch (item) {
|
||||||
|
case "Простая карта": {
|
||||||
|
map = new SimpleMap();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case "Карта острова": {
|
||||||
|
map = new IslandsMap();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case "Карта скалы": {
|
||||||
|
map = new RocksMap();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(map!=null)
|
||||||
|
{
|
||||||
|
_mapShipsCollectionGeneric = new MapWithSetShipsGeneric<DrawingObjectShip,AbstractMap>(pictureBoxShip.getWidth(),pictureBoxShip.getHeight(),map);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
_mapShipsCollectionGeneric=null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
ButtonAddShip.addActionListener(new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
if(_mapShipsCollectionGeneric==null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
FormShip form = new FormShip();
|
||||||
|
form.setSize(1000,700);
|
||||||
|
form.setVisible(true);
|
||||||
|
DrawingObjectShip ship = new DrawingObjectShip(form.GetSelectedShip());
|
||||||
|
if(_mapShipsCollectionGeneric.Add(ship)!=-1)
|
||||||
|
{
|
||||||
|
JOptionPane.showMessageDialog(null,"Объект добавлен");
|
||||||
|
UpdateWindow(_mapShipsCollectionGeneric.ShowSet());
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
JOptionPane.showMessageDialog(null, "Не удалось добавить объект");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
ButtonDeleteShip.addActionListener(new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
if (maskedTextBoxPosition.getText().isEmpty())
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
int result = JOptionPane.showConfirmDialog(null,"Удалить объект?","Удаление",JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE);
|
||||||
|
if(result==JOptionPane.NO_OPTION)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
int pos=Integer.parseInt(maskedTextBoxPosition.getText());
|
||||||
|
if(_mapShipsCollectionGeneric.Delete(pos)!=null)
|
||||||
|
{
|
||||||
|
JOptionPane.showMessageDialog(null, "Объект удален");
|
||||||
|
UpdateWindow(_mapShipsCollectionGeneric.ShowSet());
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
JOptionPane.showMessageDialog(null, "Не удалось удалить объект");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
ButtonShowStorage.addActionListener(new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
if (_mapShipsCollectionGeneric == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
UpdateWindow(_mapShipsCollectionGeneric.ShowSet());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
ButtonShowMap.addActionListener(new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
if (_mapShipsCollectionGeneric == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
UpdateWindow(_mapShipsCollectionGeneric.ShowOnMap());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
ButtonUp.addActionListener(new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
if (_mapShipsCollectionGeneric == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
pictureBoxShip.removeAll();
|
||||||
|
JLabel imageWithMapAndObject = new JLabel();
|
||||||
|
imageWithMapAndObject.setPreferredSize(pictureBoxShip.getSize());
|
||||||
|
imageWithMapAndObject.setMinimumSize(new Dimension(1, 1));
|
||||||
|
imageWithMapAndObject.setIcon(new ImageIcon(_mapShipsCollectionGeneric.MoveObject(Direction.Up)));
|
||||||
|
pictureBoxShip.add(imageWithMapAndObject, BorderLayout.CENTER);
|
||||||
|
pictureBoxShip.revalidate();
|
||||||
|
pictureBoxShip.repaint();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
ButtonDown.addActionListener(new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
if (_mapShipsCollectionGeneric == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
pictureBoxShip.removeAll();
|
||||||
|
JLabel imageWithMapAndObject = new JLabel();
|
||||||
|
imageWithMapAndObject.setPreferredSize(pictureBoxShip.getSize());
|
||||||
|
imageWithMapAndObject.setMinimumSize(new Dimension(1, 1));
|
||||||
|
imageWithMapAndObject.setIcon(new ImageIcon(_mapShipsCollectionGeneric.MoveObject(Direction.Down)));
|
||||||
|
pictureBoxShip.add(imageWithMapAndObject, BorderLayout.CENTER);
|
||||||
|
pictureBoxShip.revalidate();
|
||||||
|
pictureBoxShip.repaint();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
ButtonRight.addActionListener(new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
if (_mapShipsCollectionGeneric == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
pictureBoxShip.removeAll();
|
||||||
|
JLabel imageWithMapAndObject = new JLabel();
|
||||||
|
imageWithMapAndObject.setPreferredSize(pictureBoxShip.getSize());
|
||||||
|
imageWithMapAndObject.setMinimumSize(new Dimension(1, 1));
|
||||||
|
imageWithMapAndObject.setIcon(new ImageIcon(_mapShipsCollectionGeneric.MoveObject(Direction.Right)));
|
||||||
|
pictureBoxShip.add(imageWithMapAndObject, BorderLayout.CENTER);
|
||||||
|
pictureBoxShip.revalidate();
|
||||||
|
pictureBoxShip.repaint();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
ButtonLeft.addActionListener(new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
if (_mapShipsCollectionGeneric == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
pictureBoxShip.removeAll();
|
||||||
|
JLabel imageWithMapAndObject = new JLabel();
|
||||||
|
imageWithMapAndObject.setPreferredSize(pictureBoxShip.getSize());
|
||||||
|
imageWithMapAndObject.setMinimumSize(new Dimension(1, 1));
|
||||||
|
imageWithMapAndObject.setIcon(new ImageIcon(_mapShipsCollectionGeneric.MoveObject(Direction.Left)));
|
||||||
|
pictureBoxShip.add(imageWithMapAndObject, BorderLayout.CENTER);
|
||||||
|
pictureBoxShip.revalidate();
|
||||||
|
pictureBoxShip.repaint();
|
||||||
|
|
||||||
|
}
|
||||||
|
});
|
||||||
|
maskedTextBoxPosition.addKeyListener(new KeyAdapter() {
|
||||||
|
@Override
|
||||||
|
public void keyTyped(KeyEvent e) {
|
||||||
|
char c = e.getKeyChar();
|
||||||
|
if ( ((c < '0') || (c > '9')) || maskedTextBoxPosition.getText().length() >= 2) {
|
||||||
|
e.consume();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
62
FormParam.form
Normal file
62
FormParam.form
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="FormParam">
|
||||||
|
<grid id="27dc6" binding="MainPanel" layout-manager="GridLayoutManager" row-count="4" 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="500" height="400"/>
|
||||||
|
</constraints>
|
||||||
|
<properties/>
|
||||||
|
<border type="none"/>
|
||||||
|
<children>
|
||||||
|
<grid id="7700e" binding="pictureBoxShip" layout-manager="BorderLayout" hgap="0" vgap="0">
|
||||||
|
<constraints>
|
||||||
|
<grid row="0" column="0" row-span="1" col-span="2" 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="b960b" class="javax.swing.JButton" binding="ButtonCreate">
|
||||||
|
<constraints>
|
||||||
|
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||||
|
</constraints>
|
||||||
|
<properties>
|
||||||
|
<text value="Создать"/>
|
||||||
|
</properties>
|
||||||
|
</component>
|
||||||
|
<component id="a44b0" class="javax.swing.JButton" binding="ButtonCreateModif">
|
||||||
|
<constraints>
|
||||||
|
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||||
|
</constraints>
|
||||||
|
<properties>
|
||||||
|
<text value="Модификация"/>
|
||||||
|
</properties>
|
||||||
|
</component>
|
||||||
|
<hspacer id="56304">
|
||||||
|
<constraints>
|
||||||
|
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||||
|
</constraints>
|
||||||
|
</hspacer>
|
||||||
|
<component id="31e01" class="javax.swing.JLabel" binding="LabelInfo">
|
||||||
|
<constraints>
|
||||||
|
<grid row="2" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
|
||||||
|
</constraints>
|
||||||
|
<properties>
|
||||||
|
<text value=""/>
|
||||||
|
</properties>
|
||||||
|
</component>
|
||||||
|
<toolbar id="7a0ba" binding="StatusStrip">
|
||||||
|
<constraints>
|
||||||
|
<grid row="3" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false">
|
||||||
|
<preferred-size width="-1" height="20"/>
|
||||||
|
</grid>
|
||||||
|
</constraints>
|
||||||
|
<properties>
|
||||||
|
<enabled value="false"/>
|
||||||
|
</properties>
|
||||||
|
<border type="none"/>
|
||||||
|
<children/>
|
||||||
|
</toolbar>
|
||||||
|
</children>
|
||||||
|
</grid>
|
||||||
|
</form>
|
111
FormParam.java
Normal file
111
FormParam.java
Normal file
@ -0,0 +1,111 @@
|
|||||||
|
import javax.swing.*;
|
||||||
|
import java.awt.*;
|
||||||
|
import java.awt.event.ActionEvent;
|
||||||
|
import java.awt.event.ActionListener;
|
||||||
|
import java.awt.image.BufferedImage;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
public class FormParam extends JFrame{
|
||||||
|
public JPanel MainPanel;
|
||||||
|
private JPanel pictureBoxShip;
|
||||||
|
private JButton ButtonCreate;
|
||||||
|
private JButton ButtonCreateModif;
|
||||||
|
private JToolBar StatusStrip;
|
||||||
|
private JLabel LabelInfo;
|
||||||
|
private JLabel JLabelSpeed = new JLabel();
|
||||||
|
private JLabel JLabelWeight = new JLabel();
|
||||||
|
private JLabel JLabelColor = new JLabel();
|
||||||
|
private DrawingEntities<EntityShip,IAdditionalDrawingObject> _drawingEntities;
|
||||||
|
private IAdditionalDrawingObject SetData()
|
||||||
|
{
|
||||||
|
Random random=new Random();
|
||||||
|
int r = random.nextInt(3);
|
||||||
|
if(r==0)
|
||||||
|
{
|
||||||
|
return new DrawingTrapezDeck();
|
||||||
|
}
|
||||||
|
if(r==1)
|
||||||
|
{
|
||||||
|
return new DrawingDeck();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return new DrawingOvalDeck();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
private void Draw(DrawingShip _ship) {
|
||||||
|
pictureBoxShip.removeAll();
|
||||||
|
Random random = new Random();
|
||||||
|
BufferedImage bmp = new BufferedImage(pictureBoxShip.getWidth(), pictureBoxShip.getHeight(),BufferedImage.TYPE_INT_RGB);
|
||||||
|
Graphics gr = bmp.getGraphics();
|
||||||
|
gr.setColor(new Color(238, 238, 238));
|
||||||
|
gr.fillRect(0, 0, pictureBoxShip.getWidth(), pictureBoxShip.getHeight());
|
||||||
|
if (_ship != null) {
|
||||||
|
_ship.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100),
|
||||||
|
pictureBoxShip.getWidth(), pictureBoxShip.getHeight());
|
||||||
|
_ship.DrawTransport(gr);
|
||||||
|
JLabelSpeed.setText("Cкорость: " + _ship.GetShip().GetSpeed() + " ");
|
||||||
|
JLabelWeight.setText("Вес: " + _ship.GetShip().GetWeight() + " ");
|
||||||
|
JLabelColor.setText(("Цвет: " + _ship.GetShip().GetBodyColor() + " "));
|
||||||
|
JLabel imageOfShip = new JLabel();
|
||||||
|
imageOfShip.setPreferredSize(pictureBoxShip.getSize());
|
||||||
|
imageOfShip.setMinimumSize(new Dimension(1, 1));
|
||||||
|
imageOfShip.setIcon(new ImageIcon(bmp));
|
||||||
|
pictureBoxShip.add(imageOfShip,BorderLayout.CENTER);
|
||||||
|
}
|
||||||
|
validate();
|
||||||
|
}
|
||||||
|
public FormParam()
|
||||||
|
{
|
||||||
|
Box LabelBox = Box.createHorizontalBox();
|
||||||
|
LabelBox.setMinimumSize(new Dimension(1, 20));
|
||||||
|
LabelBox.add(JLabelSpeed);
|
||||||
|
LabelBox.add(JLabelWeight);
|
||||||
|
LabelBox.add(JLabelColor);
|
||||||
|
StatusStrip.add(LabelBox);
|
||||||
|
_drawingEntities=new DrawingEntities<>(10,10);
|
||||||
|
ButtonCreate.addActionListener(new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e){
|
||||||
|
Random random = new Random();
|
||||||
|
Color colorFirst = JColorChooser.showDialog(null, "Цвет", null);
|
||||||
|
EntityShip ship=new EntityShip(random.nextInt(100,300), random.nextInt(1000,2000),colorFirst);
|
||||||
|
IAdditionalDrawingObject deck = SetData();
|
||||||
|
int DecksCount=random.nextInt(1,4);
|
||||||
|
deck.SetAddEnum(DecksCount);
|
||||||
|
if((_drawingEntities.Insert(ship)!=-1) & (_drawingEntities.Insert(deck)!=-1))
|
||||||
|
{
|
||||||
|
JOptionPane.showMessageDialog(null,"Объект добавлен");
|
||||||
|
Draw(_drawingEntities.CreateShip());
|
||||||
|
LabelInfo.setText(_drawingEntities.indx+ " " + _drawingEntities.indy);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
JOptionPane.showMessageDialog(null, "Не удалось добавить объект");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
ButtonCreateModif.addActionListener(new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
Random random = new Random();
|
||||||
|
Color colorFirst = JColorChooser.showDialog(null, "Цвет", null);
|
||||||
|
Color colorSecond = JColorChooser.showDialog(null, "Цвет", null);
|
||||||
|
EntityContainerShip _ship=new EntityContainerShip(random.nextInt(100, 300), random.nextInt(1000, 2000), colorFirst,colorSecond,random.nextBoolean(),random.nextBoolean());
|
||||||
|
IAdditionalDrawingObject deck = SetData();
|
||||||
|
int DecksCount=random.nextInt(1,4);
|
||||||
|
deck.SetAddEnum(DecksCount);
|
||||||
|
if((_drawingEntities.Insert(_ship)!=-1) & (_drawingEntities.Insert(deck)!=-1))
|
||||||
|
{
|
||||||
|
JOptionPane.showMessageDialog(null,"Объект добавлен");
|
||||||
|
Draw(_drawingEntities.CreateShip());
|
||||||
|
LabelInfo.setText(_drawingEntities.indx+ " " + _drawingEntities.indy);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
JOptionPane.showMessageDialog(null, "Не удалось добавить объект");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
118
FormShip.form
Normal file
118
FormShip.form
Normal file
@ -0,0 +1,118 @@
|
|||||||
|
<?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="Mainpanel" layout-manager="GridLayoutManager" row-count="4" column-count="9" 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="500" height="400"/>
|
||||||
|
</constraints>
|
||||||
|
<properties/>
|
||||||
|
<border type="none"/>
|
||||||
|
<children>
|
||||||
|
<component id="2ea16" class="javax.swing.JButton" binding="ButtonDown">
|
||||||
|
<constraints>
|
||||||
|
<grid row="2" column="7" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="3" indent="0" use-parent-layout="false">
|
||||||
|
<minimum-size width="30" height="30"/>
|
||||||
|
<preferred-size width="30" height="30"/>
|
||||||
|
<maximum-size width="30" height="30"/>
|
||||||
|
</grid>
|
||||||
|
</constraints>
|
||||||
|
<properties>
|
||||||
|
<text value=""/>
|
||||||
|
</properties>
|
||||||
|
</component>
|
||||||
|
<component id="4f60a" class="javax.swing.JButton" binding="ButtonLeft">
|
||||||
|
<constraints>
|
||||||
|
<grid row="2" column="6" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="3" indent="0" use-parent-layout="false">
|
||||||
|
<minimum-size width="30" height="30"/>
|
||||||
|
<preferred-size width="30" height="30"/>
|
||||||
|
<maximum-size width="30" height="30"/>
|
||||||
|
</grid>
|
||||||
|
</constraints>
|
||||||
|
<properties>
|
||||||
|
<text value=""/>
|
||||||
|
</properties>
|
||||||
|
</component>
|
||||||
|
<component id="4eb88" class="javax.swing.JButton" binding="ButtonRight">
|
||||||
|
<constraints>
|
||||||
|
<grid row="2" column="8" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="3" indent="0" use-parent-layout="false">
|
||||||
|
<minimum-size width="30" height="30"/>
|
||||||
|
<preferred-size width="30" height="30"/>
|
||||||
|
<maximum-size width="30" height="30"/>
|
||||||
|
</grid>
|
||||||
|
</constraints>
|
||||||
|
<properties>
|
||||||
|
<horizontalAlignment value="0"/>
|
||||||
|
<text value=""/>
|
||||||
|
</properties>
|
||||||
|
</component>
|
||||||
|
<component id="fb937" class="javax.swing.JButton" binding="ButtonUp">
|
||||||
|
<constraints>
|
||||||
|
<grid row="1" column="7" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="3" indent="0" use-parent-layout="false">
|
||||||
|
<minimum-size width="30" height="30"/>
|
||||||
|
<preferred-size width="30" height="30"/>
|
||||||
|
<maximum-size width="30" height="30"/>
|
||||||
|
</grid>
|
||||||
|
</constraints>
|
||||||
|
<properties>
|
||||||
|
<text value=""/>
|
||||||
|
</properties>
|
||||||
|
</component>
|
||||||
|
<toolbar id="e747d" binding="StatusStrip">
|
||||||
|
<constraints>
|
||||||
|
<grid row="3" column="0" row-span="1" col-span="9" vsize-policy="0" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false">
|
||||||
|
<minimum-size width="-1" height="20"/>
|
||||||
|
<preferred-size width="-1" height="20"/>
|
||||||
|
<maximum-size width="-1" height="20"/>
|
||||||
|
</grid>
|
||||||
|
</constraints>
|
||||||
|
<properties>
|
||||||
|
<enabled value="false"/>
|
||||||
|
</properties>
|
||||||
|
<border type="none"/>
|
||||||
|
<children/>
|
||||||
|
</toolbar>
|
||||||
|
<hspacer id="d86ff">
|
||||||
|
<constraints>
|
||||||
|
<grid row="2" column="1" row-span="1" col-span="5" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||||
|
</constraints>
|
||||||
|
</hspacer>
|
||||||
|
<grid id="6b0b8" binding="pictureBoxShip" layout-manager="FlowLayout" hgap="5" vgap="5" flow-align="1">
|
||||||
|
<constraints>
|
||||||
|
<grid row="0" column="0" row-span="1" col-span="9" 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="9e2ce" class="javax.swing.JButton" binding="ButtonCreate">
|
||||||
|
<constraints>
|
||||||
|
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||||
|
</constraints>
|
||||||
|
<properties>
|
||||||
|
<text value="Создать"/>
|
||||||
|
</properties>
|
||||||
|
</component>
|
||||||
|
<component id="86a62" class="javax.swing.JButton" binding="ButtonCreateModif">
|
||||||
|
<constraints>
|
||||||
|
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||||
|
</constraints>
|
||||||
|
<properties>
|
||||||
|
<text value="Модификация"/>
|
||||||
|
</properties>
|
||||||
|
</component>
|
||||||
|
<hspacer id="9abb1">
|
||||||
|
<constraints>
|
||||||
|
<grid row="1" column="1" row-span="1" col-span="4" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||||
|
</constraints>
|
||||||
|
</hspacer>
|
||||||
|
<component id="8005c" class="javax.swing.JButton" binding="ButtonSelect">
|
||||||
|
<constraints>
|
||||||
|
<grid row="1" column="5" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||||
|
</constraints>
|
||||||
|
<properties>
|
||||||
|
<text value="Выбрать"/>
|
||||||
|
</properties>
|
||||||
|
</component>
|
||||||
|
</children>
|
||||||
|
</grid>
|
||||||
|
</form>
|
149
FormShip.java
Normal file
149
FormShip.java
Normal file
@ -0,0 +1,149 @@
|
|||||||
|
import javax.imageio.ImageIO;
|
||||||
|
import javax.swing.*;
|
||||||
|
import java.awt.*;
|
||||||
|
import java.awt.event.ActionEvent;
|
||||||
|
import java.awt.event.ActionListener;
|
||||||
|
import java.awt.event.ComponentAdapter;
|
||||||
|
import java.awt.event.ComponentEvent;
|
||||||
|
import java.awt.image.BufferedImage;
|
||||||
|
import java.util.Random;
|
||||||
|
public class FormShip extends JDialog{
|
||||||
|
public JPanel Mainpanel;
|
||||||
|
private Random random = new Random();
|
||||||
|
private JButton ButtonCreate;
|
||||||
|
private JButton ButtonLeft;
|
||||||
|
private JButton ButtonUp;
|
||||||
|
private JButton ButtonDown;
|
||||||
|
private JButton ButtonRight;
|
||||||
|
private JButton ButtonCreateModif;
|
||||||
|
private JButton ButtonSelect;
|
||||||
|
protected DrawingShip _ship;
|
||||||
|
private JPanel pictureBoxShip;
|
||||||
|
private JToolBar StatusStrip;
|
||||||
|
private JLabel JLabelSpeed = new JLabel();
|
||||||
|
private JLabel JLabelWeight = new JLabel();
|
||||||
|
private JLabel JLabelColor = new JLabel();
|
||||||
|
protected DrawingShip SelectedShip;
|
||||||
|
public DrawingShip GetSelectedShip()
|
||||||
|
{
|
||||||
|
return SelectedShip;
|
||||||
|
}
|
||||||
|
public void Draw(DrawingShip _ship) {
|
||||||
|
pictureBoxShip.removeAll();
|
||||||
|
BufferedImage bmp = new BufferedImage(pictureBoxShip.getWidth(), pictureBoxShip.getHeight(),BufferedImage.TYPE_INT_RGB);
|
||||||
|
Graphics gr = bmp.getGraphics();
|
||||||
|
gr.setColor(new Color(238, 238, 238));
|
||||||
|
gr.fillRect(0, 0, pictureBoxShip.getWidth(), pictureBoxShip.getHeight());
|
||||||
|
if (_ship != null) {
|
||||||
|
_ship.DrawTransport(gr);
|
||||||
|
JLabel imageOfShip = new JLabel();
|
||||||
|
imageOfShip.setPreferredSize(pictureBoxShip.getSize());
|
||||||
|
imageOfShip.setMinimumSize(new Dimension(1, 1));
|
||||||
|
imageOfShip.setIcon(new ImageIcon(bmp));
|
||||||
|
pictureBoxShip.add(imageOfShip,BorderLayout.CENTER);
|
||||||
|
}
|
||||||
|
validate();
|
||||||
|
}
|
||||||
|
public FormShip() {
|
||||||
|
super(new Frame("Корабль"));
|
||||||
|
Box LabelBox = Box.createHorizontalBox();
|
||||||
|
LabelBox.setMinimumSize(new Dimension(1, 20));
|
||||||
|
LabelBox.add(JLabelSpeed);
|
||||||
|
LabelBox.add(JLabelWeight);
|
||||||
|
LabelBox.add(JLabelColor);
|
||||||
|
StatusStrip.add(LabelBox);
|
||||||
|
try {
|
||||||
|
Image img = ImageIO.read(FormShip.class.getResource("Images/4.png"));
|
||||||
|
ButtonUp.setIcon(new ImageIcon(img));
|
||||||
|
img = ImageIO.read(FormShip.class.getResource("Images/2.png"));
|
||||||
|
ButtonDown.setIcon(new ImageIcon(img));
|
||||||
|
img = ImageIO.read(FormShip.class.getResource("Images/1.png"));
|
||||||
|
ButtonLeft.setIcon(new ImageIcon(img));
|
||||||
|
img = ImageIO.read(FormShip.class.getResource("Images/3.png"));
|
||||||
|
ButtonRight.setIcon(new ImageIcon(img));
|
||||||
|
} catch (Exception ex) {
|
||||||
|
System.out.println(ex);
|
||||||
|
}
|
||||||
|
ButtonCreate.addActionListener(new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e){
|
||||||
|
Random random = new Random();
|
||||||
|
Color colorFirst = JColorChooser.showDialog(null, "Цвет", null);
|
||||||
|
_ship=new DrawingShip(random.nextInt(100, 300), random.nextInt(1000, 2000), colorFirst);
|
||||||
|
_ship.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100), pictureBoxShip.getWidth(), pictureBoxShip.getHeight());
|
||||||
|
JLabelSpeed.setText("Cкорость: " + _ship.GetShip().GetSpeed() + " ");
|
||||||
|
JLabelWeight.setText("Вес: " + _ship.GetShip().GetWeight() + " ");
|
||||||
|
JLabelColor.setText(("Цвет: " + _ship.GetShip().GetBodyColor() + " "));
|
||||||
|
Draw(_ship);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
pictureBoxShip.addComponentListener(new ComponentAdapter() {
|
||||||
|
@Override
|
||||||
|
public void componentResized(ComponentEvent e) {
|
||||||
|
super.componentResized(e);
|
||||||
|
if(_ship!=null)
|
||||||
|
{
|
||||||
|
_ship.ChangeBorders(pictureBoxShip.getWidth(), pictureBoxShip.getHeight());
|
||||||
|
pictureBoxShip.revalidate();
|
||||||
|
Draw(_ship);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
});
|
||||||
|
ButtonUp.addActionListener(new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
_ship.MoveTransport(Direction.Up);
|
||||||
|
pictureBoxShip.revalidate();
|
||||||
|
Draw(_ship);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
ButtonDown.addActionListener(new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
_ship.MoveTransport(Direction.Down);
|
||||||
|
pictureBoxShip.revalidate();
|
||||||
|
Draw(_ship);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
ButtonRight.addActionListener(new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
_ship.MoveTransport(Direction.Right);
|
||||||
|
pictureBoxShip.revalidate();
|
||||||
|
Draw(_ship);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
ButtonLeft.addActionListener(new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
_ship.MoveTransport(Direction.Left);
|
||||||
|
pictureBoxShip.revalidate();
|
||||||
|
Draw(_ship);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
ButtonCreateModif.addActionListener(new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
Random random = new Random();
|
||||||
|
Color colorFirst = JColorChooser.showDialog(null, "Цвет", null);
|
||||||
|
Color colorSecond = JColorChooser.showDialog(null, "Цвет", null);
|
||||||
|
_ship=new DrawingContainerShip(random.nextInt(100, 300), random.nextInt(1000, 2000), colorFirst,colorSecond,random.nextBoolean(),random.nextBoolean());
|
||||||
|
_ship.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100), pictureBoxShip.getWidth(), pictureBoxShip.getHeight());
|
||||||
|
JLabelSpeed.setText("Cкорость: " + _ship.GetShip().GetSpeed() + " ");
|
||||||
|
JLabelWeight.setText("Вес: " + _ship.GetShip().GetWeight() + " ");
|
||||||
|
JLabelColor.setText(("Цвет: " + _ship.GetShip().GetBodyColor() + " "));
|
||||||
|
Draw(_ship);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
ButtonSelect.addActionListener(new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
SelectedShip=_ship;
|
||||||
|
dispose();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
setModal(true);
|
||||||
|
getContentPane().add(Mainpanel);
|
||||||
|
}
|
||||||
|
}
|
6
IAdditionalDrawingObject.java
Normal file
6
IAdditionalDrawingObject.java
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
import java.awt.*;
|
||||||
|
|
||||||
|
public interface IAdditionalDrawingObject {
|
||||||
|
void SetAddEnum(int decksAmount);
|
||||||
|
void DrawDeck(Color colorDeck, Graphics g, float _startPosX, float _startPosY);
|
||||||
|
}
|
9
IDrawingObject.java
Normal file
9
IDrawingObject.java
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
import java.awt.*;
|
||||||
|
|
||||||
|
public interface IDrawingObject {
|
||||||
|
public float Step();
|
||||||
|
void SetObject(int x, int y,int width,int height);
|
||||||
|
void MoveObject(Direction direction);
|
||||||
|
void DrawingObject(Graphics g);
|
||||||
|
float[] GetCurrentPosition();
|
||||||
|
}
|
BIN
Images/1.png
Normal file
BIN
Images/1.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 260 B |
BIN
Images/2.png
Normal file
BIN
Images/2.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 253 B |
BIN
Images/3.png
Normal file
BIN
Images/3.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 266 B |
BIN
Images/4.png
Normal file
BIN
Images/4.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 245 B |
57
IslandsMap.java
Normal file
57
IslandsMap.java
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
import java.awt.*;
|
||||||
|
|
||||||
|
public class IslandsMap extends AbstractMap{
|
||||||
|
private final Color barrierColor = Color.YELLOW;
|
||||||
|
private final Color roadColor = Color.CYAN;
|
||||||
|
@Override
|
||||||
|
protected void DrawBarrierPart(Graphics g, int i, int j)
|
||||||
|
{
|
||||||
|
Graphics2D g2d = (Graphics2D) g;
|
||||||
|
g2d.setPaint(barrierColor);
|
||||||
|
g2d.fillRect((int)(i * _size_x), (int) (j * _size_y), (int)(i * (_size_x + 1)), (int)(j * (_size_y + 1)));
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
protected void DrawRoadPart(Graphics g, int i, int j)
|
||||||
|
{
|
||||||
|
Graphics2D g2d = (Graphics2D) g;
|
||||||
|
g2d.setPaint(roadColor);
|
||||||
|
g2d.fillRect((int)(i * _size_x), (int) (j * _size_y), (int)(i * (_size_x + 1)), (int)(j * (_size_y + 1)));
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
protected void GenerateMap()
|
||||||
|
{
|
||||||
|
_map = new int[100][100];
|
||||||
|
_size_x = (float)_width / _map.length;
|
||||||
|
_size_y = (float)_height / _map[0].length;
|
||||||
|
int counter = 0;
|
||||||
|
for (int i = 0; i < _map.length; ++i)
|
||||||
|
{
|
||||||
|
for (int j = 0; j < _map[0].length; ++j)
|
||||||
|
{
|
||||||
|
_map[i][j] = _water;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
while (counter < 10)
|
||||||
|
{
|
||||||
|
int x = _random.nextInt(0, 100);
|
||||||
|
int y = _random.nextInt(0, 100);
|
||||||
|
if (_map[x][y] == _water && x < 98 && y < 99)
|
||||||
|
if (_map[x][y] == _water && x < 97 && y < 97)
|
||||||
|
{
|
||||||
|
_map[x + 1][y] = _barrier;
|
||||||
|
_map[x + 2][y] = _barrier;
|
||||||
|
_map[x][y + 1] = _barrier;
|
||||||
|
_map[x + 1][y + 1] = _barrier;
|
||||||
|
_map[x + 2][y + 1] = _barrier;
|
||||||
|
_map[x + 3][y + 1] = _barrier;
|
||||||
|
_map[x][y + 2] = _barrier;
|
||||||
|
_map[x + 1][y + 2] = _barrier;
|
||||||
|
_map[x + 2][y + 2] = _barrier;
|
||||||
|
_map[x + 3][y + 2] = _barrier;
|
||||||
|
_map[x + 1][y + 3] = _barrier;
|
||||||
|
_map[x + 2][y + 3] = _barrier;
|
||||||
|
counter++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
13
Main.java
Normal file
13
Main.java
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
import javax.swing.*;
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
JFrame frame = new JFrame("Контейнеровоз");
|
||||||
|
frame.setContentPane(new FormMapWithSetShipsGeneric().Mainpanel);
|
||||||
|
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
|
frame.setLocation(500, 200);
|
||||||
|
frame.pack();
|
||||||
|
frame.setSize(800, 600);
|
||||||
|
frame.setVisible(true);
|
||||||
|
}
|
||||||
|
}
|
145
MapWithSetShipsGeneric.java
Normal file
145
MapWithSetShipsGeneric.java
Normal file
@ -0,0 +1,145 @@
|
|||||||
|
import java.awt.*;
|
||||||
|
import java.awt.image.BufferedImage;
|
||||||
|
|
||||||
|
public class MapWithSetShipsGeneric<T extends IDrawingObject, U extends AbstractMap> {
|
||||||
|
private final int _pictureWidth;
|
||||||
|
private final int _pictureHeight;
|
||||||
|
private final int _placeSizeWidth = 210;
|
||||||
|
private final int _placeSizeHeight = 90;
|
||||||
|
private final SetShipGeneric<T> _setShips;
|
||||||
|
private final U _map;
|
||||||
|
public MapWithSetShipsGeneric(int picWidth, int picHeight, U map)
|
||||||
|
{
|
||||||
|
int width = picWidth / _placeSizeWidth;
|
||||||
|
int height = picHeight / _placeSizeHeight;
|
||||||
|
_setShips = new SetShipGeneric<T>(width * height);
|
||||||
|
_pictureWidth = picWidth;
|
||||||
|
_pictureHeight = picHeight;
|
||||||
|
_map = map;
|
||||||
|
}
|
||||||
|
public int Add(T ship)
|
||||||
|
{
|
||||||
|
return _setShips.Insert(ship);
|
||||||
|
}
|
||||||
|
public T Delete(int position)
|
||||||
|
{
|
||||||
|
return _setShips.Remove(position);
|
||||||
|
}
|
||||||
|
public BufferedImage ShowSet()
|
||||||
|
{
|
||||||
|
BufferedImage bmp = new BufferedImage(_pictureWidth,_pictureHeight,BufferedImage.TYPE_INT_RGB);
|
||||||
|
Graphics gr = bmp.getGraphics();
|
||||||
|
DrawBackground(gr);
|
||||||
|
DrawShips(gr);
|
||||||
|
return bmp;
|
||||||
|
}
|
||||||
|
public BufferedImage ShowOnMap()
|
||||||
|
{
|
||||||
|
Shaking();
|
||||||
|
for (int i = 0; i < _setShips.Count(); i++)
|
||||||
|
{
|
||||||
|
var ship = _setShips.Get(i);
|
||||||
|
if (ship != null)
|
||||||
|
{
|
||||||
|
return _map.CreateMap(_pictureWidth, _pictureHeight, ship);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_RGB);
|
||||||
|
}
|
||||||
|
public BufferedImage MoveObject(Direction direction)
|
||||||
|
{
|
||||||
|
if (_map != null)
|
||||||
|
{
|
||||||
|
return _map.MoveObject(direction);
|
||||||
|
}
|
||||||
|
return new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_RGB);
|
||||||
|
}
|
||||||
|
private void Shaking()
|
||||||
|
{
|
||||||
|
int j = _setShips.Count() - 1;
|
||||||
|
for (int i = 0; i < _setShips.Count(); 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(Graphics gr)
|
||||||
|
{
|
||||||
|
Graphics2D g = (Graphics2D) gr;
|
||||||
|
g.setColor(Color.blue);
|
||||||
|
g.fillRect( 0, 0, _pictureWidth, _pictureHeight);
|
||||||
|
|
||||||
|
for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++)
|
||||||
|
{
|
||||||
|
for (int j = 0; j < _pictureHeight / _placeSizeHeight; ++j)
|
||||||
|
{
|
||||||
|
g.setColor(new Color(90, 41, 19));
|
||||||
|
g.setStroke(new BasicStroke(3));
|
||||||
|
|
||||||
|
g.drawLine(i * _placeSizeWidth, j * _placeSizeHeight + _placeSizeHeight/2, i *
|
||||||
|
_placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight + _placeSizeHeight / 2);
|
||||||
|
|
||||||
|
for(int k = 1; k < 3; ++k)
|
||||||
|
{
|
||||||
|
g.setColor(new Color(2, 2, 2));
|
||||||
|
g.setStroke(new BasicStroke(1));
|
||||||
|
g.drawLine(i * _placeSizeWidth, j * _placeSizeHeight + k * 21, i *
|
||||||
|
_placeSizeWidth + 3 * _placeSizeWidth / 10, j * _placeSizeHeight + k * 21);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int k = 0; k < 4; ++k)
|
||||||
|
{
|
||||||
|
g.setColor(new Color(90, 41, 19));
|
||||||
|
g.setStroke(new BasicStroke(5));
|
||||||
|
g.drawLine(i * _placeSizeWidth + k * _placeSizeWidth / 10, j * _placeSizeHeight + 20, i * _placeSizeWidth + k * _placeSizeWidth / 10,
|
||||||
|
(j + 1) * _placeSizeHeight - 20);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
private void DrawShips(Graphics g)
|
||||||
|
{
|
||||||
|
int xNumOfPlaces = _pictureWidth / _placeSizeWidth;
|
||||||
|
int yNumOfPlaces = _pictureHeight / _placeSizeHeight;
|
||||||
|
int RowIndex = yNumOfPlaces - 1;
|
||||||
|
int ColumnIndex = xNumOfPlaces - 1;
|
||||||
|
for (int i = 0; i < _setShips.Count(); i++)
|
||||||
|
{
|
||||||
|
if(_setShips.Get(i)==null)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
_setShips.Get(i).SetObject(ColumnIndex * _placeSizeWidth,
|
||||||
|
RowIndex * _placeSizeHeight + (_placeSizeHeight - (int)(_setShips.Get(i).GetCurrentPosition()[3] - _setShips.Get(i).GetCurrentPosition()[1])),
|
||||||
|
_pictureWidth, _pictureHeight);
|
||||||
|
_setShips.Get(i).DrawingObject(g);
|
||||||
|
if (ColumnIndex == 0) {
|
||||||
|
ColumnIndex = xNumOfPlaces - 1;
|
||||||
|
RowIndex--;
|
||||||
|
}else
|
||||||
|
{
|
||||||
|
ColumnIndex--;
|
||||||
|
}
|
||||||
|
if (ColumnIndex > yNumOfPlaces)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
50
RocksMap.java
Normal file
50
RocksMap.java
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
import java.awt.*;
|
||||||
|
|
||||||
|
public class RocksMap extends AbstractMap{
|
||||||
|
private final Color barrierColor = Color.GRAY;
|
||||||
|
private final Color roadColor = Color.CYAN;
|
||||||
|
@Override
|
||||||
|
protected void DrawBarrierPart(Graphics g, int i, int j)
|
||||||
|
{
|
||||||
|
Graphics2D g2d = (Graphics2D) g;
|
||||||
|
g2d.setPaint(barrierColor);
|
||||||
|
g2d.fillRect((int)(i * _size_x), (int) (j * _size_y), (int)(i * (_size_x + 1)), (int)(j * (_size_y + 1)));
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
protected void DrawRoadPart(Graphics g, int i, int j)
|
||||||
|
{
|
||||||
|
Graphics2D g2d = (Graphics2D) g;
|
||||||
|
g2d.setPaint(roadColor);
|
||||||
|
g2d.fillRect((int)(i * _size_x), (int) (j * _size_y), (int)(i * (_size_x + 1)), (int)(j * (_size_y + 1)));
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
protected void GenerateMap()
|
||||||
|
{
|
||||||
|
_map = new int[100][100];
|
||||||
|
_size_x = (float)_width / _map.length;
|
||||||
|
_size_y = (float)_height / _map[0].length;
|
||||||
|
int counter = 0;
|
||||||
|
for (int i = 0; i < _map.length; ++i)
|
||||||
|
{
|
||||||
|
for (int j = 0; j < _map[0].length; ++j)
|
||||||
|
{
|
||||||
|
_map[i][j] = _water;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
while (counter < 10)
|
||||||
|
{
|
||||||
|
int x = _random.nextInt(0, 100);
|
||||||
|
int y = _random.nextInt(0, 100);
|
||||||
|
if (_map[x][y] == _water && x < 98 && y < 99)
|
||||||
|
{
|
||||||
|
_map[x][y] = _barrier;
|
||||||
|
_map[x + 1][y] = _barrier;
|
||||||
|
_map[x + 2][y] = _barrier;
|
||||||
|
_map[x + 1][y + 1] = _barrier;
|
||||||
|
|
||||||
|
counter++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
65
SetShipGeneric.java
Normal file
65
SetShipGeneric.java
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
public class SetShipGeneric<T extends Object> {
|
||||||
|
private final Object[] _places;
|
||||||
|
public int Count()
|
||||||
|
{
|
||||||
|
return _places.length;
|
||||||
|
}
|
||||||
|
public SetShipGeneric(int count)
|
||||||
|
{
|
||||||
|
_places = new Object[count];
|
||||||
|
}
|
||||||
|
public int Insert(T ship)
|
||||||
|
{
|
||||||
|
return Insert(ship, 0);
|
||||||
|
}
|
||||||
|
public int Insert(T ship, int position)
|
||||||
|
{
|
||||||
|
if (position >= _places.length || position < 0)
|
||||||
|
{
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
if (_places[position] == null)
|
||||||
|
{
|
||||||
|
_places[position] = ship;
|
||||||
|
return position;
|
||||||
|
}
|
||||||
|
int emptyIndex = -1;
|
||||||
|
for (int i = position + 1; i < Count(); i++)
|
||||||
|
{
|
||||||
|
if (_places[i] == null)
|
||||||
|
{
|
||||||
|
emptyIndex = i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (emptyIndex < 0)
|
||||||
|
{
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
for (int i = emptyIndex; i > position; i--)
|
||||||
|
{
|
||||||
|
_places[i] = _places[i - 1];
|
||||||
|
}
|
||||||
|
_places[position] = ship;
|
||||||
|
return position;
|
||||||
|
}
|
||||||
|
public T Remove(int position)
|
||||||
|
{
|
||||||
|
if (position >= _places.length || position < 0)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
T ship = (T) _places[position];
|
||||||
|
_places[position] = null;
|
||||||
|
return ship;
|
||||||
|
}
|
||||||
|
public T Get(int position)
|
||||||
|
{
|
||||||
|
if (position >= _places.length || position < 0)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return (T)_places[position];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
45
SimpleMap.java
Normal file
45
SimpleMap.java
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
import java.awt.*;
|
||||||
|
|
||||||
|
public class SimpleMap extends AbstractMap{
|
||||||
|
private final Color barrierColor = Color.black;
|
||||||
|
private final Color roadColor = Color.gray;
|
||||||
|
@Override
|
||||||
|
protected void DrawBarrierPart(Graphics g, int i, int j)
|
||||||
|
{
|
||||||
|
Graphics2D g2d = (Graphics2D) g;
|
||||||
|
g2d.setPaint(barrierColor);
|
||||||
|
g2d.fillRect((int)(i * _size_x), (int) (j * _size_y), (int)(i * (_size_x + 1)), (int)(j * (_size_y + 1)));
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
protected void DrawRoadPart(Graphics g, int i, int j)
|
||||||
|
{
|
||||||
|
Graphics2D g2d = (Graphics2D) g;
|
||||||
|
g2d.setPaint(roadColor);
|
||||||
|
g2d.fillRect((int)(i * _size_x), (int) (j * _size_y), (int)(i * (_size_x + 1)), (int)(j * (_size_y + 1)));
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
protected void GenerateMap()
|
||||||
|
{
|
||||||
|
_map = new int[100][100];
|
||||||
|
_size_x = (float)_width / _map.length;
|
||||||
|
_size_y = (float)_height / _map[0].length;
|
||||||
|
int counter = 0;
|
||||||
|
for (int i = 0; i < _map.length; ++i)
|
||||||
|
{
|
||||||
|
for (int j = 0; j < _map[0].length; ++j)
|
||||||
|
{
|
||||||
|
_map[i][j] = _water;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
while (counter < 20)
|
||||||
|
{
|
||||||
|
int x = _random.nextInt(0, 100);
|
||||||
|
int y = _random.nextInt(0, 100);
|
||||||
|
if (_map[x][y] == _water)
|
||||||
|
{
|
||||||
|
_map[x][y] = _barrier;
|
||||||
|
counter++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user