diff --git a/.idea/misc.xml b/.idea/misc.xml index 0548357..935dd37 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/images/down.png b/images/down.png new file mode 100644 index 0000000..30e8561 Binary files /dev/null and b/images/down.png differ diff --git a/images/left.png b/images/left.png new file mode 100644 index 0000000..d7cd963 Binary files /dev/null and b/images/left.png differ diff --git a/images/right.png b/images/right.png new file mode 100644 index 0000000..3387a09 Binary files /dev/null and b/images/right.png differ diff --git a/images/up.png b/images/up.png new file mode 100644 index 0000000..fb7362b Binary files /dev/null and b/images/up.png differ diff --git a/src/Main.java b/src/Main.java index 9c885bf..df6f25c 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,5 +1,7 @@ +import frames.FrameBattleship; + +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(); } } diff --git a/src/drawing_objects/BlocksNumber.java b/src/drawing_objects/BlocksNumber.java new file mode 100644 index 0000000..5d96d4f --- /dev/null +++ b/src/drawing_objects/BlocksNumber.java @@ -0,0 +1,7 @@ +package drawing_objects; + +public enum BlocksNumber { + TWO, + FOUR, + SIX +} \ No newline at end of file diff --git a/src/drawing_objects/DirectionType.java b/src/drawing_objects/DirectionType.java new file mode 100644 index 0000000..bdc0fd6 --- /dev/null +++ b/src/drawing_objects/DirectionType.java @@ -0,0 +1,8 @@ +package drawing_objects; + +public enum DirectionType { + UP, + DOWN, + LEFT, + RIGHT +} \ No newline at end of file diff --git a/src/drawing_objects/DrawingBattleship.java b/src/drawing_objects/DrawingBattleship.java new file mode 100644 index 0000000..94d0e5c --- /dev/null +++ b/src/drawing_objects/DrawingBattleship.java @@ -0,0 +1,121 @@ +package drawing_objects; + +import entities.EntityBattleship; + +import java.awt.*; + +public class DrawingBattleship { + private EntityBattleship entityBattleship; + public EntityBattleship getEntityBattleship() { + return entityBattleship; + } + private int pictureWidth; + private int pictureHeight; + private int startPosX; + private int startPosY; + private final int SHIP_WIDTH = 150; + public int getShipWidth() {return SHIP_WIDTH;} + private final int SHIP_HEIGHT = 50; + public int getShipHeight() {return SHIP_HEIGHT;} + private DrawingBlocks drawingBlocks; + public boolean init(int speed, double weight, Color bodyColor, Color + additionalColor, boolean turret, boolean rocketLauncher, int width, int height, int blocksNumber) { + if (width < SHIP_WIDTH || height < SHIP_HEIGHT) + 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 + SHIP_WIDTH > pictureWidth || y + SHIP_HEIGHT > 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.get().intValue() > 0) + startPosX -= entityBattleship.step.get().intValue(); + } + //вверх + case UP -> { + if (startPosY - entityBattleship.step.get().intValue() > 0) + startPosY -= entityBattleship.step.get().intValue(); + } + // вправо + case RIGHT -> { + if (startPosX + SHIP_WIDTH + entityBattleship.step.get().intValue() < pictureWidth) + startPosX += entityBattleship.step.get().intValue(); + } + //вниз + case DOWN -> { + if (startPosY + SHIP_HEIGHT + entityBattleship.step.get().intValue() < pictureHeight) + startPosY += entityBattleship.step.get().intValue(); + } + } + } + public void drawTransport(Graphics2D graphics2D) { + if (entityBattleship == null) + return; + BasicStroke pen = new BasicStroke(2); + graphics2D.setStroke(pen); + Color bodyColor = entityBattleship.getBodyColor(); + Color additionalColor = entityBattleship.getAdditionalColor(); + //корпус + int[] hullX = new int[] {startPosX + 5, startPosX + 100, startPosX + 150, startPosX + 100, startPosX + 5}; + int[] hullY = new int[] {startPosY, startPosY, startPosY + 25, startPosY + 50, startPosY + 50}; + graphics2D.setPaint(bodyColor); + graphics2D.fillPolygon(hullX, hullY, 5); + graphics2D.setPaint(Color.BLACK); + graphics2D.drawPolygon(hullX, hullY, 5); + graphics2D.fillRect(startPosX, startPosY + 6, 5, 13); + graphics2D.fillRect(startPosX, startPosY + 31, 5, 13); + //надстройки + graphics2D.setPaint(Color.DARK_GRAY); + graphics2D.fillRect(startPosX + 40, startPosY + 20, 30, 10); + graphics2D.fillRect(startPosX + 70, startPosY + 12, 18, 26); + graphics2D.fillOval(startPosX + 94, startPosY + 19, 12, 12); + graphics2D.setPaint(Color.BLACK); + graphics2D.drawRect(startPosX + 40, startPosY + 20, 30, 10); + graphics2D.drawRect(startPosX + 70, startPosY + 12, 18, 26); + graphics2D.drawOval(startPosX + 94, startPosY + 19, 12, 12); + //блоки + if (drawingBlocks != null){ + drawingBlocks.drawBlocks(graphics2D, startPosX, startPosY); + } + //орудийная башня + if (entityBattleship.getTurret()) { + int[] shieldX = new int[] {startPosX + 112, startPosX + 112, startPosX + 119, startPosX + 119, }; + int[] shieldY = new int[] {startPosY + 19, startPosY + 31, startPosY + 28, startPosY + 22}; + graphics2D.setPaint(additionalColor); + graphics2D.fillPolygon(shieldX, shieldY, 4); + graphics2D.fillRect(startPosX + 119, startPosY + 24, 12, 2); + graphics2D.setPaint(Color.BLACK); + graphics2D.drawPolygon(shieldX, shieldY, 4); + graphics2D.drawRect(startPosX + 119, startPosY + 24, 12, 2); + } + //ячейки для ракет + if (entityBattleship.getRocketLauncher()) { + graphics2D.setPaint(additionalColor); + graphics2D.fillRect(startPosX + 14, startPosY + 14, 10, 10); + graphics2D.fillRect(startPosX + 26, startPosY + 14, 10, 10); + graphics2D.fillRect(startPosX + 14, startPosY + 26, 10, 10); + graphics2D.fillRect(startPosX + 26, startPosY + 26, 10, 10); + graphics2D.setPaint(Color.BLACK); + graphics2D.drawRect(startPosX + 14, startPosY + 14, 10, 10); + graphics2D.drawRect(startPosX + 26, startPosY + 14, 10, 10); + graphics2D.drawRect(startPosX + 14, startPosY + 26, 10, 10); + graphics2D.drawRect(startPosX + 26, startPosY + 26, 10, 10); + } + } +} \ No newline at end of file diff --git a/src/drawing_objects/DrawingBlocks.java b/src/drawing_objects/DrawingBlocks.java new file mode 100644 index 0000000..e724e93 --- /dev/null +++ b/src/drawing_objects/DrawingBlocks.java @@ -0,0 +1,27 @@ +package drawing_objects; + +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+52, _startY+12, 6, 6); + graphics2D.fillRect(_startX+52, _startY+32, 6, 6); + if (number == BlocksNumber.FOUR || number == BlocksNumber.SIX){ + graphics2D.fillRect(_startX+62, _startY+12, 6, 6); + graphics2D.fillRect(_startX+62, _startY+32, 6, 6); + if (number == BlocksNumber.SIX){ + graphics2D.fillRect(_startX+42, _startY+12, 6, 6); + graphics2D.fillRect(_startX+42, _startY+32, 6, 6); + } + } + } +} \ No newline at end of file diff --git a/src/entities/EntityBattleship.java b/src/entities/EntityBattleship.java new file mode 100644 index 0000000..af40718 --- /dev/null +++ b/src/entities/EntityBattleship.java @@ -0,0 +1,41 @@ +package entities; + +import java.awt.*; +import java.util.function.Supplier; + +public class EntityBattleship { + private int speed; + public int getSpeed(){ + return speed; + } + private double weight; + public double getWeight(){ + return weight; + } + private Color bodyColor; + public Color getBodyColor(){ + return bodyColor; + } + private Color additionalColor; + public Color getAdditionalColor(){ + return additionalColor; + } + private boolean turret; + public boolean getTurret() { + return turret; + } + private boolean rocketLauncher; + public boolean getRocketLauncher() { + return rocketLauncher; + } + public Supplier step = () -> (double) speed * 100 / weight; + public void init(int speed, double weight, Color bodyColor, Color + additionalColor, boolean turret, boolean rocketLauncher) { + this.speed = speed; + this.weight = weight; + this.bodyColor = bodyColor; + this.additionalColor = additionalColor; + this.turret = turret; + this.rocketLauncher = rocketLauncher; + } +} \ No newline at end of file diff --git a/src/frames/FrameBattleship.java b/src/frames/FrameBattleship.java new file mode 100644 index 0000000..be7f46e --- /dev/null +++ b/src/frames/FrameBattleship.java @@ -0,0 +1,109 @@ +package frames; + +import drawing_objects.DrawingBattleship; +import drawing_objects.DirectionType; + +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("images/right.png")))); + rightButton.setPreferredSize(new Dimension(30,30)); + JButton leftButton = new JButton(new ImageIcon(ImageIO.read(new File("images/left.png")))); + leftButton.setPreferredSize(new Dimension(30,30)); + JButton upButton = new JButton(new ImageIcon(ImageIO.read(new File("images/up.png")))); + upButton.setPreferredSize(new Dimension(30,30)); + JButton downButton = new JButton(new ImageIcon(ImageIO.read(new File("images/down.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.getEntityBattleship() == null) + return; + switch (event.getActionCommand()) { + case "left" -> drawingBattleship.moveTransport(DirectionType.LEFT); + case "right" -> drawingBattleship.moveTransport(DirectionType.RIGHT); + case "up" -> drawingBattleship.moveTransport(DirectionType.UP); + case "down" -> drawingBattleship.moveTransport(DirectionType.DOWN); + } + draw(); + } + private void draw() { + if (drawingBattleship == null) + { + return; + } + pictureBox.repaint(); + } +} \ No newline at end of file