Compare commits

...

2 Commits

Author SHA1 Message Date
5bfa75169f Добавила стрелки 2024-04-29 12:13:24 +04:00
141801a3a7 Лабораторная работа №1 2024-04-29 12:08:52 +04:00
12 changed files with 464 additions and 1 deletions

BIN
src/res/arrowDown.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 699 B

BIN
src/res/arrowLeft.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 713 B

BIN
src/res/arrowRight.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 711 B

BIN
src/res/arrowUp.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 687 B

View File

@ -0,0 +1,16 @@
import javax.swing.*;
import java.awt.*;
public class CanvasBattleship extends JComponent {
public DrawingBattleship _drawingBattleship;
public CanvasBattleship(){}
public void paintComponent(Graphics g) {
if (_drawingBattleship == null) {
return;
}
super.paintComponents(g);
Graphics2D g2d = (Graphics2D) g;
_drawingBattleship.DrawTransport(g2d);
super.repaint();
}
}

View File

@ -0,0 +1,12 @@
public enum DirectionType
{
Up,
Down,
Left,
Right,
}

View File

@ -0,0 +1,177 @@
import java.awt.*;
public class DrawingBattleship
{
public EntityBattleship EntityBattleship;
private DrawingBlocks drawingBlocks = null;
private Integer _pictureWidth;
private Integer _pictureHeight;
private Integer _startPosX;
private Integer _startPosY;
private int _drawingBattleshipWidth = 120;
private int _drawingBattleshipHeight = 80;
public void Init(int speed, double weight, Color bodyColor, Color additionalColor, boolean bodyDeck, boolean compartment, boolean tower, int numBlocks)
{
EntityBattleship = new EntityBattleship();
EntityBattleship.Init(speed, weight, bodyColor, additionalColor, bodyDeck, compartment, tower);
_pictureWidth = null;
_pictureHeight = null;
_startPosX = null;
_startPosY = null;
drawingBlocks = new DrawingBlocks();
drawingBlocks.setNumBlocks(numBlocks);
}
public boolean SetPictureSize(int width, int height)
{
if (width < _drawingBattleshipWidth || height < _drawingBattleshipHeight)
{
return false;
}
_pictureWidth = width;
_pictureHeight = height;
if (_startPosX != null || _startPosY != null)
{
if (_startPosX + _drawingBattleshipWidth > _pictureWidth)
{
_startPosX = _pictureWidth - _drawingBattleshipWidth;
}
else if (_startPosX < 0) _startPosX = 0;
if (_startPosY + _drawingBattleshipHeight > _pictureHeight)
{
_startPosY = _pictureHeight - _drawingBattleshipHeight;
}
else if (_startPosY < 0) _startPosY = 0;
}
return true;
}
public void SetPosition(int x, int y) {
if (!(_pictureWidth != null && _pictureHeight != null)) return;
if (x + _drawingBattleshipWidth > _pictureWidth)
{
_startPosX = x - (x + _drawingBattleshipWidth - _pictureWidth);
}
else if (x < 0) _startPosX = 0;
else _startPosX = x;
if (y + _drawingBattleshipHeight > _pictureHeight)
{
_startPosY = y - (y + _drawingBattleshipHeight - _pictureHeight);
}
else if (y < 0) _startPosY = 0;
else _startPosY = y;
}
public boolean MoveTransport(DirectionType direction) {
if (EntityBattleship == null || _startPosX == null || _startPosY == null) return false;
switch (direction) {
case Left:
if (_startPosX - EntityBattleship.Step > 0) {
_startPosX -= (int)EntityBattleship.Step;
}
return true;
case Up:
if (_startPosY - EntityBattleship.Step > 0)
{
_startPosY -= (int)EntityBattleship.Step;
}
return true;
case Right:
if (_startPosX + _drawingBattleshipWidth + (int)EntityBattleship.Step < _pictureWidth - EntityBattleship.Step)
{
_startPosX += (int)EntityBattleship.Step;
}
return true;
case Down:
if (_startPosY + _drawingBattleshipHeight + (int)EntityBattleship.Step < _pictureHeight - EntityBattleship.Step)
{
_startPosY += (int)EntityBattleship.Step;
}
return true;
default:
return false;
}
}
public void DrawTransport(Graphics2D g)
{
if (EntityBattleship == null || _startPosX == null || _startPosY == null)
{
return;
}
int y = _startPosY;
Color bodyColor = EntityBattleship.getBodyColor();
Color additionalColor = EntityBattleship.getAdditionalColor();
//границы линкора
g.drawRect(_startPosX + 10, _startPosY, 80, 40); //ширина высота (сама палуба)
//заливка линкора
g.setColor(EntityBattleship.getBodyColor());
g.fillRect(_startPosX + 10, _startPosY, 80, 40);
//границы двигателей
g.drawRect(_startPosX + 5, _startPosY + 7, 5, 10); //двигатель верхний
g.drawRect(_startPosX + 5, _startPosY + 22, 5, 10); //двигатель нижний
//заливка двигателей (2 фигни сзади)
g.setColor(Color.BLACK);
g.fillRect(_startPosX + 5, _startPosY + 7, 5, 10); //верхний
g.fillRect(_startPosX + 5, _startPosY + 22, 5, 10); //нижний
// блоки
drawingBlocks.drawBlocks(g, Color.BLACK, _startPosX, _startPosY);
//носик палубы
int[] xPoints = {_startPosX + 90,_startPosX + 120, _startPosX + 90};
int[] yPoints = {_startPosY,_startPosY + 20,_startPosY + 40};
Polygon Points = new Polygon(xPoints, yPoints, 3);
g.drawPolygon(Points);
g.setColor(EntityBattleship.BodyColor);
g.fillPolygon(Points);
//круг на палубе
g.drawOval(_startPosX + 78, _startPosY + 12, 15, 15);
//заливка круга
g.setColor(Color.BLUE);
g.fillOval(_startPosX + 78, _startPosY + 12, 15, 15);
//ПРОВЕРКА
if (EntityBattleship.Compartment)
{
//отсеки для ракет
//заливка отсеков
g.setColor(EntityBattleship.getAdditionalColor());
g.fillRect(_startPosX + 15, _startPosY + 5, 27, 5); //верхний отсек для ракет
g.fillRect(_startPosX + 15, _startPosY + 28, 27, 5); //нижний отсек для ракет
//границы отсеков
g.drawRect(_startPosX + 15, _startPosY + 5, 27, 5); //верхний отсек
g.drawRect(_startPosX + 15, _startPosY + 28, 27, 5); //нижний отсек
}
if (EntityBattleship.Tower)
{
//границы башни
g.drawRect(_startPosX + 107, _startPosY + 17, 30, 5);
g.fillOval(_startPosX + 98, _startPosY + 15, 10, 10);
//заливка башни
g.setColor(EntityBattleship.getAdditionalColor());
g.fillOval(_startPosX + 98, _startPosY + 15, 10, 10);
g.fillRect(_startPosX + 107, _startPosY + 17, 30, 5);
}
}
}

