Bondarenko M.S. Lab Work 3 #3

Closed
maxnes3 wants to merge 1 commits from LabWork_03 into LabWork_02
13 changed files with 853 additions and 12 deletions

View File

@ -84,7 +84,7 @@ public abstract class AbstractMap {
}
return true;
}
private BufferedImage DrawMapWithObject()
public BufferedImage DrawMapWithObject()
{
BufferedImage bmp = new BufferedImage(_width, _height, BufferedImage.TYPE_INT_RGB);
if (_drawningObject == null || _map == null)

View File

@ -34,6 +34,12 @@ public class DrawingShip extends JPanel {
idd.SetDeckCount(random.nextInt(1, 4));
}
public DrawingShip(EntityWarmlyShip ship, IDrawningDeck deck)
{
warmlyShip = ship;
idd = deck;
}
protected DrawingShip(int speed, float weight, Color bodyColor, int warmlyWidth, int warmlyHeight)
{
this(speed, weight, bodyColor);

View File

@ -7,6 +7,11 @@ public class DrawningMotorShip extends DrawingShip {
warmlyShip = new EntityMotorShip(speed, weight, bodyColor, dopColor, tubes , cistern);
}
public DrawningMotorShip(EntityWarmlyShip ship, IDrawningDeck deck){
super(ship, deck);
warmlyShip = ship;
}
@Override
public void DrawTransport(Graphics g)
{

156
FormMapWithSetShip.form Normal file
View File

@ -0,0 +1,156 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="FormMapWithSetShip">
<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="785" height="400"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<grid id="afa3d" binding="GraphicsOutput" layout-manager="FlowLayout" hgap="5" vgap="5" flow-align="1">
<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/>
</grid>
<grid id="e9a63" binding="groupBox" layout-manager="GridLayoutManager" row-count="8" 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="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="0" anchor="0" fill="3" indent="0" use-parent-layout="false">
<minimum-size width="150" height="-1"/>
</grid>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="f23ee" class="javax.swing.JButton" binding="buttonAddShip">
<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>
<text value="Добавить корабль"/>
</properties>
</component>
<component id="2e61e" class="javax.swing.JComboBox" binding="comboBoxSelectorMap">
<constraints>
<grid row="0" 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/>
</properties>
</component>
<component id="5c428" class="javax.swing.JButton" binding="buttonRemoveShip">
<constraints>
<grid row="3" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Удалить корабль"/>
</properties>
</component>
<component id="e24b9" class="javax.swing.JButton" binding="buttonShowStorage">
<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="ac087" class="javax.swing.JButton" binding="buttonShowOnMap">
<constraints>
<grid row="5" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Посмотреть карту"/>
</properties>
</component>
<grid id="2821b" layout-manager="GridLayoutManager" row-count="2" 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>
<grid row="7" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="0" anchor="0" fill="3" indent="0" use-parent-layout="false">
<minimum-size width="150" height="-1"/>
</grid>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="6d803" class="javax.swing.JButton" binding="buttonLeft">
<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">
<minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<text value=""/>
</properties>
</component>
<component id="c0db2" class="javax.swing.JButton" binding="buttonUp">
<constraints>
<grid row="0" column="2" 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="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<text value=""/>
</properties>
</component>
<component id="ca0da" class="javax.swing.JButton" binding="buttonDown">
<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">
<minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<text value=""/>
</properties>
</component>
<component id="c3e45" class="javax.swing.JButton" binding="buttonRight">
<constraints>
<grid row="1" column="3" 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="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<text value=""/>
</properties>
</component>
<hspacer id="1f200">
<constraints>
<grid row="1" column="4" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<hspacer id="f8b3b">
<constraints>
<grid row="1" 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>
<vspacer id="b833e">
<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>
<component id="181ac" class="javax.swing.JTextField" binding="maskedTextBoxPosition">
<constraints>
<grid row="2" 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>
</children>
</grid>
</children>
</grid>
</form>

238
FormMapWithSetShip.java Normal file
View File

@ -0,0 +1,238 @@
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class FormMapWithSetShip extends JFrame {
private MapWithSetShipGeneric<DrawningObjectShip, AbstractMap> _mapShipCollectionGeneric;
public boolean ShipOnMap = false;
public JPanel Mainpanel;
private JButton buttonAddShip;
private JComboBox comboBoxSelectorMap;
private JTextField maskedTextBoxPosition;
private JButton buttonRemoveShip;
private JButton buttonShowStorage;
private JButton buttonShowOnMap;
private JButton buttonUp;
private JButton buttonLeft;
private JButton buttonRight;
private JButton buttonDown;
private JPanel GraphicsOutput;
private JPanel groupBox;
private JFrame getFrame() {
JFrame frame = new JFrame();
frame.setVisible(false);
frame.setBounds(300, 100, 800, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
return frame;
}
JFrame jFrame = getFrame();
private void ButtonMove_Click(String name)
{
if (_mapShipCollectionGeneric == null || !ShipOnMap) return;
Direction direction = Direction.None;
switch (name)
{
case "buttonLeft":
direction = Direction.Left;
break;
case "buttonUp":
direction = Direction.Up;
break;
case "buttonRight":
direction = Direction.Right;
break;
case "buttonDown":
direction = Direction.Down;
break;
}
GraphicsOutput.removeAll();
JLabel imageOfShip = new JLabel();
imageOfShip.setPreferredSize(GraphicsOutput.getSize());
imageOfShip.setMinimumSize(new Dimension(1, 1));
imageOfShip.setIcon(new ImageIcon(_mapShipCollectionGeneric.MoveObject(direction)));
GraphicsOutput.add(imageOfShip,BorderLayout.CENTER);
GraphicsOutput.revalidate();
GraphicsOutput.repaint();
}
public FormMapWithSetShip() {
comboBoxSelectorMap.addItem("Простая карта");
comboBoxSelectorMap.addItem("Вторая карта");
comboBoxSelectorMap.addItem("Последняя карта");
try {
Image img = ImageIO.read(FormShip.class.getResource("/Images/totop.png"));
buttonUp.setIcon(new ImageIcon(img));
img = ImageIO.read(FormShip.class.getResource("/Images/toleft.png"));
buttonLeft.setIcon(new ImageIcon(img));
img = ImageIO.read(FormShip.class.getResource("/Images/todown.png"));
buttonDown.setIcon(new ImageIcon(img));
img = ImageIO.read(FormShip.class.getResource("/Images/toright.png"));
buttonRight.setIcon(new ImageIcon(img));
} catch (Exception ex) {
System.out.println(ex);
}
comboBoxSelectorMap.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
AbstractMap map = null;
switch (comboBoxSelectorMap.getSelectedItem().toString())
{
case "Простая карта":
map = new SimpleMap();
break;
case "Вторая карта":
map = new SecondMap();
break;
case "Последняя карта":
map = new LastMap();
break;
}
if (map != null)
{
_mapShipCollectionGeneric = new MapWithSetShipGeneric<DrawningObjectShip, AbstractMap>(GraphicsOutput.getWidth(), GraphicsOutput.getHeight(), map);
}
else
{
_mapShipCollectionGeneric = null;
}
}
});
buttonAddShip.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_mapShipCollectionGeneric == null)
{
return;
}
FormShip dialog = new FormShip();
dialog.setSize(800, 600);
dialog.setLocation(500, 200);
dialog.setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
if (dialog.GetSelectedShip() == null) return;
DrawningObjectShip ship = new DrawningObjectShip(dialog.GetSelectedShip());
if (_mapShipCollectionGeneric.Add(ship) > -1)
{
JOptionPane.showMessageDialog(jFrame, "Объект добавлен");
GraphicsOutput.removeAll();
JLabel imageOfShip = new JLabel();
imageOfShip.setPreferredSize(GraphicsOutput.getSize());
imageOfShip.setMinimumSize(new Dimension(1, 1));
imageOfShip.setIcon(new ImageIcon(_mapShipCollectionGeneric.ShowSet()));
GraphicsOutput.add(imageOfShip,BorderLayout.CENTER);
GraphicsOutput.revalidate();
GraphicsOutput.repaint();
}
else
{
JOptionPane.showMessageDialog(jFrame, "Не удалось добавить объект");
}
}
});
buttonRemoveShip.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(_mapShipCollectionGeneric == null) return;
String text = maskedTextBoxPosition.getText();
if(text.isEmpty()) return;
if(JOptionPane.showConfirmDialog(jFrame, "Вы действительно хотите удалить объект?", "Удаление", JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION) return;
try {
Integer.parseInt(text);
}
catch (Exception ex){
return;
}
int pos = Integer.parseInt(text);
if (_mapShipCollectionGeneric.Delete(pos) != null)
{
JOptionPane.showMessageDialog(jFrame, "Объект удален");
GraphicsOutput.removeAll();
JLabel imageOfShip = new JLabel();
imageOfShip.setPreferredSize(GraphicsOutput.getSize());
imageOfShip.setMinimumSize(new Dimension(1, 1));
imageOfShip.setIcon(new ImageIcon(_mapShipCollectionGeneric.ShowSet()));
GraphicsOutput.add(imageOfShip,BorderLayout.CENTER);
GraphicsOutput.revalidate();
GraphicsOutput.repaint();
}
else
{
JOptionPane.showMessageDialog(jFrame, "Не удалось удалить объект");
}
}
});
buttonShowStorage.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_mapShipCollectionGeneric == null) return;
GraphicsOutput.removeAll();
JLabel imageOfShip = new JLabel();
imageOfShip.setPreferredSize(GraphicsOutput.getSize());
imageOfShip.setMinimumSize(new Dimension(1, 1));
imageOfShip.setIcon(new ImageIcon(_mapShipCollectionGeneric.ShowSet()));
GraphicsOutput.add(imageOfShip,BorderLayout.CENTER);
GraphicsOutput.revalidate();
GraphicsOutput.repaint();
ShipOnMap = false;
}
});
buttonShowOnMap.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_mapShipCollectionGeneric == null) return;
GraphicsOutput.removeAll();
JLabel imageOfShip = new JLabel();
imageOfShip.setPreferredSize(GraphicsOutput.getSize());
imageOfShip.setMinimumSize(new Dimension(1, 1));
imageOfShip.setIcon(new ImageIcon(_mapShipCollectionGeneric.ShowOnMap()));
GraphicsOutput.add(imageOfShip,BorderLayout.CENTER);
GraphicsOutput.revalidate();
GraphicsOutput.repaint();
ShipOnMap = true;
}
});
buttonUp.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ButtonMove_Click("buttonUp");
}
});
buttonLeft.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ButtonMove_Click("buttonLeft");
}
});
buttonDown.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ButtonMove_Click("buttonDown");
}
});
buttonRight.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ButtonMove_Click("buttonRight");
}
});
}
}

