Compare commits

...

5 Commits

Author SHA1 Message Date
2442b008f6 full 3 2022-12-23 12:21:55 +04:00
defe5faee7 full 2022-12-23 10:22:06 +04:00
78d063574e FULL 2022-12-05 11:12:21 +04:00
57b8cbca08 2 2022-12-05 02:29:28 +04:00
3e05b1b31e full 1 2022-12-05 00:55:23 +04:00
32 changed files with 1678 additions and 2 deletions

View File

@ -2,7 +2,9 @@
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<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="sourceFolder" forTests="false" />
</component>

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<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" />
</component>
</project>

115
AbstractMap.java Normal file
View File

@ -0,0 +1,115 @@
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;
public abstract class AbstractMap {
private IDrawningObject _drawningObject = null;
protected int[][] _map = null;
protected int _width;
protected int _height;
protected float _size_x;
protected float _size_y;
protected final Random _random = new Random();
protected final int _freeRoad = 0;
protected final int _barrier = 1;
public BufferedImage CreateMap(int width, int height, IDrawningObject drawningObject)
{
_width = width;
_height = height;
_drawningObject = drawningObject;
GenerateMap();
while (!SetObjectOnMap())
{
GenerateMap();
}
return DrawMapWithObject();
}
private boolean CheckBarriers(float topOffset, float rightOffset, float leftOffset, float bottomOffset)
{
float[] arrayPossition = _drawningObject.GetCurrentPosition();
int top = (int)((arrayPossition[1] + topOffset) / _size_y);
int right = (int)((arrayPossition[2] + rightOffset) / _size_x);
int left = (int)((arrayPossition[0] + leftOffset) / _size_x);
int bottom = (int)((arrayPossition[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] == _barrier) return false;
}
}
return true;
}
public BufferedImage MoveObject(Direction direction)
{
if (_drawningObject == null) return DrawMapWithObject();
boolean isTrue = true;
switch (direction)
{
case Left:
if (!CheckBarriers(0, -1 * _drawningObject.Step(), -1 * _drawningObject.Step(), 0)) isTrue = false;
break;
case Right:
if (!CheckBarriers(0, _drawningObject.Step(), _drawningObject.Step(), 0)) isTrue = false;
break;
case Up:
if (!CheckBarriers(-1 * _drawningObject.Step(), 0, 0, -1 * _drawningObject.Step())) isTrue = false;
break;
case Down:
if (!CheckBarriers(_drawningObject.Step(), 0, 0, _drawningObject.Step())) isTrue = false;
break;
}
if (isTrue)
{
_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 (!CheckBarriers(0, 0, 0, 0)) 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[0].length; ++j)
{
if (_map[i][j] == _freeRoad)
{
DrawRoadPart((Graphics2D) gr, i, j);
}
else if (_map[i][j] == _barrier)
{
DrawBarrierPart((Graphics2D) gr, i, j);
}
}
}
_drawningObject.DrawningObject(gr);
return bmp;
}
protected abstract void GenerateMap();
protected abstract void DrawRoadPart(Graphics2D g, int i, int j);
protected abstract void DrawBarrierPart(Graphics2D g, int i, int j);
}

8
Direction.java Normal file
View File

@ -0,0 +1,8 @@
public enum Direction {
Up(1),
Down(2),
Left(3),
Right(4),
None(0);
Direction(int value){}
}

166
DrawningBoat.java Normal file
View File

