Коммит

This commit is contained in:
dimazhelovanov 2022-12-20 20:50:09 +03:00
parent e71eaf0f2d
commit 405904860a
11 changed files with 676 additions and 8 deletions

View File

@ -2,5 +2,6 @@ public enum Direction {
Up,
Down,
Left,
Right
Right,
None
}

View File

@ -59,6 +59,10 @@ public class DrawningBattleship {
Battleship = new EntityBattleship(speed, weight, bodyColor);
}
public DrawningBattleship(EntityBattleship entity, IDrawningBlocks blocks){
Battleship = entity;
iDrawingBlocks = blocks;
}
protected DrawningBattleship(int speed, float weight, Color bodyColor, int battleshipWidth, int battleshipHeight)
{
this(speed, weight, bodyColor);

View File

@ -6,7 +6,10 @@ public class DrawningLinkor extends DrawningBattleship {
super(speed, weight, bodyColor, 120, 50);
Battleship = new EntityLinkor(speed, weight, bodyColor, dopColor, turret, missileBay);
}
public DrawningLinkor(EntityBattleship battleship, IDrawningBlocks blocks){
super(battleship, blocks);
Battleship = battleship;
}
@Override
public void DrawTransport(Graphics2D g) {

View File

@ -15,8 +15,6 @@ public class DrawningTriangleBlocks implements IDrawningBlocks {
g.fillPolygon(new int[]{x + 58, x + 66, x+ 72}, new int[]{y + 7, y, y + 7}, 3);
g.fillPolygon(new int[]{x + 58, x + 66, x+72}, new int[]{y + 49, y + 42, y + 49}, 3);
}
case Six -> {
g.fillPolygon(new int[]{x + 46, x + 54, x+ 60 }, new int[]{y + 7, y, y + 7}, 3);

135
FormBattleship.java Normal file
View File

@ -0,0 +1,135 @@
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.image.BufferedImage;
import java.util.Random;
public class FormBattleship extends JComponent {
private AbstractMap _abstractMap;
private DrawningBattleship _battleship;
private DrawningBattleship SelectedBattleship;
private EntityBattleship _entityBattleship;
private BufferedImage bufferedImage = null;
public DrawningBattleship GetSelectedBattleship() {
return SelectedBattleship;
}
public FormBattleship(JDialog caller) {
// JFrame form = new JFrame("Военный корабль");
// form.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// form.setSize(800, 500);
// form.setVisible(true);
// form.setLocationRelativeTo(null);
// form.addComponentListener(new ComponentListener() {
// @Override
// public void componentResized(ComponentEvent e) {
// if(_battleship != null) _battleship.ChangeBorders(getWidth(), getHeight());
// repaint();
// }
//
// @Override
// public void componentMoved(ComponentEvent e) {
// }
//
// @Override
// public void componentShown(ComponentEvent e) {
// }
//
// @Override
// public void componentHidden(ComponentEvent e) {
// }
// });
Panel statusPanel = new Panel();
statusPanel.setBackground(Color.WHITE);
statusPanel.setLayout(new FlowLayout());
setLayout(new BorderLayout());
add(statusPanel, BorderLayout.SOUTH);
Label speedLabel = new Label("Скорость: ");
Label weightLabel = new Label("Вес: ");
Label colorLabel = new Label("Цвет: ");
JButton createButton = new JButton("Создать");
createButton.addActionListener(e -> {
Random rnd = new Random();
Color colorFirst = JColorChooser.showDialog(null, "Color", new Color(rnd.nextInt(256), rnd.nextInt(256),rnd.nextInt(256)));
_battleship = new DrawningBattleship(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000),
colorFirst);
_battleship.SetPosition(10 + rnd.nextInt(90), 10 + rnd.nextInt(90), getWidth(), getHeight() - 75);
speedLabel.setText("Скорость: " + _battleship.Battleship.GetSpeed());
weightLabel.setText("Вес: " + (int)_battleship.Battleship.GetWeight());
colorLabel.setText("Цвет: " + _battleship.Battleship.GetBodyColor().getRed() + " " +
_battleship.Battleship.GetBodyColor().getGreen() + " " + _battleship.Battleship.GetBodyColor().getBlue() );
repaint();
});
JButton modifiedButton = new JButton("Модификация");
modifiedButton.addActionListener(e -> {
Random rnd = new Random();
Color colorFirst = JColorChooser.showDialog(null, "Color", new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)));
Color colorSecond = JColorChooser.showDialog(null, "Color", new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)));
_battleship = new DrawningLinkor(rnd.nextInt(200) + 100, rnd.nextInt(1000) + 1000,
colorFirst, colorSecond, rnd.nextBoolean(), rnd.nextBoolean());
_battleship.SetPosition(10 + rnd.nextInt(90), 10 + rnd.nextInt(90), getWidth(), getHeight() - 75);
speedLabel.setText("Скорость: " + _battleship.Battleship.GetSpeed());
weightLabel.setText("Вес: " + (int)_battleship.Battleship.GetWeight());
colorLabel.setText("Цвет:: " + _battleship.Battleship.GetBodyColor().getRed() + " " + _battleship.Battleship.GetBodyColor().getGreen() + " " + _battleship.Battleship.GetBodyColor().getBlue() );
if (_abstractMap != null) bufferedImage = _abstractMap.CreateMap(1000, 490, new DrawningObjectBattleship(_battleship));
repaint();
});
JButton selectButton = new JButton("Выбрать");
selectButton.addActionListener(e -> {
SelectedBattleship = _battleship;
caller.dispose();
});
statusPanel.add(createButton);
statusPanel.add(modifiedButton);
statusPanel.add(selectButton);
statusPanel.add(speedLabel);
statusPanel.add(weightLabel);
statusPanel.add(colorLabel);
JButton upButton = new JButton("");
JButton rightButton = new JButton("");
JButton leftButton = new JButton("");
JButton downButton = new JButton("");
upButton.addActionListener(e -> {
if (_battleship != null) _battleship.MoveTransport(Direction.Up);
repaint();
});
rightButton.addActionListener(e -> {
if (_battleship != null) _battleship.MoveTransport(Direction.Right);
repaint();
});
leftButton.addActionListener(e -> {
if (_battleship != null) _battleship.MoveTransport(Direction.Left);
repaint();
});
downButton.addActionListener(e -> {
if (_battleship != null) _battleship.MoveTransport(Direction.Down);
repaint();
});
statusPanel.add(leftButton);
statusPanel.add(upButton);
statusPanel.add(rightButton);
statusPanel.add(downButton);
//getContentPane().add(this);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
if (_battleship != null) _battleship.DrawTransport(g2);
super.repaint();
}
}