View File

@ -102,16 +102,19 @@
<text value="Модификация"/>
</properties>
</component>
<hspacer id="a48ad">
<constraints>
<grid row="1" column="2" row-span="1" col-span="2" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<hspacer id="b7fa3">
<constraints>
<grid row="2" column="0" row-span="1" col-span="3" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<component id="faea9" class="javax.swing.JButton" binding="buttonSelectShip">
<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>
<text value="Выбрать"/>
</properties>
</component>
</children>
</grid>
</form>

View File

@ -5,10 +5,12 @@ import java.awt.event.*;
import java.awt.image.*;
import java.util.*;
public class FormShip extends JFrame{
public class FormShip extends JDialog{
private JToolBar statusStrip;
public JPanel Mainpanel;
private DrawingShip ship;
private DrawingShip selectedShip;
public DrawingShip GetSelectedShip(){return selectedShip;} //Выбранный объект
private JButton buttonRight;
private JButton buttonCreate;
private JButton buttonLeft;
@ -16,6 +18,7 @@ public class FormShip extends JFrame{
private JButton buttonDown;
private JPanel GraphicsOutput;
private JButton buttonCreateModif;
private JButton buttonSelectShip;
private JLabel JLabelSpeed = new JLabel();
private JLabel JLabelWeight = new JLabel();
private JLabel JLabelColor = new JLabel();
@ -76,6 +79,8 @@ public class FormShip extends JFrame{
LabelBox.add(JLabelColor);
statusStrip.add(LabelBox);
add(Mainpanel);
try {
Image img = ImageIO.read(FormShip.class.getResource("/Images/totop.png"));
buttonUp.setIcon(new ImageIcon(img));
@ -93,7 +98,7 @@ public class FormShip extends JFrame{
@Override
public void actionPerformed(ActionEvent e) {
Random random = new Random();
ship = new DrawingShip(random.nextInt(100, 300), random.nextInt(1000, 2000), new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)));
ship = new DrawingShip(random.nextInt(100, 300), random.nextInt(1000, 2000), JColorChooser.showDialog(null, "Цвет", null));
SetData();
Draw();
}
@ -137,12 +142,20 @@ public class FormShip extends JFrame{
public void actionPerformed(ActionEvent e) {
Random random = new Random();
ship = new DrawningMotorShip(random.nextInt(100, 300), random.nextInt(1000, 3000),
new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)),
new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)),
JColorChooser.showDialog(null, "Цвет", null),
JColorChooser.showDialog(null, "Цвет", null),
random.nextBoolean(), random.nextBoolean());
SetData();
Draw();
}
});
buttonSelectShip.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
selectedShip = ship;
setVisible(false);
dispose();
}
});
}
}

