final final

This commit is contained in:
Кашин Максим 2022-11-16 11:20:07 +04:00
parent 1fb63dffef
commit 0a95106eb9
7 changed files with 285 additions and 210 deletions

View File

@ -1,66 +0,0 @@
import javax.swing.*;
import java.awt.*;
import java.util.Random;
public class DrawingField extends JPanel{
private final FormGasolineTanker Field;
DrawingGasolineTanker _gasolineTanker=null;
public DrawingField(FormGasolineTanker field) {
Field = field;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 =(Graphics2D)g;
if (_gasolineTanker!=null)
_gasolineTanker.DrawTransport(g2);
else return;
}
public void DirectionButtonAction(Direction side){
if(_gasolineTanker == null)
return;
_gasolineTanker.MoveTransport(side);
}
private void SetData() {
Random rand=new Random();
_gasolineTanker.SetPosition(rand.nextInt(100)+10,rand.nextInt(100)+10,getWidth(),getHeight());
Field.SpeedLabel.setText("Speed: "+_gasolineTanker.getGasolineTanker().getSpeed());
Field.WeightLabel.setText("Weight: "+_gasolineTanker.getGasolineTanker().getWeight());
Field.BodyColorLabel.setText("Color: "+Integer.toHexString(_gasolineTanker.getGasolineTanker().getBodyColor().getRGB()).substring(2));
}
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));
_gasolineTanker=new DrawingGasolineTanker(rand.nextInt(50)+10,rand.nextInt(3000)+20000,color1,rand.nextInt(3));
SetData();
}
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));
_gasolineTanker = new DrawingImprovedGasolineTanker(rand.nextInt(50) + 10, rand.nextInt(3000) + 20000, color1,
color2, rand.nextBoolean(), rand.nextBoolean(), rand.nextInt(3));
SetData();
}
public void ResizeField(){
if (_gasolineTanker!=null)
_gasolineTanker.ChangeBorders(getWidth(),getHeight());
else return;
}
public void Draw(Graphics2D graphics) {
if (_gasolineTanker!=null)
_gasolineTanker.DrawTransport(graphics);
else return;
}
public DrawingGasolineTanker GetDrawingGasolineTanker() {
return _gasolineTanker;
}
}

View File

