Lab3 Java HARD Петрушин Егор ПИбд-22 #3
@ -17,7 +17,7 @@ import javax.swing.ImageIcon;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.util.Random;
|
||||
|
||||
public class Form extends JFrame {
|
||||
public class FormSPAU extends JFrame {
|
||||
|
||||
private JPanel mainPanel;
|
||||
private JButton buttonCreate;
|
||||
@ -35,7 +35,7 @@ public class Form extends JFrame {
|
||||
private JTextField wheelsTextField;
|
||||
private JLabel wheelsLabel;
|
||||
|
||||
public Form() {
|
||||
public FormSPAU() {
|
||||
initComponents();
|
||||
}
|
||||
|
125
SelfPropelledArtilleryUnit/FormSPAUCollection.java
Normal file
125
SelfPropelledArtilleryUnit/FormSPAUCollection.java
Normal file
@ -0,0 +1,125 @@
|
||||
package SelfPropelledArtilleryUnit;
|
||||
|
||||
import javax.swing.*;
|
||||
|
||||
import SelfPropelledArtilleryUnit.DrawningObjects.DrawningSPAU;
|
||||
import SelfPropelledArtilleryUnit.Generics.SPAUGenericCollection;
|
||||
import SelfPropelledArtilleryUnit.MovementStrategy.DrawningObjectSPAU;
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
|
||||
public class FormSPAUCollection extends JFrame {
|
||||
private JPanel contentPane;
|
||||
private JLabel pictureBoxCollection;
|
||||
private JFormattedTextField maskedTextBoxNumber;
|
||||
private JButton ButtonAddSPAU;
|
||||
private JButton ButtonRemoveSPAU;
|
||||
private JButton ButtonRefreshCollection;
|
||||
|
||||
private final int countPlaces = 11;
|
||||
private final SPAUGenericCollection<DrawningSPAU, DrawningObjectSPAU> _SPAUs;
|
||||
|
||||
public FormSPAUCollection() {
|
||||
initComponents();
|
||||
_SPAUs = new SPAUGenericCollection<>(pictureBoxCollection.getWidth(), pictureBoxCollection.getHeight());
|
||||
}
|
||||
|
||||
private void initComponents() {
|
||||
contentPane = new JPanel();
|
||||
pictureBoxCollection = new JLabel();
|
||||
maskedTextBoxNumber = new JFormattedTextField();
|
||||
ButtonAddSPAU = new JButton();
|
||||
ButtonRemoveSPAU = new JButton();
|
||||
ButtonRefreshCollection = new JButton();
|
||||
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
setBounds(100, 100, 899, 456);
|
||||
contentPane.setLayout(null);
|
||||
|
||||
pictureBoxCollection.setBounds(12, 12, 680, 432);
|
||||
contentPane.add(pictureBoxCollection);
|
||||
|
||||
maskedTextBoxNumber.setBounds(729, 51, 143, 27);
|
||||
contentPane.add(maskedTextBoxNumber);
|
||||
|
||||
ButtonAddSPAU.setBounds(748, 102, 102, 33);
|
||||
ButtonAddSPAU.setText("Добавить");
|
||||
ButtonAddSPAU.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
ButtonAddSPAU_Click(e);
|
||||
}
|
||||
});
|
||||
contentPane.add(ButtonAddSPAU);
|
||||
|
||||
ButtonRemoveSPAU.setBounds(750, 141, 100, 37);
|
||||
ButtonRemoveSPAU.setText("Удалить");
|
||||
ButtonRemoveSPAU.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
ButtonRemoveSPAU_Click(e);
|
||||
}
|
||||
});
|
||||
contentPane.add(ButtonRemoveSPAU);
|
||||
|
||||
ButtonRefreshCollection.setBounds(750, 195, 100, 35);
|
||||
ButtonRefreshCollection.setText("Обновить");
|
||||
ButtonRefreshCollection.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
ButtonRefreshCollection_Click(e);
|
||||
}
|
||||
});
|
||||
contentPane.add(ButtonRefreshCollection);
|
||||
|
||||
setContentPane(contentPane);
|
||||
}
|
||||
|
||||
private void ButtonAddSPAU_Click(ActionEvent e) {
|
||||
FormSPAU form = new FormSPAU();
|
||||
if (form.showDialog() == JOptionPane.OK_OPTION) {
|
||||
int addedIndex = _SPAUs.add(form.getSelectedSPAU());
|
||||
if (addedIndex != -1 && addedIndex <= countPlaces) {
|
||||
JOptionPane.showMessageDialog(this, "Об<EFBFBD><EFBFBD>ект добавлен");
|
||||
pictureBoxCollection.setIcon(new ImageIcon(_SPAUs.showSPAUs()));
|
||||
} else {
|
||||
JOptionPane.showMessageDialog(this, "Не удалось добавить объект");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void ButtonRemoveSPAU_Click(ActionEvent e) {
|
||||
if (JOptionPane.showConfirmDialog(this, "Удалить объект?", "Удаление", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
|
||||
int pos;
|
||||
try {
|
||||
pos = Integer.parseInt(maskedTextBoxNumber.getText());
|
||||
} catch (NumberFormatException ex) {
|
||||
JOptionPane.showMessageDialog(this, "Не удалось удалить объект");
|
||||
return;
|
||||
}
|
||||
if (_SPAUs.remove(pos)) {
|
||||
JOptionPane.showMessageDialog(this, "Объект удален");
|
||||
pictureBoxCollection.setIcon(new ImageIcon(_SPAUs.showSPAUs()));
|
||||
} else {
|
||||
JOptionPane.showMessageDialog(this, "Не удалось удалить объект");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void ButtonRefreshCollection_Click(ActionEvent e) {
|
||||
pictureBoxCollection.setIcon(new ImageIcon(_SPAUs.showSPAUs()));
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
EventQueue.invokeLater(new Runnable() {
|
||||
public void run() {
|
||||
try {
|
||||
FormSPAUCollection frame = new FormSPAUCollection();
|
||||
frame.setVisible(true);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,83 @@
|
||||
package SelfPropelledArtilleryUnit.Generics;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.Graphics;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import SelfPropelledArtilleryUnit.DrawningObjects.DrawningSPAU;
|
||||
import SelfPropelledArtilleryUnit.MovementStrategy.IMoveableObject;
|
||||
|
||||
public class SPAUGenericCollection<T extends DrawningSPAU, U extends IMoveableObject> {
|
||||
|
||||
private final int _pictureWidth;
|
||||
private final int _pictureHeight;
|
||||
private final int _placeSizeWidth = 210;
|
||||
private final int _placeSizeHeight = 90;
|
||||
private final List<T> _collection;
|
||||
|
||||
public SPAUGenericCollection(int picWidth, int picHeight) {
|
||||
int width = picWidth / _placeSizeWidth;
|
||||
int height = picHeight / _placeSizeHeight;
|
||||
_pictureWidth = picWidth;
|
||||
_pictureHeight = picHeight;
|
||||
_collection = new ArrayList<>(width * height);
|
||||
}
|
||||
|
||||
public boolean add(T obj) {
|
||||
if (obj == null) {
|
||||
return false;
|
||||
}
|
||||
return _collection.add(obj);
|
||||
}
|
||||
|
||||
public boolean remove(int pos) {
|
||||
if (pos < 0 || pos >= _collection.size()) {
|
||||
return false;
|
||||
}
|
||||
_collection.remove(pos);
|
||||
return true;
|
||||
}
|
||||
|
||||
public U getU(int pos) {
|
||||
if (pos < 0 || pos >= _collection.size()) {
|
||||
return null;
|
||||
}
|
||||
return (U) _collection.get(pos).GetMoveableObject();
|
||||
}
|
||||
|
||||
public BufferedImage showSPAUs() {
|
||||
BufferedImage bmp = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_RGB);
|
||||
Graphics g = bmp.getGraphics();
|
||||
drawBackground(g);
|
||||
drawObjects(g);
|
||||
return bmp;
|
||||
}
|
||||
|
||||
private void drawBackground(Graphics g) {
|
||||
g.setColor(Color.BLACK);
|
||||
for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++) {
|
||||
for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j) {
|
||||
g.drawLine(i * _placeSizeWidth, j * _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight);
|
||||
}
|
||||
g.drawLine(i * _placeSizeWidth, 0, i * _placeSizeWidth, _pictureHeight / _placeSizeHeight * _placeSizeHeight);
|
||||
}
|
||||
}
|
||||
|
||||
private void drawObjects(Graphics g) {
|
||||
int j = 0;
|
||||
int stringCount = 0;
|
||||
DrawningSPAU current;
|
||||
for (int i = 0; i < _collection.size(); i++) {
|
||||
current = _collection.get(i);
|
||||
current.SetPosition(stringCount * 200, 280 - j * 100);
|
||||
stringCount++;
|
||||
if (stringCount >= 3) {
|
||||
j++;
|
||||
stringCount = 0;
|
||||
}
|
||||
current.DrawTransport(g);
|
||||
}
|
||||
}
|
||||
}
|
56
SelfPropelledArtilleryUnit/Generics/SetGeneric.java
Normal file
56
SelfPropelledArtilleryUnit/Generics/SetGeneric.java
Normal file
@ -0,0 +1,56 @@
|
||||
package SelfPropelledArtilleryUnit.Generics;
|
||||
|
||||
public class SetGeneric<T> {
|
||||
private final T[] places;
|
||||
public int count;
|
||||
|
||||
public SetGeneric(int count) {
|
||||
this.count = count;
|
||||
places = (T[]) new Object[count];
|
||||
}
|
||||
|
||||
public int insert(T spau) {
|
||||
return insert(spau, 0);
|
||||
}
|
||||
|
||||
public int insert(T spau, int position) {
|
||||
if (position < 0 || spau == null) {
|
||||
return -1;
|
||||
}
|
||||
if (position >= count) {
|
||||
return -1;
|
||||
}
|
||||
int positionNull = -1;
|
||||
for (int i = position; i < count; i++) {
|
||||
if (places[i] == null) {
|
||||
positionNull = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (positionNull == -1 && places[count - 1] != null) {
|
||||
return -1;
|
||||
} else if (positionNull == -1) {
|
||||
positionNull = count - 1;
|
||||
}
|
||||
for (int i = positionNull; i > position; i--) {
|
||||
places[i] = places[i - 1];
|
||||
}
|
||||
places[position] = spau;
|
||||
return position;
|
||||
}
|
||||
|
||||
public boolean remove(int position) {
|
||||
if (position < 0 || position >= count)
|
||||
return false;
|
||||
|
||||
places[position] = null;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public T get(int position) {
|
||||
if (position < 0 || position >= count)
|
||||
return null;
|
||||
return places[position];
|
||||
}
|
||||
}
|
@ -4,7 +4,7 @@ import javax.swing.*;
|
||||
public class SelfPropelledArtilleryUnit {
|
||||
public static void main(String[] args) {
|
||||
SwingUtilities.invokeLater(() -> {
|
||||
Form form = new Form();
|
||||
FormSPAU form = new FormSPAU();
|
||||
form.setSize(900, 500);
|
||||
form.setVisible(true);
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user