This commit is contained in:
the 2022-12-06 12:08:04 +04:00
parent 2accdec263
commit cadb01023b
10 changed files with 444 additions and 216 deletions

View File

@ -17,12 +17,18 @@ public class DrawingMotorShip extends DrawingShip {
}
g.setColor(motorShip.getDopColor());
if (motorShip.getPipes()) {
g.setStroke(new BasicStroke(8));
}
g.setStroke(new BasicStroke(6));
if (motorShip.getFuelTank()) {
if (motorShip.getPipes()) {
g.fillRect((int)_startPosX + 20, (int)_startPosY - 5, 5, 20);
g.fillRect((int)_startPosX + 30, (int)_startPosY - 10, 5, 25);
g.fillRect((int)_startPosX + 40, (int)_startPosY - 5, 5, 20);
}
g.setColor(motorShip.getBodyColor());
super.drawTransport(g);
g.setColor(motorShip.getDopColor());
if (motorShip.getFuelTank()) {
g.fillOval((int)_startPosX + 30, (int)_startPosY + 20, 25, 10);
}
}
}

View File

@ -24,12 +24,10 @@ public class DrawingRoundDecks implements IDrawingDecks {
g.setColor(color != null ? color : Color.BLACK);
switch (decksCount) {
case Two: {
g.fillRect(x, y + 5, 15, 5);
g.fillPolygon(new int[] {x, x, x + 5}, new int[] {y + 5, y + 10, y + 10}, 3);
g.fillOval(x, y, 20, 20);
}
case Three: {
g.fillRect(x + shipWidth - 20, y, 20, 10);
g.fillPolygon(new int[] {x + shipWidth - 20, x + shipWidth - 20, x + shipWidth - 25}, new int[] {y, y + 10, y + 10}, 3);
g.fillOval(x + shipWidth - 20, y, 20, 20);
}
}
}

View File

@ -24,12 +24,10 @@ public class DrawingTriDecks implements IDrawingDecks {
g.setColor(color != null ? color : Color.BLACK);
switch (decksCount) {
case Two: {
g.fillRect(x, y + 5, 15, 5);
g.fillPolygon(new int[] {x, x, x + 5}, new int[] {y + 5, y + 10, y + 10}, 3);
}
case Three: {
g.fillRect(x + shipWidth - 20, y, 20, 10);
g.fillPolygon(new int[] {x + shipWidth - 20, x + shipWidth - 20, x + shipWidth - 25}, new int[] {y, y + 10, y + 10}, 3);
g.fillPolygon(new int[] {x + shipWidth - 20, x + shipWidth - 20, x + shipWidth}, new int[] {y, y + 10, y + 10}, 3);
}
}
}

View File

