Готовая 4 лабораторная

This commit is contained in:
russell 2023-12-01 18:07:18 +04:00
parent fd07441ba6
commit 230f260f8b
6 changed files with 232 additions and 58 deletions

View File

@ -1,14 +1,16 @@
package DumpTruck; package DumpTruck;
import DumpTruck.DrawingObjects.DrawingTruck;
import javax.swing.*; import javax.swing.*;
public class FrameDumpTruck extends JFrame { public class FrameDumpTruck extends JFrame {
public PictureBoxDumpTruck pictureBoxDumpTruck; public PictureBoxDumpTruck pictureBoxDumpTruck;
public FrameDumpTruck() { public FrameDumpTruck(DrawingTruck truck) {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pictureBoxDumpTruck = new PictureBoxDumpTruck(); pictureBoxDumpTruck = new PictureBoxDumpTruck(truck);
add(pictureBoxDumpTruck); add(pictureBoxDumpTruck);
pack(); pack();
setLocationRelativeTo(null); setLocationRelativeTo(null);

View File

@ -1,12 +1,16 @@
package DumpTruck.Generics; package DumpTruck.Generics;
public class SetGeneric<T extends Object> { import java.util.ArrayList;
private Object[] _places; import java.util.Iterator;
import java.util.NoSuchElementException;
public int Count() {return _places.length;} public class SetGeneric<T extends Object> {
public final ArrayList<T> _places;
private final int _maxCount;
public SetGeneric(int count) { public SetGeneric(int count) {
_places = new Object[count]; _maxCount = count;
_places = new ArrayList<>();
} }
public int Insert(T truck) { public int Insert(T truck) {
@ -14,41 +18,53 @@ public class SetGeneric<T extends Object> {
} }
public int Insert(T truck, int position) { public int Insert(T truck, int position) {
if (position < 0 || position >= Count()) if (position < 0 || position >= _maxCount)
return -1; return -1;
if (_places[position] == null) _places.add(position, truck);
{
_places[position] = truck;
return position;
}
int index = -1;
for (int i = position; i < Count(); i++)
{
if (_places[i] == null)
{
index = i;
break;
}
}
if (index < 0) return -1;
for (int i = index; i > position; i--)
{
_places[i] = _places[i - 1];
}
_places[position] = truck;
return position; return position;
} }
public boolean Remove(int position) { public boolean Remove(int position) {
if (position < 0 || position >= Count()) if (position < 0 || position >= _places.size())
return false; return false;
_places[position] = null; _places.remove(position);
return true; return true;
} }
public T Get(int position) { public T Get(int position) {
if (position < 0 || position >= Count()) if (position < 0 || position >= _places.size())
return null; return null;
return (T) _places[position]; return _places.get(position);
}
public Iterable<T> GetTrucks(final Integer maxTrucks) {
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() && (maxTrucks == null || count < maxTrucks);
}
@Override
public T next() {
if (hasNext()) {
count++;
return _places.get(currentIndex++);
}
throw new NoSuchElementException();
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
};
}
};
} }
} }

View File

@ -27,12 +27,12 @@ public class TrucksGenericCollection<T extends DrawingTruck, U extends IMoveable
} }
return _collection.Insert(obj); return _collection.Insert(obj);
} }
public boolean remove(int pos) { public T remove(int pos) {
T obj = _collection.Get(pos); T obj = _collection.Get(pos);
if (obj != null) { if (obj != null) {
_collection.Remove(pos); _collection.Remove(pos);
} }
return false; return obj;
} }
public U GetU(int pos) { public U GetU(int pos) {
return (U) _collection.Get(pos).GetMoveableObject(); return (U) _collection.Get(pos).GetMoveableObject();
@ -57,14 +57,14 @@ public class TrucksGenericCollection<T extends DrawingTruck, U extends IMoveable
} }
} }
private void DrawObjects(Graphics g) { private void DrawObjects(Graphics g) {
for (int i = 0; i < _collection.Count(); i++) int index = 0;
for (T truck : _collection.GetTrucks(100)) {
if (truck != null)
{ {
T obj = _collection.Get(i); truck.SetPosition(index % (_pictureWidth / _placeSizeWidth) * _placeSizeWidth, index / (_pictureWidth / _placeSizeWidth) * _placeSizeHeight);
if (obj != null) truck.DrawTransport((Graphics2D) g);
{ }
obj.SetPosition(i % (_pictureWidth / _placeSizeWidth) * _placeSizeWidth, i / (_pictureWidth / _placeSizeWidth) * _placeSizeHeight); index++;
obj.DrawTransport((Graphics2D) g);
}
} }
} }
} }