@ -0,0 +1,166 @@
import javax.swing.*;
import java.awt.*;
import java.util.Random;
public class DrawningBoat extends JPanel {
EntityBoat Boat;
public EntityBoat GetBoat(){
return Boat;
}
int _startPosX;
int _startPosY;
public IDrawningOars OarsDraw;
public Integer _pictureWidth = null;
public Integer _pictureHeight = null;
private int _BoatWidth = 80;
private int _BoatHeight = 50;
public void SetOars() {
Random r = new Random();
int numIllum = r.nextInt(1,4);
OarsDraw.SetOarsCount(numIllum);
}
public DrawningBoat(int speed, float weight, Color bodyColor)
{
Boat = new EntityBoat(speed, weight, bodyColor);
Random random = new Random();
switch (random.nextInt(3)){
case 0:
OarsDraw = new DrawningOars();
break;
case 1:
OarsDraw = new DrawningSqareOars();
break;
case 2:
OarsDraw = new DrawningOvalOars();
break;
}
SetOars();
}
protected DrawningBoat(EntityBoat boat, IDrawningOars oar){
Boat = boat;
OarsDraw = oar;
}
public void SetPosition(int x, int y, int width, int height)
{
if (x >= 0 && x + _BoatWidth <= width && y >= 0 && y + _BoatHeight <= height) {
_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 + _BoatWidth + Boat.Step < _pictureWidth)
{
_startPosX += Boat.Step;
}
break;
case Left:
if (_startPosX - Boat.Step > 0)
{
_startPosX -= Boat.Step;
}
break;
case Up:
if (_startPosY - Boat.Step > 0)
{
_startPosY -= Boat.Step;
}
break;
case Down:
if (_startPosY + _BoatHeight + Boat.Step < _pictureHeight)
{
_startPosY += Boat.Step;
}
break;
}
}
protected DrawningBoat(int speed, float weight, Color bodyColor, int boatWidth, int boatHeight)
{
this(speed, weight, bodyColor);
_BoatWidth = boatWidth;
_BoatHeight = boatHeight;
}
public void DrawTransport(Graphics g) {
if (_startPosX < 0 || _startPosY < 0 || _pictureWidth == null || _pictureHeight == null) {
return;
}
if (GetBoat() == null) {
return;
}
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.BLACK);
//границы
g2d.drawRect(_startPosX, _startPosY, _BoatWidth * 3 / 4, _BoatHeight);
int[] xpoints = {(int)(_startPosX + _BoatWidth * 3 / 4), (int)(_startPosX + _BoatWidth), (int)(_startPosX + _BoatWidth * 3 / 4) };
int[] ypoints = {(int)_startPosY, (int)(_startPosY + _BoatHeight / 2), (int)(_startPosY + _BoatHeight)};
g2d.drawPolygon(xpoints, ypoints, 3);
g2d.setPaint(Boat.GetBodyColor());
g2d.fillRect(_startPosX, _startPosY, _BoatWidth * 3 / 4, _BoatHeight);
g2d.fillPolygon(xpoints, ypoints, 3);
// середина
g2d.setColor(Color.BLACK);
g2d.drawOval(_startPosX + 5, _startPosY + 5, _BoatWidth - 15, _BoatHeight - 11);
g2d.setPaint(Color.CYAN);
g2d.fillOval(_startPosX + 5, _startPosY + 5, _BoatWidth - 15, _BoatHeight - 11);
OarsDraw.DrawOars(g, _startPosX, _startPosY);
repaint();
}
public void ChangeBorders(int width, int height)
{
_pictureWidth = width;
_pictureHeight = height;
if (_pictureWidth <= _BoatWidth || _pictureHeight <= _BoatHeight)
{
_pictureWidth = null;
_pictureHeight = null;
return;
}
if (_startPosX + _BoatWidth > _pictureWidth)
{
_startPosX = _pictureWidth - _BoatWidth;
}
if (_startPosY + _BoatHeight > _pictureHeight)
{
_startPosY = _pictureHeight - _BoatHeight;
}
}
public float[] GetCurrentPosition() {
float[] dim = new float[4];
dim[0] = _startPosX;
dim[1] =_startPosY;
dim[2] = _startPosX + _BoatWidth;
dim[3] = _startPosY + _BoatHeight;
return dim;
}
}

48
DrawningCatamaran.java Normal file
View File

@ -0,0 +1,48 @@
import java.awt.*;
public class DrawningCatamaran extends DrawningBoat{
public DrawningCatamaran(int speed, float weight, Color bodyColor, Color dopColor, boolean sail, boolean floats)
{
super(speed, weight, bodyColor, 140, 70);
Boat = new EntityCatamaran(speed, weight, bodyColor, dopColor, sail, floats);
}
protected DrawningCatamaran(EntityBoat boat, IDrawningOars oars){
super(boat,oars);
Boat = boat;
}
@Override
public void DrawTransport(Graphics g)
{
if (! (Boat instanceof EntityCatamaran Catamaran))
{
return;
}
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.BLACK);
_startPosX += 10;
_startPosY += 5;
super.DrawTransport(g);
_startPosX -= 10;
_startPosY -= 5;
int _catamaranWidth = 80;
int _catamaranHeight = 50;
if (Catamaran.Sail)
{
g2d.setPaint(Catamaran.DopColor);
int[] p_x = {(int)(_startPosX + _catamaranWidth * 2 / 4), (int)(_startPosX + _catamaranWidth), (int)(_startPosX + _catamaranWidth * 2 / 4)};
int[] p_y = {(int)_startPosY + 5, (int)(_startPosY + _catamaranHeight / 2), (int)(_startPosY + _catamaranHeight)};
g.fillPolygon(p_x, p_y, 3);
}
if (Catamaran.Floats)
{
g2d.setPaint(Catamaran.DopColor);
g.fillOval(_startPosX, _startPosY, (int)(_catamaranWidth / 2), (int)(_catamaranHeight / 2));
g.fillOval(_startPosX, 27 + _startPosY + (int)(_catamaranHeight / 2), (int)(_catamaranWidth / 2), (int)(_catamaranHeight / 2));
}
}
}

57
DrawningEntities.java Normal file
View File

@ -0,0 +1,57 @@
import java.util.Random;
public class DrawningEntities <T extends EntityBoat, U extends IDrawningOars>
{
public T[] _entities;
public U[] _oars;
int entitiesCount = 0;
int oarsCount = 0;
String indx;
String indy;
public DrawningEntities(int countE,int countI){
_entities = (T[]) new EntityBoat[countE];
_oars = (U[]) new IDrawningOars[countI];
}
public int Insert(T boat){
if(entitiesCount < _entities.length){
_entities[entitiesCount] = boat;
entitiesCount++;
return entitiesCount - 1;
}
return -1;
}
public int Insert(U oars){
if(oarsCount < _oars.length){
_oars[oarsCount] = oars;
oarsCount++;
return oarsCount - 1;
}
return -1;
}
public void SetIndexs(int ind1, int ind2)
{
indx=Integer.toString(ind1);
indy=Integer.toString(ind2);
}
public DrawningBoat CreateBoat(){
Random random = new Random();
int indEnt = 0;
int indOar = 0;
if(entitiesCount - 1 != 0 & oarsCount - 1 != 0){
indEnt = random.nextInt(0,entitiesCount - 1);
indOar = random.nextInt(0, oarsCount - 1);
}
T boat = (T)_entities[indEnt];
U oar = (U)_oars[indOar];
SetIndexs(indEnt,indOar);
if(boat instanceof EntityCatamaran){
return new DrawningCatamaran(boat,oar);
}
return new DrawningBoat(boat,oar);
}
}

