From da3355686d9c6dfc95e26b6f6144c88a676b5da0 Mon Sep 17 00:00:00 2001 From: ksenianeva <95441235+ksenianeva@users.noreply.github.com> Date: Wed, 21 Dec 2022 21:54:47 +0400 Subject: [PATCH] =?UTF-8?q?=D0=92=D1=82=D0=BE=D1=80=D0=B0=D1=8F=20=D1=83?= =?UTF-8?q?=D1=81=D0=BB=D0=BE=D0=B6=D0=BD=D0=B5=D0=BD=D0=BD=D0=B0=D1=8F=20?= =?UTF-8?q?=D0=BB=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82=D0=BE=D1=80=D0=BD?= =?UTF-8?q?=D0=B0=D1=8F=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/containership/AbstractMap.java | 123 +++++++ .../src/containership/DecksType.java | 15 + .../src/containership/Direction.java | 3 +- .../containership/DrawingContainerShip.java | 55 ++++ .../src/containership/DrawingDecks.java | 29 +- .../containership/DrawingDecksReversedTr.java | 123 +++++++ .../src/containership/DrawingDecksTropec.java | 125 +++++++ .../src/containership/DrawingObjectShip.java | 58 ++++ .../src/containership/DrawingShip.java | 56 +++- .../containership/EntityContainerShip.java | 36 ++ .../src/containership/EntityShip.java | 4 +- .../src/containership/IDrawingObject.java | 40 +++ .../src/containership/IDrawingObjectAdd.java | 18 + .../src/containership/JFrame_Map.form | 228 +++++++++++++ .../src/containership/JFrame_Map.java | 308 ++++++++++++++++++ .../src/containership/JFrame_Ship.form | 46 ++- .../src/containership/JFrame_Ship.java | 84 ++++- .../src/containership/ModifyMap.java | 61 ++++ .../src/containership/SimpleMap.java | 60 ++++ 19 files changed, 1426 insertions(+), 46 deletions(-) create mode 100644 ContainerShip/src/containership/AbstractMap.java create mode 100644 ContainerShip/src/containership/DecksType.java create mode 100644 ContainerShip/src/containership/DrawingContainerShip.java create mode 100644 ContainerShip/src/containership/DrawingDecksReversedTr.java create mode 100644 ContainerShip/src/containership/DrawingDecksTropec.java create mode 100644 ContainerShip/src/containership/DrawingObjectShip.java create mode 100644 ContainerShip/src/containership/EntityContainerShip.java create mode 100644 ContainerShip/src/containership/IDrawingObject.java create mode 100644 ContainerShip/src/containership/IDrawingObjectAdd.java create mode 100644 ContainerShip/src/containership/JFrame_Map.form create mode 100644 ContainerShip/src/containership/JFrame_Map.java create mode 100644 ContainerShip/src/containership/ModifyMap.java create mode 100644 ContainerShip/src/containership/SimpleMap.java diff --git a/ContainerShip/src/containership/AbstractMap.java b/ContainerShip/src/containership/AbstractMap.java new file mode 100644 index 0000000..28ce517 --- /dev/null +++ b/ContainerShip/src/containership/AbstractMap.java @@ -0,0 +1,123 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template + */ +package containership; + +import java.awt.Graphics; +import java.awt.Graphics2D; +import java.awt.image.BufferedImage; +import java.util.Random; + +/** + * + * @author ateks + */ +public abstract class AbstractMap { + private IDrawingObject _drawningObject = null; + protected int[][] _map = null; + protected int _width; + protected int _height; + protected int _size_x; + protected int _size_y; + protected final Random _random = new Random(); + protected final int _freeRoad = 0; + protected final int _barrier = 1; + + public BufferedImage CreateMap(int width, int height, IDrawingObject drawningObject) + { + _width = width; + _height = height; + _drawningObject = drawningObject; + GenerateMap(); + while (!SetObjectOnMap()) + { + GenerateMap(); + } + return DrawMapWithObject(); + } + public BufferedImage MoveObject(Direction direction) + { + if (_drawningObject == null) return DrawMapWithObject(); + boolean canMove = true; + switch (direction) + { + case LEFT: + if (!checkForBarriers(0, -1 * _drawningObject.getStep(), -1 * _drawningObject.getStep(), 0)) canMove = false; + break; + case RIGHT: + if (!checkForBarriers(0, _drawningObject.getStep(), _drawningObject.getStep(), 0)) canMove = false; + break; + case UP: + if (!checkForBarriers(-1 * _drawningObject.getStep(), 0, 0, -1 * _drawningObject.getStep())) canMove = false; + break; + case DOWN: + if (!checkForBarriers(_drawningObject.getStep(), 0, 0, _drawningObject.getStep())) canMove = false; + break; + } + if (canMove) + { + _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); + _drawningObject.SetObject(x, y, _width, _height); + if (!checkForBarriers(0,0,0,0)) return false; + return true; + } + private BufferedImage DrawMapWithObject() + { + BufferedImage bmp = new BufferedImage(_width, _height, BufferedImage.TYPE_INT_ARGB); + if (_drawningObject == null || _map == null) + { + return bmp; + } + Graphics2D gr = bmp.createGraphics(); + for (int i = 0; i < _map.length; ++i) + { + for (int j = 0; j < _map[i].length; ++j) + { + if (_map[i][j] == _freeRoad) + { + DrawRoadPart(gr, i, j); + } + else if (_map[i][j] == _barrier) + { + DrawBarrierPart(gr, i, j); + } + } + } + _drawningObject.DrawingObject(gr); + return bmp; + } + + private boolean checkForBarriers(float topOffset, float rightOffset, float leftOffset, float bottomOffset) + { + float[] position = _drawningObject.GetCurrentPosition(); + int top = (int)((position[1] + topOffset) / _size_y); + int right = (int)((position[2] + rightOffset) / _size_x); + int left = (int)((position[0] + leftOffset) / _size_x); + int bottom = (int)((position[3] + bottomOffset) / _size_y); + if (top < 0 || left < 0 || right >= _map[0].length || bottom >= _map.length) return false; + for (int i = top; i <= bottom; i++) + { + for (int j = left; j <= right; j++) + { + if (_map[j][i] == 1) return false; + } + } + return true; + } + + protected abstract void GenerateMap(); + protected abstract void DrawRoadPart(Graphics g, int i, int j); + protected abstract void DrawBarrierPart(Graphics g, int i, int j); +} diff --git a/ContainerShip/src/containership/DecksType.java b/ContainerShip/src/containership/DecksType.java new file mode 100644 index 0000000..e393f3b --- /dev/null +++ b/ContainerShip/src/containership/DecksType.java @@ -0,0 +1,15 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template + */ +package containership; + +/** + * + * @author ateks + */ +public enum DecksType { + RECTANGLE, + TROPEC, + REVERSEDTR +} diff --git a/ContainerShip/src/containership/Direction.java b/ContainerShip/src/containership/Direction.java index 31e1a60..af5cd6d 100644 --- a/ContainerShip/src/containership/Direction.java +++ b/ContainerShip/src/containership/Direction.java @@ -12,5 +12,6 @@ public enum Direction { UP, DOWN, LEFT, - RIGHT + RIGHT, + NONE } \ No newline at end of file diff --git a/ContainerShip/src/containership/DrawingContainerShip.java b/ContainerShip/src/containership/DrawingContainerShip.java new file mode 100644 index 0000000..0f2774b --- /dev/null +++ b/ContainerShip/src/containership/DrawingContainerShip.java @@ -0,0 +1,55 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template + */ +package containership; +import java.awt.*; +/** + * + * @author ateks + */ +public class DrawingContainerShip extends DrawingShip { + public DrawingContainerShip(int speed, float weight, Color bodyColor, DecksType decksType, int numberOfDecks, Color dopColor, boolean containers, boolean crane) + { + super(speed, weight, bodyColor, decksType, numberOfDecks, 135, 55); + Ship = new EntityContainerShip(speed, weight, bodyColor, dopColor, containers, crane); + this.dopColor = dopColor; + } + Color dopColor; + + @Override + public void DrawTransport(Graphics2D g) + { + if (!(Ship instanceof EntityContainerShip containerShip)) + { + return; + } + if (containerShip.getCrane()) + { + g.setColor(dopColor); + g.drawLine((int)(_startPosX), (int)(_startPosY+10), (int)(_startPosX+25), (int)(_startPosY+20)); + g.drawLine((int)(_startPosX), (int)(_startPosY+10), (int)(_startPosX), (int)(_startPosY+22)); + g.drawLine((int)(_startPosX), (int)(_startPosY+10), (int)(_startPosX), (int)(_startPosY+22)); + g.drawLine((int)(_startPosX), (int)(_startPosY+10), (int)(_startPosX+25), (int)(_startPosY+10)); + g.drawLine((int)(_startPosX+25), (int)(_startPosY+10), (int)(_startPosX+25), (int)(_startPosY+30)); + g.setColor(Color.BLACK); + } + + super.DrawTransport(g); + + if (containerShip.getContainers()) + { + g.setColor(dopColor); + g.fillRect((int)(_startPosX+35), (int)(_startPosY+20), 10, 10); + g.fillRect((int)(_startPosX+55), (int)(_startPosY+20), 20, 10); + g.fillRect((int)(_startPosX+75), (int)(_startPosY+15), 25, 15); + g.fillRect((int)(_startPosX+100), (int)(_startPosY+20), 25, 10); + g.setColor(Color.BLACK); + g.drawRect((int)(_startPosX+35), (int)(_startPosY+20), 10, 10); + g.drawRect((int)(_startPosX+55), (int)(_startPosY+20), 20, 10); + g.drawRect((int)(_startPosX+75), (int)(_startPosY+15), 25, 15); + g.drawRect((int)(_startPosX+100), (int)(_startPosY+20), 25, 10); + } + + } +} \ No newline at end of file diff --git a/ContainerShip/src/containership/DrawingDecks.java b/ContainerShip/src/containership/DrawingDecks.java index 65086b6..17c0ea0 100644 --- a/ContainerShip/src/containership/DrawingDecks.java +++ b/ContainerShip/src/containership/DrawingDecks.java @@ -11,15 +11,17 @@ import java.awt.Color; * * @author ateks */ -public class DrawingDecks { +public class DrawingDecks implements IDrawingObjectAdd { private Decks decks = null; private int numberOfDecks; + @Override public int getNumberOfDecks() { return numberOfDecks; } + @Override public void setNumberOfDecks(int numberOfDecks) { if (numberOfDecks != 1 && numberOfDecks != 2 && numberOfDecks != 3) { return; @@ -38,27 +40,15 @@ public class DrawingDecks { } } + @Override public void drawBottomDeck(Graphics2D g, int _startPosX, int _startPosY, Color bodyColor) { - int[] curcuitY - = { - _startPosY + 30, - _startPosY + 30, - _startPosY + 55, - _startPosY + 55, - _startPosY + 30,}; - int[] curcuitX - = { - _startPosX + 100, - _startPosX + 135, - _startPosX + 115, - _startPosX + 20, - _startPosX,}; g.setColor(bodyColor); - g.fillPolygon(curcuitX, curcuitY, curcuitX.length); + g.fillRect(_startPosX, _startPosY + 30, 135, 25); g.setColor(Color.BLACK); - g.drawPolygon(curcuitX, curcuitY, curcuitX.length); + g.drawRect(_startPosX, _startPosY + 30, 135, 25); } + @Override public void drawMiddleDeck(Graphics2D g, int _startPosX, int _startPosY, Color bodyColor) { g.setColor(bodyColor); g.fillRect(_startPosX+30, _startPosY+15, 70, 15); @@ -66,6 +56,7 @@ public class DrawingDecks { g.drawRect(_startPosX+30, _startPosY+15, 70, 15 ); } + @Override public void drawUpperDeck(Graphics2D g, int _startPosX, int _startPosY, Color bodyColor) { g.setColor(bodyColor); g.fillRect(_startPosX+50, _startPosY, 45, 15 ); @@ -75,6 +66,7 @@ public class DrawingDecks { } + @Override public void drawDecks(Graphics2D g, int _startPosX, int _startPosY, Color bodyColor) { if (decks == null) { return; @@ -88,6 +80,5 @@ public class DrawingDecks { drawBottomDeck(g, _startPosX, _startPosY, bodyColor); break; } - } -} +} \ No newline at end of file diff --git a/ContainerShip/src/containership/DrawingDecksReversedTr.java b/ContainerShip/src/containership/DrawingDecksReversedTr.java new file mode 100644 index 0000000..f7bc08a --- /dev/null +++ b/ContainerShip/src/containership/DrawingDecksReversedTr.java @@ -0,0 +1,123 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template + */ +package containership; + +import java.awt.Color; +import java.awt.Graphics2D; + +/** + * + * @author ateks + */ +public class DrawingDecksReversedTr implements IDrawingObjectAdd { + + private Decks decks = null; + private int numberOfDecks; + + @Override + public int getNumberOfDecks() { + return numberOfDecks; + } + + @Override + public void setNumberOfDecks(int numberOfDecks) { + if (numberOfDecks != 1 && numberOfDecks != 2 && numberOfDecks != 3){ + return; + } + this.numberOfDecks = numberOfDecks; + switch (this.numberOfDecks) { + case 1: + decks = Decks.ONE; + break; + case 2: + decks = Decks.TWO; + break; + case 3: + decks = Decks.THREE; + break; + } + } + + @Override + public void drawDecks(Graphics2D g, int _startPosX, int _startPosY, Color bodyColor) { + if (decks == null) { + return; + } + switch (decks) { + case THREE: + drawUpperDeck(g, _startPosX, _startPosY, bodyColor); + case TWO: + drawMiddleDeck(g, _startPosX, _startPosY, bodyColor); + case ONE: + drawBottomDeck(g, _startPosX, _startPosY, bodyColor); + break; + } + } + + @Override + public void drawBottomDeck(Graphics2D g, int _startPosX, int _startPosY, Color bodyColor) { + int[] curcuitY + = { + _startPosY + 30, + _startPosY + 30, + _startPosY + 55, + _startPosY + 55,}; + int[] curcuitX + = { + _startPosX + 5, + _startPosX + 130, + _startPosX + 135, + _startPosX,}; + g.setColor(bodyColor); + g.fillPolygon(curcuitX, curcuitY, curcuitX.length); + g.setColor(Color.BLACK); + g.drawPolygon(curcuitX, curcuitY, curcuitX.length); + } + + @Override + public void drawMiddleDeck(Graphics2D g, int _startPosX, int _startPosY, Color bodyColor) { + g.setColor(bodyColor); + int[] curcuitY + = { + _startPosY + 30, + _startPosY + 15, + _startPosY + 15, + _startPosY + 30, + } ; + int[] curcuitX + = { + _startPosX + 30, + _startPosX + 40, + _startPosX + 90, + _startPosX + 100, + } ; + g.fillPolygon(curcuitX, curcuitY, curcuitX.length); + g.setColor(Color.BLACK); + g.drawPolygon(curcuitX, curcuitY, curcuitX.length); + } + + @Override + public void drawUpperDeck(Graphics2D g, int _startPosX, int _startPosY, Color bodyColor) { + g.setColor(bodyColor); + int[] curcuitY + = { + _startPosY + 15, + _startPosY, + _startPosY, + _startPosY + 15, + } ; + int[] curcuitX + = { + _startPosX + 50, + _startPosX + 55, + _startPosX + 80, + _startPosX + 85, + } ; + g.fillPolygon(curcuitX, curcuitY, curcuitX.length); + g.setColor(Color.BLACK); + g.drawPolygon(curcuitX, curcuitY, curcuitX.length); + } + +} \ No newline at end of file diff --git a/ContainerShip/src/containership/DrawingDecksTropec.java b/ContainerShip/src/containership/DrawingDecksTropec.java new file mode 100644 index 0000000..78b46d6 --- /dev/null +++ b/ContainerShip/src/containership/DrawingDecksTropec.java @@ -0,0 +1,125 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template + */ +package containership; + +import java.awt.Color; +import java.awt.Graphics2D; + +/** + * + * @author ateks + */ +public class DrawingDecksTropec implements IDrawingObjectAdd { + + private Decks decks = null; + private int numberOfDecks; + + @Override + public int getNumberOfDecks() { + return numberOfDecks; + } + + @Override + public void setNumberOfDecks(int numberOfDecks) { + if (numberOfDecks != 1 && numberOfDecks != 2 && numberOfDecks != 3){ + return; + } + this.numberOfDecks = numberOfDecks; + switch (this.numberOfDecks) { + case 1: + decks = Decks.ONE; + break; + case 2: + decks = Decks.TWO; + break; + case 3: + decks = Decks.THREE; + break; + } + } + + @Override + public void drawDecks(Graphics2D g, int _startPosX, int _startPosY, Color bodyColor) { + if (decks == null) { + return; + } + switch (decks) { + case THREE: + drawUpperDeck(g, _startPosX, _startPosY, bodyColor); + case TWO: + drawMiddleDeck(g, _startPosX, _startPosY, bodyColor); + case ONE: + drawBottomDeck(g, _startPosX, _startPosY, bodyColor); + break; + } + } + + @Override + public void drawBottomDeck(Graphics2D g, int _startPosX, int _startPosY, Color bodyColor) { + int[] curcuitY + = { + _startPosY + 30, + _startPosY + 30, + _startPosY + 55, + _startPosY + 55, + _startPosY + 30,}; + int[] curcuitX + = { + _startPosX + 100, + _startPosX + 135, + _startPosX + 115, + _startPosX + 20, + _startPosX,}; + g.setColor(bodyColor); + g.fillPolygon(curcuitX, curcuitY, curcuitX.length); + g.setColor(Color.BLACK); + g.drawPolygon(curcuitX, curcuitY, curcuitX.length); + } + + @Override + public void drawMiddleDeck(Graphics2D g, int _startPosX, int _startPosY, Color bodyColor) { + g.setColor(bodyColor); + int[] curcuitY + = { + _startPosY + 15, + _startPosY + 30, + _startPosY + 30, + _startPosY + 15, + } ; + int[] curcuitX + = { + _startPosX + 30, + _startPosX + 40, + _startPosX + 90, + _startPosX + 100, + } ; + g.fillPolygon(curcuitX, curcuitY, curcuitX.length); + g.setColor(Color.BLACK); + g.drawPolygon(curcuitX, curcuitY, curcuitX.length); + } + + @Override + public void drawUpperDeck(Graphics2D g, int _startPosX, int _startPosY, Color bodyColor) { + g.setColor(bodyColor); + int[] curcuitY + = { + _startPosY, + _startPosY + 15, + _startPosY + 15, + _startPosY, + } ; + int[] curcuitX + = { + _startPosX + 50, + _startPosX + 55, + _startPosX + 90, + _startPosX + 95, + } ; + g.fillPolygon(curcuitX, curcuitY, curcuitX.length); + g.setColor(Color.BLACK); + g.drawPolygon(curcuitX, curcuitY, curcuitX.length); + } + +} diff --git a/ContainerShip/src/containership/DrawingObjectShip.java b/ContainerShip/src/containership/DrawingObjectShip.java new file mode 100644 index 0000000..b03280b --- /dev/null +++ b/ContainerShip/src/containership/DrawingObjectShip.java @@ -0,0 +1,58 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template + */ +package containership; +import java.awt.*; + +/** + * + * @author ateks + */ +public class DrawingObjectShip implements IDrawingObject { + private DrawingShip _ship = null; + + public DrawingObjectShip(DrawingShip ship) + { + _ship = ship; + Step = _ship.Ship.Step; + } + + public float Step; + @Override + public float getStep(){ + return Step; + } + + @Override + public float[] GetCurrentPosition() + { + if (_ship == null) return null; + return _ship.GetCurrentPosition(); + } + @Override + public void MoveObject(Direction direction) + { + _ship.MoveTransport(direction); + } + @Override + public void SetObject(int x, int y, int width, int height) + { + _ship.SetPosition(x, y, width, height); + } + + @Override + public void DrawingObject(Graphics2D g) + { + if (_ship == null) return; + if (_ship instanceof DrawingContainerShip containerShip) + { + containerShip.DrawTransport(g); + } + else + { + _ship.DrawTransport(g); + } + } +} + diff --git a/ContainerShip/src/containership/DrawingShip.java b/ContainerShip/src/containership/DrawingShip.java index d5057ec..033052b 100644 --- a/ContainerShip/src/containership/DrawingShip.java +++ b/ContainerShip/src/containership/DrawingShip.java @@ -10,22 +10,47 @@ import java.util.Random; * @author ateks */ public class DrawingShip { - private EntityShip Ship; + public EntityShip Ship; //отрисовка палуб - public DrawingDecks drawingDecks; + public IDrawingObjectAdd drawingDecks; public float _startPosX; public float _startPosY; private Integer _pictureWidth = null; private Integer _pictureHeight = null; - private static final int _shipWidth = 135; - private static final int _shipHeight = 40; + private static int _shipWidth = 135; + private static int _shipHeight = 55; - public void Init(int speed, float weight, Color bodyColor, int numberOfDecks){ - Ship = new EntityShip(); - // мметод прорисовки палуб - drawingDecks = new DrawingDecks(); - Ship.Init(speed, weight, bodyColor); - drawingDecks.setNumberOfDecks(numberOfDecks);/////// палубы + public DrawingShip(int speed, float weight, Color bodyColor, DecksType decksType, int numberOfDecks){ + Ship = new EntityShip(speed, weight, bodyColor); + switch(decksType){ + case RECTANGLE: + drawingDecks = new DrawingDecks(); + break; + case TROPEC: + drawingDecks = new DrawingDecksTropec(); + break; + case REVERSEDTR: + drawingDecks = new DrawingDecksReversedTr(); + break; + } + drawingDecks.setNumberOfDecks(numberOfDecks);//палубы + } + public DrawingShip(int speed, float weight, Color bodyColor, DecksType decksType, int numberOfDecks, int width, int height){ + Ship = new EntityShip(speed, weight, bodyColor); + _shipWidth = width; + _shipHeight = height; + switch(decksType){ + case RECTANGLE: + drawingDecks = new DrawingDecks(); + break; + case TROPEC: + drawingDecks = new DrawingDecksTropec(); + break; + case REVERSEDTR: + drawingDecks = new DrawingDecksReversedTr(); + break; + } + drawingDecks.setNumberOfDecks(numberOfDecks);//палубы } public EntityShip getShip() { @@ -109,4 +134,13 @@ public class DrawingShip { _startPosY = _pictureHeight - _shipHeight; } } -} + public float [] GetCurrentPosition() + { + float[] pos = new float[4]; + pos[0] = _startPosX; + pos[1] = _startPosY; + pos[2] = _startPosX + _shipWidth; + pos[3] = _startPosY + _shipHeight; + return pos; + } +} \ No newline at end of file diff --git a/ContainerShip/src/containership/EntityContainerShip.java b/ContainerShip/src/containership/EntityContainerShip.java new file mode 100644 index 0000000..d4a6c47 --- /dev/null +++ b/ContainerShip/src/containership/EntityContainerShip.java @@ -0,0 +1,36 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template + */ +package containership; +import java.awt.*; +/** + * + * @author ateks + */ +public class EntityContainerShip extends EntityShip { + private Color DopColor; + private boolean containers; + private boolean crane; + + + public Color getDopColor() { + return DopColor; + } + + public boolean getContainers(){ + return containers; + } + + public boolean getCrane(){ + return crane; + } + + public EntityContainerShip(int speed, float weight, Color bodyColor, Color dopColor, boolean containers, boolean crane) + { + super(speed, weight, bodyColor); + DopColor = dopColor; + this.containers = containers; + this.crane = crane; + } +} diff --git a/ContainerShip/src/containership/EntityShip.java b/ContainerShip/src/containership/EntityShip.java index dbc2590..d1a7513 100644 --- a/ContainerShip/src/containership/EntityShip.java +++ b/ContainerShip/src/containership/EntityShip.java @@ -30,9 +30,7 @@ public class EntityShip { return BodyColor; } - - - public void Init(int speed, float weight, Color bodyColor){ + public EntityShip(int speed, float weight, Color bodyColor){ Random rnd = new Random(); Speed = speed <= 0 ? rnd.nextInt(50, 150) : speed; Weight = weight <= 0 ? rnd.nextInt(40, 70) : weight; diff --git a/ContainerShip/src/containership/IDrawingObject.java b/ContainerShip/src/containership/IDrawingObject.java new file mode 100644 index 0000000..cff20e2 --- /dev/null +++ b/ContainerShip/src/containership/IDrawingObject.java @@ -0,0 +1,40 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Interface.java to edit this template + */ +package containership; +import java.awt.*; +/** + * + * @author ateks + */ +public interface IDrawingObject { + /// + /// Шаг перемещения объекта + /// + public float getStep(); + /// + /// Установка позиции объекта + /// + /// Координата X + /// Координата Y + /// Ширина полотна + /// Высота полотна + void SetObject(int x, int y, int width, int height); + /// + /// Изменение направления пермещения объекта + /// + /// Направление + /// + void MoveObject(Direction direction); + /// + /// Отрисовка объекта + /// + /// + void DrawingObject(Graphics2D g); + /// + /// Получение текущей позиции объекта + /// + /// + float[] GetCurrentPosition(); +} diff --git a/ContainerShip/src/containership/IDrawingObjectAdd.java b/ContainerShip/src/containership/IDrawingObjectAdd.java new file mode 100644 index 0000000..630a1f9 --- /dev/null +++ b/ContainerShip/src/containership/IDrawingObjectAdd.java @@ -0,0 +1,18 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Interface.java to edit this template + */ +package containership; +import java.awt.*; +/** + * + * @author ateks + */ +public interface IDrawingObjectAdd { + public int getNumberOfDecks(); + void setNumberOfDecks(int numberOfDecks); + void drawDecks(Graphics2D g, int _startPosX, int _startPosY, Color bodyColor); + public void drawBottomDeck(Graphics2D g, int _startPosX, int _startPosY, Color bodyColor); + public void drawMiddleDeck(Graphics2D g, int _startPosX, int _startPosY, Color bodyColor); + public void drawUpperDeck(Graphics2D g, int _startPosX, int _startPosY, Color bodyColor); +} \ No newline at end of file diff --git a/ContainerShip/src/containership/JFrame_Map.form b/ContainerShip/src/containership/JFrame_Map.form new file mode 100644 index 0000000..fc1b597 --- /dev/null +++ b/ContainerShip/src/containership/JFrame_Map.form @@ -0,0 +1,228 @@ + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/ContainerShip/src/containership/JFrame_Map.java b/ContainerShip/src/containership/JFrame_Map.java new file mode 100644 index 0000000..0eb5e4a --- /dev/null +++ b/ContainerShip/src/containership/JFrame_Map.java @@ -0,0 +1,308 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/GUIForms/JFrame.java to edit this template + */ +package containership; + +import java.awt.Color; +import java.util.Random; +import javax.swing.*; + +/** + * + * @author ateks + */ +public class JFrame_Map extends javax.swing.JFrame { + + /** + * Creates new form JFrameMap + */ + public JFrame_Map() { + initComponents(); + } + private DrawingShip _ship; + private AbstractMap _abstractMap = new SimpleMap(); + /** + * This method is called from within the constructor to initialize the form. + * WARNING: Do NOT modify this code. The content of this method is always + * regenerated by the Form Editor. + */ + @SuppressWarnings("unchecked") + // //GEN-BEGIN:initComponents + private void initComponents() { + + ShipCanvas = new containership.MyCanvas(); + labelAmountOfDecks = new javax.swing.JLabel(); + comboBoxAmountOfDecks = new javax.swing.JComboBox<>(); + labelTypeOfDecks = new javax.swing.JLabel(); + comboBoxTypeOfDecks = new javax.swing.JComboBox<>(); + buttonCreate = new javax.swing.JButton(); + buttonModify = new javax.swing.JButton(); + buttonLeft = new javax.swing.JButton(); + buttonUp = new javax.swing.JButton(); + buttonRight = new javax.swing.JButton(); + buttonDown = new javax.swing.JButton(); + comboBoxMap = new javax.swing.JComboBox<>(); + statusLabel = new javax.swing.JLabel(); + + setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); + + labelAmountOfDecks.setText("Количество палуб"); + + comboBoxAmountOfDecks.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Одна", "Две", "Три" })); + + labelTypeOfDecks.setText("Вид палуб"); + + comboBoxTypeOfDecks.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Квадраты", "Трапеции", "Перевернутые трапеции" })); + + buttonCreate.setText("Создать"); + buttonCreate.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + buttonCreateActionPerformed(evt); + } + }); + + buttonModify.setText("Модификация"); + buttonModify.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + buttonModifyActionPerformed(evt); + } + }); + + buttonLeft.setIcon(new javax.swing.ImageIcon(getClass().getResource("/containership/LeftArrow.png"))); // NOI18N + buttonLeft.setName("buttonLeft"); // NOI18N + buttonLeft.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + moveButtonActionPerformed(evt); + } + }); + + buttonUp.setIcon(new javax.swing.ImageIcon(getClass().getResource("/containership/UpArrow.png"))); // NOI18N + buttonUp.setToolTipText(""); + buttonUp.setName("buttonUp"); // NOI18N + buttonUp.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + moveButtonActionPerformed(evt); + } + }); + + buttonRight.setIcon(new javax.swing.ImageIcon(getClass().getResource("/containership/Rightarrow.png"))); // NOI18N + buttonRight.setName("buttonRight"); // NOI18N + buttonRight.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + moveButtonActionPerformed(evt); + } + }); + + buttonDown.setIcon(new javax.swing.ImageIcon(getClass().getResource("/containership/DownArrow.png"))); // NOI18N + buttonDown.setName("buttonDown"); // NOI18N + buttonDown.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + moveButtonActionPerformed(evt); + } + }); + + comboBoxMap.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Простая карта", "Модифицированная карта" })); + comboBoxMap.addItemListener(new java.awt.event.ItemListener() { + public void itemStateChanged(java.awt.event.ItemEvent evt) { + comboBoxMapItemStateChanged(evt); + } + }); + + statusLabel.setText("Скорость: Вес: Цвет: Палубы:"); + + javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); + getContentPane().setLayout(layout); + layout.setHorizontalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addComponent(ShipCanvas, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addGroup(layout.createSequentialGroup() + .addContainerGap() + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addComponent(labelAmountOfDecks) + .addComponent(buttonCreate, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addComponent(comboBoxAmountOfDecks, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) + .addGap(18, 18, 18) + .addComponent(labelTypeOfDecks) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(comboBoxTypeOfDecks, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addComponent(buttonUp, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)) + .addGroup(layout.createSequentialGroup() + .addComponent(buttonModify) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(comboBoxMap, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 58, Short.MAX_VALUE) + .addComponent(buttonLeft, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(buttonDown, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE))) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(buttonRight, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE) + .addGap(21, 21, 21)) + .addComponent(statusLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + ); + layout.setVerticalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addComponent(ShipCanvas, javax.swing.GroupLayout.PREFERRED_SIZE, 407, javax.swing.GroupLayout.PREFERRED_SIZE) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addGap(23, 23, 23) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) + .addComponent(labelAmountOfDecks) + .addComponent(comboBoxAmountOfDecks, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) + .addComponent(labelTypeOfDecks) + .addComponent(comboBoxTypeOfDecks, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) + .addComponent(buttonModify) + .addComponent(buttonCreate) + .addComponent(comboBoxMap, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) + .addGroup(layout.createSequentialGroup() + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 12, Short.MAX_VALUE) + .addComponent(buttonUp, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) + .addComponent(buttonLeft, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE) + .addComponent(buttonRight, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE) + .addComponent(buttonDown, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)) + .addGap(25, 25, 25))) + .addComponent(statusLabel) + .addContainerGap()) + ); + + pack(); + }// //GEN-END:initComponents + + private void moveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_moveButtonActionPerformed + if (_ship == null) return; + String name = ((JButton) evt.getSource()).getName(); + Direction dir = Direction.NONE; + switch (name) + { + case "buttonUp": + dir = Direction.UP; + break; + case "buttonDown": + dir = Direction.DOWN; + break; + case "buttonLeft": + dir = Direction.LEFT; + break; + case "buttonRight": + dir = Direction.RIGHT; + break; + } + ShipCanvas.getGraphics().drawImage(_abstractMap.MoveObject(dir), 0, 0, null); + }//GEN-LAST:event_moveButtonActionPerformed + + private void comboBoxMapItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_comboBoxMapItemStateChanged + switch (comboBoxMap.getSelectedIndex()){ + case 0: + _abstractMap = new SimpleMap(); + break; + case 1: + _abstractMap = new ModifyMap(); + break; + } + }//GEN-LAST:event_comboBoxMapItemStateChanged + + private void buttonCreateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonCreateActionPerformed + Random rnd = new Random(); + DecksType selectedDecksType = null; + switch(comboBoxTypeOfDecks.getSelectedItem().toString()){ + case "Квадраты": + selectedDecksType = DecksType.RECTANGLE; + break; + case "Трапеции": + selectedDecksType = DecksType.TROPEC; + break; + case "Перевернутые трапеции": + selectedDecksType = DecksType.REVERSEDTR; + break; + } + _ship = new DrawingShip(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000), new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)), selectedDecksType, (comboBoxAmountOfDecks.getSelectedIndex() + 1)); + SetData(); + }//GEN-LAST:event_buttonCreateActionPerformed + + private void buttonModifyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonModifyActionPerformed + Random rnd = new Random(); + DecksType selectedDecksType = null; + switch(comboBoxTypeOfDecks.getSelectedItem().toString()){ + case "Квадраты": + selectedDecksType = DecksType.RECTANGLE; + break; + case "Трапеции": + selectedDecksType = DecksType.TROPEC; + break; + case "Перевернутые трапеции": + selectedDecksType = DecksType.REVERSEDTR; + break; + } + _ship = new DrawingContainerShip(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000), new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)), selectedDecksType, (comboBoxAmountOfDecks.getSelectedIndex() + 1), new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)), rnd.nextBoolean(), rnd.nextBoolean()); + SetData(); + }//GEN-LAST:event_buttonModifyActionPerformed + + private void SetData(){ + Random rnd = new Random(); + _ship.SetPosition(rnd.nextInt(10, 100), rnd.nextInt(10, 100), ShipCanvas.getWidth(), ShipCanvas.getHeight()); + statusLabel.setText("Скорость: " + _ship.getShip().getSpeed() + " Вес: " + (int) _ship.getShip().getWeight() + " Цвет: " + _ship.getShip().getBodyColor() + " Палубы: " + _ship.drawingDecks.getNumberOfDecks()); + ShipCanvas.setShip(_ship); + ShipCanvas.getGraphics().drawImage(_abstractMap.CreateMap(ShipCanvas.getWidth() + 6, ShipCanvas.getHeight() + 3, new DrawingObjectShip(_ship)), 0, 0, null); + } + /** + * @param args the command line arguments + */ + public static void main(String args[]) { + /* Set the Nimbus look and feel */ + // + /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. + * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html + */ + try { + for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { + if ("Nimbus".equals(info.getName())) { + javax.swing.UIManager.setLookAndFeel(info.getClassName()); + break; + } + } + } catch (ClassNotFoundException ex) { + java.util.logging.Logger.getLogger(JFrame_Map.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } catch (InstantiationException ex) { + java.util.logging.Logger.getLogger(JFrame_Map.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } catch (IllegalAccessException ex) { + java.util.logging.Logger.getLogger(JFrame_Map.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } catch (javax.swing.UnsupportedLookAndFeelException ex) { + java.util.logging.Logger.getLogger(JFrame_Map.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } + // + // + + /* Create and display the form */ + java.awt.EventQueue.invokeLater(new Runnable() { + public void run() { + new JFrame_Map().setVisible(true); + } + }); + } + + // Variables declaration - do not modify//GEN-BEGIN:variables + private containership.MyCanvas ShipCanvas; + private javax.swing.JButton buttonCreate; + private javax.swing.JButton buttonDown; + private javax.swing.JButton buttonLeft; + private javax.swing.JButton buttonModify; + private javax.swing.JButton buttonRight; + private javax.swing.JButton buttonUp; + private javax.swing.JComboBox comboBoxAmountOfDecks; + private javax.swing.JComboBox comboBoxMap; + private javax.swing.JComboBox comboBoxTypeOfDecks; + private javax.swing.JLabel labelAmountOfDecks; + private javax.swing.JLabel labelTypeOfDecks; + private javax.swing.JLabel statusLabel; + // End of variables declaration//GEN-END:variables +} diff --git a/ContainerShip/src/containership/JFrame_Ship.form b/ContainerShip/src/containership/JFrame_Ship.form index 07daf13..9cb3c1d 100644 --- a/ContainerShip/src/containership/JFrame_Ship.form +++ b/ContainerShip/src/containership/JFrame_Ship.form @@ -30,13 +30,20 @@ - + + + + + + + + @@ -70,9 +77,14 @@ + + - + + + + @@ -181,5 +193,35 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/ContainerShip/src/containership/JFrame_Ship.java b/ContainerShip/src/containership/JFrame_Ship.java index 37873bf..81ffdd3 100644 --- a/ContainerShip/src/containership/JFrame_Ship.java +++ b/ContainerShip/src/containership/JFrame_Ship.java @@ -38,6 +38,9 @@ public class JFrame_Ship extends javax.swing.JFrame { buttonDown = new javax.swing.JButton(); buttonRight = new javax.swing.JButton(); buttonUp = new javax.swing.JButton(); + labelTypeOfDecks = new javax.swing.JLabel(); + comboBoxTypeOfDecks = new javax.swing.JComboBox<>(); + buttonModify = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); @@ -93,6 +96,17 @@ public class JFrame_Ship extends javax.swing.JFrame { } }); + labelTypeOfDecks.setText("Вид палуб"); + + comboBoxTypeOfDecks.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Квадраты", "Трапеции", "Перевернутые трапеции" })); + + buttonModify.setText("Модификация"); + buttonModify.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + buttonModifyActionPerformed(evt); + } + }); + javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( @@ -104,12 +118,19 @@ public class JFrame_Ship extends javax.swing.JFrame { .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(buttonCreate) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 381, Short.MAX_VALUE) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(buttonModify) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(buttonLeft, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup() .addComponent(labelAmountOfDecks) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) - .addComponent(comboBoxAmountOfDecks, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))) + .addComponent(comboBoxAmountOfDecks, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(labelTypeOfDecks) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(comboBoxTypeOfDecks, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE) + .addGap(0, 106, Short.MAX_VALUE))) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() @@ -133,9 +154,13 @@ public class JFrame_Ship extends javax.swing.JFrame { .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(labelAmountOfDecks) - .addComponent(comboBoxAmountOfDecks, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) + .addComponent(comboBoxAmountOfDecks, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) + .addComponent(labelTypeOfDecks) + .addComponent(comboBoxTypeOfDecks, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) - .addComponent(buttonCreate)) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) + .addComponent(buttonCreate) + .addComponent(buttonModify))) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addComponent(buttonUp, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) @@ -149,17 +174,27 @@ public class JFrame_Ship extends javax.swing.JFrame { ); labelAmountOfDecks.getAccessibleContext().setAccessibleName(""); + labelTypeOfDecks.getAccessibleContext().setAccessibleName(""); pack(); }// //GEN-END:initComponents private void buttonCreateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonCreateActionPerformed Random rnd = new Random(); - _ship = new DrawingShip(); - _ship.Init(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000), new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)), (comboBoxAmountOfDecks.getSelectedIndex() + 1)); - _ship.SetPosition(rnd.nextInt(10, 100), rnd.nextInt(10, 100), ShipCanvas.getWidth(), ShipCanvas.getHeight()); - statusLabel.setText("Скорость: " + _ship.getShip().getSpeed() + " Вес: " + (int) _ship.getShip().getWeight() + " Цвет: " + _ship.getShip().getBodyColor() + " Палубы: " + _ship.drawingDecks.getNumberOfDecks()); - ShipCanvas.setShip(_ship); + DecksType selectedDecksType = null; + switch(comboBoxTypeOfDecks.getSelectedItem().toString()){ + case "Квадраты": + selectedDecksType = DecksType.RECTANGLE; + break; + case "Трапеции": + selectedDecksType = DecksType.TROPEC; + break; + case "Перевернутые трапеции": + selectedDecksType = DecksType.REVERSEDTR; + break; + } + _ship = new DrawingShip(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000), new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)), selectedDecksType, (comboBoxAmountOfDecks.getSelectedIndex() + 1)); + SetData(); Draw(); }//GEN-LAST:event_buttonCreateActionPerformed @@ -189,9 +224,35 @@ public class JFrame_Ship extends javax.swing.JFrame { _ship.ChangeBorders(ShipCanvas.getWidth(), ShipCanvas.getHeight()); }//GEN-LAST:event_ShipCanvasComponentResized + private void buttonModifyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonModifyActionPerformed + Random rnd = new Random(); + DecksType selectedDecksType = null; + switch(comboBoxTypeOfDecks.getSelectedItem().toString()){ + case "Квадраты": + selectedDecksType = DecksType.RECTANGLE; + break; + case "Трапеции": + selectedDecksType = DecksType.TROPEC; + break; + case "Перевернутые трапеции": + selectedDecksType = DecksType.REVERSEDTR; + break; + } + _ship = new DrawingContainerShip(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000), new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)), selectedDecksType, (comboBoxAmountOfDecks.getSelectedIndex() + 1), new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)), rnd.nextBoolean(), rnd.nextBoolean()); + SetData(); + Draw(); + }//GEN-LAST:event_buttonModifyActionPerformed + private void Draw(){ ShipCanvas.repaint(); } + + private void SetData(){ + Random rnd = new Random(); + _ship.SetPosition(rnd.nextInt(10, 100), rnd.nextInt(10, 100), ShipCanvas.getWidth(), ShipCanvas.getHeight()); + statusLabel.setText("Скорость: " + _ship.getShip().getSpeed() + " Вес: " + (int) _ship.getShip().getWeight() + " Цвет: " + _ship.getShip().getBodyColor() + " Палубы: " + _ship.drawingDecks.getNumberOfDecks()); + ShipCanvas.setShip(_ship); + } /** * @param args the command line arguments */ @@ -222,7 +283,7 @@ public class JFrame_Ship extends javax.swing.JFrame { /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { - new JFrame_Ship().setVisible(true); + new JFrame_Map().setVisible(true); } }); } @@ -232,10 +293,13 @@ public class JFrame_Ship extends javax.swing.JFrame { private javax.swing.JButton buttonCreate; private javax.swing.JButton buttonDown; private javax.swing.JButton buttonLeft; + private javax.swing.JButton buttonModify; private javax.swing.JButton buttonRight; private javax.swing.JButton buttonUp; private javax.swing.JComboBox comboBoxAmountOfDecks; + private javax.swing.JComboBox comboBoxTypeOfDecks; private javax.swing.JLabel labelAmountOfDecks; + private javax.swing.JLabel labelTypeOfDecks; private javax.swing.JLabel statusLabel; // End of variables declaration//GEN-END:variables } diff --git a/ContainerShip/src/containership/ModifyMap.java b/ContainerShip/src/containership/ModifyMap.java new file mode 100644 index 0000000..13eca7e --- /dev/null +++ b/ContainerShip/src/containership/ModifyMap.java @@ -0,0 +1,61 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template + */ +package containership; +import java.awt.*; +/** + * + * @author ateks + */ +public class ModifyMap extends AbstractMap { + private final Color barrierColor = Color.GRAY; + private final Color roadColor = Color.BLUE; + + @Override + protected void DrawBarrierPart(Graphics g, int i, int j) + { + g.setColor(barrierColor); + g.fillRect(i * _size_x, j * _size_y, _size_x, _size_y); + g.setColor(Color.BLACK); + } + @Override + protected void DrawRoadPart(Graphics g, int i, int j) + { + g.setColor(roadColor); + g.fillRect(i * _size_x, j * _size_y, _size_x, _size_y); + g.setColor(Color.BLACK); + } + @Override + protected void GenerateMap() + { + int pointY; + int firstPointX; + int secondPointX; + _map = new int[100][100]; + _size_x = _width / _map.length; + _size_y = _height / _map[0].length; + int counter = 0; + while(counter < 15) + { + boolean freePlace = true; + firstPointX = _random.nextInt(0, _map.length-10); + secondPointX = _random.nextInt(firstPointX, _map.length-10); + pointY = _random.nextInt(0, _map[0].length-10); + for(int i = firstPointX; i + /// Цвет участка закрытого + /// + private final Color barrierColor = Color.BLACK; + /// + /// Цвет участка открытого + /// + private final Color roadColor = Color.GRAY; + + @Override + protected void DrawBarrierPart(Graphics g, int i, int j) + { + g.setColor(barrierColor); + g.fillRect(i * _size_x, j * _size_y, _size_x, _size_y); + g.setColor(Color.BLACK); + } + @Override + protected void DrawRoadPart(Graphics g, int i, int j) + { + g.setColor(roadColor); + g.fillRect(i * _size_x, j * _size_y, _size_x, _size_y); + g.setColor(Color.BLACK); + } + @Override + protected void GenerateMap() + { + _map = new int[100][100]; + _size_x = _width / _map.length; + _size_y = _height / _map[0].length; + int counter = 0; + for (int i = 0; i < _map.length; ++i) + { + for (int j = 0; j < _map[0].length; ++j) + { + _map[i][j] = _freeRoad; + } + } + while (counter < 50) + { + int x = _random.nextInt(0, 100); + int y = _random.nextInt(0, 100); + if (_map[x][y] == _freeRoad) + { + _map[x][y] = _barrier; + counter++; + } + } + } +}