Zhimolostnova A.V. Hard lab work 3 #5

Closed
AnnZhimol wants to merge 7 commits from LabRab_3 into LabRab_2
16 changed files with 994 additions and 329 deletions

39
src/CreaterGeneric.java Normal file
View File

@ -0,0 +1,39 @@
import java.util.ArrayList;
import java.util.Random;
public class CreaterGeneric<T extends EntityWarship, U extends IDrawingObjectBlock> {
ArrayList<T> Warships;
ArrayList<U> Blocks;
int WarshipsCount=0;
int BlocksCount=0;
public CreaterGeneric(int warshipsCount,int blocksCount){
Warships=new ArrayList<>(warshipsCount);
Blocks=new ArrayList<>(blocksCount);
}
public int Add(T warship){
if(WarshipsCount<=Warships.size()){
Warships.add(warship);
WarshipsCount++;
return WarshipsCount-1;
}
return -1;
}
public int Add(U block){
if(BlocksCount<=Blocks.size()){
Blocks.add(block);
BlocksCount++;
return BlocksCount-1;
}
return -1;
}
public DrawingWarship NewWarshipCreating()
{
Random rand=new Random();
T warship = Warships.get(rand.nextInt(WarshipsCount));
U block = Blocks.get(rand.nextInt(BlocksCount));
if(warship instanceof EntityAdvancedWarship){
return new DrawingAdvancedWarship(warship,block);
}
return new DrawingWarship(warship,block);
}
}

View File

@ -8,6 +8,15 @@ public class DrawingAdvancedWarship extends DrawingWarship {
Blocks= GetFormOfBlock(blockForm);
Blocks.SetBlockCount(2*(int)(Math.random()*3+1));
}
public DrawingAdvancedWarship(int speed, float weight, Color bodyColor, Color dopColor, boolean Helipad,boolean Antenna, boolean Missile,IDrawingObjectBlock blockForm) {
super(speed, weight, bodyColor, 120, 50);
Warship=new EntityAdvancedWarship(speed,weight,bodyColor,dopColor,Helipad,Antenna,Missile);
Blocks= blockForm;
}
public DrawingAdvancedWarship(EntityWarship warship,IDrawingObjectBlock block){
super(warship,block);
Warship = warship;
}
@Override
public void DrawTransport(Graphics g) {

View File

@ -4,7 +4,9 @@ import java.util.Random;
public class DrawingField extends JPanel{
private final FormWarship Field;
DrawingWarship _warship=null;
private DrawingWarship _warship=null;
public DrawingWarship SelectedWarship;
public DrawingField(FormWarship field) {
Field = field;
}
@ -51,4 +53,8 @@ public class DrawingField extends JPanel{
_warship.DrawTransport(graphics);
else return;
}
public DrawingWarship GetDrawingWarship() {
return _warship;
}
}

View File

@ -1,65 +0,0 @@
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;
public class DrawingMap extends JPanel {
private final FormMap Map;
private AbstractMap _abstractMap;
BufferedImage bufferedImage;
public DrawingMap(FormMap map){
Map=map;
_abstractMap = new SimpleMap();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(bufferedImage,0,0,null);
}
private void SetData(DrawingWarship warship) {
Random rand=new Random();
warship.SetPosition(rand.nextInt(100)+10,rand.nextInt(100)+10,getWidth(),getHeight());
Map.SpeedLabel.setText("Скорость: "+warship.GetWarship().GetSpeed());
Map.WeightLabel.setText("Вес: "+warship.GetWarship().GetWeight());
Map.BodyColorLabel.setText("Цвет: "+Integer.toHexString(warship.GetWarship().GetBodyColor().getRGB()).substring(2));
bufferedImage = _abstractMap.CreateMap(700,550,new DrawingObjectWarship(warship));
}
public void CreateButtonAction(){
Random rand=new Random();
Color color1=new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256));
DrawingWarship warship=new DrawingWarship(rand.nextInt(50)+10,rand.nextInt(3000)+20000,color1,rand.nextInt(3));
SetData(warship);
}
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));
DrawingWarship warship = new DrawingAdvancedWarship(rand.nextInt(50) + 10, rand.nextInt(3000) + 20000, color1,
color2, rand.nextBoolean(), rand.nextBoolean(), rand.nextBoolean(),rand.nextInt(3));
SetData(warship);
}
public void DirectionButtonAction(Direction side){
if(_abstractMap != null){
bufferedImage = _abstractMap.MoveObject(side);
}
}
public void ComboBoxSelectorMapAction(String name){
switch (name){
case "Простая карта":
_abstractMap = new SimpleMap();
break;
case "Вторая карта":
_abstractMap = new SecondMap();
break;
}
}
public void DrawMap(Graphics g){
g.drawImage(bufferedImage,0,0,null);
}
}