37
DrawningOars.java Normal file
View File

@ -0,0 +1,37 @@
import javax.swing.*;
import java.awt.*;
public class DrawningOars implements IDrawningOars {
private OarsCount _Oars;
@Override
public void SetOarsCount(int numOfOars) {
_Oars = OarsCount.GetOarsCount(numOfOars);
}
public void DrawOars(Graphics g, int _startPosX, int _startPosY) {
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.BLACK);
int numOfIlluminator = 0;
switch (_Oars)
{
case One:
numOfIlluminator = 1;
break;
case Two:
numOfIlluminator = 2;
break;
case Three:
numOfIlluminator = 3;
break;
}
for(int i = numOfIlluminator; i >= 1; --i){
g2d.setColor(Color.CYAN);
g2d.fillRect(_startPosX + (10 * (i + 1)), _startPosY -15, 5, 15);
g2d.setColor(Color.BLACK);
g2d.drawRect(_startPosX + (10 * (i + 1)), _startPosY - 15, 5, 15);
}
}
}

39
DrawningObjectBoat.java Normal file
View File

@ -0,0 +1,39 @@
import java.awt.*;
import java.util.Enumeration;
public class DrawningObjectBoat implements IDrawningObject {
private DrawningBoat _boat = null;
public DrawningObjectBoat(DrawningBoat boat)
{
_boat = boat;
}
@Override
public float Step() {
if(_boat != null && _boat.Boat != null)
return _boat.Boat.GetStep();
return 0;
}
@Override
public void SetObject(int x, int y, int width, int height) {
_boat.SetPosition(x,y,width,height);
}
@Override
public void MoveObject(Direction direction) {
_boat.MoveTransport(direction);
}
@Override
public void DrawningObject(Graphics g) {
_boat.DrawTransport(g);
}
@Override
public float[] GetCurrentPosition() {
if(_boat!=null)
return _boat.GetCurrentPosition();
return null;
}
}

36
DrawningOvalOars.java Normal file
View File

@ -0,0 +1,36 @@
import java.awt.*;
public class DrawningOvalOars implements IDrawningOars{
private OarsCount _Oars;
@Override
public void SetOarsCount(int numOfOars) {
_Oars = OarsCount.GetOarsCount(numOfOars);
}
@Override
public void DrawOars(Graphics g, int _startPosX, int _startPosY) {
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.BLACK);
int numOfOars = 0;
switch (_Oars)
{
case One:
numOfOars = 1;
break;
case Two:
numOfOars = 2;
break;
case Three:
numOfOars = 3;
break;
}
for(int i = numOfOars; i >= 1; --i){
g2d.setColor(Color.CYAN);
g2d.fillOval(_startPosX + (15 * (i + 1)), _startPosY -15, 15, 15);
g2d.setColor(Color.BLACK);
g2d.drawOval(_startPosX + (15 * (i + 1)), _startPosY - 15, 15, 15);
}
}
}

33
DrawningSqareOars.java Normal file
View File

@ -0,0 +1,33 @@
import java.awt.*;
public class DrawningSqareOars implements IDrawningOars{
private OarsCount _Oars;
@Override
public void SetOarsCount(int numOfOars) {_Oars = OarsCount.GetOarsCount(numOfOars); }
@Override
public void DrawOars(Graphics g, int _startPosX, int _startPosY) {
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.BLACK);
int numOfOars = 0;
switch (_Oars)
{
case One:
numOfOars = 1;
break;
case Two:
numOfOars = 2;
break;
case Three:
numOfOars = 3;
break;
}
for(int i = numOfOars; i >= 1; --i){
g2d.setColor(Color.CYAN);
g2d.fillRect(_startPosX + (15 * (i + 1)), _startPosY -15, 15, 15);
g2d.setColor(Color.BLACK);
g2d.drawRect(_startPosX + (15 * (i + 1)), _startPosY - 15, 15, 15);
}
}
}

32
EntityBoat.java Normal file
View File

@ -0,0 +1,32 @@
import java.awt.*;
import java.util.Random;
public class EntityBoat {
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 Step;
public float GetStep(){return Step;}
public EntityBoat(int speed, float weight, Color bodyColor)
{
Random rnd = new Random();
Speed = speed <= 0 ? rnd.nextInt(10,30) : speed;
Weight = weight <= 0 ? rnd.nextInt(40,70) : weight;
BodyColor = bodyColor;
Step = Speed * 100 / (int)Weight;
}
}

17
EntityCatamaran.java Normal file
View File

@ -0,0 +1,17 @@
import java.awt.*;
public class EntityCatamaran extends EntityBoat{
public Color DopColor;
public boolean Sail;
public boolean Floats;
public EntityCatamaran(int speed, float weight, Color bodyColor, Color dopColor, boolean sail, boolean floats) {
super(speed, weight, bodyColor);
DopColor = dopColor;
Sail = sail;
Floats = floats;
}
}

111
FormBoat.form Normal file
View File

