лр2 коммит 1

This commit is contained in:
Катя Ихонкина 2022-12-10 00:10:36 +04:00
parent 769fedd755
commit 52f515ce73
18 changed files with 980 additions and 51 deletions

146
src/AbstractMap.java Normal file
View File

@ -0,0 +1,146 @@
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;
public abstract class AbstractMap {
private IDrawningObject _drawingObject = null;
protected int[][] _map = null;
protected int _width;
protected int _height;
//øèðèíà è âûñîòà ÿ÷åéêè
protected float _size_x;
protected float _size_y;
protected final Random _random = new Random();
protected final int _freeRoad = 0;
protected final int _barrier = 1;
public BufferedImage CreateMap(int width, int height, IDrawningObject drawingObject)
{
_width = width;
_height = height;
_drawingObject = drawingObject;
GenerateMap();
while (!SetObjectOnMap())
{
GenerateMap();
}
return DrawMapWithObject();
}
public BufferedImage MoveObject(Direction direction)
{
_drawingObject.MoveObject(direction);
float[] tuple = _drawingObject.GetCurrentPosition();
if (CheckBarrier(tuple[0],tuple[1],tuple[2],tuple[3]))
{
_drawingObject.MoveObject(MoveObjectBack(direction));
}
return DrawMapWithObject();
}
private Direction MoveObjectBack(Direction direction)
{
switch (direction)
{
case Up:
return Direction.Down;
case Down:
return Direction.Up;
case Left:
return Direction.Right;
case Right:
return Direction.Left;
}
return Direction.None;
}
private boolean CheckBarrier(float Left, float Right, float Top, float Bottom)
{
int startX = (int)(Left / _size_x);
int startY = (int)(Right / _size_y);
int endX = (int)(Top / _size_x);
int endY = (int)(Bottom / _size_y);
if (startX < 0 || startY < 0 || endX >= _map[0].length || endY >= _map.length)
{
return true;
}
for (int i = startX; i <= endX; i++)
{
for (int j = startY; j <= endY; j++)
{
if (_map[i][j] == _barrier)
{
return true;
}
}
}
return false;
}
private boolean SetObjectOnMap()
{
if (_drawingObject == null || _map == null)
{
return false;
}
int x = _random.nextInt(0, 10);
int y = _random.nextInt(0, 10);
_drawingObject.SetObject(x, y, _width, _height);
float[] tuple = _drawingObject.GetCurrentPosition();
if (!CheckBarrier(tuple[0], tuple[1], tuple[2], tuple[3])) return true;
float startX = tuple[0];
float startY = tuple[1];
float lengthX = tuple[2] - tuple[0];
float lengthY = tuple[3] - tuple[1];
while (CheckBarrier(startX, startY, startX + lengthX, startY + lengthY))
{
boolean result;
do
{
result = CheckBarrier(startX, startY, startX + lengthX, startY + lengthY);
if (!result)
{
_drawingObject.SetObject((int)startX, (int)startY, _width, _height);
return true;
}
else
{
startX += _size_x;
}
} while (result);
startX = x;
startY += _size_y;
}
return false;
}
private BufferedImage DrawMapWithObject()
{
BufferedImage bmp = new BufferedImage(_width, _height, BufferedImage.TYPE_INT_RGB);
if (_drawingObject == 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(gr, i, j);
}
else if (_map[i][j] == _barrier)
{
DrawBarrierPart(gr, i, j);
}
}
}
// _drawingObject.DrawingObject(gr);
return bmp;
}
protected abstract void GenerateMap();
protected abstract void DrawRoadPart(Graphics g, int i, int j);
protected abstract void DrawBarrierPart(Graphics g, int i, int j);
}

View File

