лаба 4
This commit is contained in:
parent
7b0ffff8ac
commit
4a6f1a14dd
@ -4,9 +4,9 @@ public class FrameExcavator extends JFrame {
|
||||
|
||||
public PictureBoxExcavator pictureBoxExcavator;
|
||||
|
||||
public FrameExcavator() {
|
||||
public FrameExcavator(DrawningTracktor tracktor) {
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
pictureBoxExcavator = new PictureBoxExcavator();
|
||||
pictureBoxExcavator = new PictureBoxExcavator(tracktor);
|
||||
add(pictureBoxExcavator);
|
||||
pack();
|
||||
setLocationRelativeTo(null);
|
||||
|
@ -3,27 +3,100 @@ import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.util.LinkedList;
|
||||
import java.util.Random;
|
||||
|
||||
public class PictureBoxCollection extends JPanel {
|
||||
public TracktorsGenericCollection<DrawningTracktor,DrawningObjectExcavator> _traktors;
|
||||
private TracktorsGenericStorage _storage;
|
||||
private LinkedList<DrawningTracktor> removedTracktors;
|
||||
private JLabel labelTools;
|
||||
private JButton buttonAddTracktor, buttonDeleteTracktor, buttonRefreshCollection, buttonShowDop;
|
||||
private JTextField textFieldNumber;
|
||||
private JButton buttonAddTracktor, buttonDeleteTracktor, 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() {
|
||||
removedTracktors = new LinkedList<>();
|
||||
setLayout(null);
|
||||
setBounds(0, 0, 800, 450);
|
||||
_traktors = new TracktorsGenericCollection<>(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 TracktorsGenericStorage(this.getWidth() - 200, this.getHeight());
|
||||
labelTools = new JLabel("Инструменты");
|
||||
labelTools.setBounds(660, 300, 150, 30);
|
||||
add(labelTools);
|
||||
buttonAddTracktor = new JButton("Добавить экскаватор");
|
||||
buttonAddTracktor.setFocusable(false);
|
||||
buttonAddTracktor.setBounds(620, 50, 150, 30);
|
||||
buttonAddTracktor.setBounds(620, 350, 150, 30);
|
||||
buttonAddTracktor.addActionListener(e -> {
|
||||
FrameExcavator frameExcavator = new FrameExcavator();
|
||||
if (listStorage.getSelectedIndex() == -1)
|
||||
return;
|
||||
var obj = _storage.get(listStorage.getSelectedValue());
|
||||
if (obj == null)
|
||||
return;
|
||||
FrameExcavator frameExcavator = new FrameExcavator(null);
|
||||
frameExcavator.pictureBoxExcavator.buttonSelectTracktor.addActionListener(e1 -> {
|
||||
if (_traktors.Add(frameExcavator.pictureBoxExcavator.drawingTracktor) != -1) {
|
||||
if (obj.Add(frameExcavator.pictureBoxExcavator.drawingTracktor) != -1) {
|
||||
JOptionPane.showMessageDialog(null, "Объект добавлен", "Информация", JOptionPane.INFORMATION_MESSAGE);
|
||||
} else {
|
||||
JOptionPane.showMessageDialog(null, "Не удалось добавить объект", "Информация", JOptionPane.INFORMATION_MESSAGE);
|
||||
@ -36,13 +109,18 @@ public class PictureBoxCollection extends JPanel {
|
||||
add(buttonAddTracktor);
|
||||
|
||||
textFieldNumber = new JTextField();
|
||||
textFieldNumber.setBounds(620, 100, 150, 30);
|
||||
textFieldNumber.setBounds(620, 400, 150, 30);
|
||||
add(textFieldNumber);
|
||||
|
||||
buttonDeleteTracktor = new JButton("Удалить");
|
||||
buttonDeleteTracktor.setFocusable(false);
|
||||
buttonDeleteTracktor.setBounds(620, 150, 150, 30);
|
||||
buttonDeleteTracktor.setBounds(620, 450, 150, 30);
|
||||
buttonDeleteTracktor.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;
|
||||
}
|
||||
@ -57,8 +135,11 @@ public class PictureBoxCollection extends JPanel {
|
||||
}
|
||||
|
||||
int pos = Integer.parseInt(textFieldNumber.getText());
|
||||
if (_traktors.remove(pos)) {
|
||||
DrawningTracktor removed = null;
|
||||
removed = obj.remove(pos);
|
||||
if (removed != null) {
|
||||
JOptionPane.showMessageDialog(null, "Объект удален", "Информация", JOptionPane.INFORMATION_MESSAGE);
|
||||
removedTracktors.add(removed);
|
||||
} else {
|
||||
JOptionPane.showMessageDialog(null, "Не удалось удалить объект", "Информация", JOptionPane.INFORMATION_MESSAGE);
|
||||
}
|
||||
@ -68,23 +149,44 @@ 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 (removedTracktors.size() == 0){
|
||||
JOptionPane.showMessageDialog(null, "Коллекция пуста", "Информация", JOptionPane.INFORMATION_MESSAGE);
|
||||
return;
|
||||
}
|
||||
var tracktor = removedTracktors.removeLast();
|
||||
FrameExcavator frameExcavator = new FrameExcavator(tracktor);
|
||||
frameExcavator.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(_traktors.ShowTracktors(), 0, 0, null);
|
||||
if (listStorage.getSelectedIndex() == -1)
|
||||
return;
|
||||
var obj = _storage.get(listStorage.getSelectedValue());
|
||||
if (obj == null)
|
||||
return;
|
||||
if (obj.ShowTracktors() == null)
|
||||
return;
|
||||
g2d.drawImage(obj.ShowTracktors(), 0, 0, null);
|
||||
}
|
||||
}
|
||||
|
@ -22,16 +22,21 @@ public class PictureBoxExcavator extends JPanel {
|
||||
private JButton buttonStep;
|
||||
public JButton buttonSelectTracktor;
|
||||
|
||||
public PictureBoxExcavator() {
|
||||
public PictureBoxExcavator(DrawningTracktor tracktor) {
|
||||
Random random = new Random();
|
||||
setLayout(null);
|
||||
setBounds(0, 0, 800, 450);
|
||||
buttonCreateTracktor = new JButton("Создать Трактор");
|
||||
buttonCreateTracktor.setFocusable(false);
|
||||
buttonCreateTracktor.setBounds(12, 415, 150, 30);
|
||||
add(buttonCreateTracktor);
|
||||
if (tracktor != null){
|
||||
drawingTracktor = tracktor;
|
||||
drawingTracktor.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100));
|
||||
repaint();
|
||||
}
|
||||
|
||||
buttonCreateTracktor.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)
|
||||
@ -52,7 +57,6 @@ public class PictureBoxExcavator extends JPanel {
|
||||
add(buttonCreateExcavator);
|
||||
|
||||
buttonCreateExcavator.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)
|
||||
|
@ -1,45 +1,61 @@
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.NoSuchElementException;
|
||||
public class SetGeneric<T extends Object> {
|
||||
private Object[] _places;
|
||||
public int Count(){return _places.length;}
|
||||
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 tracktor){
|
||||
return Insert(tracktor,0);
|
||||
}
|
||||
public int Insert(T tracktor, int position){
|
||||
if(position < 0 || position >= Count())
|
||||
if(position < 0 || position >= _maxCount)
|
||||
return -1;
|
||||
if (_places[position] == null){
|
||||
_places[position] = tracktor;
|
||||
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] = tracktor;
|
||||
_places.add(position, tracktor);
|
||||
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> GetTracktors(final Integer maxTracktors){
|
||||
return new Iterable<T>() {
|
||||
@Override
|
||||
public Iterator<T> iterator() {
|
||||
return new Iterator<T>() {
|
||||
private int currentIndex;
|
||||
private int count = 0;
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return currentIndex < _places.size() && (maxTracktors == null || count < maxTracktors);
|
||||
}
|
||||
|
||||
@Override
|
||||
public T next() {
|
||||
if (hasNext()){
|
||||
count++;
|
||||
return _places.get(currentIndex++);
|
||||
}
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
@Override
|
||||
public void remove(){
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -13,16 +13,17 @@ public class TracktorsGenericCollection<T extends DrawningTracktor, U extends IM
|
||||
_pictureHeight = pictureHeight;
|
||||
_collection = new SetGeneric<T>(width*height);
|
||||
}
|
||||
//перегрузка оператора сложения
|
||||
public int Add(T obj){
|
||||
if(obj == null)
|
||||
return -1;
|
||||
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();
|
||||
@ -45,12 +46,13 @@ public class TracktorsGenericCollection<T extends DrawningTracktor, U extends IM
|
||||
}
|
||||
}
|
||||
private void DrawObjects(Graphics g){
|
||||
for(int i = 0; i < _collection.Count();i++){
|
||||
T obj = _collection.Get(i);
|
||||
if(obj != null){
|
||||
obj.SetPosition(i %(_pictureWidth/ _placeSizeWidth)*_placeSizeWidth,i/(_pictureWidth/_placeSizeWidth) * _placeSizeHeight);
|
||||
obj.DrawTransport((Graphics2D) g);
|
||||
int index = 0;
|
||||
for(T tracktor: _collection.GetTracktors(100)){
|
||||
if(tracktor != null){
|
||||
tracktor.SetPosition(index %(_pictureWidth/ _placeSizeWidth)*_placeSizeWidth,index/(_pictureWidth/_placeSizeWidth) * _placeSizeHeight);
|
||||
tracktor.DrawTransport((Graphics2D) g);
|
||||
}
|
||||
index++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
43
src/TracktorsGenericStorage.java
Normal file
43
src/TracktorsGenericStorage.java
Normal file
@ -0,0 +1,43 @@
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
|
||||
public class TracktorsGenericStorage {
|
||||
final HashMap<String, TracktorsGenericCollection<DrawningTracktor, DrawningObjectExcavator>> _tracktorStorages;
|
||||
public ArrayList<String> Keys()
|
||||
{
|
||||
return new ArrayList<>(_tracktorStorages.keySet());
|
||||
}
|
||||
private final int _pictureWidth;
|
||||
private final int _pictureHeight;
|
||||
public TracktorsGenericStorage(int pictureWidth, int pictureHeight)
|
||||
{
|
||||
_pictureHeight = pictureHeight;
|
||||
_pictureWidth = pictureWidth;
|
||||
_tracktorStorages = new HashMap<>();
|
||||
}
|
||||
public void AddSet(String name)
|
||||
{
|
||||
if (Keys().contains(name))
|
||||
return;
|
||||
_tracktorStorages.put(name, new TracktorsGenericCollection<>(_pictureWidth, _pictureHeight));
|
||||
}
|
||||
public void DelSet(String name)
|
||||
{
|
||||
if (!Keys().contains(name))
|
||||
return;
|
||||
_tracktorStorages.remove(name);
|
||||
}
|
||||
|
||||
public TracktorsGenericCollection<DrawningTracktor, DrawningObjectExcavator> get(String ind)
|
||||
{
|
||||
if (Keys().contains(ind))
|
||||
return _tracktorStorages.get(ind);
|
||||
return null;
|
||||
}
|
||||
public DrawningObjectExcavator get(String name, int ind)
|
||||
{
|
||||
if (!Keys().contains(name))
|
||||
return null;
|
||||
return _tracktorStorages.get(name).GetU(ind);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user