@ -0,0 +1,111 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="FormBoat">
<grid id="27dc6" binding="Mainpanel" layout-manager="GridLayoutManager" row-count="4" column-count="6" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="513" height="406"/>
</constraints>
<properties>
<opaque value="true"/>
<preferredSize width="800" height="600"/>
</properties>
<border type="none"/>
<children>
<component id="2ea16" class="javax.swing.JButton" binding="ButtonDown">
<constraints>
<grid row="2" column="4" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="3" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<text value=""/>
</properties>
</component>
<component id="4f60a" class="javax.swing.JButton" binding="ButtonLeft">
<constraints>
<grid row="2" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="3" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<text value=""/>
</properties>
</component>
<component id="4eb88" class="javax.swing.JButton" binding="ButtonRight">
<constraints>
<grid row="2" column="5" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="3" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<horizontalAlignment value="0"/>
<text value=""/>
</properties>
</component>
<toolbar id="e747d" binding="StatusStrip">
<constraints>
<grid row="3" column="0" row-span="1" col-span="6" vsize-policy="0" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false">
<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>
<component id="9e2ce" class="javax.swing.JButton" binding="ButtonCreate">
<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>
<grid id="febd4" binding="PictureBox" layout-manager="FlowLayout" hgap="5" vgap="5" flow-align="1">
<constraints>
<grid row="0" column="0" row-span="1" col-span="6" 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="fb937" class="javax.swing.JButton" binding="ButtonUp">
<constraints>
<grid row="1" column="4" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="3" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<text value=""/>
</properties>
</component>
<component id="44b75" class="javax.swing.JButton" binding="ButtonModif">
<constraints>
<grid row="2" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Модификация"/>
</properties>
</component>
<component id="9ba33" class="javax.swing.JButton" binding="ButtonSelectBoat">
<constraints>
<grid row="2" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Select"/>
</properties>
</component>
</children>
</grid>
</form>

124
FormBoat.java Normal file
View File

@ -0,0 +1,124 @@
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.image.BufferedImage;
import java.util.Random;
import static java.lang.Boolean.parseBoolean;
public class FormBoat extends JDialog{
public JPanel Mainpanel;
private JButton ButtonCreate;
private JButton ButtonLeft;
private JButton ButtonUp;
private JButton ButtonDown;
private JButton ButtonRight;
protected DrawningBoat _boat;
private JPanel PictureBox;
private JToolBar StatusStrip;
private JButton ButtonModif;
private JButton ButtonSelectBoat;
private final JLabel JLabelSpeed = new JLabel();
private final JLabel JLabelWeight = new JLabel();
private final JLabel JLabelColor = new JLabel();
private DrawningBoat SelectedBoat;
public DrawningBoat GetSelectedBoat() {
return SelectedBoat;
}
public void Draw() {
PictureBox.removeAll();
BufferedImage bmp = new BufferedImage(PictureBox.getWidth(), PictureBox.getHeight(),BufferedImage.TYPE_INT_RGB);
Graphics gr = bmp.getGraphics();
gr.setColor(new Color(238, 238, 238));
gr.fillRect(0, 0, PictureBox.getWidth(), PictureBox.getHeight());
if (_boat != null) {
_boat.DrawTransport(gr);
JLabel imageOfBoat = new JLabel();
imageOfBoat.setPreferredSize(PictureBox.getSize());
imageOfBoat.setMinimumSize(new Dimension(1, 1));
imageOfBoat.setIcon(new ImageIcon(bmp));
PictureBox.add(imageOfBoat,BorderLayout.CENTER);
}
validate();
}
private void SetData(){
Random random = new Random();
_boat.SetPosition(random.nextInt(10, 100), random.nextInt(30, 100), PictureBox.getWidth(), PictureBox.getHeight());
JLabelSpeed.setText("орость: " + _boat.GetBoat().GetSpeed() + " ");
JLabelWeight.setText("Вес: " + _boat.GetBoat().GetWeight() + " ");
JLabelColor.setText(("Цвет: " + _boat.GetBoat().GetBodyColor() + " "));
}
public FormBoat() {
add(Mainpanel);
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(FormBoat.class.getResource("/Resource/up.jpg"));
ButtonUp.setIcon(new ImageIcon(img));
img = ImageIO.read(FormBoat.class.getResource("/Resource/down.jpg"));
ButtonDown.setIcon(new ImageIcon(img));
img = ImageIO.read(FormBoat.class.getResource("/Resource/left.jpg"));
ButtonLeft.setIcon(new ImageIcon(img));
img = ImageIO.read(FormBoat.class.getResource("/Resource/right.jpg"));
ButtonRight.setIcon(new ImageIcon(img));
} catch (Exception ex) {
System.out.println(ex);
}
ButtonCreate.addActionListener(e -> {
Random random = new Random();
Color color = JColorChooser.showDialog(null, "Цвет", null);
_boat = new DrawningBoat(random.nextInt(100, 300),random.nextInt(1000, 2000),color);
SetData();
Draw();
});
ButtonModif.addActionListener(e -> {
Random random = new Random();
Color first = JColorChooser.showDialog(null, "Цвет", null);
Color second = JColorChooser.showDialog(null, "Цвет", null);
_boat = new DrawningCatamaran(random.nextInt(100, 300), random.nextInt(1000, 2000), first, second, random.nextBoolean(), random.nextBoolean());
SetData();
Draw();
});
PictureBox.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
if(_boat == null || _boat.GetBoat() == null) return;
super.componentResized(e);
_boat.ChangeBorders(PictureBox.getWidth(), PictureBox.getHeight());
PictureBox.revalidate();
Draw();
}
});
ButtonUp.addActionListener(e -> {
_boat.MoveTransport(Direction.Up);
PictureBox.revalidate();
Draw();
});
ButtonDown.addActionListener(e -> {
_boat.MoveTransport(Direction.Down);
PictureBox.revalidate();
Draw();
});
ButtonRight.addActionListener(e -> {
_boat.MoveTransport(Direction.Right);
PictureBox.revalidate();
Draw();
});
ButtonLeft.addActionListener(e -> {
_boat.MoveTransport(Direction.Left);
PictureBox.revalidate();
Draw();
});
ButtonSelectBoat.addActionListener(e -> {
SelectedBoat = _boat;
setVisible(false);
dispose();
});
}
}