@ -2,7 +2,7 @@ import java.awt.*;
public class DrawingObjectGasolineTanker implements IDrawingObject { public class DrawingObjectGasolineTanker implements IDrawingObject {
private DrawingGasolineTanker _gasolineTanker=null; private DrawingGasolineTanker _gasolineTanker;
public DrawingObjectGasolineTanker(DrawingGasolineTanker gasolineTanker) public DrawingObjectGasolineTanker(DrawingGasolineTanker gasolineTanker)
{ {
@ -36,4 +36,7 @@ public class DrawingObjectGasolineTanker implements IDrawingObject {
return _gasolineTanker.GetCurrentPosition(); return _gasolineTanker.GetCurrentPosition();
return null; return null;
} }
public DrawingGasolineTanker GetGasolineTanker() {
return _gasolineTanker;
}
} }

View File

@ -2,11 +2,11 @@ import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.awt.event.ComponentAdapter; import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent; import java.awt.event.ComponentEvent;
import java.util.Random;
public class FormGasolineTanker extends JFrame{ public class FormGasolineTanker extends JFrame{
private int Width; private int Width;
private int Height; private int Height;
DrawingField field = new DrawingField(this);
DrawingGasolineTanker SelectedGasolineTanker; DrawingGasolineTanker SelectedGasolineTanker;
private JButton ButtonDown; private JButton ButtonDown;
private JButton ButtonRight; private JButton ButtonRight;
@ -19,18 +19,17 @@ public class FormGasolineTanker extends JFrame{
public JLabel BodyColorLabel; public JLabel BodyColorLabel;
private JPanel PictureBox; private JPanel PictureBox;
private JButton ButtonSelect; private JButton ButtonSelect;
private DrawingGasolineTanker _gasolineTanker=null;
public FormGasolineTanker(){ public FormGasolineTanker(){
super("Gasoline Tanker"); setTitle("Gasoline Tanker");
setContentPane(PictureBox); setContentPane(PictureBox);
setSize(1000,700); setSize(1000,500);
Width = getWidth(); setResizable(false);
Height = getHeight();
ShowWindow(); ShowWindow();
field.setBounds(0,0,Width,Height);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
} }
ImageIcon spriteUp =new ImageIcon((new ImageIcon("Material\\KeyUp.png")). ImageIcon spriteUp =new ImageIcon((new ImageIcon("Material\\KeyUp.png")).
getImage().getScaledInstance(10,10,Image.SCALE_SMOOTH)); getImage().getScaledInstance(10,10,Image.SCALE_SMOOTH));
ImageIcon spriteDown =new ImageIcon((new ImageIcon("Material\\KeyDown.png")). ImageIcon spriteDown =new ImageIcon((new ImageIcon("Material\\KeyDown.png")).
@ -39,38 +38,69 @@ public class FormGasolineTanker extends JFrame{
getImage().getScaledInstance(10,10,Image.SCALE_SMOOTH)); getImage().getScaledInstance(10,10,Image.SCALE_SMOOTH));
ImageIcon spriteRight =new ImageIcon((new ImageIcon("Material\\KeyRight.png")). ImageIcon spriteRight =new ImageIcon((new ImageIcon("Material\\KeyRight.png")).
getImage().getScaledInstance(10,10,Image.SCALE_SMOOTH)); getImage().getScaledInstance(10,10,Image.SCALE_SMOOTH));
private void SetData() {
Random rand=new Random();
_gasolineTanker.SetPosition(rand.nextInt(100)+10,rand.nextInt(100)+10,getWidth(),getHeight());
SpeedLabel.setText("Скорость: "+_gasolineTanker.getGasolineTanker().getSpeed());
WeightLabel.setText("Вес: "+_gasolineTanker.getGasolineTanker().getWeight());
BodyColorLabel.setText("Цвет: "+Integer.toHexString(_gasolineTanker.getGasolineTanker().getBodyColor().getRGB()).substring(2));
}
@Override
public void paint(Graphics g) {
super.paint(g);
if (_gasolineTanker != null) {
PictureBox.paintComponents(PictureBox.getGraphics());
_gasolineTanker.DrawTransport(g);
}
}
private void ShowWindow(){ private void ShowWindow(){
ButtonCreate.addActionListener(e -> { ButtonCreate.addActionListener(e -> {
field.CreateButtonAction(); Random rand=new Random();
ReDraw(); _gasolineTanker=new DrawingGasolineTanker(rand.nextInt(50)+10,rand.nextInt(3000)+20000,new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)),rand.nextInt(3));
SetData();
repaint();
}); });
ButtonCreateModif.addActionListener(e -> { ButtonCreateModif.addActionListener(e -> {
field.CreateModifButtonAction(); Random rand=new Random();
ReDraw(); _gasolineTanker = new DrawingImprovedGasolineTanker(rand.nextInt(50) + 10, rand.nextInt(3000) + 20000, new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)),
new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)), rand.nextBoolean(), rand.nextBoolean(),rand.nextInt(3));
SetData();
repaint();
}); });
ButtonUp.setIcon(spriteUp); ButtonUp.setIcon(spriteUp);
ButtonUp.addActionListener(e -> { ButtonUp.addActionListener(e -> {
field.DirectionButtonAction(Direction.Up); if(_gasolineTanker == null)
ReDraw(); return;
_gasolineTanker.MoveTransport(Direction.Up);
repaint();
}); });
ButtonLeft.setIcon(spriteLeft); ButtonLeft.setIcon(spriteLeft);
ButtonLeft.addActionListener(e -> { ButtonLeft.addActionListener(e -> {
field.DirectionButtonAction(Direction.Left); if(_gasolineTanker == null)
ReDraw(); return;
_gasolineTanker.MoveTransport(Direction.Left);
repaint();;
}); });
ButtonRight.setIcon(spriteRight); ButtonRight.setIcon(spriteRight);
ButtonRight.addActionListener(e -> { ButtonRight.addActionListener(e -> {
field.DirectionButtonAction(Direction.Right); if(_gasolineTanker == null)
ReDraw(); return;
_gasolineTanker.MoveTransport(Direction.Right);
repaint();
}); });
ButtonDown.setIcon(spriteDown); ButtonDown.setIcon(spriteDown);
ButtonDown.addActionListener(e -> { ButtonDown.addActionListener(e -> {
field.DirectionButtonAction(Direction.Down); if(_gasolineTanker == null)
ReDraw(); return;
_gasolineTanker.MoveTransport(Direction.Down);
repaint();
}); });
ButtonSelect.addActionListener(e -> { ButtonSelect.addActionListener(e -> {
SelectedGasolineTanker=field.GetDrawingGasolineTanker(); SelectedGasolineTanker=GetDrawingGasolineTanker();
JOptionPane.showMessageDialog(PictureBox, "Gasoline tanker added."); JOptionPane.showMessageDialog(PictureBox, "Gasoline tanker added.");
}); });
@ -81,18 +111,21 @@ public class FormGasolineTanker extends JFrame{
Width=getWidth(); Width=getWidth();
Height=getHeight(); Height=getHeight();
field.ResizeField(); if (_gasolineTanker!=null)
ReDraw(); _gasolineTanker.ChangeBorders(getWidth(),getHeight());
field.setBounds(0,0,Width,Height); else return;
repaint();
setBounds(0,0,Width,Height);
} }
}); });
} }
private void ReDraw() public DrawingGasolineTanker GetDrawingGasolineTanker() {
{ return _gasolineTanker;
Graphics2D graphics = (Graphics2D) PictureBox.getGraphics(); }
graphics.clearRect(0, 0, PictureBox.getWidth(), PictureBox.getHeight()); public void SetDrawingGasolineTanker(DrawingGasolineTanker gasolineTanker) {
PictureBox.paintComponents(graphics); _gasolineTanker=gasolineTanker;
field.Draw(graphics); SetData();
repaint();
} }
} }