View File

@ -10,9 +10,7 @@ public class FormMap extends JComponent {
private AbstractMap _abstractMap;
private DrawningBattleship _battleship;
private BufferedImage bufferedImage;
public static void main(String[] args) {
FormMap formMap = new FormMap();
}
public FormMap() {
JFrame form = new JFrame("Карта");
form.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
@ -55,7 +53,6 @@ public class FormMap extends JComponent {
createButton.addActionListener(e -> {
Random rnd = new Random();
var battleship = new DrawningBattleship(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000),
Color.getHSBColor(rnd.nextInt(0, 256), rnd.nextInt(0, 256),
rnd.nextInt(0, 256)));

View File

@ -0,0 +1,197 @@
import javax.swing.*;
import javax.swing.text.MaskFormatter;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.text.ParseException;
public class FormMapWithSetBattleship extends JComponent {
private BufferedImage bufferedImage = null;
private MapWithSetBattleshipsGeneric<DrawningObjectBattleship, AbstractMap> _mapBattleshipsCollectionGeneric;
public static void main(String[] args) {
FormMapWithSetBattleship formMap = new FormMapWithSetBattleship();
}
public FormMapWithSetBattleship(){
JFrame form = new JFrame("FormMapWithSetBattleship");
form.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
form.setSize(750, 500);
form.setLocationRelativeTo(null);
Panel statusPanel = new Panel();
statusPanel.setBackground(Color.WHITE);
statusPanel.setLayout(new GridLayout(0, 1, 20, 20));
setLayout(new BorderLayout());
add(statusPanel, BorderLayout.EAST);
String[] maps = {
"Простая карта",
"Морская карта",
};
JComboBox mapSelectComboBox = new JComboBox(maps);
mapSelectComboBox.setEditable(true);
mapSelectComboBox.addActionListener(e -> {
AbstractMap map = null;
String item = (String)mapSelectComboBox.getSelectedItem();
switch (item) {
case "Простая карта":
map = new SimpleMap();
break;
case "Морская карта":
map = new SeaMap();
break;
}
if (map != null)
{
_mapBattleshipsCollectionGeneric = new MapWithSetBattleshipsGeneric<DrawningObjectBattleship, AbstractMap>
(1000, 700, map);
}
else
{
_mapBattleshipsCollectionGeneric = null;
}
});
statusPanel.add(mapSelectComboBox);
JButton addAirbusButton = new JButton("Добавить корабль");
addAirbusButton.addActionListener(e -> {
if (_mapBattleshipsCollectionGeneric == null)
{
return;
}
JDialog dialog = new JDialog(form, "Диалого", true);
FormBattleship formShip = new FormBattleship(dialog);
dialog.setSize(800, 500);
dialog.setContentPane(formShip);
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
DrawningObjectBattleship battleship = new DrawningObjectBattleship(formShip.GetSelectedBattleship());
if (_mapBattleshipsCollectionGeneric.Add(battleship) != -1) {
JOptionPane.showMessageDialog(form, "Объект добавлен", "Добавление", JOptionPane.OK_CANCEL_OPTION);
bufferedImage = _mapBattleshipsCollectionGeneric.ShowSet();
repaint();
}
else {
JOptionPane.showMessageDialog(form, "Объект не добавлен", "Добавление", JOptionPane.OK_CANCEL_OPTION);
}
});
statusPanel.add(addAirbusButton);
JFormattedTextField maskedTextFieldPosition = null;
try {
MaskFormatter positionMask = new MaskFormatter("##");
maskedTextFieldPosition = new JFormattedTextField(positionMask);
statusPanel.add(maskedTextFieldPosition);
}
catch (ParseException e) {
e.printStackTrace();
}
JButton deleteAirbusButton = new JButton("Удалить корабль");
JFormattedTextField finalMaskedTextFieldPosition = maskedTextFieldPosition;
deleteAirbusButton.addActionListener(e -> {
if ((String)mapSelectComboBox.getSelectedItem() == null) {
return;
}
if (finalMaskedTextFieldPosition == null) {
return;
}
int position = Integer.parseInt(finalMaskedTextFieldPosition.getText());
if (_mapBattleshipsCollectionGeneric.Subtraction(position) != null) {
JOptionPane.showMessageDialog(form, "Объект удален", "Удаление", JOptionPane.OK_CANCEL_OPTION);
bufferedImage = _mapBattleshipsCollectionGeneric.ShowSet();
repaint();
}
else{
JOptionPane.showMessageDialog(form, "Объект не удален", "Удаление", JOptionPane.OK_CANCEL_OPTION);
}
});
statusPanel.add(deleteAirbusButton);
JButton showStorageButton = new JButton("Хранилище");
showStorageButton.addActionListener(e -> {
if (_mapBattleshipsCollectionGeneric == null)
{
return;
}
bufferedImage = _mapBattleshipsCollectionGeneric.ShowSet();
repaint();
});
statusPanel.add(showStorageButton);
JButton showOnMapButton = new JButton("Карта");
showOnMapButton.addActionListener(e -> {
if (_mapBattleshipsCollectionGeneric == null)
{
return;
}
bufferedImage = _mapBattleshipsCollectionGeneric.ShowOnMap();
});
statusPanel.add(showOnMapButton);
ActionListener moveButtonListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_mapBattleshipsCollectionGeneric == null)
{
return;
}
String name = e.getActionCommand();
Direction dir = Direction.None;
switch (name)
{
case "":
dir = Direction.Up;
break;
case "":
dir = Direction.Down;
break;
case "":
dir = Direction.Left;
break;
case "":
dir = Direction.Right;
break;
}
bufferedImage = _mapBattleshipsCollectionGeneric.MoveObject(dir);
}
};
//Кнопки управления
JButton moveDownButton = new JButton("");
moveDownButton.addActionListener(moveButtonListener);
JButton moveUpButton = new JButton("");
moveUpButton.addActionListener(moveButtonListener);
JButton moveLeftButton = new JButton("");
moveLeftButton.addActionListener(moveButtonListener);
JButton moveRightButton = new JButton("");
moveRightButton.addActionListener(moveButtonListener);
JButton showGalleryButton = new JButton("Галерея");
showGalleryButton.addActionListener(e -> {
new FormParameterClass();
});
statusPanel.add(showGalleryButton);
statusPanel.add(moveUpButton);
statusPanel.add(moveDownButton);
statusPanel.add(moveLeftButton);
statusPanel.add(moveRightButton);
form.getContentPane().add(this);
form.setVisible(true);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
if (bufferedImage != null) g2.drawImage(bufferedImage, 0,0,600,500,null);
super.repaint();
}
}

96
FormParameterClass.java Normal file
View File

@ -0,0 +1,96 @@
import javax.swing.*;
import java.awt.*;
import java.util.Random;
public class FormParameterClass extends JComponent {
private DrawningBattleship battleship1;
private DrawningBattleship battleship2;
private DrawningBattleship battleship3;
private DrawningBattleship battleship4;
private DrawningBattleship battleship5;
MyParametrClass<EntityBattleship, IDrawningBlocks> parameterClass;
public FormParameterClass(){
JFrame form = new JFrame("Экземпляры от параметризованного класса");
form.setSize(900, 500);
form.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
form.setLocationRelativeTo(null);
Panel statusPanel = new Panel();
statusPanel.setBackground(Color.WHITE);
statusPanel.setLayout(new FlowLayout());
setLayout(new BorderLayout());
add(statusPanel, BorderLayout.SOUTH);
JButton showRandomEntity = new JButton("Создать");
showRandomEntity.addActionListener(e -> {
Random random = new Random();
int[] arrayBlocks = {2, 4, 6};
if (parameterClass == null) {
parameterClass = new MyParametrClass<EntityBattleship, IDrawningBlocks>(5, 5);
for (int i = 0; i < 5; i ++) {
if (random.nextBoolean()) {
parameterClass.Insert(new EntityBattleship(random.nextInt(100), random.nextInt(100),
new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255))));
}
else {
parameterClass.Insert(new EntityLinkor(random.nextInt(100), random.nextInt(100),
new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255)),
new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255)),
random.nextBoolean(), random.nextBoolean()));
}
}
for (int i = 0; i < 5; i ++) {
int rnd = random.nextInt(3);
switch (rnd) {
case 0:
parameterClass.Insert(new DrawningBlocks(arrayBlocks[random.nextInt(3)],
new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255))));
break;
case 1:
parameterClass.Insert(new DrawningRoundBlocks(arrayBlocks[random.nextInt(3)]));
break;
case 2:
parameterClass.Insert(new DrawningTriangleBlocks(arrayBlocks[random.nextInt(3)]));
break;
}
}
}
battleship1 = parameterClass.GetDrawningBattleship();
battleship1.SetPosition(200, 200, form.getWidth(), form.getHeight() - 75);
battleship2 = parameterClass.GetDrawningBattleship();
battleship2.SetPosition(400, 200, form.getWidth(), form.getHeight() - 75);
battleship3 = parameterClass.GetDrawningBattleship();
battleship3.SetPosition(600, 200, form.getWidth(), form.getHeight() - 75);
battleship4 = parameterClass.GetDrawningBattleship();
battleship4.SetPosition(300, 300, form.getWidth(), form.getHeight() - 75);
battleship5 = parameterClass.GetDrawningBattleship();
battleship5.SetPosition(500, 300, form.getWidth(), form.getHeight() - 75);
repaint();
});
statusPanel.add(showRandomEntity);
form.getContentPane().add(this);
form.setVisible(true);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
if (battleship1 != null) battleship1.DrawTransport(g2);
if (battleship2 != null) battleship2.DrawTransport(g2);
if (battleship3 != null) battleship3.DrawTransport(g2);
if (battleship4 != null) battleship4.DrawTransport(g2);
if (battleship5 != null) battleship5.DrawTransport(g2);
super.repaint();
}
}

