This commit is contained in:
Вячеслав Иванов 2023-11-11 16:24:09 +04:00
parent 69f50aabc3
commit e40b4d1a35
6 changed files with 60 additions and 54 deletions

View File

@ -1,14 +1,14 @@
package DoubleDeckerBus; package DoubleDeckerBus;
import DoubleDeckerBus.DrawningObjects.DrawningBus; import DoubleDeckerBus.DrawningObjects.DrawningBus;
import DoubleDeckerBus.Generics.BusGenericCollection; import DoubleDeckerBus.Generics.TheBusesGenericCollection;
import DoubleDeckerBus.MovementStrategy.DrawningObjectBus; import DoubleDeckerBus.MovementStrategy.DrawningObjectBus;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
public class CollectionCanvas extends JComponent { public class CollectionCanvas extends JComponent {
public BusGenericCollection<DrawningBus, DrawningObjectBus> _bus; public TheBusesGenericCollection<DrawningBus, DrawningObjectBus> _bus;
@Override @Override
public void paintComponent (Graphics g){ public void paintComponent (Graphics g){

View File

@ -1,7 +1,7 @@
package DoubleDeckerBus; package DoubleDeckerBus;
import DoubleDeckerBus.DrawningObjects.DrawningBus; import DoubleDeckerBus.DrawningObjects.DrawningBus;
import DoubleDeckerBus.Generics.BusGenericCollection; import DoubleDeckerBus.Generics.TheBusesGenericCollection;
import DoubleDeckerBus.MovementStrategy.DrawningObjectBus; import DoubleDeckerBus.MovementStrategy.DrawningObjectBus;
import javax.swing.*; import javax.swing.*;
@ -9,7 +9,7 @@ import java.awt.*;
import java.awt.event.*; import java.awt.event.*;
public class FormBusCollection { public class FormBusCollection {
private final BusGenericCollection<DrawningBus, DrawningObjectBus> _bus; private final TheBusesGenericCollection<DrawningBus, DrawningObjectBus> _bus;
private int pictureBoxWidth = 605; private int pictureBoxWidth = 605;
private int pictureBoxHeight = 426; private int pictureBoxHeight = 426;
@ -23,7 +23,7 @@ public class FormBusCollection {
} }
public FormBusCollection() { public FormBusCollection() {
_bus = new BusGenericCollection<>(pictureBoxWidth, pictureBoxHeight); _bus = new TheBusesGenericCollection<>(pictureBoxWidth, pictureBoxHeight);
canvas = new CollectionCanvas(); canvas = new CollectionCanvas();
JPanel toolBox = new JPanel(); JPanel toolBox = new JPanel();
JFrame collectionFrame = new JFrame(); JFrame collectionFrame = new JFrame();

View File

@ -7,57 +7,56 @@ import DoubleDeckerBus.Entities.EntityBus;
import java.util.Random; import java.util.Random;
public class HardGeneric <T extends EntityBus,U extends IDraw> { public class HardGeneric <T extends EntityBus,U extends IDraw> {
T[] arrFirst; T[] busArray;
U[] arrSecond; U[] doorArray;
private int curSz; private int currentSize;
private int CountFirst; private int MaxCountBuses;
private int CountSecond; private int MaxCountDoors;
private int pictureBoxWidth; private int pictureBoxWidth;
private int pictureBoxHeight; private int pictureBoxHeight;
public HardGeneric(int countFirst, int countSecond, int width, int height){ public HardGeneric(int maxCountBuses, int countSecond, int width, int height) {
curSz = 0; currentSize = 0;
CountFirst = countFirst; MaxCountBuses = maxCountBuses;
CountSecond = countSecond; MaxCountDoors = countSecond;
arrFirst = (T[]) new EntityBus[CountFirst]; busArray = (T[]) new EntityBus[MaxCountBuses];
arrSecond = (U[]) new IDraw[CountSecond]; doorArray = (U[]) new IDraw[MaxCountDoors];
pictureBoxHeight = height; pictureBoxHeight = height;
pictureBoxWidth = width; pictureBoxWidth = width;
} }
public int InsertFirst(T entityBus) { public int insertBus(T entityBus) {
if (arrFirst[CountFirst-1] != null) { if (busArray[MaxCountBuses - 1] != null) {
return -1; return -1;
} }
for (int i = curSz - 1; i >= 0; i--) { for (int i = currentSize - 1; i >= 0; i--) {
arrFirst[i + 1] = arrFirst[i]; busArray[i + 1] = busArray[i];
arrSecond[i + 1] = arrSecond[i]; doorArray[i + 1] = doorArray[i];
} }
curSz++; currentSize++;
arrFirst[0] = entityBus; busArray[0] = entityBus;
return 0; return 0;
} }
public int InsertSecond(U inter) { public int insertDoor(U inter) {
if (arrSecond[CountSecond-1] != null) { if (doorArray[MaxCountBuses - 1] != null) {
return -1; return -1;
} }
arrSecond[0] = inter; doorArray[0] = inter;
return 0; return 0;
} }
public DrawningBus MakeObject() { public DrawningBus MakeObject() {
Random rand = new Random(); Random rand = new Random();
int indFirst = rand.nextInt(0, curSz); int indBus = rand.nextInt(0, currentSize);
int indSecond = rand.nextInt(0,curSz); int indDoors = rand.nextInt(0,currentSize);
EntityBus entity = arrFirst[indFirst]; EntityBus entity = busArray[indBus];
IDraw inter = arrSecond[indSecond]; IDraw inter = doorArray[indDoors];
DrawningBus bus = new DrawningBus(entity.Speed(), entity.Weight(), entity.BodyColor(), return new DrawningBus(entity.Speed(), entity.Weight(), entity.BodyColor(),
pictureBoxWidth, pictureBoxHeight); pictureBoxWidth, pictureBoxHeight);
return bus;
} }
} }

View File

@ -6,7 +6,7 @@ import DoubleDeckerBus.MovementStrategy.IMoveableObject;
import java.awt.*; import java.awt.*;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
public class BusGenericCollection<T extends DrawningBus, U extends IMoveableObject> { public class TheBusesGenericCollection<T extends DrawningBus, U extends IMoveableObject> {
private final int _pictureWidth; private final int _pictureWidth;
private final int _pictureHeight; private final int _pictureHeight;
@ -17,7 +17,7 @@ public class BusGenericCollection<T extends DrawningBus, U extends IMoveableObje
private final SetGeneric<T> _collection; private final SetGeneric<T> _collection;
public BusGenericCollection(int picWidth, int picHeight){ public TheBusesGenericCollection(int picWidth, int picHeight){
int width = picWidth / _placeSizeWidth; int width = picWidth / _placeSizeWidth;
int height = picHeight / _placeSizeHeight; int height = picHeight / _placeSizeHeight;
_pictureWidth = picWidth; _pictureWidth = picWidth;

View File

@ -2,6 +2,7 @@ package DoubleDeckerBus;
import DoubleDeckerBus.DrawningObjects.DrawningBus; import DoubleDeckerBus.DrawningObjects.DrawningBus;
import DoubleDeckerBus.DrawningObjects.DrawningDoor; import DoubleDeckerBus.DrawningObjects.DrawningDoor;
import DoubleDeckerBus.DrawningObjects.DrawningDoorTriangle;
import DoubleDeckerBus.DrawningObjects.IDraw; import DoubleDeckerBus.DrawningObjects.IDraw;
import DoubleDeckerBus.Entities.EntityBus; import DoubleDeckerBus.Entities.EntityBus;
import DoubleDeckerBus.Generics.HardGeneric; import DoubleDeckerBus.Generics.HardGeneric;
@ -18,55 +19,61 @@ public class HardForm {
private int pictureBoxWidth; private int pictureBoxWidth;
private int pictureBoxHeight; private int pictureBoxHeight;
private EntityBus makeEntity(){ private EntityBus createRandomEntityBus() {
Random rand = new Random(); Random rand = new Random();
return new EntityBus(rand.nextInt(100, 300), rand.nextDouble(1000,3000), return new EntityBus(
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)
)
);
} }
void Draw(){ void redrawCanvas() {
if (canvas == null) { if (canvas == null) {
return; return;
} }
canvas.repaint(); canvas.repaint();
} }
private IDraw makeIDraw(){ private IDraw createDrawningDoor(){
Random rand = new Random();
return new DrawningDoor(pictureBoxWidth, pictureBoxHeight, 0, 0); return new DrawningDoor(pictureBoxWidth, pictureBoxHeight, 0, 0);
} }
public HardForm(){ public HardForm() {
Random rand = new Random(); Random rand = new Random();
int sz = rand.nextInt(1, 10); int size = rand.nextInt(1, 10);
JFrame HardFrame = new JFrame(); JFrame HardFrame = new JFrame();
HardFrame.setSize(700, 400); HardFrame.setSize(700, 400);
HardFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); HardFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
canvas = new Canvas(); canvas = new Canvas();
canvas.setBounds(0,0,pictureBoxWidth,pictureBoxHeight); canvas.setBounds(0,0,pictureBoxWidth,pictureBoxHeight);
JButton makeObject = new JButton("Создать"); JButton createObjectButton = new JButton("Создать");
makeObject.setBounds(0, 0, 100, 40); createObjectButton.setBounds(0, 0, 100, 40);
canvas.add(makeObject); canvas.add(createObjectButton);
HardFrame.setContentPane(canvas); HardFrame.setContentPane(canvas);
HardFrame.setVisible(true); HardFrame.setVisible(true);
pictureBoxHeight = canvas.getHeight(); pictureBoxHeight = canvas.getHeight();
pictureBoxWidth = canvas.getWidth(); pictureBoxWidth = canvas.getWidth();
HardGeneric<EntityBus, IDraw> toDraw = new HardGeneric<>(sz, sz, pictureBoxWidth, pictureBoxHeight); HardGeneric<EntityBus, IDraw> objectGenerator = new HardGeneric<>(size, size, pictureBoxWidth, pictureBoxHeight);
for (int i = 0; i < sz; i++) { for (int i = 0; i < size; i++) {
EntityBus ent = makeEntity(); EntityBus ent = createRandomEntityBus();
toDraw.InsertFirst(ent); objectGenerator.insertBus(ent);
toDraw.InsertSecond(makeIDraw()); objectGenerator.insertDoor(createDrawningDoor());
} }
makeObject.addActionListener( createObjectButton.addActionListener(
new ActionListener() { new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
DrawningBus DrawningBus = toDraw.MakeObject(); DrawningBus DrawningBus = objectGenerator.MakeObject();
DrawningBus.SetPosition(pictureBoxWidth / 2 - DrawningBus.GetWidth()/2, DrawningBus.SetPosition(pictureBoxWidth / 2 - DrawningBus.GetWidth()/2,
pictureBoxHeight / 2 - DrawningBus.GetHeight()/2); pictureBoxHeight / 2 - DrawningBus.GetHeight()/2);
canvas.DrawningBus = DrawningBus; canvas.DrawningBus = DrawningBus;
Draw(); redrawCanvas();
} }
} }
); );

View File

@ -4,7 +4,7 @@ import java.io.IOException;
public class Main { public class Main {
public static void main(String[] args) throws IOException { public static void main(String[] args) throws IOException {
//FormDoubleDeckerBus form = new FormDoubleDeckerBus(); //new FormBusCollection();
new HardForm(); new HardForm();
} }
} }