View File

@ -20,6 +20,15 @@ public class DrawingWarship {
Blocks= GetFormOfBlock(blockForm);
Blocks.SetBlockCount(2*(int)(Math.random()*3+1));
}
public DrawingWarship(int speed, float weight, Color bodyColor, IDrawingObjectBlock blockForm)
{
Warship = new EntityWarship(speed, weight, bodyColor);
Blocks= blockForm;
}
public DrawingWarship(EntityWarship warship,IDrawingObjectBlock block){
Warship = warship;
Blocks = block;
}
public IDrawingObjectBlock GetFormOfBlock(int FormOfBlock){
BlockForm temp = null;

258
src/FormCreater.form Normal file
View File

@ -0,0 +1,258 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="FormCreater">
<grid id="27dc6" binding="PictureBox" 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="630" height="629"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<grid id="27a9" binding="PropertiesPanel" layout-manager="GridLayoutManager" row-count="8" 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>
<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"/>
</constraints>
<properties>
<opaque value="true"/>
</properties>
<border type="etched" title="Настройки"/>
<children>
<vspacer id="c17c4">
<constraints>
<grid row="7" 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="e26e" layout-manager="GridLayoutManager" row-count="2" 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="2" column="0" row-span="1" col-span="2" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="etched" title="Тип корабля:"/>
<children>
<component id="beec9" class="javax.swing.JRadioButton" binding="BasicRadioButton">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Обычный корабль"/>
</properties>
</component>
<component id="5837d" class="javax.swing.JRadioButton" binding="AdvancedRadioButton">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Продвинутый корабль"/>
</properties>
</component>
</children>
</grid>
<grid id="d8b7b" layout-manager="GridLayoutManager" row-count="2" 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>
<grid row="0" column="0" row-span="1" col-span="2" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="etched" title="Характеристики корабля:"/>
<children>
<component id="9e0cc" class="javax.swing.JTextField" binding="WeightTextField">
<constraints>
<grid row="1" column="1" 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="67385" class="javax.swing.JTextField" binding="SpeedTextField">
<constraints>
<grid row="0" column="1" 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="77c84" class="javax.swing.JLabel" binding="SetWeightLabel">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Вес"/>
</properties>
</component>
<component id="ff8c4" class="javax.swing.JLabel" binding="SetSpeedLabel">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Скорость"/>
</properties>
</component>
</children>
</grid>
<grid id="d3b9d" binding="CargoPanel" layout-manager="GridLayoutManager" row-count="3" 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="4" column="0" row-span="1" col-span="2" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="etched" title="Характеристики груза:"/>
<children>
<component id="fd3ec" class="javax.swing.JRadioButton" binding="TriangleFormRadioButton">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Треугольные ящики"/>
</properties>
</component>
<component id="d03eb" class="javax.swing.JRadioButton" binding="RoundFormRadioButton">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<label value="Круглые ящики"/>
<text value="Круглые ящики"/>
</properties>
</component>
<component id="5048f" class="javax.swing.JRadioButton" binding="RectangleFormRadioButton">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Прямоугольные ящики"/>
</properties>
</component>
</children>
</grid>
<grid id="e7910" binding="CountOfBlocksPanel" layout-manager="GridLayoutManager" row-count="3" 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="3" column="0" row-span="1" col-span="2" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="etched" title="Количество ящиков:"/>
<children>
<component id="f4e43" class="javax.swing.JRadioButton" binding="SixRadioButton">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="6"/>
</properties>
</component>
<component id="38916" class="javax.swing.JRadioButton" binding="FourRadioButton">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="4"/>
</properties>
</component>
<component id="538af" class="javax.swing.JRadioButton" binding="TwoRadioButton">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="2"/>
</properties>
</component>
</children>
</grid>
<grid id="ded4" layout-manager="GridLayoutManager" row-count="2" 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="6" column="0" row-span="1" col-span="2" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="413bc" class="javax.swing.JButton" binding="ShowWarshipButton">
<constraints>
<grid row="0" 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>
<label value="Показать"/>
<text value="Показать"/>
</properties>
</component>
<component id="8cb53" class="javax.swing.JButton" binding="ChooseButton">
<constraints>
<grid row="1" 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>
<label value="Выбрать"/>
<text value="Выбрать"/>
</properties>
</component>
</children>
</grid>
<vspacer id="106a9">
<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="fc97" layout-manager="GridLayoutManager" row-count="3" 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="1" column="0" row-span="1" col-span="2" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="etched" title="Улучшения:"/>
<children>
<component id="fca4f" class="javax.swing.JCheckBox" binding="MissileCheckBox">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<label value="Боевые ракеты"/>
<text value="Боевые ракеты"/>
</properties>
</component>
<component id="a7f46" class="javax.swing.JCheckBox" binding="AntennaCheckBox">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<label value="Антенна"/>
<text value="Антенна"/>
</properties>
</component>
<component id="632da" class="javax.swing.JCheckBox" binding="HelipadCheckBox">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<label value="Вертолетная площадка"/>
<text value="Вертолетная площадка"/>
</properties>
</component>
</children>
</grid>
</children>
</grid>
<hspacer id="ca2ae">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
</children>
</grid>
<buttonGroups>
<group name="Entity">
<member id="beec9"/>
<member id="5837d"/>
</group>
<group name="Form">
<member id="5048f"/>
<member id="d03eb"/>
<member id="fd3ec"/>
</group>
<group name="Count">
<member id="f4e43"/>
<member id="38916"/>
<member id="538af"/>
</group>
</buttonGroups>
</form>

132
src/FormCreater.java Normal file
View File

@ -0,0 +1,132 @@
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
public class FormCreater extends JDialog{
private JLabel SetSpeedLabel;
private JPanel PropertiesPanel;
private JTextField SpeedTextField;
private JLabel SetWeightLabel;
private JTextField WeightTextField;
private JRadioButton BasicRadioButton;
private JRadioButton AdvancedRadioButton;
private JCheckBox MissileCheckBox;
private JCheckBox AntennaCheckBox;
private JCheckBox HelipadCheckBox;
private JPanel CargoPanel;
private JRadioButton TriangleFormRadioButton;
private JRadioButton RoundFormRadioButton;
private JRadioButton RectangleFormRadioButton;
private JPanel CountOfBlocksPanel;
private JRadioButton SixRadioButton;
private JRadioButton FourRadioButton;
private JRadioButton TwoRadioButton;
private JButton ShowWarshipButton;
private JButton ChooseButton;
private JPanel PictureBox;
BlockCount block=null;
IDrawingObjectBlock fblock=null;
private final CreaterGeneric<EntityWarship,IDrawingObjectBlock> createrGeneric=new CreaterGeneric<>(40,40);
private DrawingWarship _warship;
private DrawingWarship selectedWarship;
public FormCreater(){
setTitle("Военный корабль");
setContentPane(PictureBox);
setResizable(false);
setSize(1000,500);
ShowWindow();
setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
}
@Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) PictureBox.getGraphics();
if (_warship != null) {
_warship.DrawTransport(g2d);
}
}
private void ShowWindow(){
TwoRadioButton.addActionListener(e -> {
block=BlockCount.TwoBlocks;
});
FourRadioButton.addActionListener(e -> {
block=BlockCount.FourBlocks;
});
SixRadioButton.addActionListener(e -> {
block=BlockCount.SixBlocks;
});
RectangleFormRadioButton.addActionListener(e -> {
if(block==null){
return;
}
fblock=new DrawingBlock(block);
if(fblock==null)
return;
fblock.SetBlockCount(block.GetBlockCount());
createrGeneric.Add(fblock);
});
RoundFormRadioButton.addActionListener(e -> {
if(block==null){
return;
}
fblock=new DrawingRoundBlocks(block);
if(fblock==null)
return;
fblock.SetBlockCount(block.GetBlockCount());
createrGeneric.Add(fblock);
});
TriangleFormRadioButton.addActionListener(e -> {
if(block==null){
return;
}
fblock=new DrawingTriangleBlocks(block);
if(fblock==null)
return;
fblock.SetBlockCount(block.GetBlockCount());
createrGeneric.Add(fblock);
});
BasicRadioButton.addActionListener(e -> {
Color color=JColorChooser.showDialog(this,"Выберите цвет корпуса корабля",Color.WHITE);
if(Integer.parseInt(SpeedTextField.getText())==0 || Integer.parseInt(WeightTextField.getText())==0 || color==null){
return;
}
createrGeneric.Add(new EntityWarship(Integer.parseInt(SpeedTextField.getText()),Integer.parseInt(WeightTextField.getText()),color));
});
AdvancedRadioButton.addActionListener(e -> {
Color color1=JColorChooser.showDialog(this,"Выберите цвет корпуса корабля",Color.WHITE);
if(Integer.parseInt(SpeedTextField.getText())==0 || Integer.parseInt(WeightTextField.getText())==0 || color1==null){
return;
}
Color color2=JColorChooser.showDialog(this,"Выберите цвет модификаций корабля",Color.WHITE);
if(color2==null){
return;
}
createrGeneric.Add(new EntityAdvancedWarship(Integer.parseInt(SpeedTextField.getText()),Integer.parseInt(WeightTextField.getText()),
color1,color2,HelipadCheckBox.isSelected(),AntennaCheckBox.isSelected(),MissileCheckBox.isSelected()));
});
ShowWarshipButton.addActionListener(e -> {
Random rand=new Random();
_warship=createrGeneric.NewWarshipCreating();
_warship.SetPosition(rand.nextInt(100),rand.nextInt(100),getWidth(),getHeight());
repaint();
});
ChooseButton.addActionListener(e -> {
selectedWarship=_warship;
dispose();
});
}
public DrawingWarship getSelectedWarship() {
return selectedWarship;
}
}