@ -1,4 +1,5 @@
public enum Direction {
None,
Up,
Down,
Left,

View File

@ -1,23 +1,34 @@
import javax.swing.*;
import java.awt.*;
import java.util.Random;
public class DrawingBoat extends JComponent {
public EntityBoat Boat;
private float _startPosX;
private float _startPosY;
protected float _startPosX;
protected float _startPosY;
private Integer _pictureWidth = null;
private Integer _pictureHeight = null;
private final int _boatWidth = 90;
private final int _boatHeight = 80;
private DrawingPaddle _paddles;
public DrawingBoat() {
super();
}
public void Init(int speed, float weight, Color bodyColor, int paddleCount)
private int _boatWidth = 90;
private int _boatHeight = 80;
private IDrawningObjectPaddle _paddles;
public DrawingBoat(int speed, float weight, Color bodyColor, int paddleCount)
{
Boat = new EntityBoat();
Boat.Init(speed, weight, bodyColor);
_paddles = new DrawingPaddle();
_paddles.SetPaddlesAmount(paddleCount);
Random random = new Random();
Boat = new EntityBoat(speed, weight, bodyColor);
int randomPattern=random.nextInt(3);
if (randomPattern==0)
_paddles = new DrawingHardPaddle();
else if(randomPattern==1)
_paddles = new DrawningHard2Paddle();
else
_paddles = new DrawingPaddle();
_paddles.SetPaddlesCount(paddleCount);
}
protected DrawingBoat(int speed, float weight, Color bodyColor, int rollersAmount, int bulldozerWidth, int bulldozerHeight)
{
this(speed, weight, bodyColor, rollersAmount);
_boatWidth = bulldozerWidth;
_boatHeight = bulldozerHeight;
}
public void SetPosition(int x, int y, int width, int height)
{
@ -27,8 +38,8 @@ public class DrawingBoat extends JComponent {
_pictureHeight = null;
return;
}
_startPosX = x;
_startPosY = y;
_startPosX = x+20;
_startPosY = y+20;
_pictureWidth = width;
_pictureHeight = height;
}
@ -43,28 +54,29 @@ public class DrawingBoat extends JComponent {
case Right:
{
if (_startPosX + _boatWidth + Boat.Step() < _pictureWidth) {
System.out.println(Boat.Step());
_startPosX += Boat.Step();
}
break;
}
case Left:
if (_startPosX - Boat.Step() > 0)
{
case Left: {
if (_startPosX - Boat.Step() > 0) {
_startPosX -= Boat.Step();
}
break;
case Up:
if (_startPosY - Boat.Step() > 0)
{
}
case Up: {
if (_startPosY - Boat.Step() > 0) {
_startPosY -= Boat.Step();
}
break;
case Down:
if (_startPosY + _boatHeight + Boat.Step() < _pictureHeight)
{
}
case Down: {
if (_startPosY + _boatHeight + Boat.Step() < _pictureHeight) {
_startPosY += Boat.Step();
}
break;
}
}
}
public void paintComponent(Graphics gr)
@ -116,4 +128,12 @@ public class DrawingBoat extends JComponent {
_startPosY = _pictureHeight - _boatHeight;
}
}
public float[] GetCurrentPosition(){
float[] tuple = new float[4];
tuple[0] = _startPosX;
tuple[1] =_startPosY;
tuple[2] = _startPosX + _boatWidth;
tuple[3] = _startPosY + _boatHeight;
return tuple;
}
}

View File

@ -0,0 +1,72 @@
import java.awt.*;
public class DrawingHardPaddle implements IDrawningObjectPaddle {
private AdditionalDirection _paddleEnum;
@Override
public void SetPaddlesCount(int rollersAmount) {
for (AdditionalDirection item: _paddleEnum.values()) {
if (item.getCount() == rollersAmount) {
_paddleEnum = item;
return;
}
}
}
@Override
public void DrawPaddles(Graphics gr, int _startPosPaddlesX, int _startPosPaddlessY, Color mainBrush) {
Graphics2D g=(Graphics2D)gr;
//öâåò
Color br = new Color(0,0,0);
try { br = mainBrush; }
catch (Exception e) {
}
_startPosPaddlesX +=40;
_startPosPaddlessY +=33;
if (_paddleEnum.getCount() >= 1) {
paintPaddleUgol(g, _startPosPaddlesX-3, _startPosPaddlessY+5, _startPosPaddlessY + 16+5,mainBrush,false);
paintPaddle(g, _startPosPaddlesX, _startPosPaddlessY, _startPosPaddlessY + 16,mainBrush);
}
if (_paddleEnum.getCount() >= 2) {
paintPaddleUgol(g, _startPosPaddlesX -10-3, _startPosPaddlessY -41+5, _startPosPaddlessY -51+5,mainBrush,true);
paintPaddle(g, _startPosPaddlesX -10, _startPosPaddlessY -41, _startPosPaddlessY -51,mainBrush);
}
if (_paddleEnum.getCount() >= 3) {
paintPaddleUgol(g, _startPosPaddlesX -20-3, _startPosPaddlessY+5, _startPosPaddlessY + 16+5,mainBrush,false);
paintPaddle(g, _startPosPaddlesX -20, _startPosPaddlessY, _startPosPaddlessY + 16,mainBrush);
}
}
protected void paintPaddleUgol(Graphics2D g, int _startPosX, int _startPosY1,int _startPosY2, Color pen,boolean need){
try { g.setPaint(pen); }
catch (Exception e) {
g.setPaint(Color.black);
}
if(need){
g.fillPolygon(new int[]{_startPosX,_startPosX+5,_startPosX+10}, new int[]{_startPosY2,_startPosY2-10,_startPosY2},3);
g.setPaint(Color.black);
g.drawPolygon(new int[]{_startPosX,_startPosX+5,_startPosX+10}, new int[]{_startPosY2,_startPosY2-10,_startPosY2},3);}
else{
g.fillPolygon(new int[]{_startPosX,_startPosX+5,_startPosX+10}, new int[]{_startPosY2,_startPosY2+10,_startPosY2},3);
g.setPaint(Color.black);
g.drawPolygon(new int[]{_startPosX,_startPosX+5,_startPosX+10}, new int[]{_startPosY2,_startPosY2+10,_startPosY2},3);
}
/* g.fillRect(_startPosX,_startPosY1,4,17);
g.fillOval(_startPosX-1, _startPosY2, 6, 10);
g.setPaint(Color.black);
g.drawRect(_startPosX,_startPosY1,4,17);
g.drawOval(_startPosX-1, _startPosY2, 6, 10);*/
}
protected void paintPaddle(Graphics2D g, int _startPosX, int _startPosY1,int _startPosY2, Color pen){
try { g.setPaint(pen); }
catch (Exception e) {
g.setPaint(Color.black);
}
g.fillRect(_startPosX,_startPosY1,4,17);
g.fillOval(_startPosX-1, _startPosY2, 6, 10);
g.setPaint(Color.black);
g.drawRect(_startPosX,_startPosY1,4,17);
g.drawOval(_startPosX-1, _startPosY2, 6, 10);
}
}

View File

@ -1,8 +1,9 @@
import javax.swing.*;
import java.awt.*;
public class DrawingPaddle extends JComponent {
public class DrawingPaddle implements IDrawningObjectPaddle {
private AdditionalDirection _paddle;
public void SetPaddlesAmount(int rpaddlesAmount) {
@Override
public void SetPaddlesCount(int rpaddlesAmount) {
for (AdditionalDirection item: _paddle.values()) {
if (item.getCount() == rpaddlesAmount) {
_paddle = item;
@ -10,8 +11,8 @@ public class DrawingPaddle extends JComponent {
}
}
}
@Override
public void DrawPaddles(Graphics gr, int _startPosPaddlesX, int _startPosPaddlessY, Color pen) {
super.paintComponent(gr);
Graphics2D g=(Graphics2D)gr;
_startPosPaddlesX +=40;

View File

@ -0,0 +1,47 @@
import java.awt.*;
public class DrawningHard2Paddle implements IDrawningObjectPaddle {
private AdditionalDirection _paddleEnum;
@Override
public void SetPaddlesCount(int paddlesCount) {
for (AdditionalDirection item: _paddleEnum.values()) {
if (item.getCount() == paddlesCount) {
_paddleEnum = item;
return;
}
}
}
@Override
public void DrawPaddles(Graphics gr, int _startPosPaddlesX, int _startPosPaddlessY, Color mainBrush) {
Graphics2D g=(Graphics2D)gr;
//öâåò
Color br = new Color(0,0,0);
try { br = mainBrush; }
catch (Exception e) {
}
_startPosPaddlesX +=40;
_startPosPaddlessY +=33;
if (_paddleEnum.getCount() >= 1) {
paintPaddle(g, _startPosPaddlesX, _startPosPaddlessY, _startPosPaddlessY + 16,mainBrush);
}
if (_paddleEnum.getCount() >= 2) {
paintPaddle(g, _startPosPaddlesX -10, _startPosPaddlessY -41, _startPosPaddlessY -51,mainBrush);
}
if (_paddleEnum.getCount() >= 3) {
paintPaddle(g, _startPosPaddlesX -20, _startPosPaddlessY, _startPosPaddlessY + 16,mainBrush);
}
}
protected void paintPaddle(Graphics2D g, int _startPosX, int _startPosY1,int _startPosY2, Color pen){
try { g.setPaint(pen); }
catch (Exception e) {
g.setPaint(Color.black);
}
g.fillRect(_startPosX,_startPosY1,4,17);
g.fillRect(_startPosX-5, _startPosY2, 13, 13);
g.setPaint(Color.black);
g.drawRect(_startPosX,_startPosY1,4,17);
g.drawRect(_startPosX-5, _startPosY2, 13, 13);
}
}

View File

@ -0,0 +1,78 @@
import java.awt.*;
public class DrawningMotorBoat extends DrawingBoat{
public DrawningMotorBoat(int speed, float weight, Color bodyColor, int paddleamount, Color dopColor, boolean attachment, boolean ripper)
{
super(speed, weight, bodyColor, paddleamount, 138, 78);
Boat = new EntityMotorBoat(speed, weight, bodyColor, dopColor, attachment, ripper);
}
@Override
public void paintComponent(Graphics gr)
{
boolean check = Boat instanceof EntityMotorBoat;
if (!check) {
return;
}
EntityMotorBoat bulldozer = (EntityMotorBoat) Boat;
//_startPosY+=40;
Graphics2D g=(Graphics2D)gr;
Color dopBrush = bulldozer.DopColor();
gr.setColor(dopBrush);
g.fillOval( (int)_startPosX - 17, (int)_startPosY - 24, 14, 5);
gr.setColor(Color.BLACK);
g.drawOval((int) _startPosX - 17, (int)_startPosY - 24, 14, 5);
gr.setColor(dopBrush);
g.fillRect((int) _startPosX - 10, (int)_startPosY - 30, 10, 20);
gr.setColor(Color.BLACK);
g.drawRect( (int)_startPosX - 10, (int)_startPosY - 30, 10, 20);
int[] pointsx;
int[] pointsy;
if (bulldozer.Attachment())
{
pointsx = new int[]{(int)(_startPosX),(int)(_startPosX),(int)(_startPosX+50),(int)(_startPosX+50)};
pointsy = new int[]
{
(int)(_startPosY-50),(int)(_startPosY+10),(int)(_startPosY),(int)(_startPosY-40)
};
gr.setColor(dopBrush);
g.fillPolygon(pointsx,pointsy,4);
gr.setColor(Color.BLACK);
g.drawPolygon(pointsx,pointsy,4);
}
/* _startPosX -= 15;
_startPosY -= 5;*/
if (bulldozer.Ripper())
{
pointsx = new int[]{(int)(_startPosX)+50,(int)(_startPosX)+80,(int)(_startPosX+50)};
pointsy = new int[]{(int)(_startPosY-40),(int)(_startPosY-20),(int)(_startPosY)};
gr.setColor(dopBrush);
g.fillPolygon(pointsx,pointsy,3);
gr.setColor(Color.BLACK);
g.drawPolygon(pointsx,pointsy,3);
}
_startPosY-=40;
/* _startPosX += 15;
_startPosY += 5;*/
super.paintComponent(gr);
_startPosY+=40;
pointsx = new int[]
{
(int)(_startPosX)+50,(int)(_startPosX)+57,(int)(_startPosX+50) };
pointsy = new int[]
{
(int)(_startPosY-34),(int)(_startPosY-20),(int)(_startPosY-6)
};
gr.setColor(Color.CYAN);
g.fillPolygon(pointsx,pointsy,3);
gr.setColor(Color.BLACK);
((Graphics2D) gr).setStroke(new BasicStroke(1));
g.drawPolygon(pointsx,pointsy,3);
}
}

View File

@ -0,0 +1,38 @@
import java.awt.*;
public class DrawningObjectBoat implements IDrawningObject{
private DrawingBoat _boat = null;
public DrawningObjectBoat(DrawingBoat boat) {
_boat = boat;
}
@Override
public float Step() {
if (_boat!=null && _boat.Boat != null) {
return _boat.Boat.Step();
}
return 0;
}
@Override
public void SetObject(int x, int y, int width, int height) {
_boat.SetPosition(x+20, y+50, width, height);
}
@Override
public void MoveObject(Direction direction) {
_boat.MoveTransport(direction);
}
@Override
public void DrawingObject(Graphics g) {
_boat.paintComponent(g);
}
@Override
public float[] GetCurrentPosition()
{
if(_boat!=null)
return _boat.GetCurrentPosition();
return null;
}
}

View File

@ -14,7 +14,7 @@ public class EntityBoat {
}
public float Step()
{ return Speed() * 20 / Weight(); }
public void Init(int speed, float weight, Color bodyColor)
public EntityBoat(int speed, float weight, Color bodyColor)
{
Random random = new Random();
this.speed = speed <= 0 ? random.nextInt(50, 150) : speed;

21
src/EntityMotorBoat.java Normal file
View File

@ -0,0 +1,21 @@
import java.awt.*;
public class EntityMotorBoat extends EntityBoat{
// Äîïîëíèòåëüíûé öâåò
private Color dopColor;
public Color DopColor() { return dopColor; }
// Ïðèçíàê íàëè÷èÿ îòâàëà
private boolean attachment;
public boolean Attachment() { return attachment; }
// Ïðèçíàê íàëè÷èÿ ðûõëèòåëÿ
private boolean ripper;
public boolean Ripper() { return ripper; }
// Èíèöèàëèçàöèÿ ñâîéñòâ
public EntityMotorBoat(int speed, float weight, Color bodyColor, Color dopColor, boolean attachment, boolean ripper)
{
super(speed*100, weight, bodyColor);
this.dopColor = dopColor;
this.attachment = attachment;
this.ripper = ripper;
}
}

View File

@ -1,6 +1,6 @@
<?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="5" column-count="7" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="27dc6" binding="mainPanel" layout-manager="GridLayoutManager" row-count="6" column-count="7" 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="766" height="400"/>
@ -21,7 +21,7 @@
<grid id="59ac9" binding="radioButtonsBox" layout-manager="GridLayoutManager" row-count="2" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="3" column="0" row-span="2" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
<grid row="3" column="0" row-span="3" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
@ -99,17 +99,17 @@
</vspacer>
<hspacer id="8d179">
<constraints>
<grid row="4" column="1" row-span="1" col-span="3" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
<grid row="5" column="1" row-span="1" col-span="3" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<hspacer id="1de1f">
<constraints>
<grid row="3" column="1" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
<grid row="3" column="1" row-span="2" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<component id="d05dd" class="javax.swing.JButton" binding="buttonCreate">
<constraints>
<grid row="3" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="2" fill="1" indent="0" use-parent-layout="false">
<grid row="4" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="2" fill="1" indent="0" use-parent-layout="false">
<minimum-size width="120" height="40"/>
<preferred-size width="120" height="40"/>
<maximum-size width="120" height="40"/>
@ -122,7 +122,7 @@
<grid id="8ef30" binding="buttonsBox" layout-manager="GridLayoutManager" row-count="2" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="3" column="4" row-span="2" col-span="2" vsize-policy="3" hsize-policy="3" anchor="10" fill="0" indent="0" use-parent-layout="false">
<grid row="3" column="4" row-span="3" col-span="2" vsize-policy="3" hsize-policy="3" anchor="10" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="150" height="150"/>
<preferred-size width="150" height="150"/>
<maximum-size width="150" height="150"/>
@ -185,6 +185,18 @@
</component>
</children>
</grid>
<component id="f60b1" class="javax.swing.JButton" binding="buttonCreateModif">
<constraints>
<grid row="3" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="2" fill="1" indent="0" use-parent-layout="false">
<minimum-size width="120" height="40"/>
<preferred-size width="120" height="40"/>
<maximum-size width="120" height="40"/>
</grid>
</constraints>
<properties>
<text value="Создать"/>
</properties>
</component>
</children>
</grid>
</form>

View File

@ -3,14 +3,7 @@ import java.awt.*;
import java.awt.event.*;
import java.util.Random;
public class FormBoat extends JFrame {
public static void main(String[] args) {
FormBoat window = new FormBoat();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.pack();
window.setLocationRelativeTo(null);
window.setVisible(true);
}
private JPanel mainPanel;
private JPanel statusStrip;
@ -28,10 +21,13 @@ public class FormBoat extends JFrame {
private JRadioButton radioButtonPaddle3;
private JPanel radioButtonsBox;
private JPanel pictureBoxBoat;
private JButton buttonCreateModif;
private DrawingBoat _boat;
private int pictureBoxBoatWidth;
private int pictureBoxBoatHeight;
ButtonGroup buttonGroupPaddlesRadBut;
/*
public FormBoat() {
super("Моторная лодка");
buttonGroupPaddlesRadBut = new ButtonGroup();
@ -49,16 +45,24 @@ public class FormBoat extends JFrame {
} catch (Exception c) {
}
Random random = new Random();
_boat = new DrawingBoat();
_boat.Init(random.nextInt(50, 150), random.nextInt(40, 70), new Color(random.nextInt(0, 256),
_boat = new DrawingBoat(random.nextInt(50, 150), random.nextInt(40, 70), new Color(random.nextInt(0, 256),
random.nextInt(0, 256), random.nextInt(0, 256)), GetRollersAmount());
ChangePictureBoxBoatBorders();
_boat.SetPosition(random.nextInt(20, 100), random.nextInt(50, 100), pictureBoxBoatWidth, pictureBoxBoatHeight);
toolStripStatusLabelSpeed.setText("Скорость: " + _boat.Boat.Speed());
toolStripStatusLabelWeight.setText("Вес: " + _boat.Boat.Weight());
toolStripStatusLabelBodyColor.setText("Цвет: " + Integer.toHexString(_boat.Boat.BodyColor().getRGB()));
pictureBoxBoat.add(_boat, BorderLayout.CENTER);
SetData();
}
});
buttonCreateModif.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
pictureBoxBoat.remove(_boat);
} catch (Exception c) {
}
Random random = new Random();
_boat = new DrawningMotorBoat(random.nextInt(30, 50), random.nextInt(1000, 2000),
new Color(random.nextInt(0, 256), random.nextInt(0, 256),
random.nextInt(0, 256)), GetRollersAmount(), new Color(random.nextInt(0, 256), random.nextInt(0, 256),
random.nextInt(0, 256)), random.nextBoolean(), random.nextBoolean());
SetData();
}
});
addComponentListener(new ComponentAdapter() {
@ -85,6 +89,15 @@ public class FormBoat extends JFrame {
buttonLeft.addActionListener(buttonsMove);
buttonRight.addActionListener(buttonsMove);
buttonDown.addActionListener(buttonsMove);
}
private void SetData() {
Random random = new Random();
ChangePictureBoxBoatBorders();
_boat.SetPosition(random.nextInt(20, 100), random.nextInt(50, 100), pictureBoxBoatWidth, pictureBoxBoatHeight);
toolStripStatusLabelSpeed.setText("Скорость: " + _boat.Boat.Speed());
toolStripStatusLabelWeight.setText("Вес: " + _boat.Boat.Weight());
toolStripStatusLabelBodyColor.setText("Цвет: " + Integer.toHexString(_boat.Boat.BodyColor().getRGB()));
pictureBoxBoat.add(_boat, BorderLayout.CENTER);
}
class ButtonsMove implements ActionListener {
public void actionPerformed(ActionEvent e) {
@ -121,5 +134,5 @@ public class FormBoat extends JFrame {
} else {
return 3;
}
}
}*/
}

213
src/FormMap.form Normal file
View File

@ -0,0 +1,213 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="FormMap">
<grid id="27dc6" binding="mainPanel" layout-manager="GridLayoutManager" row-count="6" column-count="7" 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="766" height="400"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<grid id="bbded" binding="pictureBoxBoat" layout-manager="BorderLayout" hgap="0" vgap="0">
<constraints>
<grid row="0" column="0" row-span="2" col-span="6" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<background color="-5185294"/>
</properties>
<border type="none"/>
<children/>
</grid>
<grid id="59ac9" binding="radioButtonsBox" layout-manager="GridLayoutManager" row-count="2" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="3" column="0" row-span="3" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="95721" class="javax.swing.JRadioButton" binding="radioButtonPaddle1">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<selected value="true"/>
<text value="Одно весло"/>
</properties>
</component>
<component id="58261" class="javax.swing.JRadioButton" binding="radioButtonPaddle2">
<constraints>
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Два весла"/>
</properties>
</component>
<component id="fe956" class="javax.swing.JRadioButton" binding="radioButtonPaddle3">
<constraints>
<grid row="1" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Три весла"/>
</properties>
</component>
<grid id="ba39f" binding="statusStrip" layout-manager="GridLayoutManager" row-count="4" column-count="4" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="0" row-span="1" col-span="3" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="7fb0a" class="javax.swing.JLabel" binding="toolStripStatusLabelBodyColor">
<constraints>
<grid row="0" column="2" row-span="4" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Цвет: "/>
</properties>
</component>
<component id="4180a" class="javax.swing.JLabel" binding="toolStripStatusLabelSpeed">
<constraints>
<grid row="0" column="0" row-span="4" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Скорость: "/>
</properties>
</component>
<hspacer id="42ce">
<constraints>
<grid row="1" column="3" row-span="3" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<component id="2ba60" class="javax.swing.JLabel" binding="toolStripStatusLabelWeight">
<constraints>
<grid row="0" column="1" row-span="4" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Вес: "/>
</properties>
</component>
</children>
</grid>
</children>
</grid>
<vspacer id="93048">
<constraints>
<grid row="1" column="6" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="8" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
<hspacer id="8d179">
<constraints>
<grid row="5" column="1" row-span="1" col-span="3" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<hspacer id="1de1f">
<constraints>
<grid row="3" column="1" row-span="2" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<component id="d05dd" class="javax.swing.JButton" binding="buttonCreate">
<constraints>
<grid row="4" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="2" fill="1" indent="0" use-parent-layout="false">
<minimum-size width="120" height="40"/>
<preferred-size width="120" height="40"/>
<maximum-size width="120" height="40"/>
</grid>
</constraints>
<properties>
<text value="Создать"/>
</properties>
</component>
<grid id="8ef30" binding="buttonsBox" layout-manager="GridLayoutManager" row-count="2" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="3" column="4" row-span="3" col-span="2" vsize-policy="3" hsize-policy="3" anchor="10" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="150" height="150"/>
<preferred-size width="150" height="150"/>
<maximum-size width="150" height="150"/>
</grid>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="68b27" class="javax.swing.JButton" binding="buttonRight">
<constraints>
<grid row="1" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="40" height="40"/>
<preferred-size width="40" height="40"/>
<maximum-size width="40" height="40"/>
</grid>
</constraints>
<properties>
<icon value="images/right.png"/>
<text value=""/>
</properties>
</component>
<component id="80f7e" class="javax.swing.JButton" binding="buttonLeft">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="4" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="40" height="40"/>
<preferred-size width="40" height="40"/>
<maximum-size width="40" height="40"/>
</grid>
</constraints>
<properties>
<icon value="images/left.png"/>
<text value=""/>
</properties>
</component>
<component id="f431c" class="javax.swing.JButton" binding="buttonUp">
<constraints>
<grid row="0" 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="40" height="40"/>
<preferred-size width="40" height="40"/>
<maximum-size width="40" height="40"/>
</grid>
</constraints>
<properties>
<icon value="images/up.png"/>
<text value=""/>
</properties>
</component>
<component id="2c574" class="javax.swing.JButton" binding="buttonDown">
<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="40" height="40"/>
<preferred-size width="40" height="40"/>
<maximum-size width="40" height="40"/>
</grid>
</constraints>
<properties>
<icon value="images/down.png"/>
<text value=""/>
</properties>
</component>
</children>
</grid>
<component id="7ec6a" class="javax.swing.JComboBox" binding="comboBoxSelectorMap">
<constraints>
<grid row="2" 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>
<item value="Простая карта"/>
<item value="Морская карта"/>
</model>
</properties>
</component>
<component id="dcee2" class="javax.swing.JButton" binding="buttonCreateModif">
<constraints>
<grid row="3" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="2" fill="1" indent="0" use-parent-layout="false">
<minimum-size width="120" height="40"/>
<preferred-size width="120" height="40"/>
<maximum-size width="120" height="40"/>
</grid>
</constraints>
<properties>
<text value="Создать модифицированный"/>
</properties>
</component>
</children>
</grid>
</form>

157
src/FormMap.java Normal file
View File

@ -0,0 +1,157 @@
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.util.Random;
public class FormMap extends JFrame {
public static void main(String[] args) {
FormMap window = new FormMap();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.pack();
window.setLocationRelativeTo(null);
window.setVisible(true);
}
private JPanel mainPanel;
private JPanel statusStrip;
private JLabel toolStripStatusLabelSpeed;
private JLabel toolStripStatusLabelBodyColor;
private JLabel toolStripStatusLabelWeight;
private JPanel buttonsBox;
private JButton buttonLeft;
private JButton buttonRight;
private JButton buttonUp;
private JButton buttonDown;
private JButton buttonCreate;
private JRadioButton radioButtonPaddle1;
private JRadioButton radioButtonPaddle2;
private JRadioButton radioButtonPaddle3;
private JPanel radioButtonsBox;
private JPanel pictureBoxBoat;
private JComboBox comboBoxSelectorMap;
private JButton buttonCreateModif;
private DrawingBoat _boat;
private int pictureBoxBoatWidth;
private int pictureBoxBoatHeight;
private AbstractMap _abstractMap;
private JLabel pictureLabel;
private DrawningObjectBoat drawningObjectBoat;
ButtonGroup buttonGroupPaddlesRadBut;
public FormMap() {
super("Моторная лодка");
buttonGroupPaddlesRadBut = new ButtonGroup();
buttonGroupPaddlesRadBut.add(radioButtonPaddle1);
buttonGroupPaddlesRadBut.add(radioButtonPaddle2);
buttonGroupPaddlesRadBut.add(radioButtonPaddle3);
setPreferredSize(new Dimension(1000, 700));
getContentPane().add(mainPanel);
buttonCreate.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Random random = new Random();
var _boat = new DrawingBoat(random.nextInt(50, 150), random.nextInt(40, 70), new Color(random.nextInt(0, 256),
random.nextInt(0, 256), random.nextInt(0, 256)), GetRollersAmount());
SetData(_boat);
}
});
buttonCreateModif.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Random random = new Random();
var _boat = new DrawningMotorBoat(random.nextInt(30, 50), random.nextInt(1000, 2000),
new Color(random.nextInt(0, 256), random.nextInt(0, 256),
random.nextInt(0, 256)), GetRollersAmount(), new Color(random.nextInt(0, 256), random.nextInt(0, 256),
random.nextInt(0, 256)), random.nextBoolean(), random.nextBoolean());
SetData(_boat);
}
});
comboBoxSelectorMap.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
comboBoxSelectorMap = (JComboBox)e.getSource();
String item = String.valueOf(comboBoxSelectorMap.getSelectedItem());
System.out.println(item);
switch (item) {
case "Простая карта"->
_abstractMap = new SimpleMap();
case "Морская карта" ->
_abstractMap = new SeaMap();
}
}
});
//джижение
ButtonsMove buttonsMove = new ButtonsMove();
buttonUp.setName("Up");
buttonLeft.setName("Left");
buttonRight.setName("Right");
buttonDown.setName("Down");
buttonUp.addActionListener(buttonsMove);
buttonLeft.addActionListener(buttonsMove);
buttonRight.addActionListener(buttonsMove);
buttonDown.addActionListener(buttonsMove);
}
private void SetData(DrawingBoat _boat) {
if (_abstractMap == null) return;
try {
pictureBoxBoat.remove(pictureLabel);
} catch (Exception c) { }
ChangePictureBoxBoatBorders();
// _boat.SetPosition(random.nextInt(20, 100), random.nextInt(50, 100), pictureBoxBoatWidth, pictureBoxBoatHeight);
toolStripStatusLabelSpeed.setText("Скорость: " + _boat.Boat.Speed());
toolStripStatusLabelWeight.setText("Вес: " + _boat.Boat.Weight());
toolStripStatusLabelBodyColor.setText("Цвет: " + Integer.toHexString(_boat.Boat.BodyColor().getRGB()));
drawningObjectBoat = new DrawningObjectBoat(_boat);
BufferedImage picture = _abstractMap.CreateMap(pictureBoxBoatWidth, pictureBoxBoatHeight,
drawningObjectBoat);
drawningObjectBoat.DrawingObject(picture.getGraphics());
pictureLabel = new JLabel(new ImageIcon(picture));
pictureBoxBoat.add(pictureLabel, BorderLayout.CENTER);
}
class ButtonsMove implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (_abstractMap == null) return;
pictureBoxBoat.remove(pictureLabel);
pictureBoxBoat.revalidate();
pictureBoxBoat.repaint();
JButton temp = (JButton) e.getSource();
String name = temp.getName();
BufferedImage picture = _abstractMap.MoveObject(Direction.None);
switch (name) {
case "Up" -> picture=_abstractMap.MoveObject(Direction.Up);
case "Right" ->picture= _abstractMap.MoveObject(Direction.Right);
case "Left" -> picture=_abstractMap.MoveObject(Direction.Left);
case "Down" -> picture=_abstractMap.MoveObject(Direction.Down);
}
drawningObjectBoat.DrawingObject(picture.getGraphics());
pictureLabel = new JLabel(new ImageIcon(picture));
pictureBoxBoat.add(pictureLabel, BorderLayout.CENTER);
}
}
private void ChangePictureBoxBoatBorders() {
char[] temp = pictureBoxBoat.getSize().toString().toCharArray();
for (int i = 0; i < temp.length; i++) {
if (!Character.isDigit(temp[i])) {
temp[i] = ' ';
}
}
String width = new String(temp);
String[] parameters = width.split("\\s*(\\s|,|!|\\.)\\s*", 4);
pictureBoxBoatWidth = Integer.parseInt(parameters[1]);
pictureBoxBoatHeight = Integer.parseInt(parameters[2]);
}
private int GetRollersAmount() {
if (radioButtonPaddle1.isSelected()) {
return 1;
}
else if (radioButtonPaddle2.isSelected()) {
return 2;
} else {
return 3;
}
}
}

