Bondarenko M.S. Lab Work 2 #2

Closed
maxnes3 wants to merge 10 commits from LabWork_02 into LabWork_01
20 changed files with 886 additions and 78 deletions

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 Random _random = new Random();
protected int _freeRoad = 0;
protected 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 CanMove(float[] position) //Проверка на возможность шага
{
for (int i = (int)(position[2] / _size_y); i <= (int)(position[3] / _size_y); ++i)
{
for (int j = (int)(position[0] / _size_x); j <= (int)(position[1] / _size_x); ++j)
{
if (i >= 0 && j >= 0 && i < _map.length && j < _map[0].length && _map[i][j] == _barrier) return false;
}
}
return true;
}
public BufferedImage MoveObject(Direction direction)
{
float[] position = _drawningObject.GetCurrentPosition();
if (direction == Direction.Left)
{
position[0] -= _drawningObject.getStep();
}
else if (direction == Direction.Right)
{
position[1] += _drawningObject.getStep();
}
else if (direction == Direction.Up)
{
position[2] -= _drawningObject.getStep();
}
else if (direction == Direction.Down)
{
position[3] += _drawningObject.getStep();
}
if (CanMove(position))
{
_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);
for (int i = (int)(_drawningObject.GetCurrentPosition()[2] / _size_y); i <= (int)(_drawningObject.GetCurrentPosition()[3] / _size_y); ++i)
{
for (int j = (int)(_drawningObject.GetCurrentPosition()[0] / _size_x); j <= (int)(_drawningObject.GetCurrentPosition()[1] / _size_x); ++j)
{
if (_map[i][j] == _barrier) _map[i][j] = _freeRoad;
}
}
return true;
}
private BufferedImage DrawMapWithObject()
{
BufferedImage bmp = new BufferedImage(_width, _height, BufferedImage.TYPE_INT_RGB);
if (_drawningObject == null || _map == null)
{
return bmp;
}
Graphics g = 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(g, i, j);
}
else if (_map[i][j] == _barrier)
{
DrawBarrierPart(g, i, j);
}
}
}
_drawningObject.DrawningObject(g);
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(0), //None
Left(1), //Влево
Up(2), //Вверх
Right(3), //Вправо

View File