View File

@ -1,175 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="FormMap">
<grid id="27dc6" binding="PictureBox" layout-manager="GridLayoutManager" row-count="4" column-count="5" 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="24" width="816" height="551"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<grid id="73281" layout-manager="GridLayoutManager" row-count="1" column-count="4" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="3" column="0" row-span="1" col-span="5" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="9090d" class="javax.swing.JLabel" binding="SpeedLabel">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Скорость: "/>
</properties>
</component>
<hspacer id="5a45f">
<constraints>
<grid row="0" column="3" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<component id="c4bc2" class="javax.swing.JLabel" binding="WeightLabel">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Вес: "/>
</properties>
</component>
<component id="74fba" class="javax.swing.JLabel" binding="BodyColorLabel">
<constraints>
<grid row="0" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Цвет: "/>
</properties>
</component>
</children>
</grid>
<hspacer id="6e33b">
<constraints>
<grid row="2" column="2" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<vspacer id="e09c8">
<constraints>
<grid row="1" column="2" 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="786ee" 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>
<grid row="2" column="1" 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="none"/>
<children>
<component id="92069" class="javax.swing.JButton" binding="ButtonCreate">
<constraints>
<grid row="0" 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="82dd2" class="javax.swing.JButton" binding="ButtonCreateModif">
<constraints>
<grid row="0" 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>
<text value="Модификация"/>
</properties>
</component>
</children>
</grid>
<grid id="b99f9" layout-manager="GridLayoutManager" row-count="2" 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="2" column="3" 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="none"/>
<children>
<component id="ba264" class="javax.swing.JButton" binding="ButtonDown">
<constraints>
<grid row="1" 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"/>
<text value=""/>
</properties>
</component>
<component id="8f6d" class="javax.swing.JButton" binding="ButtonRight">
<constraints>
<grid row="1" 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"/>
<text value=""/>
</properties>
</component>
<component id="9d65" class="javax.swing.JButton" binding="ButtonLeft">
<constraints>
<grid row="1" 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"/>
<text value=""/>
</properties>
</component>
<component id="7d46" class="javax.swing.JButton" binding="ButtonUp">
<constraints>
<grid row="0" 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"/>
<text value=""/>
</properties>
</component>
</children>
</grid>
<grid id="7affd" 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"/>
<constraints>
<grid row="2" column="4" 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="none"/>
<children>
<hspacer id="25">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
</children>
</grid>
<grid id="81336" 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"/>
<constraints>
<grid row="2" 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="none"/>
<children>
<hspacer id="89fd1">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
</children>
</grid>
<component id="fd77d" class="javax.swing.JComboBox" binding="ComboBoxSelectorMap">
<constraints>
<grid row="0" column="3" 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>
</children>
</grid>
</form>

