лаб3.2

This commit is contained in:
Ilya Ryabov 2024-04-27 17:38:24 +04:00
parent 029dcb4e42
commit 5f3ecf6c51
3 changed files with 64 additions and 72 deletions

View File

@ -17,55 +17,54 @@ import java.util.Random;
public class FormAdditionalCollection extends JFrame { public class FormAdditionalCollection extends JFrame {
public DrawingBaseStormtrooper _drawingBaseStormtrooper = null; public DrawingBaseStormtrooper _drawingBaseStormtrooper = null;
private DrawingBaseStormtrooper copyStormtrooper = null;
private AbstractCompany _company = null; private AbstractCompany _company = null;
private CanvasStormtrooper _canvasStormtrooper = new CanvasStormtrooper(); private CanvasStormtrooper _canvasStormtrooper = new CanvasStormtrooper();
private AdditionalCollection<EntityBaseStormtrooper, IDrawingEngines> _additionalCollection = null; private AdditionalCollection<EntityBaseStormtrooper, IDrawingEngines> _additionalCollection = null;
private Random random = new Random(); private Random random = new Random();
private JButton buttonGenerate = new JButton("Создать"); private JButton buttonGenerate = new JButton("Создать");
private JButton buttonGoToCollection = new JButton("В коллекцию");
private JList<String> listEntity = new JList<String>(); private JList<String> listEntity = new JList<String>();
private JList<String> listEngines = new JList<String>(); private JList<String> listEngines = new JList<String>();
public FormAdditionalCollection() { public FormAdditionalCollection() {
setTitle("Random stormtrooper"); setTitle("Случайные бомбардировщики");
setMinimumSize(new Dimension(650,310)); setMinimumSize(new Dimension(970,310));
_additionalCollection = new AdditionalCollection<EntityBaseStormtrooper,IDrawingEngines>(3,(Class)EntityBaseStormtrooper.class,(Class)IDrawingEngines.class); _additionalCollection = new AdditionalCollection<EntityBaseStormtrooper,IDrawingEngines>(3,(Class)EntityBaseStormtrooper.class,(Class)IDrawingEngines.class);
AddEntities(); AddEntities();
AddEngines(); AddEngines();
buttonGoToCollection.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(_drawingBaseStormtrooper!=null){
_company._collection.Insert(copyStormtrooper);
FormStormtrooperCollection.canvasShow();
}
}
});
buttonGenerate.addActionListener(new ActionListener() { buttonGenerate.addActionListener(new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
_drawingBaseStormtrooper = _additionalCollection.CreateAdditionalCollectionStormtrooper(); _drawingBaseStormtrooper = _additionalCollection.CreateAdditionalCollectionStormtrooper();
_drawingBaseStormtrooper.SetPictureSize(getWidth(), getHeight()); _drawingBaseStormtrooper.SetPictureSize(getWidth(), getHeight());
_drawingBaseStormtrooper.SetPosition(50,50); _drawingBaseStormtrooper.SetPosition(360,30);
_canvasStormtrooper._drawingBaseStormtrooper = _drawingBaseStormtrooper; _canvasStormtrooper._drawingBaseStormtrooper = _drawingBaseStormtrooper;
_canvasStormtrooper.repaint(); _canvasStormtrooper.repaint();
DrawingBaseStormtrooper copyStormtrooper;
if (_drawingBaseStormtrooper instanceof DrawingStormtrooper) if (_drawingBaseStormtrooper instanceof DrawingStormtrooper)
copyStormtrooper = new DrawingStormtrooper((EntityStormtrooper) _drawingBaseStormtrooper.EntityBaseStormtrooper, _drawingBaseStormtrooper.drawingEngines); copyStormtrooper = new DrawingStormtrooper((EntityStormtrooper) _drawingBaseStormtrooper.EntityBaseStormtrooper, _drawingBaseStormtrooper.drawingEngines);
else else
copyStormtrooper = new DrawingBaseStormtrooper(_drawingBaseStormtrooper.EntityBaseStormtrooper, _drawingBaseStormtrooper.drawingEngines); copyStormtrooper = new DrawingBaseStormtrooper(_drawingBaseStormtrooper.EntityBaseStormtrooper, _drawingBaseStormtrooper.drawingEngines);
_company._collection.Insert(copyStormtrooper);
FormStormtrooperCollection.canvasShow();
String[] data1 = new String[_additionalCollection.CountEntities];
for (int i = 0; i < _additionalCollection.CountEntities; i++) {
EntityBaseStormtrooper entity = _additionalCollection._collectionEntity[i];
data1[i] = ToString(entity);
}
String[] data2 = new String[_additionalCollection.CountEngines];
for (int i = 0; i < _additionalCollection.CountEngines; i++) {
IDrawingEngines engines = _additionalCollection._collectionEngines[i];
data2[i] = ToString(engines);
}
listEntity.setListData(data1);
listEngines.setListData(data2);
} }
}); });
buttonGenerate.setBounds(450, 10, 100, 50); buttonGoToCollection.setBounds(830,200,120,60);
buttonGenerate.setBounds(830, 130, 120, 60);
listEntity.setBounds(10,200,400,60);
listEngines.setBounds(420,200,400,60);
add(buttonGenerate); add(buttonGenerate);
listEntity.setBounds(10,200,300,60); add(buttonGoToCollection);
listEngines.setBounds(320,200,300,60);
add(listEntity); add(listEntity);
add(listEngines); add(listEngines);
add(_canvasStormtrooper); add(_canvasStormtrooper);
@ -73,8 +72,8 @@ public class FormAdditionalCollection extends JFrame {
} }
private String ToString(EntityBaseStormtrooper entity) { private String ToString(EntityBaseStormtrooper entity) {
String str = ""; String str = "";
if (entity instanceof EntityStormtrooper) str += "EntityWarmlyShip "; if (entity instanceof EntityStormtrooper) str += "EntityStormtrooper ";
else str += "EntityShip "; else str += "EntityBaseStormtrooper ";
str += entity.getBodyColor().toString(); str += entity.getBodyColor().toString();
return str; return str;
} }
@ -138,17 +137,17 @@ public class FormAdditionalCollection extends JFrame {
} }
void setCompany(AbstractCompany company) { void setCompany(AbstractCompany company) {
this._company = company; this._company = company;
for (int i = 0; i < _additionalCollection.CountEntities; i++){ String[] data1 = new String[_additionalCollection.CountEntities];
for (int i = 0; i < _additionalCollection.CountEntities; i++) {
EntityBaseStormtrooper entity = _additionalCollection._collectionEntity[i]; EntityBaseStormtrooper entity = _additionalCollection._collectionEntity[i];
DrawingBaseStormtrooper _listStormtrooper; data1[i] = ToString(entity);
if(entity instanceof EntityStormtrooper){
_listStormtrooper = new DrawingStormtrooper((EntityStormtrooper) entity,_additionalCollection._collectionEngines[i]);
}else{
_listStormtrooper = new DrawingBaseStormtrooper(entity,_additionalCollection._collectionEngines[i]);
} }
_company._collection.Insert(_listStormtrooper); String[] data2 = new String[_additionalCollection.CountEngines];
for (int i = 0; i < _additionalCollection.CountEngines; i++) {
IDrawingEngines engines = _additionalCollection._collectionEngines[i];
data2[i] = ToString(engines);
} }
listEntity.setListData(data1);
listEngines.setListData(data2);
} }
} }