View File

@ -3,12 +3,12 @@
<grid id="27dc6" binding="MainPanel" layout-manager="GridLayoutManager" row-count="4" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1"> <grid id="27dc6" binding="MainPanel" layout-manager="GridLayoutManager" row-count="4" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/> <margin top="0" left="0" bottom="0" right="0"/>
<constraints> <constraints>
<xy x="20" y="20" width="668" height="474"/> <xy x="20" y="20" width="668" height="652"/>
</constraints> </constraints>
<properties/> <properties/>
<border type="none"/> <border type="none"/>
<children> <children>
<grid id="42b0" binding="GroupBoxTools" layout-manager="GridLayoutManager" row-count="9" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1"> <grid id="42b0" binding="GroupBoxTools" layout-manager="GridLayoutManager" row-count="10" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/> <margin top="0" left="0" bottom="0" right="0"/>
<constraints> <constraints>
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/> <grid row="1" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
@ -19,7 +19,7 @@
<grid id="bc845" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1"> <grid id="bc845" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/> <margin top="0" left="0" bottom="0" right="0"/>
<constraints> <constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/> <grid row="1" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints> </constraints>
<properties/> <properties/>
<border type="none"/> <border type="none"/>
@ -31,17 +31,6 @@
</vspacer> </vspacer>
</children> </children>
</grid> </grid>
<component id="52e81" class="javax.swing.JComboBox" binding="СomboBoxSelectorMap">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<model>
<item value="Simple map"/>
<item value="Long map"/>
</model>
</properties>
</component>
<vspacer id="f144d"> <vspacer id="f144d">
<constraints> <constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/> <grid row="2" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
@ -52,7 +41,7 @@
<grid row="3" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/> <grid row="3" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints> </constraints>
<properties> <properties>
<text value="Add"/> <text value="Add GL"/>
</properties> </properties>
</component> </component>
<component id="ceb03" class="javax.swing.JTextField" binding="TextBoxPosition"> <component id="ceb03" class="javax.swing.JTextField" binding="TextBoxPosition">
@ -68,7 +57,7 @@
<grid row="5" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/> <grid row="5" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints> </constraints>
<properties> <properties>
<text value="Remove"/> <text value="Remove GL"/>
</properties> </properties>
</component> </component>
<vspacer id="76e51"> <vspacer id="76e51">
@ -92,6 +81,67 @@
<text value="Map"/> <text value="Map"/>
</properties> </properties>
</component> </component>
<grid id="13e89" binding="MapPanel" layout-manager="GridLayoutManager" row-count="5" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="bevel-lowered" title="Maps" title-justification="1" title-position="2"/>
<children>
<component id="52e81" class="javax.swing.JComboBox" binding="СomboBoxSelectorMap">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<model>
<item value="Simple map"/>
<item value="Long map"/>
</model>
</properties>
</component>
<component id="db334" class="javax.swing.JTextField" binding="TextFieldMap">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
<properties/>
</component>
<component id="e500c" class="javax.swing.JButton" binding="CreateMapButton">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Create map"/>
</properties>
</component>
<component id="8b172" class="javax.swing.JButton" binding="DeleteMapButton">
<constraints>
<grid row="4" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Delete map"/>
</properties>
</component>
<component id="10e77" class="javax.swing.JList" binding="ListBoxMaps">
<constraints>
<grid row="3" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="2" anchor="0" fill="3" indent="0" use-parent-layout="false">
<preferred-size width="150" height="50"/>
</grid>
</constraints>
<properties/>
</component>
</children>
</grid>
<component id="fdb6d" class="javax.swing.JButton" binding="ButtonShowDeleted">
<constraints>
<grid row="9" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Show deleted GL"/>
</properties>
</component>
</children> </children>
</grid> </grid>
<grid id="c9878" layout-manager="GridLayoutManager" row-count="2" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1"> <grid id="c9878" layout-manager="GridLayoutManager" row-count="2" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">

