diff --git a/.idea/shelf/Uncommitted_changes_before_Checkout_at_21_12_2023_16_01__Changes_.xml b/.idea/shelf/Uncommitted_changes_before_Checkout_at_21_12_2023_16_01__Changes_.xml new file mode 100644 index 0000000..a2e733d --- /dev/null +++ b/.idea/shelf/Uncommitted_changes_before_Checkout_at_21_12_2023_16_01__Changes_.xml @@ -0,0 +1,4 @@ + + \ No newline at end of file diff --git a/.idea/shelf/Uncommitted_changes_before_Checkout_at_21_12_2023_16_01__Changes_1.xml b/.idea/shelf/Uncommitted_changes_before_Checkout_at_21_12_2023_16_01__Changes_1.xml new file mode 100644 index 0000000..97dcf23 --- /dev/null +++ b/.idea/shelf/Uncommitted_changes_before_Checkout_at_21_12_2023_16_01__Changes_1.xml @@ -0,0 +1,4 @@ + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 275dc5a..0f4d59c 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -1,13 +1,29 @@ + + - + + + + + + + + + + @@ -16,12 +32,12 @@ - { + "keyToString": { + "RunOnceActivity.OpenProjectViewOnStart": "true", + "RunOnceActivity.ShowReadmeOnStart": "true" } -}]]> +} @@ -33,4 +49,8 @@ + + + \ No newline at end of file diff --git a/src/BlocksNumber.java b/src/BlocksNumber.java new file mode 100644 index 0000000..6709aba --- /dev/null +++ b/src/BlocksNumber.java @@ -0,0 +1,5 @@ +public enum BlocksNumber { + TWO, + FOUR, + SIX +} diff --git a/src/DirectionType.java b/src/DirectionType.java new file mode 100644 index 0000000..dd54b84 --- /dev/null +++ b/src/DirectionType.java @@ -0,0 +1,6 @@ +public enum DirectionType { + UP, + DOWN, + LEFT, + RIGHT +} diff --git a/src/DrawingBattleship.java b/src/DrawingBattleship.java new file mode 100644 index 0000000..cacb674 --- /dev/null +++ b/src/DrawingBattleship.java @@ -0,0 +1,130 @@ +import java.awt.*; + +public class DrawingBattleship { + public EntityBattleship entityBattleship; + private DrawingBlocks drawingBlocks; + private int pictureWidth; + private int pictureHeight; + private int startPosX; + private int startPosY; + private final int shipWidth = 175; + private final int shipHeight = 80; + + public boolean Init(int speed, double weight, Color bodyColor, Color + additionalColor, boolean turret, boolean rocketLauncher, int width, int height, int blocksNumber) { + if (width < shipWidth || height < shipHeight) + return false; + pictureWidth = width; + pictureHeight = height; + entityBattleship = new EntityBattleship(); + entityBattleship.Init(speed, weight, bodyColor, additionalColor, turret, rocketLauncher); + drawingBlocks = new DrawingBlocks(); + drawingBlocks.setNumber(blocksNumber); + return true; + } + public void SetPosition(int x, int y) { + if (x < 0 || y < 0 || x + shipWidth > pictureWidth || y + shipHeight > pictureHeight) { + x = 0; + y = 0; + } + startPosX = x; + startPosY = y; + } + public void moveTransport(DirectionType direction) { + if (entityBattleship == null) + return; + switch (direction) { + //влево + case LEFT: + if (startPosX - entityBattleship.Step > 0) { + startPosX -= entityBattleship.Step; + } + break; + case UP: + if (startPosY - entityBattleship.Step > 0) { + startPosY -= entityBattleship.Step; + } + break; + case RIGHT: + if (startPosX + shipWidth + entityBattleship.Step < pictureWidth) { + startPosX += entityBattleship.Step; + } + break; + case DOWN: + if (startPosY + shipHeight + entityBattleship.Step < pictureHeight) { + startPosY += entityBattleship.Step; + } + break; + default: + break; + } + } + + public void drawTransport(Graphics2D g2d){ + if(entityBattleship == null) + return; + BasicStroke pen = new BasicStroke(2); + g2d.setStroke(pen); + Color bodyColor = entityBattleship.BodyColor; + Color additionalColor = entityBattleship.AdditionalColor; + //основа + int[] xPoints = {startPosX + 5, startPosX + 120, startPosX + 160, startPosX + 120, startPosX + 5}; + int[] yPoints = {startPosY + 0, startPosY + 0, startPosY + 35, startPosY + 70, startPosY + 70}; + g2d.setColor(bodyColor); + g2d.fillPolygon(xPoints, yPoints, 5); + g2d.setPaint(Color.BLACK); + g2d.drawPolygon(xPoints, yPoints, 5); + + //блоки + + g2d.setPaint(Color.DARK_GRAY); + g2d.fillRect(startPosX + 70, startPosY + 15, 20, 40); + g2d.setPaint(Color.BLACK); + g2d.drawRect(startPosX + 70, startPosY + 15, 20, 40); + g2d.setPaint(Color.DARK_GRAY); + g2d.fillRect(startPosX + 40, startPosY + 25, 30, 20); + g2d.setPaint(Color.BLACK); + g2d.drawRect(startPosX + 40, startPosY + 25, 30, 20); + g2d.setPaint(Color.DARK_GRAY); + g2d.fillOval(startPosX + 100, startPosY + 20, 30, 30); + g2d.setPaint(Color.BLACK); + g2d.drawOval(startPosX + 100, startPosY + 20, 30, 30); + + //для ускорения + g2d.setPaint(Color.YELLOW); + g2d.fillRect(startPosX + 0, startPosY + 10, 5, 20); + g2d.fillRect(startPosX + 0, startPosY + 40, 5, 20); + g2d.setPaint(Color.BLACK); + g2d.drawRect(startPosX + 0, startPosY + 40, 5, 20); + g2d.drawRect(startPosX + 0, startPosY + 10, 5, 20); + + //блоки + if (drawingBlocks != null) { + drawingBlocks.drawBlocks(g2d, startPosX, startPosY); + } + //орудийная башня + if (entityBattleship.Tower) { + g2d.setColor(additionalColor); + g2d.fillRect(startPosX + 108, startPosY + 28, 15, 15); + g2d.drawRect(startPosX + 108, startPosY + 28, 15, 15); + // добавить черный цвет пушке + Color gunColor = Color.BLACK; + g2d.setColor(gunColor); + g2d.setStroke(pen); + g2d.fillRect(startPosX + 123, startPosY + 32, 47, 6); + g2d.drawRect(startPosX + 123, startPosY + 32, 55, 6); + } + //отсеки под ракеты + if (entityBattleship.Section) { + //добавить серый цвет + g2d.setColor(additionalColor); + g2d.fillRect(startPosX + 20, startPosY + 70, 40, 10); + g2d.fillRect(startPosX + 75, startPosY + 70, 40, 10); + g2d.setPaint(Color.BLACK); + g2d.drawRect(startPosX + 20, startPosY + 70, 40, 10); + g2d.drawRect(startPosX + 75, startPosY + 70, 40, 10); + } + + } + +} diff --git a/src/DrawingBlocks.java b/src/DrawingBlocks.java new file mode 100644 index 0000000..31ad076 --- /dev/null +++ b/src/DrawingBlocks.java @@ -0,0 +1,25 @@ +import java.awt.*; + +public class DrawingBlocks { + private BlocksNumber number; + public void setNumber(int x){ + if(x <= 2) + number = BlocksNumber.TWO; + if(x == 4) + number = BlocksNumber.FOUR; + if(x >= 6) + number = BlocksNumber.SIX; + } + public void drawBlocks(Graphics2D graphics2D, int _startX, int _startY){ + graphics2D.fillRect(_startX+45, _startY+15, 5, 5); + graphics2D.fillRect(_startX+45, _startY+50, 5, 5); + if (number == BlocksNumber.FOUR || number == BlocksNumber.SIX){ + graphics2D.fillRect(_startX+55, _startY+15, 5, 5); + graphics2D.fillRect(_startX+55, _startY+50, 5, 5); + if (number == BlocksNumber.SIX){ + graphics2D.fillRect(_startX+35, _startY+15, 5, 5); + graphics2D.fillRect(_startX+35, _startY+50, 5, 5); + } + } + } +} diff --git a/src/EntityBattleship.java b/src/EntityBattleship.java new file mode 100644 index 0000000..6008b8a --- /dev/null +++ b/src/EntityBattleship.java @@ -0,0 +1,21 @@ +import java.awt.*; + +public class EntityBattleship { + public int Speed; + public double Weight; + public Color BodyColor; + public Color AdditionalColor; + public boolean Tower; + public boolean Section; + public double Step = (double)Speed * 100 / Weight; + public void Init(int speed, double weight, Color bodyColor,Color + additionalColor, boolean tower, boolean section){ + Speed = speed; + Weight = weight; + Step = (double)Speed * 100 / Weight; + BodyColor = bodyColor; + AdditionalColor = additionalColor; + Tower = tower; + Section = section; + } +} \ No newline at end of file diff --git a/src/FrameBattleship.java b/src/FrameBattleship.java new file mode 100644 index 0000000..0e96127 --- /dev/null +++ b/src/FrameBattleship.java @@ -0,0 +1,118 @@ +import javax.imageio.ImageIO; +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.io.File; +import java.io.IOException; +import java.util.Random; + +public class FrameBattleship extends JFrame { + private DrawingBattleship drawingBattleship; + private JComponent pictureBox; + + public FrameBattleship() throws IOException { + super("Линкор"); + setSize(new Dimension(900, 500)); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + //components initialisation + pictureBox = new JComponent() { + public void paintComponent(Graphics graphics) { + super.paintComponent(graphics); + Graphics2D graphics2D = (Graphics2D) graphics; + if (drawingBattleship != null) drawingBattleship.drawTransport(graphics2D); + super.repaint(); + } + }; + pictureBox.setBounds(0, 0, getContentPane().getWidth(), getContentPane().getHeight()); + JButton createButton = new JButton("Создать"); + JButton rightButton = new JButton(new ImageIcon(ImageIO.read(new File("C:\\RPP intellij\\LabWork_01\\images\\right-arrow.png")))); + rightButton.setPreferredSize(new Dimension(30, 30)); + JButton leftButton = new JButton(new ImageIcon(ImageIO.read(new File("C:\\RPP intellij\\LabWork_01\\images\\left-arrow.png")))); + leftButton.setPreferredSize(new Dimension(30, 30)); + JButton upButton = new JButton(new ImageIcon(ImageIO.read(new File("C:\\RPP intellij\\LabWork_01\\images\\upper-arrow.png")))); + upButton.setPreferredSize(new Dimension(30, 30)); + JButton downButton = new JButton(new ImageIcon(ImageIO.read(new File("C:\\RPP intellij\\LabWork_01\\images\\down-arrow.png")))); + downButton.setPreferredSize(new Dimension(30, 30)); + //ActionListeners and ActionCommand addition + createButton.addActionListener(e -> buttonCreateClick()); + rightButton.setActionCommand("right"); + rightButton.addActionListener(this::buttonMoveClick); + leftButton.setActionCommand("left"); + leftButton.addActionListener(this::buttonMoveClick); + upButton.setActionCommand("up"); + upButton.addActionListener(this::buttonMoveClick); + downButton.setActionCommand("down"); + downButton.addActionListener(this::buttonMoveClick); + //panels and constraints initialisation + JPanel panelBattleship = new JPanel(new BorderLayout()); + JPanel createPanel = new JPanel(new BorderLayout()); + createPanel.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2)); + JPanel movementPanel = new JPanel(new GridBagLayout()); + JPanel rightPanel = new JPanel(new BorderLayout()); + GridBagConstraints constraints = new GridBagConstraints(); + constraints.insets.left = constraints.insets.top = constraints.insets.bottom = constraints.insets.right = 2; + //addition to createPanel + createPanel.add(createButton, BorderLayout.SOUTH); + //addition to movementPanel + constraints.gridx = 2; + constraints.gridy = 1; + movementPanel.add(rightButton, constraints); + constraints.gridx = 0; + constraints.gridy = 1; + movementPanel.add(leftButton, constraints); + constraints.gridx = 1; + constraints.gridy = 0; + movementPanel.add(upButton, constraints); + constraints.gridx = 1; + constraints.gridy = 1; + movementPanel.add(downButton, constraints); + //addition to frame + setLayout(new BorderLayout()); + add(pictureBox); + rightPanel.add(movementPanel, BorderLayout.SOUTH); + panelBattleship.add(rightPanel, BorderLayout.EAST); + panelBattleship.add(createPanel, BorderLayout.WEST); + add(panelBattleship, BorderLayout.CENTER); + setVisible(true); + } + + private void buttonCreateClick() { + Random random = new Random(); + drawingBattleship = new DrawingBattleship(); + pictureBox.setBounds(0, 0, getContentPane().getWidth(), getContentPane().getHeight()); + drawingBattleship.Init(random.nextInt(200) + 100, random.nextInt(2000) + 1000, new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)), + new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)), random.nextBoolean(), random.nextBoolean(), pictureBox.getWidth(), pictureBox.getHeight(), (random.nextInt(3) + 1) * 2); + drawingBattleship.SetPosition(random.nextInt(90) + 10, random.nextInt(90) + 10); + draw(); + } + + private void buttonMoveClick(ActionEvent event) { + if (drawingBattleship == null || drawingBattleship.entityBattleship == null) + return; + switch (event.getActionCommand()) { + case "left": + drawingBattleship.moveTransport(DirectionType.LEFT); + break; + case "right": + drawingBattleship.moveTransport(DirectionType.RIGHT); + break; + case "up": + drawingBattleship.moveTransport(DirectionType.UP); + break; + case "down": + drawingBattleship.moveTransport(DirectionType.DOWN); + break; + default: + break; + } + draw(); + } + + private void draw() { + if (drawingBattleship == null) { + return; + } + pictureBox.repaint(); + } + +} diff --git a/src/Main.java b/src/Main.java index 3e59c38..af80a70 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,5 +1,8 @@ + +import javax.swing.*; +import java.io.IOException; + public class Main { - public static void main(String[] args) { - System.out.println("Hello world!"); + public static void main(String[] args) throws IOException { new FrameBattleship(); } -} \ No newline at end of file +}