Дополнительное задание

This commit is contained in:
prodigygirl 2022-11-02 18:57:20 +04:00
parent 123fae95d6
commit 5fc726afe3
3 changed files with 150 additions and 0 deletions

View File

@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="FormGenericArmoredCar">
<grid id="27dc6" binding="mainPanel" custom-create="true" layout-manager="GridLayoutManager" row-count="2" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="833" height="459"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="6837" class="javax.swing.JButton" binding="createButton">
<constraints>
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Создать объекты"/>
</properties>
</component>
<hspacer id="8e062">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<vspacer id="31893">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
</children>
</grid>
</form>

View File

@ -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<EntityArmoredCar, IDrawingCaterpillar> 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();
}
};
}
}

View File

@ -0,0 +1,36 @@
import java.util.Random;
public class GenericEntityArmoredCar<T extends EntityArmoredCar, U extends IDrawingCaterpillar> {
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;
}
}