Laba4 KozyrevSS GasolineTanker JAVA HARD

This commit is contained in:
Sergey Kozyrev 2023-11-15 14:06:06 +04:00
parent 264bf6c6af
commit 59c78e5111
3 changed files with 43 additions and 39 deletions

View File

@ -5,6 +5,7 @@ import java.awt.event.ActionListener;
class CollectionFrame extends JComponent { class CollectionFrame extends JComponent {
private final CarsGenericCollection<DrawTanker, DrawingObjectTanker> _tanks; private final CarsGenericCollection<DrawTanker, DrawingObjectTanker> _tanks;
protected final int Width; protected final int Width;
protected final int Height; protected final int Height;
public CollectionFrame(int width, int height) public CollectionFrame(int width, int height)
@ -123,6 +124,8 @@ class GarageFrame extends JFrame {
} }
}); });
Collection = new CollectionFrame(Width-200, Height); Collection = new CollectionFrame(Width-200, Height);
Collection.setLayout(null); Collection.setLayout(null);
Collection.setBounds(0, 0, Width-200, Height); Collection.setBounds(0, 0, Width-200, Height);

View File

@ -10,14 +10,12 @@ class DrawRandomTanker extends JComponent {
public DrawRandomTanker() { public DrawRandomTanker() {
GeneratorTanker = new ComboGenericCollection<>(25, 1000, 600); GeneratorTanker = new ComboGenericCollection<>(25, 1000, 600);
GeneratorTanker.Add(GeneratorTanker.GenerateTanker()); GeneratorTanker.Add(GenerateTanker());
GeneratorTanker.Add(GeneratorTanker.GenerateTanker()); GeneratorTanker.Add(GenerateWheel());
GeneratorTanker.Add(GeneratorTanker.GenerateTanker()); GeneratorTanker.Add(GenerateTanker());
GeneratorTanker.Add(GeneratorTanker.GenerateTanker()); GeneratorTanker.Add(GenerateWheel());
GeneratorTanker.Add(GeneratorTanker.GenerateWheel()); GeneratorTanker.Add(GenerateTanker());
GeneratorTanker.Add(GeneratorTanker.GenerateWheel()); GeneratorTanker.Add(GenerateWheel());
GeneratorTanker.Add(GeneratorTanker.GenerateWheel());
GeneratorTanker.Add(GeneratorTanker.GenerateWheel());
} }
public void paintComponent(Graphics g) public void paintComponent(Graphics g)
{ {
@ -27,7 +25,40 @@ class DrawRandomTanker extends JComponent {
_drawTanker.DrawTransport(g); _drawTanker.DrawTransport(g);
super.repaint(); super.repaint();
} }
private boolean IntToBool(int n) {return n % 2 == 0;}
public BaseTanker GenerateTanker()
{
Random random = new Random();
int mode = random.nextInt(0, 100) % 2;
if (mode == 0)
{
return new BaseTanker(random.nextInt(100, 200), random.nextInt(2000, 4000),
new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)));
}
else
{
return new GasolineTanker(random.nextInt(100, 200), random.nextInt(2000, 4000),
new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)),
new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)),
IntToBool(random.nextInt(0, 100)),IntToBool(random.nextInt(0, 100)), IntToBool(random.nextInt(0, 100)));
}
}
public IWheelDraw GenerateWheel()
{
Random random = new Random();
int mode = random.nextInt(0, 100) % 3;
IWheelDraw Wheels = new DrawWheelSquare();
switch (mode)
{
case 0 -> {Wheels = new DrawWheelCircle();}
case 1 -> {Wheels = new DrawWheelClassic();}
case 2 -> {Wheels = new DrawWheelSquare();}
}
int count = random.nextInt(0, 100);
Wheels.setWheelCount(count);
return Wheels;
}
protected void GenerateTanker_Click() protected void GenerateTanker_Click()
{ {
_drawTanker = GeneratorTanker.CreateDraw(); _drawTanker = GeneratorTanker.CreateDraw();

View File

@ -22,37 +22,7 @@ public class ComboGenericCollection <T extends BaseTanker, U extends IWheelDraw
Height = height; Height = height;
} }
public T GenerateTanker()
{
int mode = random.nextInt(0, 100) % 2;
if (mode == 0)
{
return (T) new BaseTanker(random.nextInt(100, 200), random.nextInt(2000, 4000),
new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)));
}
else
{
return (T) new GasolineTanker(random.nextInt(100, 200), random.nextInt(2000, 4000),
new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)),
new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)),
IntToBool(random.nextInt(0, 100)),IntToBool(random.nextInt(0, 100)), IntToBool(random.nextInt(0, 100)));
}
}
public U GenerateWheel()
{
int mode = random.nextInt(0, 100) % 3;
U Wheels = (U) new DrawWheelSquare();
switch (mode)
{
case 0 -> {Wheels = (U) new DrawWheelCircle();}
case 1 -> {Wheels = (U) new DrawWheelClassic();}
case 2 -> {Wheels = (U) new DrawWheelSquare();}
}
int count = random.nextInt(0, 100);
Wheels.setWheelCount(count);
return Wheels;
}
public boolean Add(T tanker) public boolean Add(T tanker)
{ {
@ -83,7 +53,7 @@ public class ComboGenericCollection <T extends BaseTanker, U extends IWheelDraw
} }
private boolean IntToBool(int n) {return n % 2 == 0;}
public DrawTanker CreateDraw() public DrawTanker CreateDraw()
{ {