166
FormMapWithSetBoats.form Normal file
View File

@ -0,0 +1,166 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="FormMapWithSetBoats">
<grid id="27dc6" binding="Mainpanel" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="785" height="400"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<grid id="afa3d" binding="PictureBox" layout-manager="FlowLayout" hgap="5" vgap="5" flow-align="1">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children/>
</grid>
<grid id="e9a63" binding="groupBox" layout-manager="GridLayoutManager" row-count="10" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="0" anchor="0" fill="3" indent="0" use-parent-layout="false">
<minimum-size width="150" height="-1"/>
</grid>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="f23ee" class="javax.swing.JButton" binding="buttonAdd">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Добавить лодку"/>
</properties>
</component>
<component id="2e61e" class="javax.swing.JComboBox" binding="comboBoxSelectorMap">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<model/>
</properties>
</component>
<component id="5c428" class="javax.swing.JButton" binding="buttonRemove">
<constraints>
<grid row="4" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Удалить лодку"/>
</properties>
</component>
<component id="e24b9" class="javax.swing.JButton" binding="buttonShowStorage">
<constraints>
<grid row="6" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Посмотреть Хранилище"/>
</properties>
</component>
<component id="ac087" class="javax.swing.JButton" binding="buttonShowOnMap">
<constraints>
<grid row="7" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Посмотреть карту"/>
</properties>
</component>
<grid id="2821b" layout-manager="GridLayoutManager" row-count="2" column-count="5" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="9" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="0" anchor="0" fill="3" indent="0" use-parent-layout="false">
<minimum-size width="150" height="-1"/>
</grid>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="6d803" class="javax.swing.JButton" binding="buttonLeft">
<constraints>
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<text value=""/>
</properties>
</component>
<component id="c0db2" class="javax.swing.JButton" binding="buttonUp">
<constraints>
<grid row="0" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<text value=""/>
</properties>
</component>
<component id="ca0da" class="javax.swing.JButton" binding="buttonDown">
<constraints>
<grid row="1" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<text value=""/>
</properties>
</component>
<component id="c3e45" class="javax.swing.JButton" binding="buttonRight">
<constraints>
<grid row="1" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<text value=""/>
</properties>
</component>
<hspacer id="1f200">
<constraints>
<grid row="1" column="4" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<hspacer id="f8b3b">
<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>
</children>
</grid>
<vspacer id="b833e">
<constraints>
<grid row="8" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
<component id="181ac" class="javax.swing.JTextField" binding="textBoxPosition">
<constraints>
<grid row="3" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
<properties/>
</component>
<vspacer id="53ec0">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
<vspacer id="a6919">
<constraints>
<grid row="5" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
</children>
</grid>
</children>
</grid>
</form>

192
FormMapWithSetBoats.java Normal file
View File

