diff --git a/src/CreaterGeneric.java b/src/CreaterGeneric.java new file mode 100644 index 0000000..5b45c14 --- /dev/null +++ b/src/CreaterGeneric.java @@ -0,0 +1,38 @@ +import java.util.Random; +import java.util.ArrayList; +public class CreaterGeneric { + ArrayList GasolineTanker; + ArrayList Wheels; + int GasolineTankerCount=0; + int WheelsCount=0; + public CreaterGeneric(int gasolineTankerCount,int wheelsCount){ + GasolineTanker=new ArrayList<>(gasolineTankerCount); + Wheels=new ArrayList<>(wheelsCount); + } + public int Add(T gasolineTanker){ + if(GasolineTankerCount<=GasolineTanker.size()){ + GasolineTanker.add(gasolineTanker); + GasolineTankerCount++; + return GasolineTankerCount-1; + } + return -1; + } + public int Add(U wheels){ + if(WheelsCount<=Wheels.size()){ + Wheels.add(wheels); + WheelsCount++; + return WheelsCount-1; + } + return -1; + } + public DrawingGasolineTanker NewGasolineTankerCreating() + { + Random rand=new Random(); + T gasolineTanker = (GasolineTanker.get(rand.nextInt(GasolineTankerCount))); + U wheel = (Wheels.get(rand.nextInt(WheelsCount))); + if(gasolineTanker instanceof EntityImprovedGasolineTanker){ + return new DrawingImprovedGasolineTanker(gasolineTanker,wheel); + } + return new DrawingGasolineTanker(gasolineTanker,wheel); + } +} diff --git a/src/DrawingField.java b/src/DrawingField.java index 9aef989..825f30d 100644 --- a/src/DrawingField.java +++ b/src/DrawingField.java @@ -60,4 +60,7 @@ public class DrawingField extends JPanel{ _gasolineTanker.DrawTransport(graphics); else return; } + public DrawingGasolineTanker GetDrawingGasolineTanker() { + return _gasolineTanker; + } } diff --git a/src/DrawingGasolineTanker.java b/src/DrawingGasolineTanker.java index 4aedf6d..939c0da 100644 --- a/src/DrawingGasolineTanker.java +++ b/src/DrawingGasolineTanker.java @@ -20,6 +20,16 @@ public class DrawingGasolineTanker { Wheels= GetFormOfWheels(wheelsForm); Wheels.SetCountWheels((int)(2 + Math.random() + Math.random()*2)); } + public DrawingGasolineTanker(int speed, float weight, Color bodyColor, IDrawningObjectWheels wheelsForm) + { + GasolineTanker = new EntityGasolineTanker(speed, weight, bodyColor); + Wheels= wheelsForm; + } + public DrawingGasolineTanker(EntityGasolineTanker gasolineTanker,IDrawningObjectWheels wheels) + { + GasolineTanker = gasolineTanker; + Wheels = wheels; + } public IDrawningObjectWheels GetFormOfWheels(int FormOfWheel){ OrnamentForm temp = null; diff --git a/src/DrawingImprovedGasolineTanker.java b/src/DrawingImprovedGasolineTanker.java index 4a62c10..daf5c28 100644 --- a/src/DrawingImprovedGasolineTanker.java +++ b/src/DrawingImprovedGasolineTanker.java @@ -8,6 +8,16 @@ public class DrawingImprovedGasolineTanker extends DrawingGasolineTanker { Wheels= GetFormOfWheels(wheelForm); Wheels.SetCountWheels((int)(2 + Math.random() + Math.random()*2)); } + public DrawingImprovedGasolineTanker(int speed, float weight, Color bodyColor, Color dopColor, boolean bodyKit, boolean antena,IDrawningObjectWheels wheelForm) { + super(speed, weight, bodyColor, 160, 70); + GasolineTanker=new EntityImprovedGasolineTanker(speed,weight,bodyColor,dopColor,bodyKit,antena); + Wheels= wheelForm; + } + public DrawingImprovedGasolineTanker(EntityGasolineTanker gasolineTanker,IDrawningObjectWheels wheels) { + super(gasolineTanker,wheels); + GasolineTanker=gasolineTanker; + } + @Override public void DrawTransport(Graphics g) { diff --git a/src/DrawingMap.java b/src/DrawingMap.java deleted file mode 100644 index 7d151d2..0000000 --- a/src/DrawingMap.java +++ /dev/null @@ -1,71 +0,0 @@ -import javax.swing.*; -import java.awt.*; -import java.awt.image.BufferedImage; -import java.util.Random; - -public class DrawingMap extends JPanel { - private final FormMap Map; - private AbstractMap _abstractMap; - BufferedImage bufferedImage; - - public DrawingMap(FormMap map){ - Map=map; - _abstractMap = new SimpleMap(); - } - - @Override - protected void paintComponent(Graphics g) { - super.paintComponent(g); - g.drawImage(bufferedImage,0,0,null); - } - - private void SetData(DrawingGasolineTanker gasolineTanker) { - Random rand=new Random(); - gasolineTanker.SetPosition(rand.nextInt(100)+10,rand.nextInt(100)+10,getWidth(),getHeight()); - Map.SpeedLabel.setText("Speed: "+gasolineTanker.getGasolineTanker().getSpeed()); - Map.WeightLabel.setText("Weight: "+gasolineTanker.getGasolineTanker().getWeight()); - Map.BodyColorLabel.setText("Color: "+Integer.toHexString(gasolineTanker.getGasolineTanker().getBodyColor().getRGB()).substring(2)); - bufferedImage = _abstractMap.CreateMap(700,550,new DrawingObjectGasolineTanker(gasolineTanker)); - } - public void CreateButtonAction(){ - Random rand=new Random(); - Color color1=new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)); - if(color1==null) - color1=new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)); - DrawingGasolineTanker gasolineTanker=new DrawingGasolineTanker(rand.nextInt(50)+10,rand.nextInt(3000)+20000,color1,rand.nextInt(3)); - SetData(gasolineTanker); - } - public void CreateModifButtonAction(){ - Random rand=new Random(); - Color color1=new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)); - Color color2=new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)); - if(color1==null) - color1=new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)); - if (color2==null) - color2=new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)); - DrawingGasolineTanker gasolineTanker = new DrawingImprovedGasolineTanker(rand.nextInt(50) + 10, rand.nextInt(3000) + 20000, color1, - color2, rand.nextBoolean(), rand.nextBoolean(),rand.nextInt(3)); - SetData(gasolineTanker); - } - public void DirectionButtonAction(Direction side){ - if(_abstractMap != null){ - bufferedImage = _abstractMap.MoveObject(side); - } - } - - public void ComboBoxSelectorMapAction(String name){ - switch (name){ - case "Simple map": - _abstractMap = new SimpleMap(); - break; - case "Long map": - _abstractMap = new LongMap(); - break; - } - } - - public void DrawMap(Graphics g){ - g.drawImage(bufferedImage,0,0,null); - } - -} diff --git a/src/FormCreater.form b/src/FormCreater.form new file mode 100644 index 0000000..dc9a6c3 --- /dev/null +++ b/src/FormCreater.form @@ -0,0 +1,230 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/src/FormCreater.java b/src/FormCreater.java new file mode 100644 index 0000000..4719ada --- /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.Add(fwheels); + }); + + RedOrnament.addActionListener(e -> { + if(wheels==null){ + return; + } + fwheels=new DrawingOrnamentWheelsSecond(wheels); + if(fwheels==null) + return; + fwheels.SetCountWheels(wheels.getCountWheels()); + createrGeneric.Add(fwheels); + }); + + GrayOrnament.addActionListener(e -> { + if(wheels==null){ + return; + } + fwheels=new DrawingOrnamentWheelsFirst(wheels); + if(fwheels==null) + return; + fwheels.SetCountWheels(wheels.getCountWheels()); + createrGeneric.Add(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.Add(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.Add(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..adba354 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, "Gasoline tanker added."); + }); 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..e07db24 --- /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, "\n" + "Object added", "Success", JOptionPane.INFORMATION_MESSAGE); + bufferedImage = _mapGasolineTankerCollectionGeneric.ShowSet(); + repaint(); + } else { + JOptionPane.showMessageDialog(this, "Failed to add object", "Error",JOptionPane.INFORMATION_MESSAGE); + } + } + }); + + ButtonRemoveGasolineTanker.addActionListener(e -> { + String txt=TextBoxPosition.getText(); + if (txt==null||_mapGasolineTankerCollectionGeneric==null||txt.isEmpty()) + { + return; + } + int result = JOptionPane.showConfirmDialog( + this, + "Delete object?", + "Delete", + JOptionPane.YES_NO_CANCEL_OPTION); + if (result == JOptionPane.NO_OPTION) + { + return; + } + int pos = Integer.parseInt(txt); + if (_mapGasolineTankerCollectionGeneric.minus(pos) !=null) + { + JOptionPane.showMessageDialog(this,"Object added", "Success",JOptionPane.INFORMATION_MESSAGE); + bufferedImage = _mapGasolineTankerCollectionGeneric.ShowSet(); + repaint(); + } + else + { + JOptionPane.showMessageDialog(this,"Failed to add object", "Error",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]; + } +}