PIbd-21. Anisin R.S. Lab work 04 Hard #4
@ -1,14 +1,16 @@
|
||||
package DumpTruck;
|
||||
|
||||
import DumpTruck.DrawingObjects.DrawingTruck;
|
||||
|
||||
import javax.swing.*;
|
||||
|
||||
public class FrameDumpTruck extends JFrame {
|
||||
|
||||
public PictureBoxDumpTruck pictureBoxDumpTruck;
|
||||
|
||||
public FrameDumpTruck() {
|
||||
public FrameDumpTruck(DrawingTruck truck) {
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
pictureBoxDumpTruck = new PictureBoxDumpTruck();
|
||||
pictureBoxDumpTruck = new PictureBoxDumpTruck(truck);
|
||||
add(pictureBoxDumpTruck);
|
||||
pack();
|
||||
setLocationRelativeTo(null);
|
||||
|
@ -1,12 +1,16 @@
|
||||
package DumpTruck.Generics;
|
||||
|
||||
public class SetGeneric<T extends Object> {
|
||||
private Object[] _places;
|
||||
import java.util.ArrayList;
|
||||
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) {
|
||||
_places = new Object[count];
|
||||
_maxCount = count;
|
||||
_places = new ArrayList<>();
|
||||
}
|
||||
|
||||
public int Insert(T truck) {
|
||||
@ -14,41 +18,53 @@ public class SetGeneric<T extends Object> {
|
||||
}
|
||||
|
||||
public int Insert(T truck, int position) {
|
||||
if (position < 0 || position >= Count())
|
||||
if (position < 0 || position >= _maxCount)
|
||||
return -1;
|
||||
if (_places[position] == null)
|
||||
{
|
||||
_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;
|
||||
_places.add(position, truck);
|
||||
return position;
|
||||
}
|
||||
|
||||
public boolean Remove(int position) {
|
||||
if (position < 0 || position >= Count())
|
||||
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 >= Count())
|
||||
if (position < 0 || position >= _places.size())
|
||||
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();
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
@ -27,12 +27,12 @@ public class TrucksGenericCollection<T extends DrawingTruck, U extends IMoveable
|
||||
}
|
||||
return _collection.Insert(obj);
|
||||
}
|
||||
public boolean remove(int pos) {
|
||||
public T remove(int pos) {
|
||||
T obj = _collection.Get(pos);
|
||||
if (obj != null) {
|
||||
_collection.Remove(pos);
|
||||
}
|
||||
return false;
|
||||
return obj;
|
||||
}
|
||||
public U GetU(int pos) {
|
||||
return (U) _collection.Get(pos).GetMoveableObject();
|
||||
@ -57,14 +57,14 @@ public class TrucksGenericCollection<T extends DrawingTruck, U extends IMoveable
|
||||
}
|
||||
}
|
||||
private void DrawObjects(Graphics g) {
|
||||
for (int i = 0; i < _collection.Count(); i++)
|
||||
{
|
||||
T obj = _collection.Get(i);
|
||||
if (obj != null)
|
||||
int index = 0;
|
||||
for (T truck : _collection.GetTrucks(100)) {
|
||||
if (truck != null)
|
||||
{
|
||||
obj.SetPosition(i % (_pictureWidth / _placeSizeWidth) * _placeSizeWidth, i / (_pictureWidth / _placeSizeWidth) * _placeSizeHeight);
|
||||
obj.DrawTransport((Graphics2D) g);
|
||||
truck.SetPosition(index % (_pictureWidth / _placeSizeWidth) * _placeSizeWidth, index / (_pictureWidth / _placeSizeWidth) * _placeSizeHeight);
|
||||
truck.DrawTransport((Graphics2D) g);
|
||||
}
|
||||
index++;
|
||||
}
|
||||
}
|
||||
}
|
48
src/DumpTruck/Generics/TrucksGenericStorage.java
Normal file
48
src/DumpTruck/Generics/TrucksGenericStorage.java
Normal 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);
|
||||
}
|
||||
}
|
@ -4,30 +4,104 @@ import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.util.LinkedList;
|
||||
import java.util.Random;
|
||||
import DumpTruck.DrawingObjects.*;
|
||||
import DumpTruck.Generics.*;
|
||||
import DumpTruck.MovementStrategy.*;
|
||||
|
||||
public class PictureBoxCollection extends JPanel {
|
||||
public TrucksGenericCollection<DrawingTruck, DrawingObjectTruck> _trucks;
|
||||
private TrucksGenericStorage _storage;
|
||||
private LinkedList<DrawingTruck> removedTrucks;
|
||||
private JLabel labelTools;
|
||||
private JButton buttonAddTruck, buttonDeleteTruck, buttonRefreshCollection, buttonShowDop;
|
||||
private JTextField textFieldNumber;
|
||||
private JButton buttonAddTruck, buttonDeleteTruck, buttonRefreshCollection, buttonShowDop, buttonAddCollection, buttonDeleteCollection, buttonShowDeleted;
|
||||
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() {
|
||||
removedTrucks = new LinkedList<>();
|
||||
setLayout(null);
|
||||
setBounds(0, 0, 800, 450);
|
||||
_trucks = new TrucksGenericCollection<>(this.getWidth() - 200, this.getHeight());
|
||||
labelTools = new JLabel("Инструменты");
|
||||
setBounds(0, 0, 800, 750);
|
||||
labelTools = new JLabel("Наборы");
|
||||
labelTools.setBounds(660, 10, 150, 30);
|
||||
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.setFocusable(false);
|
||||
buttonAddTruck.setBounds(620, 50, 150, 30);
|
||||
buttonAddTruck.setBounds(620, 350, 150, 30);
|
||||
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 -> {
|
||||
if (_trucks.Add(frameDumpTruck.pictureBoxDumpTruck.drawingTruck) != -1) {
|
||||
if (obj.Add(frameDumpTruck.pictureBoxDumpTruck.drawingTruck) != -1) {
|
||||
JOptionPane.showMessageDialog(null, "Объект добавлен", "Информация", JOptionPane.INFORMATION_MESSAGE);
|
||||
} else {
|
||||
JOptionPane.showMessageDialog(null, "Не удалось добавить объект", "Информация", JOptionPane.INFORMATION_MESSAGE);
|
||||
@ -40,13 +114,18 @@ public class PictureBoxCollection extends JPanel {
|
||||
add(buttonAddTruck);
|
||||
|
||||
textFieldNumber = new JTextField();
|
||||
textFieldNumber.setBounds(620, 100, 150, 30);
|
||||
textFieldNumber.setBounds(620, 400, 150, 30);
|
||||
add(textFieldNumber);
|
||||
|
||||
buttonDeleteTruck = new JButton("Удалить грузовик");
|
||||
buttonDeleteTruck.setFocusable(false);
|
||||
buttonDeleteTruck.setBounds(620, 150, 150, 30);
|
||||
buttonDeleteTruck.setBounds(620, 450, 150, 30);
|
||||
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) {
|
||||
return;
|
||||
}
|
||||
@ -61,8 +140,11 @@ public class PictureBoxCollection extends JPanel {
|
||||
}
|
||||
|
||||
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);
|
||||
removedTrucks.add(removed);
|
||||
} else {
|
||||
JOptionPane.showMessageDialog(null, "Не удалось удалить объект", "Информация", JOptionPane.INFORMATION_MESSAGE);
|
||||
}
|
||||
@ -72,23 +154,45 @@ public class PictureBoxCollection extends JPanel {
|
||||
|
||||
buttonRefreshCollection = new JButton("Обновить коллекцию");
|
||||
buttonRefreshCollection.setFocusable(false);
|
||||
buttonRefreshCollection.setBounds(620, 200, 150, 30);
|
||||
buttonRefreshCollection.setBounds(620, 500, 150, 30);
|
||||
buttonRefreshCollection.addActionListener(e -> repaint());
|
||||
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.setFocusable(false);
|
||||
buttonShowDop.setBounds(620, 250, 150, 30);
|
||||
buttonShowDop.setBounds(620, 650, 150, 30);
|
||||
buttonShowDop.addActionListener(e -> new FrameDop());
|
||||
add(buttonShowDop);
|
||||
|
||||
setPreferredSize(new Dimension(800, 450));
|
||||
setPreferredSize(new Dimension(800, 750));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void paintComponent(Graphics g) {
|
||||
super.paintComponent(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);
|
||||
}
|
||||
}
|
||||
|
@ -26,16 +26,21 @@ public class PictureBoxDumpTruck extends JPanel {
|
||||
private JButton buttonStep;
|
||||
public JButton buttonSelectTruck;
|
||||
|
||||
public PictureBoxDumpTruck() {
|
||||
public PictureBoxDumpTruck(DrawingTruck truck) {
|
||||
Random random = new Random();
|
||||
setLayout(null);
|
||||
setBounds(0, 0, 800, 450);
|
||||
buttonCreateTruck = new JButton("Создать грузовик");
|
||||
buttonCreateTruck.setFocusable(false);
|
||||
buttonCreateTruck.setBounds(12, 415, 150, 30);
|
||||
add(buttonCreateTruck);
|
||||
if (truck != null){
|
||||
drawingTruck = truck;
|
||||
drawingTruck.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100));
|
||||
repaint();
|
||||
}
|
||||
|
||||
buttonCreateTruck.addActionListener(e -> {
|
||||
Random random = new Random();
|
||||
Color bodyColor = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256));
|
||||
Color selectedColor = JColorChooser.showDialog(this, "Выберите цвет", Color.WHITE);
|
||||
if (selectedColor != null)
|
||||
@ -56,7 +61,6 @@ public class PictureBoxDumpTruck extends JPanel {
|
||||
add(buttonCreateDumpTruck);
|
||||
|
||||
buttonCreateDumpTruck.addActionListener(e -> {
|
||||
Random random = new Random();
|
||||
Color bodyColor = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256));
|
||||
Color selectedColor = JColorChooser.showDialog(this, "Выберите цвет", Color.WHITE);
|
||||
if (selectedColor != null)
|
||||
|
Loading…
Reference in New Issue
Block a user