lab4 maybe done

This commit is contained in:
Галина Федоренко 2023-11-22 18:42:51 +04:00
parent 2b82b9c861
commit 083c28bd97
5 changed files with 306 additions and 65 deletions

View File

@ -21,6 +21,13 @@ public class FormHydroplane{
canv.repaint();
}
public void ChangePlane(DrawingPlane newPlane){
newPlane.SetPosition(0,0);
_drawingPlane = newPlane;
canv._drawingPlane = _drawingPlane;
}
public FormHydroplane(){
w=new JFrame ("Hydroplane");
JButton buttonCreatePlane = new JButton("createPlane");

View File

@ -4,25 +4,38 @@ import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;
import java.util.Stack;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
public class FormPlaneCollecltion {
private class Canvas extends JComponent{
public PlanesGenericCollection<DrawingPlane, DrawningObjectPlane> _planes;
public Canvas(){}
public Canvas(){
}
public void paintComponent (Graphics g){
super.paintComponent(g);
if (_planes.ShowPlanes() != null) {
g.drawImage(_planes.ShowPlanes(), 0, 0, this);
if (jListStorage.getSelectedIndex() == -1)
{
return;
}
var obj = _storage.get(jListStorage.getSelectedValue());
if (obj == null)
{
return;
}
if (obj.ShowPlanes() != null) {
g.drawImage(obj.ShowPlanes(), 0, 0, this);
}
super.repaint();
}
@ -33,31 +46,68 @@ public class FormPlaneCollecltion {
static int pictureBoxWidth = 800;
static int pictureBoxHeight = 580;
private PlanesGenericCollection<DrawingPlane, DrawningObjectPlane> _planes;
private PlanesGenericStorage _storage;
public void Draw(){
canv.repaint();
}
private Stack<DrawingPlane> stackRemoved;
private JList<String> jListStorage;
private DefaultListModel<String> listModel;
private void ReloadObjects()
{
int index = jListStorage.getSelectedIndex();
listModel.clear();
for (String key : _storage.Keys()) {
listModel.addElement(key);
}
if (listModel.size() > 0 && (index == -1 || index >= listModel.size()))
{
jListStorage.setSelectedIndex(0);
}
else if (listModel.size() > 0 && index > -1 && index < listModel.size())
{
jListStorage.setSelectedIndex(index);
}
}
FormPlaneCollecltion() {
listModel = new DefaultListModel<String>();
jListStorage = new JList<String>(listModel);
canv = new Canvas();
JFrame w = new JFrame("PlaneCollecltion");
_planes = new PlanesGenericCollection<DrawingPlane, DrawningObjectPlane>(pictureBoxWidth, pictureBoxHeight);
canv._planes = _planes;
_storage = new PlanesGenericStorage(pictureBoxWidth, pictureBoxHeight);
JButton ButtonAddPlane = new JButton("ButtonAddPlane");
JButton ButtonAddPlane = new JButton("AddPlane");
ButtonAddPlane.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (jListStorage.getSelectedIndex() == -1)
{
return;
}
var obj = _storage.get(jListStorage.getSelectedValue());
if (obj == null)
{
return;
}
FormHydroplane form = new FormHydroplane();
form.buttonSelectPlane.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (_planes.Add(form._drawingPlane) != -1) {
if (obj.Add(form._drawingPlane) != -1)
{
JOptionPane.showMessageDialog(null, "Объект добавлен", "Информация", JOptionPane.INFORMATION_MESSAGE);
System.out.println("Объект добавлен");
Draw();
} else {
}
else
{
JOptionPane.showMessageDialog(null, "Не удалось добавить объект", "Информация", JOptionPane.INFORMATION_MESSAGE);
System.out.println("Не удалось добавить объект");
}
form.w.dispose();
}
@ -67,39 +117,79 @@ public class FormPlaneCollecltion {
}
);
stackRemoved = new Stack<DrawingPlane>();
JTextField TextBoxNumber = new JTextField();
JButton ButtonRemovePlane = new JButton("ButtonRemovePlane");
JButton ButtonRemovePlane = new JButton("RemovePlane");
ButtonRemovePlane.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (JOptionPane.showConfirmDialog(null, "Удалить объект?", "Удаление", JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION) {
public void actionPerformed(ActionEvent e){
if (jListStorage.getSelectedIndex() == -1)
{
return;
}
var obj = _storage.get(jListStorage.getSelectedValue());
if (obj == null)
{
return;
}
if (JOptionPane.showConfirmDialog(null, "Удалить объект?", "Удаление", JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION)
{
return;
}
for (char it : TextBoxNumber.getText().toCharArray())
if (it < '0' || it > '9') {
if (it < '0' || it > '9')
{
JOptionPane.showMessageDialog(null, "Не удалось удалить объект", "Информация", JOptionPane.INFORMATION_MESSAGE);
System.out.println("Не удалось удалить объект");
return;
}
if (TextBoxNumber.getText().length() == 0) {
if (TextBoxNumber.getText().length() == 0)
{
JOptionPane.showMessageDialog(null, "Не удалось удалить объект", "Информация", JOptionPane.INFORMATION_MESSAGE);
System.out.println("Не удалось удалить объект");
return;
}
int pos = Integer.parseInt(TextBoxNumber.getText());
if (_planes.remove(pos) != null) {
var removed = obj.remove(pos);
if (removed != null)
{
stackRemoved.add(removed);
JOptionPane.showMessageDialog(null, "Объект удален", "Информация", JOptionPane.INFORMATION_MESSAGE);
System.out.println("Объект удален");
Draw();
} else {
}
else
{
JOptionPane.showMessageDialog(null, "Не удалось удалить объект", "Информация", JOptionPane.INFORMATION_MESSAGE);
System.out.println("Не удалось удалить объект");
}
}
}
);
JButton ButtonRefreshCollection = new JButton("ButtonRefreshCollection");
JButton buttonGetRemoved = new JButton("buttonGetRemoved");
buttonGetRemoved.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e){
if (stackRemoved.size()==0){
JOptionPane.showMessageDialog(null, "Нет удалённых", "Информация", JOptionPane.INFORMATION_MESSAGE);
System.out.println("Нет удалённых");
return;
}
System.out.println("Удалённый объект");
FormHydroplane form = new FormHydroplane();
form.ChangePlane(stackRemoved.pop());
form.Draw();
}
}
);
JButton ButtonRefreshCollection = new JButton("Refresh Collection");
ButtonRefreshCollection.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
public void actionPerformed(ActionEvent e){
Draw();
}
}
@ -108,27 +198,86 @@ public class FormPlaneCollecltion {
JButton toForm4GenericDopClass = new JButton("ToForm4GenericDopClass");
toForm4GenericDopClass.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
Form4GenericDopClass form4GenericDopClass = new Form4GenericDopClass();
public void actionPerformed(ActionEvent e){
Form4GenericDopClass form4GenericDopClass = new Form4GenericDopClass();
}
}
);
w.setSize(1000, 600);
w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextField textBoxSetName = new JTextField();
JButton buttonAddSet = new JButton("Add Set");
buttonAddSet.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e){
if (textBoxSetName.getText().length() == 0)
{
JOptionPane.showMessageDialog(null, "Не все данные заполнены", "Информация", JOptionPane.INFORMATION_MESSAGE);
System.out.println("Не все данные заполнены");
return;
}
_storage.AddSet(textBoxSetName.getText());
ReloadObjects();
}
}
);
jListStorage.addListSelectionListener(
new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e){
Draw();
}
}
);
JButton buttonRemoveSet = new JButton("Remove Set");
buttonRemoveSet.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e){
if (jListStorage.getSelectedIndex() == -1)
{
return;
}
if (JOptionPane.showConfirmDialog(null, "Удалить объект " + jListStorage.getSelectedValue() + "?", "Удаление", JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION)
{
return;
}
_storage.DelSet(jListStorage.getSelectedValue());
ReloadObjects();
}
}
);
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);
ButtonAddPlane.setBounds(pictureBoxWidth, 0, 160, 20);
TextBoxNumber.setBounds(pictureBoxWidth, 30, 160, 20);
ButtonRemovePlane.setBounds(pictureBoxWidth, 60, 160, 20);
ButtonRefreshCollection.setBounds(pictureBoxWidth, 90, 160, 20);
toForm4GenericDopClass.setBounds(pictureBoxWidth, 120, 160, 20);
buttonAddSet.setBounds(pictureBoxWidth, 150, 160, 20);
textBoxSetName.setBounds(pictureBoxWidth, 180, 160, 20);
jListStorage.setBounds(pictureBoxWidth, 210, 160, 80);
buttonRemoveSet.setBounds(pictureBoxWidth, 300, 160, 20);
buttonGetRemoved.setBounds(pictureBoxWidth, 330, 160, 20);
w.add(canv);
w.add(ButtonAddPlane);
w.add(ButtonRemovePlane);
w.add(ButtonRefreshCollection);
w.add(TextBoxNumber);
w.add(toForm4GenericDopClass);
w.add(buttonAddSet);
w.add(textBoxSetName);
w.add(jListStorage);
w.add(buttonRemoveSet);
w.add(buttonGetRemoved);
w.setVisible(true);
}
}

