From 4e19e9e45193f1d5b3689f19ca263ae5c8ec1f74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=93=D0=B0=D0=BB=D0=B8=D0=BD=D0=B0=20=D0=A4=D0=B5=D0=B4?= =?UTF-8?q?=D0=BE=D1=80=D0=B5=D0=BD=D0=BA=D0=BE?= Date: Sat, 18 Nov 2023 21:43:41 +0400 Subject: [PATCH] lab3 DONE! --- .../java/org/example/DrawingHydroplane.java | 6 + .../main/java/org/example/DrawingPlane.java | 13 ++ .../org/example/Form4GenericDopClass.java | 63 ++++++++ .../main/java/org/example/FormHydroplane.java | 12 +- .../org/example/FormPlaneCollecltion.java | 135 ++++++++++++++++++ .../java/org/example/GenericDopClass.java | 63 ++++++++ .../src/main/java/org/example/Main.java | 2 +- .../org/example/PlanesGenericCollection.java | 91 ++++++++++++ .../src/main/java/org/example/SetGeneric.java | 57 ++++++++ 9 files changed, 438 insertions(+), 4 deletions(-) create mode 100644 Hydroplane/src/main/java/org/example/Form4GenericDopClass.java create mode 100644 Hydroplane/src/main/java/org/example/FormPlaneCollecltion.java create mode 100644 Hydroplane/src/main/java/org/example/GenericDopClass.java create mode 100644 Hydroplane/src/main/java/org/example/PlanesGenericCollection.java create mode 100644 Hydroplane/src/main/java/org/example/SetGeneric.java diff --git a/Hydroplane/src/main/java/org/example/DrawingHydroplane.java b/Hydroplane/src/main/java/org/example/DrawingHydroplane.java index bac953c..3e0870e 100644 --- a/Hydroplane/src/main/java/org/example/DrawingHydroplane.java +++ b/Hydroplane/src/main/java/org/example/DrawingHydroplane.java @@ -12,6 +12,12 @@ public class DrawingHydroplane extends DrawingPlane{ } + public DrawingHydroplane(EntityPlane plane, IWindowDrawing _windowDrawing, int width, int height ){ + super(plane, _windowDrawing, width, height); + if (height < _pictureHeight || width < _pictureWidth) + return; + } + public void SetPosition(int x, int y) { _startPosX = Math.min(x, _pictureWidth-_planeWidth); diff --git a/Hydroplane/src/main/java/org/example/DrawingPlane.java b/Hydroplane/src/main/java/org/example/DrawingPlane.java index b1d5919..52ddea1 100644 --- a/Hydroplane/src/main/java/org/example/DrawingPlane.java +++ b/Hydroplane/src/main/java/org/example/DrawingPlane.java @@ -5,6 +5,8 @@ import java.util.*; public class DrawingPlane{ + public IMoveableObject GetMoveableObject() { return new DrawningObjectPlane(this);} + protected IWindowDrawing windowDrawing; public EntityPlane _EntityPlane; @@ -48,6 +50,17 @@ public class DrawingPlane{ } + public DrawingPlane(EntityPlane plane, IWindowDrawing _windowDrawing, int width, int height) + { + if (height < _planeHeight || width < _planeWidth) + return; + _pictureWidth = width; + _pictureHeight = height; + _EntityPlane = plane; + windowDrawing = _windowDrawing; + windowDrawing.setNumWindow(_EntityPlane.numWindow); + } + public void SetPosition(int x, int y) { _startPosX = Math.min(x, _pictureWidth-_planeWidth); diff --git a/Hydroplane/src/main/java/org/example/Form4GenericDopClass.java b/Hydroplane/src/main/java/org/example/Form4GenericDopClass.java new file mode 100644 index 0000000..51ec9f0 --- /dev/null +++ b/Hydroplane/src/main/java/org/example/Form4GenericDopClass.java @@ -0,0 +1,63 @@ +package org.example; + +import java.awt.Color; +import java.awt.Graphics; +import java.awt.Graphics2D; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +import javax.swing.JButton; +import javax.swing.JComponent; +import javax.swing.JFrame; + +public class Form4GenericDopClass extends JFrame { + static int pictureBoxWidth = 980; + static int pictureBoxHeight = 560; + public DrawingPlane _drawingPlane; + private class Canvas extends JComponent{ + public Canvas(){ + } + public void paintComponent (Graphics g){ + if (_drawingPlane == null){ + return; + } + super.paintComponents (g) ; + Graphics2D g2d = (Graphics2D)g; + _drawingPlane.SetPosition(50, 50); + _drawingPlane.DrawTransport(g2d); + super.repaint(); + } + } + GenericDopClass genericDopClass; + public Form4GenericDopClass(){ + _drawingPlane = null; + Canvas canv = new Canvas(); + setSize (1000, 600); + setLayout(null); + canv.setBounds(0,0,pictureBoxWidth, pictureBoxHeight); + + genericDopClass = new GenericDopClass<>(100, 100, pictureBoxWidth, pictureBoxHeight); + genericDopClass.addPlane(new EntityPlane(100, 100, Color.BLUE, 30)); + genericDopClass.addPlane(new EntityPlane(100, 100, Color.RED, 10)); + genericDopClass.addPlane(new EntityPlane(100, 100, Color.GRAY, 20)); + genericDopClass.addPlane(new EntityHydroplane(100, 100, Color.BLUE, Color.ORANGE, true, true, 20)); + genericDopClass.addWindow(new WindowDrawing()); + genericDopClass.addWindow(new WindowDrawingTringle()); + + JButton creatButton = new JButton("createButton"); + creatButton.addActionListener( + new ActionListener() { + public void actionPerformed(ActionEvent e){ + _drawingPlane = genericDopClass.getRDrawingObject(); + canv.repaint(); + } + } + ); + creatButton.setBounds(pictureBoxWidth/2, pictureBoxHeight-20, 120, 20); + + add(canv); + add(creatButton); + setVisible(true); + } +} + diff --git a/Hydroplane/src/main/java/org/example/FormHydroplane.java b/Hydroplane/src/main/java/org/example/FormHydroplane.java index 50b1588..5b78423 100644 --- a/Hydroplane/src/main/java/org/example/FormHydroplane.java +++ b/Hydroplane/src/main/java/org/example/FormHydroplane.java @@ -7,20 +7,25 @@ import java.awt.event.*; public class FormHydroplane{ - private DrawingPlane _drawingPlane; + public DrawingPlane _drawingPlane; + public DrawingPlane SelectedPlane; + public boolean DialogResult = false; + public JButton buttonSelectPlane; private AbstractStrategy abstractStrategy; Canvas canv; static int pictureBoxWidth = 980; static int pictureBoxHeight = 580; + public JFrame w; public void Draw(){ canv.repaint(); } public FormHydroplane(){ - JFrame w=new JFrame ("Hydroplane"); + w=new JFrame ("Hydroplane"); JButton buttonCreatePlane = new JButton("createPlane"); JButton buttonCreateHydroplane = new JButton("createHydroplane"); + buttonSelectPlane = new JButton("select plane"); JButton buttonStrategysStep = new JButton("strategys step"); JComboBox comboBoxStrategy = new JComboBox( new String[]{ @@ -162,7 +167,6 @@ public class FormHydroplane{ right.addActionListener(actioListener); w.setSize (1000, 600); - w.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); w.setLayout(null); canv = new Canvas(); canv.setBounds(0, 0, pictureBoxWidth, pictureBoxHeight); @@ -174,6 +178,7 @@ public class FormHydroplane{ right.setBounds(940, 520, 40, 40); comboBoxStrategy.setBounds(pictureBoxWidth - 150, 20, 150, 20); buttonStrategysStep.setBounds(pictureBoxWidth - 150, 45, 150, 20); + buttonSelectPlane.setBounds(pictureBoxWidth/2, 540, 150, 20); w.add(canv); w.add(buttonCreatePlane); w.add(buttonCreateHydroplane); @@ -183,6 +188,7 @@ public class FormHydroplane{ w.add(right); w.add(comboBoxStrategy); w.add(buttonStrategysStep); + w.add(buttonSelectPlane); w.setVisible(true); } diff --git a/Hydroplane/src/main/java/org/example/FormPlaneCollecltion.java b/Hydroplane/src/main/java/org/example/FormPlaneCollecltion.java new file mode 100644 index 0000000..5d0cc47 --- /dev/null +++ b/Hydroplane/src/main/java/org/example/FormPlaneCollecltion.java @@ -0,0 +1,135 @@ +package org.example; + +import java.awt.Graphics; +import java.awt.Graphics2D; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +import javax.swing.JButton; +import javax.swing.JComponent; +import javax.swing.JFrame; +import javax.swing.JOptionPane; +import javax.swing.JTextField; + +public class FormPlaneCollecltion { + + private class Canvas extends JComponent{ + + public PlanesGenericCollection _planes; + + public Canvas(){} + + public void paintComponent (Graphics g){ + super.paintComponent(g); + if (_planes.ShowTrains() != null) { + g.drawImage(_planes.ShowTrains(), 0, 0, this); + } + super.repaint(); + } + } + + Canvas canv; + + static int pictureBoxWidth = 800; + static int pictureBoxHeight = 580; + + private PlanesGenericCollection _planes; + + public void Draw(){ + canv.repaint(); + } + + FormPlaneCollecltion() { + canv = new Canvas(); + JFrame w = new JFrame("PlaneCollecltion"); + _planes = new PlanesGenericCollection(pictureBoxWidth, pictureBoxHeight); + canv._planes = _planes; + + JButton ButtonAddPlane = new JButton("ButtonAddPlane"); + ButtonAddPlane.addActionListener( + new ActionListener() { + public void actionPerformed(ActionEvent e) { + FormHydroplane form = new FormHydroplane(); + form.buttonSelectPlane.addActionListener( + new ActionListener() { + public void actionPerformed(ActionEvent e) { + if (_planes.Add(form._drawingPlane) != -1) { + JOptionPane.showMessageDialog(null, "Объект добавлен", "Информация", JOptionPane.INFORMATION_MESSAGE); + Draw(); + } else { + JOptionPane.showMessageDialog(null, "Не удалось добавить объект", "Информация", JOptionPane.INFORMATION_MESSAGE); + } + form.w.dispose(); + } + } + ); + } + } + ); + + JTextField TextBoxNumber = new JTextField(); + JButton ButtonRemovePlane = new JButton("ButtonRemovePlane"); + ButtonRemovePlane.addActionListener( + new ActionListener() { + public void actionPerformed(ActionEvent e) { + if (JOptionPane.showConfirmDialog(null, "Удалить объект?", "Удаление", JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION) { + return; + } + for (char it : TextBoxNumber.getText().toCharArray()) + if (it < '0' || it > '9') { + JOptionPane.showMessageDialog(null, "Не удалось удалить объект", "Информация", JOptionPane.INFORMATION_MESSAGE); + return; + } + if (TextBoxNumber.getText().length() == 0) { + JOptionPane.showMessageDialog(null, "Не удалось удалить объект", "Информация", JOptionPane.INFORMATION_MESSAGE); + return; + } + + int pos = Integer.parseInt(TextBoxNumber.getText()); + if (_planes.remove(pos) != null) { + JOptionPane.showMessageDialog(null, "Объект удален", "Информация", JOptionPane.INFORMATION_MESSAGE); + Draw(); + } else { + JOptionPane.showMessageDialog(null, "Не удалось удалить объект", "Информация", JOptionPane.INFORMATION_MESSAGE); + } + } + } + ); + + JButton ButtonRefreshCollection = new JButton("ButtonRefreshCollection"); + ButtonRefreshCollection.addActionListener( + new ActionListener() { + public void actionPerformed(ActionEvent e) { + Draw(); + } + } + ); + + JButton toForm4GenericDopClass = new JButton("ToForm4GenericDopClass"); + toForm4GenericDopClass.addActionListener( + new ActionListener() { + public void actionPerformed(ActionEvent e) { + Form4GenericDopClass form4GenericDopClass = new Form4GenericDopClass(); + } + } + ); + + w.setSize(1000, 600); + w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + w.setLayout(null); + canv.setBounds(0, 0, pictureBoxWidth, pictureBoxHeight); + ButtonAddPlane.setBounds(pictureBoxWidth, 0, 150, 20); + TextBoxNumber.setBounds(pictureBoxWidth, 30, 150, 20); + ButtonRemovePlane.setBounds(pictureBoxWidth, 60, 150, 20); + ButtonRefreshCollection.setBounds(pictureBoxWidth, 90, 150, 20); + toForm4GenericDopClass.setBounds(pictureBoxWidth, 120, 150, 20); + w.add(canv); + w.add(ButtonAddPlane); + w.add(ButtonRemovePlane); + w.add(ButtonRefreshCollection); + w.add(TextBoxNumber); + w.add(toForm4GenericDopClass); + w.setVisible(true); + } +} + diff --git a/Hydroplane/src/main/java/org/example/GenericDopClass.java b/Hydroplane/src/main/java/org/example/GenericDopClass.java new file mode 100644 index 0000000..ca60588 --- /dev/null +++ b/Hydroplane/src/main/java/org/example/GenericDopClass.java @@ -0,0 +1,63 @@ +package org.example; + +import java.util.ArrayList; +import java.util.Random; + +public class GenericDopClass { + + private ArrayList Planes; + private ArrayList Windows; + private int maxCountPlanes; + private int countPlanes; + private int maxCountWindows; + private int countWindows; + private Random random; + + private int _pictureWidth; + private int _pictureHeight; + + public GenericDopClass(int _maxCountPlanes, int _maxCountWindows, int pictureWidth, int pictureHeight){ + maxCountPlanes = _maxCountPlanes; + maxCountWindows = _maxCountWindows; + Planes = new ArrayList(maxCountPlanes); + Windows = new ArrayList(maxCountWindows); + countPlanes = 0; + countWindows = 0; + _pictureWidth = pictureWidth; + _pictureHeight = pictureHeight; + random = new Random(); + } + + public boolean addPlane(T plane){ + if (plane == null) + return false; + if (countPlanes > maxCountPlanes) + return false; + Planes.add(countPlanes++, plane); + return true; + } + + public boolean addWindow(U window){ + if (window == null) + return false; + if (countWindows > maxCountWindows) + return false; + Windows.add(countWindows++, window); + return true; + } + + public DrawingPlane getRDrawingObject(){ + if (countPlanes == 0 || countWindows == 0) + return null; + int i = random.nextInt(countPlanes); + int j = random.nextInt(countWindows); + DrawingPlane drawingPlane; + if (Planes.get(i) instanceof EntityHydroplane){ + drawingPlane = new DrawingHydroplane((EntityHydroplane)Planes.get(i), Windows.get(j), _pictureWidth, _pictureHeight); + } + else{ + drawingPlane = new DrawingPlane(Planes.get(i), Windows.get(j), _pictureWidth, _pictureHeight); + } + return drawingPlane; + } +} diff --git a/Hydroplane/src/main/java/org/example/Main.java b/Hydroplane/src/main/java/org/example/Main.java index 9484c07..ea80c55 100644 --- a/Hydroplane/src/main/java/org/example/Main.java +++ b/Hydroplane/src/main/java/org/example/Main.java @@ -2,6 +2,6 @@ package org.example; public class Main{ public static void main(String[] args) { - FormHydroplane formHydroplane = new FormHydroplane(); + FormPlaneCollecltion formPlaneCollecltion = new FormPlaneCollecltion(); } } \ No newline at end of file diff --git a/Hydroplane/src/main/java/org/example/PlanesGenericCollection.java b/Hydroplane/src/main/java/org/example/PlanesGenericCollection.java new file mode 100644 index 0000000..42b2400 --- /dev/null +++ b/Hydroplane/src/main/java/org/example/PlanesGenericCollection.java @@ -0,0 +1,91 @@ +package org.example; + +import java.awt.Color; +import java.awt.Graphics2D; +import java.awt.image.BufferedImage; + +public class PlanesGenericCollection { + + private int _pictureWidth; + + private int _pictureHeight; + + private int _placeSizeWidth = 175; + + private int _placeSizeHeight = 85; + + 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); + } + + public int Add(T obj) + { + if (obj == null) + { + return -1; + } + return _collection.Insert(obj); + } + + public T remove(int pos) + { + T obj = _collection.Get(pos); + if (obj != null) + { + _collection.Remove(pos); + } + return obj; + } + + public U GetU(int pos) + { + return (U)_collection.Get(pos).GetMoveableObject(); + } + + public BufferedImage ShowTrains() + { + BufferedImage bmp = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_ARGB); + Graphics2D g = bmp.createGraphics(); + DrawBackground(g); + DrawObjects(g); + g.dispose(); + return bmp; + } + + private void DrawBackground(Graphics2D g) + { + g.setColor(Color.BLACK); + + for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++) + { + for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j) + {//линия рамзетки места + g.drawLine( i * _placeSizeWidth, j * _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight); + } + g.drawLine( i * _placeSizeWidth, 0, i * _placeSizeWidth, _pictureHeight / _placeSizeHeight * _placeSizeHeight); + } + } + + private void DrawObjects(Graphics2D g) + { + for (int i = 0; i < _collection.Count; i++) + { + T t = _collection.Get(i); + if (t != null) + { + t.SetPosition((i % (_pictureWidth / _placeSizeWidth)) * _placeSizeWidth, (i / (_pictureWidth / _placeSizeWidth)) * _placeSizeHeight); + if (t instanceof DrawingHydroplane) + ((DrawingHydroplane) t).DrawTransport(g); + else + t.DrawTransport(g); + } + } + } +} diff --git a/Hydroplane/src/main/java/org/example/SetGeneric.java b/Hydroplane/src/main/java/org/example/SetGeneric.java new file mode 100644 index 0000000..4ed3002 --- /dev/null +++ b/Hydroplane/src/main/java/org/example/SetGeneric.java @@ -0,0 +1,57 @@ +package org.example; + +public class SetGeneric { + + private Object[] _places; + + public int Count; + + public SetGeneric(int count) + { + _places = new Object[count]; + Count = _places.length; + } + + public int Insert(T plane) + { + if (_places[Count - 1] != null) + return -1; + return Insert(plane, 0); + } + + public int Insert(T plane, int position) + { + if (position < 0 || position >= Count) + return -1; + if (_places[position] != null) + { + int indexEnd = position + 1; + while (_places[indexEnd] != null) + { + indexEnd++; + } + for (int i = indexEnd + 1; i > position; i--) + { + _places[i] = _places[i - 1]; + } + + } + _places[position] = plane; + return position; + } + + public boolean Remove(int position) + { + if (position < 0 || position >= _places.length) + return false; + _places[position] = null; + return true; + } + + public T Get(int position) + { + if (position < 0 || position >= _places.length) + return null; + return (T)_places[position]; + } +}