View File

@ -1,10 +1,6 @@
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.awt.event.ActionEvent; import java.util.HashMap;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.util.Objects;
public class FormMapWithSetGasolineTanker extends JFrame{ public class FormMapWithSetGasolineTanker extends JFrame{
private JPanel GroupBoxTools; private JPanel GroupBoxTools;
@ -20,8 +16,22 @@ public class FormMapWithSetGasolineTanker extends JFrame{
private JPanel MainPanel; private JPanel MainPanel;
private JTextField TextBoxPosition; private JTextField TextBoxPosition;
private JPanel PictureBox; private JPanel PictureBox;
private JPanel MapPanel;
private JTextField TextFieldMap;
private JButton CreateMapButton;
private JButton DeleteMapButton;
private JButton ButtonShowDeleted;
private JList ListBoxMaps;
private Image bufferedImage; private Image bufferedImage;
private MapsCollection _mapsCollection;
private final HashMap<String,AbstractMap> _mapsDict=new HashMap<String,AbstractMap>(){
{
put("Simple map",new SimpleMap());
put("Long map",new LongMap());
}
};
private MapWithSetGasolineTankerGeneric<DrawingObjectGasolineTanker,AbstractMap> _mapGasolineTankerCollectionGeneric; private MapWithSetGasolineTankerGeneric<DrawingObjectGasolineTanker,AbstractMap> _mapGasolineTankerCollectionGeneric;
ImageIcon spriteUp =new ImageIcon((new ImageIcon("Material\\KeyUp.png")). ImageIcon spriteUp =new ImageIcon((new ImageIcon("Material\\KeyUp.png")).
@ -40,6 +50,11 @@ public class FormMapWithSetGasolineTanker extends JFrame{
ShowWindow(); ShowWindow();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true); setVisible(true);
_mapsCollection=new MapsCollection(getWidth(),getHeight());
СomboBoxSelectorMap.removeAllItems();
for(String elem:_mapsDict.keySet()){
СomboBoxSelectorMap.addItem(elem);
}
} }
@Override @Override
@ -52,28 +67,79 @@ public class FormMapWithSetGasolineTanker extends JFrame{
} }
} }
private void ReloadMaps()
{
int index = ListBoxMaps.getSelectedIndex();
ListBoxMaps.setListData(_mapsCollection.Keys().toArray(new String[0]));
if (ListBoxMaps.getModel().getSize() > 0 && (index == -1 || index >= ListBoxMaps.getModel().getSize()))
{
ListBoxMaps.setSelectedIndex(0);
}
else if (ListBoxMaps.getModel().getSize() > 0 && index > -1 && index < ListBoxMaps.getModel().getSize())
{
ListBoxMaps.setSelectedIndex(index);
}
repaint();
}
private void ShowWindow(){ private void ShowWindow(){
ButtonShowOnMap.addActionListener(e -> { ListBoxMaps.addListSelectionListener(e -> {
if (_mapGasolineTankerCollectionGeneric == null) if(ListBoxMaps.getSelectedIndex()==-1)
return;
bufferedImage = _mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).ShowSet();
repaint();
});
DeleteMapButton.addActionListener(e -> {
if (ListBoxMaps.getSelectedIndex() == -1)
{ {
return; return;
} }
bufferedImage = _mapGasolineTankerCollectionGeneric.ShowOnMap(); if(JOptionPane.showConfirmDialog(this,"Delete map "+ListBoxMaps.getSelectedValue().toString()+"?",
"Удаление",JOptionPane.YES_NO_OPTION)==0)
{
_mapsCollection.DelMap(ListBoxMaps.getSelectedValue().toString());
ReloadMaps();
}
});
CreateMapButton.addActionListener(e -> {
if (СomboBoxSelectorMap.getSelectedIndex() == -1 || TextFieldMap.getText()==null || TextFieldMap.getText().equals(""))
{
JOptionPane.showMessageDialog(this,"\n" + "Not all data is complete","Error",JOptionPane.ERROR_MESSAGE);
return;
}
if (!_mapsDict.containsKey(СomboBoxSelectorMap.getSelectedItem()))
{
JOptionPane.showMessageDialog(this,"No such card","Error",JOptionPane.ERROR_MESSAGE);
return;
}
_mapsCollection.AddMap(TextFieldMap.getText(), _mapsDict.get(СomboBoxSelectorMap.getSelectedItem().toString()));
ReloadMaps();
});
ButtonShowOnMap.addActionListener(e -> {
if (ListBoxMaps.getSelectedIndex() ==-1)
{
return;
}
bufferedImage = _mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).ShowOnMap();
repaint(); repaint();
}); });
ButtonShowStorage.addActionListener(e -> { ButtonShowStorage.addActionListener(e -> {
if (_mapGasolineTankerCollectionGeneric == null) if (ListBoxMaps.getSelectedIndex() ==-1)
{ {
return; return;
} }
bufferedImage = _mapGasolineTankerCollectionGeneric.ShowSet(); bufferedImage = _mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).ShowSet();
repaint(); repaint();
}); });
ButtonAddGasolineTanker.addActionListener(e -> { ButtonAddGasolineTanker.addActionListener(e -> {
if (_mapGasolineTankerCollectionGeneric == null) if (ListBoxMaps.getSelectedIndex() ==-1)
{ {
return; return;
} }
@ -85,9 +151,9 @@ public class FormMapWithSetGasolineTanker extends JFrame{
if (dialog.getSelectedGasolineTanker()!=null) { if (dialog.getSelectedGasolineTanker()!=null) {
DrawingObjectGasolineTanker gasolineTanker = new DrawingObjectGasolineTanker(dialog.getSelectedGasolineTanker()); DrawingObjectGasolineTanker gasolineTanker = new DrawingObjectGasolineTanker(dialog.getSelectedGasolineTanker());
if (_mapGasolineTankerCollectionGeneric.plus(gasolineTanker) >= 0) { if (_mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).plus(gasolineTanker)>=0) {
JOptionPane.showMessageDialog(this, "\n" + "Object added", "Success", JOptionPane.INFORMATION_MESSAGE); JOptionPane.showMessageDialog(this, "Object added", "Success", JOptionPane.INFORMATION_MESSAGE);
bufferedImage = _mapGasolineTankerCollectionGeneric.ShowSet(); bufferedImage = _mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).ShowSet();
repaint(); repaint();
} else { } else {
JOptionPane.showMessageDialog(this, "Failed to add object", "Error",JOptionPane.INFORMATION_MESSAGE); JOptionPane.showMessageDialog(this, "Failed to add object", "Error",JOptionPane.INFORMATION_MESSAGE);
@ -97,79 +163,68 @@ public class FormMapWithSetGasolineTanker extends JFrame{
ButtonRemoveGasolineTanker.addActionListener(e -> { ButtonRemoveGasolineTanker.addActionListener(e -> {
String txt=TextBoxPosition.getText(); String txt=TextBoxPosition.getText();
if (txt==null||_mapGasolineTankerCollectionGeneric==null||txt.isEmpty()) if (txt==null||ListBoxMaps.getSelectedIndex() ==-1)
{ {
return; return;
} }
int result = JOptionPane.showConfirmDialog( int result = JOptionPane.showConfirmDialog(
this, this,
"Delete object?", "Delete object?",
"Delete", "Removal",
JOptionPane.YES_NO_CANCEL_OPTION); JOptionPane.YES_NO_CANCEL_OPTION);
if (result == JOptionPane.NO_OPTION) if (result!=0)
{ {
return; return;
} }
int pos = Integer.parseInt(txt); int pos = Integer.parseInt(txt);
if (_mapGasolineTankerCollectionGeneric.minus(pos) !=null) if(_mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).minus(pos)!=null){
{ JOptionPane.showMessageDialog(this, "Object removed", "Success", JOptionPane.INFORMATION_MESSAGE);
JOptionPane.showMessageDialog(this,"Object added", "Success",JOptionPane.INFORMATION_MESSAGE); bufferedImage = _mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).ShowSet();
bufferedImage = _mapGasolineTankerCollectionGeneric.ShowSet();
repaint(); repaint();
} }
else else
{ {
JOptionPane.showMessageDialog(this,"Failed to add object", "Error",JOptionPane.INFORMATION_MESSAGE); JOptionPane.showMessageDialog(this,"Failed to delete object","\n" + "Error",JOptionPane.INFORMATION_MESSAGE);
} }
}); });
СomboBoxSelectorMap.addActionListener(e -> { ButtonShowDeleted.addActionListener(e -> {
AbstractMap map = null; if (ListBoxMaps.getSelectedIndex()==-1)
return;
switch (СomboBoxSelectorMap.getSelectedItem().toString()) DrawingObjectGasolineTanker gasolineTanker=(DrawingObjectGasolineTanker)_mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).GetGasolineTankerInDeleted();
{ if(gasolineTanker!=null){
case "Simple map": FormGasolineTanker dialog=new FormGasolineTanker();
map = new SimpleMap(); dialog.SetDrawingGasolineTanker(gasolineTanker.GetGasolineTanker());
break; dialog.setSize(1200,700);
case "Long map": dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
map = new LongMap(); dialog.setVisible(true);
break;
}
if (map != null)
{
_mapGasolineTankerCollectionGeneric = new MapWithSetGasolineTankerGeneric<>(
PictureBox.getWidth(), PictureBox.getHeight(), map);
}
else
{
_mapGasolineTankerCollectionGeneric = null;
} }
}); });
ButtonUp.setIcon(spriteUp); ButtonUp.setIcon(spriteUp);
ButtonUp.addActionListener(e -> { ButtonUp.addActionListener(e -> {
if (_mapGasolineTankerCollectionGeneric != null) { if (ListBoxMaps.getSelectedIndex() !=-1) {
bufferedImage = _mapGasolineTankerCollectionGeneric.MoveObject(Direction.Up); bufferedImage = _mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).MoveObject(Direction.Up);
repaint(); repaint();
} }
}); });
ButtonDown.setIcon(spriteDown); ButtonDown.setIcon(spriteDown);
ButtonDown.addActionListener(e -> { ButtonDown.addActionListener(e -> {
if (_mapGasolineTankerCollectionGeneric != null) { if (ListBoxMaps.getSelectedIndex() !=-1) {
bufferedImage = _mapGasolineTankerCollectionGeneric.MoveObject(Direction.Down); bufferedImage = _mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).MoveObject(Direction.Down);
repaint(); repaint();
} }
}); });
ButtonRight.setIcon(spriteRight); ButtonRight.setIcon(spriteRight);
ButtonRight.addActionListener(e -> { ButtonRight.addActionListener(e -> {
if (_mapGasolineTankerCollectionGeneric != null) { if (ListBoxMaps.getSelectedIndex() !=-1) {
bufferedImage = _mapGasolineTankerCollectionGeneric.MoveObject(Direction.Right); bufferedImage = _mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).MoveObject(Direction.Right);
repaint(); repaint();
} }
}); });
ButtonLeft.setIcon(spriteLeft); ButtonLeft.setIcon(spriteLeft);
ButtonLeft.addActionListener(e -> { ButtonLeft.addActionListener(e -> {
if (_mapGasolineTankerCollectionGeneric != null) { if (ListBoxMaps.getSelectedIndex() !=-1) {
bufferedImage = _mapGasolineTankerCollectionGeneric.MoveObject(Direction.Left); bufferedImage = _mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).MoveObject(Direction.Left);
repaint(); repaint();
} }
}); });

View File

@ -1,5 +1,6 @@
import java.awt.*; import java.awt.*;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.util.ArrayDeque;
public class MapWithSetGasolineTankerGeneric <T extends IDrawingObject,U extends AbstractMap>{ public class MapWithSetGasolineTankerGeneric <T extends IDrawingObject,U extends AbstractMap>{
private final int _pictureWidth; private final int _pictureWidth;
@ -7,6 +8,7 @@ public class MapWithSetGasolineTankerGeneric <T extends IDrawingObject,U extends
private final int _placeSizeWidth = 210; private final int _placeSizeWidth = 210;
private final int _placeSizeHeight = 90; private final int _placeSizeHeight = 90;
private final SetGasolineTankerGeneric<T> _setGasolineTanker; private final SetGasolineTankerGeneric<T> _setGasolineTanker;
private final ArrayDeque<T> DeletedGasolineTanker;
private final U _map; private final U _map;
public MapWithSetGasolineTankerGeneric(int picWidth,int picHeight, U map) public MapWithSetGasolineTankerGeneric(int picWidth,int picHeight, U map)
@ -17,6 +19,7 @@ public class MapWithSetGasolineTankerGeneric <T extends IDrawingObject,U extends
_pictureWidth = picWidth; _pictureWidth = picWidth;
_pictureHeight = picHeight; _pictureHeight = picHeight;
_map = map; _map = map;
DeletedGasolineTanker = new ArrayDeque<>();
} }
public int plus(T gasolineTanker) public int plus(T gasolineTanker)
@ -26,7 +29,9 @@ public class MapWithSetGasolineTankerGeneric <T extends IDrawingObject,U extends
public T minus(int position) public T minus(int position)
{ {
return _setGasolineTanker.Remove(position); T gasolineTanker=_setGasolineTanker.Remove(position);
DeletedGasolineTanker.push(gasolineTanker);
return gasolineTanker;
} }
public Image ShowSet() public Image ShowSet()
@ -41,13 +46,9 @@ public class MapWithSetGasolineTankerGeneric <T extends IDrawingObject,U extends
public Image ShowOnMap() public Image ShowOnMap()
{ {
Shaking(); Shaking();
for (int i = 0; i < _setGasolineTanker.Count(); i++) for (IDrawingObject gasolineTanker : _setGasolineTanker)
{ {
T gasolineTanker = _setGasolineTanker.Get(i); return _map.CreateMap(_pictureWidth, _pictureHeight, gasolineTanker);
if (gasolineTanker != null)
{
return _map.CreateMap(_pictureWidth, _pictureHeight, gasolineTanker);
}
} }
return new BufferedImage(_pictureWidth, _pictureHeight,1); return new BufferedImage(_pictureWidth, _pictureHeight,1);
} }
@ -122,4 +123,13 @@ public class MapWithSetGasolineTankerGeneric <T extends IDrawingObject,U extends
} }
} }
} }
public T GetGasolineTankerArrayDeque(int i){
return _setGasolineTanker.Get(i);
}
public T GetGasolineTankerInDeleted() {
if(DeletedGasolineTanker.isEmpty())
return null;
return DeletedGasolineTanker.pop();
}
} }

