From 385c55967af175829143be0155695587314b0bc4 Mon Sep 17 00:00:00 2001 From: Artyom_Yashin Date: Fri, 10 Nov 2023 00:10:11 +0400 Subject: [PATCH] Lab3 Done --- src/Generics/HardGeneric.java | 59 +++++++++ src/Generics/PlanesGenericCollection.java | 123 ++++++++++++++++++ src/Generics/SetGeneric.java | 40 ++++++ src/Main.java | 4 +- .../DrawingEnginesNotRounded.java | 16 +++ .../DrawingEnginesRoundedBack.java | 16 +++ .../DrawingEnginesRoundedFront.java | 16 +++ src/drawing_objects/DrawingPlane.java | 5 + src/drawing_objects/IDrawEngines.java | 2 + src/form/FrameAirBomber.java | 25 +++- src/form/FrameHard.java | 81 ++++++++++++ src/form/FramePlaneCollection.java | 108 +++++++++++++++ 12 files changed, 491 insertions(+), 4 deletions(-) create mode 100644 src/Generics/HardGeneric.java create mode 100644 src/Generics/PlanesGenericCollection.java create mode 100644 src/Generics/SetGeneric.java create mode 100644 src/form/FrameHard.java create mode 100644 src/form/FramePlaneCollection.java diff --git a/src/Generics/HardGeneric.java b/src/Generics/HardGeneric.java new file mode 100644 index 0000000..99a8ee8 --- /dev/null +++ b/src/Generics/HardGeneric.java @@ -0,0 +1,59 @@ +package Generics; + +import drawing_objects.DrawingAirBomber; +import drawing_objects.DrawingPlane; +import drawing_objects.IDrawEngines; +import entities.EntityAirBomber; +import entities.EntityPlane; + +import java.util.Random; + +public class HardGeneric { + T[] planes; + U[] engines; + private int planesNumber; + private int enginesNumber; + private int pictureBoxWidth; + private int pictureBoxHeight; + + public HardGeneric(int planesCount, int enginesCount, int width, int height) { + planesNumber = 0; + enginesNumber = 0; + planes = (T[]) new EntityPlane[planesCount]; + engines = (U[]) new IDrawEngines[enginesCount]; + pictureBoxHeight = height; + pictureBoxWidth = width; + } + + public int InsertPlanes(T entityPlane) { + if (planes[planes.length - 1] != null) + return -1; + for (int i = planesNumber - 1; i >= 0; i--) { + planes[i + 1] = planes[i]; + } + planesNumber++; + planes[0] = entityPlane; + return 0; + } + + public int InsertEngine(U engine) { + if (engines[engines.length - 1] != null) + return -1; + for (int i = enginesNumber - 1; i >= 0; i--) { + engines[i + 1] = engines[i]; + } + enginesNumber++; + engines[0] = engine; + return 0; + } + + public DrawingPlane makeObject() { + Random rand = new Random(); + EntityPlane entity = planes[rand.nextInt(0, planesNumber)]; + IDrawEngines engine = engines[rand.nextInt(0, enginesNumber)]; + if(entity instanceof EntityAirBomber) + return new DrawingAirBomber(entity.getSpeed(), entity.getWeight(), entity.getBodyColor(), ((EntityAirBomber) entity).getAdditionalColor(), + ((EntityAirBomber) entity).getBombs(), ((EntityAirBomber) entity).getFuel(), pictureBoxWidth, pictureBoxHeight, engine.getType(), engine.getNumber()); + return new DrawingPlane(entity.getSpeed(), entity.getWeight(), entity.getBodyColor(), pictureBoxWidth, pictureBoxHeight, engine.getType(), engine.getNumber()); + } +} \ No newline at end of file diff --git a/src/Generics/PlanesGenericCollection.java b/src/Generics/PlanesGenericCollection.java new file mode 100644 index 0000000..55cc1cf --- /dev/null +++ b/src/Generics/PlanesGenericCollection.java @@ -0,0 +1,123 @@ +package Generics; + +import drawing_objects.DrawingPlane; +import movement_strategy.IMoveableObject; + +import java.awt.*; + +public class PlanesGenericCollection { + /// + /// Ширина окна прорисовки + /// + private int pictureWidth; + /// + /// Высота окна прорисовки + /// + private int _pictureHeight; + /// + /// Размер занимаемого объектом места (ширина) + /// + private final int _placeSizeWidth = 170; + /// + /// Размер занимаемого объектом места (высота) + /// + private final int _placeSizeHeight = 180; + /// + /// Набор объектов + /// + private SetGeneric collection; + /// + /// Конструктор + /// + /// + /// + public PlanesGenericCollection(int picWidth, int picHeight) + { + int width = picWidth / _placeSizeWidth; + int height = picHeight / _placeSizeHeight; + pictureWidth = picWidth; + _pictureHeight = picHeight; + collection = new SetGeneric(width * height, (Class)DrawingPlane.class); + } + /// + /// Перегрузка оператора сложения + /// + /// + /// + /// + public boolean Insert(T obj) + { + if (obj == null ) + return false; + return collection.insert(obj); + } + /// + /// Перегрузка оператора вычитания + /// + /// + /// + /// + public boolean Remove(int pos) + { + T obj = collection.Get(pos); + if (obj == null) + return false; + return collection.remove(pos); + } + /// + /// Получение объекта IMoveableObject + /// + /// + /// + public U GetU(int pos) + { + if(collection.Get(pos) == null) + return null; + return (U)collection.Get(pos).GetMoveableObject(); + } + /// + /// Вывод всего набора объектов + /// + /// + public void ShowPlanes(Graphics2D gr) + { + DrawBackground(gr); + DrawObjects(gr); + } + /// + /// Метод отрисовки фона + /// + /// + private void DrawBackground(Graphics2D g) + { + BasicStroke pen = new BasicStroke(3); + g.setStroke(pen); + for (int i = 0; i < pictureWidth / _placeSizeWidth; i++) + { + for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j) + {//линия разметки места + g.drawLine(i * _placeSizeWidth + 10, j * _placeSizeHeight +5, + i * _placeSizeWidth + _placeSizeWidth / 2 + 50, j * _placeSizeHeight +5); + } + g.drawLine(i * _placeSizeWidth + 10, 5, i * _placeSizeWidth + 10, + _pictureHeight / _placeSizeHeight * _placeSizeHeight +5); + } + } + /// + /// Метод прорисовки объектов + /// + /// + private void DrawObjects(Graphics2D g) + { + for (int i = 0; i < collection.getCount(); i++) + { + DrawingPlane plane = collection.Get(i); + if (plane != null) + { + int inRow = pictureWidth / _placeSizeWidth; + plane.setPosition(pictureWidth - _placeSizeWidth - (i % inRow * _placeSizeWidth)-8, i / inRow * _placeSizeHeight + 20); + plane.drawTransport(g); + } + } + } +} diff --git a/src/Generics/SetGeneric.java b/src/Generics/SetGeneric.java new file mode 100644 index 0000000..433dd42 --- /dev/null +++ b/src/Generics/SetGeneric.java @@ -0,0 +1,40 @@ +package Generics; +import java.lang.reflect.Array; + +public class SetGeneric{ + private final T[] places; + public int getCount() {return places.length;} + public SetGeneric(int count, Class type){ + places = (T[])Array.newInstance(type, count); + } + public boolean insert(T plane){ + return insert(plane, 0); + } + public boolean insert(T plane, int position){ + if (!(position >= 0 && position < places.length)) + return false; + if (places[position] != null) + { + int ind = position; + while (ind < places.length && places[ind] != null) + ind++; + if (ind == places.length) + return false; + for (int i = ind - 1; i >= position; i--) + places[i + 1] = places[i]; + } + places[position] = plane; + return true; + } + public boolean remove(int position){ + if(!(position >= 0 && position < getCount())) + return false; + places[position] = null; + return true; + } + public T Get(int position){ + if(!(position >= 0 && position < getCount())) + return null; + return places[position]; + } +} \ No newline at end of file diff --git a/src/Main.java b/src/Main.java index 9e1285e..7bc0280 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,7 +1,9 @@ import form.FrameAirBomber; +import form.FrameHard; +import form.FramePlaneCollection; import java.io.IOException; public class Main { - public static void main(String[] args) throws IOException { new FrameAirBomber(); } + public static void main(String[] args) throws IOException { new FrameHard(); } } diff --git a/src/drawing_objects/DrawingEnginesNotRounded.java b/src/drawing_objects/DrawingEnginesNotRounded.java index 28a92bb..a3c5f4b 100644 --- a/src/drawing_objects/DrawingEnginesNotRounded.java +++ b/src/drawing_objects/DrawingEnginesNotRounded.java @@ -5,6 +5,22 @@ import java.awt.*; public class DrawingEnginesNotRounded implements IDrawEngines{ private EngineNumber number; + + @Override + public int getType() { + return 1; + } + + public int getNumber(){ + int x = 0; + if(number == EngineNumber.TWO) + x = 2; + if(number == EngineNumber.FOUR) + x = 4; + if(number == EngineNumber.SIX) + x = 6; + return x; + } public void setNumber(int x){ if(x <= 2) number = EngineNumber.TWO; diff --git a/src/drawing_objects/DrawingEnginesRoundedBack.java b/src/drawing_objects/DrawingEnginesRoundedBack.java index 5fc9108..9689df6 100644 --- a/src/drawing_objects/DrawingEnginesRoundedBack.java +++ b/src/drawing_objects/DrawingEnginesRoundedBack.java @@ -5,6 +5,22 @@ import java.awt.*; public class DrawingEnginesRoundedBack implements IDrawEngines { private EngineNumber number; + + @Override + public int getType() { + return 0; + } + + public int getNumber(){ + int x = 0; + if(number == EngineNumber.TWO) + x = 2; + if(number == EngineNumber.FOUR) + x = 4; + if(number == EngineNumber.SIX) + x = 6; + return x; + } public void setNumber(int x){ if(x <= 2) number = EngineNumber.TWO; diff --git a/src/drawing_objects/DrawingEnginesRoundedFront.java b/src/drawing_objects/DrawingEnginesRoundedFront.java index 95010af..a0146df 100644 --- a/src/drawing_objects/DrawingEnginesRoundedFront.java +++ b/src/drawing_objects/DrawingEnginesRoundedFront.java @@ -5,6 +5,22 @@ import java.awt.*; public class DrawingEnginesRoundedFront implements IDrawEngines { private EngineNumber number; + + @Override + public int getType() { + return 2; + } + + public int getNumber(){ + int x = 0; + if(number == EngineNumber.TWO) + x = 2; + if(number == EngineNumber.FOUR) + x = 4; + if(number == EngineNumber.SIX) + x = 6; + return x; + } public void setNumber(int x){ if(x <= 2) number = EngineNumber.TWO; diff --git a/src/drawing_objects/DrawingPlane.java b/src/drawing_objects/DrawingPlane.java index a48f135..84bd624 100644 --- a/src/drawing_objects/DrawingPlane.java +++ b/src/drawing_objects/DrawingPlane.java @@ -2,6 +2,8 @@ package drawing_objects; import entities.EntityPlane; import enums.*; +import movement_strategy.DrawingObjectPlane; +import movement_strategy.IMoveableObject; import java.awt.*; @@ -41,6 +43,9 @@ public class DrawingPlane { } drawingEngines.setNumber(enginesNumber); } + + public IMoveableObject GetMoveableObject() {return new DrawingObjectPlane(this);} + public void setPosition(int x, int y) { if (x < 0 || y < 0 || x + PLANE_WIDTH > pictureWidth || y + PLANE_HEIGHT > pictureHeight) x = y = 0; diff --git a/src/drawing_objects/IDrawEngines.java b/src/drawing_objects/IDrawEngines.java index 7f88167..0a8ff3c 100644 --- a/src/drawing_objects/IDrawEngines.java +++ b/src/drawing_objects/IDrawEngines.java @@ -3,6 +3,8 @@ package drawing_objects; import java.awt.*; public interface IDrawEngines { + public int getType(); + public int getNumber(); public void setNumber(int x); public void drawEngines(Graphics2D graphics2D, int _startX, int _startY); } diff --git a/src/form/FrameAirBomber.java b/src/form/FrameAirBomber.java index 4fc84dd..8b6ff23 100644 --- a/src/form/FrameAirBomber.java +++ b/src/form/FrameAirBomber.java @@ -16,6 +16,12 @@ import java.util.Random; public class FrameAirBomber extends JFrame { private DrawingPlane drawingPlane; private AbstractStrategy abstractStrategy; + public JButton selectPlaneButton; + + private DrawingPlane selectedPlane; + public DrawingPlane getSelectedPlane() { + return selectedPlane; + } private JComboBox comboBoxStrategy; private final JComponent pictureBox; public FrameAirBomber() throws IOException { @@ -39,6 +45,7 @@ public class FrameAirBomber extends JFrame { JButton leftButton = new JButton(new ImageIcon(ImageIO.read(new File("images/left.png")))); JButton upButton = new JButton(new ImageIcon(ImageIO.read(new File("images/up.png")))); JButton downButton = new JButton(new ImageIcon(ImageIO.read(new File("images/down.png")))); + selectPlaneButton = new JButton("Выбрать самолет"); pictureBox.setBounds( 0, 0, getContentPane().getWidth(), getContentPane().getHeight()); //ActionListeners and ActionCommand addition createPlaneButton.addActionListener(e -> buttonCreatePlaneClick()); @@ -67,6 +74,9 @@ public class FrameAirBomber extends JFrame { constraints.gridx = 1; constraints.gridy = 0; createPanel.add(createAirBomberButton, constraints); + constraints.gridx = 2; + constraints.gridy = 0; + createPanel.add(selectPlaneButton, constraints); //movementPanel JPanel movementPanel = new JPanel(new GridBagLayout()); rightButton.setPreferredSize(new Dimension(30,30)); @@ -106,15 +116,18 @@ public class FrameAirBomber extends JFrame { private void buttonCreateAirBomberClick() { Random random = new Random(); pictureBox.setBounds(0,0,getContentPane().getWidth(),getContentPane().getHeight()); - drawingPlane = new DrawingAirBomber(random.nextInt(200) + 100, random.nextInt(2000) + 1000, new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)), - new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)), random.nextBoolean(), random.nextBoolean(), pictureBox.getWidth(), pictureBox.getHeight(), random.nextInt(3),(random.nextInt(3)+1)*2); + Color bodyColor = JColorChooser.showDialog(null, "Choose a color", Color.RED); + Color additionalColor = JColorChooser.showDialog(null, "Choose a color", Color.RED); + drawingPlane = new DrawingAirBomber(random.nextInt(200) + 100, random.nextInt(2000) + 1000, bodyColor, + additionalColor, random.nextBoolean(), random.nextBoolean(), pictureBox.getWidth(), pictureBox.getHeight(), random.nextInt(3),(random.nextInt(3)+1)*2); drawingPlane.setPosition(random.nextInt(90) + 10, random.nextInt(90) + 10); draw(); } private void buttonCreatePlaneClick(){ Random random = new Random(); pictureBox.setBounds(0,0,getContentPane().getWidth(),getContentPane().getHeight()); - drawingPlane = new DrawingPlane(random.nextInt(200) + 100, random.nextInt(2000) + 1000, new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)), + Color bodyColor = JColorChooser.showDialog(null, "Choose a color", Color.RED); + drawingPlane = new DrawingPlane(random.nextInt(200) + 100, random.nextInt(2000) + 1000, bodyColor, pictureBox.getWidth(), pictureBox.getHeight(), random.nextInt(3),(random.nextInt(3)+1)*2); drawingPlane.setPosition(random.nextInt(90) + 10, random.nextInt(90) + 10); draw(); @@ -181,4 +194,10 @@ public class FrameAirBomber extends JFrame { return; pictureBox.repaint(); } + public void select(){ + if (drawingPlane == null) { + return; + } + selectedPlane = drawingPlane; + } } \ No newline at end of file diff --git a/src/form/FrameHard.java b/src/form/FrameHard.java new file mode 100644 index 0000000..a00f586 --- /dev/null +++ b/src/form/FrameHard.java @@ -0,0 +1,81 @@ +package form; + +import Generics.HardGeneric; +import drawing_objects.*; +import entities.EntityAirBomber; +import entities.EntityPlane; + +import javax.swing.*; +import java.awt.*; +import java.util.Random; + +public class FrameHard extends JFrame { + HardGeneric generic; + DrawingPlane drawing; + private JComponent pictureBox; + private final int pictureBoxWidth = 200; + private final int pictureBoxHeight = 200; + + public FrameHard() { + setLocationRelativeTo(null); + setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); + pictureBox = new JComponent() { + public void paintComponent(Graphics graphics) { + super.paintComponent(graphics); + Graphics2D graphics2D = (Graphics2D) graphics; + if (drawing != null) drawing.drawTransport(graphics2D); + super.repaint(); + } + }; + pictureBox.setPreferredSize(new Dimension(pictureBoxWidth, pictureBoxHeight)); + JButton buttonMakeObject = new JButton("Создать новый объект"); + Random rand = new Random(); + int size = rand.nextInt(1, 10); + generic = new HardGeneric<>(size, size, pictureBoxWidth, pictureBoxHeight); + for (int i = 0; i < size; i++) { + generic.InsertPlanes(makeRandomPlane()); + generic.InsertEngine(makeRandomEngine()); + } + buttonMakeObject.addActionListener(e -> { + DrawingPlane drawingShip = generic.makeObject(); + drawingShip.setPosition(pictureBoxWidth / 2 - drawingShip.getWidth() / 2, pictureBoxHeight / 2 - drawingShip.getHeight() / 2); + drawing = drawingShip; + draw(); + }); + setLayout(new BorderLayout()); + add(pictureBox, BorderLayout.CENTER); + add(buttonMakeObject, BorderLayout.SOUTH); + pack(); + setVisible(true); + } + + public EntityPlane makeRandomPlane() { + Random rand = new Random(); + EntityPlane plane; + switch (rand.nextInt(2)){ + case 1 -> plane = new EntityAirBomber(rand.nextInt(100, 300), rand.nextDouble(1000, 3000), + new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)), + new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)), + rand.nextBoolean(), rand.nextBoolean()); + default -> plane = new EntityPlane(rand.nextInt(100, 300), rand.nextDouble(1000, 3000), + new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256))); + } + return plane; + } + + public IDrawEngines makeRandomEngine() { + Random random = new Random(); + IDrawEngines engines; + switch (random.nextInt(3)) { + case 1 -> engines = new DrawingEnginesNotRounded(); + case 2 -> engines = new DrawingEnginesRoundedFront(); + default -> engines = new DrawingEnginesRoundedBack(); + } + engines.setNumber((random.nextInt(3) + 1) * 2); + return engines; + } + + void draw() { + pictureBox.repaint(); + } +} diff --git a/src/form/FramePlaneCollection.java b/src/form/FramePlaneCollection.java new file mode 100644 index 0000000..e046ddd --- /dev/null +++ b/src/form/FramePlaneCollection.java @@ -0,0 +1,108 @@ +package form; + +import javax.swing.*; +import Generics.PlanesGenericCollection; +import drawing_objects.DrawingPlane; +import movement_strategy.DrawingObjectPlane; + +import java.awt.*; +import java.io.IOException; + +public class FramePlaneCollection extends JFrame { + private PlanesGenericCollection planes; + JComponent pictureBoxCollection; + TextField textFieldNumber; + public FramePlaneCollection(){ + super("Набор самолетов"); + setSize(new Dimension(900,600)); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + creteGUI(); + planes = new PlanesGenericCollection<>(pictureBoxCollection.getWidth(), pictureBoxCollection.getHeight()); + pictureBoxCollection.repaint(); + setVisible(true); + } + + public void creteGUI(){ + pictureBoxCollection = new JComponent(){ + public void paintComponent(Graphics graphics){ + super.paintComponent(graphics); + Graphics2D graphics2D = (Graphics2D) graphics; + if (planes != null) planes.ShowPlanes(graphics2D); + super.repaint(); + } + }; + pictureBoxCollection.setBounds(0, 0, 700, 600); + JButton buttonAddPlane = new JButton("Добавить самолет"); + textFieldNumber = new TextField(); + JButton buttonRemovePlane = new JButton("Удалить самолет"); + JButton buttonRefreshCollection = new JButton("Обновить коллекцию"); + //ActionListeners + buttonAddPlane.addActionListener(e -> ButtonAddPlaneClick()); + buttonRemovePlane.addActionListener(e -> ButtonRemovePlaneClick()); + buttonRefreshCollection.addActionListener(e -> ButtonRefreshCollectionClick()); + //addition to panel + JPanel panelCollection = new JPanel(new GridBagLayout()); + GridBagConstraints constraints = new GridBagConstraints(); + constraints.insets.left = constraints.insets.right = 2; + constraints.insets.top = constraints.insets.bottom = 20; + constraints.fill = GridBagConstraints.BOTH; + constraints.gridx = 0; + constraints.gridy = 0; + panelCollection.add(buttonAddPlane, constraints); + constraints.gridx = 0; + constraints.gridy = 1; + panelCollection.add(textFieldNumber, constraints); + constraints.gridx = 0; + constraints.gridy = 2; + panelCollection.add(buttonRemovePlane, constraints); + constraints.gridx = 0; + constraints.gridy = 5; + panelCollection.add(buttonRefreshCollection, constraints); + JPanel upperPanel = new JPanel(new BorderLayout()); + setLayout(new BorderLayout()); + add(panelCollection, BorderLayout.EAST); + add(pictureBoxCollection, BorderLayout.CENTER); + } + + private void ButtonAddPlaneClick(){ + FrameAirBomber form; + try { + form = new FrameAirBomber(); + } catch (IOException e){ + throw new RuntimeException(e); + } + form.selectPlaneButton.addActionListener(e -> { + form.select(); + var selectedPlane = form.getSelectedPlane(); + form.dispose(); + if (planes.Insert(selectedPlane)) + { + JOptionPane.showMessageDialog(this, "Объект добавлен"); + pictureBoxCollection.repaint(); + } + else + { + JOptionPane.showMessageDialog(this, "Не удалось добавить объект"); + } + }); + } + + private void ButtonRemovePlaneClick() + { + int pos = Integer.parseInt(textFieldNumber.getText()); + if (planes.Remove(pos)) + { + JOptionPane.showMessageDialog(this, "Объект удален"); + pictureBoxCollection.repaint(); + } + else + { + JOptionPane.showMessageDialog(this, "Не удалось удалить объект"); + } + } + + private void ButtonRefreshCollectionClick() + { + pictureBoxCollection.repaint(); + } +}