Zhimolostnova A.V. Hard lab work 4 #9

Closed
AnnZhimol wants to merge 6 commits from LabRab_4 into LabRab_3
8 changed files with 352 additions and 210 deletions

View File

@ -1,60 +0,0 @@
import javax.swing.*;
import java.awt.*;
import java.util.Random;
public class DrawingField extends JPanel{
private final FormWarship Field;
private DrawingWarship _warship=null;
public DrawingWarship SelectedWarship;
public DrawingField(FormWarship field) {
Field = field;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 =(Graphics2D)g;
if (_warship!=null)
_warship.DrawTransport(g2);
else return;
}
public void DirectionButtonAction(Direction side){
if(_warship == null)
return;
_warship.MoveTransport(side);
}
private void SetData() {
Random rand=new Random();
_warship.SetPosition(rand.nextInt(100)+10,rand.nextInt(100)+10,getWidth(),getHeight());
Field.SpeedLabel.setText("Скорость: "+_warship.GetWarship().GetSpeed());
Field.WeightLabel.setText("Вес: "+_warship.GetWarship().GetWeight());
Field.BodyColorLabel.setText("Цвет: "+Integer.toHexString(_warship.GetWarship().GetBodyColor().getRGB()).substring(2));
}
public void CreateButtonAction(){
Random rand=new Random();
_warship=new DrawingWarship(rand.nextInt(50)+10,rand.nextInt(3000)+20000,new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)),rand.nextInt(3));
SetData();
}
public void CreateModifButtonAction(){
Random rand=new Random();
_warship = new DrawingAdvancedWarship(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.nextBoolean(),rand.nextInt(3));
SetData();
}
public void ResizeField(){
if (_warship!=null)
_warship.ChangeBorders(getWidth(),getHeight());
else return;
}
public void Draw(Graphics2D graphics) {
if (_warship!=null)
_warship.DrawTransport(graphics);
else return;
}
public DrawingWarship GetDrawingWarship() {
return _warship;
}
}

View File