View File

@ -0,0 +1,139 @@
import java.awt.*;
import java.awt.image.BufferedImage;
public class MapWithSetBattleshipsGeneric <T extends IDrawningObject, U extends AbstractMap>{
/// Ширина окна отрисовки
private final int _pictureWidth;
/// Высота окна отрисовки
private final int _pictureHeight;
/// Размер занимаемого объектом места (ширина)
private final int _placeSizeWidth = 250;
/// Размер занимаемого объектом места (высота)
private final int _placeSizeHeight = 100;
private final SetBattleshipGeneric<T> _setBattleship;
/// Карта
private final U _map;
/// Конструктор
public MapWithSetBattleshipsGeneric(int picWidth, int picHeight, U map)
{
int width = picWidth / _placeSizeWidth;
int height = picHeight / _placeSizeHeight;
_setBattleship = new SetBattleshipGeneric<T>(width * height);
_pictureWidth = picWidth;
_pictureHeight = picHeight;
_map = map;
}
public int Add(T battleship)
{
return this._setBattleship.Insert(battleship);
}
public T Subtraction(int position)
{
return this._setBattleship.Remove(position);
}
public BufferedImage ShowSet()
{
BufferedImage bmp = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_RGB);
Graphics gr = bmp.getGraphics();
DrawBackground((Graphics2D)gr);
DrawBattleship((Graphics2D)gr);
return bmp;
}
public BufferedImage ShowOnMap()
{
Shaking();
for (int i = 0; i < _setBattleship.Count(); i++)
{
var battleship = _setBattleship.Get(i);
if (battleship != null)
{
return _map.CreateMap(_pictureWidth, _pictureHeight, battleship);
}
}
return new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_RGB);
}
/// Перемещение объекта по крате
public BufferedImage MoveObject(Direction direction)
{
if (_map != null)
{
return _map.MoveObject(direction);
}
return new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_RGB);
}
private void Shaking()
{
int j = _setBattleship.Count() - 1;
for (int i = 0; i < _setBattleship.Count(); i++)
{
if (_setBattleship.Get(i) == null)
{
for (; j > i; j--)
{
var battleship = _setBattleship.Get(j);
if (battleship != null)
{
_setBattleship.Insert(battleship, i);
_setBattleship.Remove(j);
break;
}
}
if (j <= i)
{
return;
}
}
}
}
private void DrawBackground(Graphics2D g)
{
g.setColor(Color.blue);
//Brush seaBrush = new SolidBrush(Color.Aqua);
g.fillRect(0, 0, _pictureWidth, _pictureHeight);
for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++)
{
for (int j = 0; j <= _pictureHeight / _placeSizeHeight; ++j)
{
g.setStroke(new BasicStroke(7));
g.setColor(Color.black);
g.drawLine(i * _placeSizeWidth, j * _placeSizeHeight, i *
_placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight);
g.setColor(Color.RED);
g.drawLine(i * _placeSizeWidth + 20, j * _placeSizeHeight-5, i * _placeSizeWidth + 20, j * _placeSizeHeight - 23);
g.drawLine( i * _placeSizeWidth + 70, j * _placeSizeHeight - 5, i * _placeSizeWidth + 70, j * _placeSizeHeight - 23);
}
g.drawLine(i * _placeSizeWidth, 0, i * _placeSizeWidth,
(_pictureHeight / _placeSizeHeight) * _placeSizeHeight);
}
}
private void DrawBattleship(Graphics2D g)
{
int countColumns = _pictureWidth / _placeSizeWidth - 1;
int countRows = _pictureHeight / _placeSizeHeight - 1;
int currentColumn = 0;
for (int i = 0; i < _setBattleship.Count(); i++)
{
if((_setBattleship.Get(i) != null))
{
_setBattleship.Get(i).SetObject((currentColumn) * _placeSizeWidth + 10, (countRows) * _placeSizeHeight + 15, _pictureWidth, _pictureHeight);
_setBattleship.Get(i).DrawningObject(g);
}
if (currentColumn >= countColumns)
{
currentColumn = 0;
countRows--;
}
else
{
currentColumn++;
}
}
}
}