View File

@ -1,76 +0,0 @@
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.util.Objects;
public class FormMap extends JFrame {
private int Width;
private int Height;
DrawingMap map = new DrawingMap(this);
private JButton ButtonDown;
private JButton ButtonRight;
private JButton ButtonLeft;
private JButton ButtonUp;
private JButton ButtonCreate;
private JButton ButtonCreateModif;
public JLabel SpeedLabel;
public JLabel WeightLabel;
public JLabel BodyColorLabel;
private JComboBox ComboBoxSelectorMap;
private JPanel PictureBox;
public FormMap(){
super("Военный корабль");
setContentPane(PictureBox);
setSize(1000,700);
Width = getWidth();
Height = getHeight();
ShowWindow();
map.setBounds(0,0,Width,Height);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
private void ShowWindow(){
ButtonCreate.addActionListener(e -> {
map.CreateButtonAction();
ReDraw();
});
ButtonCreateModif.addActionListener(e -> {
map.CreateModifButtonAction();
ReDraw();
});
ButtonUp.addActionListener(e -> {
map.DirectionButtonAction(Direction.Up);
ReDraw();
});
ButtonLeft.addActionListener(e -> {
map.DirectionButtonAction(Direction.Left);
ReDraw();
});
ButtonRight.addActionListener(e -> {
map.DirectionButtonAction(Direction.Right);
ReDraw();
});
ButtonDown.addActionListener(e -> {
map.DirectionButtonAction(Direction.Down);
ReDraw();
});
ComboBoxSelectorMap.addActionListener(e -> {
map.ComboBoxSelectorMapAction(Objects.requireNonNull(ComboBoxSelectorMap.getSelectedItem()).toString());
ReDraw();
});
}
private void ReDraw()
{
Graphics2D graphics = (Graphics2D) PictureBox.getGraphics();
graphics.clearRect(0, 0, PictureBox.getWidth(), PictureBox.getHeight());
PictureBox.paintComponents(graphics);
map.DrawMap(graphics);
}
}

View File

@ -0,0 +1,146 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="FormMapWithSetWarships">
<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"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<grid id="7fa54" binding="PictureBox" 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"/>
<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="none"/>
<children>
<hspacer id="53f34">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="1" hsize-policy="7" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</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">
<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"/>
</constraints>
<properties/>
<border type="bevel-lowered" title="Инструменты" title-justification="1" title-position="2">
<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"/>
</constraints>
</vspacer>
<component id="5312c" class="javax.swing.JButton" binding="ButtonAddWarship">
<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"/>
</constraints>
<properties>
<text value="Добавить корабль"/>
</properties>
</component>
<component id="d2569" class="javax.swing.JButton" binding="ButtonRemoveWarship">
<constraints>
<grid row="4" 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>
<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"/>
</constraints>
<properties>
<text value="Посмотреть хранилище"/>
</properties>
</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"/>
</constraints>
<properties>
<text value="Посмотреть карту"/>
</properties>
</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"/>
</constraints>
<properties>
<icon value="arrowDown.jpg"/>
<selectedIcon value="arrowDown.jpg"/>
<text value=""/>
</properties>
</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"/>
</constraints>
<properties>
<icon value="arrowUp.jpg"/>
<selectedIcon value="arrowUp.jpg"/>
<text value=""/>
</properties>
</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"/>
</constraints>
<properties>
<icon value="arrowLeft.jpg"/>
<selectedIcon value="arrowLeft.jpg"/>
<text value=""/>
</properties>
</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"/>
</constraints>
<properties>
<icon value="arrowRight.jpg"/>
<selectedIcon value="arrowRight.jpg"/>
<text value=""/>
</properties>
</component>
<component id="b49b1" class="javax.swing.JTextField" binding="TextBoxPosition">
<constraints>
<grid row="3" column="0" row-span="1" col-span="3" 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>
<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"/>
</constraints>
</vspacer>
<vspacer id="42f1e">
<constraints>
<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>
</children>
</grid>
</children>
</grid>
</form>

View File

@ -0,0 +1,165 @@
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
public class FormMapWithSetWarships extends JFrame{
private JComboBox СomboBoxSelectorMap;
private JButton ButtonAddWarship;
private JTextField TextBoxPosition;
private JButton ButtonRemoveWarship;
private JButton ButtonShowStorage;
private JButton ButtonShowOnMap;
private JButton ButtonDown;
private JButton ButtonUp;
private JButton ButtonLeft;
private JButton ButtonRight;
private JPanel PictureBox;
private JPanel MainPanel;
private JPanel GroupBoxTools;
private MapWithSetWarshipsGeneric<DrawingObjectWarship,AbstractMap> _mapWarshipsCollectionGeneric;
private Image bufferedImage;
public FormMapWithSetWarships(){
setTitle("Военный корабль");
setContentPane(MainPanel);
setResizable(false);
setSize(1000,685);
ShowWindow();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
@Override
public void paint(Graphics g) {
super.paint(g);
if (bufferedImage != null) {
PictureBox.paintComponents(bufferedImage.getGraphics());
PictureBox.getGraphics().drawImage(bufferedImage, 0, 0, null);
}
}
private void ShowWindow(){
ButtonShowOnMap.addActionListener(e -> {
if (_mapWarshipsCollectionGeneric == null)
{
return;
}
bufferedImage = _mapWarshipsCollectionGeneric.ShowOnMap();
repaint();
});
ButtonShowStorage.addActionListener(e -> {
if (_mapWarshipsCollectionGeneric == null)
{
return;
}
bufferedImage = _mapWarshipsCollectionGeneric.ShowSet();
repaint();
});
ButtonAddWarship.addActionListener(e -> {
if (_mapWarshipsCollectionGeneric == null)
{
return;
}
FormCreater dialog=new FormCreater();
dialog.setSize(1200,700);
dialog.setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
if (dialog.getSelectedWarship()!=null) {
DrawingObjectWarship warship = new DrawingObjectWarship(dialog.getSelectedWarship());
if (_mapWarshipsCollectionGeneric.plus(warship) >= 0) {
JOptionPane.showMessageDialog(this, "Объект добавлен", "Успех", JOptionPane.INFORMATION_MESSAGE);
bufferedImage = _mapWarshipsCollectionGeneric.ShowSet();
repaint();
} else {
JOptionPane.showMessageDialog(this, "Не удалось добавить объект", "Ошибка",JOptionPane.INFORMATION_MESSAGE);
}
}
});
ButtonRemoveWarship.addActionListener(e -> {
String txt=TextBoxPosition.getText();
if (txt==null||_mapWarshipsCollectionGeneric==null||txt.isEmpty())
{
return;
}
int result = JOptionPane.showConfirmDialog(
this,
"Удалить объект?",
"Удаление",
JOptionPane.YES_NO_CANCEL_OPTION);
if (result == JOptionPane.NO_OPTION)
{
return;
}
int pos = Integer.parseInt(txt);
if (_mapWarshipsCollectionGeneric.minus(pos) !=null)
{
JOptionPane.showMessageDialog(this,"Объект удален","Успех",JOptionPane.INFORMATION_MESSAGE);
bufferedImage = _mapWarshipsCollectionGeneric.ShowSet();
repaint();
}
else
{
JOptionPane.showMessageDialog(this,"Не удалось удалить объект","Ошибка",JOptionPane.INFORMATION_MESSAGE);
}
});
С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;
}
});
ButtonUp.addActionListener(e -> {
if (_mapWarshipsCollectionGeneric != null) {
bufferedImage = _mapWarshipsCollectionGeneric.MoveObject(Direction.Up);
repaint();
}
});
ButtonDown.addActionListener(e -> {
if (_mapWarshipsCollectionGeneric != null) {
bufferedImage = _mapWarshipsCollectionGeneric.MoveObject(Direction.Down);
repaint();
}
});
ButtonRight.addActionListener(e -> {
if (_mapWarshipsCollectionGeneric != null) {
bufferedImage = _mapWarshipsCollectionGeneric.MoveObject(Direction.Right);
repaint();
}
});
ButtonLeft.addActionListener(e -> {
if (_mapWarshipsCollectionGeneric != null) {
bufferedImage = _mapWarshipsCollectionGeneric.MoveObject(Direction.Left);
repaint();
}
});
}
}