70
FormShipMixer.form Normal file
View File

@ -0,0 +1,70 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="FormShipMixer">
<grid id="27dc6" binding="Mainpanel" layout-manager="GridLayoutManager" row-count="4" 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>
<xy x="20" y="20" width="500" height="400"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<grid id="7700e" binding="pictureBox" layout-manager="BorderLayout" hgap="0" vgap="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/>
<border type="none"/>
<children/>
</grid>
<component id="b960b" class="javax.swing.JButton" binding="ButtonCreate">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Создать"/>
</properties>
</component>
<component id="a44b0" class="javax.swing.JButton" binding="ButtonCreateModif">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Модификация"/>
</properties>
</component>
<component id="31e01" class="javax.swing.JLabel" binding="LabelInfo">
<constraints>
<grid row="2" column="1" row-span="1" col-span="2" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value=""/>
</properties>
</component>
<toolbar id="7a0ba" binding="StatusStrip">
<constraints>
<grid row="3" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="-1" height="20"/>
</grid>
</constraints>
<properties>
<enabled value="false"/>
</properties>
<border type="none"/>
<children/>
</toolbar>
<component id="e8cb9" class="javax.swing.JButton" binding="ButtonMix">
<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>
<text value="Сгенерировать"/>
</properties>
</component>
<hspacer id="e60aa">
<constraints>
<grid row="1" column="1" 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>
</form>

