lab3 some minor fixes

This commit is contained in:
Zakharov_Rostislav 2023-11-10 10:06:27 +04:00
parent 42e878848f
commit 957775120d
2 changed files with 24 additions and 24 deletions

View File

@ -1,18 +1,17 @@
package frames; package frames;
import drawing_objects.*; import drawing_objects.DrawingShip;
import entities.EntityBattleship; import drawing_objects.IDrawBlocks;
import entities.EntityShip; import entities.EntityShip;
import generics.HardGeneric; import generics.HardGeneric;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.util.Random;
public class HardFrame extends JFrame { public class HardFrame extends JFrame {
HardGeneric<EntityShip, IDrawBlocks> generic; HardGeneric<EntityShip, IDrawBlocks> generic;
DrawingShip drawing; DrawingShip drawing;
private JComponent pictureBox; private final JComponent pictureBox;
private final int pictureBoxWidth = 200; private final int pictureBoxWidth = 200;
private final int pictureBoxHeight = 100; private final int pictureBoxHeight = 100;
public HardFrame(){ public HardFrame(){
@ -28,13 +27,15 @@ public class HardFrame extends JFrame {
}; };
pictureBox.setPreferredSize(new Dimension(pictureBoxWidth, pictureBoxHeight)); pictureBox.setPreferredSize(new Dimension(pictureBoxWidth, pictureBoxHeight));
JButton buttonMakeObject = new JButton("Создать новый объект"); JButton buttonMakeObject = new JButton("Создать новый объект");
Random rand = new Random(); generic = new HardGeneric<>(10, 25, pictureBoxWidth, pictureBoxHeight);
int size = rand.nextInt(1, 10); boolean check;
generic = new HardGeneric<>(size, size, pictureBoxWidth, pictureBoxHeight); do{
for(int i = 0; i < size; i++){ check = generic.insertShip(generic.makeRandomShip());
generic.insertShip(generic.makeRandomShip()); }while(check);
generic.insertBlock(generic.makeRandomBlock()); do {
} check = generic.insertBlock(generic.makeRandomBlock());
}while(check);
buttonMakeObject.addActionListener(e -> { buttonMakeObject.addActionListener(e -> {
DrawingShip drawingShip = generic.makeObject(); DrawingShip drawingShip = generic.makeObject();
drawingShip.setPosition(pictureBoxWidth / 2 - drawingShip.getWidth()/2, pictureBoxHeight / 2 - drawingShip.getHeight()/2); drawingShip.setPosition(pictureBoxWidth / 2 - drawingShip.getWidth()/2, pictureBoxHeight / 2 - drawingShip.getHeight()/2);

View File

@ -12,8 +12,8 @@ public class HardGeneric<T extends EntityShip, U extends IDrawBlocks>{
U[] blocks; U[] blocks;
private int shipsNumber; private int shipsNumber;
private int blocksNumber; private int blocksNumber;
private int pictureBoxWidth; private final int pictureBoxWidth;
private int pictureBoxHeight; private final int pictureBoxHeight;
public HardGeneric(int shipsCount, int blocksCount, int width, int height){ public HardGeneric(int shipsCount, int blocksCount, int width, int height){
shipsNumber = 0; shipsNumber = 0;
blocksNumber = 0; blocksNumber = 0;
@ -22,25 +22,25 @@ public class HardGeneric<T extends EntityShip, U extends IDrawBlocks>{
pictureBoxHeight = height; pictureBoxHeight = height;
pictureBoxWidth = width; pictureBoxWidth = width;
} }
public int insertShip(T entityShip){ public boolean insertShip(T entityShip){
if(ships[ships.length-1] != null) if(ships[ships.length-1] != null)
return -1; return false;
for(int i = shipsNumber -1; i>= 0; i--) { for(int i = shipsNumber -1; i>= 0; i--) {
ships[i + 1] = ships[i]; ships[i + 1] = ships[i];
} }
shipsNumber++; shipsNumber++;
ships[0] = entityShip; ships[0] = entityShip;
return 0; return true;
} }
public int insertBlock(U block){ public boolean insertBlock(U block){
if(blocks[blocks.length-1] != null) if(blocks[blocks.length-1] != null)
return -1; return false;
for(int i = blocksNumber -1; i>= 0; i--) { for(int i = blocksNumber -1; i>= 0; i--) {
blocks[i + 1] = blocks[i]; blocks[i + 1] = blocks[i];
} }
blocksNumber++; blocksNumber++;
blocks[0] = block; blocks[0] = block;
return 0; return true;
} }
public DrawingShip makeObject(){ public DrawingShip makeObject(){
Random rand = new Random(); Random rand = new Random();
@ -53,16 +53,15 @@ public class HardGeneric<T extends EntityShip, U extends IDrawBlocks>{
} }
public EntityShip makeRandomShip(){ public EntityShip makeRandomShip(){
Random random = new Random(); Random random = new Random();
EntityShip ship; if (random.nextInt(2) == 0) {
switch (random.nextInt(2)){ return new EntityShip(random.nextInt(100, 300), random.nextDouble(1000, 3000),
case 0 -> ship = new EntityShip(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)));
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)),
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()); random.nextBoolean(), random.nextBoolean());
} }
return ship;
} }
public IDrawBlocks makeRandomBlock(){ public IDrawBlocks makeRandomBlock(){
Random random = new Random(); Random random = new Random();