@ -1,33 +1,32 @@
import java.awt.*;
public class DrawDeck {
public class DrawDeck implements IDrawningDeck {
private Deck deckCount;
@Override
public void SetDeckCount(int count){
deckCount = Deck.GetDeck(count);
}
@Override
public void DrawningDeck(float _startPosX, float _startPosY, int _warmlyShipWidth, Graphics2D g2d, Color bodyColor){
int count = 1;
switch (deckCount)
{
case One:
break;
case Two:
g2d.setColor(bodyColor);
g2d.fillRect((int)(_startPosX + _warmlyShipWidth / 5), (int)_startPosY - 20, _warmlyShipWidth * 3 / 5, 20);
g2d.setColor(Color.BLACK);
g2d.drawRect((int)(_startPosX + _warmlyShipWidth / 5), (int)_startPosY - 20, _warmlyShipWidth * 3 / 5, 20);
count = 2;
break;
case Three:
for (int i = 1; i < 3; ++i){
g2d.setColor(bodyColor);
g2d.fillRect((int)(_startPosX + _warmlyShipWidth / 5), (int)_startPosY - 20 * i, _warmlyShipWidth * 3 / 5, 20);
g2d.setColor(Color.BLACK);
g2d.drawRect((int)(_startPosX + _warmlyShipWidth / 5), (int)_startPosY - 20 * i, _warmlyShipWidth * 3 / 5, 20);
}
count = 3;
break;
}
for (int i = 1; i < count; ++i){
g2d.setColor(bodyColor);
g2d.fillRect((int)(_startPosX + _warmlyShipWidth / 5), (int)_startPosY - 20 * i, _warmlyShipWidth * 3 / 5, 20);
g2d.setColor(Color.BLACK);
g2d.drawRect((int)(_startPosX + _warmlyShipWidth / 5), (int)_startPosY - 20 * i, _warmlyShipWidth * 3 / 5, 20);
}
}
}

32
DrawGuns.java Normal file
View File

@ -0,0 +1,32 @@
import java.awt.*;
public class DrawGuns implements IDrawningDeck {
private Deck gunsCount;
@Override
public void SetDeckCount(int count) {
gunsCount = Deck.GetDeck(count);
}
@Override
public void DrawningDeck(float _startPosX, float _startPosY, int _warmlyShipWidth, Graphics2D g2d, Color bodyColor) {
int count = 1;
switch (gunsCount)
{
case One:
break;
case Two:
count = 2;
break;
case Three:
count = 3;
break;
}
g2d.setColor(Color.DARK_GRAY);
for (int i = 0; i < count; ++i)
{
g2d.fillPolygon(new Polygon(new int[]{(int)(_startPosX + _warmlyShipWidth / 5 * (i + 1)) + 10, (int)(_startPosX + _warmlyShipWidth / 5 * (i + 1)) + 25, (int)(_startPosX + _warmlyShipWidth / 5 * (i + 1)) + 40, (int)(_startPosX + _warmlyShipWidth / 5 * (i + 1)) + 25}, new int[]{(int)_startPosY - 20, (int)_startPosY - 10, (int)_startPosY - 30, (int)_startPosY - 40}, 4));
g2d.fillOval((int)(_startPosX + _warmlyShipWidth / 5 * (i + 1)), (int)_startPosY - 20, 25, 20);
}
}
}

32
DrawOvalDeck.java Normal file
View File

@ -0,0 +1,32 @@
import java.awt.*;
public class DrawOvalDeck implements IDrawningDeck{
private Deck deckCount;
@Override
public void SetDeckCount(int count) {
deckCount = Deck.GetDeck(count);
}
@Override
public void DrawningDeck(float _startPosX, float _startPosY, int _warmlyShipWidth, Graphics2D g2d, Color bodyColor) {
int count = 1;
switch (deckCount)
{
case One:
break;
case Two:
count = 2;
break;
case Three:
count = 3;
break;
}
for (int i = 1; i < count; ++i){
g2d.setColor(bodyColor);
g2d.fillOval((int)(_startPosX + _warmlyShipWidth / 5), (int)_startPosY - 20 * i, _warmlyShipWidth * 3 / 5, 20);
g2d.setColor(Color.BLACK);
g2d.drawOval((int)(_startPosX + _warmlyShipWidth / 5), (int)_startPosY - 20 * i, _warmlyShipWidth * 3 / 5, 20);
}
}
}

View File

@ -3,25 +3,42 @@ import java.awt.*;
import java.util.*;
public class DrawingShip extends JPanel {
private EntityWarmlyShip warmlyShip; //Класс-сущность
public EntityWarmlyShip warmlyShip; //Класс-сущность
public EntityWarmlyShip GetWarmlyShip(){return warmlyShip;}
public float _startPosX; //Координаты отрисовки по оси x
public float _startPosY; //Координаты отрисовки по оси y
private Integer _pictureWidth = null; //Ширина окна
private Integer _pictureHeight = null; //Высота окна
private final int _warmlyShipWidth = 125; //Ширина отрисовки корабля
private final int _warmlyShipHeight = 50; //Высота отрисовки корабля
protected int _warmlyShipWidth = 125; //Ширина отрисовки корабля
protected int _warmlyShipHeight = 50; //Высота отрисовки корабля
private int deckCount = 1;
private DrawDeck dd = new DrawDeck();
private IDrawningDeck idd;
//Инициализация
public void Init(int speed, float weight, Color bodyColor)
public DrawingShip(int speed, float weight, Color bodyColor)
{
warmlyShip = new EntityWarmlyShip();
warmlyShip.Init(speed, weight, bodyColor);
warmlyShip = new EntityWarmlyShip(speed, weight, bodyColor);
Random random = new Random();
dd.SetDeckCount(random.nextInt(1, 4));
switch (random.nextInt(3))
{
case 0:
idd = new DrawDeck();
break;
case 1:
idd = new DrawGuns();
break;
case 2:
idd = new DrawOvalDeck();
break;
}
idd.SetDeckCount(random.nextInt(1, 4));
}
protected DrawingShip(int speed, float weight, Color bodyColor, int warmlyWidth, int warmlyHeight)
{
this(speed, weight, bodyColor);
_warmlyShipWidth = warmlyWidth;
_warmlyShipHeight = warmlyHeight;
}
//Начальные коордитанты
@ -57,24 +74,14 @@ public class DrawingShip extends JPanel {
}
//Отрисовка транспорта
public void DrawTransport()
public void DrawTransport(Graphics g)
{
if (_startPosX < 0 || _startPosY < 0 || _pictureWidth == null || _pictureHeight == null)
{
return;
}
repaint();
}
@Override
public void paintComponent(Graphics g){
if (GetWarmlyShip() == null) return;
if (_startPosX < 0 || _startPosY < 0 || _pictureWidth == null || _pictureHeight == null)
{
return;
}
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(warmlyShip.GetBodyColor());
int[] xPol = new int[] {(int)(_startPosX), (int)(_startPosX + _warmlyShipWidth), (int)(_startPosX + _warmlyShipWidth - 25), (int)(_startPosX + 25)};
@ -92,7 +99,7 @@ public class DrawingShip extends JPanel {
g2d.drawOval((int)(_startPosX + _warmlyShipWidth / 5), (int)_startPosY + 25, 20, 20);
g2d.drawOval((int)(_startPosX + _warmlyShipWidth * 3 / 5 + 5), (int)_startPosY + 25, 20, 20);
g2d.drawOval((int)(_startPosX + _warmlyShipWidth * 2 / 5 + 2.5f), (int)_startPosY + 25, 20, 20);
dd.DrawningDeck(_startPosX, _startPosY, _warmlyShipWidth, g2d, warmlyShip.GetBodyColor());
idd.DrawningDeck(_startPosX, _startPosY, _warmlyShipWidth, g2d, warmlyShip.GetBodyColor());
}
//Изменение границ отрисовки
@ -116,4 +123,8 @@ public class DrawingShip extends JPanel {
}
}
public float[] GetCurrentPosition() {
return new float[]{_startPosX, _startPosX + _warmlyShipWidth, _startPosY, _startPosY + _warmlyShipHeight};
}
}

45
DrawningMotorShip.java Normal file
View File

@ -0,0 +1,45 @@
import java.awt.*;
public class DrawningMotorShip extends DrawingShip {
public DrawningMotorShip(int speed, float weight, Color bodyColor, Color dopColor, boolean tubes, boolean cistern)
{
super(speed, weight, bodyColor, 150, 75);
warmlyShip = new EntityMotorShip(speed, weight, bodyColor, dopColor, tubes , cistern);
}
@Override
public void DrawTransport(Graphics g)
{
if (!(warmlyShip instanceof EntityMotorShip motorShip))
{
return;
}
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.BLACK);
g2d.setColor(motorShip.GetDopColor());
if (motorShip.GetTubes())
{
g2d.setColor(motorShip.GetDopColor());
g2d.fillRect((int)_startPosX + _warmlyShipWidth / 5, (int)_startPosY, _warmlyShipWidth / 5, _warmlyShipHeight / 2);
g2d.setColor(Color.BLACK);
g2d.drawRect((int)_startPosX + _warmlyShipWidth / 5, (int)_startPosY, _warmlyShipWidth / 5, _warmlyShipHeight / 2);
g2d.setColor(motorShip.GetDopColor());
g2d.fillRect((int)_startPosX + _warmlyShipWidth * 3 / 5, (int)_startPosY, _warmlyShipWidth / 5, _warmlyShipHeight / 2);
g2d.setColor(Color.BLACK);
g2d.drawRect((int)_startPosX + _warmlyShipWidth * 3 / 5, (int)_startPosY, _warmlyShipWidth / 5, _warmlyShipHeight / 2);
}
_startPosY += 25;
super.DrawTransport(g);
_startPosY -= 25;
if (motorShip.GetCistern())
{
g2d.setColor(motorShip.GetDopColor());
g2d.fillOval((int)_startPosX, (int)_startPosY + 25, 25, 20);
g2d.setColor(Color.BLACK);
g2d.drawOval((int)_startPosX, (int)_startPosY + 25, 25, 20);
}
}
}

