WIP:Zhelovanov D.Y LabWork04 #4

Closed
Zhelovanov_Dmitrii wants to merge 3 commits from LabWork04 into LabWork03
10 changed files with 239 additions and 131 deletions
Showing only changes of commit 589f563c90 - Show all commits

View File

@ -3,7 +3,6 @@ import java.util.Random;
public class DrawningBattleship {
EntityBattleship Battleship;
private DrawningBlocks drawingBlocks;
private IDrawningBlocks iDrawingBlocks;
public EntityBattleship Battleship()
{return Battleship; }
@ -40,7 +39,6 @@ public class DrawningBattleship {
public DrawningBattleship(int speed, float weight, Color bodyColor)
{
//Battleship.Init(speed, weight, bodyColor);
Random random = new Random();
int[] ArrayBlocks = new int[]{2, 4, 6};
@ -193,6 +191,6 @@ public class DrawningBattleship {
}
public float[] GetCurrentPosition()
{
return new float[] {_startPosY, _startPosX + _battleshipWidth, /*DOWN*/ _startPosY + _battleshipHeight, /*LEFT*/ _startPosX};
return new float[] {_startPosY, _startPosX + _battleshipWidth, _startPosY + _battleshipHeight, _startPosX};
}
}

View File

@ -22,7 +22,7 @@ public class DrawningLinkor extends DrawningBattleship {
if (entityLinkor.turret)
{
//Brush dopBrushRed = new SolidBrush(Color.Red);
g.setColor(entityLinkor.dopColor);
g.fillOval((int)_startPosX + 15, (int)_startPosY, 20, 20);
g.fillOval((int) _startPosX + 15, (int)_startPosY + 30, 20, 20);

View File

@ -6,6 +6,10 @@ public class DrawningObjectBattleship implements IDrawningObject {
public DrawningObjectBattleship(DrawningBattleship battleship){
_battleship = battleship;
}
public DrawningBattleship GetDrawningBattleship() {
return _battleship;
}
@Override
public float getStep() {
if (_battleship.Battleship != null) {

View File

@ -17,30 +17,6 @@ public class FormBattleship extends JComponent {
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());
@ -55,7 +31,7 @@ public class FormBattleship extends JComponent {
createButton.addActionListener(e -> {
Random rnd = new Random();
Color colorFirst = JColorChooser.showDialog(null, "Color", new Color(rnd.nextInt(256), rnd.nextInt(256),rnd.nextInt(256)));
Color colorFirst = JColorChooser.showDialog(null, "Цвет", 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);
@ -69,8 +45,8 @@ public class FormBattleship extends JComponent {
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)));
Color colorFirst = JColorChooser.showDialog(null, "Цвет", new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)));
Color colorSecond = JColorChooser.showDialog(null, "Цвет", 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);
@ -123,7 +99,6 @@ public class FormBattleship extends JComponent {
statusPanel.add(rightButton);
statusPanel.add(downButton);
//getContentPane().add(this);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
@ -131,5 +106,9 @@ public class FormBattleship extends JComponent {
if (_battleship != null) _battleship.DrawTransport(g2);
super.repaint();
}
public void SetBattleship(DrawningObjectBattleship battleship){
_battleship = battleship.GetDrawningBattleship();
repaint();
}
}

View File

@ -5,10 +5,23 @@ import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.text.ParseException;
import java.util.HashMap;
public class FormMapWithSetBattleship extends JComponent {
private BufferedImage bufferedImage = null;
JList listBoxMaps;
Panel statusPanel;
DefaultListModel<String> mapsListModel;
JComboBox mapSelectComboBox;
private final HashMap<String, AbstractMap> _mapsDict = new HashMap<>() {
{
put("Простая карта", new SimpleMap());
put("Морская карта", new SeaMap());
}
};
private final MapsCollection _mapsCollection;
private MapWithSetBattleshipsGeneric<DrawningObjectBattleship, AbstractMap> _mapBattleshipsCollectionGeneric;
public static void main(String[] args) {
FormMapWithSetBattleship formMap = new FormMapWithSetBattleship();
@ -19,7 +32,7 @@ public class FormMapWithSetBattleship extends JComponent {
form.setSize(750, 500);
form.setLocationRelativeTo(null);
Panel statusPanel = new Panel();
statusPanel = new Panel();
statusPanel.setBackground(Color.WHITE);
statusPanel.setLayout(new GridLayout(0, 1, 20, 20));
setLayout(new BorderLayout());
@ -28,56 +41,88 @@ public class FormMapWithSetBattleship extends JComponent {
"Простая карта",
"Морская карта",
};
JComboBox mapSelectComboBox = new JComboBox(maps);
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);
mapsListModel = new DefaultListModel<>();
JButton addAirbusButton = new JButton("Добавить корабль");
addAirbusButton.addActionListener(e -> {
_mapsCollection = new MapsCollection(800, 500);
mapSelectComboBox.removeAllItems();
for (var elem : _mapsDict.keySet())
{
mapSelectComboBox.addItem(elem);
}
JScrollPane listMapPane = new JScrollPane();
listBoxMaps = new JList(mapsListModel);
listBoxMaps.addListSelectionListener(e -> {
if(listBoxMaps.getSelectedValue() == null) return;
bufferedImage = _mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).ShowSet();
repaint();
});
statusPanel.add(listBoxMaps);
listMapPane.setViewportView(listBoxMaps);
listBoxMaps.setLayoutOrientation(JList.VERTICAL);
statusPanel.add(listMapPane);
TextField textFieldMapName = new TextField();
statusPanel.add(textFieldMapName);
statusPanel.add(mapSelectComboBox);
JButton addMapButton = new JButton("Добавить карту");
addMapButton.addActionListener(e -> {
if (_mapBattleshipsCollectionGeneric == null)
if (mapSelectComboBox.getSelectedIndex() == -1 || textFieldMapName.getText() == null)
{
return;
}
JDialog dialog = new JDialog(form, "Диалого", true);
if (!_mapsDict.containsKey(mapSelectComboBox.getSelectedItem().toString()))
{
return;
}
_mapsCollection.AddMap(textFieldMapName.getText(), _mapsDict.get(mapSelectComboBox.getSelectedItem().toString()));
ReloadMaps();
});
statusPanel.add(addMapButton);
JButton deleteMapButton = new JButton("Удалить карту");
deleteMapButton.addActionListener(e -> {
// логика удаления
if (listBoxMaps.getSelectedIndex() == -1)
{
return;
}
if(listBoxMaps.getSelectedValue().toString() == null) return;
_mapsCollection.DelMap(listBoxMaps.getSelectedValue().toString());
ReloadMaps();
repaint();
});
statusPanel.add(deleteMapButton);
JButton addBattleshipButton = new JButton("Добавить корабль");
addBattleshipButton.addActionListener(e -> {
if (listBoxMaps.getSelectedIndex() == -1)
{
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) {
if (_mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).Add(battleship)!= -1) {
JOptionPane.showMessageDialog(form, "Объект добавлен", "Добавление", JOptionPane.OK_CANCEL_OPTION);
bufferedImage = _mapBattleshipsCollectionGeneric.ShowSet();
bufferedImage = _mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).ShowSet();
repaint();
}
else {
JOptionPane.showMessageDialog(form, "Объект не добавлен", "Добавление", JOptionPane.OK_CANCEL_OPTION);
}
});
statusPanel.add(addAirbusButton);
statusPanel.add(addBattleshipButton);
JFormattedTextField maskedTextFieldPosition = null;
try {
@ -89,9 +134,10 @@ public class FormMapWithSetBattleship extends JComponent {
e.printStackTrace();
}
JButton deleteAirbusButton = new JButton("Удалить корабль");
JButton deleteBattleshipButton = new JButton("Удалить корабль");
JFormattedTextField finalMaskedTextFieldPosition = maskedTextFieldPosition;
deleteAirbusButton.addActionListener(e -> {
deleteBattleshipButton.addActionListener(e -> {
if ((String)mapSelectComboBox.getSelectedItem() == null) {
return;
}
@ -99,16 +145,19 @@ public class FormMapWithSetBattleship extends JComponent {
return;
}
int position = Integer.parseInt(finalMaskedTextFieldPosition.getText());
if (_mapBattleshipsCollectionGeneric.Subtraction(position) != null) {
JOptionPane.showMessageDialog(form, "Объект удален", "Удаление", JOptionPane.OK_CANCEL_OPTION);
bufferedImage = _mapBattleshipsCollectionGeneric.ShowSet();
if (_mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).Subtraction(position) != null) {
JOptionPane.showMessageDialog(form, "Объект удален", "Успех", JOptionPane.OK_CANCEL_OPTION);
bufferedImage = _mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).ShowSet();
repaint();
}
else{
JOptionPane.showMessageDialog(form, "Объект не удален", "Удаление", JOptionPane.OK_CANCEL_OPTION);
}
});
statusPanel.add(deleteAirbusButton);
statusPanel.add(deleteBattleshipButton);
JButton showStorageButton = new JButton("Хранилище");
showStorageButton.addActionListener(e -> {
@ -123,19 +172,19 @@ public class FormMapWithSetBattleship extends JComponent {
JButton showOnMapButton = new JButton("Карта");
showOnMapButton.addActionListener(e -> {
if (_mapBattleshipsCollectionGeneric == null)
if (listBoxMaps.getSelectedIndex() == -1)
{
return;
}
bufferedImage = _mapBattleshipsCollectionGeneric.ShowOnMap();
bufferedImage = _mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).ShowOnMap();
repaint();
});
statusPanel.add(showOnMapButton);
ActionListener moveButtonListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_mapBattleshipsCollectionGeneric == null)
if (listBoxMaps.getSelectedIndex() == -1)
{
return;
}
@ -156,7 +205,7 @@ public class FormMapWithSetBattleship extends JComponent {
dir = Direction.Right;
break;
}
bufferedImage = _mapBattleshipsCollectionGeneric.MoveObject(dir);
bufferedImage = _mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).MoveObject(dir);
}
};
@ -177,7 +226,27 @@ public class FormMapWithSetBattleship extends JComponent {
showGalleryButton.addActionListener(e -> {
new FormParameterClass();
});
JDialog dialog = new JDialog(form, "Dialog", true);
statusPanel.add(showGalleryButton);
JButton showDeleteObjButton = new JButton("Удаленные корабли");
showDeleteObjButton.addActionListener(e -> {
DrawningObjectBattleship battleship = _mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).GetDeleted();
if (battleship == null) {
JOptionPane.showMessageDialog(null, "Больше нет");
}
if (battleship != null) {
FormBattleship formBattleship = new FormBattleship(dialog);
formBattleship.SetBattleship(battleship);
dialog.setSize(800, 500);
dialog.setContentPane(formBattleship);
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
}
});
statusPanel.add(showDeleteObjButton);
statusPanel.add(moveUpButton);
statusPanel.add(moveDownButton);
statusPanel.add(moveLeftButton);
@ -185,7 +254,23 @@ public class FormMapWithSetBattleship extends JComponent {
form.getContentPane().add(this);
form.setVisible(true);
}
private void ReloadMaps(){
int index = listBoxMaps.getSelectedIndex();
listBoxMaps.removeAll();
mapsListModel.removeAllElements();
for (int i = 0; i < _mapsCollection.Keys().size(); i++){
mapsListModel.addElement(_mapsCollection.Keys().get(i));
}
if (mapsListModel.size() > 0 && (index == -1 || index >= mapsListModel.size())){
listBoxMaps.setSelectedIndex(0);
}
else if (mapsListModel.size() > 0 && index > -1 && index < mapsListModel.size())
{
listBoxMaps.setSelectedIndex(index);
}
listBoxMaps.setModel(mapsListModel);
statusPanel.repaint();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);

View File

@ -27,8 +27,8 @@ public class FormParameterClass extends JComponent {
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 ++) {
parameterClass = new MyParametrClass<EntityBattleship, IDrawningBlocks>(20, 20);
for (int i = 0; i < 20; 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))));
@ -40,7 +40,7 @@ public class FormParameterClass extends JComponent {
random.nextBoolean(), random.nextBoolean()));
}
}
for (int i = 0; i < 5; i ++) {
for (int i = 0; i < 20; i ++) {
int rnd = random.nextInt(3);
switch (rnd) {

View File

@ -1,5 +1,6 @@
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.LinkedList;
public class MapWithSetBattleshipsGeneric <T extends IDrawningObject, U extends AbstractMap>{
@ -8,12 +9,13 @@ public class MapWithSetBattleshipsGeneric <T extends IDrawningObject, U extends
/// Высота окна отрисовки
private final int _pictureHeight;
/// Размер занимаемого объектом места (ширина)
private final int _placeSizeWidth = 250;
private final int _placeSizeWidth = 210;
/// Размер занимаемого объектом места (высота)
private final int _placeSizeHeight = 100;
private final SetBattleshipGeneric<T> _setBattleship;
private final int _placeSizeHeight = 90;
public final SetBattleshipGeneric<T> _setBattleship;
/// Карта
private final U _map;
LinkedList<DrawningObjectBattleship> deletedBattleships = new LinkedList<>();
/// Конструктор
public MapWithSetBattleshipsGeneric(int picWidth, int picHeight, U map)
{
@ -30,7 +32,11 @@ public class MapWithSetBattleshipsGeneric <T extends IDrawningObject, U extends
}
public T Subtraction(int position)
{
return this._setBattleship.Remove(position);
T delObj = this._setBattleship.Remove(position);
if (delObj == null) return null;
deletedBattleships.add((DrawningObjectBattleship) delObj);
return delObj;
}
public BufferedImage ShowSet()
{
@ -90,7 +96,6 @@ public class MapWithSetBattleshipsGeneric <T extends IDrawningObject, U extends
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++)
{
@ -136,4 +141,8 @@ public class MapWithSetBattleshipsGeneric <T extends IDrawningObject, U extends
}
}
}
public DrawningObjectBattleship GetDeleted() {
if (deletedBattleships.isEmpty()) return null;
return deletedBattleships.remove(0);
}
}

43
MapsCollection.java Normal file
View File

@ -0,0 +1,43 @@
import java.util.ArrayList;
import java.util.HashMap;
public class MapsCollection {
final HashMap<String, MapWithSetBattleshipsGeneric<DrawningObjectBattleship, AbstractMap>> _mapStorages;
private final int _pictureWidth;
private final int _pictureHeight;
public ArrayList<String> Keys() {
return new ArrayList<String>(_mapStorages.keySet());
}
public MapsCollection(int pictureWidth, int pictureHeight)
{
_mapStorages = new HashMap<String, MapWithSetBattleshipsGeneric<DrawningObjectBattleship, AbstractMap>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
public void AddMap(String name, AbstractMap map)
{
if (!_mapStorages.containsKey(name))
{
_mapStorages.put(name, new MapWithSetBattleshipsGeneric<DrawningObjectBattleship, AbstractMap>(_pictureWidth, _pictureHeight, map));
}
// TODO Прописать логику для добавления
}
public void DelMap(String name)
{
if (_mapStorages.containsKey(name)) { _mapStorages.remove(name); }
}
public MapWithSetBattleshipsGeneric<DrawningObjectBattleship, AbstractMap> Get(String ind)
{
if (_mapStorages.containsKey(ind)) return _mapStorages.get(ind);
return null;
}
public DrawningObjectBattleship Get(String name, int pos)
{
if (_mapStorages.containsKey(name)) return _mapStorages.get(name)._setBattleship.Get(pos);
return null;
}
}

View File

@ -1,35 +1,37 @@
import java.io.Console;
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;
private final ArrayList<T> EntityArray;
private final ArrayList<U> BlocksArray;
int entitiesCount = 0;
int blocksCount = 0;
public MyParametrClass(int entitiesCount, int blocksCount) {
EntityArray = new Object[entitiesCount];
BlocksArray = new Object[blocksCount];
EntityArray = new ArrayList<T>();
BlocksArray = new ArrayList<U>();
}
public void Insert(T battleship){
if(entitiesCount < EntityArray.length) {
EntityArray[entitiesCount] = battleship;
EntityArray.add(battleship);
entitiesCount++;
}
}
public void Insert(U drawningBlocks){
if(blocksCount < BlocksArray.length) {
BlocksArray[blocksCount] = drawningBlocks;
BlocksArray.add(drawningBlocks);
blocksCount++;
}
}
public DrawningBattleship GetDrawningBattleship() {
Random random = new Random();
int entityIndex = random.nextInt(EntityArray.length);
int blockIndex = random.nextInt(BlocksArray.length);
int entityIndex = random.nextInt(EntityArray.size());
int blockIndex = random.nextInt( BlocksArray.size());
EntityBattleship battleship = (T)EntityArray[entityIndex];
IDrawningBlocks blocks = (U)BlocksArray[blockIndex];
EntityBattleship battleship = EntityArray.get(entityIndex);
IDrawningBlocks blocks = BlocksArray.get(blockIndex);
if (battleship instanceof EntityLinkor) {
return new DrawningLinkor(battleship, blocks);

View File

@ -1,50 +1,34 @@
import java.util.ArrayList;
public class SetBattleshipGeneric <T>
{
private final T[] _places;
public SetBattleshipGeneric(int count) {
_places = (T[]) new Object[count];
}
private final ArrayList<T> _places;
private final int _maxCount;
public int Count() {
return _places.length;
return _places.size();
}
public SetBattleshipGeneric(int count) {
_maxCount = count;
_places = new ArrayList<>();;
}
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;
if (position >= _maxCount|| position < 0) return -1;
_places.add(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;
if (position >= _places.size() || position < 0) return null;
T result = _places.get(position);
_places.remove(position);
return result;
}
@ -54,6 +38,10 @@ public class SetBattleshipGeneric <T>
{
return null;
}
return _places[position];
return _places.get(position);
}
public Iterable<T> GetBattleships()
{
return _places;
}
}