Eliseev E.E. LabWork04 #4

Closed
ElEgEv wants to merge 14 commits from LabWork04 into LabWork03
12 changed files with 505 additions and 203 deletions

View File

@ -60,6 +60,7 @@ public abstract class AbstractMap
int rightCell = (int)(cortege[2] / _size_x);
int step = (int)_drawingObject.Step();
boolean canMove = true;
switch (direction)
{
case Left:
@ -111,10 +112,12 @@ public abstract class AbstractMap
}
break;
}
if (canMove)
{
_drawingObject.MoveObject(direction);
}
return DrawMapWithObject();
}

View File

@ -38,18 +38,18 @@ public class DrawingAirbus extends DrawingPlane
if (airbus.AddСompartment)
{
g2d.setPaint(airbus.AddColor);
g.fillRect((int)_startPosX + 30, (int)_startPosY + 12, 14, 3);
g2d.fillRect((int)_startPosX + 30, (int)_startPosY + 12, 14, 3);
g2d.setPaint(Color.BLACK);
g.drawRect((int)_startPosX + 30, (int)_startPosY + 12, 14, 3);
g2d.drawRect((int)_startPosX + 30, (int)_startPosY + 12, 14, 3);
}
//дополнительный двигатель
if (airbus.AddEngine)
{
g2d.setPaint(airbus.AddColor);
g.fillOval((int)_startPosX + 24, (int)_startPosY + 22, 10, 5);
g2d.fillOval((int)_startPosX + 24, (int)_startPosY + 22, 10, 5);
g2d.setPaint(Color.BLACK);
g.drawOval((int)_startPosX + 24, (int)_startPosY + 22, 10, 5);
g2d.drawOval((int)_startPosX + 24, (int)_startPosY + 22, 10, 5);
}
}
}

View File

