From 0d9c3714a7f639bca1dcb35ac6e9060260e8d063 Mon Sep 17 00:00:00 2001 From: abazov73 <92822431+abazov73@users.noreply.github.com> Date: Mon, 31 Oct 2022 19:31:33 +0400 Subject: [PATCH] =?UTF-8?q?=D0=A2=D1=80=D0=B5=D1=82=D1=8C=D1=8F=20=D0=BB?= =?UTF-8?q?=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82=D0=BE=D1=80=D0=BD=D0=B0?= =?UTF-8?q?=D1=8F=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/AirBomberPackage/JFrameAirBomber.form | 22 +- .../src/AirBomberPackage/JFrameAirBomber.java | 48 ++- .../AirBomberPackage/JFrameDopGenerics.form | 171 +++++++++ .../AirBomberPackage/JFrameDopGenerics.java | 261 +++++++++++++ .../JFrameMapWithSetAirBombers.form | 247 ++++++++++++ .../JFrameMapWithSetAirBombers.java | 355 ++++++++++++++++++ .../MapWithSetAirBombersGeneric.java | 123 ++++++ .../SetAirBombersGeneric.java | 100 +++++ .../SetAirBombersGenericDop.java | 99 +++++ 9 files changed, 1409 insertions(+), 17 deletions(-) create mode 100644 AirBomber/src/AirBomberPackage/JFrameDopGenerics.form create mode 100644 AirBomber/src/AirBomberPackage/JFrameDopGenerics.java create mode 100644 AirBomber/src/AirBomberPackage/JFrameMapWithSetAirBombers.form create mode 100644 AirBomber/src/AirBomberPackage/JFrameMapWithSetAirBombers.java create mode 100644 AirBomber/src/AirBomberPackage/MapWithSetAirBombersGeneric.java create mode 100644 AirBomber/src/AirBomberPackage/SetAirBombersGeneric.java create mode 100644 AirBomber/src/AirBomberPackage/SetAirBombersGenericDop.java diff --git a/AirBomber/src/AirBomberPackage/JFrameAirBomber.form b/AirBomber/src/AirBomberPackage/JFrameAirBomber.form index e29b23f..e978d4f 100644 --- a/AirBomber/src/AirBomberPackage/JFrameAirBomber.form +++ b/AirBomber/src/AirBomberPackage/JFrameAirBomber.form @@ -1,15 +1,12 @@ -
+ - + - - - @@ -50,6 +47,8 @@ + + @@ -86,8 +85,9 @@ - - + + + @@ -267,5 +267,13 @@ + + + + + + + + diff --git a/AirBomber/src/AirBomberPackage/JFrameAirBomber.java b/AirBomber/src/AirBomberPackage/JFrameAirBomber.java index da71d20..ea18373 100644 --- a/AirBomber/src/AirBomberPackage/JFrameAirBomber.java +++ b/AirBomber/src/AirBomberPackage/JFrameAirBomber.java @@ -5,23 +5,26 @@ package AirBomberPackage; import java.awt.*; import javax.swing.*; +import javax.swing.JOptionPane; import java.util.Random; /** * * @author Андрей */ -public class JFrameAirBomber extends javax.swing.JFrame { +public class JFrameAirBomber extends javax.swing.JDialog { /** * Creates new form JFrameAirBomber */ - public JFrameAirBomber() { + public JFrameAirBomber(JFrame parent) { + super(parent, "Бомбардировщик", true); initComponents(); } private DrawingAirBomber _airBomber; private EnginesType enginesType = EnginesType.RECTANGLE; + private DrawingAirBomber _airBomberResult; /** * This method is called from within the constructor to initialize the form. @@ -44,11 +47,11 @@ public class JFrameAirBomber extends javax.swing.JFrame { ModifyButton = new javax.swing.JButton(); jLabelEngines1 = new javax.swing.JLabel(); jComboBoxEnginesType = new javax.swing.JComboBox<>(); + buttonSelect = new javax.swing.JButton(); - setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); + setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); setTitle("Бомбардировщик"); setFont(new java.awt.Font("Arial", 0, 10)); // NOI18N - setPreferredSize(new java.awt.Dimension(700, 400)); setSize(new java.awt.Dimension(700, 400)); createAirBomberButton.setText("Создать"); @@ -135,6 +138,13 @@ public class JFrameAirBomber extends javax.swing.JFrame { } }); + buttonSelect.setText("Выбрать"); + buttonSelect.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + buttonSelectActionPerformed(evt); + } + }); + javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( @@ -154,7 +164,9 @@ public class JFrameAirBomber extends javax.swing.JFrame { .addGroup(layout.createSequentialGroup() .addComponent(createAirBomberButton) .addGap(18, 18, 18) - .addComponent(ModifyButton))) + .addComponent(ModifyButton) + .addGap(18, 18, 18) + .addComponent(buttonSelect))) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 157, Short.MAX_VALUE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(upButton, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE) @@ -183,7 +195,8 @@ public class JFrameAirBomber extends javax.swing.JFrame { .addComponent(jComboBoxEnginesType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) - .addComponent(createAirBomberButton, javax.swing.GroupLayout.DEFAULT_SIZE, 31, Short.MAX_VALUE) + .addComponent(createAirBomberButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addComponent(buttonSelect, javax.swing.GroupLayout.DEFAULT_SIZE, 31, Short.MAX_VALUE) .addComponent(ModifyButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addComponent(upButton, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE) @@ -201,7 +214,9 @@ public class JFrameAirBomber extends javax.swing.JFrame { private void createAirBomberButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_createAirBomberButtonActionPerformed Random rnd = new Random(); - _airBomber = new DrawingAirBomber(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000), new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)), (jComboBoxEngines.getSelectedIndex() + 1) * 2, enginesType); + Color chosenColor = JColorChooser.showDialog(this, "Вибирете цвет", Color.BLACK); + _airBomber = new DrawingAirBomber(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000), chosenColor, (jComboBoxEngines.getSelectedIndex() + 1) * 2, enginesType); + SetData(); Draw(); }//GEN-LAST:event_createAirBomberButtonActionPerformed @@ -233,11 +248,13 @@ public class JFrameAirBomber extends javax.swing.JFrame { private void ModifyButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ModifyButtonActionPerformed Random rnd = new Random(); + Color chosenColor = JColorChooser.showDialog(this, "Вибирете цвет", Color.BLACK); + Color chosenDopColor = JColorChooser.showDialog(this, "Вибирете цвет", Color.BLACK); _airBomber = new DrawingHeavyAirBomber(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000), - new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)), + chosenColor, (jComboBoxEngines.getSelectedIndex() + 1) * 2, enginesType, - new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)), + chosenDopColor, rnd.nextBoolean(), rnd.nextBoolean(), rnd.nextBoolean()); SetData(); Draw(); @@ -258,6 +275,11 @@ public class JFrameAirBomber extends javax.swing.JFrame { } }//GEN-LAST:event_jComboBoxEnginesTypeItemStateChanged + private void buttonSelectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonSelectActionPerformed + _airBomberResult = _airBomber; + dispose(); + }//GEN-LAST:event_buttonSelectActionPerformed + private void SetData() { Random rnd = new Random(); @@ -270,6 +292,11 @@ public class JFrameAirBomber extends javax.swing.JFrame { airBomberCanvas.repaint(); } + public DrawingAirBomber run(){ + setVisible(true); + return _airBomberResult; + } + /** * @param args the command line arguments */ @@ -300,7 +327,7 @@ public class JFrameAirBomber extends javax.swing.JFrame { /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { - new JFrameMap().setVisible(true); + new JFrameDopGenerics().setVisible(true); } }); } @@ -308,6 +335,7 @@ public class JFrameAirBomber extends javax.swing.JFrame { // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton ModifyButton; private AirBomberPackage.CanvasMy airBomberCanvas; + private javax.swing.JButton buttonSelect; private javax.swing.JButton createAirBomberButton; private javax.swing.JButton downButton; private javax.swing.JComboBox jComboBoxEngines; diff --git a/AirBomber/src/AirBomberPackage/JFrameDopGenerics.form b/AirBomber/src/AirBomberPackage/JFrameDopGenerics.form new file mode 100644 index 0000000..a160bc8 --- /dev/null +++ b/AirBomber/src/AirBomberPackage/JFrameDopGenerics.form @@ -0,0 +1,171 @@ + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/AirBomber/src/AirBomberPackage/JFrameDopGenerics.java b/AirBomber/src/AirBomberPackage/JFrameDopGenerics.java new file mode 100644 index 0000000..5b44774 --- /dev/null +++ b/AirBomber/src/AirBomberPackage/JFrameDopGenerics.java @@ -0,0 +1,261 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/GUIForms/JFrame.java to edit this template + */ +package AirBomberPackage; + +import java.util.Random; +import java.awt.*; +import javax.swing.*; + +/** + * + * @author Андрей + */ +public class JFrameDopGenerics extends javax.swing.JFrame { + + /** + * Creates new form JFrameDopGenerics + */ + public JFrameDopGenerics() { + initComponents(); + int numOfDecals = 2; + airBomberGenerator = new SetAirBombersGenericDop<>(numOfDecals); + Random rnd = new Random(System.currentTimeMillis()); + for (int i = 0; i < numOfDecals; i++){ + EntityAirBomber airBomber; + int rndNum = rnd.nextInt(0, 100); + if (rndNum % 2 == 0){ + //создаём обычный бомбардировщик + airBomber = new EntityAirBomber(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000), new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256))); + } + else{ + //создаём тяжелый бомбардировщик + airBomber = new EntityHeavyAirBomber(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000), + new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)), + new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)), + rnd.nextBoolean(), + rnd.nextBoolean(), + rnd.nextBoolean()); + } + airBomberGenerator.add(airBomber); + } + + for (int i = 0; i < numOfDecals; i++){ + int rndNum = rnd.nextInt(0, 500); + IDrawingObjectDop engines = null; + if (rndNum % 3 == 0){ + //создаем квадратные двигатели + engines = new DrawingEngines(); + } + else if (rndNum % 3 == 1){ + //создаём треугольные двигатели + engines = new DrawingEnginesTriangle(); + } + else { + //создаём круглые двигатели + engines = new DrawingEnginesOval(); + } + engines.setNumberOfEngines(rnd.nextInt(1,4) * 2); + airBomberGenerator.add(engines); + } + } + + private DrawingAirBomber _airBomber; + private SetAirBombersGenericDop airBomberGenerator; + + /** + * This method is called from within the constructor to initialize the form. + * WARNING: Do NOT modify this code. The content of this method is always + * regenerated by the Form Editor. + */ + @SuppressWarnings("unchecked") + // //GEN-BEGIN:initComponents + private void initComponents() { + + createAirBomberButton = new javax.swing.JButton(); + leftButton = new javax.swing.JButton(); + downButton = new javax.swing.JButton(); + rightButton = new javax.swing.JButton(); + upButton = new javax.swing.JButton(); + airBomberCanvas = new AirBomberPackage.CanvasMy(); + + setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); + setTitle("Бомбардировщик"); + + createAirBomberButton.setText("Создать"); + createAirBomberButton.setName("createAirBomberButton"); // NOI18N + createAirBomberButton.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + createAirBomberButtonActionPerformed(evt); + } + }); + + leftButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/AirBomberPackage/arrowLeft.png"))); // NOI18N + leftButton.setMargin(new java.awt.Insets(0, 0, 0, 0)); + leftButton.setMaximumSize(new java.awt.Dimension(30, 30)); + leftButton.setMinimumSize(new java.awt.Dimension(30, 30)); + leftButton.setName("buttonLeft"); // NOI18N + leftButton.setPreferredSize(new java.awt.Dimension(30, 30)); + leftButton.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + moveButtonActionPerformed(evt); + } + }); + + downButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/AirBomberPackage/arrowDown.png"))); // NOI18N + downButton.setMaximumSize(new java.awt.Dimension(30, 30)); + downButton.setMinimumSize(new java.awt.Dimension(30, 30)); + downButton.setName("buttonDown"); // NOI18N + downButton.setPreferredSize(new java.awt.Dimension(30, 30)); + downButton.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + moveButtonActionPerformed(evt); + } + }); + + rightButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/AirBomberPackage/arrowRight.png"))); // NOI18N + rightButton.setMaximumSize(new java.awt.Dimension(30, 30)); + rightButton.setMinimumSize(new java.awt.Dimension(30, 30)); + rightButton.setName("buttonRight"); // NOI18N + rightButton.setPreferredSize(new java.awt.Dimension(30, 30)); + rightButton.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + moveButtonActionPerformed(evt); + } + }); + + upButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/AirBomberPackage/arrowUp.png"))); // NOI18N + upButton.setMaximumSize(new java.awt.Dimension(30, 30)); + upButton.setMinimumSize(new java.awt.Dimension(30, 30)); + upButton.setName("buttonUp"); // NOI18N + upButton.setPreferredSize(new java.awt.Dimension(30, 30)); + upButton.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + moveButtonActionPerformed(evt); + } + }); + + javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); + getContentPane().setLayout(layout); + layout.setHorizontalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addContainerGap() + .addComponent(createAirBomberButton, javax.swing.GroupLayout.DEFAULT_SIZE, 569, Short.MAX_VALUE) + .addGap(18, 18, 18) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) + .addComponent(upButton, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE) + .addGroup(layout.createSequentialGroup() + .addComponent(leftButton, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(downButton, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(rightButton, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE) + .addGap(17, 17, 17)) + .addGroup(layout.createSequentialGroup() + .addComponent(airBomberCanvas, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addContainerGap()) + ); + layout.setVerticalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() + .addComponent(airBomberCanvas, javax.swing.GroupLayout.DEFAULT_SIZE, 327, Short.MAX_VALUE) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addComponent(createAirBomberButton, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE) + .addGroup(layout.createSequentialGroup() + .addComponent(upButton, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) + .addComponent(leftButton, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE) + .addComponent(downButton, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE) + .addComponent(rightButton, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)))) + .addContainerGap()) + ); + + pack(); + }// //GEN-END:initComponents + + private void createAirBomberButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_createAirBomberButtonActionPerformed + _airBomber = airBomberGenerator.buildAirBomber(); + SetData(); + Draw(); + }//GEN-LAST:event_createAirBomberButtonActionPerformed + + private void moveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_moveButtonActionPerformed + if (_airBomber == null) return; + String name = ((JButton) evt.getSource()).getName(); + switch (name) + { + case "buttonUp": + _airBomber.MoveTransport(Direction.UP); + break; + case "buttonDown": + _airBomber.MoveTransport(Direction.DOWN); + break; + case "buttonLeft": + _airBomber.MoveTransport(Direction.LEFT); + break; + case "buttonRight": + _airBomber.MoveTransport(Direction.RIGHT); + break; + } + Draw(); + }//GEN-LAST:event_moveButtonActionPerformed + + private void SetData() + { + Random rnd = new Random(); + _airBomber.SetPosition(rnd.nextInt(10, 100), rnd.nextInt(10, 100), airBomberCanvas.getWidth(), airBomberCanvas.getHeight()); + airBomberCanvas.setAirBomber(_airBomber); + } + + private void Draw(){ + airBomberCanvas.repaint(); + } + + /** + * @param args the command line arguments + */ + public static void main(String args[]) { + /* Set the Nimbus look and feel */ + // + /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. + * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html + */ + try { + for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { + if ("Nimbus".equals(info.getName())) { + javax.swing.UIManager.setLookAndFeel(info.getClassName()); + break; + } + } + } catch (ClassNotFoundException ex) { + java.util.logging.Logger.getLogger(JFrameDopGenerics.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } catch (InstantiationException ex) { + java.util.logging.Logger.getLogger(JFrameDopGenerics.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } catch (IllegalAccessException ex) { + java.util.logging.Logger.getLogger(JFrameDopGenerics.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } catch (javax.swing.UnsupportedLookAndFeelException ex) { + java.util.logging.Logger.getLogger(JFrameDopGenerics.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } + // + + /* Create and display the form */ + java.awt.EventQueue.invokeLater(new Runnable() { + public void run() { + new JFrameDopGenerics().setVisible(true); + } + }); + } + + // Variables declaration - do not modify//GEN-BEGIN:variables + private AirBomberPackage.CanvasMy airBomberCanvas; + private javax.swing.JButton createAirBomberButton; + private javax.swing.JButton downButton; + private javax.swing.JButton leftButton; + private javax.swing.JButton rightButton; + private javax.swing.JButton upButton; + // End of variables declaration//GEN-END:variables +} diff --git a/AirBomber/src/AirBomberPackage/JFrameMapWithSetAirBombers.form b/AirBomber/src/AirBomberPackage/JFrameMapWithSetAirBombers.form new file mode 100644 index 0000000..2abbdcc --- /dev/null +++ b/AirBomber/src/AirBomberPackage/JFrameMapWithSetAirBombers.form @@ -0,0 +1,247 @@ + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/AirBomber/src/AirBomberPackage/JFrameMapWithSetAirBombers.java b/AirBomber/src/AirBomberPackage/JFrameMapWithSetAirBombers.java new file mode 100644 index 0000000..8d153de --- /dev/null +++ b/AirBomber/src/AirBomberPackage/JFrameMapWithSetAirBombers.java @@ -0,0 +1,355 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/GUIForms/JFrame.java to edit this template + */ +package AirBomberPackage; +import java.awt.*; +import javax.swing.*; +/** + * + * @author Андрей + */ +public class JFrameMapWithSetAirBombers extends javax.swing.JFrame { + + /** + * Creates new form JFrameMapWithSetAirBombers + */ + public JFrameMapWithSetAirBombers() { + initComponents(); + } + private MapWithSetAirBombersGeneric _mapAirBombersCollectionGeneric; + + /** + * This method is called from within the constructor to initialize the form. + * WARNING: Do NOT modify this code. The content of this method is always + * regenerated by the Form Editor. + */ + @SuppressWarnings("unchecked") + // //GEN-BEGIN:initComponents + private void initComponents() { + + airBomberCanvas = new AirBomberPackage.CanvasMy(); + comboBoxSelectorMap = new javax.swing.JComboBox<>(); + buttonAddAirBomber = new javax.swing.JButton(); + maskedTextBoxPosition = new javax.swing.JFormattedTextField(); + buttonRemoveAirBomber = new javax.swing.JButton(); + buttonShowStorage = new javax.swing.JButton(); + buttonShowOnMap = new javax.swing.JButton(); + leftButton = new javax.swing.JButton(); + downButton = new javax.swing.JButton(); + rightButton = new javax.swing.JButton(); + upButton = new javax.swing.JButton(); + jLabel1 = new javax.swing.JLabel(); + + setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); + + comboBoxSelectorMap.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Простая карта", "Городская карта", "Линейная карта" })); + comboBoxSelectorMap.setSelectedIndex(-1); + comboBoxSelectorMap.addItemListener(new java.awt.event.ItemListener() { + public void itemStateChanged(java.awt.event.ItemEvent evt) { + comboBoxSelectorMapItemStateChanged(evt); + } + }); + + buttonAddAirBomber.setText("Добавить бомбардировщик"); + buttonAddAirBomber.setToolTipText(""); + buttonAddAirBomber.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + buttonAddAirBomberActionPerformed(evt); + } + }); + + try { + maskedTextBoxPosition.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##"))); + } catch (java.text.ParseException ex) { + ex.printStackTrace(); + } + + buttonRemoveAirBomber.setText("Удалить бомбардировщик"); + buttonRemoveAirBomber.setToolTipText(""); + buttonRemoveAirBomber.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + buttonRemoveAirBomberActionPerformed(evt); + } + }); + + buttonShowStorage.setText("Посмотреть хранилище"); + buttonShowStorage.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + buttonShowStorageActionPerformed(evt); + } + }); + + buttonShowOnMap.setText("Посмотреть карту"); + buttonShowOnMap.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + buttonShowOnMapActionPerformed(evt); + } + }); + + leftButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/AirBomberPackage/arrowLeft.png"))); // NOI18N + leftButton.setMargin(new java.awt.Insets(0, 0, 0, 0)); + leftButton.setMaximumSize(new java.awt.Dimension(30, 30)); + leftButton.setMinimumSize(new java.awt.Dimension(30, 30)); + leftButton.setName("buttonLeft"); // NOI18N + leftButton.setPreferredSize(new java.awt.Dimension(30, 30)); + leftButton.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + moveButtonActionPerformed(evt); + } + }); + + downButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/AirBomberPackage/arrowDown.png"))); // NOI18N + downButton.setMaximumSize(new java.awt.Dimension(30, 30)); + downButton.setMinimumSize(new java.awt.Dimension(30, 30)); + downButton.setName("buttonDown"); // NOI18N + downButton.setPreferredSize(new java.awt.Dimension(30, 30)); + downButton.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + moveButtonActionPerformed(evt); + } + }); + + rightButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/AirBomberPackage/arrowRight.png"))); // NOI18N + rightButton.setMaximumSize(new java.awt.Dimension(30, 30)); + rightButton.setMinimumSize(new java.awt.Dimension(30, 30)); + rightButton.setName("buttonRight"); // NOI18N + rightButton.setPreferredSize(new java.awt.Dimension(30, 30)); + rightButton.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + moveButtonActionPerformed(evt); + } + }); + + upButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/AirBomberPackage/arrowUp.png"))); // NOI18N + upButton.setMaximumSize(new java.awt.Dimension(30, 30)); + upButton.setMinimumSize(new java.awt.Dimension(30, 30)); + upButton.setName("buttonUp"); // NOI18N + upButton.setPreferredSize(new java.awt.Dimension(30, 30)); + upButton.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + moveButtonActionPerformed(evt); + } + }); + + jLabel1.setText("Инструменты"); + + javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); + getContentPane().setLayout(layout); + layout.setHorizontalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addComponent(airBomberCanvas, javax.swing.GroupLayout.PREFERRED_SIZE, 664, javax.swing.GroupLayout.PREFERRED_SIZE) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 25, Short.MAX_VALUE) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) + .addComponent(comboBoxSelectorMap, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addComponent(buttonAddAirBomber, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addComponent(maskedTextBoxPosition) + .addComponent(buttonRemoveAirBomber, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addComponent(buttonShowStorage, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addComponent(buttonShowOnMap, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) + .addComponent(jLabel1)) + .addGap(23, 23, 23)) + .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() + .addComponent(leftButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addComponent(upButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) + .addGroup(layout.createSequentialGroup() + .addComponent(downButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(rightButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))) + .addGap(62, 62, 62)))) + ); + layout.setVerticalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addComponent(airBomberCanvas, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addGroup(layout.createSequentialGroup() + .addGap(15, 15, 15) + .addComponent(jLabel1) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(comboBoxSelectorMap, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) + .addGap(60, 60, 60) + .addComponent(buttonAddAirBomber) + .addGap(38, 38, 38) + .addComponent(maskedTextBoxPosition, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) + .addGap(18, 18, 18) + .addComponent(buttonRemoveAirBomber) + .addGap(96, 96, 96) + .addComponent(buttonShowStorage) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 12, Short.MAX_VALUE) + .addComponent(buttonShowOnMap) + .addGap(108, 108, 108) + .addComponent(upButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) + .addComponent(downButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) + .addComponent(leftButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) + .addComponent(rightButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) + .addContainerGap()) + ); + + pack(); + }// //GEN-END:initComponents + + private void moveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_moveButtonActionPerformed + String name = ((JButton) evt.getSource()).getName(); + Direction dir = Direction.NONE; + switch (name) + { + case "buttonUp": + dir = Direction.UP; + break; + case "buttonDown": + dir = Direction.DOWN; + break; + case "buttonLeft": + dir = Direction.LEFT; + break; + case "buttonRight": + dir = Direction.RIGHT; + break; + } + airBomberCanvas.getGraphics().drawImage(_mapAirBombersCollectionGeneric.MoveObject(dir), 0, 0, null); + }//GEN-LAST:event_moveButtonActionPerformed + + private void comboBoxSelectorMapItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_comboBoxSelectorMapItemStateChanged + AbstractMap map = null; + switch (comboBoxSelectorMap.getSelectedIndex()) + { + case 0: + map = new SimpleMap(); + break; + case 1: + map = new CityMap(); + break; + case 2: + map = new LineMap(); + break; + } + if (map != null) + { + _mapAirBombersCollectionGeneric = new MapWithSetAirBombersGeneric( + airBomberCanvas.getWidth(), airBomberCanvas.getHeight(), map); + airBomberCanvas.getGraphics().drawImage(_mapAirBombersCollectionGeneric.ShowSet(), 0, 0, null); + } + else + { + _mapAirBombersCollectionGeneric = null; + } + }//GEN-LAST:event_comboBoxSelectorMapItemStateChanged + + private void buttonAddAirBomberActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonAddAirBomberActionPerformed + if (_mapAirBombersCollectionGeneric == null) + { + return; + } + + JFrameAirBomber form = new JFrameAirBomber(this); + DrawingAirBomber selectedAirBomber = form.run(); + if (selectedAirBomber != null) + { + DrawingObjectAirBomber objectAirBomber = new DrawingObjectAirBomber(selectedAirBomber); + if (_mapAirBombersCollectionGeneric.add(objectAirBomber) != -1) + { + JOptionPane.showMessageDialog(this, "Объект добавлен"); + airBomberCanvas.getGraphics().drawImage(_mapAirBombersCollectionGeneric.ShowSet(), 0, 0, null); + } + else + { + JOptionPane.showMessageDialog(this, "Не удалось добавить объект"); + } + } + }//GEN-LAST:event_buttonAddAirBomberActionPerformed + + private void buttonRemoveAirBomberActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonRemoveAirBomberActionPerformed + if (maskedTextBoxPosition.getText().isBlank() || maskedTextBoxPosition.getText().isEmpty()) + { + return; + } + var res = JOptionPane.showConfirmDialog((Component) this, (Object) "Удалить объект?", "Удаление", JOptionPane.OK_CANCEL_OPTION); + if (res == JOptionPane.CANCEL_OPTION) + { + return; + } + int pos = Integer.parseInt(maskedTextBoxPosition.getText()); + if (_mapAirBombersCollectionGeneric.remove(pos) != null) + { + JOptionPane.showMessageDialog(null,"Объект удален"); + airBomberCanvas.getGraphics().drawImage(_mapAirBombersCollectionGeneric.ShowSet(), 0, 0, null); + } + else + { + JOptionPane.showMessageDialog(null,"Не удалось удалить объект!"); + } + }//GEN-LAST:event_buttonRemoveAirBomberActionPerformed + + private void buttonShowStorageActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonShowStorageActionPerformed + if (_mapAirBombersCollectionGeneric == null) + { + return; + } + airBomberCanvas.getGraphics().drawImage(_mapAirBombersCollectionGeneric.ShowSet(), 0, 0, null); + }//GEN-LAST:event_buttonShowStorageActionPerformed + + private void buttonShowOnMapActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonShowOnMapActionPerformed + if (_mapAirBombersCollectionGeneric == null) + { + return; + } + airBomberCanvas.getGraphics().drawImage(_mapAirBombersCollectionGeneric.ShowOnMap(), 0, 0, null); + }//GEN-LAST:event_buttonShowOnMapActionPerformed + + /** + * @param args the command line arguments + */ + public static void main(String args[]) { + /* Set the Nimbus look and feel */ + // + /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. + * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html + */ + try { + for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { + if ("Nimbus".equals(info.getName())) { + javax.swing.UIManager.setLookAndFeel(info.getClassName()); + break; + } + } + } catch (ClassNotFoundException ex) { + java.util.logging.Logger.getLogger(JFrameMapWithSetAirBombers.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } catch (InstantiationException ex) { + java.util.logging.Logger.getLogger(JFrameMapWithSetAirBombers.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } catch (IllegalAccessException ex) { + java.util.logging.Logger.getLogger(JFrameMapWithSetAirBombers.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } catch (javax.swing.UnsupportedLookAndFeelException ex) { + java.util.logging.Logger.getLogger(JFrameMapWithSetAirBombers.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } + // + + /* Create and display the form */ + java.awt.EventQueue.invokeLater(new Runnable() { + public void run() { + new JFrameMapWithSetAirBombers().setVisible(true); + } + }); + } + + // Variables declaration - do not modify//GEN-BEGIN:variables + private AirBomberPackage.CanvasMy airBomberCanvas; + private javax.swing.JButton buttonAddAirBomber; + private javax.swing.JButton buttonRemoveAirBomber; + private javax.swing.JButton buttonShowOnMap; + private javax.swing.JButton buttonShowStorage; + private javax.swing.JComboBox comboBoxSelectorMap; + private javax.swing.JButton downButton; + private javax.swing.JLabel jLabel1; + private javax.swing.JButton leftButton; + private javax.swing.JFormattedTextField maskedTextBoxPosition; + private javax.swing.JButton rightButton; + private javax.swing.JButton upButton; + // End of variables declaration//GEN-END:variables +} diff --git a/AirBomber/src/AirBomberPackage/MapWithSetAirBombersGeneric.java b/AirBomber/src/AirBomberPackage/MapWithSetAirBombersGeneric.java new file mode 100644 index 0000000..71e788d --- /dev/null +++ b/AirBomber/src/AirBomberPackage/MapWithSetAirBombersGeneric.java @@ -0,0 +1,123 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template + */ +package AirBomberPackage; +import java.awt.*; +import java.awt.image.BufferedImage; +/** + * + * @author Андрей + */ +public class MapWithSetAirBombersGeneric { + private int _pictureWidth; + private int _pictureHeight; + private int _placeSizeWidth = 110; + private int _placeSizeHeight = 100; + private SetAirBombersGeneric _setAirBombers; + private U _map; + public MapWithSetAirBombersGeneric(int picWidth, int picHeight, U map) + { + int width = picWidth / _placeSizeWidth; + int height = picHeight / _placeSizeHeight; + _setAirBombers = new SetAirBombersGeneric(width * height); + _pictureWidth = picWidth; + _pictureHeight = picHeight; + _map = map; + } + + public int add(T airBomber){ + return _setAirBombers.Insert(airBomber); + } + + public T remove(int position){ + return _setAirBombers.Remove(position); + } + + public BufferedImage ShowSet() + { + BufferedImage bmp = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_ARGB); + Graphics2D gr = bmp.createGraphics(); + DrawBackground(gr); + DrawAirBombers(gr); + return bmp; + } + + public BufferedImage ShowOnMap() + { + Shaking(); + for (int i = 0; i < _setAirBombers.Count; i++) + { + var airBomber = _setAirBombers.Get(i); + if (airBomber != null) + { + return _map.CreateMap(700, 700, airBomber); + } + } + return new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_ARGB); + } + + public BufferedImage MoveObject(Direction direction) + { + if (_map != null) + { + return _map.MoveObject(direction); + } + return new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_ARGB); + } + + private void Shaking() + { + int j = _setAirBombers.Count - 1; + for (int i = 0; i < _setAirBombers.Count; i++) + { + if (_setAirBombers.Get(i) == null) + { + for (; j > i; j--) + { + var car = _setAirBombers.Get(j); + if (car != null) + { + _setAirBombers.Insert(car, i); + _setAirBombers.Remove(j); + break; + } + } + if (j <= i) + { + return; + } + } + } + } + + private void DrawBackground(Graphics2D g) + { + g.setColor(Color.GRAY); + g.fillRect(0, 0, _pictureWidth, _pictureHeight); + g.setColor(Color.WHITE); + g.setStroke(new BasicStroke(3)); + 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); + } + g.setStroke(new BasicStroke(1)); + g.setColor(Color.BLACK); + } + + private void DrawAirBombers(Graphics2D g) + { + int numOfObjectsInRow = _pictureWidth / _placeSizeWidth; + for (int i = 0; i < _setAirBombers.Count; i++) + { + if (_setAirBombers.Get(i) != null){ + _setAirBombers.Get(i).SetObject((numOfObjectsInRow - (i % numOfObjectsInRow) - 1) * _placeSizeWidth, (i / numOfObjectsInRow) * _placeSizeHeight, _pictureWidth, _pictureHeight); + _setAirBombers.Get(i).DrawingObject(g); + } + } + } +} diff --git a/AirBomber/src/AirBomberPackage/SetAirBombersGeneric.java b/AirBomber/src/AirBomberPackage/SetAirBombersGeneric.java new file mode 100644 index 0000000..b425ac4 --- /dev/null +++ b/AirBomber/src/AirBomberPackage/SetAirBombersGeneric.java @@ -0,0 +1,100 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template + */ +package AirBomberPackage; + +/** + * + * @author Андрей + */ +public class SetAirBombersGeneric { + /// + /// Массив объектов, которые храним + /// + private T[] _places; + /// + /// Количество объектов в массиве + /// + public int Count; + /// + /// Конструктор + /// + /// + public SetAirBombersGeneric(int count) + { + _places = (T[]) new Object[count]; + Count = count; + } + /// + /// Добавление объекта в набор + /// + /// Добавляемый автомобиль + /// + public int Insert(T airBomber) + { + return Insert(airBomber, 0); + } + /// + /// Добавление объекта в набор на конкретную позицию + /// + /// Добавляемый автомобиль + /// Позиция + /// + public int Insert(T airBomber, int position) + { + if (position >= _places.length) + { + return -1; + } + if (_places[position] != null) + { + int indexNull = -1; + for (int i = position; i < _places.length; i++) + { + if (_places[i] == null) + { + indexNull = i; + break; + } + } + if (indexNull == -1) return -1; + for (int i = indexNull; i > position; i--) + { + T tmp = _places[i]; + _places[i] = _places[i - 1]; + _places[i - 1] = tmp; + } + } + _places[position] = airBomber; + return position; + } + /// + /// Удаление объекта из набора с конкретной позиции + /// + /// + /// + public T Remove(int position) + { + if (position >= _places.length) + { + return null; + } + T removedObject = _places[position]; + _places[position] = null; + return removedObject; + } + /// + /// Получение объекта из набора по позиции + /// + /// + /// + public T Get(int position) + { + if (position >= _places.length) + { + return null; + } + return _places[position]; + } +} diff --git a/AirBomber/src/AirBomberPackage/SetAirBombersGenericDop.java b/AirBomber/src/AirBomberPackage/SetAirBombersGenericDop.java new file mode 100644 index 0000000..4bb8908 --- /dev/null +++ b/AirBomber/src/AirBomberPackage/SetAirBombersGenericDop.java @@ -0,0 +1,99 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template + */ +package AirBomberPackage; +import java.util.Random; +/** + * + * @author Андрей + */ +public class SetAirBombersGenericDop { + private Object[] _bases; + private Object[] _engines; + private int Count; + + public SetAirBombersGenericDop(int count){ + _bases = new Object[count]; + _engines = new Object[count]; + Count = count; + } + + public int add(T base){ + return add(base, 0); + } + + public int add(T base, int position){ + if (position >= _bases.length) + { + return -1; + } + if (_bases[position] != null) + { + int indexNull = -1; + for (int i = position; i < _bases.length; i++) + { + if (_bases[i] == null) + { + indexNull = i; + break; + } + } + if (indexNull == -1) return -1; + for (int i = indexNull; i > position; i--) + { + T tmp = (T) _bases[i]; + _bases[i] = _bases[i - 1]; + _bases[i - 1] = tmp; + } + } + _bases[position] = base; + return position; + } + + public int add(U engine){ + return add(engine, 0); + } + + public int add(U engine, int position){ + if (position >= _engines.length) + { + return -1; + } + if (_engines[position] != null) + { + int indexNull = -1; + for (int i = position; i < _engines.length; i++) + { + if (_engines[i] == null) + { + indexNull = i; + break; + } + } + if (indexNull == -1) return -1; + for (int i = indexNull; i > position; i--) + { + U tmp = (U) _engines[i]; + _engines[i] = _engines[i - 1]; + _engines[i - 1] = tmp; + } + } + _engines[position] = engine; + return position; + } + + + public DrawingAirBomber buildAirBomber(){ + Random rnd = new Random(); + int baseIndex = rnd.nextInt(0, _bases.length); + int engineIndex = rnd.nextInt(0, _engines.length); + EnginesType enginesType; + T selectedBase = (T) _bases[baseIndex]; + U selectedEngines = (U) _engines[engineIndex]; + if (selectedEngines instanceof DrawingEngines) enginesType = EnginesType.RECTANGLE; + else if (selectedEngines instanceof DrawingEnginesTriangle) enginesType = EnginesType.TRIANGLE; + else enginesType = EnginesType.OVAL; + return new DrawingAirBomber(selectedBase.getSpeed(), selectedBase.getWeight(), selectedBase.getBodyColor(), selectedEngines.getNumberOfEngines(), enginesType); + } +} -- 2.25.1