2023-11-24 20:37:08 +03:00

91 lines
3.1 KiB
Java
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package Trolleybus;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
public class FormDifficult{
private JFrame frameMain;
private JPanel panelMain;
private JButton buttonCreate;
private GenericDifficult<EntityBus, IDrawingDoors> generator;
public FormDifficult() {
InitializeComponent();
Random rand = new Random();
// макс. кол-во вариаций форм дверей/видов автобуса
int maxCnt = rand.nextInt(5, 11);
generator = new GenericDifficult<>(maxCnt, maxCnt, panelMain.getWidth(), panelMain.getHeight());
// добавление в массивы с дверьми/сущностями рандомные варианты
for (int i = 0; i < maxCnt; i++) {
generator.Add(createRandomEntityBus());
generator.Add(createRandomDoors());
}
}
private void InitializeComponent() {
//Само окно
frameMain = new JFrame("Усложнённая лаб 3");
frameMain.setSize(900, 500);
frameMain.setLayout(new BorderLayout());
frameMain.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Панель, на которой отрисовывается
panelMain = new JPanel();
panelMain.setLayout(null);
buttonCreate = new JButton("Создать");
buttonCreate.setBounds(10,400,150,30);
buttonCreate.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
createRandomDrawingBus(e);
}
});
frameMain.add(panelMain, BorderLayout.CENTER);
panelMain.add(buttonCreate);
frameMain.setVisible(true);
}
private void Draw(DrawingBus drawingBus){
if (drawingBus == null) {
return;
}
Graphics g = panelMain.getGraphics();
// Очистка перед перерисовкой
panelMain.paint(g);
drawingBus.DrawTransport(g);
}
private void createRandomDrawingBus(ActionEvent e) {
DrawingBus drawingBus = generator.CreateObject();
drawingBus.SetPosition(50, 50);
Draw(drawingBus);
}
private EntityBus createRandomEntityBus() {
Random rand = new Random();
Color color = new Color(rand.nextInt(0, 256), rand.nextInt(0, 256), rand.nextInt(0, 256));
EntityBus entityBus = new EntityBus(rand.nextInt(100, 300), rand.nextDouble(1000, 3000), color);
return entityBus;
}
private IDrawingDoors createRandomDoors() {
IDrawingDoors doors;
Random rand = new Random();
int shape = rand.nextInt(1, 4);
if (shape == 1) {
doors = new DrawingDoors();
}
else if (shape == 2) {
doors = new DrawingOvalDoors();
}
else {
doors = new DrawingTriangleDoors();
}
doors.SetCntOfDoors(rand.nextInt(3, 6));
return doors;
}
}