@ -18,7 +18,6 @@ public class DrawingPlane extends JPanel
public void SetFormEnum()
{
Random rnd = new Random();
int numbEnum = rnd.nextInt(1, 4);
if(numbEnum == 1)

View File

@ -4,6 +4,11 @@ public class DrawningObjectPlane implements IDrawningObject
{
private DrawingPlane _plane = null;
public DrawingPlane GetDrawingObjectPlane()
{
return _plane;
}
public DrawningObjectPlane(DrawingPlane plane){ _plane = plane; }
@Override

View File

@ -3,7 +3,7 @@
<grid id="27dc6" binding="MainPanel" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="994" height="609"/>
<xy x="20" y="20" width="994" height="654"/>
</constraints>
<properties/>
<border type="none"/>
@ -11,46 +11,30 @@
<grid id="92d87" binding="PictureBoxPlane" layout-manager="BorderLayout" hgap="0" vgap="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">
<minimum-size width="730" height="550"/>
<preferred-size width="730" height="550"/>
<maximum-size width="730" height="550"/>
<minimum-size width="730" height="650"/>
<preferred-size width="730" height="650"/>
<maximum-size width="730" height="650"/>
</grid>
</constraints>
<properties/>
<border type="none"/>
<children/>
</grid>
<grid id="4871f" binding="ButtonGroupPanel" layout-manager="GridLayoutManager" row-count="13" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="4871f" binding="GroupBoxTools" layout-manager="GridLayoutManager" row-count="17" column-count="3" 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="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false">
<minimum-size width="250" height="550"/>
<preferred-size width="250" height="550"/>
<maximum-size width="250" height="550"/>
<minimum-size width="250" height="650"/>
<preferred-size width="250" height="650"/>
<maximum-size width="250" height="650"/>
</grid>
</constraints>
<properties/>
<border type="none" title="Инструменты"/>
<children>
<component id="1388e" class="javax.swing.JComboBox" binding="ComboBoxSelectorMap">
<constraints>
<grid row="0" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="2" anchor="0" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="220" height="30"/>
<preferred-size width="220" height="30"/>
<maximum-size width="220" height="30"/>
</grid>
</constraints>
<properties>
<model>
<item value="Простая карта"/>
<item value="Буря в пустыне"/>
<item value="Звёздные войны"/>
</model>
</properties>
</component>
<component id="d0967" class="javax.swing.JButton" binding="ButtonLeft">
<constraints>
<grid row="12" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="4" fill="0" indent="0" use-parent-layout="false">
<grid row="16" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="4" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="45" height="45"/>
<preferred-size width="45" height="45"/>
<maximum-size width="45" height="45"/>
@ -62,7 +46,7 @@
</component>
<component id="d9af3" class="javax.swing.JButton" binding="ButtonDown">
<constraints>
<grid row="12" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false">
<grid row="16" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false">
<minimum-size width="45" height="45"/>
<preferred-size width="45" height="45"/>
<maximum-size width="45" height="45"/>
@ -74,7 +58,7 @@
</component>
<component id="9e43" class="javax.swing.JButton" binding="ButtonRight">
<constraints>
<grid row="12" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false">
<grid row="16" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="45" height="45"/>
<preferred-size width="45" height="45"/>
<maximum-size width="45" height="45"/>
@ -86,7 +70,7 @@
</component>
<component id="4d1dd" class="javax.swing.JButton" binding="ButtonUp">
<constraints>
<grid row="11" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false">
<grid row="15" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false">
<minimum-size width="45" height="45"/>
<preferred-size width="45" height="45"/>
<maximum-size width="45" height="45"/>
@ -98,7 +82,7 @@
</component>
<component id="67190" class="javax.swing.JButton" binding="ButtonShowOnMap">
<constraints>
<grid row="9" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false">
<grid row="13" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false">
<minimum-size width="220" height="30"/>
<preferred-size width="220" height="30"/>
<maximum-size width="220" height="30"/>
@ -110,7 +94,7 @@
</component>
<component id="28576" class="javax.swing.JButton" binding="ButtonShowStorage">
<constraints>
<grid row="7" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false">
<grid row="11" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false">
<minimum-size width="220" height="30"/>
<preferred-size width="220" height="30"/>
<maximum-size width="220" height="30"/>
@ -122,7 +106,7 @@
</component>
<component id="7119" class="javax.swing.JButton" binding="ButtonAddPlane">
<constraints>
<grid row="2" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false">
<grid row="6" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false">
<minimum-size width="220" height="30"/>
<preferred-size width="220" height="30"/>
<maximum-size width="220" height="30"/>
@ -134,7 +118,7 @@
</component>
<component id="6f119" class="javax.swing.JTextField" binding="MaskedTextBoxPosition">
<constraints>
<grid row="4" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="6" anchor="0" fill="0" indent="0" use-parent-layout="false">
<grid row="7" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="6" anchor="0" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="220" height="30"/>
<preferred-size width="220" height="30"/>
<maximum-size width="220" height="30"/>
@ -147,7 +131,7 @@
</component>
<component id="d75e9" class="javax.swing.JButton" binding="ButtonRemovePlane">
<constraints>
<grid row="5" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false">
<grid row="8" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false">
<minimum-size width="220" height="30"/>
<preferred-size width="220" height="30"/>
<maximum-size width="220" height="30"/>
@ -157,31 +141,110 @@
<text value="Удалить самолёт"/>
</properties>
</component>
<vspacer id="85889">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
<vspacer id="ed421">
<constraints>
<grid row="3" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
<vspacer id="f972a">
<constraints>
<grid row="6" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
<vspacer id="65c78">
<constraints>
<grid row="8" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
<vspacer id="7d70b">
<constraints>
<grid row="10" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
<vspacer id="65c78">
<constraints>
<grid row="12" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
<vspacer id="7d70b">
<constraints>
<grid row="14" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
<vspacer id="d4b8a">
<constraints>
<grid row="5" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
<grid id="fe895" binding="GroupBoxMap" 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="5" col-span="3" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none" title="Карты"/>
<children>
<component id="7efbe" class="javax.swing.JTextField" binding="TextBoxNewMapName">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="0" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="220" height="30"/>
<preferred-size width="220" height="30"/>
<maximum-size width="220" height="30"/>
</grid>
</constraints>
<properties/>
</component>
<component id="1388e" class="javax.swing.JComboBox" binding="ComboBoxSelectorMap">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="0" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="220" height="30"/>
<preferred-size width="220" height="30"/>
<maximum-size width="220" height="30"/>
</grid>
</constraints>
<properties>
<model>
<item value="Простая карта"/>
<item value="Буря в пустыне"/>
<item value="Звёздные войны"/>
</model>
</properties>
</component>
<component id="3b6b1" class="javax.swing.JButton" binding="ButtonAddMap">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="220" height="30"/>
<preferred-size width="220" height="30"/>
<maximum-size width="220" height="30"/>
</grid>
</constraints>
<properties>
<text value="Добавить карту"/>
</properties>
</component>
<component id="9a389" class="javax.swing.JList" binding="ListBoxMaps" custom-create="true">
<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">
<minimum-size width="220" height="100"/>
<preferred-size width="220" height="100"/>
<maximum-size width="220" height="100"/>
</grid>
</constraints>
<properties>
<selectionMode value="2"/>
</properties>
</component>
<component id="81eff" class="javax.swing.JButton" binding="ButtonDeleteMap">
<constraints>
<grid row="4" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="220" height="30"/>
<preferred-size width="220" height="30"/>
<maximum-size width="220" height="30"/>
</grid>
</constraints>
<properties>
<text value="Удалить карту"/>
</properties>
</component>
</children>
</grid>
<component id="860bc" class="javax.swing.JButton" binding="ButtonShowDelete">
<constraints>
<grid row="9" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false">
<minimum-size width="220" height="30"/>
<preferred-size width="220" height="30"/>
<maximum-size width="220" height="30"/>
</grid>
</constraints>
<properties>
<text value="Посмотреть удалённый самолёт"/>
</properties>
</component>
</children>
</grid>
</children>

View File

@ -1,16 +1,19 @@
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.util.HashMap;
public class FormMapWithSetPlanesGeneric {
public JPanel MainPanel;
private JPanel PictureBoxPlane;
private JPanel ButtonGroupPanel;
private JPanel GroupBoxTools;
private JComboBox ComboBoxSelectorMap;
private JButton ButtonLeft;
private JButton ButtonDown;
@ -21,9 +24,58 @@ public class FormMapWithSetPlanesGeneric {
private JButton ButtonAddPlane;
private JTextField MaskedTextBoxPosition;
private JButton ButtonRemovePlane;
private JTextField TextBoxNewMapName;
private JButton ButtonAddMap;
private JList ListBoxMaps;
private JButton ButtonDeleteMap;
private JPanel GroupBoxMap;
private JButton ButtonShowDelete;
//объект от класса карты с набором объектов
private MapWithSetPlanesGeneric<DrawningObjectPlane, AbstractMap> _mapPlanesCollectionGeneric;
//объект от коллекции карт
private final MapsCollection _mapsCollection;
//Для выпадающего списка
HashMap<String, AbstractMap> _mapsHashMap = new HashMap<>()
{{
put("Простая карта", new SimpleMap());
put("Буря в пустыне", new DesertStormMap());
put("Звёздные войны", new StarWarsMap());
}};
public FormMapWithSetPlanesGeneric()
{
CreateWindow();
_mapsCollection = new MapsCollection(730, 650);
ComboBoxSelectorMap.removeAllItems();
for (String elem : _mapsHashMap.keySet())
{
ComboBoxSelectorMap.addItem(elem);
}
}
//заполнение ListBoxMaps
private void ReloadMaps()
{
int index = ListBoxMaps.getSelectedIndex();
DefaultListModel<String> model = (DefaultListModel<String>)ListBoxMaps.getModel();
model.removeAllElements();
for (int i = 0; i < _mapsCollection.Keys().size(); i++)
{
model.addElement(_mapsCollection.Keys().get(i));
}
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);
}
}
//обновление отрисовки
public void UpdateWindow(BufferedImage bmp)
@ -37,7 +89,7 @@ public class FormMapWithSetPlanesGeneric {
PictureBoxPlane.revalidate();
}
public FormMapWithSetPlanesGeneric()
public void CreateWindow()
{
//загрузка изображений для кнопок
try
@ -56,11 +108,89 @@ public class FormMapWithSetPlanesGeneric {
System.out.println(ex.getMessage());
}
//добавить карту
ButtonAddMap.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(ComboBoxSelectorMap.getSelectedIndex() == -1 || TextBoxNewMapName.getText().isEmpty())
{
JOptionPane.showMessageDialog(null, "Не все данные заполнены", "Ошибка", JOptionPane.ERROR_MESSAGE);
return;
}
if(!_mapsHashMap.containsKey(ComboBoxSelectorMap.getSelectedItem()))
{
JOptionPane.showMessageDialog(null, "Данная карта отсутсвует", "Ошибка", JOptionPane.ERROR_MESSAGE);
return;
}
_mapsCollection.AddMap(TextBoxNewMapName.getText(), _mapsHashMap.get(ComboBoxSelectorMap.getSelectedItem().toString()));
ReloadMaps();
}
});
//удалить карту
ButtonDeleteMap.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(ListBoxMaps.getSelectedIndex() == -1)
{
return;
}
if(JOptionPane.showConfirmDialog(null, "Удалить карту" + ListBoxMaps.getSelectedValue().toString() + "?",
"Удаление", JOptionPane.YES_NO_OPTION) == 0)
{
_mapsCollection.DelMap(ListBoxMaps.getSelectedValue().toString());
ReloadMaps();
}
}
});
//список карт
ListBoxMaps.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
if(ListBoxMaps.getSelectedIndex() == -1)
{
return;
}
UpdateWindow(_mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).ShowSet());
}
});
//Просмотр удалённых самолётов
ButtonShowDelete.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(ListBoxMaps.getSelectedIndex() == -1)
{
return;
}
DrawningObjectPlane plane = _mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).GetPlaneInDelete();
if(plane == null)
{
JOptionPane.showMessageDialog(null, "Список пуст", "Ошибка", JOptionPane.ERROR_MESSAGE);
return;
}
FormPlane form = new FormPlane(plane);
form.setSize(700, 500);
form.setVisible(true);
}
});
//добавление самолёта
ButtonAddPlane.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(_mapPlanesCollectionGeneric == null)
if(ListBoxMaps.getSelectedIndex() == -1)
{
return;
}
@ -71,10 +201,10 @@ public class FormMapWithSetPlanesGeneric {
form.setModal(true);
DrawningObjectPlane plane = new DrawningObjectPlane(form.GetSelectedShip());
if(_mapPlanesCollectionGeneric.Add(plane) != -1)
if(_mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).Add(plane) != -1)
{
JOptionPane.showMessageDialog(null,"Объект добавлен");
UpdateWindow(_mapPlanesCollectionGeneric.ShowSet());
UpdateWindow(_mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).ShowSet());
}
else
{
@ -95,17 +225,17 @@ public class FormMapWithSetPlanesGeneric {
int result = JOptionPane.showConfirmDialog(null,"Удалить объект?","Удаление",
JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE);
if(result==JOptionPane.NO_OPTION)
if(result == JOptionPane.NO_OPTION)
{
return;
}
int pos = Integer.parseInt(MaskedTextBoxPosition.getText());
if(_mapPlanesCollectionGeneric.Delete(pos) != null)
if(_mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).Delete(pos) != null)
{
JOptionPane.showMessageDialog(null,"Объект удалён");
UpdateWindow(_mapPlanesCollectionGeneric.ShowSet());
UpdateWindow(_mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).ShowSet());
}
else
{
@ -118,12 +248,12 @@ public class FormMapWithSetPlanesGeneric {
ButtonShowStorage.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(_mapPlanesCollectionGeneric == null)
if(_mapsCollection == null)
{
return;
}
UpdateWindow(_mapPlanesCollectionGeneric.ShowSet());
UpdateWindow(_mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).ShowSet());
}
});
@ -131,19 +261,19 @@ public class FormMapWithSetPlanesGeneric {
ButtonShowOnMap.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_mapPlanesCollectionGeneric == null)
if (_mapsCollection == null)
{
return;
}
UpdateWindow(_mapPlanesCollectionGeneric.ShowOnMap());
UpdateWindow(_mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).ShowOnMap());
}
});
ButtonUp.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(_mapPlanesCollectionGeneric == null)
if(_mapsCollection== null)
{
return;
}
@ -153,7 +283,7 @@ public class FormMapWithSetPlanesGeneric {
JLabel imageWithMapAndObject = new JLabel();
imageWithMapAndObject.setPreferredSize(PictureBoxPlane.getSize());
imageWithMapAndObject.setMinimumSize(new Dimension(1, 1));
imageWithMapAndObject.setIcon(new ImageIcon(_mapPlanesCollectionGeneric.MoveObject((Direction.Up))));
imageWithMapAndObject.setIcon(new ImageIcon(_mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).MoveObject((Direction.Up))));
PictureBoxPlane.add(imageWithMapAndObject, BorderLayout.CENTER);
PictureBoxPlane.revalidate();
@ -164,7 +294,7 @@ public class FormMapWithSetPlanesGeneric {
ButtonLeft.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(_mapPlanesCollectionGeneric == null)
if(_mapsCollection == null)
{
return;
}
@ -174,7 +304,7 @@ public class FormMapWithSetPlanesGeneric {
JLabel imageWithMapAndObject = new JLabel();
imageWithMapAndObject.setPreferredSize(PictureBoxPlane.getSize());
imageWithMapAndObject.setMinimumSize(new Dimension(1, 1));
imageWithMapAndObject.setIcon(new ImageIcon(_mapPlanesCollectionGeneric.MoveObject((Direction.Left))));
imageWithMapAndObject.setIcon(new ImageIcon(_mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).MoveObject((Direction.Left))));
PictureBoxPlane.add(imageWithMapAndObject, BorderLayout.CENTER);
PictureBoxPlane.revalidate();
@ -185,7 +315,7 @@ public class FormMapWithSetPlanesGeneric {
ButtonDown.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(_mapPlanesCollectionGeneric == null)
if(_mapsCollection == null)
{
return;
}
@ -195,7 +325,7 @@ public class FormMapWithSetPlanesGeneric {
JLabel imageWithMapAndObject = new JLabel();
imageWithMapAndObject.setPreferredSize(PictureBoxPlane.getSize());
imageWithMapAndObject.setMinimumSize(new Dimension(1, 1));
imageWithMapAndObject.setIcon(new ImageIcon(_mapPlanesCollectionGeneric.MoveObject((Direction.Down))));
imageWithMapAndObject.setIcon(new ImageIcon(_mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).MoveObject((Direction.Down))));
PictureBoxPlane.add(imageWithMapAndObject, BorderLayout.CENTER);
PictureBoxPlane.revalidate();
@ -206,7 +336,7 @@ public class FormMapWithSetPlanesGeneric {
ButtonRight.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(_mapPlanesCollectionGeneric == null)
if(_mapsCollection == null)
{
return;
}
@ -216,7 +346,7 @@ public class FormMapWithSetPlanesGeneric {
JLabel imageWithMapAndObject = new JLabel();
imageWithMapAndObject.setPreferredSize(PictureBoxPlane.getSize());
imageWithMapAndObject.setMinimumSize(new Dimension(1, 1));
imageWithMapAndObject.setIcon(new ImageIcon(_mapPlanesCollectionGeneric.MoveObject((Direction.Right))));
imageWithMapAndObject.setIcon(new ImageIcon(_mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).MoveObject((Direction.Right))));
PictureBoxPlane.add(imageWithMapAndObject, BorderLayout.CENTER);
PictureBoxPlane.revalidate();
@ -224,49 +354,21 @@ public class FormMapWithSetPlanesGeneric {
}
});
//выпадающий список с вариантами карт
ComboBoxSelectorMap.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
AbstractMap map = null;
ComboBoxSelectorMap = (JComboBox)e.getSource();
String item = (String)ComboBoxSelectorMap.getSelectedItem();
switch(item)
{
case "Простая карта":
map = new SimpleMap();
break;
case "Буря в пустыне":
map = new DesertStormMap();
break;
case "Звёздные войны":
map = new StarWarsMap();
break;
}
if(map != null)
{
_mapPlanesCollectionGeneric = new MapWithSetPlanesGeneric<DrawningObjectPlane, AbstractMap>(
PictureBoxPlane.getWidth(), PictureBoxPlane.getHeight(), map);
}
else
{
_mapPlanesCollectionGeneric = null;
}
}
});
MaskedTextBoxPosition.addKeyListener(new KeyAdapter() {
@Override
public void keyTyped(KeyEvent e) {
char c = e.getKeyChar();
if ( ((c < '0') || (c > '9')) || MaskedTextBoxPosition.getText().length() >= 2) {
if ((c < '0') || (c > '9')) {
e.consume();
}
}
});
}
private void createUIComponents()
{
DefaultListModel<String> defListMod = new DefaultListModel<String>();
ListBoxMaps = new JList(defListMod);
}
}