@ -1,110 +0,0 @@
import javax.swing.*;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.Random;
public class FormMap extends JFrame {
private JPanel shipPane;
private JLabel speedLabel;
private JLabel weightLabel;
private JLabel colorLabel;
private JPanel pictureBox;
private JButton buttonUp;
private JButton buttonDown;
private JButton buttonLeft;
private JButton buttonRight;
private JButton createButton;
private JButton createAdvancedButton;
private JComboBox comboBoxSelectorMap;
private AbstractMap _abstractMap;
private Image bufferedImage;
public FormMap() {
this.setTitle("Ship");
this.setContentPane(shipPane);
_abstractMap = new SimpleMap();
createButton.addActionListener(e -> {
Random rnd = new Random();
var ship = new DrawingShip(
rnd.nextInt(100, 300),
rnd.nextInt(1000, 2000),
new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)),
rnd.nextInt(1, 4)
);
setData(ship);
});
buttonLeft.setForeground(new Color(0, 0, 0, 0));
buttonRight.setForeground(new Color(0, 0, 0, 0));
buttonUp.setForeground(new Color(0, 0, 0, 0));
buttonDown.setForeground(new Color(0, 0, 0, 0));
buttonLeft.addActionListener(e -> {
if (_abstractMap != null) {
bufferedImage = _abstractMap.moveObject(Direction.Left);
repaint();
}
});
buttonRight.addActionListener(e -> {
if (_abstractMap != null) {
bufferedImage = _abstractMap.moveObject(Direction.Right);
repaint();
}
});
buttonUp.addActionListener(e -> {
if (_abstractMap != null) {
bufferedImage = _abstractMap.moveObject(Direction.Up);
repaint();
}
});
buttonDown.addActionListener(e -> {
if (_abstractMap != null) {
bufferedImage = _abstractMap.moveObject(Direction.Down);
repaint();
}
});
createAdvancedButton.addActionListener(e -> {
Random rnd = new Random();
var ship = new DrawingMotorShip(
rnd.nextInt(100, 300),
rnd.nextInt(1000, 2000),
new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)),
rnd.nextInt(1, 4),
new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)),
rnd.nextBoolean(),
rnd.nextBoolean()
);
setData(ship);
});
comboBoxSelectorMap.addItemListener(e -> {
if (e.getStateChange() == ItemEvent.SELECTED) {
switch (e.getItem().toString()) {
case "Простая карта" -> _abstractMap = new SimpleMap();
case "Лесная карта" -> _abstractMap = new WaterMap();
}
}
});
}
private void setData(DrawingShip ship) {
Random rnd = new Random();
ship.SetPosition(rnd.nextInt(10, 100), rnd.nextInt(10, 100), pictureBox.getWidth(), pictureBox.getHeight());
speedLabel.setText(String.format("Скорость: %d", ship.ship.getSpeed()));
weightLabel.setText(String.format("Вес: %f", ship.ship.getWeight()));
colorLabel.setText(String.format("Цвет: %x", ship.getShip().getBodyColor().getRGB()));
bufferedImage = _abstractMap.createMap(pictureBox.getWidth(), pictureBox.getHeight(), new DrawingObjectShip(ship));
repaint();
}
@Override
public void paint(Graphics g) {
super.paint(g);
if (bufferedImage != null) {
pictureBox.paintComponents(bufferedImage.getGraphics());
pictureBox.getGraphics().drawImage(bufferedImage, 0, 0, null);
}
}
}

View File

