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;
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<EntityShip, IDrawBlocks> 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);

View File

@ -12,8 +12,8 @@ public class HardGeneric<T extends EntityShip, U extends IDrawBlocks>{
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<T extends EntityShip, U extends IDrawBlocks>{
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<T extends EntityShip, U extends IDrawBlocks>{
}
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();