From 66f961be08a9ee5da7eef9a5e366ed2421f893e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9A=D0=B0=D1=88=D0=B8=D0=BD=20=D0=9C=D0=B0=D0=BA=D1=81?= =?UTF-8?q?=D0=B8=D0=BC?= Date: Sun, 30 Oct 2022 01:31:33 +0400 Subject: [PATCH 1/4] LabWork03 --- src/CreaterGeneric.java | 37 +++ src/DrawingField.java | 3 + src/DrawingGasolineTanker.java | 10 + src/DrawingImprovedGasolineTanker.java | 10 + src/DrawingMap.java | 71 ------ src/FormCreater.form | 230 ++++++++++++++++++ src/FormCreater.java | 134 ++++++++++ src/FormGasolineTanker.form | 28 ++- src/FormGasolineTanker.java | 7 + src/FormMap.java | 87 ------- ...form => FormMapWithSetGasolineTanker.form} | 225 +++++++++-------- src/FormMapWithSetGasolineTanker.java | 177 ++++++++++++++ src/Main.java | 2 +- src/MapWithSetGasolineTankerGeneric.java | 125 ++++++++++ src/SetGasolineTankerGeneric.java | 68 ++++++ 15 files changed, 937 insertions(+), 277 deletions(-) create mode 100644 src/CreaterGeneric.java delete mode 100644 src/DrawingMap.java create mode 100644 src/FormCreater.form create mode 100644 src/FormCreater.java delete mode 100644 src/FormMap.java rename src/{FormMap.form => FormMapWithSetGasolineTanker.form} (50%) create mode 100644 src/FormMapWithSetGasolineTanker.java create mode 100644 src/MapWithSetGasolineTankerGeneric.java create mode 100644 src/SetGasolineTankerGeneric.java diff --git a/src/CreaterGeneric.java b/src/CreaterGeneric.java new file mode 100644 index 0000000..fb110c6 --- /dev/null +++ b/src/CreaterGeneric.java @@ -0,0 +1,37 @@ +import java.util.Random; +public class CreaterGeneric { + Object[] GasolineTanker; + Object[] Wheels; + int GasolineTankerCount=0; + int WheelsCount=0; + public CreaterGeneric(int gasolineTankerCount,int wheelsCount){ + GasolineTanker=new Object[gasolineTankerCount]; + Wheels=new Object[wheelsCount]; + } + public int AddGasolineTanker(T gasolineTanker){ + if(GasolineTankerCount +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/src/FormCreater.java b/src/FormCreater.java new file mode 100644 index 0000000..b62c267 --- /dev/null +++ b/src/FormCreater.java @@ -0,0 +1,134 @@ +import javax.swing.*; +import java.awt.*; +import java.util.Random; + +public class FormCreater extends JDialog{ + private JButton ChooseButton; + private JButton ShowGasolineTankerButton; + private JTextField SpeedTextField; + private JTextField WeightTextField; + private JCheckBox BodyKitCheckBox; + private JCheckBox AntennaCheckBox; + private JRadioButton BasicRadioButton; + private JRadioButton AdvancedRadioButton; + private JRadioButton TwoRadioButton; + private JRadioButton ThreeRadioButton; + private JRadioButton FourRadioButton; + private JRadioButton NotOrnament; + private JRadioButton RedOrnament; + private JRadioButton GrayOrnament; + private JLabel SetSpeedLabel; + private JLabel SetWeightLabel; + private JPanel PictureBox; + CountWheels wheels=null; + IDrawningObjectWheels fwheels=null; + private final CreaterGeneric createrGeneric=new CreaterGeneric<>(40,40); + private DrawingGasolineTanker _gasolineTanker; + private DrawingGasolineTanker selectedGasolineTanker; + + ImageIcon spriteUp =new ImageIcon((new ImageIcon("Material\\KeyUp.png")). + getImage().getScaledInstance(10,10,Image.SCALE_SMOOTH)); + ImageIcon spriteDown =new ImageIcon((new ImageIcon("Material\\KeyDown.png")). + getImage().getScaledInstance(10,10,Image.SCALE_SMOOTH)); + ImageIcon spriteLeft =new ImageIcon((new ImageIcon("Material\\KeyLeft.png")). + getImage().getScaledInstance(10,10,Image.SCALE_SMOOTH)); + ImageIcon spriteRight =new ImageIcon((new ImageIcon("Material\\KeyRight.png")). + getImage().getScaledInstance(10,10,Image.SCALE_SMOOTH)); + public FormCreater(){ + setTitle("Gasoline tanker"); + setContentPane(PictureBox); + setResizable(false); + setSize(1000,500); + ShowWindow(); + setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); + } + + @Override + public void paint(Graphics g) { + super.paint(g); + Graphics2D g2d = (Graphics2D) PictureBox.getGraphics(); + if (_gasolineTanker != null) { + _gasolineTanker.DrawTransport(g2d); + } + } + private void ShowWindow(){ + + TwoRadioButton.addActionListener(e -> { + wheels=CountWheels.Two; + }); + ThreeRadioButton.addActionListener(e -> { + wheels=CountWheels.Three; + }); + FourRadioButton.addActionListener(e -> { + wheels=CountWheels.Four; + }); + + NotOrnament.addActionListener(e -> { + if(wheels==null){ + return; + } + fwheels=new DrawingWheels(wheels); + if(fwheels==null) + return; + fwheels.SetCountWheels(wheels.getCountWheels()); + createrGeneric.AddWheels(fwheels); + }); + + RedOrnament.addActionListener(e -> { + if(wheels==null){ + return; + } + fwheels=new DrawingOrnamentWheelsSecond(wheels); + if(fwheels==null) + return; + fwheels.SetCountWheels(wheels.getCountWheels()); + createrGeneric.AddWheels(fwheels); + }); + + GrayOrnament.addActionListener(e -> { + if(wheels==null){ + return; + } + fwheels=new DrawingOrnamentWheelsFirst(wheels); + if(fwheels==null) + return; + fwheels.SetCountWheels(wheels.getCountWheels()); + createrGeneric.AddWheels(fwheels); + }); + + BasicRadioButton.addActionListener(e -> { + Color color=JColorChooser.showDialog(this,"Select body color",Color.WHITE); + if(Integer.parseInt(SpeedTextField.getText())==0 || Integer.parseInt(WeightTextField.getText())==0 || color==null){ + return; + } + createrGeneric.AddGasolineTanker(new EntityGasolineTanker(Integer.parseInt(SpeedTextField.getText()),Integer.parseInt(WeightTextField.getText()),color)); + }); + + AdvancedRadioButton.addActionListener(e -> { + Color color1=JColorChooser.showDialog(this,"Select body color",Color.WHITE); + if(Integer.parseInt(SpeedTextField.getText())==0 || Integer.parseInt(WeightTextField.getText())==0 || color1==null){ + return; + } + Color color2=JColorChooser.showDialog(this,"Select modification color",Color.WHITE); + if(color2==null){ + return; + } + createrGeneric.AddGasolineTanker(new EntityImprovedGasolineTanker(Integer.parseInt(SpeedTextField.getText()),Integer.parseInt(WeightTextField.getText()), + color1,color2, BodyKitCheckBox.isSelected(),AntennaCheckBox.isSelected())); + }); + + ShowGasolineTankerButton.addActionListener(e -> { + Random rand=new Random(); + _gasolineTanker=createrGeneric.NewGasolineTankerCreating(); + _gasolineTanker.SetPosition(rand.nextInt(100),rand.nextInt(100),getWidth(),getHeight()); + repaint(); + }); + ChooseButton.addActionListener(e -> { + selectedGasolineTanker=_gasolineTanker; + dispose(); + }); + } + public DrawingGasolineTanker getSelectedGasolineTanker() { + return selectedGasolineTanker; + } +} diff --git a/src/FormGasolineTanker.form b/src/FormGasolineTanker.form index 2c51dc1..47a580b 100644 --- a/src/FormGasolineTanker.form +++ b/src/FormGasolineTanker.form @@ -1,9 +1,9 @@
- + - + @@ -11,7 +11,7 @@ - + @@ -76,7 +76,7 @@ - + @@ -128,7 +128,7 @@ - + @@ -155,6 +155,24 @@ + + + + + + + + + + + + + + + + + + diff --git a/src/FormGasolineTanker.java b/src/FormGasolineTanker.java index 079ba06..04e1085 100644 --- a/src/FormGasolineTanker.java +++ b/src/FormGasolineTanker.java @@ -7,6 +7,7 @@ public class FormGasolineTanker extends JFrame{ private int Width; private int Height; DrawingField field = new DrawingField(this); + DrawingGasolineTanker SelectedGasolineTanker; private JButton ButtonDown; private JButton ButtonRight; private JButton ButtonLeft; @@ -17,6 +18,7 @@ public class FormGasolineTanker extends JFrame{ public JLabel WeightLabel; public JLabel BodyColorLabel; private JPanel PictureBox; + private JButton ButtonSelect; public FormGasolineTanker(){ super("Gasoline Tanker"); @@ -67,6 +69,10 @@ public class FormGasolineTanker extends JFrame{ field.DirectionButtonAction(Direction.Down); ReDraw(); }); + ButtonSelect.addActionListener(e -> { + SelectedGasolineTanker=field.GetDrawingGasolineTanker(); + JOptionPane.showMessageDialog(PictureBox, "Корабль добавлен."); + }); addComponentListener(new ComponentAdapter() { @Override @@ -88,4 +94,5 @@ public class FormGasolineTanker extends JFrame{ PictureBox.paintComponents(graphics); field.Draw(graphics); } + } diff --git a/src/FormMap.java b/src/FormMap.java deleted file mode 100644 index accc5f3..0000000 --- a/src/FormMap.java +++ /dev/null @@ -1,87 +0,0 @@ -import javax.swing.*; -import java.awt.*; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; -import java.awt.event.ComponentAdapter; -import java.awt.event.ComponentEvent; -import java.util.Objects; - -public class FormMap extends JFrame { - private int Width; - private int Height; - DrawingMap map = new DrawingMap(this); - private JButton ButtonDown; - private JButton ButtonRight; - private JButton ButtonLeft; - private JButton ButtonUp; - private JButton ButtonCreate; - private JButton ButtonCreateModif; - public JLabel SpeedLabel; - public JLabel WeightLabel; - public JLabel BodyColorLabel; - private JComboBox ComboBoxSelectorMap; - private JPanel PictureBox; - ImageIcon spriteUp =new ImageIcon((new ImageIcon("Material\\KeyUp.png")). - getImage().getScaledInstance(10,10,Image.SCALE_SMOOTH)); - ImageIcon spriteDown =new ImageIcon((new ImageIcon("Material\\KeyDown.png")). - getImage().getScaledInstance(10,10,Image.SCALE_SMOOTH)); - ImageIcon spriteLeft =new ImageIcon((new ImageIcon("Material\\KeyLeft.png")). - getImage().getScaledInstance(10,10,Image.SCALE_SMOOTH)); - ImageIcon spriteRight =new ImageIcon((new ImageIcon("Material\\KeyRight.png")). - getImage().getScaledInstance(10,10,Image.SCALE_SMOOTH)); - public FormMap(){ - super("Gasoline tanker"); - setContentPane(PictureBox); - setSize(1000,700); - Width = getWidth(); - Height = getHeight(); - ShowWindow(); - map.setBounds(0,0,Width,Height); - setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - setVisible(true); - } - - private void ShowWindow(){ - ButtonCreate.addActionListener(e -> { - map.CreateButtonAction(); - ReDraw(); - }); - ButtonCreateModif.addActionListener(e -> { - map.CreateModifButtonAction(); - ReDraw(); - }); - ButtonUp.setIcon(spriteUp); - ButtonUp.addActionListener(e -> { - map.DirectionButtonAction(Direction.Up); - ReDraw(); - }); - ButtonLeft.setIcon(spriteLeft); - ButtonLeft.addActionListener(e -> { - map.DirectionButtonAction(Direction.Left); - ReDraw(); - }); - ButtonRight.setIcon(spriteRight); - ButtonRight.addActionListener(e -> { - map.DirectionButtonAction(Direction.Right); - ReDraw(); - }); - ButtonDown.setIcon(spriteDown); - ButtonDown.addActionListener(e -> { - map.DirectionButtonAction(Direction.Down); - ReDraw(); - }); - - ComboBoxSelectorMap.addActionListener(e -> { - map.ComboBoxSelectorMapAction(Objects.requireNonNull(ComboBoxSelectorMap.getSelectedItem()).toString()); - ReDraw(); - }); - } - private void ReDraw() - { - Graphics2D graphics = (Graphics2D) PictureBox.getGraphics(); - graphics.clearRect(0, 0, PictureBox.getWidth(), PictureBox.getHeight()); - PictureBox.paintComponents(graphics); - map.DrawMap(graphics); - } - -} diff --git a/src/FormMap.form b/src/FormMapWithSetGasolineTanker.form similarity index 50% rename from src/FormMap.form rename to src/FormMapWithSetGasolineTanker.form index 8826466..30f7091 100644 --- a/src/FormMap.form +++ b/src/FormMapWithSetGasolineTanker.form @@ -1,111 +1,108 @@ -
- + + - + - + - + - + - + + - + + + + + + + + + + + + + + + - + + + + - + - + - - + + - + - + - + - + + + + + + + + + - + + + + + + + + + + + + + + + + + + + + + + - + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + @@ -113,59 +110,61 @@ + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - + - + - - - - - - - + - - + - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + diff --git a/src/FormMapWithSetGasolineTanker.java b/src/FormMapWithSetGasolineTanker.java new file mode 100644 index 0000000..fd26dc7 --- /dev/null +++ b/src/FormMapWithSetGasolineTanker.java @@ -0,0 +1,177 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.ComponentAdapter; +import java.awt.event.ComponentEvent; +import java.util.Objects; + +public class FormMapWithSetGasolineTanker extends JFrame{ + private JPanel GroupBoxTools; + private JComboBox СomboBoxSelectorMap; + private JButton ButtonAddGasolineTanker; + private JButton ButtonRemoveGasolineTanker; + private JButton ButtonShowStorage; + private JButton ButtonShowOnMap; + private JButton ButtonLeft; + private JButton ButtonDown; + private JButton ButtonRight; + private JButton ButtonUp; + private JPanel MainPanel; + private JTextField TextBoxPosition; + private JPanel PictureBox; + private Image bufferedImage; + + private MapWithSetGasolineTankerGeneric _mapGasolineTankerCollectionGeneric; + + ImageIcon spriteUp =new ImageIcon((new ImageIcon("Material\\KeyUp.png")). + getImage().getScaledInstance(10,10,Image.SCALE_SMOOTH)); + ImageIcon spriteDown =new ImageIcon((new ImageIcon("Material\\KeyDown.png")). + getImage().getScaledInstance(10,10,Image.SCALE_SMOOTH)); + ImageIcon spriteLeft =new ImageIcon((new ImageIcon("Material\\KeyLeft.png")). + getImage().getScaledInstance(10,10,Image.SCALE_SMOOTH)); + ImageIcon spriteRight =new ImageIcon((new ImageIcon("Material\\KeyRight.png")). + getImage().getScaledInstance(10,10,Image.SCALE_SMOOTH)); + public FormMapWithSetGasolineTanker(){ + setTitle("Gasoline tanker"); + setContentPane(MainPanel); + setResizable(false); + setSize(1000,685); + ShowWindow(); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setVisible(true); + } + + @Override + public void paint(Graphics g) { + super.paint(g); + + if (bufferedImage != null) { + PictureBox.paintComponents(bufferedImage.getGraphics()); + PictureBox.getGraphics().drawImage(bufferedImage, 0, 0, null); + } + } + + private void ShowWindow(){ + + ButtonShowOnMap.addActionListener(e -> { + if (_mapGasolineTankerCollectionGeneric == null) + { + return; + } + bufferedImage = _mapGasolineTankerCollectionGeneric.ShowOnMap(); + repaint(); + }); + + ButtonShowStorage.addActionListener(e -> { + if (_mapGasolineTankerCollectionGeneric == null) + { + return; + } + bufferedImage = _mapGasolineTankerCollectionGeneric.ShowSet(); + repaint(); + }); + + ButtonAddGasolineTanker.addActionListener(e -> { + if (_mapGasolineTankerCollectionGeneric == null) + { + return; + } + FormCreater dialog=new FormCreater(); + dialog.setSize(1200,700); + dialog.setModalityType(Dialog.ModalityType.APPLICATION_MODAL); + dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); + dialog.setVisible(true); + if (dialog.getSelectedGasolineTanker()!=null) { + DrawingObjectGasolineTanker gasolineTanker = new DrawingObjectGasolineTanker(dialog.getSelectedGasolineTanker()); + + if (_mapGasolineTankerCollectionGeneric.plus(gasolineTanker) >= 0) { + JOptionPane.showMessageDialog(this, "Объект добавлен", "Успех", JOptionPane.INFORMATION_MESSAGE); + bufferedImage = _mapGasolineTankerCollectionGeneric.ShowSet(); + repaint(); + } else { + JOptionPane.showMessageDialog(this, "Не удалось добавить объект", "Ошибка",JOptionPane.INFORMATION_MESSAGE); + } + } + }); + + ButtonRemoveGasolineTanker.addActionListener(e -> { + String txt=TextBoxPosition.getText(); + if (txt==null||_mapGasolineTankerCollectionGeneric==null||txt.isEmpty()) + { + return; + } + int result = JOptionPane.showConfirmDialog( + this, + "Удалить объект?", + "Удаление", + JOptionPane.YES_NO_CANCEL_OPTION); + if (result == JOptionPane.NO_OPTION) + { + return; + } + int pos = Integer.parseInt(txt); + if (_mapGasolineTankerCollectionGeneric.minus(pos) !=null) + { + JOptionPane.showMessageDialog(this,"Объект удален","Успех",JOptionPane.INFORMATION_MESSAGE); + bufferedImage = _mapGasolineTankerCollectionGeneric.ShowSet(); + repaint(); + } + else + { + JOptionPane.showMessageDialog(this,"Не удалось удалить объект","Ошибка",JOptionPane.INFORMATION_MESSAGE); + } + }); + + СomboBoxSelectorMap.addActionListener(e -> { + AbstractMap map = null; + + switch (СomboBoxSelectorMap.getSelectedItem().toString()) + { + case "Simple map": + map = new SimpleMap(); + break; + case "Long map": + map = new LongMap(); + break; + } + if (map != null) + { + _mapGasolineTankerCollectionGeneric = new MapWithSetGasolineTankerGeneric<>( + PictureBox.getWidth(), PictureBox.getHeight(), map); + } + else + { + _mapGasolineTankerCollectionGeneric = null; + } + }); + ButtonUp.setIcon(spriteUp); + ButtonUp.addActionListener(e -> { + if (_mapGasolineTankerCollectionGeneric != null) { + bufferedImage = _mapGasolineTankerCollectionGeneric.MoveObject(Direction.Up); + repaint(); + } + }); + ButtonDown.setIcon(spriteDown); + ButtonDown.addActionListener(e -> { + if (_mapGasolineTankerCollectionGeneric != null) { + bufferedImage = _mapGasolineTankerCollectionGeneric.MoveObject(Direction.Down); + repaint(); + } + }); + ButtonRight.setIcon(spriteRight); + ButtonRight.addActionListener(e -> { + if (_mapGasolineTankerCollectionGeneric != null) { + bufferedImage = _mapGasolineTankerCollectionGeneric.MoveObject(Direction.Right); + repaint(); + } + }); + ButtonLeft.setIcon(spriteLeft); + ButtonLeft.addActionListener(e -> { + if (_mapGasolineTankerCollectionGeneric != null) { + bufferedImage = _mapGasolineTankerCollectionGeneric.MoveObject(Direction.Left); + repaint(); + } + }); + } +} diff --git a/src/Main.java b/src/Main.java index 7b927cf..202d0ac 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,5 +1,5 @@ public class Main { public static void main(String[] args) { - new FormMap(); + new FormMapWithSetGasolineTanker(); } } \ No newline at end of file diff --git a/src/MapWithSetGasolineTankerGeneric.java b/src/MapWithSetGasolineTankerGeneric.java new file mode 100644 index 0000000..effb49a --- /dev/null +++ b/src/MapWithSetGasolineTankerGeneric.java @@ -0,0 +1,125 @@ +import java.awt.*; +import java.awt.image.BufferedImage; + +public class MapWithSetGasolineTankerGeneric { + private final int _pictureWidth; + private final int _pictureHeight; + private final int _placeSizeWidth = 210; + private final int _placeSizeHeight = 90; + private final SetGasolineTankerGeneric _setGasolineTanker; + private final U _map; + + public MapWithSetGasolineTankerGeneric(int picWidth,int picHeight, U map) + { + int width = picWidth / _placeSizeWidth; + int height = picHeight/_placeSizeHeight; + _setGasolineTanker = new SetGasolineTankerGeneric<>(width * height); + _pictureWidth = picWidth; + _pictureHeight = picHeight; + _map = map; + } + + public int plus(T gasolineTanker) + { + return _setGasolineTanker.Insert(gasolineTanker); + } + + public T minus(int position) + { + return _setGasolineTanker.Remove(position); + } + + public Image ShowSet() + { + BufferedImage bmp = new BufferedImage(_pictureWidth, _pictureWidth,BufferedImage.TYPE_INT_ARGB); + Graphics gr = bmp.getGraphics(); + DrawBackground(gr); + DrawGasolineTanker(gr); + return bmp; + } + + public Image ShowOnMap() + { + Shaking(); + for (int i = 0; i < _setGasolineTanker.Count(); i++) + { + T gasolineTanker = _setGasolineTanker.Get(i); + if (gasolineTanker != null) + { + return _map.CreateMap(_pictureWidth, _pictureHeight, gasolineTanker); + } + } + return new BufferedImage(_pictureWidth, _pictureHeight,1); + } + + public Image MoveObject(Direction direction) + { + if (_map != null) + { + return _map.MoveObject(direction); + } + return new BufferedImage(_pictureWidth, _pictureHeight,1); + } + + public void Shaking() + { + int j = _setGasolineTanker.Count() - 1; + for (int i = 0; i < _setGasolineTanker.Count(); i++) + { + if (_setGasolineTanker.Get(i) == null) + { + for (; j > i; j--) + { + T gasolineTanker = _setGasolineTanker.Get(j); + if (gasolineTanker != null) + { + _setGasolineTanker.Insert(gasolineTanker, i); + _setGasolineTanker.Remove(j); + break; + } + } + if (j <= i) + { + return; + } + } + } + } + + private void DrawBackground(Graphics gr) + { + Graphics2D g=(Graphics2D)gr; + + Color brush=Color.BLACK; + Stroke penWide = new BasicStroke(5); + Stroke penThin = new BasicStroke(1); + + for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++) + { + for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j) + { + g.setColor(brush); + g.setStroke(penWide); + g.drawLine(i * _placeSizeWidth + 10, j * _placeSizeHeight + 2, i * _placeSizeWidth + (int)(_placeSizeWidth * 0.8), j * _placeSizeHeight + 2); + g.drawLine(i * _placeSizeWidth + (int)(_placeSizeWidth * 0.8), j * _placeSizeHeight + 2, i * _placeSizeWidth + (int)(_placeSizeWidth * 0.9) + 10, j * _placeSizeHeight + 2 + 20); + g.drawLine(i * _placeSizeWidth + (int)(_placeSizeWidth * 0.9) + 10, j * _placeSizeHeight + 2 + 20, i * _placeSizeWidth + (int)(_placeSizeWidth * 0.9) + 10, j * _placeSizeHeight + 2 + 20); + g.setStroke(penThin); + } + } + } + + private void DrawGasolineTanker(Graphics gr) + { + int width = _pictureWidth / _placeSizeWidth; + int height = _pictureHeight / _placeSizeHeight; + + for (int i = 0; i < _setGasolineTanker.Count(); i++) + { + if (_setGasolineTanker.Get(i) != null) + { + _setGasolineTanker.Get(i).SetObject(i % width * _placeSizeWidth, (height - 1 - i / width) * _placeSizeHeight, _pictureWidth, _pictureHeight); + _setGasolineTanker.Get(i).DrawingObject(gr); + } + } + } +} diff --git a/src/SetGasolineTankerGeneric.java b/src/SetGasolineTankerGeneric.java new file mode 100644 index 0000000..1e55731 --- /dev/null +++ b/src/SetGasolineTankerGeneric.java @@ -0,0 +1,68 @@ +public class SetGasolineTankerGeneric { + private final Object[] _places; + + public int Count() { + return _places.length; + } + + public SetGasolineTankerGeneric(int count) + { + _places = new Object[count]; + } + + public int Insert(T gasolineTanker) + { + return Insert(gasolineTanker,0); + } + + public int Insert(T gasolineTanker, int position) + { + int EmptyEl=-1; + + if (position>=Count() || position < 0) + return -1; + + if (_places[position] == null) + { + _places[position] = gasolineTanker; + return 1; + } + + else if (_places[position] != null) + { + for (int i = position + 1; i < Count(); i++) + if (_places[i] == null) + { + EmptyEl = i; + break; + } + + if (EmptyEl == -1) + return -1; + + for (int i = EmptyEl; i > position; i--) + _places[i] = _places[i - 1]; + } + + _places[position] = gasolineTanker; + return 1; + } + + public T Remove(int position) + { + if (position >= Count() || position < 0 || _places[position]==null) + return null; + + T deleted =(T)_places[position]; + _places[position] = null; + return deleted; + } + + public T Get(int position) + { + if (position >= Count() || position < 0) + return null; + + return (T)_places[position]; + } +} -- 2.25.1 From 2035ffd79d7362088ec5c84f74d773e377247714 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9A=D0=B0=D1=88=D0=B8=D0=BD=20=D0=9C=D0=B0=D0=BA=D1=81?= =?UTF-8?q?=D0=B8=D0=BC?= Date: Tue, 15 Nov 2022 18:15:47 +0400 Subject: [PATCH 2/4] fix object --- src/CreaterGeneric.java | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/src/CreaterGeneric.java b/src/CreaterGeneric.java index fb110c6..0e56006 100644 --- a/src/CreaterGeneric.java +++ b/src/CreaterGeneric.java @@ -1,24 +1,25 @@ import java.util.Random; +import java.util.ArrayList; public class CreaterGeneric { - Object[] GasolineTanker; - Object[] Wheels; + ArrayList GasolineTanker; + ArrayList Wheels; int GasolineTankerCount=0; int WheelsCount=0; public CreaterGeneric(int gasolineTankerCount,int wheelsCount){ - GasolineTanker=new Object[gasolineTankerCount]; - Wheels=new Object[wheelsCount]; + GasolineTanker=new ArrayList<>(gasolineTankerCount); + Wheels=new ArrayList<>(wheelsCount); } public int AddGasolineTanker(T gasolineTanker){ - if(GasolineTankerCount Date: Wed, 16 Nov 2022 10:30:07 +0400 Subject: [PATCH 3/4] fix 03 --- src/FormGasolineTanker.java | 2 +- src/FormMapWithSetGasolineTanker.java | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/FormGasolineTanker.java b/src/FormGasolineTanker.java index 04e1085..adba354 100644 --- a/src/FormGasolineTanker.java +++ b/src/FormGasolineTanker.java @@ -71,7 +71,7 @@ public class FormGasolineTanker extends JFrame{ }); ButtonSelect.addActionListener(e -> { SelectedGasolineTanker=field.GetDrawingGasolineTanker(); - JOptionPane.showMessageDialog(PictureBox, "Корабль добавлен."); + JOptionPane.showMessageDialog(PictureBox, "Gasoline tanker added."); }); addComponentListener(new ComponentAdapter() { diff --git a/src/FormMapWithSetGasolineTanker.java b/src/FormMapWithSetGasolineTanker.java index fd26dc7..e07db24 100644 --- a/src/FormMapWithSetGasolineTanker.java +++ b/src/FormMapWithSetGasolineTanker.java @@ -86,11 +86,11 @@ public class FormMapWithSetGasolineTanker extends JFrame{ DrawingObjectGasolineTanker gasolineTanker = new DrawingObjectGasolineTanker(dialog.getSelectedGasolineTanker()); if (_mapGasolineTankerCollectionGeneric.plus(gasolineTanker) >= 0) { - JOptionPane.showMessageDialog(this, "Объект добавлен", "Успех", JOptionPane.INFORMATION_MESSAGE); + JOptionPane.showMessageDialog(this, "\n" + "Object added", "Success", JOptionPane.INFORMATION_MESSAGE); bufferedImage = _mapGasolineTankerCollectionGeneric.ShowSet(); repaint(); } else { - JOptionPane.showMessageDialog(this, "Не удалось добавить объект", "Ошибка",JOptionPane.INFORMATION_MESSAGE); + JOptionPane.showMessageDialog(this, "Failed to add object", "Error",JOptionPane.INFORMATION_MESSAGE); } } }); @@ -103,8 +103,8 @@ public class FormMapWithSetGasolineTanker extends JFrame{ } int result = JOptionPane.showConfirmDialog( this, - "Удалить объект?", - "Удаление", + "Delete object?", + "Delete", JOptionPane.YES_NO_CANCEL_OPTION); if (result == JOptionPane.NO_OPTION) { @@ -113,13 +113,13 @@ public class FormMapWithSetGasolineTanker extends JFrame{ int pos = Integer.parseInt(txt); if (_mapGasolineTankerCollectionGeneric.minus(pos) !=null) { - JOptionPane.showMessageDialog(this,"Объект удален","Успех",JOptionPane.INFORMATION_MESSAGE); + JOptionPane.showMessageDialog(this,"Object added", "Success",JOptionPane.INFORMATION_MESSAGE); bufferedImage = _mapGasolineTankerCollectionGeneric.ShowSet(); repaint(); } else { - JOptionPane.showMessageDialog(this,"Не удалось удалить объект","Ошибка",JOptionPane.INFORMATION_MESSAGE); + JOptionPane.showMessageDialog(this,"Failed to add object", "Error",JOptionPane.INFORMATION_MESSAGE); } }); -- 2.25.1 From 1fb63dffef921e623d5fd4689de50058623314c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9A=D0=B0=D1=88=D0=B8=D0=BD=20=D0=9C=D0=B0=D0=BA=D1=81?= =?UTF-8?q?=D0=B8=D0=BC?= Date: Wed, 16 Nov 2022 10:38:18 +0400 Subject: [PATCH 4/4] fix final --- src/CreaterGeneric.java | 4 ++-- src/FormCreater.java | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/CreaterGeneric.java b/src/CreaterGeneric.java index 0e56006..5b45c14 100644 --- a/src/CreaterGeneric.java +++ b/src/CreaterGeneric.java @@ -9,7 +9,7 @@ public class CreaterGeneric (gasolineTankerCount); Wheels=new ArrayList<>(wheelsCount); } - public int AddGasolineTanker(T gasolineTanker){ + public int Add(T gasolineTanker){ if(GasolineTankerCount<=GasolineTanker.size()){ GasolineTanker.add(gasolineTanker); GasolineTankerCount++; @@ -17,7 +17,7 @@ public class CreaterGeneric { @@ -82,7 +82,7 @@ public class FormCreater extends JDialog{ if(fwheels==null) return; fwheels.SetCountWheels(wheels.getCountWheels()); - createrGeneric.AddWheels(fwheels); + createrGeneric.Add(fwheels); }); GrayOrnament.addActionListener(e -> { @@ -93,7 +93,7 @@ public class FormCreater extends JDialog{ if(fwheels==null) return; fwheels.SetCountWheels(wheels.getCountWheels()); - createrGeneric.AddWheels(fwheels); + createrGeneric.Add(fwheels); }); BasicRadioButton.addActionListener(e -> { @@ -101,7 +101,7 @@ public class FormCreater extends JDialog{ if(Integer.parseInt(SpeedTextField.getText())==0 || Integer.parseInt(WeightTextField.getText())==0 || color==null){ return; } - createrGeneric.AddGasolineTanker(new EntityGasolineTanker(Integer.parseInt(SpeedTextField.getText()),Integer.parseInt(WeightTextField.getText()),color)); + createrGeneric.Add(new EntityGasolineTanker(Integer.parseInt(SpeedTextField.getText()),Integer.parseInt(WeightTextField.getText()),color)); }); AdvancedRadioButton.addActionListener(e -> { @@ -113,7 +113,7 @@ public class FormCreater extends JDialog{ if(color2==null){ return; } - createrGeneric.AddGasolineTanker(new EntityImprovedGasolineTanker(Integer.parseInt(SpeedTextField.getText()),Integer.parseInt(WeightTextField.getText()), + createrGeneric.Add(new EntityImprovedGasolineTanker(Integer.parseInt(SpeedTextField.getText()),Integer.parseInt(WeightTextField.getText()), color1,color2, BodyKitCheckBox.isSelected(),AntennaCheckBox.isSelected())); }); -- 2.25.1