View File

@ -1,9 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="FormWarship">
<grid id="27dc6" binding="PictureBox" layout-manager="GridLayoutManager" row-count="3" column-count="5" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="27dc6" binding="PictureBox" layout-manager="GridLayoutManager" row-count="3" column-count="6" 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="637" height="507"/>
<xy x="20" y="20" width="660" height="507"/>
</constraints>
<properties/>
<border type="none"/>
@ -11,7 +11,7 @@
<grid id="11e3d" layout-manager="GridLayoutManager" row-count="1" column-count="4" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="2" column="0" row-span="1" col-span="5" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
<grid row="2" column="0" row-span="1" col-span="6" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
@ -50,7 +50,7 @@
<grid id="ba3a6" layout-manager="GridLayoutManager" row-count="2" 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="1" column="3" 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="4" 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="none"/>
@ -132,7 +132,7 @@
<grid id="e85d9" 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"/>
<constraints>
<grid row="1" column="4" 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="5" 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="none"/>
@ -159,6 +159,24 @@
</hspacer>
</children>
</grid>
<grid id="c2e3" 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"/>
<constraints>
<grid row="1" column="3" 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="none"/>
<children>
<component id="f6508" class="javax.swing.JButton" binding="ButtonSelect">
<constraints>
<grid row="0" 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>
</children>
</grid>
</children>
</grid>
</form>