118
FormShipMixer.java Normal file
View File

@ -0,0 +1,118 @@
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.util.Random;
public class FormShipMixer extends JFrame{
public JPanel Mainpanel;
private JPanel pictureBox;
private JButton ButtonCreate;
private JButton ButtonCreateModif;
private JToolBar StatusStrip;
private JLabel LabelInfo;
private JButton ButtonMix;
private JLabel JLabelSpeed = new JLabel();
private JLabel JLabelWeight = new JLabel();
private JLabel JLabelColor = new JLabel();
private ShipsMix<EntityWarmlyShip,IDrawningDeck> _drawningEntities;
private IDrawningDeck SetData()
{
Random random = new Random();
switch (random.nextInt(3))
{
case 0:
return new DrawDeck();
case 1:
return new DrawGuns();
}
return new DrawOvalDeck();
}
private void Draw(DrawingShip _plane) {
pictureBox.removeAll();
Random random = new Random();
BufferedImage bmp = new BufferedImage(pictureBox.getWidth(), pictureBox.getHeight(),BufferedImage.TYPE_INT_RGB);
Graphics gr = bmp.getGraphics();
gr.setColor(new Color(238, 238, 238));
gr.fillRect(0, 0, pictureBox.getWidth(), pictureBox.getHeight());
if (_plane != null) {
_plane.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100),
pictureBox.getWidth(), pictureBox.getHeight());
_plane.DrawTransport(gr);
JLabelSpeed.setText("орость: " + _plane.GetWarmlyShip().GetSpeed() + " ");
JLabelWeight.setText("Вес: " + _plane.GetWarmlyShip().GetWeight() + " ");
JLabelColor.setText(("Цвет: " + _plane.GetWarmlyShip().GetBodyColor() + " "));
JLabel imageOfShip = new JLabel();
imageOfShip.setPreferredSize(pictureBox.getSize());
imageOfShip.setMinimumSize(new Dimension(1, 1));
imageOfShip.setIcon(new ImageIcon(bmp));
pictureBox.add(imageOfShip,BorderLayout.CENTER);
}
validate();
}
public FormShipMixer()
{
Box LabelBox = Box.createHorizontalBox();
LabelBox.setMinimumSize(new Dimension(1, 20));
LabelBox.add(JLabelSpeed);
LabelBox.add(JLabelWeight);
LabelBox.add(JLabelColor);
StatusStrip.add(LabelBox);
_drawningEntities = new ShipsMix<>(10,10);
ButtonCreate.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e){
Random random = new Random();
Color colorFirst = JColorChooser.showDialog(null, "Цвет", null);
EntityWarmlyShip ship = new EntityWarmlyShip(random.nextInt(100,300), random.nextInt(1000,2000),colorFirst);
IDrawningDeck deck = SetData();
int DecksCount=random.nextInt(1,4);
deck.SetDeckCount(DecksCount);
if(_drawningEntities.Insert(ship) != -1 && _drawningEntities.Insert(deck) != -1)
{
JOptionPane.showMessageDialog(null,"Объект добавлен");
Draw(_drawningEntities.MakeShipMix());
LabelInfo.setText(_drawningEntities.indexX + " " + _drawningEntities.indexY);
}
else
{
JOptionPane.showMessageDialog(null, "Не удалось добавить объект");
}
}
});
ButtonCreateModif.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Random random = new Random();
Color colorFirst = JColorChooser.showDialog(null, "Цвет", null);
Color colorSecond = JColorChooser.showDialog(null, "Цвет", null);
EntityMotorShip _ship = new EntityMotorShip(random.nextInt(100, 300), random.nextInt(1000, 2000), colorFirst, colorSecond, random.nextBoolean(), random.nextBoolean());
IDrawningDeck deck = SetData();
int DecksCount=random.nextInt(1,4);
deck.SetDeckCount(DecksCount);
if(_drawningEntities.Insert(_ship) != -1 && _drawningEntities.Insert(deck) != -1)
{
JOptionPane.showMessageDialog(null,"Объект добавлен");
Draw(_drawningEntities.MakeShipMix());
LabelInfo.setText(_drawningEntities.indexX + " " + _drawningEntities.indexY);
}
else
{
JOptionPane.showMessageDialog(null, "Не удалось добавить объект");
}
}
});
ButtonMix.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(_drawningEntities.shipsCount == 0 || _drawningEntities.deckCount == 0) return;
Draw(_drawningEntities.MakeShipMix());
LabelInfo.setText(_drawningEntities.indexX + " " + _drawningEntities.indexY);
}
});
}
}