@ -2,7 +2,7 @@ import java.awt.*;
public class DrawingObjectWarship implements IDrawingObject {
private DrawingWarship _warship=null;
private DrawingWarship _warship;
public DrawingObjectWarship(DrawingWarship warship)
{
@ -36,4 +36,7 @@ public class DrawingObjectWarship implements IDrawingObject {
return _warship.GetCurrentPosition();
return null;
}
public DrawingWarship GetWarship() {
return _warship;
}
}

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="8" y="20" width="691" height="470"/>
<xy x="8" y="20" width="691" height="602"/>
</constraints>
<properties/>
<border type="none"/>
@ -23,7 +23,7 @@
</hspacer>
</children>
</grid>
<grid id="4ec10" binding="GroupBoxTools" layout-manager="GridLayoutManager" row-count="11" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="4ec10" binding="GroupBoxTools" layout-manager="GridLayoutManager" row-count="12" 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="0" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
@ -33,20 +33,9 @@
<font/>
</border>
<children>
<component id="cd72b" class="javax.swing.JComboBox" binding="СomboBoxSelectorMap">
<constraints>
<grid row="0" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<model>
<item value="Первая карта"/>
<item value="Вторая карта"/>
</model>
</properties>
</component>
<vspacer id="b5fa4">
<constraints>
<grid row="8" column="1" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
<grid row="9" column="1" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
<component id="5312c" class="javax.swing.JButton" binding="ButtonAddWarship">
@ -67,7 +56,7 @@
</component>
<component id="a8d48" class="javax.swing.JButton" binding="ButtonShowStorage">
<constraints>
<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"/>
<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"/>
</constraints>
<properties>
<text value="Посмотреть хранилище"/>
@ -75,7 +64,7 @@
</component>
<component id="c5558" class="javax.swing.JButton" binding="ButtonShowOnMap">
<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="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"/>
</constraints>
<properties>
<text value="Посмотреть карту"/>
@ -83,7 +72,7 @@
</component>
<component id="11722" class="javax.swing.JButton" binding="ButtonDown">
<constraints>
<grid row="10" 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="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"/>
</constraints>
<properties>
<icon value="arrowDown.jpg"/>
@ -93,7 +82,7 @@
</component>
<component id="a2c02" class="javax.swing.JButton" binding="ButtonUp">
<constraints>
<grid row="9" 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="10" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<icon value="arrowUp.jpg"/>
@ -103,7 +92,7 @@
</component>
<component id="2380f" class="javax.swing.JButton" binding="ButtonLeft">
<constraints>
<grid row="10" 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="11" 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>
<icon value="arrowLeft.jpg"/>
@ -113,7 +102,7 @@
</component>
<component id="de75d" class="javax.swing.JButton" binding="ButtonRight">
<constraints>
<grid row="10" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
<grid row="11" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<icon value="arrowRight.jpg"/>
@ -131,7 +120,7 @@
</component>
<vspacer id="d7861">
<constraints>
<grid row="5" column="1" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
<grid row="6" column="1" 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="42f1e">
@ -139,6 +128,69 @@
<grid row="1" column="1" 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="93fdf" 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="3" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<name value=""/>
</properties>
<border type="bevel-lowered" title="Карты" title-justification="1" title-position="2"/>
<children>
<component id="cd72b" 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="Первая карта"/>
<item value="Вторая карта"/>
</model>
</properties>
</component>
<component id="19c2f" 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="ac509" 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="Добавить карту"/>
</properties>
</component>
<component id="bf9c2" 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="Удалить карту"/>
</properties>
</component>
<component id="2aa62" 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="2934d" class="javax.swing.JButton" binding="ButtonShowDeleted">
<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"/>
</constraints>
<properties>
<text value="Показать удаленный корабль"/>
</properties>
</component>
</children>
</grid>
</children>

View File

@ -1,6 +1,10 @@
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Optional;
public class FormMapWithSetWarships extends JFrame{
@ -17,8 +21,20 @@ public class FormMapWithSetWarships extends JFrame{
private JPanel PictureBox;
private JPanel MainPanel;
private JPanel GroupBoxTools;
private MapWithSetWarshipsGeneric<DrawingObjectWarship,AbstractMap> _mapWarshipsCollectionGeneric;
private JTextField TextFieldMap;
private JButton CreateMapButton;
private JButton DeleteMapButton;
private JList ListBoxMaps;
private JPanel MapPanel;
private JButton ButtonShowDeleted;
private Image bufferedImage;
private MapsCollection _mapsCollection;
private final HashMap<String,AbstractMap> _mapsDict=new HashMap<String,AbstractMap>(){
{
put("Первая карта",new SimpleMap());
put("Вторая карта",new SecondMap());
}
};
public FormMapWithSetWarships(){
setTitle("Военный корабль");
@ -28,6 +44,11 @@ public class FormMapWithSetWarships extends JFrame{
ShowWindow();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
_mapsCollection=new MapsCollection(getWidth(),getHeight());
СomboBoxSelectorMap.removeAllItems();
for(String elem:_mapsDict.keySet()){
СomboBoxSelectorMap.addItem(elem);
}
}
@Override
@ -40,28 +61,79 @@ public class FormMapWithSetWarships 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(){
ButtonShowOnMap.addActionListener(e -> {
if (_mapWarshipsCollectionGeneric == null)
ListBoxMaps.addListSelectionListener(e -> {
if(ListBoxMaps.getSelectedIndex()==-1)
return;
bufferedImage = _mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).ShowSet();
repaint();
});
DeleteMapButton.addActionListener(e -> {
if (ListBoxMaps.getSelectedIndex() == -1)
{
return;
}
bufferedImage = _mapWarshipsCollectionGeneric.ShowOnMap();
if(JOptionPane.showConfirmDialog(this,"Удалить карту "+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,"Не все данные заполнены","Ошибка",JOptionPane.ERROR_MESSAGE);
return;
}
if (!_mapsDict.containsKey(СomboBoxSelectorMap.getSelectedItem()))
{
JOptionPane.showMessageDialog(this,"Нет такой карты","Ошибка",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();
});
ButtonShowStorage.addActionListener(e -> {
if (_mapWarshipsCollectionGeneric == null)
if (ListBoxMaps.getSelectedIndex() ==-1)
{
return;
}
bufferedImage = _mapWarshipsCollectionGeneric.ShowSet();
bufferedImage = _mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).ShowSet();
repaint();
});
ButtonAddWarship.addActionListener(e -> {
if (_mapWarshipsCollectionGeneric == null)
if (ListBoxMaps.getSelectedIndex() ==-1)
{
return;
}
@ -73,9 +145,9 @@ public class FormMapWithSetWarships extends JFrame{
if (dialog.getSelectedWarship()!=null) {
DrawingObjectWarship warship = new DrawingObjectWarship(dialog.getSelectedWarship());
if (_mapWarshipsCollectionGeneric.plus(warship) >= 0) {
if (_mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).plus(warship)>=0) {
JOptionPane.showMessageDialog(this, "Объект добавлен", "Успех", JOptionPane.INFORMATION_MESSAGE);
bufferedImage = _mapWarshipsCollectionGeneric.ShowSet();
bufferedImage = _mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).ShowSet();
repaint();
} else {
JOptionPane.showMessageDialog(this, "Не удалось добавить объект", "Ошибка",JOptionPane.INFORMATION_MESSAGE);
@ -85,7 +157,7 @@ public class FormMapWithSetWarships extends JFrame{
ButtonRemoveWarship.addActionListener(e -> {
String txt=TextBoxPosition.getText();
if (txt==null||_mapWarshipsCollectionGeneric==null||txt.isEmpty())
if (txt==null||ListBoxMaps.getSelectedIndex() ==-1)
{
return;
}
@ -94,15 +166,12 @@ public class FormMapWithSetWarships extends JFrame{
"Удалить объект?",
"Удаление",
JOptionPane.YES_NO_CANCEL_OPTION);
if (result == JOptionPane.NO_OPTION)
{
if (result!=0)
return;
}
int pos = Integer.parseInt(txt);
if (_mapWarshipsCollectionGeneric.minus(pos) !=null)
{
JOptionPane.showMessageDialog(this,"Объект удален","Успех",JOptionPane.INFORMATION_MESSAGE);
bufferedImage = _mapWarshipsCollectionGeneric.ShowSet();
if(_mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).minus(pos)!=null){
JOptionPane.showMessageDialog(this, "Объект удален", "Успех", JOptionPane.INFORMATION_MESSAGE);
bufferedImage = _mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).ShowSet();
repaint();
}
else
@ -111,53 +180,44 @@ public class FormMapWithSetWarships extends JFrame{
}
});
СomboBoxSelectorMap.addActionListener(e -> {
AbstractMap map = null;
switch (СomboBoxSelectorMap.getSelectedItem().toString())
{
case "Первая карта":
map = new SimpleMap();
break;
case "Вторая карта":
map = new SecondMap();
break;
}
if (map != null)
{
_mapWarshipsCollectionGeneric = new MapWithSetWarshipsGeneric<>(
PictureBox.getWidth(), PictureBox.getHeight(), map);
}
else
{
_mapWarshipsCollectionGeneric = null;
ButtonShowDeleted.addActionListener(e -> {
if (ListBoxMaps.getSelectedIndex()==-1)
return;
DrawingObjectWarship warship=(DrawingObjectWarship)_mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).GetWarshipInDeleted();
if(warship!=null){
FormWarship dialog=new FormWarship();
dialog.SetDrawingWarship(warship.GetWarship());
dialog.setSize(1200,700);
dialog.setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
}
});
ButtonUp.addActionListener(e -> {
if (_mapWarshipsCollectionGeneric != null) {
bufferedImage = _mapWarshipsCollectionGeneric.MoveObject(Direction.Up);
if (ListBoxMaps.getSelectedIndex() !=-1) {
bufferedImage = _mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).MoveObject(Direction.Up);
repaint();
}
});
ButtonDown.addActionListener(e -> {
if (_mapWarshipsCollectionGeneric != null) {
bufferedImage = _mapWarshipsCollectionGeneric.MoveObject(Direction.Down);
if (ListBoxMaps.getSelectedIndex() !=-1) {
bufferedImage = _mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).MoveObject(Direction.Down);
repaint();
}
});
ButtonRight.addActionListener(e -> {
if (_mapWarshipsCollectionGeneric != null) {
bufferedImage = _mapWarshipsCollectionGeneric.MoveObject(Direction.Right);
if (ListBoxMaps.getSelectedIndex() !=-1) {
bufferedImage = _mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).MoveObject(Direction.Right);
repaint();
}
});
ButtonLeft.addActionListener(e -> {
if (_mapWarshipsCollectionGeneric != null) {
bufferedImage = _mapWarshipsCollectionGeneric.MoveObject(Direction.Left);
if (ListBoxMaps.getSelectedIndex() !=-1) {
bufferedImage = _mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).MoveObject(Direction.Left);
repaint();
}
});

View File

@ -2,11 +2,11 @@ import javax.swing.*;
import java.awt.*;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.util.Random;
public class FormWarship extends JDialog{
private int Width;
private int Height;
DrawingField field = new DrawingField(this);
DrawingWarship SelectedWarship;
private JButton ButtonDown;
private JButton ButtonRight;
@ -19,47 +19,76 @@ public class FormWarship extends JDialog{
public JLabel BodyColorLabel;
private JPanel PictureBox;
private JButton ButtonSelect;
private DrawingWarship _warship=null;
public FormWarship(JFrame frame){
super(frame,"Военный корабль");
public FormWarship(){
setTitle("Военный корабль");
setContentPane(PictureBox);
setSize(1000,700);
Width = getWidth();
Height = getHeight();
setSize(1000,500);
setResizable(false);
ShowWindow();
field.setBounds(0,0,Width,Height);
setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
setVisible(true);
}
private void SetData() {
Random rand=new Random();
_warship.SetPosition(rand.nextInt(100)+10,rand.nextInt(100)+10,getWidth(),getHeight());
SpeedLabel.setText("Скорость: "+_warship.GetWarship().GetSpeed());
WeightLabel.setText("Вес: "+_warship.GetWarship().GetWeight());
BodyColorLabel.setText("Цвет: "+Integer.toHexString(_warship.GetWarship().GetBodyColor().getRGB()).substring(2));
}
@Override
public void paint(Graphics g) {
super.paint(g);
if (_warship != null) {
PictureBox.paintComponents(PictureBox.getGraphics());
_warship.DrawTransport(g);
}
}
private void ShowWindow(){
ButtonCreate.addActionListener(e -> {
field.CreateButtonAction();
ReDraw();
Random rand=new Random();
_warship=new DrawingWarship(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 -> {
field.CreateModifButtonAction();
ReDraw();
Random rand=new Random();
_warship = new DrawingAdvancedWarship(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.nextBoolean(),rand.nextInt(3));
SetData();
repaint();
});
ButtonUp.addActionListener(e -> {
field.DirectionButtonAction(Direction.Up);
ReDraw();
if(_warship == null)
return;
_warship.MoveTransport(Direction.Up);
repaint();
});
ButtonLeft.addActionListener(e -> {
field.DirectionButtonAction(Direction.Left);
ReDraw();
if(_warship == null)
return;
_warship.MoveTransport(Direction.Left);
repaint();
});
ButtonRight.addActionListener(e -> {
field.DirectionButtonAction(Direction.Right);
ReDraw();
if(_warship == null)
return;
_warship.MoveTransport(Direction.Right);
repaint();
});
ButtonDown.addActionListener(e -> {
field.DirectionButtonAction(Direction.Down);
ReDraw();
if(_warship == null)
return;
_warship.MoveTransport(Direction.Down);
repaint();
});
ButtonSelect.addActionListener(e -> {
SelectedWarship=field.GetDrawingWarship();
SelectedWarship=GetDrawingWarship();
JOptionPane.showMessageDialog(PictureBox, "Корабль добавлен.");
});
@ -69,17 +98,20 @@ public class FormWarship extends JDialog{
super.componentResized(e);
Width=getWidth();
Height=getHeight();
field.ResizeField();
ReDraw();
field.setBounds(0,0,Width,Height);
if (_warship!=null)
_warship.ChangeBorders(getWidth(),getHeight());
else return;
repaint();
setBounds(0,0,Width,Height);
}
});
}
private void ReDraw() {
Graphics2D graphics = (Graphics2D) PictureBox.getGraphics();
graphics.clearRect(0, 0, PictureBox.getWidth(), PictureBox.getHeight());
PictureBox.paintComponents(graphics);
field.Draw(graphics);
public DrawingWarship GetDrawingWarship() {
return _warship;
}
public void SetDrawingWarship(DrawingWarship warship) {
_warship=warship;
SetData();
repaint();
}
}

View File

@ -1,5 +1,6 @@
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.LinkedList;
public class MapWithSetWarshipsGeneric<T extends IDrawingObject,U extends AbstractMap> {
private final int _pictureWidth;
@ -7,6 +8,7 @@ public class MapWithSetWarshipsGeneric<T extends IDrawingObject,U extends Abstra
private final int _placeSizeWidth = 120;
private final int _placeSizeHeight = 50;
private final SetWarshipsGeneric<T> _setWarship;
private final LinkedList<T> DeletedWarships;
private final U _map;
public MapWithSetWarshipsGeneric(int picWidth,int picHeight, U map)
@ -17,6 +19,7 @@ public class MapWithSetWarshipsGeneric<T extends IDrawingObject,U extends Abstra
_pictureWidth = picWidth;
_pictureHeight = picHeight;
_map = map;
DeletedWarships=new LinkedList<>();
}
public int plus(T warship)
@ -26,7 +29,9 @@ public class MapWithSetWarshipsGeneric<T extends IDrawingObject,U extends Abstra
public T minus(int position)
{
return _setWarship.Remove(position);
T warship=_setWarship.Remove(position);
DeletedWarships.push(warship);
return warship;
}
public Image ShowSet()
@ -41,13 +46,9 @@ public class MapWithSetWarshipsGeneric<T extends IDrawingObject,U extends Abstra
public Image ShowOnMap()
{
Shaking();
for (int i = 0; i < _setWarship.Count(); i++)
for (IDrawingObject warship : _setWarship)
{
T warship = _setWarship.Get(i);
if (warship != null)
{
return _map.CreateMap(_pictureWidth, _pictureHeight, warship);
}
return _map.CreateMap(_pictureWidth, _pictureHeight, warship);
}
return new BufferedImage(_pictureWidth, _pictureHeight,1);
}
@ -111,16 +112,27 @@ public class MapWithSetWarshipsGeneric<T extends IDrawingObject,U extends Abstra
private void DrawWarship(Graphics gr)
{
int i=0;
int width = _pictureWidth / _placeSizeWidth;
int height = _pictureHeight / _placeSizeHeight;
for (int i = 0; i < _setWarship.Count(); i++)
for (IDrawingObject warship : _setWarship)
{
if (_setWarship.Get(i) != null)
{
_setWarship.Get(i).SetObject(i % width * _placeSizeWidth, (height - 1 - i / width) * _placeSizeHeight, _pictureWidth, _pictureHeight);
_setWarship.Get(i).DrawingObject(gr);
}
if (warship==null)
return;
_setWarship.Get(i).SetObject(i % width * _placeSizeWidth, (height - 1 - i / width) * _placeSizeHeight, _pictureWidth, _pictureHeight);
_setWarship.Get(i).DrawingObject(gr);
i++;
}
}
public T GetWarshipInList(int i){
return _setWarship.Get(i);
}
public T GetWarshipInDeleted() {
if(DeletedWarships.isEmpty())
return null;
return DeletedWarships.pop();
}
}

48
src/MapsCollection.java Normal file
View File

@ -0,0 +1,48 @@
import java.util.ArrayList;
import java.util.HashMap;
public class MapsCollection {
private final HashMap<String,MapWithSetWarshipsGeneric<DrawingObjectWarship,AbstractMap>> _mapStorages;
private final int _pictureWidth;
private final int _pictureHeight;
public MapsCollection(int pictureWidth,int pictureHeight){
_mapStorages=new HashMap<>();
_pictureWidth=pictureWidth;
_pictureHeight=pictureHeight;
}
public ArrayList<String> Keys(){
return new ArrayList<>(_mapStorages.keySet());
}
public void AddMap(String name, AbstractMap map){
if (_mapStorages.containsKey(name)){
return;
}
else{
_mapStorages.put(name,new MapWithSetWarshipsGeneric<>(_pictureWidth,_pictureHeight,map));
}
}
public void DelMap(String name){
_mapStorages.remove(name);
}
public MapWithSetWarshipsGeneric<DrawingObjectWarship,AbstractMap> get(String name){
if (_mapStorages.containsKey(name)){
return _mapStorages.get(name);
}
return null;
}
public DrawingObjectWarship get(String name,int i) {
if (_mapStorages.containsKey(name))
{
return _mapStorages.get(name).GetWarshipInList(i);
}
return null;
}
}

View File

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