@ -0,0 +1,192 @@
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
public class FormMapWithSetBoats extends JFrame {
private MapWithSetBoatsGeneric<DrawningObjectBoat, AbstractMap> _mapBoatsCollectionGeneric;
public JPanel Mainpanel;
private JButton buttonAdd;
private JComboBox comboBoxSelectorMap;
private JTextField textBoxPosition;
private JButton buttonRemove;
private JButton buttonShowStorage;
private JButton buttonShowOnMap;
private JButton buttonUp;
private JButton buttonLeft;
private JButton buttonRight;
private JButton buttonDown;
private JPanel PictureBox;
private JPanel groupBox;
AbstractMap _abstractMap;
private JFrame getFrame() {
JFrame frame = new JFrame();
frame.setVisible(false);
frame.setBounds(300, 100, 800, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
return frame;
}
JFrame jFrame = getFrame();
private void ButtonMove_Click(String name)
{
if (_mapBoatsCollectionGeneric == null) return;
Direction direction = Direction.None;
switch (name)
{
case "buttonLeft":
direction = Direction.Left;
break;
case "buttonUp":
direction = Direction.Up;
break;
case "buttonRight":
direction = Direction.Right;
break;
case "buttonDown":
direction = Direction.Down;
break;
}
PictureBox.removeAll();
JLabel imageWithMapAndObject = new JLabel();
imageWithMapAndObject.setPreferredSize(PictureBox.getSize());
imageWithMapAndObject.setMinimumSize(new Dimension(1, 1));
imageWithMapAndObject.setIcon(new ImageIcon(_mapBoatsCollectionGeneric.MoveObject(direction)));
PictureBox.add(imageWithMapAndObject, BorderLayout.CENTER);
PictureBox.revalidate();
PictureBox.repaint();
}
public FormMapWithSetBoats() {
comboBoxSelectorMap.addItem("Простая карта");
comboBoxSelectorMap.addItem("Вторая карта");
try {
Image img = ImageIO.read(FormMapWithSetBoats.class.getResource("/Resource/up.jpg"));
buttonUp.setIcon(new ImageIcon(img));
img = ImageIO.read(FormMapWithSetBoats.class.getResource("/Resource/down.jpg"));
buttonDown.setIcon(new ImageIcon(img));
img = ImageIO.read(FormMapWithSetBoats.class.getResource("/Resource/left.jpg"));
buttonLeft.setIcon(new ImageIcon(img));
img = ImageIO.read(FormMapWithSetBoats.class.getResource("/Resource/right.jpg"));
buttonRight.setIcon(new ImageIcon(img));
} catch (Exception ex) {
System.out.println(ex);
}
comboBoxSelectorMap.addActionListener(e -> {
AbstractMap map = null;
switch (comboBoxSelectorMap.getSelectedItem().toString())
{
case "Простая карта":
map = new SimpleMap();
break;
case "Вторая карта":
map = new MyMap();
break;
}
if (map != null)
{
_mapBoatsCollectionGeneric = new MapWithSetBoatsGeneric(PictureBox.getWidth(), PictureBox.getHeight(), map);
}
else
{
_mapBoatsCollectionGeneric = null;
}
});
buttonAdd.addActionListener(e -> {
if (_mapBoatsCollectionGeneric == null)
{
return;
}
FormBoat dialog = new FormBoat();
dialog.setSize(800, 600);
dialog.setLocation(500, 200);
dialog.setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
if (dialog.GetSelectedBoat() == null) return;
DrawningObjectBoat boat = new DrawningObjectBoat(dialog.GetSelectedBoat());
if (_mapBoatsCollectionGeneric.addBoat(boat) != -1)
{
JOptionPane.showMessageDialog(jFrame, "Объект добавлен");
PictureBox.removeAll();
JLabel imageOfBoat = new JLabel();
imageOfBoat.setPreferredSize(PictureBox.getSize());
imageOfBoat.setMinimumSize(new Dimension(1, 1));
imageOfBoat.setIcon(new ImageIcon(_mapBoatsCollectionGeneric.ShowSet()));
PictureBox.add(imageOfBoat,BorderLayout.CENTER);
PictureBox.revalidate();
PictureBox.repaint();
}
else
{
JOptionPane.showMessageDialog(jFrame, "Не удалось добавить объект");
}
});
buttonRemove.addActionListener(e -> {
if(_mapBoatsCollectionGeneric == null) return;
String text = textBoxPosition.getText();
if(text.isEmpty()) return;
if(JOptionPane.showConfirmDialog(jFrame, "Вы действительно хотите удалить объект?", "Удаление", JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION) return;
try {
Integer.parseInt(text);
}
catch (Exception ex){
return;
}
int pos = Integer.parseInt(text);
if (_mapBoatsCollectionGeneric.removeBoat(pos) != null)
{
JOptionPane.showMessageDialog(jFrame, "Объект удален");
PictureBox.removeAll();
JLabel imageOfShip = new JLabel();
imageOfShip.setPreferredSize(PictureBox.getSize());
imageOfShip.setMinimumSize(new Dimension(1, 1));
imageOfShip.setIcon(new ImageIcon(_mapBoatsCollectionGeneric.ShowSet()));
PictureBox.add(imageOfShip,BorderLayout.CENTER);
PictureBox.revalidate();
PictureBox.repaint();
}
else
{
JOptionPane.showMessageDialog(jFrame, "Не удалось удалить объект");
}
});
buttonShowStorage.addActionListener(e -> {
if (_mapBoatsCollectionGeneric == null) return;
PictureBox.removeAll();
JLabel imageOfShip = new JLabel();
imageOfShip.setPreferredSize(PictureBox.getSize());
imageOfShip.setMinimumSize(new Dimension(1, 1));
imageOfShip.setIcon(new ImageIcon(_mapBoatsCollectionGeneric.ShowSet()));
PictureBox.add(imageOfShip,BorderLayout.CENTER);
PictureBox.revalidate();
PictureBox.repaint();
});
buttonShowOnMap.addActionListener(e -> {
if (_mapBoatsCollectionGeneric == null) return;
PictureBox.removeAll();
JLabel imageOfShip = new JLabel();
imageOfShip.setPreferredSize(PictureBox.getSize());
imageOfShip.setMinimumSize(new Dimension(1, 1));
imageOfShip.setIcon(new ImageIcon(_mapBoatsCollectionGeneric.ShowOnMap()));
PictureBox.add(imageOfShip,BorderLayout.CENTER);
PictureBox.revalidate();
PictureBox.repaint();
});
buttonUp.addActionListener(e -> ButtonMove_Click("buttonUp"));
buttonLeft.addActionListener(e -> ButtonMove_Click("buttonLeft"));
buttonDown.addActionListener(e -> ButtonMove_Click("buttonDown"));
buttonRight.addActionListener(e -> ButtonMove_Click("buttonRight"));
}
}

62
FormParam.form Normal file
View 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="pictureBoxBoat" 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>

103
FormParam.java Normal file
View File

@ -0,0 +1,103 @@
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 pictureBoxBoat;
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 DrawningEntities<EntityBoat,IDrawningOars> _drawningEntities;
private IDrawningOars SetData()
{
Random random=new Random();
int r = random.nextInt(3);
if(r==0)
{
return new DrawningOars();
}
if(r==1)
{
return new DrawningSqareOars();
}
else
{
return new DrawningOvalOars();
}
}
private void Draw(DrawningBoat _boat) {
pictureBoxBoat.removeAll();
Random random = new Random();
BufferedImage bmp = new BufferedImage(pictureBoxBoat.getWidth(), pictureBoxBoat.getHeight(),BufferedImage.TYPE_INT_RGB);
Graphics gr = bmp.getGraphics();
gr.setColor(new Color(238, 238, 238));
gr.fillRect(0, 0, pictureBoxBoat.getWidth(), pictureBoxBoat.getHeight());
if (_boat != null) {
_boat.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100), pictureBoxBoat.getWidth(), pictureBoxBoat.getHeight());
_boat.DrawTransport(gr);
JLabelSpeed.setText("орость: " + _boat.GetBoat().GetSpeed() + " ");
JLabelWeight.setText("Вес: " + _boat.GetBoat().GetWeight() + " ");
JLabelColor.setText(("Цвет: " + _boat.GetBoat().GetBodyColor() + " "));
JLabel imageOfBoat = new JLabel();
imageOfBoat.setPreferredSize(pictureBoxBoat.getSize());
imageOfBoat.setMinimumSize(new Dimension(1, 1));
imageOfBoat.setIcon(new ImageIcon(bmp));
pictureBoxBoat.add(imageOfBoat,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);
_drawningEntities = new DrawningEntities<>(10,10);
ButtonCreate.addActionListener(e -> {
Random random = new Random();
Color colorFirst = JColorChooser.showDialog(null, "Цвет", null);
EntityBoat _boat = new EntityBoat(random.nextInt(100,300), random.nextInt(1000,2000),colorFirst);
IDrawningOars oar = SetData();
int OarCount = random.nextInt(1,4);
oar.SetOarsCount(OarCount);
if((_drawningEntities.Insert(_boat)!=-1) & (_drawningEntities.Insert(oar)!=-1))
{
JOptionPane.showMessageDialog(null,"Объект добавлен");
Draw(_drawningEntities.CreateBoat());
LabelInfo.setText(_drawningEntities.indx+ " " + _drawningEntities.indy);
}
else
{
JOptionPane.showMessageDialog(null, "Не удалось добавить объект");
}
});
ButtonCreateModif.addActionListener(e -> {
Random random = new Random();
Color colorFirst = JColorChooser.showDialog(null, "Цвет", null);
Color colorSecond = JColorChooser.showDialog(null, "Цвет", null);
EntityCatamaran _boat = new EntityCatamaran(random.nextInt(100, 300), random.nextInt(1000, 2000), colorFirst, colorSecond, random.nextBoolean(), random.nextBoolean());
IDrawningOars oar = SetData();
int OarCount = random.nextInt(1,4);
oar.SetOarsCount(OarCount);
if((_drawningEntities.Insert(_boat)!=-1) & (_drawningEntities.Insert(oar)!=-1))
{
JOptionPane.showMessageDialog(null,"Объект добавлен");
Draw(_drawningEntities.CreateBoat());
LabelInfo.setText(_drawningEntities.indx+ " " + _drawningEntities.indy);
}
else
{
JOptionPane.showMessageDialog(null, "Не удалось добавить объект");
}
});
}
}

