diff --git a/src/main/java/FormGenericArmoredCar.form b/src/main/java/FormGenericArmoredCar.form new file mode 100644 index 0000000..d2b7d08 --- /dev/null +++ b/src/main/java/FormGenericArmoredCar.form @@ -0,0 +1,31 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/src/main/java/FormGenericArmoredCar.java b/src/main/java/FormGenericArmoredCar.java new file mode 100644 index 0000000..493803c --- /dev/null +++ b/src/main/java/FormGenericArmoredCar.java @@ -0,0 +1,83 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.Random; + +public class FormGenericArmoredCar extends JFrame{ + private JButton createButton; + private JPanel mainPanel; + GenericEntityArmoredCar genericEntityArmoredCar; + DrawingArmoredCar[] cars = new DrawingArmoredCar[3]; + + public FormGenericArmoredCar() { + super("Форма с новым объектом"); + + setBounds(100, 100, 1000, 700); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + fill(); + createButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + for (int i = 0; i < 3; i++) { + cars[i] = genericEntityArmoredCar.getDrawingObject(); + } + setData(); + + + } + }); + setContentPane(mainPanel); + setVisible(true); + } + private void fill() { + genericEntityArmoredCar = new GenericEntityArmoredCar<>(10); + for (int i = 0; i < 10; i++) { + Random r = new Random(); + EntityArmoredCar armoredCar = new EntityArmoredCar( + r.nextInt(1000, 2000), + r.nextInt(1000, 2000), + new Color(r.nextInt(256), r.nextInt(256), r.nextInt(256))); + IDrawingCaterpillar[] realisations = new IDrawingCaterpillar[]{ + new DrawingCaterpillar(armoredCar.getBodyColor()), + new DrawingCrossCaterpillar(armoredCar.getBodyColor()), + new DrawingDoubleCaterpillar(armoredCar.getBodyColor())}; + IDrawingCaterpillar drawingCaterpillar = realisations[r.nextInt(3)]; + genericEntityArmoredCar.Insert(armoredCar); + genericEntityArmoredCar.Insert(drawingCaterpillar); + } + } + private void setData() + { + int x = mainPanel.getWidth() / 4; + int y = mainPanel.getHeight() / 2; + for (DrawingArmoredCar armoredCar : + cars) { + armoredCar.SetPosition(x, y, mainPanel.getWidth(), mainPanel.getHeight()); + x += 150; + } + } + + private void DrawCars(Graphics2D g) { + for (DrawingArmoredCar armoredCar : cars) { + if (armoredCar == null) + return; + armoredCar.DrawTransport(g); + } + } + public static void main(String[] args) { + new FormGenericArmoredCar(); + } + + private void createUIComponents() { + mainPanel = new JPanel() { + @Override + public void paintComponent(Graphics g) { + super.paintComponent(g); + DrawCars((Graphics2D) g); + super.repaint(); + } + }; + } +} diff --git a/src/main/java/GenericEntityArmoredCar.java b/src/main/java/GenericEntityArmoredCar.java new file mode 100644 index 0000000..08ebb72 --- /dev/null +++ b/src/main/java/GenericEntityArmoredCar.java @@ -0,0 +1,36 @@ +import java.util.Random; + +public class GenericEntityArmoredCar { + private T[] armoredCars; + private int carsCount = 0; + private U[] caterpillars; + private int caterpillarsCount = 0; + + public GenericEntityArmoredCar(int numberObjects) { + this.armoredCars = (T[]) new EntityArmoredCar[numberObjects]; + this.caterpillars = (U[]) new IDrawingCaterpillar[numberObjects]; + } + + public void Insert(T armoredCar) + { + if (carsCount >= armoredCars.length) + return; + armoredCars[carsCount++] = armoredCar; + } + + public void Insert(U caterpillar) + { + if (caterpillarsCount >= caterpillars.length) + return; + caterpillars[caterpillarsCount++] = caterpillar; + } + + public DrawingArmoredCar getDrawingObject() { + Random r = new Random(); + EntityArmoredCar armoredCar = armoredCars[r.nextInt(carsCount)]; + IDrawingCaterpillar caterpillar = caterpillars[r.nextInt(caterpillarsCount)]; + DrawingArmoredCar o = new DrawingArmoredCar(armoredCar.getSpeed(), armoredCar.getWeight(), armoredCar.getBodyColor()); + o.SetCaterpillar(caterpillar); + return o; + } +}