13
src/IDrawningObject.java Normal file
View File

@ -0,0 +1,13 @@
import java.awt.*;
public interface IDrawningObject {
public float Step();
// Óñòàíîâêà ïîçèöèè îáúåêòà
void SetObject(int x, int y, int width, int height);
// Èçìåíåíèå íàïðàâëåíèÿ ïåðìåùåíèÿ îáúåêòà
void MoveObject(Direction direction);
// Îòðèñîâêà îáúåêòà
void DrawingObject(Graphics g);
// Ïîëó÷åíèå òåêóùåé ïîçèöèè îáúåêòà
float[] GetCurrentPosition();
}

View File

@ -0,0 +1,8 @@
import java.awt.*;
public interface IDrawningObjectPaddle {
//ñâîéñòâî äëÿ ïåðåäà÷è ÷èñëîâîãî çíà÷åíèÿ
void SetPaddlesCount(int rollersAmount);
//ìåòîä, ïðèíèìàþùèé ïàðàìåòðû äëÿ îòðèñîâêè
void DrawPaddles(Graphics gr, int _startPosRollersX, int _startPosRollersY, Color mainBrush);
}

44
src/SeaMap.java Normal file
View File

@ -0,0 +1,44 @@
import java.awt.*;
public class SeaMap extends SimpleMap {
private final Color barrierColor = Color.BLUE;
// Öâåò ó÷àñòêà îòêðûòîãî
private final Color roadColor = Color.CYAN;
@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 < 10)
{
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(Graphics g, int i, int j) {
g.setColor(roadColor);
g.fillRect((int)(i * _size_x), (int)(j * _size_y), (int)(i * (_size_x + 1)), (int)(j * (_size_y + 1)));
}
@Override
protected void DrawBarrierPart(Graphics g, int i, int j) {
g.setColor(barrierColor);
g.fillRect((int)(i * _size_x), (int)(j * _size_y),(int) (i * (_size_x + 1)), (int)(j * (_size_y + 1)));
}
}

45
src/SimpleMap.java Normal file
View File

@ -0,0 +1,45 @@
import java.awt.*;
public class SimpleMap extends AbstractMap {
private final Color barrierColor = Color.BLACK;
// Öâåò ó÷àñòêà îòêðûòîãî
private final Color roadColor = Color.GRAY;
@Override
protected void 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 < 10)
{
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(Graphics g, int i, int j) {
g.setColor(roadColor);
g.fillRect((int)(i * _size_x), (int)(j * _size_y), (int)(i * (_size_x + 1)), (int)(j * (_size_y + 1)));
}
@Override
protected void DrawBarrierPart(Graphics g, int i, int j) {
g.setColor(barrierColor);
g.fillRect((int)(i * _size_x), (int)(j * _size_y),(int) (i * (_size_x + 1)), (int)(j * (_size_y + 1)));
}
}