View File

@ -0,0 +1,96 @@
import java.awt.*;
public class DrawingBlocks {
private NumberBlock numBlocks;
//определяет количество блоков
public void setNumBlocks(int number) {
switch (number) {
case 1:
numBlocks = NumberBlock.Two;
break;
case 2:
numBlocks = NumberBlock.Four;
break;
case 3:
numBlocks = NumberBlock.Six;
break;
default:
numBlocks = NumberBlock.Four;
}
}
public void drawBlocks(Graphics2D g2D, Color color, int _startPosX, int _startPosY) {
switch (numBlocks) {
case Two:
drawTwoBlocks(g2D, color, _startPosX, _startPosY);
break;
case Four:
drawFourBlocks(g2D, color, _startPosX, _startPosY);
break;
case Six:
drawSixBlocks(g2D, color, _startPosX, _startPosY);
break;
}
}
private void drawTwoBlocks(Graphics2D g2D, Color color, int _startPosX, int _startPosY) {
//прямоугольник левый на палубе (горизонтальный)
g2D.setColor(Color.LIGHT_GRAY);
g2D.drawRect(_startPosX + 32, _startPosY + 13, 21, 12);
g2D.fillRect(_startPosX + 32, _startPosY + 13, 21, 12);
//прямоугольник правый на палубе (вертикальный)
g2D.setColor(Color.GRAY);
g2D.drawRect(_startPosX + 53, _startPosY + 7, 16, 24);
g2D.fillRect(_startPosX + 53, _startPosY + 7, 16, 24);
}
private void drawFourBlocks(Graphics2D g2D, Color color, int _startPosX, int _startPosY) {
//прямоугольник левый на палубе (горизонтальный)
g2D.setColor(Color.magenta);
g2D.drawRect(_startPosX + 32, _startPosY + 13, 8, 12);
g2D.fillRect(_startPosX + 32, _startPosY + 13, 8, 12);
//прямоугольник правый на палубе (вертикальный)
g2D.setColor(Color.GREEN);
g2D.drawRect(_startPosX + 53, _startPosY + 8, 16, 8);
g2D.fillRect(_startPosX + 53, _startPosY + 8, 16, 8);
g2D.setColor(Color.GREEN);
g2D.drawRect(_startPosX + 53, _startPosY + 23, 16, 8);
g2D.fillRect(_startPosX + 53, _startPosY + 23, 16, 8);
g2D.setColor(Color.DARK_GRAY);
g2D.drawRect(_startPosX + 19, _startPosY + 13, 8, 12);
g2D.fillRect(_startPosX + 19, _startPosY + 13, 8, 12);
}
private void drawSixBlocks(Graphics2D g2D, Color color, int _startPosX, int _startPosY) {
g2D.setColor(Color.GRAY);
g2D.drawRect(_startPosX + 19, _startPosY + 13, 10, 13);
g2D.fillRect(_startPosX + 19, _startPosY + 13, 10, 13);
g2D.setColor(Color.LIGHT_GRAY);
g2D.drawRect(_startPosX + 31, _startPosY + 15, 12, 9);
g2D.fillRect(_startPosX + 31, _startPosY + 15, 12, 9);
g2D.setColor(Color.PINK);
g2D.drawRect(_startPosX + 46, _startPosY + 13, 10, 13);
g2D.fillRect(_startPosX + 46, _startPosY + 13, 10, 13);
g2D.setColor(Color.GRAY);
g2D.drawRect(_startPosX + 64, _startPosY + 4, 7, 8);
g2D.fillRect(_startPosX + 64, _startPosY + 4, 7, 8);
g2D.setColor(Color.GRAY);
g2D.drawRect(_startPosX + 64, _startPosY + 15, 7, 8);
g2D.fillRect(_startPosX + 64, _startPosY + 15, 7, 8);
g2D.setColor(Color.GRAY);
g2D.drawRect(_startPosX + 64, _startPosY + 28, 7, 8);
g2D.fillRect(_startPosX + 64, _startPosY + 28, 7, 8);
}
}

