four commit
This commit is contained in:
parent
53230875c8
commit
226955f1fe
@ -4,11 +4,11 @@ public class BomberGenericCollection<T extends DrawingAir, U extends IMoveableOb
|
||||
/// <summary>
|
||||
/// Ширина окна прорисовки
|
||||
/// </summary>
|
||||
private int pictureWidth;
|
||||
private final int pictureWidth;
|
||||
/// <summary>
|
||||
/// Высота окна прорисовки
|
||||
/// </summary>
|
||||
private int _pictureHeight;
|
||||
private final int _pictureHeight;
|
||||
/// <summary>
|
||||
/// Размер занимаемого объектом места (ширина)
|
||||
/// </summary>
|
||||
@ -32,7 +32,10 @@ public class BomberGenericCollection<T extends DrawingAir, U extends IMoveableOb
|
||||
int height = picHeight / _placeSizeHeight;
|
||||
pictureWidth = picWidth;
|
||||
_pictureHeight = picHeight;
|
||||
collection = new SetGeneric<T>(width * height, (Class<T>) DrawingAir.class);
|
||||
collection = new SetGeneric<T>(width * height);
|
||||
}
|
||||
public int size(){
|
||||
return collection.getCount();
|
||||
}
|
||||
/// <summary>
|
||||
/// Перегрузка оператора сложения
|
||||
@ -52,11 +55,7 @@ public class BomberGenericCollection<T extends DrawingAir, U extends IMoveableOb
|
||||
/// <param name="collect"></param>
|
||||
/// <param name="pos"></param>
|
||||
/// <returns></returns>
|
||||
public boolean Remove(int pos)
|
||||
{
|
||||
T obj = collection.Get(pos);
|
||||
if (obj == null)
|
||||
return false;
|
||||
public boolean Remove(int pos) {
|
||||
return collection.remove(pos);
|
||||
}
|
||||
/// <summary>
|
||||
@ -70,6 +69,11 @@ public class BomberGenericCollection<T extends DrawingAir, U extends IMoveableOb
|
||||
return null;
|
||||
return (U)collection.Get(pos).GetMoveableObject();
|
||||
}
|
||||
public T Get(int position){
|
||||
if(position < 0 || position >= collection.getCount())
|
||||
return null;
|
||||
return collection.Get(position);
|
||||
}
|
||||
/// <summary>
|
||||
/// Вывод всего набора объектов
|
||||
/// </summary>
|
||||
|
37
src/BomberGenericStorage.java
Normal file
37
src/BomberGenericStorage.java
Normal file
@ -0,0 +1,37 @@
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
public class BomberGenericStorage {
|
||||
final HashMap<String, BomberGenericCollection<DrawingAir, DrawingObjectPlane>> planeStorages;
|
||||
private final int _pictureWidth;
|
||||
private final int _pictureHeight;
|
||||
|
||||
public BomberGenericStorage(int pictureWidth, int pictureHeight){
|
||||
planeStorages = new HashMap<>();
|
||||
_pictureWidth = pictureWidth;
|
||||
_pictureHeight = pictureHeight;
|
||||
}
|
||||
public ArrayList<String> Keys(){
|
||||
return new ArrayList<>(planeStorages.keySet());
|
||||
}
|
||||
public void AddSet(String name){
|
||||
if(planeStorages.containsKey(name))
|
||||
return;
|
||||
planeStorages.put(name, new BomberGenericCollection<>(_pictureWidth, _pictureHeight));
|
||||
}
|
||||
|
||||
public void DelSet(String name){
|
||||
if(!planeStorages.containsKey(name))
|
||||
return;
|
||||
planeStorages.remove(name);
|
||||
}
|
||||
|
||||
public BomberGenericCollection<DrawingAir, DrawingObjectPlane> getCollection(String name){
|
||||
if(!planeStorages.containsKey(name))
|
||||
return null;
|
||||
return planeStorages.get(name);
|
||||
}
|
||||
|
||||
public DrawingAir getPlanes(String collectionName, int position){
|
||||
return planeStorages.get(collectionName).Get(position);
|
||||
}
|
||||
}
|
16
src/BomberTrashCollection.java
Normal file
16
src/BomberTrashCollection.java
Normal file
@ -0,0 +1,16 @@
|
||||
import java.util.LinkedList;
|
||||
public class BomberTrashCollection<T extends DrawingAir> {
|
||||
LinkedList<T> linkedList;
|
||||
public BomberTrashCollection(){
|
||||
linkedList = new LinkedList<>();
|
||||
}
|
||||
public void Push(T plane){
|
||||
linkedList.push(plane);
|
||||
}
|
||||
public T Pop(){
|
||||
return linkedList.pop();
|
||||
}
|
||||
public int GetSize(){
|
||||
return linkedList.size();
|
||||
}
|
||||
}
|
@ -24,6 +24,7 @@ public class FrameAirBomber extends JFrame {
|
||||
pictureBox = new JComponent(){
|
||||
public void paintComponent(Graphics graphics){
|
||||
super.paintComponent(graphics);
|
||||
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
|
||||
Graphics2D graphics2D = (Graphics2D) graphics;
|
||||
if (drawingPlane != null) drawingPlane.drawTransport(graphics2D);
|
||||
super.repaint();
|
||||
@ -192,4 +193,11 @@ public class FrameAirBomber extends JFrame {
|
||||
}
|
||||
selectedPlane = drawingPlane;
|
||||
}
|
||||
|
||||
public void ChangePlane(DrawingAir newPlane){
|
||||
drawingPlane = newPlane;
|
||||
drawingPlane.setPosition(0,0);
|
||||
pictureBox.setBounds(0,0,getContentPane().getWidth(),getContentPane().getHeight());
|
||||
draw();
|
||||
}
|
||||
}
|
@ -1,102 +1,194 @@
|
||||
import javax.swing.*;
|
||||
import javax.swing.border.StrokeBorder;
|
||||
|
||||
import java.awt.*;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Objects;
|
||||
|
||||
public class FramePlaneCollection extends JFrame {
|
||||
private BomberGenericCollection<DrawingAir, DrawingObjectPlane> planes;
|
||||
JComponent pictureBoxCollection;
|
||||
TextField textFieldNumber;
|
||||
private BomberTrashCollection<DrawingAir> trashCollection = new BomberTrashCollection<>();
|
||||
private final BomberGenericStorage storage;
|
||||
private JComponent pictureBoxCollection;
|
||||
private TextField textFieldNumber;
|
||||
private TextField textFieldStorageName;
|
||||
private JList<String> listStorages;
|
||||
private DefaultListModel<String> listModel;
|
||||
public FramePlaneCollection(){
|
||||
super("Набор самолетов");
|
||||
setSize(new Dimension(900,600));
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
creteGUI();
|
||||
planes = new BomberGenericCollection<>(pictureBoxCollection.getWidth(), pictureBoxCollection.getHeight());
|
||||
pictureBoxCollection.repaint();
|
||||
createGui();
|
||||
pack();
|
||||
storage = new BomberGenericStorage(pictureBoxCollection.getWidth(), pictureBoxCollection.getHeight());
|
||||
setVisible(true);
|
||||
}
|
||||
|
||||
public void creteGUI(){
|
||||
private void createGui(){
|
||||
//components initialisation
|
||||
pictureBoxCollection = new JComponent(){
|
||||
public void paintComponent(Graphics graphics){
|
||||
super.paintComponent(graphics);
|
||||
Graphics2D graphics2D = (Graphics2D) graphics;
|
||||
if (planes != null) planes.ShowPlanes(graphics2D);
|
||||
if (listStorages == null || storage == null)
|
||||
return;
|
||||
var collection = storage.getCollection(listStorages.getSelectedValue());
|
||||
if (collection == null)
|
||||
return;
|
||||
collection.ShowPlanes(graphics2D);
|
||||
super.repaint();
|
||||
}
|
||||
};
|
||||
pictureBoxCollection.setBounds(0, 0, 700, 600);
|
||||
pictureBoxCollection.setPreferredSize(new Dimension(700, 600));
|
||||
JButton buttonAddPlane = new JButton("Добавить самолет");
|
||||
textFieldNumber = new TextField();
|
||||
JButton buttonRemovePlane = new JButton("Удалить самолет");
|
||||
JButton buttonRefreshCollection = new JButton("Обновить коллекцию");
|
||||
//ActionListeners
|
||||
buttonAddPlane.addActionListener(e -> ButtonAddPlaneClick());
|
||||
buttonRemovePlane.addActionListener(e -> ButtonRemovePlaneClick());
|
||||
buttonRefreshCollection.addActionListener(e -> ButtonRefreshCollectionClick());
|
||||
//addition to panel
|
||||
JPanel panelCollection = new JPanel(new GridBagLayout());
|
||||
JButton buttonAddSet = new JButton("Добавить набор");
|
||||
JButton buttonDeleteSet = new JButton("Удалить набор");
|
||||
JButton buttonTrash = new JButton("Корзина");
|
||||
textFieldStorageName = new TextField();
|
||||
listModel = new DefaultListModel<>();
|
||||
JScrollPane scrollPane = new JScrollPane();
|
||||
listStorages= new JList<>(listModel);
|
||||
scrollPane.setViewportView(listStorages);
|
||||
//ActionListeners and ActionCommand addition
|
||||
buttonAddSet.addActionListener(e -> buttonAddSet_Click());
|
||||
buttonDeleteSet.addActionListener(e -> buttonDeleteSet_Click());
|
||||
buttonAddPlane.addActionListener(e -> buttonAddPlaneClick());
|
||||
buttonRemovePlane.addActionListener(e -> buttonRemovePlaneClick());
|
||||
buttonRefreshCollection.addActionListener(e -> buttonRefreshCollectionClick());
|
||||
buttonTrash.addActionListener(e->buttonTrashClick());
|
||||
//panels and constraints initialisation
|
||||
JPanel panelTools = new JPanel(new GridBagLayout());
|
||||
panelTools.setBorder(new StrokeBorder(new BasicStroke(3)));
|
||||
panelTools.setToolTipText("Инструменты");
|
||||
JPanel panelSets = new JPanel(new GridBagLayout());
|
||||
panelSets.setBorder(new StrokeBorder(new BasicStroke(3)));
|
||||
panelSets.setToolTipText("Наборы");
|
||||
GridBagConstraints constraints = new GridBagConstraints();
|
||||
constraints.insets.left = constraints.insets.right = 2;
|
||||
constraints.insets.top = constraints.insets.bottom = 20;
|
||||
constraints.insets.left = constraints.insets.right = 5;
|
||||
constraints.insets.top = constraints.insets.bottom = 5;
|
||||
constraints.fill = GridBagConstraints.BOTH;
|
||||
//addition to panelSets
|
||||
constraints.gridx = 0;
|
||||
constraints.gridy = 0;
|
||||
panelCollection.add(buttonAddPlane, constraints);
|
||||
panelSets.add(textFieldStorageName, constraints);
|
||||
constraints.gridx = 0;
|
||||
constraints.gridy = 1;
|
||||
panelCollection.add(textFieldNumber, constraints);
|
||||
panelSets.add(buttonAddSet, constraints);
|
||||
constraints.gridx = 0;
|
||||
constraints.gridy = 2;
|
||||
panelCollection.add(buttonRemovePlane, constraints);
|
||||
panelSets.add(scrollPane, constraints);
|
||||
constraints.gridx = 0;
|
||||
constraints.gridy = 3;
|
||||
panelSets.add(buttonDeleteSet, constraints);
|
||||
//addition to panelTools
|
||||
constraints.gridx = 0;
|
||||
constraints.gridy = 0;
|
||||
panelTools.add(panelSets, constraints);
|
||||
constraints.gridx = 0;
|
||||
constraints.gridy = 1;
|
||||
panelTools.add(buttonAddPlane, constraints);
|
||||
constraints.gridx = 0;
|
||||
constraints.gridy = 2;
|
||||
panelTools.add(textFieldNumber, constraints);
|
||||
constraints.gridx = 0;
|
||||
constraints.gridy = 3;
|
||||
panelTools.add(buttonRemovePlane, constraints);
|
||||
constraints.gridx = 0;
|
||||
constraints.gridy = 4;
|
||||
panelTools.add(buttonRefreshCollection, constraints);
|
||||
constraints.gridx = 0;
|
||||
constraints.gridy = 5;
|
||||
panelCollection.add(buttonRefreshCollection, constraints);
|
||||
JPanel upperPanel = new JPanel(new BorderLayout());
|
||||
panelTools.add(buttonTrash, constraints);
|
||||
//addition to frame
|
||||
setLayout(new BorderLayout());
|
||||
add(panelCollection, BorderLayout.EAST);
|
||||
add(panelTools, BorderLayout.EAST);
|
||||
add(pictureBoxCollection, BorderLayout.CENTER);
|
||||
}
|
||||
|
||||
private void ButtonAddPlaneClick(){
|
||||
private void reloadObjects(){
|
||||
int index = listStorages.getSelectedIndex();
|
||||
listModel.clear();
|
||||
ArrayList<String> keys = storage.Keys();
|
||||
for (String key : keys) {
|
||||
listModel.addElement(key);
|
||||
}
|
||||
if(listModel.size() > 0 && (index == -1 || index >= listModel.size()))
|
||||
listStorages.setSelectedIndex(0);
|
||||
else if(listModel.size() > 0)
|
||||
listStorages.setSelectedIndex(index);
|
||||
}
|
||||
public void buttonAddSet_Click() {
|
||||
if(textFieldStorageName.getText() == null ) {
|
||||
JOptionPane.showMessageDialog(this, "Не все данные заполнены");
|
||||
return;
|
||||
}
|
||||
String name = textFieldStorageName.getText();
|
||||
if (Objects.equals(name, "")) {
|
||||
JOptionPane.showMessageDialog(this, "Не все данные заполнены");
|
||||
return;
|
||||
}
|
||||
storage.AddSet(name);
|
||||
reloadObjects();
|
||||
}
|
||||
public void buttonDeleteSet_Click() {
|
||||
if (listStorages.getSelectedIndex() == -1)
|
||||
return;
|
||||
storage.DelSet(listStorages.getSelectedValue());
|
||||
reloadObjects();
|
||||
}
|
||||
private void buttonAddPlaneClick() {
|
||||
if (listStorages.getSelectedIndex() == -1)
|
||||
return;
|
||||
var obj = storage.getCollection(listStorages.getSelectedValue());
|
||||
if (obj == null)
|
||||
return;
|
||||
FrameAirBomber form;
|
||||
try {
|
||||
form = new FrameAirBomber();
|
||||
} catch (IOException e){
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
form.selectPlaneButton.addActionListener(e -> {
|
||||
form.selectPlaneButton.addActionListener(e->{
|
||||
form.select();
|
||||
var selectedPlane = form.getSelectedPlane();
|
||||
DrawingAir plane = form.getSelectedPlane();
|
||||
form.dispose();
|
||||
if (planes.Insert(selectedPlane))
|
||||
{
|
||||
if (obj.Insert(plane)) {
|
||||
JOptionPane.showMessageDialog(this, "Объект добавлен");
|
||||
pictureBoxCollection.repaint();
|
||||
}
|
||||
else
|
||||
{
|
||||
else {
|
||||
JOptionPane.showMessageDialog(this, "Не удалось добавить объект");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void ButtonRemovePlaneClick()
|
||||
{
|
||||
private void buttonRemovePlaneClick(){
|
||||
if (listStorages.getSelectedIndex() == -1)
|
||||
return;
|
||||
var obj = storage.getCollection(listStorages.getSelectedValue());
|
||||
if (obj == null)
|
||||
return;
|
||||
int pos = Integer.parseInt(textFieldNumber.getText());
|
||||
if (planes.Remove(pos))
|
||||
{
|
||||
var plane = obj.Get(pos);
|
||||
if (obj.Remove(pos)){
|
||||
JOptionPane.showMessageDialog(this, "Объект удален");
|
||||
trashCollection.Push(plane);
|
||||
pictureBoxCollection.repaint();
|
||||
}
|
||||
else
|
||||
{
|
||||
else{
|
||||
JOptionPane.showMessageDialog(this, "Не удалось удалить объект");
|
||||
}
|
||||
}
|
||||
|
||||
private void ButtonRefreshCollectionClick()
|
||||
{
|
||||
private void buttonRefreshCollectionClick(){
|
||||
pictureBoxCollection.repaint();
|
||||
}
|
||||
private void buttonTrashClick(){
|
||||
if(trashCollection.GetSize() == 0)
|
||||
return;
|
||||
try {
|
||||
FrameAirBomber form = new FrameAirBomber();
|
||||
form.ChangePlane(trashCollection.Pop());
|
||||
} catch (Exception e){
|
||||
throw new RuntimeException();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
import java.io.IOException;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) throws IOException { new FrameHard(); }
|
||||
public static void main(String[] args) throws IOException { new FramePlaneCollection(); }
|
||||
}
|
||||
|
@ -1,39 +1,42 @@
|
||||
import java.lang.reflect.Array;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class SetGeneric<T extends Object>{
|
||||
private final T[] places;
|
||||
public int getCount() {return places.length;}
|
||||
public SetGeneric(int count, Class<T> type){
|
||||
places = (T[])Array.newInstance(type, count);
|
||||
public class SetGeneric<T>{
|
||||
private final ArrayList<T> places;
|
||||
private final int maxCount;
|
||||
public int getCount() {return places.size();}
|
||||
public SetGeneric(int count){
|
||||
maxCount = count;
|
||||
places = new ArrayList<>();
|
||||
}
|
||||
public boolean insert(T plane){
|
||||
return insert(plane, 0);
|
||||
}
|
||||
public boolean insert(T plane, int position){
|
||||
if (!(position >= 0 && position < places.length))
|
||||
if (!(position >= 0 && position <= places.size() && places.size() < maxCount))
|
||||
return false;
|
||||
if (places[position] != null)
|
||||
{
|
||||
int ind = position;
|
||||
while (ind < places.length && places[ind] != null)
|
||||
ind++;
|
||||
if (ind == places.length)
|
||||
return false;
|
||||
for (int i = ind - 1; i >= position; i--)
|
||||
places[i + 1] = places[i];
|
||||
}
|
||||
places[position] = plane;
|
||||
places.add(position, plane);
|
||||
return true;
|
||||
}
|
||||
public boolean remove(int position){
|
||||
if(!(position >= 0 && position < getCount()))
|
||||
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 < getCount()))
|
||||
return null;
|
||||
return places[position];
|
||||
return places.get(position);
|
||||
}
|
||||
public ArrayList<T> getPlanes(int maxPlanes){
|
||||
ArrayList<T> toRet = new ArrayList<>();
|
||||
for(int i = 0; i < places.size(); i++){
|
||||
toRet.add(places.get(i));
|
||||
if(i == maxPlanes)
|
||||
return toRet;
|
||||
}
|
||||
return toRet;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user