@ -1,61 +1,103 @@
<?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="shipPane" layout-manager="GridLayoutManager" row-count="2" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="FormMapWithSetShips">
<grid id="27dc6" binding="paneShips" 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="420"/>
<xy x="20" y="20" width="620" height="400"/>
</constraints>
<properties>
<minimumSize width="80" height="50"/>
<preferredSize width="640" height="480"/>
</properties>
<properties/>
<border type="none"/>
<children>
<grid id="57516" layout-manager="GridLayoutManager" row-count="1" 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="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="dcb7f" 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>
<component id="998a6" 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="cffd1" class="javax.swing.JLabel" binding="colorLabel">
<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>
<grid id="52641" binding="pictureBox" layout-manager="GridLayoutManager" row-count="4" column-count="6" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="fcfe1" 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/>
</grid>
<grid id="f232" binding="toolsGroup" layout-manager="GridLayoutManager" row-count="9" 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="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="none"/>
<children>
<component id="6937b" class="javax.swing.JButton" binding="buttonUp">
<component id="d7a62" class="javax.swing.JLabel" binding="toolsLabel">
<constraints>
<grid row="2" column="4" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="1" indent="0" use-parent-layout="false">
<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="1c91f">
<constraints>
<grid row="0" column="1" row-span="1" col-span="3" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<vspacer id="352bc">
<constraints>
<grid row="5" column="0" row-span="2" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
<component id="6a0f5" class="javax.swing.JComboBox" binding="comboBoxMapSelector">
<constraints>
<grid row="1" column="1" 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>
<component id="52049" class="javax.swing.JButton" binding="buttonAddShip">
<constraints>
<grid row="2" column="1" 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="2f9ed" class="javax.swing.JFormattedTextField" binding="textBoxPosition">
<constraints>
<grid row="3" column="1" 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>
<component id="1dbb5" class="javax.swing.JButton" binding="buttonRemoveShip">
<constraints>
<grid row="4" column="1" row-span="1" col-span="3" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<actionCommand value=""/>
<text value="Удалить корабль"/>
</properties>
</component>
<component id="4e6ca" class="javax.swing.JButton" binding="buttonShowStorage">
<constraints>
<grid row="5" column="1" 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="51371" class="javax.swing.JButton" binding="buttonShowOnMap">
<constraints>
<grid row="6" column="1" 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="fb3e4" class="javax.swing.JButton" binding="buttonUp">
<constraints>
<grid row="7" column="1" row-span="1" col-span="3" vsize-policy="0" hsize-policy="0" 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"/>
@ -68,19 +110,9 @@
<text value=""/>
</properties>
</component>
<hspacer id="48a84">
<component id="328f6" class="javax.swing.JButton" binding="buttonDown">
<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="f9d1a">
<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>
<component id="e761a" class="javax.swing.JButton" binding="buttonDown">
<constraints>
<grid row="3" column="4" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false">
<grid row="8" 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"/>
@ -92,9 +124,9 @@
<text value=""/>
</properties>
</component>
<component id="b99d6" class="javax.swing.JButton" binding="buttonLeft">
<component id="4f2dc" class="javax.swing.JButton" binding="buttonLeft">
<constraints>
<grid row="3" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false">
<grid row="8" 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"/>
@ -107,9 +139,9 @@
<text value=""/>
</properties>
</component>
<component id="7f134" class="javax.swing.JButton" binding="buttonRight">
<component id="bac06" class="javax.swing.JButton" binding="buttonRight">
<constraints>
<grid row="3" column="5" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false">
<grid row="8" 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"/>
@ -122,37 +154,8 @@
<text value=""/>
</properties>
</component>
<component id="fc5b2" class="javax.swing.JButton" binding="createButton">
<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>
<alignmentX value="0.0"/>
<margin top="0" left="0" bottom="0" right="0"/>
<text value="Создать"/>
</properties>
</component>
<component id="65e99" class="javax.swing.JButton" binding="createAdvancedButton">
<constraints>
<grid row="3" 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>
<component id="80160" class="javax.swing.JComboBox" binding="comboBoxSelectorMap">
<constraints>
<grid row="0" column="0" row-span="1" col-span="2" 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>
</children>
</grid>
</form>
</form>

150
FormMapWithSetShips.java Normal file
View File

