Принятая лаба

This commit is contained in:
dimazhelovanov 2022-12-06 16:23:28 +03:00
parent 92be17d6f6
commit df4d079c07
15 changed files with 739 additions and 32 deletions

158
AbstractMap.java Normal file
View File

@ -0,0 +1,158 @@
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
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();
}
public BufferedImage MoveObject(Direction direction)
{
int _startX = (int) (_drawningObject.GetCurrentPosition()[3] / _size_x);
int _startY = (int)(_drawningObject.GetCurrentPosition()[0] / _size_y);
int _objWidth = (int)(_drawningObject.GetCurrentPosition()[1] / _size_x);
int _objHeight = (int)(_drawningObject.GetCurrentPosition()[2] / _size_y);
boolean isMove = true;
switch (direction)
{
case Right:
for (int i = _objWidth; i <= _objWidth + (int)(_drawningObject.getStep() / _size_x); i++)
{
for (int j = _startY; j <= _objHeight; j++)
{
if (_map[i][j] == _barrier)
{
isMove = false;
break;
}
}
}
break;
case Left:
for (int i = _startX; i >= (int)(_drawningObject.getStep() / _size_x); i--)
{
for (int j = _startY; j <= _objHeight; j++)
{
if (_map[i][j] == _barrier)
{
isMove = false;
break;
}
}
}
break;
case Up:
for (int i = _startX; i <= _objWidth; i++)
{
for (int j = _startY; j >= (int)(_drawningObject.getStep() / _size_y); j--)
{
if (_map[i][j] == _barrier)
{
isMove = false;
break;
}
}
}
break;
case Down:
for (int i = _startX; i <= _objWidth; i++)
{
for (int j = _objHeight; j <= _objHeight + (int)(_drawningObject.getStep() / _size_y); j++)
{
if (_map[i][j] == _barrier)
{
isMove = false;
break;
}
}
}
break;
}
if (isMove)
{
_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);
// TODO првоерка, что объект не "накладывается" на закрытые участки
for (int i = 0; i < _map.length; ++i)
{
for (int j = 0; j < _map[0].length; ++j)
{
if (i * _size_x >= x && j * _size_y >= y &&
i * _size_x <= x + _drawningObject.GetCurrentPosition()[1] &&
j * _size_y <= j + _drawningObject.GetCurrentPosition()[2])
{
if (_map[i][j] == _barrier)
{
return false;
}
}
}
}
return true;
}
private BufferedImage DrawMapWithObject()
{
BufferedImage bmp = new BufferedImage(_width, _height, BufferedImage.TYPE_INT_RGB);
if (_drawningObject == null || _map == null)
{
return bmp;
}
Graphics gr = bmp.getGraphics();
for (int i = 0; i < _map.length; i++)
{
for (int j = 0; j < _map[0].length; j++)
{
if (_map[i][j] == _freeRoad)
{
DrawRoadPart(gr, i, j);
}
else if (_map[i][j] == _barrier)
{
DrawBarrierPart(gr, i, j);
}
}
}
_drawningObject.DrawningObject(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,18 +1,20 @@
import java.awt.*;
import java.util.Random;
public class DrawningBattleship {
EntityBattleship Battleship;
private DrawningBlocks drawingBlocks;
private IDrawningBlocks iDrawingBlocks;
public EntityBattleship Battleship()
{return Battleship; }
/// <summary>
/// Левая координата отрисовки корабля
/// </summary>
private float _startPosX;
protected float _startPosX;
/// <summary>
/// Верхняя кооридната отрисовки корабля
/// </summary>
private float _startPosY;
protected float _startPosY;
/// <summary>
/// Ширина окна отрисовки
/// </summary>
@ -24,23 +26,44 @@ public class DrawningBattleship {
/// <summary>
/// Ширина отрисовки корабля
/// </summary>
private final int _battleshipWidth = 120;
private int _battleshipWidth = 120;
/// <summary>
/// Высота отрисовки корабля
/// </summary>
private final int _battleshipHeight = 50;
private int _battleshipHeight = 50;
/// <summary>
/// Инициализация свойств
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес корабля</param>
/// <param name="bodyColor">Цвет корпуса</param>
public void Init(int speed, float weight, Color bodyColor, EntityBattleship entityBattleship, int blocks)
public DrawningBattleship(int speed, float weight, Color bodyColor)
{
Battleship = new EntityBattleship();
Battleship.Init(speed, weight, bodyColor);
drawingBlocks = new DrawningBlocks();
drawingBlocks.Init(blocks, Color.black);
//Battleship.Init(speed, weight, bodyColor);
Random random = new Random();
int[] ArrayBlocks = new int[]{2, 4, 6};
switch (random.nextInt(3)){
case 0:
iDrawingBlocks = new DrawningBlocks(ArrayBlocks[random.nextInt(0, 3)], Color.BLACK);
break;
case 1:
iDrawingBlocks = new DrawningRoundBlocks(ArrayBlocks[random.nextInt(0, 3)]);
break;
case 2:
iDrawingBlocks = new DrawningTriangleBlocks(ArrayBlocks[random.nextInt(0, 3)]);
break;
}
Battleship = new EntityBattleship(speed, weight, bodyColor);
}
protected DrawningBattleship(int speed, float weight, Color bodyColor, int battleshipWidth, int battleshipHeight)
{
this(speed, weight, bodyColor);
_battleshipWidth = battleshipWidth;
_battleshipHeight = battleshipHeight;
}
/// <summary>
/// Установка позиции корабля
@ -51,7 +74,6 @@ public class DrawningBattleship {
/// <param name="height">Высота картинки</param>
public void SetPosition(int x, int y, int width, int height)
{
if ((x > 0 && y > 0) && (_battleshipHeight + y < height) && (_battleshipWidth + x < width))
{
_startPosX = x;
@ -137,7 +159,7 @@ public class DrawningBattleship {
g.setColor(Color.BLACK);
g.fillRect((int)_startPosX-5, (int)_startPosY+10, 5, 5);
g.fillRect((int)_startPosX - 5, (int)_startPosY + 35, 5, 5);
drawingBlocks.Draw(g, (int) _startPosX, (int) _startPosY);
iDrawingBlocks.DrawBlocks(g, (int) _startPosX, (int) _startPosY, Color.BLACK);
}
/// <summary>
@ -163,5 +185,10 @@ public class DrawningBattleship {
{
_startPosY = _pictureHeight - _battleshipHeight;
}
}
public float[] GetCurrentPosition()
{
return new float[] {_startPosY, _startPosX + _battleshipWidth, /*DOWN*/ _startPosY + _battleshipHeight, /*LEFT*/ _startPosX};
}
}

View File

@ -1,7 +1,7 @@
import java.awt.*;
public class DrawningBlocks {
private DirectionBlocksOnDeck blocksCount;
public class DrawningBlocks implements IDrawningBlocks {
private DirectionBlocksOnDeck blocksCount = DirectionBlocksOnDeck.Two;
private Color color;
public void SetNewBlocks(int countBlocks){
if (countBlocks == 4) {
@ -13,28 +13,63 @@ public class DrawningBlocks {
blocksCount = DirectionBlocksOnDeck.Two;
}
}
public void Init(int blocksCount, Color color)
{
SetNewBlocks(blocksCount);
public DrawningBlocks(int count, Color color){
SetBlocks(count);
this.color = color;
}
public void Draw(Graphics2D g, int x, int y) {
g.setColor(color != null ? color : Color.BLACK);
switch (blocksCount) {
case Four -> {
g.fillRect(x + 26, y + 10, 10, 5);
g.fillRect(x + 38, y + 10, 10, 5);
g.fillRect(x + 38, y + 35, 10, 5);
g.fillRect(x + 26, y + 35, 10, 5);
g.fillRect(x + 36, y + 5, 10, 5);
g.fillRect(x + 48, y + 5, 10, 5);
g.fillRect(x + 38, y + 40, 10, 5);
g.fillRect(x + 46, y + 40, 10, 5);
}
case Six -> {
g.fillRect(x + 14, y + 10, 10, 5);
g.fillRect(x + 26, y + 10, 10, 5);
g.fillRect(x + 38, y + 10, 10, 5);
g.fillRect(x + 38, y + 35, 10, 5);
g.fillRect(x + 26, y + 35, 10, 5);
g.fillRect(x + 14, y + 35, 10, 5);
g.fillRect(x + 14, y + 5, 10, 5);
g.fillRect(x + 26, y + 5, 10, 5);
g.fillRect(x + 38, y + 5, 10, 5);
g.fillRect(x + 38, y + 40, 10, 5);
g.fillRect(x + 26, y + 40, 10, 5);
g.fillRect(x + 14, y + 40, 10, 5);
}
}
}
@Override
public void DrawBlocks(Graphics g, int x, int y, Color bodyColor) {
g.setColor(color != null ? color : Color.BLACK);
switch (blocksCount) {
case Four -> {
g.fillRect(x + 58, y, 8, 5);
g.fillRect(x + 70, y, 8, 5);
g.fillRect(x + 70, y + 40, 8, 5);
g.fillRect(x + 58, y + 40, 8, 5);
}
case Six -> {
g.fillRect(x + 46, y, 8, 5);
g.fillRect(x + 58, y , 8, 5);
g.fillRect(x + 70, y, 8, 5);
g.fillRect(x + 46, y + 45, 8, 5);
g.fillRect(x + 58, y + 45, 8, 5);
g.fillRect(x + 70, y + 45, 8, 5);
}
}
}
@Override
public void SetBlocks(int count) {
switch(count)
{
case 4:
blocksCount = DirectionBlocksOnDeck.Four;
break;
case 6:
blocksCount = DirectionBlocksOnDeck.Six;
break;
default:
break;
}
}
}

44
DrawningLinkor.java Normal file
View File

@ -0,0 +1,44 @@
import java.awt.*;
public class DrawningLinkor extends DrawningBattleship {
public DrawningLinkor(int speed, float weight, Color bodyColor, Color dopColor, boolean turret, boolean missileBay) {
super(speed, weight, bodyColor, 120, 50);
Battleship = new EntityLinkor(speed, weight, bodyColor, dopColor, turret, missileBay);
}
@Override
public void DrawTransport(Graphics2D g) {
if(!(Battleship instanceof EntityLinkor linkor))
{
return;
}
super.DrawTransport(g);
EntityLinkor entityLinkor = (EntityLinkor) Battleship;
if (entityLinkor.turret)
{
//Brush dopBrushRed = new SolidBrush(Color.Red);
g.setColor(entityLinkor.dopColor);
g.fillOval((int)_startPosX + 15, (int)_startPosY, 20, 20);
g.fillOval((int) _startPosX + 15, (int)_startPosY + 30, 20, 20);
g.setColor(Color.BLACK);
g.fillRect((int)_startPosX + 20, (int)_startPosY+5, 23, 7);
g.fillRect((int)_startPosX + 20,(int) _startPosY + 37, 23, 7);
}
if (entityLinkor.missileBay)
{
g.setColor(Color.BLACK);
g.fillOval((int)_startPosX, (int)_startPosY + 15, 20, 20);
g.setColor(entityLinkor.dopColor);
g.fillOval((int)_startPosX+5,(int) _startPosY + 20, 10, 10);
}
}
}

View File

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

48
DrawningRoundBlocks.java Normal file
View File

@ -0,0 +1,48 @@
import java.awt.*;
public class DrawningRoundBlocks implements IDrawningBlocks{
private DirectionBlocksOnDeck blocksOnDeck = DirectionBlocksOnDeck.Two;
@Override
public void DrawBlocks(Graphics g, int x, int y, Color bodyColor) {
g.setColor(Color.black);
switch(blocksOnDeck){
case Four -> {
g.fillOval(x + 56, y , 8, 8);
g.fillOval(x + 68, y , 8, 8);
g.fillOval(x + 68, y +40, 8, 8);
g.fillOval(x + 56, y + 40, 8, 8);
}
case Six -> {
g.fillOval(x + 54, y , 8, 8);
g.fillOval(x + 66, y , 8, 8);
g.fillOval(x + 78, y , 8, 8);
g.fillOval(x + 78, y + 40, 8, 8);
g.fillOval(x + 66, y + 40, 8, 8);
g.fillOval(x + 54, y + 40, 8, 8);
}}
}
@Override
public void SetBlocks(int count) {
switch(count)
{
case 2:
blocksOnDeck = DirectionBlocksOnDeck.Two;
break;
case 4:
blocksOnDeck = DirectionBlocksOnDeck.Four;
break;
case 6:
blocksOnDeck = DirectionBlocksOnDeck.Six;
break;
default:
break;
}
}
public DrawningRoundBlocks (int num) {
SetBlocks(num);
}
}

View File

@ -0,0 +1,63 @@
import java.awt.*;
public class DrawningTriangleBlocks implements IDrawningBlocks {
private DirectionBlocksOnDeck blocksOnDeck = DirectionBlocksOnDeck.Two;
@Override
public void DrawBlocks(Graphics g, int x, int y, Color bodyColor) {
g.setColor(Color.black);
switch(blocksOnDeck){
case Four -> {
g.fillPolygon(new int[]{x + 46, x + 54, x+ 60 }, new int[]{y + 7, y, y + 7}, 3);
g.fillPolygon(new int[]{x + 46, x + 54, x+ 60 }, new int[]{y + 49, y + 42, y + 49}, 3);
g.fillPolygon(new int[]{x + 58, x + 66, x+ 72}, new int[]{y + 7, y, y + 7}, 3);
g.fillPolygon(new int[]{x + 58, x + 66, x+72}, new int[]{y + 49, y + 42, y + 49}, 3);
// g.fillOval(x + 26, y + 10, 8, 8);
// g.fillOval(x + 38, y + 10, 8, 8);
// g.fillOval(x + 38, y + 35, 8, 8);
// g.fillOval(x + 26, y + 35, 8, 8);
}
case Six -> {
g.fillPolygon(new int[]{x + 46, x + 54, x+ 60 }, new int[]{y + 7, y, y + 7}, 3);
g.fillPolygon(new int[]{x + 66, x + 64, x+ 80 }, new int[]{y + 7, y, y + 7}, 3);
g.fillPolygon(new int[]{x + 46, x + 54, x+ 60 }, new int[]{y + 49, y + 42, y + 49}, 3);
g.fillPolygon(new int[]{x + 66, x + 64, x+ 80 }, new int[]{y + 49, y + 42, y + 49}, 3);
g.fillPolygon(new int[]{x + 58, x + 66, x+ 72}, new int[]{y + 7, y, y + 7}, 3);
g.fillPolygon(new int[]{x + 58, x + 66, x+72}, new int[]{y + 49, y + 42, y + 49}, 3);
// g.fillOval(x + 14, y + 10, 8, 8);
// g.fillOval(x + 26, y + 10, 8, 8);
// g.fillOval(x + 38, y + 10, 8, 8);
// g.fillOval(x + 38, y + 35, 8, 8);
// g.fillOval(x + 26, y + 35, 8, 8);
// g.fillOval(x + 14, y + 35, 8, 8);
}}
}
@Override
public void SetBlocks(int count) {
switch(count)
{
case 2:
blocksOnDeck = DirectionBlocksOnDeck.Two;
break;
case 4:
blocksOnDeck = DirectionBlocksOnDeck.Four;
break;
case 6:
blocksOnDeck = DirectionBlocksOnDeck.Six;
break;
default:
break;
}
}
public DrawningTriangleBlocks (int num) {
SetBlocks(num);
}
}

View File

@ -30,7 +30,7 @@ public class EntityBattleship {
/// <param name="weight"></param>
/// <param name="bodyColor"></param>
/// <returns></returns>
public void Init(int speed, float weight, Color bodyColor)
public EntityBattleship(int speed, float weight, Color bodyColor)
{
Random rnd = new Random();
this.speed = speed <= 0 ? rnd.nextInt(950) + 1050 : speed;

15
EntityLinkor.java Normal file
View File

@ -0,0 +1,15 @@
import java.awt.*;
public class EntityLinkor extends EntityBattleship {
public final boolean turret;
public final boolean missileBay;
public final Color dopColor;
public EntityLinkor(int speed, float weight, Color bodyColor, Color DopColor, boolean Turret, boolean MissileBay) {
super(speed, weight, bodyColor);
dopColor = DopColor;
missileBay = MissileBay;
turret = Turret;
}
}

View File

@ -9,7 +9,7 @@ public class FormBattleship extends JComponent {
private DrawningBattleship _battleship;
private EntityBattleship _entityBattleship;
public static void main(String[] args) {
FormBattleship formBattleship = new FormBattleship();
FormMap formMap = new FormMap();
}
public FormBattleship() {
JFrame form = new JFrame("Военный корабль");
@ -51,10 +51,9 @@ public class FormBattleship extends JComponent {
int[] countBlocks = {2, 4, 6};
Random rnd = new Random();
_battleship = new DrawningBattleship();
_battleship.Init(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000),
_battleship = new DrawningBattleship(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000),
Color.getHSBColor(rnd.nextInt(0, 256), rnd.nextInt(0, 256),
rnd.nextInt(0, 256)), _entityBattleship, countBlocks[rnd.nextInt(0, 3)]);
rnd.nextInt(0, 256)));
_battleship.SetPosition(10 + rnd.nextInt(90), 10 + rnd.nextInt(90), form.getWidth(), form.getHeight() - 75);
speedLabel.setText("Скорость: " + _battleship.Battleship.GetSpeed());
weightLabel.setText("Вес: " + (int)_battleship.Battleship.GetWeight());
@ -63,7 +62,26 @@ public class FormBattleship extends JComponent {
repaint();
});
JButton modifiedButton = new JButton("Модификация");
modifiedButton.addActionListener(e -> {
Random rnd = new Random();
Color colorFirst = JColorChooser.showDialog(null, "Color", new Color(rnd.nextInt(256), rnd.nextInt(256),rnd.nextInt(256)));
Color colorSecond = JColorChooser.showDialog(null, "Color", new Color(rnd.nextInt(256), rnd.nextInt(256),rnd.nextInt(256)));
_battleship = new DrawningLinkor(rnd.nextInt(200) + 100, rnd.nextInt(1000) + 1000,
colorFirst,
colorSecond,
rnd.nextBoolean(),
rnd.nextBoolean());
_battleship.SetPosition(10 + rnd.nextInt(90), 10 + rnd.nextInt(90), 800, 500 - 75);
// speedLabel.setText("Speed: " + _locomotive.Locomotive.getSpeed());
// weightLabel.setText("Weight: " + (int)_locomotive.Locomotive.getWeight());
// colorLabel.setText("Color: " + _locomotive.Locomotive.getBodyColor().getRed() + " " + _locomotive.Locomotive.getBodyColor().getGreen() + " " + _locomotive.Locomotive.getBodyColor().getBlue() );
repaint();
});
statusPanel.add(createButton);
statusPanel.add(modifiedButton);
statusPanel.add(speedLabel);
statusPanel.add(weightLabel);
statusPanel.add(colorLabel);

156
FormMap.java Normal file
View File

@ -0,0 +1,156 @@
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.image.BufferedImage;
import java.util.Random;
public class FormMap extends JComponent {
private AbstractMap _abstractMap;
private DrawningBattleship _battleship;
private BufferedImage bufferedImage;
public static void main(String[] args) {
FormMap formMap = new FormMap();
}
public FormMap() {
JFrame form = new JFrame("Карта");
form.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
form.setSize(1000, 500);
form.setVisible(true);
form.setLocationRelativeTo(null);
// form.addComponentListener(new ComponentListener() {
// @Override
// public void componentResized(ComponentEvent e) {
// if(_battleship != null) _battleship.ChangeBorders(getWidth(), getHeight());
// repaint();
// }
//
// @Override
// public void componentMoved(ComponentEvent e) {
// }
//
// @Override
// public void componentShown(ComponentEvent e) {
// }
//
// @Override
// public void componentHidden(ComponentEvent e) {
// }
// });
Panel statusPanel = new Panel();
statusPanel.setBackground(Color.WHITE);
statusPanel.setLayout(new FlowLayout());
setLayout(new BorderLayout());
form.add(statusPanel, BorderLayout.SOUTH);
Label speedLabel = new Label("Скорость: ");
Label weightLabel = new Label("Вес: ");
Label colorLabel = new Label("Цвет: ");
String[] maps = {
"Simple Map",
"Sea Map"
};
JComboBox mapSelectComboBox = new JComboBox(maps);
mapSelectComboBox.setEditable(true);
mapSelectComboBox.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String item = (String)mapSelectComboBox.getSelectedItem();
if (item == null) return;
switch (item) {
case ("Simple Map"):
_abstractMap = new SimpleMap();
break;
case ("Sea Map"):
_abstractMap = new SeaMap();
break;
}
}
});
JButton createButton = new JButton("Создать");
createButton.addActionListener(e -> {
Random rnd = new Random();
var battleship = new DrawningBattleship(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000),
Color.getHSBColor(rnd.nextInt(0, 256), rnd.nextInt(0, 256),
rnd.nextInt(0, 256)));
speedLabel.setText("Скорость: " + battleship.Battleship.GetSpeed());
weightLabel.setText("Вес: " + (int)battleship.Battleship.GetWeight());
colorLabel.setText("Цвет: " + battleship.Battleship.GetBodyColor().getRed() + " " +
battleship.Battleship.GetBodyColor().getGreen() + " " + battleship.Battleship.GetBodyColor().getBlue() );
if (_abstractMap != null) bufferedImage = _abstractMap.CreateMap(1000, 490, new DrawningObjectBattleship(battleship));
repaint();
});
JButton modifiedButton = new JButton("Модификация");
modifiedButton.addActionListener(e -> {
Random rnd = new Random();
Color colorFirst = new Color(rnd.nextInt(256), rnd.nextInt(256),rnd.nextInt(256));
Color colorSecond = new Color(rnd.nextInt(256), rnd.nextInt(256),rnd.nextInt(256));
var battleship = new DrawningLinkor(rnd.nextInt(200) + 100, rnd.nextInt(1000) + 1000,
colorFirst,
colorSecond,
rnd.nextBoolean(),
rnd.nextBoolean());
battleship.SetPosition(10 + rnd.nextInt(90), 10 + rnd.nextInt(90), 800, 500 - 75);
speedLabel.setText("Speed: " + battleship.Battleship.GetSpeed());
weightLabel.setText("Weight: " + battleship.Battleship.GetWeight());
colorLabel.setText("Color: " + battleship.Battleship.GetBodyColor().getRed() + " " + battleship.Battleship.GetBodyColor().getGreen() + " " + battleship.Battleship.GetBodyColor().getBlue() );
if (_abstractMap != null) bufferedImage = _abstractMap.CreateMap(1000, 490, new DrawningObjectBattleship(battleship));
repaint();
});
statusPanel.add(mapSelectComboBox);
statusPanel.add(createButton);
statusPanel.add(modifiedButton);
statusPanel.add(speedLabel);
statusPanel.add(weightLabel);
statusPanel.add(colorLabel);
JButton upButton = new JButton("");
JButton rightButton = new JButton("");
JButton leftButton = new JButton("");
JButton downButton = new JButton("");
upButton.addActionListener(e -> {
if(bufferedImage != null) bufferedImage = _abstractMap.MoveObject(Direction.Up);
repaint();
});
rightButton.addActionListener(e -> {
if(bufferedImage != null) bufferedImage = _abstractMap.MoveObject(Direction.Right);
repaint();
});
leftButton.addActionListener(e -> {
if(bufferedImage != null) bufferedImage = _abstractMap.MoveObject(Direction.Left);
repaint();
});
downButton.addActionListener(e -> {
if(bufferedImage != null) bufferedImage = _abstractMap.MoveObject(Direction.Down);
repaint();
});
statusPanel.add(leftButton);
statusPanel.add(upButton);
statusPanel.add(rightButton);
statusPanel.add(downButton);
form.getContentPane().add(this);
super.repaint();
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
if (bufferedImage != null) g2.drawImage(bufferedImage, 0,0,1000,490,null);
super.repaint();
}
}

7
IDrawningBlocks.java Normal file
View File

@ -0,0 +1,7 @@
import java.awt.*;
public interface IDrawningBlocks {
void DrawBlocks(Graphics g, int startX, int startY, Color bodyColor);
void SetBlocks(int count);
}

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();
}

46
SeaMap.java Normal file
View File

@ -0,0 +1,46 @@
import java.awt.*;
public class SeaMap extends AbstractMap {
@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(1, 99);
int y = _random.nextInt(1, 100);
if (_map[x][y] == _freeRoad)
{
_map[x][y] = _barrier;
_map[x + 1][y] = _barrier;
counter++;
}
}
}
@Override
protected void DrawRoadPart(Graphics g, int i, int j) {
g.setColor(Color.BLUE);
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(Color.WHITE);
g.fillPolygon(new int[]{i * (int) _size_x, i * (int) _size_x + 18, i * (int) _size_x - 18},new int[]{ j * (int) _size_y, j * (int) _size_y + 20,
j * (int) _size_y + 20}, 3);
}
}

43
SimpleMap.java Normal file
View File

@ -0,0 +1,43 @@
import java.awt.*;
public class SimpleMap extends AbstractMap {
Color roadColor = Color.GRAY;
Color barrierColor = Color.BLACK;
@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(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)));
}
}