View File

@ -75,17 +75,18 @@ public class PlanesGenericCollection<T extends DrawingPlane, U extends IMoveable
private void DrawObjects(Graphics2D g)
{
for (int i = 0; i < _collection.Count; i++)
int i = 0;
for (T plane : _collection.GetPlanes(100))
{
T t = _collection.Get(i);
if (t != null)
if (plane != null)
{
t.SetPosition((i % (_pictureWidth / _placeSizeWidth)) * _placeSizeWidth, (i / (_pictureWidth / _placeSizeWidth)) * _placeSizeHeight);
if (t instanceof DrawingHydroplane)
((DrawingHydroplane) t).DrawTransport(g);
plane.SetPosition((i % (_pictureWidth / _placeSizeWidth)) * _placeSizeWidth, (i / (_pictureWidth / _placeSizeWidth)) * _placeSizeHeight);
if (plane instanceof DrawingHydroplane)
((DrawingHydroplane)plane).DrawTransport(g);
else
t.DrawTransport(g);
plane.DrawTransport(g);
}
i++;
}
}
}

View File

@ -0,0 +1,52 @@
package org.example;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;
public class PlanesGenericStorage {
HashMap<String, PlanesGenericCollection<DrawingPlane, DrawningObjectPlane>> _planeStorages;
public List<String> Keys(){
return _planeStorages.keySet().stream().collect(Collectors.toList());
}
private int _pictureWidth;
private int _pictureHeight;
public PlanesGenericStorage(int pictureWidth, int pictureHeight)
{
_planeStorages = new HashMap<String, PlanesGenericCollection<DrawingPlane, DrawningObjectPlane>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
public void AddSet(String name)
{
if (_planeStorages.containsKey(name))
return;
_planeStorages.put(name, new PlanesGenericCollection<DrawingPlane, DrawningObjectPlane>(_pictureWidth, _pictureHeight));
}
public void DelSet(String name)
{
if (!_planeStorages.containsKey(name))
return;
_planeStorages.remove(name);
}
public PlanesGenericCollection<DrawingPlane, DrawningObjectPlane> get(String ind)
{
if (_planeStorages.containsKey(ind))
return _planeStorages.get(ind);
return null;
}
public DrawningObjectPlane get(String ind1, int ind2){
if (!_planeStorages.containsKey(ind1))
return null;
return _planeStorages.get(ind1).GetU(ind2);
}
}

View File

@ -1,57 +1,89 @@
package org.example;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.NoSuchElementException;
public class SetGeneric<T extends Object> {
private Object[] _places;
private ArrayList<T>_places;
public int Count;
public int Count () { return _places.size();};
private int _maxCount;
public SetGeneric(int count)
{
_places = new Object[count];
Count = _places.length;
_maxCount = count;
_places = new ArrayList<T>(count);
}
public int Insert(T plane)
{
if (_places[Count - 1] != null)
if (_places.size() >= _maxCount)
return -1;
return Insert(plane, 0);
_places.add(0, plane);
return 0;
}
public int Insert(T plane, int position)
public boolean 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];
}
if (_places.size() >= _maxCount)
return false;
}
_places[position] = plane;
return position;
if (position < 0 || position > _places.size())
return false;
if (position == _places.size())
_places.add(plane);
else
_places.add(position, plane);
return true;
}
public boolean Remove(int position)
{
if (position < 0 || position >= _places.length)
if (position < 0 || position >= _places.size())
return false;
_places[position] = null;
_places.remove(position);
return true;
}
public T Get(int position)
{
if (position < 0 || position >= _places.length)
if (position < 0 || position >= _places.size())
return null;
return (T)_places[position];
return _places.get(position);
}
public Iterable<T> GetPlanes(final Integer maxPlanes) {
return new Iterable<T>() {
@Override
public Iterator<T> iterator() {
return new Iterator<T>() {
private int currentIndex = 0;
private int count = 0;
@Override
public boolean hasNext() {
return currentIndex < _places.size() && (maxPlanes == null || count < maxPlanes);
}
@Override
public T next() {
if (hasNext()) {
count++;
return _places.get(currentIndex++);
}
throw new NoSuchElementException();
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
};
}
};
}
}