View File

@ -1,68 +1,58 @@
public class SetGasolineTankerGeneric <T extends Object>{ import java.util.ArrayList;
private final Object[] _places; import java.util.Iterator;
public class SetGasolineTankerGeneric <T extends Object> implements Iterable<T>{
private final ArrayList<T> _places;
public int Count() { public int Count() {
return _places.length; return _places.size();
} }
private final int _maxCount;
public SetGasolineTankerGeneric(int count) public SetGasolineTankerGeneric(int count)
{ {
_places = new Object[count]; _maxCount=count;
_places = new ArrayList<>();
} }
public int Insert(T gasolineTanker) public int Insert(T gasolineTanker)
{ {
return Insert(gasolineTanker,0); if (_places.size()+1>=_maxCount)
return -1;
_places.add(0,gasolineTanker);
return 0;
} }
public int Insert(T gasolineTanker, int position) public int Insert(T gasolineTanker, int position)
{ {
int EmptyEl=-1; if (position>=_maxCount||position<0)
if (position>=Count() || position < 0)
return -1; return -1;
if (_places.size()+1>=_maxCount)
if (_places[position] == null) return -1;
{ _places.add(position,gasolineTanker);
_places[position] = gasolineTanker; return position;
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) public T Remove(int position)
{ {
if (position >= Count() || position < 0 || _places[position]==null) if (position>=_maxCount||position<0)
return null; return null;
T deleted=_places.get(position);
T deleted =(T)_places[position]; _places.remove(position);
_places[position] = null;
return deleted; return deleted;
} }
public T Get(int position) public T Get(int position)
{ {
if (position >= Count() || position < 0) if (position>=_maxCount||position<0)
return null; return null;
return _places.get(position);
}
return (T)_places[position]; public void Set(int position,T gasolineTanker){
if (position >= _maxCount||position<0)
return;
Insert(gasolineTanker,position);
}
@Override
public Iterator<T> iterator(){
return _places.iterator();
} }
} }