View File

@ -2,8 +2,8 @@ import javax.swing.*;
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame("Hard №2");
frame.setContentPane(new FormMap().Mainpanel);
JFrame frame = new JFrame("Hard №3");
frame.setContentPane(new FormShipMixer().Mainpanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocation(500, 200);
frame.pack();

122
MapWithSetShipGeneric.java Normal file
View File

@ -0,0 +1,122 @@
import java.awt.*;
import java.awt.image.BufferedImage;
public class MapWithSetShipGeneric<T extends IDrawningObject, U extends AbstractMap> {
private int _pictureWidth;
private int _pictureHeight;
private int _placeSizeWidth = 210;
private int _placeSizeHeight = 100;
private SetShipGeneric<T> _setShips;
private U _map;
public MapWithSetShipGeneric(int picWidth, int picHeight, U map)
{
int width = picWidth / _placeSizeWidth;
int height = picHeight / _placeSizeHeight;
_setShips = new SetShipGeneric<T>(width * height);
_pictureWidth = picWidth;
_pictureHeight = picHeight;
_map = map;
}
public int Add(T ship)
{
return _setShips.Insert(ship);
}
public T Delete(int position)
{
return _setShips.Remove(position);
}
public BufferedImage ShowSet()
{
BufferedImage bmp = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_RGB);
Graphics g = bmp.getGraphics();
DrawBackground((Graphics2D) g);
DrawShips(g);
return bmp;
}
public BufferedImage ShowOnMap()
{
BufferedImage bmp = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_RGB);
Shaking();
for (int i = 0; i < _setShips.getCount(); i++)
{
var ship = _setShips.Get(i);
if (ship != null)
{
return _map.CreateMap(_pictureWidth, _pictureHeight, ship);
}
}
return bmp;
}
public BufferedImage MoveObject(Direction direction)
{
BufferedImage bmp = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_RGB);
if (_map != null)
{
return _map.MoveObject(direction);
}
return bmp;
}
private void Shaking()
{
int j = _setShips.getCount() - 1;
for (int i = 0; i < _setShips.getCount(); ++i)
{
if (_setShips.Get(i) == null)
{
for (; j > i; --j)
{
var car = _setShips.Get(j);
if (car != null)
{
_setShips.Insert(car, i);
_setShips.Remove(j);
break;
}
}
if (j <= i)
{
return;
}
}
}
}
private void DrawBackground(Graphics2D g2d)
{
g2d.setColor(Color.WHITE);
g2d.fillRect(0, 0, _pictureWidth, _pictureHeight);
g2d.setColor(new Color(150, 75, 0));
for (int i = 0; i < _pictureWidth / _placeSizeWidth; ++i)
{
for (int j = 1; j < _pictureHeight / _placeSizeHeight + 1; ++j) //линия рамзетки места
{
g2d.drawLine(i * _placeSizeWidth, j * _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight);
for (int k = 0; k < _placeSizeWidth / (30 * 2); ++k)
g2d.drawLine(i * _placeSizeWidth + (k + 1) * 30, j * _placeSizeHeight, i * _placeSizeWidth + (k + 1) * 30, j * _placeSizeHeight + 30);
}
g2d.drawLine(i * _placeSizeWidth, 0, i * _placeSizeWidth, (_pictureHeight / _placeSizeHeight) * _placeSizeHeight);
}
}
private void DrawShips(Graphics g)
{
for (int i = 0; i < _setShips.getCount(); ++i)
{
if (_setShips.Get(i) != null )
{
int temp = 0;
if (_setShips.Get(i).GetCurrentPosition()[3] - _setShips.Get(i).GetCurrentPosition()[2] < 75) temp = (int)(_setShips.Get(i).GetCurrentPosition()[3] - _setShips.Get(i).GetCurrentPosition()[2]);
_setShips.Get(i).SetObject((_pictureWidth / _placeSizeWidth - (i % (_pictureWidth / _placeSizeWidth)) - 1) * _placeSizeWidth, (i / (_pictureWidth / _placeSizeWidth)) * _placeSizeHeight + temp, _pictureWidth, _pictureHeight);
_setShips.Get(i).DrawningObject(g);
}
}
}
}

