PIbd-21_KozyrevSS_GasolineT.../ComboFrame.java

106 lines
3.3 KiB
Java
Raw Normal View History

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
class DrawRandomTanker extends JComponent {
private DrawTanker _drawTanker;
private final ComboGenericCollection<BaseTanker, IWheelDraw> GeneratorTanker;
public DrawRandomTanker() {
GeneratorTanker = new ComboGenericCollection<>(25, 1000, 600);
GeneratorTanker.Add(GenerateTanker());
GeneratorTanker.Add(GenerateWheel());
GeneratorTanker.Add(GenerateTanker());
GeneratorTanker.Add(GenerateWheel());
GeneratorTanker.Add(GenerateTanker());
GeneratorTanker.Add(GenerateWheel());
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
if (_drawTanker == null)
return;
_drawTanker.DrawTransport(g);
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()
{
_drawTanker = GeneratorTanker.CreateDraw();
super.repaint();
}
}
class ComboFrame extends JFrame {
public ComboFrame()
{
initUI();
}
protected static final int Width = 1000;
protected static final int Height = 600;
DrawRandomTanker Tanker;
private void initUI()
{
setSize(Width, Height);
setTitle("TankGasoline");
setLocationRelativeTo(null);
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
Tanker = new DrawRandomTanker();
Tanker.setBounds(0, 0, Width, Height);
add(Tanker);
JButton UpdateButton = new JButton("Update tanker");
UpdateButton.setLayout(null);
UpdateButton.setBounds(Width-200, Height- 200, 100, 100);
add(UpdateButton);
UpdateButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Tanker.GenerateTanker_Click();
}
});
}
}