View File

@ -3,10 +3,11 @@ import java.awt.*;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
public class FormWarship extends JFrame{
public class FormWarship extends JDialog{
private int Width;
private int Height;
DrawingField field = new DrawingField(this);
DrawingWarship SelectedWarship;
private JButton ButtonDown;
private JButton ButtonRight;
private JButton ButtonLeft;
@ -17,16 +18,17 @@ public class FormWarship extends JFrame{
public JLabel WeightLabel;
public JLabel BodyColorLabel;
private JPanel PictureBox;
private JButton ButtonSelect;
public FormWarship(){
super("Военный корабль");
public FormWarship(JFrame frame){
super(frame,"Военный корабль");
setContentPane(PictureBox);
setSize(1000,700);
Width = getWidth();
Height = getHeight();
ShowWindow();
field.setBounds(0,0,Width,Height);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
setVisible(true);
}
@ -56,6 +58,10 @@ public class FormWarship extends JFrame{
field.DirectionButtonAction(Direction.Down);
ReDraw();
});
ButtonSelect.addActionListener(e -> {
SelectedWarship=field.GetDrawingWarship();
JOptionPane.showMessageDialog(PictureBox, "Корабль добавлен.");
});
addComponentListener(new ComponentAdapter() {
@Override
@ -70,8 +76,7 @@ public class FormWarship extends JFrame{
}
});
}
private void ReDraw()
{
private void ReDraw() {
Graphics2D graphics = (Graphics2D) PictureBox.getGraphics();
graphics.clearRect(0, 0, PictureBox.getWidth(), PictureBox.getHeight());
PictureBox.paintComponents(graphics);

View File

@ -1,5 +1,5 @@
public class Main {
public static void main(String[] args) {
new FormMap();
new FormMapWithSetWarships();
}
}

View File

@ -0,0 +1,126 @@
import java.awt.*;
import java.awt.image.BufferedImage;
public class MapWithSetWarshipsGeneric<T extends IDrawingObject,U extends AbstractMap> {
private final int _pictureWidth;
private final int _pictureHeight;
private final int _placeSizeWidth = 120;
private final int _placeSizeHeight = 50;
private final SetWarshipsGeneric<T> _setWarship;
private final U _map;
public MapWithSetWarshipsGeneric(int picWidth,int picHeight, U map)
{
int width = picWidth / _placeSizeWidth;
int height = picHeight/_placeSizeHeight;
_setWarship = new SetWarshipsGeneric<T>(width * height);
_pictureWidth = picWidth;
_pictureHeight = picHeight;
_map = map;
}
public int plus(T warship)
{
return _setWarship.Insert(warship);
}
public T minus(int position)
{
return _setWarship.Remove(position);
}
public Image ShowSet()
{
BufferedImage bmp = new BufferedImage(_pictureWidth, _pictureWidth,BufferedImage.TYPE_INT_ARGB);
Graphics gr = bmp.getGraphics();
DrawBackground(gr);
DrawWarship(gr);
return bmp;
}
public Image ShowOnMap()
{
Shaking();
for (int i = 0; i < _setWarship.Count(); i++)
{
T warship = _setWarship.Get(i);
if (warship != null)
{
return _map.CreateMap(_pictureWidth, _pictureHeight, warship);
}
}
return new BufferedImage(_pictureWidth, _pictureHeight,1);
}
public Image MoveObject(Direction direction)
{
if (_map != null)
{
return _map.MoveObject(direction);
}
return new BufferedImage(_pictureWidth, _pictureHeight,1);
}
public void Shaking()
{
int j = _setWarship.Count() - 1;
for (int i = 0; i < _setWarship.Count(); i++)
{
if (_setWarship.Get(i) == null)
{
for (; j > i; j--)
{
T warship = _setWarship.Get(j);
if (warship != null)
{
_setWarship.Insert(warship, i);
_setWarship.Remove(j);
break;
}
}
if (j <= i)
{
return;
}
}
}
}
private void DrawBackground(Graphics gr)
{
Graphics2D g=(Graphics2D)gr;
Color brush=Color.BLACK;
Stroke penWide = new BasicStroke(5);
Stroke penThin = new BasicStroke(1);
for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++)
{
for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j)
{
g.setColor(brush);
g.setStroke(penWide);
g.drawLine(i * _placeSizeWidth + 20, j * _placeSizeHeight+2, i * _placeSizeWidth + (int)(_placeSizeWidth*0.8), j * _placeSizeHeight+2);
g.drawLine(i * _placeSizeWidth + 20, j * _placeSizeHeight + _placeSizeHeight/2+2, i * _placeSizeWidth + (int)(_placeSizeWidth * 0.8), j * _placeSizeHeight + _placeSizeHeight/2+2);
g.drawLine(i * _placeSizeWidth + (int)(_placeSizeWidth * 0.8), j * _placeSizeHeight + 2, i * _placeSizeWidth + _placeSizeWidth, j * _placeSizeHeight + _placeSizeHeight/2);
g.drawLine(i * _placeSizeWidth + _placeSizeWidth, j * _placeSizeHeight + _placeSizeHeight / 2, i * _placeSizeWidth+ (int)(_placeSizeWidth * 0.8), j * _placeSizeHeight + _placeSizeHeight);
g.setStroke(penThin);
}
}
}
private void DrawWarship(Graphics gr)
{
int width = _pictureWidth / _placeSizeWidth;
int height = _pictureHeight / _placeSizeHeight;
for (int i = 0; i < _setWarship.Count(); i++)
{
if (_setWarship.Get(i) != null)
{
_setWarship.Get(i).SetObject(i % width * _placeSizeWidth, (height - 1 - i / width) * _placeSizeHeight, _pictureWidth, _pictureHeight);
_setWarship.Get(i).DrawingObject(gr);
}
}
}
}

View File

@ -0,0 +1,68 @@
public class SetWarshipsGeneric<T extends Object> {
private final Object[] _places;
public int Count() {
return _places.length;
}
public SetWarshipsGeneric(int count)
{
_places = new Object[count];
}
public int Insert(T warship)
{
return Insert(warship,0);
}
public int Insert(T warship, int position)
{
int EmptyEl=-1;
if (position>=Count() || 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;
}
public T Remove(int position)
{
if (position >= Count() || position < 0 || _places[position]==null)
return null;
T deleted =(T)_places[position];
_places[position] = null;
return deleted;
}
public T Get(int position)
{
if (position >= Count() || position < 0)
return null;
return (T)_places[position];
}
}