View File

@ -0,0 +1,28 @@
import java.awt.*;
import java.util.Random;
public class EntityBattleship
{
public int Speed;
public double Weight;
public Color BodyColor;
public Color AdditionalColor;
public boolean BodyDeck;
public boolean Compartment;
public boolean Tower;
public double Step;
public Color getBodyColor() { return BodyColor; }
public Color getAdditionalColor() { return AdditionalColor; }
public double getStep() { return Speed * 100 / Weight; }
public void Init(int speed, double weight, Color bodyColor, Color additionalColor, boolean bodyDeck, boolean compartment, boolean tower) {
Random rnd = new Random();
Speed = speed <= 0 ? rnd.nextInt(50, 150) : speed;
Weight = weight <= 0 ? rnd.nextInt(40,70) : weight;
BodyColor = bodyColor;
AdditionalColor = additionalColor;
BodyDeck = bodyDeck;
Compartment = compartment;
Tower = tower;
Step = Speed * 100 / Weight;
}
}

125
src/src/FormBattleship.java Normal file
View File

@ -0,0 +1,125 @@
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.util.Random;
public class FormBattleship extends JFrame {
private String title;
private Dimension dimension;
private int Width, Height;
private CanvasBattleship canvasBattleship = new CanvasBattleship();
private JButton CreateButton = new JButton("Создать");;
private JButton UpButton = new JButton();
private JButton DownButton = new JButton();;
private JButton LeftButton = new JButton();;
private JButton RightButton = new JButton();
public FormBattleship(String title, Dimension dimension) {
this.title = title;
this.dimension = dimension;
}
public void Init() {
setTitle(title);
setMinimumSize(dimension);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Width = getWidth() - 15;
Height = getHeight() - 35;
CreateButton.setName("CREATE");
Icon iconUp = new ImageIcon("src/res/arrowUp.png");
UpButton.setIcon(iconUp);
UpButton.setName("Up");
DownButton.setName("Down");
Icon iconDown = new ImageIcon("src/res/arrowDown.png");
DownButton.setIcon(iconDown);
LeftButton.setName("Left");
Icon iconLeft = new ImageIcon("src/res/arrowLeft.png");
LeftButton.setIcon(iconLeft);
RightButton.setName("Right");
Icon iconRight = new ImageIcon("src/res/arrowRight.png");
RightButton.setIcon(iconRight);
CreateButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int StartPositionX = (int)(Math.random() * 90 + 10);
int StartPositionY = (int)(Math.random() * 90 + 10);
int speed = (int)(Math.random() * 300 + 100);
double weight = (double)(Math.random() * 3000 + 1000);
Color bodyColor = new Color((int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0));
Color additionalColor = new Color((int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0));;
boolean bodyDeck = new Random().nextBoolean();
boolean compartment = new Random().nextBoolean();;
boolean tower = new Random().nextBoolean();
int blocksNum = (int)(Math.random() * 6) + 1;
canvasBattleship._drawingBattleship = new DrawingBattleship();
canvasBattleship._drawingBattleship.Init(speed, weight, bodyColor, additionalColor, bodyDeck, compartment, tower, blocksNum);
canvasBattleship._drawingBattleship.SetPictureSize(Width, Height);
canvasBattleship._drawingBattleship.SetPosition(StartPositionX, StartPositionY);
canvasBattleship.repaint();
}
});
ActionListener actionListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
if (canvasBattleship._drawingBattleship == null) return;
boolean result = false;
switch ((((JButton)(event.getSource())).getName())) {
case "Up":
result = canvasBattleship._drawingBattleship.MoveTransport(DirectionType.Up);
break;
case "Down":
result = canvasBattleship._drawingBattleship.MoveTransport(DirectionType.Down);
break;
case "Left":
result = canvasBattleship._drawingBattleship.MoveTransport(DirectionType.Left);
break;
case "Right":
result = canvasBattleship._drawingBattleship.MoveTransport(DirectionType.Right);
break;
}
if (result) {
canvasBattleship.repaint();
}
}
};
UpButton.addActionListener(actionListener);
DownButton.addActionListener(actionListener);
LeftButton.addActionListener(actionListener);
RightButton.addActionListener(actionListener);
setSize(dimension.width,dimension.height);
setLayout(null);
canvasBattleship.setBounds(0,0, getWidth(), getHeight());
CreateButton.setBounds(10, getHeight() - 90, 100, 40);
UpButton.setBounds(getWidth() - 140, getHeight() - 160, 50, 50);
DownButton.setBounds(getWidth() - 140, getHeight() - 100, 50, 50);
RightButton.setBounds(getWidth() - 80, getHeight() - 100, 50, 50);
LeftButton.setBounds(getWidth() - 200, getHeight() - 100, 50, 50);
add(CreateButton);
add(UpButton);
add(DownButton);
add(RightButton);
add(LeftButton);
add(canvasBattleship);
setVisible(true);
//обработка события изменения размеров окна
addComponentListener(new ComponentAdapter() {
public void componentResized(ComponentEvent e) {
Width = getWidth() - 15;
Height = getHeight() - 35;
if (canvasBattleship._drawingBattleship != null)
canvasBattleship._drawingBattleship.SetPictureSize(Width, Height);
canvasBattleship.setBounds(0,0, getWidth(), getHeight());
CreateButton.setBounds(10, getHeight() - 90, 100, 40);
UpButton.setBounds(getWidth() - 140, getHeight() - 160, 50, 50);
DownButton.setBounds(getWidth() - 140, getHeight() - 100, 50, 50);
RightButton.setBounds(getWidth() - 80, getHeight() - 100, 50, 50);
LeftButton.setBounds(getWidth() - 200, getHeight() - 100, 50, 50);
}
});
}
}

View File

@ -1,5 +1,9 @@
import java.awt.*;
public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
FormBattleship form = new FormBattleship("Линкор", new Dimension(700,500));
form.Init();
}
}

5
src/src/NumberBlock.java Normal file
View File

@ -0,0 +1,5 @@
public enum NumberBlock {
Two,
Four,
Six
}