This commit is contained in:
Вячеслав Иванов 2023-11-11 19:48:39 +04:00
parent a7d52f3692
commit 35d781923f
2 changed files with 41 additions and 38 deletions

View File

@ -4,14 +4,16 @@ import DoubleDeckerBus.DrawningObjects.DrawningBus;
import DoubleDeckerBus.DrawningObjects.IDraw;
import DoubleDeckerBus.Entities.EntityBus;
import java.util.ArrayList;
import java.util.Random;
public class HardGeneric <T extends EntityBus,U extends IDraw> {
T[] busArray;
U[] doorArray;
ArrayList<T> Buses;
ArrayList<U> Doors;
private int currentSize;
private int CountBuses;
private int MaxCountBuses;
private int CountDoors;
private int MaxCountDoors;
private int pictureBoxWidth;
@ -19,44 +21,47 @@ public class HardGeneric <T extends EntityBus,U extends IDraw> {
private int pictureBoxHeight;
public HardGeneric(int maxCountBuses, int maxCountDoors, int width, int height) {
currentSize = 0;
MaxCountBuses = maxCountBuses;
MaxCountDoors = maxCountDoors;
busArray = (T[]) new EntityBus[MaxCountBuses];
doorArray = (U[]) new IDraw[MaxCountDoors];
Buses = new ArrayList<T>(MaxCountBuses);
Doors = new ArrayList<U>(MaxCountDoors);
CountBuses = 0;
CountDoors = 0;
pictureBoxHeight = height;
pictureBoxWidth = width;
}
public int insertBus(T entityBus) {
if (busArray[MaxCountBuses - 1] != null) {
return -1;
public boolean insertBus(T bus) {
if (bus == null) {
return false;
}
for (int i = currentSize - 1; i >= 0; i--) {
busArray[i + 1] = busArray[i];
doorArray[i + 1] = doorArray[i];
if (CountBuses > MaxCountBuses) {
return false;
}
currentSize++;
busArray[0] = entityBus;
return 0;
Buses.add(CountBuses++, bus);
return true;
}
public int insertDoor(U door) {
if (doorArray[MaxCountDoors - 1] != null) {
return -1;
public boolean insertDoor(U door) {
if (door == null) {
return false;
}
doorArray[0] = door;
return 0;
if (CountDoors > MaxCountDoors) {
return false;
}
Doors.add(CountDoors++, door);
return true;
}
public DrawningBus MakeObject() {
if (CountBuses == 0 || CountDoors == 0) {
return null;
}
Random rand = new Random();
int indBus = rand.nextInt(0, currentSize);
int indDoors = rand.nextInt(0,currentSize);
EntityBus entity = busArray[indBus];
IDraw door = doorArray[indDoors];
return new DrawningBus(entity.Speed(), entity.Weight(), entity.BodyColor(),
pictureBoxWidth, pictureBoxHeight);
int indBus = rand.nextInt(0, CountBuses);
int indDoors = rand.nextInt(0,CountDoors);
T entity = Buses.get(indBus);
U door = Doors.get(indDoors);
return new DrawningBus(entity.Speed(), entity.Weight(), entity.BodyColor(), pictureBoxWidth, pictureBoxHeight);
}
}

View File

@ -1,8 +1,6 @@
package DoubleDeckerBus;
import DoubleDeckerBus.DrawningObjects.DrawningBus;
import DoubleDeckerBus.DrawningObjects.DrawningDoor;
import DoubleDeckerBus.DrawningObjects.IDraw;
import DoubleDeckerBus.DrawningObjects.*;
import DoubleDeckerBus.Entities.EntityBus;
import DoubleDeckerBus.Generics.HardGeneric;
@ -12,7 +10,7 @@ import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
public class HardForm {
public class HardForm extends JFrame {
private Canvas canvas;
private int pictureBoxWidth;
@ -21,13 +19,13 @@ public class HardForm {
private EntityBus createRandomEntityBus() {
Random rand = new Random();
return new EntityBus(
rand.nextInt(100, 300),
rand.nextDouble(1000, 3000),
Color.getHSBColor(
rand.nextInt(0, 301),
rand.nextInt(0, 301),
rand.nextInt(0, 301)
)
rand.nextInt(100, 300),
rand.nextDouble(1000, 3000),
Color.getHSBColor(
rand.nextInt(0, 301),
rand.nextInt(0, 301),
rand.nextInt(0, 301)
)
);
}