From 42e878848f1185b3146f4eba28eb94b1aef3f045 Mon Sep 17 00:00:00 2001 From: Zakharov_Rostislav Date: Fri, 10 Nov 2023 00:10:09 +0400 Subject: [PATCH] lab3 some minor fixes --- src/frames/HardFrame.java | 28 ++-------------------------- src/generics/HardGeneric.java | 29 ++++++++++++++++++++++++++--- 2 files changed, 28 insertions(+), 29 deletions(-) diff --git a/src/frames/HardFrame.java b/src/frames/HardFrame.java index 7e37d71..1b454ee 100644 --- a/src/frames/HardFrame.java +++ b/src/frames/HardFrame.java @@ -32,8 +32,8 @@ public class HardFrame extends JFrame { int size = rand.nextInt(1, 10); generic = new HardGeneric<>(size, size, pictureBoxWidth, pictureBoxHeight); for(int i = 0; i < size; i++){ - generic.insertShip(makeRandomShip()); - generic.insertBlock(makeRandomBlock()); + generic.insertShip(generic.makeRandomShip()); + generic.insertBlock(generic.makeRandomBlock()); } buttonMakeObject.addActionListener(e -> { DrawingShip drawingShip = generic.makeObject(); @@ -47,29 +47,5 @@ public class HardFrame extends JFrame { pack(); setVisible(true); } - 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), - new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256))); - default -> ship = 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(); - IDrawBlocks block; - switch (random.nextInt(3)){ - case 1 -> block = new DrawingRoundBlocks(); - case 2 -> block = new DrawingCrossBlocks(); - default -> block = new DrawingBlocks(); - } - block.setNumber((random.nextInt(3) + 1) * 2); - return block; - } void draw(){pictureBox.repaint();} } \ No newline at end of file diff --git a/src/generics/HardGeneric.java b/src/generics/HardGeneric.java index f18ba18..36f6ffc 100644 --- a/src/generics/HardGeneric.java +++ b/src/generics/HardGeneric.java @@ -1,11 +1,10 @@ package generics; -import drawing_objects.DrawingBattleship; -import drawing_objects.DrawingShip; -import drawing_objects.IDrawBlocks; +import drawing_objects.*; import entities.EntityBattleship; import entities.EntityShip; +import java.awt.*; import java.util.Random; public class HardGeneric{ @@ -52,4 +51,28 @@ public class HardGeneric{ ((EntityBattleship) entity).getTurret(), ((EntityBattleship) entity).getRocketLauncher(), pictureBoxWidth, pictureBoxHeight, block.getType(), block.getNumber()); return new DrawingShip(entity.getSpeed(), entity.getWeight(), entity.getBodyColor(), pictureBoxWidth, pictureBoxHeight, block.getType(), block.getNumber()); } + 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), + new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256))); + default -> ship = 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(); + IDrawBlocks block; + switch (random.nextInt(3)){ + case 1 -> block = new DrawingRoundBlocks(); + case 2 -> block = new DrawingCrossBlocks(); + default -> block = new DrawingBlocks(); + } + block.setNumber((random.nextInt(3) + 1) * 2); + return block; + } }