38
DrawningObjectShip.java Normal file
View File

@ -0,0 +1,38 @@
import java.awt.*;
public class DrawningObjectShip implements IDrawningObject {
private DrawingShip _warmlyShip = null;
public DrawningObjectShip(DrawingShip warmlyShip)
{
_warmlyShip = warmlyShip;
}
@Override
public float getStep() {
if (_warmlyShip == null || _warmlyShip.warmlyShip == null) return 0;
return _warmlyShip.warmlyShip.GetStep();
}
@Override
public void SetObject(int x, int y, int width, int height) {
_warmlyShip.SetPosition(x, y, width, height);
}
@Override
public void MoveObject(Direction direction) {
_warmlyShip.MoveTransport(direction);
}
@Override
public void DrawningObject(Graphics g) {
_warmlyShip.DrawTransport(g);
}
@Override
public float[] GetCurrentPosition() {
if (_warmlyShip == null || _warmlyShip.warmlyShip == null) return null;
return _warmlyShip.GetCurrentPosition();
}
}

21
EntityMotorShip.java Normal file
View File

@ -0,0 +1,21 @@
import java.awt.*;
public class EntityMotorShip extends EntityWarmlyShip {
private Color DopColor;
private boolean Tubes;
private boolean Cistern;
public EntityMotorShip(int speed, float weight, Color bodyColor, Color dopColor, boolean tubes, boolean cistern)
{
super(speed, weight, bodyColor);
DopColor = dopColor;
Tubes = tubes;
Cistern = cistern;
}
public Color GetDopColor(){return DopColor;}
public boolean GetTubes(){return Tubes;}
public boolean GetCistern(){return Cistern;}
}