View File

@ -37,17 +37,17 @@ public class FormStormtrooper extends JFrame {
Width = getWidth() - 10; Width = getWidth() - 10;
Height = getHeight() - 34; Height = getHeight() - 34;
_strategy = null; _strategy = null;
Icon iconUp = new ImageIcon("Resources\\arrowUp.jpg"); Icon iconUp = new ImageIcon("E:\\ООП\\Java\\Lab3\\ProjectStormtrooper\\Resources\\arrowUp.jpg");
UpButton.setIcon(iconUp); UpButton.setIcon(iconUp);
UpButton.setName("UP"); UpButton.setName("UP");
DownButton.setName("DOWN"); DownButton.setName("DOWN");
Icon iconDown = new ImageIcon("Resources\\arrowDown.jpg"); Icon iconDown = new ImageIcon("E:\\ООП\\Java\\Lab3\\ProjectStormtrooper\\Resources\\arrowDown.jpg");
DownButton.setIcon(iconDown); DownButton.setIcon(iconDown);
LeftButton.setName("LEFT"); LeftButton.setName("LEFT");
Icon iconLeft = new ImageIcon("Resources\\arrowLeft.jpg"); Icon iconLeft = new ImageIcon("E:\\ООП\\Java\\Lab3\\ProjectStormtrooper\\Resources\\arrowLeft.jpg");
LeftButton.setIcon(iconLeft); LeftButton.setIcon(iconLeft);
RightButton.setName("RIGHT"); RightButton.setName("RIGHT");
Icon iconRight = new ImageIcon("Resources\\arrowRight.jpg"); Icon iconRight = new ImageIcon("E:\\ООП\\Java\\Lab3\\ProjectStormtrooper\\Resources\\arrowRight.jpg");
RightButton.setIcon(iconRight); RightButton.setIcon(iconRight);
ButtonStrategy.addActionListener(new ActionListener() { ButtonStrategy.addActionListener(new ActionListener() {

View File

@ -20,11 +20,11 @@ public class FormStormtrooperCollection extends JFrame{
private JButton CreateButton = new JButton("Создать бомбардировщик");; private JButton CreateButton = new JButton("Создать бомбардировщик");;
private JButton CreateShipButton = new JButton("Создать базовый бомбардировщик"); private JButton CreateShipButton = new JButton("Создать базовый бомбардировщик");
private JButton RemoveButton = new JButton("Удалить"); private JButton RemoveButton = new JButton("Удалить");
private JButton GoToCheckButton = new JButton("Check"); private JButton GoToCheckButton = new JButton("На проверку");
private JButton RandomButton = new JButton("RandomShip"); private JButton RandomButton = new JButton("Случайные");
private JButton RefreshButton = new JButton("Refresh"); private JButton RefreshButton = new JButton("Обновить");
private JComboBox ComboBoxCollections = new JComboBox(new String[]{"", "Хранилище"}); private JComboBox ComboBoxCollections = new JComboBox(new String[]{"", "Хранилище"});
private JFormattedTextField MaskedTextField; private JFormattedTextField TextField;
public FormStormtrooperCollection(String title, Dimension dimension) { public FormStormtrooperCollection(String title, Dimension dimension) {
this.title = title; this.title = title;
this.dimension = dimension; this.dimension = dimension;
@ -72,15 +72,8 @@ public class FormStormtrooperCollection extends JFrame{
setMinimumSize(dimension); setMinimumSize(dimension);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MaskFormatter mask = null;
try {
mask = new MaskFormatter("##");
mask.setPlaceholder("00");
} catch (ParseException e) {
throw new RuntimeException(e);
}
MaskedTextField = new JFormattedTextField(mask); TextField = new JFormattedTextField();
ComboBoxCollections.addActionListener(new ActionListener() { ComboBoxCollections.addActionListener(new ActionListener() {
@Override @Override
@ -109,10 +102,10 @@ public class FormStormtrooperCollection extends JFrame{
RemoveButton.addActionListener(new ActionListener() { RemoveButton.addActionListener(new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
if (_company == null || MaskedTextField.getText() == null) { if (_company == null || TextField.getText() == null) {
return; return;
} }
int pos = parseInt(MaskedTextField.getText()); int pos = parseInt(TextField.getText());
int resultConfirmDialog = JOptionPane.showConfirmDialog(null, "Удалить", "Удаление", JOptionPane.YES_NO_OPTION); int resultConfirmDialog = JOptionPane.showConfirmDialog(null, "Удалить", "Удаление", JOptionPane.YES_NO_OPTION);
if (resultConfirmDialog == JOptionPane.NO_OPTION) return; if (resultConfirmDialog == JOptionPane.NO_OPTION) return;
if (_company._collection.Remove(pos) != null) { if (_company._collection.Remove(pos) != null) {
@ -185,10 +178,10 @@ public class FormStormtrooperCollection extends JFrame{
ComboBoxCollections.setBounds(getWidth()-190, 10, 150, 20); ComboBoxCollections.setBounds(getWidth()-190, 10, 150, 20);
CreateShipButton.setBounds(getWidth()-190, 60, 150, 30); CreateShipButton.setBounds(getWidth()-190, 60, 150, 30);
CreateButton.setBounds(getWidth()-190, 100, 150, 30); CreateButton.setBounds(getWidth()-190, 100, 150, 30);
MaskedTextField.setBounds(getWidth()-190,200,150,30); RandomButton.setBounds(getWidth()-190, 140, 150, 30);
TextField.setBounds(getWidth()-190,200,150,30);
RemoveButton.setBounds(getWidth()-190, 240, 150, 30); RemoveButton.setBounds(getWidth()-190, 240, 150, 30);
GoToCheckButton.setBounds(getWidth()-190, 280, 150, 30); GoToCheckButton.setBounds(getWidth()-190, 280, 150, 30);
RandomButton.setBounds(getWidth()-190, 320, 150, 30);
RefreshButton.setBounds(getWidth()-190, getHeight()-90, 150, 30); RefreshButton.setBounds(getWidth()-190, getHeight()-90, 150, 30);
setSize(dimension.width,dimension.height); setSize(dimension.width,dimension.height);
@ -197,7 +190,7 @@ public class FormStormtrooperCollection extends JFrame{
add(ComboBoxCollections); add(ComboBoxCollections);
add(CreateShipButton); add(CreateShipButton);
add(CreateButton); add(CreateButton);
add(MaskedTextField); add(TextField);
add(RemoveButton); add(RemoveButton);
add(GoToCheckButton); add(GoToCheckButton);
add(RandomButton); add(RandomButton);
@ -210,10 +203,10 @@ public class FormStormtrooperCollection extends JFrame{
ComboBoxCollections.setBounds(getWidth()-190, 10, 150, 20); ComboBoxCollections.setBounds(getWidth()-190, 10, 150, 20);
CreateShipButton.setBounds(getWidth()-190, 60, 150, 30); CreateShipButton.setBounds(getWidth()-190, 60, 150, 30);
CreateButton.setBounds(getWidth()-190, 100, 150, 30); CreateButton.setBounds(getWidth()-190, 100, 150, 30);
MaskedTextField.setBounds(getWidth()-190,200,150,30); TextField.setBounds(getWidth()-190,200,150,30);
RemoveButton.setBounds(getWidth()-190, 240, 150, 30); RemoveButton.setBounds(getWidth()-190, 240, 150, 30);
GoToCheckButton.setBounds(getWidth()-190, 280, 150, 30); GoToCheckButton.setBounds(getWidth()-190, 280, 150, 30);
RandomButton.setBounds(getWidth()-190, 320, 150, 30); RandomButton.setBounds(getWidth()-190, 140, 150, 30);
RefreshButton.setBounds(getWidth()-190, getHeight()-90, 150, 30); RefreshButton.setBounds(getWidth()-190, getHeight()-90, 150, 30);
} }
}); });