@ -0,0 +1,150 @@
import javax.swing.*;
import javax.swing.text.DefaultFormatterFactory;
import javax.swing.text.MaskFormatter;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.text.ParseException;
public class FormMapWithSetShips extends JFrame {
private JPanel pictureBox;
private JPanel toolsGroup;
private JLabel toolsLabel;
private JComboBox comboBoxMapSelector;
private JButton buttonAddShip;
private JPanel paneShips;
private JFormattedTextField textBoxPosition;
private JButton buttonRemoveShip;
private JButton buttonShowStorage;
private JButton buttonUp;
private JButton buttonDown;
private JButton buttonLeft;
private JButton buttonRight;
private JButton buttonShowOnMap;
private Image bufferedImage;
private MapWithSetShipsGeneric<DrawingObjectShip, AbstractMap> _mapShipsCollectionGeneric;
public FormMapWithSetShips() {
this.setTitle("Ship");
this.setContentPane(paneShips);
try {
textBoxPosition.setFormatterFactory(new DefaultFormatterFactory(new MaskFormatter("##")));
} catch (ParseException e) {
e.printStackTrace();
}
comboBoxMapSelector.addActionListener(e -> {
AbstractMap map = switch (((JComboBox) e.getSource()).getSelectedItem().toString()) {
case "Простая карта" -> new SimpleMap();
case "Водная карта" -> new WaterMap();
default -> null;
};
if (map != null) {
_mapShipsCollectionGeneric = new MapWithSetShipsGeneric<>(pictureBox.getWidth(), pictureBox.getHeight(), map);
} else {
_mapShipsCollectionGeneric = null;
}
});
buttonAddShip.addActionListener(e -> {
if (_mapShipsCollectionGeneric == null) {
return;
}
FormShip dialog = new FormShip();
dialog.setSize(800, 500);
dialog.setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
if (dialog.getSelectedShip() != null) {
DrawingObjectShip ship = new DrawingObjectShip(dialog.getSelectedShip());
if (_mapShipsCollectionGeneric.addShip(ship) != -1)
{
JOptionPane.showMessageDialog(this, "Объект добавлен", "Успех", JOptionPane.INFORMATION_MESSAGE);
bufferedImage = _mapShipsCollectionGeneric.showSet();
repaint();
}
else
{
JOptionPane.showMessageDialog(this, "Не удалось добавить объект", "Провал", JOptionPane.INFORMATION_MESSAGE);
}
}
});
buttonRemoveShip.addActionListener(e -> {
String text = textBoxPosition.getText();
if (text == null || text.isEmpty()) {
return;
}
if (JOptionPane.showConfirmDialog(this, "Удалить объект?", "Удаление", JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION) {
return;
}
int position = Integer.parseInt(text);
if (_mapShipsCollectionGeneric.removeShipAt(position) != null) {
JOptionPane.showMessageDialog(this, "Объект удалён", "Успех", JOptionPane.INFORMATION_MESSAGE);
bufferedImage = _mapShipsCollectionGeneric.showSet();
repaint();
} else {
JOptionPane.showMessageDialog(this, "Не удалось удалить объект", "Провал", JOptionPane.INFORMATION_MESSAGE);
}
});
buttonShowStorage.addActionListener(e -> {
if (_mapShipsCollectionGeneric == null) {
return;
}
bufferedImage = _mapShipsCollectionGeneric.showSet();
repaint();
});
buttonShowOnMap.addActionListener(e -> {
if (_mapShipsCollectionGeneric == null) {
return;
}
bufferedImage = _mapShipsCollectionGeneric.showOnMap();
repaint();
});
buttonLeft.addActionListener(e -> {
if (_mapShipsCollectionGeneric != null) {
bufferedImage = _mapShipsCollectionGeneric.moveObject(Direction.Left);
repaint();
}
});
buttonRight.addActionListener(e -> {
if (_mapShipsCollectionGeneric != null) {
bufferedImage = _mapShipsCollectionGeneric.moveObject(Direction.Right);
repaint();
}
});
buttonUp.addActionListener(e -> {
if (_mapShipsCollectionGeneric != null) {
bufferedImage = _mapShipsCollectionGeneric.moveObject(Direction.Up);
repaint();
}
});
buttonDown.addActionListener(e -> {
if (_mapShipsCollectionGeneric != null) {
bufferedImage = _mapShipsCollectionGeneric.moveObject(Direction.Down);
repaint();
}
});
}
@Override
public void paint(Graphics g) {
super.paint(g);
if (bufferedImage != null) {
pictureBox.paintComponents(bufferedImage.getGraphics());
pictureBox.getGraphics().drawImage(bufferedImage, 0, 0, null);
}
}
}

View File

@ -20,12 +20,14 @@ public class FormShipDisplay extends JFrame {
storage = new EntityWithDecks<>(20);
for(int i = 0; i < 20; i++) {
var base_color = new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256));
var dop_color = new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256));
if (rnd.nextBoolean()) {
storage.add(new EntityMotorShip(
rnd.nextInt(100, 300),
rnd.nextInt(1000, 2000),
new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)),
new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)),
base_color,
dop_color,
rnd.nextBoolean(),
rnd.nextBoolean()
));
@ -33,10 +35,10 @@ public class FormShipDisplay extends JFrame {
storage.add(new EntityShip(
rnd.nextInt(100, 300),
rnd.nextInt(1000, 2000),
new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256))
base_color
));
}
storage.add(DecksType.random(rnd.nextInt(0, 3), new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256))));
storage.add(DecksType.random(rnd.nextInt(0, 3), base_color));
}
buttonRefresh.addActionListener(e -> {

111
MapWithSetShipsGeneric.java Normal file
View File

@ -0,0 +1,111 @@
import java.awt.*;
import java.awt.image.BufferedImage;
public class MapWithSetShipsGeneric<T extends IDrawingObject, U extends AbstractMap> {
public final int _pictureWidth;
public final int _pictureHeight;
public final int _placeSizeWidth = 210;
public final int _placeSizeHeight = 90;
private final SetShipsGeneric<T> _setShips;
private final U _map;
public MapWithSetShipsGeneric(int picWidth, int picHeight, U map) {
int width = picWidth / _placeSizeWidth;
int height = picHeight / _placeSizeHeight;
_setShips = new SetShipsGeneric<T>(width * height);
_pictureWidth = picWidth;
_pictureHeight = picHeight;
_map = map;
}
public int addShip(T ship) {
return _setShips.insert(ship);
}
public T removeShipAt(int position) {
return _setShips.remove(position);
}
public Image showSet() {
BufferedImage img = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = (Graphics2D) img.getGraphics();
drawBackground(g2d);
drawShips(g2d);
return img;
}
public Image showOnMap() {
shaking();
for (int i = 0; i < _setShips.getCount(); i++)
{
var ship = _setShips.get(i);
if (ship != null)
{
return _map.createMap(_pictureWidth, _pictureHeight, ship);
}
}
return new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_ARGB);
}
public Image moveObject(Direction direction) {
if (_map != null) {
return _map.moveObject(direction);
}
return new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_ARGB);
}
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 ship = _setShips.get(j);
if (ship != null)
{
_setShips.insert(ship, i);
_setShips.remove(j);
break;
}
}
if (j <= i)
{
return;
}
}
}
}
private void drawBackground(Graphics2D g) {
Color pen = Color.black;
Color box = new Color(0, 100, 0, 255);
Color flag = Color.red;
Stroke normalStroke = new BasicStroke(1);
Stroke penStroke = new BasicStroke(3);
Stroke thinPenStroke = new BasicStroke(2);
for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++)
{
for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j) {
}
}
g.setStroke(normalStroke);
}
private void drawShips(Graphics2D g) {
int width = _pictureWidth / _placeSizeWidth;
int height = _pictureHeight / _placeSizeHeight;
for (int i = 0; i < _setShips.getCount(); i++)
{
var ship = _setShips.get(i);
if (ship != null)
{
ship.setObject(i % width * _placeSizeWidth + 10, (height - 1 - i / width) * _placeSizeHeight + 10, _pictureWidth, _pictureHeight);
ship.drawingObject(g);
}
}
}
}