7
IDrawningOars.java Normal file
View File

@ -0,0 +1,7 @@
import java.awt.*;
public interface IDrawningOars {
void SetOarsCount(int numOfOars);
void DrawOars(Graphics g, int _startPosX, int _startPosY);
}

7
IDrawningOarsjava Normal file
View File

@ -0,0 +1,7 @@
import java.awt.*;
public interface IDrawningIlluminator {
void SetIlluminatorCount(int numOfIllum);
void DrawIlluminator(Graphics g, int _startPosX, int _startPosY);
}

13
IDrawningObject.java Normal file
View File

@ -0,0 +1,13 @@
import java.awt.*;
public interface IDrawningObject {
float Step();
void SetObject(int x, int y, int width, int height);
void MoveObject(Direction direction);
void DrawningObject(Graphics g);
float[] GetCurrentPosition();
}

14
Main.java Normal file
View File

@ -0,0 +1,14 @@
import javax.swing.*;
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame("Лодка");
frame.setContentPane(new FormParam().MainPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocation(500, 200);
frame.pack();
frame.setSize(800, 600);
frame.setVisible(true);
}
}

132
MapWithSetBoatsGeneric.java Normal file
View File

@ -0,0 +1,132 @@
import java.awt.*;
import java.awt.image.BufferedImage;
public class MapWithSetBoatsGeneric <T extends IDrawningObject, U extends AbstractMap>
{
private int _pictureWidth;
private int _pictureHeight;
private int _placeSizeWidth = 210;
private int _placeSizeHeight = 90;
private SetBoatsGeneric<T> _setBoats;
private U _map;
public MapWithSetBoatsGeneric(int picWidth, int picHeight, U map)
{
int width = picWidth / _placeSizeWidth;
int height = picHeight / _placeSizeHeight;
_setBoats = new SetBoatsGeneric<T>(width * height);
_pictureWidth = picWidth;
_pictureHeight = picHeight;
_map = map;
}
public int addBoat(T boat)
{
return _setBoats.Insert(boat);
}
public T removeBoat(int position)
{
return _setBoats.Remove(position);
}
public BufferedImage ShowSet()
{
BufferedImage img = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_ARGB);
Graphics2D gr = (Graphics2D) img.getGraphics();
DrawBackground(gr);
DrawBoats(gr);
return img;
}
public BufferedImage ShowOnMap()
{
BufferedImage bmp = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_RGB);
Shaking();
for (int i = 0; i < _setBoats.Count(); i++)
{
var ship = _setBoats.Get(i);
if (ship != null)
{
return _map.CreateMap(_pictureWidth, _pictureHeight, ship);
}
}
return bmp;
}
public BufferedImage MoveObject(Direction direction)
{
BufferedImage img = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_ARGB);
if (_map != null)
{
_map.MoveObject(direction);
}
return img;
}
private void Shaking()
{
int j = _setBoats.Count() - 1;
for (int i = 0; i < _setBoats.Count(); i++)
{
if (_setBoats.Get(i) == null)
{
for (; j > i; j--)
{
var boat = _setBoats.Get(j);
if (boat != null)
{
_setBoats.Insert(boat, i);
_setBoats.Remove(j);
break;
}
}
if (j <= i)
{
return;
}
}
}
}
private void DrawBackground(Graphics g)
{
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.BLACK);
for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++)
{
for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j)
{//линия рамзетки места
g.drawLine(i * _placeSizeWidth, j * _placeSizeHeight, i *
_placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight);
for (int k = 0; k < 7; k++)
{
g.drawOval( i * _placeSizeWidth + 15 * k, j * _placeSizeHeight, 15, 15);
g.drawOval( i * _placeSizeWidth + 15 * k, j * _placeSizeHeight - 15, 15, 15);
}
}
g.drawLine(i * _placeSizeWidth, 0, i * _placeSizeWidth, (_pictureHeight / _placeSizeHeight) * _placeSizeHeight);
for (int k = 0; k < 6 * 4; k++)
{
g.drawOval( i * _placeSizeWidth, 0 + 15 * k, 15, 15);
}
}
}
private void DrawBoats(Graphics g)
{
int numberOfSeatsInWidth = _pictureWidth / _placeSizeWidth;
int numberOfSeatsInHeight = _pictureHeight / _placeSizeHeight;
int bottomLine = (numberOfSeatsInHeight - 1) * _placeSizeHeight;
for (int i = 0; i < _setBoats.Count(); i++)
{
if(_setBoats.Get(i) != null)
{
_setBoats.Get(i).SetObject(i % numberOfSeatsInWidth * _placeSizeWidth, bottomLine - i / numberOfSeatsInWidth * _placeSizeHeight, _pictureWidth, _pictureHeight);
_setBoats.Get(i).DrawningObject(g);
}
}
}
}

