PIbd-22 Isaeva A.I. Lab work 3 Hard #4

Closed
dex_moth wants to merge 2 commits from lab3 into lab2
3 changed files with 65 additions and 15 deletions
Showing only changes of commit aad104ad8e - Show all commits

View File

@ -70,6 +70,7 @@ public class FormAirbusCollection extends JFrame {
@Override
public void actionPerformed(ActionEvent e) {
FormGenerationAirbus form = new FormGenerationAirbus();
dispose();
}
};

View File

@ -1,13 +1,22 @@
import Drawnings.DrawningAirbus;
import Drawnings.*;
import Entities.EntityAirbus;
import Entities.EntityPlane;
import Generics.AirbusGenericCollection;
import Generics.GenericParametrized;
import MovementStrategy.DrawningObjectAirbus;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.*;
public class FormGenerationAirbus extends JFrame {
Canvas canvas;
GenericParametrized<EntityAirbus, IDrawningPortholes> generic;
DrawningAirbus _airbus;
private int _width = 800;
private int _height = 500;
JButton buttonGenerate;
@ -17,8 +26,10 @@ public class FormGenerationAirbus extends JFrame {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(null);
generic = new GenericParametrized<>(50, 50, _width, _height);
canvas = new FormGenerationAirbus.Canvas();
canvas.setBounds(0 , 0, 800, 500);
canvas.setBounds(0 , 0, _width, _height);
buttonGenerate = new JButton("Сгенерировать аэробус");
buttonGenerate.setMargin(new Insets(0, 0, 0, 0));
@ -27,17 +38,53 @@ public class FormGenerationAirbus extends JFrame {
add(canvas);
add(buttonGenerate);
setVisible(true);
buttonGenerate.addActionListener(GenerateAirbus);
}
class Canvas extends JComponent {
public AirbusGenericCollection<DrawningAirbus, DrawningObjectAirbus> _airbus;
ActionListener GenerateAirbus = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Random rand = new Random();
// генерация сущности
EntityAirbus entity;
if (rand.nextBoolean()) {
entity = new EntityAirbus(
rand.nextInt(200) + 100, rand.nextInt(2000) + 1000,
new Color(rand.nextInt(0, 256), rand.nextInt(0, 256), rand.nextInt(0, 256)));
} else {
entity = new EntityPlane(rand.nextInt(200) + 100, rand.nextInt(2000) + 1000,
new Color(rand.nextInt(0, 256), rand.nextInt(0, 256), rand.nextInt(0, 256)),
new Color(rand.nextInt(0, 256), rand.nextInt(0, 256), rand.nextInt(0, 256)),
rand.nextBoolean(), rand.nextBoolean());
}
generic.addAirbus(entity);
// генерация иллюминаторов
IDrawningPortholes potholes;
int n = rand.nextInt(3);
if (n == 0)
potholes = new DrawningPortholesCircle();
else if (n == 1)
potholes = new DrawningPortholesHeart();
else
potholes = new DrawningPortholesSquare();
potholes.SetCount(rand.nextInt(1,4)*10);
generic.addPortholes(potholes);
// отрисовка
_airbus = generic.randomDrawingObject();
_airbus.SetPosition(100, 100);
canvas.repaint();
}
};
class Canvas extends JComponent {
public Canvas() {}
public void paintComponent(Graphics g) {
super.paintComponents(g);
if (_airbus.ShowAirbus() != null) {
g.drawImage(_airbus.ShowAirbus(), 0, 0, this);
}
Graphics2D g2d = (Graphics2D)g;
if (_airbus != null)
_airbus.DrawTransport(g2d);
super.repaint();
}
}

View File

@ -24,6 +24,8 @@ public class GenericParametrized<T extends EntityAirbus, U extends IDrawningPort
CountPortholes = 0;
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
_airbus = new ArrayList<T>();
_portholes = new ArrayList<U>();
}
public boolean addAirbus(T airbus)
Review

Методы добавления должны быть полиморфными

Методы добавления должны быть полиморфными
@ -52,14 +54,7 @@ public class GenericParametrized<T extends EntityAirbus, U extends IDrawningPort
int ar = rand.nextInt(CountAirbus);
int port = rand.nextInt(CountPortholes);
DrawningAirbus drawningAirbus;
if (_airbus.get(ar) instanceof EntityAirbus)
drawningAirbus = new DrawningAirbus(
_airbus.get(ar).getSpeed(),
_airbus.get(ar).getWeight(),
_airbus.get(ar).getBodyColor(),
_portholes.get(port).getCount(),
_pictureWidth, _pictureHeight);
else
if (_airbus.get(ar) instanceof EntityPlane)
drawningAirbus = new DrawningPlane(
_airbus.get(ar).getSpeed(),
_airbus.get(ar).getWeight(),
@ -69,6 +64,13 @@ public class GenericParametrized<T extends EntityAirbus, U extends IDrawningPort
((EntityPlane)_airbus.get(ar)).IsCompartment(),
((EntityPlane)_airbus.get(ar)).IsAdditionalEngine(),
_pictureWidth, _pictureHeight);
else
drawningAirbus = new DrawningAirbus(
_airbus.get(ar).getSpeed(),
_airbus.get(ar).getWeight(),
_airbus.get(ar).getBodyColor(),
_portholes.get(port).getCount(),
_pictureWidth, _pictureHeight);
return drawningAirbus;
}
}