View File

@ -0,0 +1,48 @@
package DumpTruck.Generics;
import DumpTruck.DrawingObjects.*;
import DumpTruck.MovementStrategy.*;
import java.util.ArrayList;
import java.util.HashMap;
public class TrucksGenericStorage {
final HashMap<String, TrucksGenericCollection<DrawingTruck, DrawingObjectTruck>> _truckStorages;
public ArrayList<String> Keys()
{
return new ArrayList<>(_truckStorages.keySet());
}
private final int _pictureWidth;
private final int _pictureHeight;
public TrucksGenericStorage(int pictureWidth, int pictureHeight)
{
_pictureHeight = pictureHeight;
_pictureWidth = pictureWidth;
_truckStorages = new HashMap<>();
}
public void AddSet(String name)
{
if (Keys().contains(name))
return;
_truckStorages.put(name, new TrucksGenericCollection<>(_pictureWidth, _pictureHeight));
}
public void DelSet(String name)
{
if (!Keys().contains(name))
return;
_truckStorages.remove(name);
}
public TrucksGenericCollection<DrawingTruck, DrawingObjectTruck> get(String ind)
{
if (Keys().contains(ind))
return _truckStorages.get(ind);
return null;
}
public DrawingObjectTruck get(String name, int ind)
{
if (!Keys().contains(name))
return null;
return _truckStorages.get(name).GetU(ind);
}
}

View File