View File

@ -84,6 +84,7 @@ public class FormParam extends JFrame
Color colorCorpus = JColorChooser.showDialog(null, "Выбор цвета", null);
EntityPlane plane = new EntityPlane(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000), colorCorpus);
IAdditionalDrawingObject addWindows = SetData();
int countWindows = rnd.nextInt(1, 4);
addWindows.SetAddEnum(countWindows);
@ -97,6 +98,7 @@ public class FormParam extends JFrame
{
JOptionPane.showMessageDialog(null, "Не удалось добавить объект");
}
}
});

View File

@ -21,8 +21,10 @@ public class FormPlane extends JDialog
private JLabel LabelWeight = new JLabel();
private JLabel LabelColor = new JLabel();
protected DrawingPlane plane;
protected DrawingPlane _plane;
protected DrawingPlane _selectedPlane;
private int PictureBoxPlaneWidth;
private int PictureBoxPlaneHeight;
public DrawingPlane GetSelectedShip()
{
@ -38,7 +40,7 @@ public class FormPlane extends JDialog
gr.setColor(new Color(238, 238, 238));
gr.fillRect(0, 0, PictureBoxPlane.getWidth(), PictureBoxPlane.getHeight());
if (plane.GetPlane() != null) {
if (_plane.GetPlane() != null) {
_plane.DrawTransport(gr);
JLabel imageOfLogo = new JLabel();
imageOfLogo.setPreferredSize(PictureBoxPlane.getSize());
@ -50,19 +52,45 @@ public class FormPlane extends JDialog
validate();
}
//создание всплывающего окна
//создание всплывающего окна (первый конструктор)
public FormPlane()
{
super(new Frame("Airbus"));
super(new Frame("Аэробус"));
CreateWindow();
setModal(true);
getContentPane().add(MainPanel);
}
//второй конструктор
protected FormPlane(DrawningObjectPlane plane)
{
super(new Frame("Аэробус"));
CreateWindow();
setModal(true);
_plane = plane.GetDrawingObjectPlane();
SetData();
}
public DrawingPlane GetSelectedPlane()
{
return _selectedPlane;
}
public void SetData()
{
Random rnd = new Random();
_plane.SetPosition(rnd.nextInt(10, 100), rnd.nextInt(10, 100),
PictureBoxPlane.getWidth(), PictureBoxPlane.getHeight());
LabelSpeed.setText("Скорость: " + _plane.GetPlane().GetSpeed() + " ");
LabelWeight.setText("Вес: " + _plane.GetPlane().GetWeight() + " ");
LabelColor.setText("Цвет: r = " + _plane.GetPlane().GetColor().getRed() + " g = " + _plane.GetPlane().GetColor().getGreen() +
" b = " + _plane.GetPlane().GetColor().getBlue());
PictureBoxPlane.add(_plane, BorderLayout.CENTER);
}
public void CreateWindow()
{
setModal(true);
//создание строки отображения скорости, веса и цвета объекта
Box LableBox = Box.createHorizontalBox();
LableBox.setMinimumSize(new Dimension(1, 20));
@ -71,6 +99,10 @@ public class FormPlane extends JDialog
LableBox.add(LabelColor);
StatusStrip.add(LableBox);
PictureBoxPlane.setBackground(Color.WHITE);
setPreferredSize(new Dimension(1000, 700));
getContentPane().add(MainPanel);
try
{
Image img = ImageIO.read(getClass().getResource("resourses/Up.png"));
@ -87,46 +119,56 @@ public class FormPlane extends JDialog
System.out.println(ex.getMessage());
}
_selectedPlane = plane;
_selectedPlane = _plane;
ButtonCreate.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try
{
PictureBoxPlane.remove(_plane);
}
catch (Exception c){ }
Random rnd = new Random();
Color colorSimple = JColorChooser.showDialog(null, "Выберите цвет", null);
plane = new DrawingPlane(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000),
_plane = new DrawingPlane(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000),
colorSimple);
plane.SetPosition(rnd.nextInt(10, 100), rnd.nextInt(10, 100),
PictureBoxPlane.getWidth(), PictureBoxPlane.getHeight());
LabelSpeed.setText("Скорость: " + plane.GetPlane().GetSpeed() + " ");
LabelWeight.setText("Вес: " + plane.GetPlane().GetWeight() + " ");
LabelColor.setText("Цвет: r = " + plane.GetPlane().GetColor().getRed() + " g = " + plane.GetPlane().GetColor().getGreen() +
" b = " + plane.GetPlane().GetColor().getBlue());
SetData();
Draw(plane);
Draw(_plane);
}
});
ButtonCreateModif.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try
{
PictureBoxPlane.remove(_plane);
}
catch (Exception c){ }
Random rnd = new Random();
Color colorSimple = JColorChooser.showDialog(null, "Выберите цвет", null);
Color colorModif = JColorChooser.showDialog(null, "Выберите цвет", null);
plane = new DrawingAirbus(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000),
_plane = new DrawingAirbus(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000),
colorSimple, colorModif, rnd.nextBoolean(), rnd.nextBoolean());
plane.SetPosition(rnd.nextInt(10, 100), rnd.nextInt(10, 100),
PictureBoxPlane.getWidth(), PictureBoxPlane.getHeight());
LabelSpeed.setText("Скорость: " + plane.GetPlane().GetSpeed() + " ");
LabelWeight.setText("Вес: " + plane.GetPlane().GetWeight() + " ");
LabelColor.setText("Цвет: r = " + plane.GetPlane().GetColor().getRed() + " g = " + plane.GetPlane().GetColor().getGreen() +
" b = " + plane.GetPlane().GetColor().getBlue());
SetData();
Draw(plane);
Draw(_plane);
}
});
ButtonSelectPlane.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
_selectedPlane = _plane;
dispose();
}
});
@ -135,48 +177,56 @@ public class FormPlane extends JDialog
public void componentResized(ComponentEvent e) {
super.componentResized(e);
if(plane != null)
if(_plane == null)
{
plane.ChangeBorders(PictureBoxPlane.getWidth(), PictureBoxPlane.getHeight());
PictureBoxPlane.revalidate();
Draw(plane);
return;
}
_plane.ChangeBorders(PictureBoxPlane.getWidth(), PictureBoxPlane.getHeight());
try
{
PictureBoxPlane.remove(_plane);
}
catch (Exception c){ }
PictureBoxPlane.add(_plane, BorderLayout.CENTER);
}
});
ButtonUp.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
plane.MoveTransport(Direction.Up);
_plane.MoveTransport(Direction.Up);
PictureBoxPlane.revalidate();
Draw(plane);
Draw(_plane);
}
});
ButtonLeft.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
plane.MoveTransport(Direction.Left);
_plane.MoveTransport(Direction.Left);
PictureBoxPlane.revalidate();
Draw(plane);
Draw(_plane);
}
});
ButtonDown.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
plane.MoveTransport(Direction.Down);
_plane.MoveTransport(Direction.Down);
PictureBoxPlane.revalidate();
Draw(plane);
Draw(_plane);
}
});
ButtonRight.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
plane.MoveTransport(Direction.Right);
_plane.MoveTransport(Direction.Right);
PictureBoxPlane.revalidate();
Draw(plane);
Draw(_plane);
}
});
@ -184,7 +234,7 @@ public class FormPlane extends JDialog
ButtonSelectPlane.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
_selectedPlane = plane;
_selectedPlane = _plane;
dispose();
}
});

