From 957775120dffabc78aa971e82e67f2116683f22b Mon Sep 17 00:00:00 2001 From: Zakharov_Rostislav Date: Fri, 10 Nov 2023 10:06:27 +0400 Subject: [PATCH] lab3 some minor fixes --- src/frames/HardFrame.java | 23 ++++++++++++----------- src/generics/HardGeneric.java | 25 ++++++++++++------------- 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/src/frames/HardFrame.java b/src/frames/HardFrame.java index 1b454ee..af62889 100644 --- a/src/frames/HardFrame.java +++ b/src/frames/HardFrame.java @@ -1,18 +1,17 @@ package frames; -import drawing_objects.*; -import entities.EntityBattleship; +import drawing_objects.DrawingShip; +import drawing_objects.IDrawBlocks; import entities.EntityShip; import generics.HardGeneric; import javax.swing.*; import java.awt.*; -import java.util.Random; public class HardFrame extends JFrame { HardGeneric generic; DrawingShip drawing; - private JComponent pictureBox; + private final JComponent pictureBox; private final int pictureBoxWidth = 200; private final int pictureBoxHeight = 100; public HardFrame(){ @@ -28,13 +27,15 @@ public class HardFrame extends JFrame { }; pictureBox.setPreferredSize(new Dimension(pictureBoxWidth, pictureBoxHeight)); JButton buttonMakeObject = new JButton("Создать новый объект"); - Random rand = new Random(); - int size = rand.nextInt(1, 10); - generic = new HardGeneric<>(size, size, pictureBoxWidth, pictureBoxHeight); - for(int i = 0; i < size; i++){ - generic.insertShip(generic.makeRandomShip()); - generic.insertBlock(generic.makeRandomBlock()); - } + generic = new HardGeneric<>(10, 25, pictureBoxWidth, pictureBoxHeight); + boolean check; + do{ + check = generic.insertShip(generic.makeRandomShip()); + }while(check); + do { + check = generic.insertBlock(generic.makeRandomBlock()); + }while(check); + buttonMakeObject.addActionListener(e -> { DrawingShip drawingShip = generic.makeObject(); drawingShip.setPosition(pictureBoxWidth / 2 - drawingShip.getWidth()/2, pictureBoxHeight / 2 - drawingShip.getHeight()/2); diff --git a/src/generics/HardGeneric.java b/src/generics/HardGeneric.java index 36f6ffc..616d531 100644 --- a/src/generics/HardGeneric.java +++ b/src/generics/HardGeneric.java @@ -12,8 +12,8 @@ public class HardGeneric{ U[] blocks; private int shipsNumber; private int blocksNumber; - private int pictureBoxWidth; - private int pictureBoxHeight; + private final int pictureBoxWidth; + private final int pictureBoxHeight; public HardGeneric(int shipsCount, int blocksCount, int width, int height){ shipsNumber = 0; blocksNumber = 0; @@ -22,25 +22,25 @@ public class HardGeneric{ pictureBoxHeight = height; pictureBoxWidth = width; } - public int insertShip(T entityShip){ + public boolean insertShip(T entityShip){ if(ships[ships.length-1] != null) - return -1; + return false; for(int i = shipsNumber -1; i>= 0; i--) { ships[i + 1] = ships[i]; } shipsNumber++; ships[0] = entityShip; - return 0; + return true; } - public int insertBlock(U block){ + public boolean insertBlock(U block){ if(blocks[blocks.length-1] != null) - return -1; + return false; for(int i = blocksNumber -1; i>= 0; i--) { blocks[i + 1] = blocks[i]; } blocksNumber++; blocks[0] = block; - return 0; + return true; } public DrawingShip makeObject(){ Random rand = new Random(); @@ -53,16 +53,15 @@ public class HardGeneric{ } public EntityShip makeRandomShip(){ Random random = new Random(); - EntityShip ship; - switch (random.nextInt(2)){ - case 0 -> ship = new EntityShip(random.nextInt(100, 300), random.nextDouble(1000,3000), + if (random.nextInt(2) == 0) { + return new EntityShip(random.nextInt(100, 300), random.nextDouble(1000, 3000), new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256))); - default -> ship = new EntityBattleship(random.nextInt(100, 300), random.nextDouble(1000,3000), + } else { + return new EntityBattleship(random.nextInt(100, 300), random.nextDouble(1000, 3000), 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()); } - return ship; } public IDrawBlocks makeRandomBlock(){ Random random = new Random();