@ -4,30 +4,104 @@ import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.util.LinkedList;
import java.util.Random; import java.util.Random;
import DumpTruck.DrawingObjects.*; import DumpTruck.DrawingObjects.*;
import DumpTruck.Generics.*; import DumpTruck.Generics.*;
import DumpTruck.MovementStrategy.*; import DumpTruck.MovementStrategy.*;
public class PictureBoxCollection extends JPanel { public class PictureBoxCollection extends JPanel {
public TrucksGenericCollection<DrawingTruck, DrawingObjectTruck> _trucks; private TrucksGenericStorage _storage;
private LinkedList<DrawingTruck> removedTrucks;
private JLabel labelTools; private JLabel labelTools;
private JButton buttonAddTruck, buttonDeleteTruck, buttonRefreshCollection, buttonShowDop; private JButton buttonAddTruck, buttonDeleteTruck, buttonRefreshCollection, buttonShowDop, buttonAddCollection, buttonDeleteCollection, buttonShowDeleted;
private JTextField textFieldNumber; private JTextField textFieldNumber, textFieldCollectionNumber;
private JList<String> listStorage;
private DefaultListModel<String> listModel;
protected void ReloadCollections()
{
int index = listStorage.getSelectedIndex();
listModel.clear();
for (String key : _storage.Keys())
{
listModel.addElement(key);
}
if (!listModel.isEmpty() && (index == -1 || index >= listModel.size()))
{
listStorage.setSelectedIndex(0);
}
else if (!listModel.isEmpty() && index > -1 && index < listModel.size())
{
listStorage.setSelectedIndex(index);
}
}
public PictureBoxCollection() { public PictureBoxCollection() {
removedTrucks = new LinkedList<>();
setLayout(null); setLayout(null);
setBounds(0, 0, 800, 450); setBounds(0, 0, 800, 750);
_trucks = new TrucksGenericCollection<>(this.getWidth() - 200, this.getHeight()); labelTools = new JLabel("Наборы");
labelTools = new JLabel("Инструменты");
labelTools.setBounds(660, 10, 150, 30); labelTools.setBounds(660, 10, 150, 30);
add(labelTools); add(labelTools);
textFieldCollectionNumber = new JTextField();
textFieldCollectionNumber.setBounds(620, 50, 150, 30);
add(textFieldCollectionNumber);
buttonAddCollection = new JButton("Добавить набор");
buttonAddCollection.setFocusable(false);
buttonAddCollection.setBounds(620, 100, 150, 30);
buttonAddCollection.addActionListener(e -> {
String name = textFieldCollectionNumber.getText();
if (name.length() == 0)
return;
_storage.AddSet(name);
ReloadCollections();
});
add(buttonAddCollection);
listModel = new DefaultListModel<>();
listStorage = new JList<String>(listModel);
listStorage.setLayout(null);
listStorage.setBounds(620, 150, 150, 100);
add(listStorage);
listStorage.addListSelectionListener(e -> {
repaint();
});
buttonDeleteCollection = new JButton("Удалить набор");
buttonDeleteCollection.setFocusable(false);
buttonDeleteCollection.setBounds(620, 270, 150, 30);
buttonDeleteCollection.addActionListener(e -> {
if (listStorage.getSelectedIndex() == -1)
{
return;
}
if (JOptionPane.showConfirmDialog(null, "Delete object " + listStorage.getSelectedValue() + "?", "Delete", JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION)
{
return;
}
_storage.DelSet(listStorage.getSelectedValue());
ReloadCollections();
});
add(buttonDeleteCollection);
_storage = new TrucksGenericStorage(this.getWidth() - 200, this.getHeight());
labelTools = new JLabel("Инструменты");
labelTools.setBounds(660, 300, 150, 30);
add(labelTools);
buttonAddTruck = new JButton("Добавить грузовик"); buttonAddTruck = new JButton("Добавить грузовик");
buttonAddTruck.setFocusable(false); buttonAddTruck.setFocusable(false);
buttonAddTruck.setBounds(620, 50, 150, 30); buttonAddTruck.setBounds(620, 350, 150, 30);
buttonAddTruck.addActionListener(e -> { buttonAddTruck.addActionListener(e -> {
FrameDumpTruck frameDumpTruck = new FrameDumpTruck(); if (listStorage.getSelectedIndex() == -1)
return;
var obj = _storage.get(listStorage.getSelectedValue());
if (obj == null)
return;
FrameDumpTruck frameDumpTruck = new FrameDumpTruck(null);
frameDumpTruck.pictureBoxDumpTruck.buttonSelectTruck.addActionListener(e1 -> { frameDumpTruck.pictureBoxDumpTruck.buttonSelectTruck.addActionListener(e1 -> {
if (_trucks.Add(frameDumpTruck.pictureBoxDumpTruck.drawingTruck) != -1) { if (obj.Add(frameDumpTruck.pictureBoxDumpTruck.drawingTruck) != -1) {
JOptionPane.showMessageDialog(null, "Объект добавлен", "Информация", JOptionPane.INFORMATION_MESSAGE); JOptionPane.showMessageDialog(null, "Объект добавлен", "Информация", JOptionPane.INFORMATION_MESSAGE);
} else { } else {
JOptionPane.showMessageDialog(null, "Не удалось добавить объект", "Информация", JOptionPane.INFORMATION_MESSAGE); JOptionPane.showMessageDialog(null, "Не удалось добавить объект", "Информация", JOptionPane.INFORMATION_MESSAGE);
@ -40,13 +114,18 @@ public class PictureBoxCollection extends JPanel {
add(buttonAddTruck); add(buttonAddTruck);
textFieldNumber = new JTextField(); textFieldNumber = new JTextField();
textFieldNumber.setBounds(620, 100, 150, 30); textFieldNumber.setBounds(620, 400, 150, 30);
add(textFieldNumber); add(textFieldNumber);
buttonDeleteTruck = new JButton("Удалить грузовик"); buttonDeleteTruck = new JButton("Удалить грузовик");
buttonDeleteTruck.setFocusable(false); buttonDeleteTruck.setFocusable(false);
buttonDeleteTruck.setBounds(620, 150, 150, 30); buttonDeleteTruck.setBounds(620, 450, 150, 30);
buttonDeleteTruck.addActionListener(e -> { buttonDeleteTruck.addActionListener(e -> {
if (listStorage.getSelectedIndex() == -1)
return;
var obj = _storage.get(listStorage.getSelectedValue());
if (obj == null)
return;
if (JOptionPane.showConfirmDialog(null, "Удалить объект?", "Удаление", JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION) { if (JOptionPane.showConfirmDialog(null, "Удалить объект?", "Удаление", JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION) {
return; return;
} }
@ -61,8 +140,11 @@ public class PictureBoxCollection extends JPanel {
} }
int pos = Integer.parseInt(textFieldNumber.getText()); int pos = Integer.parseInt(textFieldNumber.getText());
if (_trucks.remove(pos)) { DrawingTruck removed = null;
removed = obj.remove(pos);
if (removed != null) {
JOptionPane.showMessageDialog(null, "Объект удален", "Информация", JOptionPane.INFORMATION_MESSAGE); JOptionPane.showMessageDialog(null, "Объект удален", "Информация", JOptionPane.INFORMATION_MESSAGE);
removedTrucks.add(removed);
} else { } else {
JOptionPane.showMessageDialog(null, "Не удалось удалить объект", "Информация", JOptionPane.INFORMATION_MESSAGE); JOptionPane.showMessageDialog(null, "Не удалось удалить объект", "Информация", JOptionPane.INFORMATION_MESSAGE);
} }
@ -72,23 +154,45 @@ public class PictureBoxCollection extends JPanel {
buttonRefreshCollection = new JButton("Обновить коллекцию"); buttonRefreshCollection = new JButton("Обновить коллекцию");
buttonRefreshCollection.setFocusable(false); buttonRefreshCollection.setFocusable(false);
buttonRefreshCollection.setBounds(620, 200, 150, 30); buttonRefreshCollection.setBounds(620, 500, 150, 30);
buttonRefreshCollection.addActionListener(e -> repaint()); buttonRefreshCollection.addActionListener(e -> repaint());
add(buttonRefreshCollection); add(buttonRefreshCollection);
buttonShowDeleted = new JButton("Удаленные");
buttonShowDeleted.setFocusable(false);
buttonShowDeleted.setBounds(620, 550, 150, 30);
buttonShowDeleted.addActionListener(e -> {
if (removedTrucks.size() == 0){
JOptionPane.showMessageDialog(null, "Коллекция пуста", "Информация", JOptionPane.INFORMATION_MESSAGE);
return;
}
var truck = removedTrucks.removeLast();
FrameDumpTruck frameDumpTruck = new FrameDumpTruck(truck);
frameDumpTruck.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
});
add(buttonShowDeleted);
buttonShowDop = new JButton("Показать доп"); buttonShowDop = new JButton("Показать доп");
buttonShowDop.setFocusable(false); buttonShowDop.setFocusable(false);
buttonShowDop.setBounds(620, 250, 150, 30); buttonShowDop.setBounds(620, 650, 150, 30);
buttonShowDop.addActionListener(e -> new FrameDop()); buttonShowDop.addActionListener(e -> new FrameDop());
add(buttonShowDop); add(buttonShowDop);
setPreferredSize(new Dimension(800, 450)); setPreferredSize(new Dimension(800, 750));
} }
@Override @Override
protected void paintComponent(Graphics g) { protected void paintComponent(Graphics g) {
super.paintComponent(g); super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g; Graphics2D g2d = (Graphics2D) g;
g2d.drawImage(_trucks.ShowTrucks(), 0, 0, null); if (listStorage.getSelectedIndex() == -1)
return;
var obj = _storage.get(listStorage.getSelectedValue());
if (obj == null)
return;
if (obj.ShowTrucks() == null)
return;
g2d.drawImage(obj.ShowTrucks(), 0, 0, null);
} }
} }

View File

@ -26,16 +26,21 @@ public class PictureBoxDumpTruck extends JPanel {
private JButton buttonStep; private JButton buttonStep;
public JButton buttonSelectTruck; public JButton buttonSelectTruck;
public PictureBoxDumpTruck() { public PictureBoxDumpTruck(DrawingTruck truck) {
Random random = new Random();
setLayout(null); setLayout(null);
setBounds(0, 0, 800, 450); setBounds(0, 0, 800, 450);
buttonCreateTruck = new JButton("Создать грузовик"); buttonCreateTruck = new JButton("Создать грузовик");
buttonCreateTruck.setFocusable(false); buttonCreateTruck.setFocusable(false);
buttonCreateTruck.setBounds(12, 415, 150, 30); buttonCreateTruck.setBounds(12, 415, 150, 30);
add(buttonCreateTruck); add(buttonCreateTruck);
if (truck != null){
drawingTruck = truck;
drawingTruck.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100));
repaint();
}
buttonCreateTruck.addActionListener(e -> { buttonCreateTruck.addActionListener(e -> {
Random random = new Random();
Color bodyColor = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)); Color bodyColor = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256));
Color selectedColor = JColorChooser.showDialog(this, "Выберите цвет", Color.WHITE); Color selectedColor = JColorChooser.showDialog(this, "Выберите цвет", Color.WHITE);
if (selectedColor != null) if (selectedColor != null)
@ -56,7 +61,6 @@ public class PictureBoxDumpTruck extends JPanel {
add(buttonCreateDumpTruck); add(buttonCreateDumpTruck);
buttonCreateDumpTruck.addActionListener(e -> { buttonCreateDumpTruck.addActionListener(e -> {
Random random = new Random();
Color bodyColor = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)); Color bodyColor = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256));
Color selectedColor = JColorChooser.showDialog(this, "Выберите цвет", Color.WHITE); Color selectedColor = JColorChooser.showDialog(this, "Выберите цвет", Color.WHITE);
if (selectedColor != null) if (selectedColor != null)