44
MyMap.java Normal file
View File

@ -0,0 +1,44 @@
import java.awt.*;
public class MyMap extends AbstractMap{
private Color barrierColor = Color.WHITE;
private Color roadColor = Color.CYAN;
@Override
protected void DrawBarrierPart(Graphics2D g, int i, int j)
{
g.setPaint(barrierColor);
g.fillRect((int)Math.floor(i * _size_x), (int)Math.floor(j * _size_y), (int)Math.ceil(_size_x), (int)Math.ceil(_size_y));
}
@Override
protected void DrawRoadPart(Graphics2D g, int i, int j)
{
g.setPaint(roadColor);
g.fillRect((int)Math.floor(i * _size_x), (int)Math.floor(j * _size_y), (int)Math.ceil(_size_x), (int)Math.ceil(_size_y));
}
@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] = _freeRoad;
}
}
while (counter < 25)
{
int x = _random.nextInt(0, 100);
int y = _random.nextInt(0, 100);
if (_map[x][y] == _freeRoad)
{
_map[x][y] = _barrier;
counter++;
}
}
}
}

22
OarsCount.java Normal file
View File

@ -0,0 +1,22 @@
public enum OarsCount {
One,
Two,
Three;
public static OarsCount GetOarsCount(int Value)
{
switch(Value)
{
case 1:
return One;
case 2:
return Two;
case 3:
return Three;
}
return null;
}
}

BIN
Resource/down.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

BIN
Resource/left.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

BIN
Resource/right.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

BIN
Resource/up.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

44
SetBoatsGeneric.java Normal file
View File

@ -0,0 +1,44 @@
public class SetBoatsGeneric<T> {
private T[] _places;
public int Count() {
return _places.length;
}
public SetBoatsGeneric(int count) {
_places = (T[]) (new Object[count]);
}
public int Insert(T boat) {
for (int i = 0; i < _places.length; i++) {
if (_places[i] == null) {
_places[i] = boat;
return i;
}
}
return -1;
}
public int Insert(T boat, int position) {
int index = position;
while (_places[index] != null && index < _places.length) index++;
if (index == _places.length) return -1;
for (int i = index; i > position; --i) _places[i] = _places[i - 1];
_places[position] = boat;
return position;
}
public T Remove(int position) {
if (position >= _places.length) return null;
T res = _places[position];
_places[position] = null;
return res;
}
public T Get(int position) {
return _places[position];
}
}

45
SimpleMap.java Normal file
View File

@ -0,0 +1,45 @@
import java.awt.*;
public class SimpleMap extends AbstractMap{
private Color barrierColor = Color.BLACK;
private Color roadColor = Color.GRAY;
@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] = _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++;
}
}
}
@Override
protected void DrawRoadPart(Graphics2D g, int i, int j)
{
g.setPaint(roadColor);
g.fillRect((int)Math.floor(i * _size_x), (int)Math.floor(j * _size_y), (int)Math.ceil(_size_x), (int)Math.ceil(_size_y));
}
@Override
protected void DrawBarrierPart(Graphics2D g, int i, int j)
{
g.setPaint(barrierColor);
g.fillRect((int)Math.floor(i * _size_x), (int)Math.floor(j * _size_y), (int)Math.ceil(_size_x), (int)Math.ceil(_size_y));
}
}