View File

@ -8,7 +8,7 @@ public class EntityWarmlyShip {
private float Step; //Шаг при перемещении
//Инициализация
public void Init(int speed, float weight, Color bodyColor)
public EntityWarmlyShip(int speed, float weight, Color bodyColor)
{
Random random = new Random();
Speed = speed <= 0 ? random.nextInt(50, 150) : speed;

133
FormMap.form Normal file
View File

@ -0,0 +1,133 @@
<?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="JPanel" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="500" height="400"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<grid id="bd11a" 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>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<opaque value="true"/>
<preferredSize width="800" height="600"/>
</properties>
<border type="none"/>
<children>
<component id="aa94c" 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="579c4" 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>
<component id="43c16" 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>
<toolbar id="595a9" 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="769a2" 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">
<preferred-size width="122" height="30"/>
</grid>
</constraints>
<properties>
<text value="Создать"/>
</properties>
</component>
<component id="b0694" 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>
<grid id="55dd2" binding="GraphicsOutput" 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="b8384" class="javax.swing.JButton" binding="buttonCreateModif">
<constraints>
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="122" height="30"/>
</grid>
</constraints>
<properties>
<text value="Модификация"/>
</properties>
</component>
<component id="a36c3" 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/>
</component>
<hspacer id="c0027">
<constraints>
<grid row="2" column="1" row-span="1" col-span="2" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<hspacer id="7e42b">
<constraints>
<grid row="1" column="2" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
</children>
</grid>
</children>
</grid>
</form>

159
FormMap.java Normal file
View File

@ -0,0 +1,159 @@
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
public class FormMap extends JFrame {
private AbstractMap _abstractMap;
private JPanel JPanel;
private JButton buttonDown;
private JButton buttonRight;
private JButton buttonUp;
private JToolBar statusStrip;
private JButton buttonCreate;
private JButton buttonLeft;
private JPanel GraphicsOutput;
private JButton buttonCreateModif;
public JPanel Mainpanel;
private JComboBox comboBoxSelectorMap;
private JLabel JLabelSpeed = new JLabel();
private JLabel JLabelWeight = new JLabel();
private JLabel JLabelColor = new JLabel();
private void SetData(DrawingShip ship)
{
Random random = new Random();
GraphicsOutput.removeAll();
ship.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100), GraphicsOutput.getWidth(), GraphicsOutput.getHeight());
JLabelSpeed.setText("орость: " + ship.GetWarmlyShip().GetSpeed() + " ");
JLabelWeight.setText("Вес: " + ship.GetWarmlyShip().GetWeight() + " ");
JLabelColor.setText(("Цвет: " + ship.GetWarmlyShip().GetBodyColor() + " "));
JLabel imageOfShip = new JLabel();
imageOfShip.setPreferredSize(GraphicsOutput.getSize());
imageOfShip.setMinimumSize(new Dimension(1, 1));
imageOfShip.setIcon(new ImageIcon(_abstractMap.CreateMap(GraphicsOutput.getWidth(), GraphicsOutput.getHeight(), new DrawningObjectShip(ship))));
GraphicsOutput.add(imageOfShip,BorderLayout.CENTER);
GraphicsOutput.revalidate();
GraphicsOutput.repaint();
}
private void ButtonMove_Click(String name)
{
if (_abstractMap == 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;
}
GraphicsOutput.removeAll();
JLabel imageOfShip = new JLabel();
imageOfShip.setPreferredSize(GraphicsOutput.getSize());
imageOfShip.setMinimumSize(new Dimension(1, 1));
imageOfShip.setIcon(new ImageIcon(_abstractMap.MoveObject(direction)));
GraphicsOutput.add(imageOfShip,BorderLayout.CENTER);
GraphicsOutput.revalidate();
GraphicsOutput.repaint();
}
public FormMap() {
Box LabelBox = Box.createHorizontalBox();
LabelBox.setMinimumSize(new Dimension(1, 20));
LabelBox.add(JLabelSpeed);
LabelBox.add(JLabelWeight);
LabelBox.add(JLabelColor);
statusStrip.add(LabelBox);
comboBoxSelectorMap.addItem("Простая карта");
comboBoxSelectorMap.addItem("Вторая карта");
comboBoxSelectorMap.addItem("Последняя карта");
_abstractMap = new SimpleMap();
try {
Image img = ImageIO.read(FormShip.class.getResource("/Images/totop.png"));
buttonUp.setIcon(new ImageIcon(img));
img = ImageIO.read(FormShip.class.getResource("/Images/toleft.png"));
buttonLeft.setIcon(new ImageIcon(img));
img = ImageIO.read(FormShip.class.getResource("/Images/todown.png"));
buttonDown.setIcon(new ImageIcon(img));
img = ImageIO.read(FormShip.class.getResource("/Images/toright.png"));
buttonRight.setIcon(new ImageIcon(img));
} catch (Exception ex) {
System.out.println(ex);
}
buttonCreate.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Random random = new Random();
var ship = new DrawingShip(random.nextInt(100, 300), random.nextInt(1000, 2000), new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)));
SetData(ship);
}
});
buttonUp.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ButtonMove_Click("buttonUp");
}
});
buttonLeft.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ButtonMove_Click("buttonLeft");
}
});
buttonDown.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ButtonMove_Click("buttonDown");
}
});
buttonRight.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ButtonMove_Click("buttonRight");
}
});
buttonCreateModif.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Random random = new Random();
var ship = new DrawningMotorShip(random.nextInt(100, 300), random.nextInt(1000, 3000),
new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)),
new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)),
random.nextBoolean(), random.nextBoolean());
SetData(ship);
}
});
comboBoxSelectorMap.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
switch (comboBoxSelectorMap.getSelectedItem().toString())
{
case "Простая карта":
_abstractMap = new SimpleMap();
break;
case "Вторая карта":
_abstractMap = new SecondMap();
break;
case "Последняя карта":
_abstractMap = new LastMap();
break;
}
}
});
}
}

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="FormShip">
<grid id="27dc6" binding="Mainpanel" layout-manager="GridLayoutManager" row-count="4" column-count="5" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<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="500" height="400"/>
@ -13,7 +13,7 @@
<children>
<component id="2ea16" class="javax.swing.JButton" binding="buttonDown">
<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">
<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"/>
@ -25,7 +25,7 @@
</component>
<component id="4eb88" class="javax.swing.JButton" binding="buttonRight">
<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">
<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"/>
@ -36,17 +36,9 @@
<text value=""/>
</properties>
</component>
<component id="9f55" class="DrawingShip" binding="pictureShip">
<constraints>
<grid row="0" column="0" row-span="1" col-span="5" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false">
<preferred-size width="10" height="286"/>
</grid>
</constraints>
<properties/>
</component>
<component id="fb937" class="javax.swing.JButton" binding="buttonUp">
<constraints>
<grid row="1" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="3" indent="0" use-parent-layout="false">
<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"/>
@ -58,7 +50,7 @@
</component>
<toolbar id="e747d" binding="statusStrip">
<constraints>
<grid row="3" column="0" row-span="1" col-span="5" vsize-policy="0" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false">
<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"/>
@ -70,20 +62,10 @@
<border type="none"/>
<children/>
</toolbar>
<hspacer id="d6bea">
<constraints>
<grid row="1" column="1" row-span="1" col-span="2" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<hspacer id="d86ff">
<constraints>
<grid row="2" column="0" row-span="1" col-span="2" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<component id="9e2ce" class="javax.swing.JButton" binding="buttonCreate">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="118" height="30"/>
<preferred-size width="122" height="30"/>
</grid>
</constraints>
<properties>
@ -92,7 +74,7 @@
</component>
<component id="4f60a" class="javax.swing.JButton" binding="buttonLeft">
<constraints>
<grid row="2" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="3" indent="0" use-parent-layout="false">
<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"/>
@ -102,6 +84,34 @@
<text value=""/>
</properties>
</component>
<grid id="8b0ad" binding="GraphicsOutput" 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="bbe04" class="javax.swing.JButton" binding="buttonCreateModif">
<constraints>
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="122" height="30"/>
</grid>
</constraints>
<properties>
<text value="Модификация"/>
</properties>
</component>
<hspacer id="a48ad">
<constraints>
<grid row="1" column="2" row-span="1" col-span="2" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<hspacer id="b7fa3">
<constraints>
<grid row="2" column="0" row-span="1" col-span="3" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
</children>
</grid>
</form>