View File

@ -2,7 +2,7 @@ import javax.swing.*;
public class Program {
public static void main(String[] args) {
FormMap form = new FormMap();
FormShipDisplay form = new FormShipDisplay();
form.setSize(640, 480);
form.setVisible(true);
form.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

70
SetShipsGeneric.java Normal file
View File

@ -0,0 +1,70 @@
public class SetShipsGeneric<T> {
private final Object[] _places;
public int getCount() {
return _places.length;
}
public SetShipsGeneric(int count) {
_places = new Object[count];
}
public int insert(T ship) {
return insert(ship, 0);
}
public int insert(T ship, int position) {
if (position < 0 || position >= getCount()) {
return -1;
}
if (_places[position] == null) {
_places[position] = ship;
return position;
}
int firstNull = -1;
for (int i = position + 1; i < getCount(); i++)
{
if (_places[i] == null)
{
firstNull = i;
break;
}
}
if (firstNull == -1)
{
return -1;
}
System.arraycopy(_places, position, _places, position + 1, firstNull - position);
_places[position] = ship;
return position;
}
@SuppressWarnings("unchecked")
public T remove(int position) {
if (position < 0 || position >= getCount())
{
return null;
}
var result = _places[position];
_places[position] = null;
return (T) result;
}
@SuppressWarnings("unchecked")
public T get(int position) {
if (position < 0 || position >= getCount()) {
return null;
}
return (T) _places[position];
}
}