39
MyParametrClass.java Normal file
View File

@ -0,0 +1,39 @@
import java.util.ArrayList;
import java.util.Random;
public class MyParametrClass<T extends EntityBattleship, U extends IDrawningBlocks> {
private final Object[] EntityArray;
private final Object[] BlocksArray;
int entitiesCount = 0;
int blocksCount = 0;
public MyParametrClass(int entitiesCount, int blocksCount) {
EntityArray = new Object[entitiesCount];
BlocksArray = new Object[blocksCount];
}
public void Insert(T battleship){
if(entitiesCount < EntityArray.length) {
EntityArray[entitiesCount] = battleship;
entitiesCount++;
}
}
public void Insert(U drawningBlocks){
if(blocksCount < BlocksArray.length) {
BlocksArray[blocksCount] = drawningBlocks;
blocksCount++;
}
}
public DrawningBattleship GetDrawningBattleship() {
Random random = new Random();
int entityIndex = random.nextInt(EntityArray.length);
int blockIndex = random.nextInt(BlocksArray.length);
EntityBattleship battleship = (T)EntityArray[entityIndex];
IDrawningBlocks blocks = (U)BlocksArray[blockIndex];
if (battleship instanceof EntityLinkor) {
return new DrawningLinkor(battleship, blocks);
}
return new DrawningLinkor(battleship, blocks);
}
}

59
SetBattleshipGeneric.java Normal file
View File

@ -0,0 +1,59 @@
public class SetBattleshipGeneric <T>
{
private final T[] _places;
public SetBattleshipGeneric(int count) {
_places = (T[]) new Object[count];
}
public int Count() {
return _places.length;
}
public int Insert(T battleship)
{
return Insert(battleship, 0);
}
public int Insert(T battleship, int position)
{
if (position >= _places.length || position < 0) return -1;
if (_places[position] == null) {
_places[position] = battleship;
return position;
}
int emptyEl = -1;
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] = battleship;
return position;
}
public T Remove (int position) {
if (position >= _places.length || position < 0) return null;
T result = _places[position];
_places[position] = null;
return result;
}
public T Get(int position)
{
if (position >= Count() || position < 0)
{
return null;
}
return _places[position];
}
}