54
SetShipGeneric.java Normal file
View File

@ -0,0 +1,54 @@
public class SetShipGeneric<T> {
private T[] _places;
public int getCount() {
return _places.length;
}
public SetShipGeneric(int count)
{
_places = (T[])(new Object[count]);
}
private boolean CanInsert(int position)
{
for (int i = position; i < _places.length; ++i)
if (_places[i] == null) return true;
return false;
}
public int Insert(T ship)
{
return Insert(ship, 0);
}
public int Insert(T ship, int position)
{
if (position < 0 || position > _places.length) return -1;
if (_places[position] != null && CanInsert(position))
{
for (int i = _places.length - 1; i > position; --i)
{
if (_places[i] == null)
{
_places[i] = _places[i - 1];
_places[i - 1] = null;
}
}
}
_places[position] = ship;
return position;
}
public T Remove(int position)
{
T DelElement = _places[position];
_places[position] = null;
return DelElement;
}
public T Get(int position)
{
if (position < 0 || position > _places.length) return null;
return _places[position];
}
}

56
ShipsMix.java Normal file
View File

@ -0,0 +1,56 @@
import java.util.Random;
public class ShipsMix<T extends EntityWarmlyShip, U extends IDrawningDeck>{
public T[] _ships;
public U[] _decks;
int shipsCount = 0;
int deckCount = 0;
String indexX;
String indexY;
public ShipsMix(int countE,int countI){
_ships = (T[]) new EntityWarmlyShip[countE];
_decks = (U[]) new IDrawningDeck[countI];
}
public int Insert(T ship){
if(shipsCount < _ships.length){
_ships[shipsCount] = ship;
++shipsCount;
return shipsCount - 1;
}
return -1;
}
public int Insert(U deck){
if(deckCount < _decks.length){
_decks[deckCount] = deck;
++deckCount;
return deckCount - 1;
}
return -1;
}
public void SetIndexs(int ind1, int ind2)
{
indexX = Integer.toString(ind1);
indexY = Integer.toString(ind2);
}
public DrawingShip MakeShipMix(){
Random random = new Random();
int indEnt = 0;
int indIllum = 0;
if(shipsCount - 1 != 0 && deckCount - 1 != 0){
indEnt = random.nextInt(0,shipsCount - 1);
indIllum = random.nextInt(0, deckCount - 1);
}
T plane = (T)_ships[indEnt];
U illum = (U)_decks[indIllum];
SetIndexs(indEnt,indIllum);
if(plane instanceof EntityMotorShip){
return new DrawningMotorShip(plane,illum);
}
return new DrawingShip(plane,illum);
}
}