View File

@ -5,11 +5,11 @@ public class Main
public static void main(String[] args)
{
JFrame frame = new JFrame("Аэробус");
frame.setContentPane(new FormParam().MainPanel);
frame.setContentPane(new FormMapWithSetPlanesGeneric().MainPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocation(500, 200);
frame.setLocation(200, 100);
frame.pack();
frame.setSize(800, 500);
frame.setSize(1000, 700);
frame.setVisible(true);
}
}

View File

@ -1,5 +1,6 @@
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.LinkedList;
public class MapWithSetPlanesGeneric <T extends IDrawningObject, U extends AbstractMap>
{
@ -18,6 +19,9 @@ public class MapWithSetPlanesGeneric <T extends IDrawningObject, U extends Abstr
//набор объектов
private final SetPlanesGeneric<T> _setPlanes;
//набор удалённых объектов (двусвязный список)
public final LinkedList<T> _deletePlane;
//карта
private final U _map;
@ -27,6 +31,7 @@ public class MapWithSetPlanesGeneric <T extends IDrawningObject, U extends Abstr
int width = picWidth / _placeSizeWidth;
int height = picHeight / _placeSizeHeight;
_setPlanes = new SetPlanesGeneric<T>(width * height);
_deletePlane = new LinkedList<>();
_pictureWidth = picWidth;
_pictureHeight = picHeight;
_map = map;
@ -39,7 +44,10 @@ public class MapWithSetPlanesGeneric <T extends IDrawningObject, U extends Abstr
public T Delete(int position)
{
return _setPlanes.Remove(position);
T temp = _setPlanes.Remove(position);
_deletePlane.add(temp);
return temp;
}
//вывод всего набора объектов
@ -49,6 +57,7 @@ public class MapWithSetPlanesGeneric <T extends IDrawningObject, U extends Abstr
Graphics gr = bmp.getGraphics();
DrawBackground(gr);
DrawPlanes(gr);
return bmp;
}
@ -164,7 +173,7 @@ public class MapWithSetPlanesGeneric <T extends IDrawningObject, U extends Abstr
g2d.setColor(new Color(0xFF, 0xB6, 0xC1));
int[] xPoint = {(i * 70 - 10) + 45, i * 70 - 10, i * 70 - 10};
int[] yPoint = {30, 50, 10};
g.fillPolygon(xPoint, yPoint, xPoint.length);
g2d.fillPolygon(xPoint, yPoint, xPoint.length);
g2d.setStroke(new BasicStroke(3));
g2d.setColor(Color.BLACK);
@ -179,7 +188,7 @@ public class MapWithSetPlanesGeneric <T extends IDrawningObject, U extends Abstr
{
int position = 0;
int currentWidth = 1;
int currentHeight = 5;
int currentHeight = 6;
for (int i = 0; i < _setPlanes.Count(); i++)
{
@ -202,4 +211,19 @@ public class MapWithSetPlanesGeneric <T extends IDrawningObject, U extends Abstr
}
}
}
public T GetPlaneInList(int index)
{
return _setPlanes.Get(index);
}
public T GetPlaneInDelete()
{
if(_deletePlane.isEmpty())
{
return null;
}
return _deletePlane.pop();
}
}

