Сделанная лабораторная
This commit is contained in:
parent
de8c7eb3ae
commit
33ef22cccc
5
DirectionBlocksOnDeck.java
Normal file
5
DirectionBlocksOnDeck.java
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
public enum DirectionBlocksOnDeck {
|
||||||
|
Two,
|
||||||
|
Four,
|
||||||
|
Six
|
||||||
|
}
|
@ -2,6 +2,7 @@ import java.awt.*;
|
|||||||
|
|
||||||
public class DrawningBattleship {
|
public class DrawningBattleship {
|
||||||
EntityBattleship Battleship;
|
EntityBattleship Battleship;
|
||||||
|
private DrawningBlocks drawingBlocks;
|
||||||
public EntityBattleship Battleship()
|
public EntityBattleship Battleship()
|
||||||
{return Battleship; }
|
{return Battleship; }
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -15,11 +16,11 @@ public class DrawningBattleship {
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Ширина окна отрисовки
|
/// Ширина окна отрисовки
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private Integer _pictureWidth = 100;
|
private Integer _pictureWidth = null;
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Высота окна отрисовки
|
/// Высота окна отрисовки
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private Integer _pictureHeight = 100;
|
private Integer _pictureHeight = null;
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Ширина отрисовки корабля
|
/// Ширина отрисовки корабля
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -34,10 +35,12 @@ public class DrawningBattleship {
|
|||||||
/// <param name="speed">Скорость</param>
|
/// <param name="speed">Скорость</param>
|
||||||
/// <param name="weight">Вес корабля</param>
|
/// <param name="weight">Вес корабля</param>
|
||||||
/// <param name="bodyColor">Цвет корпуса</param>
|
/// <param name="bodyColor">Цвет корпуса</param>
|
||||||
public void Init(int speed, float weight, Color bodyColor, EntityBattleship entityBattleship)
|
public void Init(int speed, float weight, Color bodyColor, EntityBattleship entityBattleship, int blocks)
|
||||||
{
|
{
|
||||||
Battleship = new EntityBattleship();
|
Battleship = new EntityBattleship();
|
||||||
Battleship.Init(speed, weight, bodyColor);
|
Battleship.Init(speed, weight, bodyColor);
|
||||||
|
drawingBlocks = new DrawningBlocks();
|
||||||
|
drawingBlocks.Init(blocks, Color.black);
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Установка позиции корабля
|
/// Установка позиции корабля
|
||||||
@ -83,6 +86,7 @@ public class DrawningBattleship {
|
|||||||
{
|
{
|
||||||
_startPosX -= Battleship.GetStep();
|
_startPosX -= Battleship.GetStep();
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
//вверх
|
//вверх
|
||||||
@ -91,6 +95,7 @@ public class DrawningBattleship {
|
|||||||
{
|
{
|
||||||
_startPosY -= Battleship.GetStep();
|
_startPosY -= Battleship.GetStep();
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
//вниз
|
//вниз
|
||||||
case Down:
|
case Down:
|
||||||
@ -105,7 +110,7 @@ public class DrawningBattleship {
|
|||||||
/// Отрисовка корабля
|
/// Отрисовка корабля
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="g"></param>
|
/// <param name="g"></param>
|
||||||
public void DrawTransport(Graphics g)
|
public void DrawTransport(Graphics2D g)
|
||||||
{
|
{
|
||||||
if (_startPosX < 0 || _startPosY < 0
|
if (_startPosX < 0 || _startPosY < 0
|
||||||
|| _pictureHeight == null || _pictureWidth == null)
|
|| _pictureHeight == null || _pictureWidth == null)
|
||||||
@ -118,9 +123,9 @@ public class DrawningBattleship {
|
|||||||
//Brush br = new SolidBrush(Battleship?.BodyColor ?? Color.Black);
|
//Brush br = new SolidBrush(Battleship?.BodyColor ?? Color.Black);
|
||||||
// Polygon[] shipHullArrayPoints = {((int)_startPosX, (int)_startPosY)), (int)_startPosX, (int)_startPosY + 50), new Point((int)_startPosX + 80, (int)_startPosY + 50),
|
// Polygon[] shipHullArrayPoints = {((int)_startPosX, (int)_startPosY)), (int)_startPosX, (int)_startPosY + 50), new Point((int)_startPosX + 80, (int)_startPosY + 50),
|
||||||
//(int)_startPosX + 120, (int)_startPosY + 25 ), new Point((int)_startPosX + 80, (int)_startPosY), new Point((int)_startPosX, (int)_startPosY)};
|
//(int)_startPosX + 120, (int)_startPosY + 25 ), new Point((int)_startPosX + 80, (int)_startPosY), new Point((int)_startPosX, (int)_startPosY)};
|
||||||
g.setColor(Battleship.BodyColor);
|
g.setColor(Battleship.bodyColor);
|
||||||
g.fillPolygon(new int[]{(int) _startPosX, (int) _startPosX , (int) _startPosX+80, (int) _startPosX + 120, (int)_startPosX + 80, (int)_startPosX},
|
g.fillPolygon(new int[]{(int) _startPosX, (int) _startPosX , (int) _startPosX+80, (int) _startPosX + 120, (int)_startPosX + 80, (int)_startPosX},
|
||||||
new int[]{(int) _startPosY, (int) _startPosY+50 , (int) _startPosY+50, (int) _startPosX + 25, (int)_startPosY, (int)_startPosY}, 6);
|
new int[]{(int) _startPosY, (int) _startPosY+50 , (int) _startPosY+50, (int) _startPosY + 25, (int)_startPosY, (int)_startPosY}, 6);
|
||||||
|
|
||||||
//Пушка
|
//Пушка
|
||||||
|
|
||||||
@ -137,6 +142,7 @@ public class DrawningBattleship {
|
|||||||
g.setColor(Color.BLACK);
|
g.setColor(Color.BLACK);
|
||||||
g.fillRect((int)_startPosX-5, (int)_startPosY+10, 5, 5);
|
g.fillRect((int)_startPosX-5, (int)_startPosY+10, 5, 5);
|
||||||
g.fillRect((int)_startPosX - 5, (int)_startPosY + 35, 5, 5);
|
g.fillRect((int)_startPosX - 5, (int)_startPosY + 35, 5, 5);
|
||||||
|
drawingBlocks.Draw(g, (int) _startPosX, (int) _startPosY);
|
||||||
|
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
43
DrawningBlocks.java
Normal file
43
DrawningBlocks.java
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
import java.awt.*;
|
||||||
|
|
||||||
|
public class DrawningBlocks {
|
||||||
|
private DirectionBlocksOnDeck blocksCount;
|
||||||
|
private Color color;
|
||||||
|
public void SetNewBlocks(int countBlocks){
|
||||||
|
if (countBlocks == 4) {
|
||||||
|
blocksCount = DirectionBlocksOnDeck.Four;
|
||||||
|
} else if (countBlocks == 6) {
|
||||||
|
blocksCount = DirectionBlocksOnDeck.Six;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
blocksCount = DirectionBlocksOnDeck.Two;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
public void Init(int blocksCount, Color color)
|
||||||
|
{
|
||||||
|
SetNewBlocks(blocksCount);
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
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);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -2,9 +2,9 @@ import java.awt.*;
|
|||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
|
||||||
public class EntityBattleship {
|
public class EntityBattleship {
|
||||||
private int speed;
|
private int speed = 0;
|
||||||
private float weight;
|
private float weight = 0;
|
||||||
Color BodyColor;
|
Color bodyColor;
|
||||||
public int GetSpeed() {
|
public int GetSpeed() {
|
||||||
return speed;
|
return speed;
|
||||||
}
|
}
|
||||||
@ -15,7 +15,7 @@ public class EntityBattleship {
|
|||||||
/// Цвет корпуса
|
/// Цвет корпуса
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public Color GetBodyColor() {
|
public Color GetBodyColor() {
|
||||||
return BodyColor;
|
return bodyColor;
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Шаг перемещения корабля
|
/// Шаг перемещения корабля
|
||||||
@ -33,9 +33,8 @@ public class EntityBattleship {
|
|||||||
public void Init(int speed, float weight, Color bodyColor)
|
public void Init(int speed, float weight, Color bodyColor)
|
||||||
{
|
{
|
||||||
Random rnd = new Random();
|
Random rnd = new Random();
|
||||||
if(speed <= 0) speed = rnd.nextInt(50, 150);
|
this.speed = speed <= 0 ? rnd.nextInt(950) + 1050 : speed;
|
||||||
if(weight <= 0) weight = rnd.nextInt(40, 70);
|
this.weight = weight <= 0 ? rnd.nextInt(40) + 70 : weight;
|
||||||
|
this.bodyColor = bodyColor;
|
||||||
BodyColor = bodyColor;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
|
import java.awt.event.ActionEvent;
|
||||||
|
import java.awt.event.ActionListener;
|
||||||
import java.awt.event.ComponentEvent;
|
import java.awt.event.ComponentEvent;
|
||||||
import java.awt.event.ComponentListener;
|
import java.awt.event.ComponentListener;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
@ -11,5 +13,107 @@ public class FormBattleship extends JComponent {
|
|||||||
FormBattleship formBattleship = new FormBattleship();
|
FormBattleship formBattleship = new FormBattleship();
|
||||||
}
|
}
|
||||||
public FormBattleship() {
|
public FormBattleship() {
|
||||||
|
JFrame form = new JFrame("Военный корабль");
|
||||||
|
form.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
|
form.setSize(800, 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("Цвет: ");
|
||||||
|
|
||||||
|
|
||||||
|
JButton createButton = new JButton("Создать");
|
||||||
|
createButton.addActionListener(e -> {
|
||||||
|
int[] countBlocks = {2, 4, 6};
|
||||||
|
Random rnd = new Random();
|
||||||
|
|
||||||
|
_battleship = new DrawningBattleship();
|
||||||
|
_battleship.Init(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)]);
|
||||||
|
_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());
|
||||||
|
colorLabel.setText("Цвет: " + _battleship.Battleship.GetBodyColor().getRed() + " " +
|
||||||
|
_battleship.Battleship.GetBodyColor().getGreen() + " " + _battleship.Battleship.GetBodyColor().getBlue() );
|
||||||
|
|
||||||
|
repaint();
|
||||||
|
|
||||||
|
|
||||||
|
});
|
||||||
|
statusPanel.add(createButton);
|
||||||
|
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 (_battleship != null) _battleship.MoveTransport(Direction.Up);
|
||||||
|
repaint();
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
rightButton.addActionListener(e -> {
|
||||||
|
if (_battleship != null) _battleship.MoveTransport(Direction.Right);
|
||||||
|
repaint();
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
leftButton.addActionListener(e -> {
|
||||||
|
if (_battleship != null) _battleship.MoveTransport(Direction.Left);
|
||||||
|
repaint();
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
downButton.addActionListener(e -> {
|
||||||
|
if (_battleship != null) _battleship.MoveTransport(Direction.Down);
|
||||||
|
repaint();
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
statusPanel.add(leftButton);
|
||||||
|
statusPanel.add(upButton);
|
||||||
|
statusPanel.add(rightButton);
|
||||||
|
statusPanel.add(downButton);
|
||||||
|
|
||||||
|
form.getContentPane().add(this);
|
||||||
|
}
|
||||||
|
protected void paintComponent(Graphics g) {
|
||||||
|
super.paintComponent(g);
|
||||||
|
Graphics2D g2 = (Graphics2D)g;
|
||||||
|
if (_battleship != null) _battleship.DrawTransport(g2);
|
||||||
|
super.repaint();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user