View File

@ -5,43 +5,66 @@ import java.awt.event.*;
import java.awt.image.*;
import java.util.*;
public class FormShip {
public class FormShip extends JFrame{
private JToolBar statusStrip;
public JPanel Mainpanel;
private DrawingShip pictureShip;
private DrawingShip ship;
private JButton buttonRight;
private JButton buttonCreate;
private JButton buttonLeft;
private JButton buttonUp;
private JButton buttonDown;
private JPanel GraphicsOutput;
private JButton buttonCreateModif;
private JLabel JLabelSpeed = new JLabel();
private JLabel JLabelWeight = new JLabel();
private JLabel JLabelColor = new JLabel();
private void Draw()
{
if (pictureShip.GetWarmlyShip() == null) return;
pictureShip.DrawTransport();
if (ship == null || ship.GetWarmlyShip() == null) return;
GraphicsOutput.removeAll();
BufferedImage bmp = new BufferedImage(GraphicsOutput.getWidth(), GraphicsOutput.getHeight(),BufferedImage.TYPE_INT_RGB);
Graphics g = bmp.getGraphics();
g.setColor(new Color(238, 238, 238));
g.fillRect(0, 0, GraphicsOutput.getWidth(), GraphicsOutput.getHeight());
ship.DrawTransport(g);
JLabel imageOfShip = new JLabel();
imageOfShip.setPreferredSize(GraphicsOutput.getSize());
imageOfShip.setMinimumSize(new Dimension(1, 1));
imageOfShip.setIcon(new ImageIcon(bmp));
GraphicsOutput.add(imageOfShip,BorderLayout.CENTER);
validate();
}
private void SetData()
{
Random random = new Random();
ship.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100), GraphicsOutput.getWidth(), GraphicsOutput.getHeight());
JLabelSpeed.setText("орость: " + ship.GetWarmlyShip().GetSpeed() + " ");
JLabelWeight.setText("Вес: " + ship.GetWarmlyShip().GetWeight() + " ");
JLabelColor.setText(("Цвет: " + ship.GetWarmlyShip().GetBodyColor() + " "));
}
private void ButtonMove_Click(String name)
{
if (pictureShip == null) return;
if (ship == null) return;
switch (name)
{
case "buttonLeft":
pictureShip.MoveTransport(Direction.Left);
ship.MoveTransport(Direction.Left);
break;
case "buttonUp":
pictureShip.MoveTransport(Direction.Up);
ship.MoveTransport(Direction.Up);
break;
case "buttonRight":
pictureShip.MoveTransport(Direction.Right);
ship.MoveTransport(Direction.Right);
break;
case "buttonDown":
pictureShip.MoveTransport(Direction.Down);
ship.MoveTransport(Direction.Down);
break;
}
GraphicsOutput.revalidate();
Draw();
}
@ -70,11 +93,8 @@ public class FormShip {
@Override
public void actionPerformed(ActionEvent e) {
Random random = new Random();
pictureShip.Init(random.nextInt(100, 300), random.nextInt(1000, 2000), new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)));
pictureShip.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100), pictureShip.getWidth(), pictureShip.getHeight());
JLabelSpeed.setText("орость: " + pictureShip.GetWarmlyShip().GetSpeed() + " ");
JLabelWeight.setText("Вес: " + pictureShip.GetWarmlyShip().GetWeight() + " ");
JLabelColor.setText(("Цвет: " + pictureShip.GetWarmlyShip().GetBodyColor() + " "));
ship = new DrawingShip(random.nextInt(100, 300), random.nextInt(1000, 2000), new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)));
SetData();
Draw();
}
});
@ -102,11 +122,25 @@ public class FormShip {
ButtonMove_Click("buttonRight");
}
});
pictureShip.addComponentListener(new ComponentAdapter() {
GraphicsOutput.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
super.componentResized(e);
pictureShip.ChangeBorders(pictureShip.getWidth(), pictureShip.getHeight());
if (ship == null || ship.GetWarmlyShip() == null) return;
ship.ChangeBorders(GraphicsOutput.getWidth(), GraphicsOutput.getHeight());
GraphicsOutput.revalidate();
Draw();
}
});
buttonCreateModif.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Random random = new Random();
ship = new DrawningMotorShip(random.nextInt(100, 300), random.nextInt(1000, 3000),
new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)),
new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)),
random.nextBoolean(), random.nextBoolean());
SetData();
Draw();
}
});