View File

@ -0,0 +1,66 @@
import java.util.*;
//класс для хранения коллекции карт
public class MapsCollection
{
//словарь (хранилище) с картами
public HashMap<String, MapWithSetPlanesGeneric<DrawningObjectPlane, AbstractMap>> _mapStorage;
//возвращение списка названий карт
public ArrayList<String> Keys()
{
return new ArrayList<>(_mapStorage.keySet());
}
//ширина окна отрисовки
private final int _pictureWidth;
//высота окна отрисовки
private final int _pictureHeight;
//конструктор
public MapsCollection(int pictureWidth, int pictureHeight)
{
_mapStorage = new HashMap<>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
//добавление карты
public void AddMap(String name, AbstractMap map)
{
if(_mapStorage.containsKey(name))
{
return;
}
_mapStorage.put(name, new MapWithSetPlanesGeneric<>(_pictureWidth, _pictureHeight, map));
}
//удаление карты
public void DelMap(String name)
{
_mapStorage.remove(name);
}
//Доступ к аэродрому
public MapWithSetPlanesGeneric<DrawningObjectPlane, AbstractMap> get(String index)
{
if(_mapStorage.containsKey(index))
{
return _mapStorage.get(index);
}
return null;
}
public DrawningObjectPlane Get(String name, int index)
{
if(_mapStorage.containsKey(name))
{
return _mapStorage.get(name).GetPlaneInList(index);
}
return null;
}
}

View File

@ -1,18 +1,25 @@
public class SetPlanesGeneric<T extends Object>
import java.util.ArrayList;
import java.util.Iterator;
public class SetPlanesGeneric<T extends Object> implements Iterable<T>
{
//массив объектов, которые храним
private final Object[] _places;
private ArrayList<T> _places;
//максимальное кол-во элементов в списке
private final int _maxCount;
//количество объектов в массиве
public int Count()
{
return _places.length;
return _places.size();
}
//конструктор
public SetPlanesGeneric(int count)
{
_places = new Object[count];
_maxCount = count;
_places = new ArrayList<>();
}
//добавление объекта в набор
@ -25,43 +32,12 @@ public class SetPlanesGeneric<T extends Object>
public int Insert(T plane, int position)
{
//проверка на корректность значения индекса
if (position >= _places.length || position < 0)
if (position >= _maxCount|| position < 0)
{
return -1;
}
//проверка ячейки на пустоту
if (_places[position] == null)
{
_places[position] = plane;
return position;
}
//поиск первой свободной ячейки
int _emptyPositionIndex = -1;
for (int i = position + 1; i < Count(); i++)
{
if (_places[i] == null)
{
_emptyPositionIndex = i;
break;
}
}
//есла пустых ячеек нет
if (_emptyPositionIndex < 0)
{
return -1;
}
//сдвиг объектов
for (int i = _emptyPositionIndex; i > position; i--)
{
_places[i] = _places[i - 1];
}
_places[position] = plane;
_places.add(plane);
return position;
}
@ -70,14 +46,14 @@ public class SetPlanesGeneric<T extends Object>
public T Remove(int position)
{
// проверка позиции
if (position >= _places.length || position < 0)
if (position >= _maxCount || position < 0)
{
return null;
}
// удаление объекта из массива, присовив элементу массива значение null
T temp = (T)_places[position];
_places[position] = null;
T temp = _places.get(position);
_places.remove(position);
return temp;
}
@ -85,15 +61,27 @@ public class SetPlanesGeneric<T extends Object>
//получение объекта из набора по позиции
public T Get(int position)
{
if (position >= _places.length || position < 0)
{
return null;
}
else if (_places[position] == null)
if (position >= _maxCount || position < 0)
{
return null;
}
return (T)_places[position];
return _places.get(position);
}
public void Set(int position, T value)
{
if(position >= _maxCount || position < 0)
{
return;
}
Insert(value, position);
}
@Override
public Iterator<T> iterator()
{
return _places.iterator();
}
}