6
IDrawningDeck.java Normal file
View File

@ -0,0 +1,6 @@
import java.awt.*;
public interface IDrawningDeck {
void SetDeckCount(int count);
void DrawningDeck(float _startPosX, float _startPosY, int _warmlyShipWidth, Graphics2D g2d, Color bodyColor);
}

9
IDrawningObject.java Normal file
View File

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

56
LastMap.java Normal file
View File

@ -0,0 +1,56 @@
import java.awt.*;
public class LastMap extends AbstractMap{
private Color barrierColor = Color.RED;
private Color roadColor = Color.GREEN;
@Override
protected void DrawBarrierPart(Graphics g, int i, int j)
{
Graphics2D g2d = (Graphics2D) g;
g2d.setPaint(barrierColor);
g2d.fillRect((int)Math.floor(j * _size_x), (int)Math.floor(i * _size_y),(int)Math.ceil(_size_x), (int)Math.ceil(_size_y));
}
@Override
protected void DrawRoadPart(Graphics g, int i, int j)
{
Graphics2D g2d = (Graphics2D) g;
g2d.setPaint(roadColor);
g2d.fillRect((int)Math.floor(j * _size_x), (int)Math.floor(i * _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 < 45)
{
int x = _random.nextInt(0, 100);
int y = _random.nextInt(0, 100);
if (_map[x][y] == _freeRoad)
{
_map[x][y] = _barrier;
if (x > 0 && y > 0 && x < _map.length - 1 && y < _map[0].length - 1)
{
_map[x - 1][y] = _barrier;
_map[x + 1][y] = _barrier;
_map[x][y - 1] = _barrier;
_map[x][y + 1] = _barrier;
}
counter++;
}
}
}
}

View File

@ -2,8 +2,8 @@ import javax.swing.*;
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame("Hard №1");
frame.setContentPane(new FormShip().Mainpanel);
JFrame frame = new JFrame("Hard №2");
frame.setContentPane(new FormMap().Mainpanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocation(500, 200);
frame.pack();

58
SecondMap.java Normal file
View File

@ -0,0 +1,58 @@
import java.awt.*;
public class SecondMap extends AbstractMap{
private Color barrierColor = Color.ORANGE;
private Color roadColor = Color.BLACK;
@Override
protected void DrawBarrierPart(Graphics g, int i, int j)
{
Graphics2D g2d = (Graphics2D) g;
g2d.setPaint(barrierColor);
g2d.fillRect((int)Math.floor(j * _size_x), (int)Math.floor(i * _size_y),(int)Math.ceil(_size_x), (int)Math.ceil(_size_y));
}
@Override
protected void DrawRoadPart(Graphics g, int i, int j)
{
Graphics2D g2d = (Graphics2D) g;
g2d.setPaint(roadColor);
g2d.fillRect((int)Math.floor(j * _size_x), (int)Math.floor(i * _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;
}
}
for (int i = 0; i < _map.length; ++i)
{
_map[i][_map[0].length / 2] = _barrier;
_map[i][_map[0].length - 1] = _barrier;
}
for (int j = 0; j < _map[0].length; ++j)
{
_map[_map.length - 1][j] = _barrier;
}
while (counter < 45)
{
int x = _random.nextInt(0, 100);
int y = _random.nextInt(0, 100);
if (_map[x][y] == _freeRoad)
{
_map[x][y] = _barrier;
counter++;
}
}
}
}

49
SimpleMap.java Normal file
View File

@ -0,0 +1,49 @@
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 DrawBarrierPart(Graphics g, int i, int j)
{
Graphics2D g2d = (Graphics2D) g;
g2d.setPaint(barrierColor);
g2d.fillRect((int)Math.floor(j * _size_x), (int)Math.floor(i * _size_y),(int)Math.ceil(_size_x), (int)Math.ceil(_size_y));
}
@Override
protected void DrawRoadPart(Graphics g, int i, int j)
{
Graphics2D g2d = (Graphics2D) g;
g2d.setPaint(roadColor);
g2d.fillRect((int)Math.floor(j * _size_x), (int)Math.floor(i * _size_y),(int)Math.ceil(_size_x), (int)Math.ceil(_size_y));
}
}