Port from base project
This commit is contained in:
parent
4d2ce83547
commit
95460faacd
@ -13,9 +13,15 @@ public class BomberForm extends JFrame
|
||||
private BomberRendererBase _bomberRenderer;
|
||||
private AbstractStrategy _strategy;
|
||||
|
||||
private BomberRendererBase SelectedRenderer;
|
||||
public BomberRendererBase GetSelectedRenderer() { return _bomberRenderer; }
|
||||
|
||||
public BomberForm()
|
||||
{
|
||||
InitializeComponent();
|
||||
|
||||
_strategy = null;
|
||||
SelectedRenderer = null;
|
||||
}
|
||||
|
||||
private void Draw()
|
||||
@ -38,11 +44,28 @@ public class BomberForm extends JFrame
|
||||
{
|
||||
Random random = new Random();
|
||||
|
||||
Color BodyColor = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256));
|
||||
Color AdditionalColor = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256));
|
||||
|
||||
JColorChooser BodyColorPicker = new JColorChooser(Color.BLACK);
|
||||
if (JOptionPane.showConfirmDialog(null, BodyColorPicker, "Выберите цвет", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE)
|
||||
== JOptionPane.OK_OPTION)
|
||||
{
|
||||
BodyColor = BodyColorPicker.getColor();
|
||||
}
|
||||
|
||||
JColorChooser AdditionalColorPicker = new JColorChooser(Color.BLACK);
|
||||
if (JOptionPane.showConfirmDialog(null, AdditionalColorPicker, "Выберите цвет", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE)
|
||||
== JOptionPane.OK_OPTION)
|
||||
{
|
||||
AdditionalColor = AdditionalColorPicker.getColor();
|
||||
}
|
||||
|
||||
_bomberRenderer = new BomberRenderer(
|
||||
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)),
|
||||
BodyColor,
|
||||
AdditionalColor,
|
||||
true,
|
||||
true,
|
||||
BomberPictureBox.getWidth(),
|
||||
@ -58,10 +81,19 @@ public class BomberForm extends JFrame
|
||||
{
|
||||
Random random = new Random();
|
||||
|
||||
Color BodyColor = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256));
|
||||
|
||||
JColorChooser BodyColorPicker = new JColorChooser(Color.BLACK);
|
||||
if (JOptionPane.showConfirmDialog(null, BodyColorPicker, "Выберите цвет", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE)
|
||||
== JOptionPane.OK_OPTION)
|
||||
{
|
||||
BodyColor = BodyColorPicker.getColor();
|
||||
}
|
||||
|
||||
_bomberRenderer = new BomberRendererBase(
|
||||
random.nextInt(100, 300),
|
||||
random.nextInt(1000, 3000),
|
||||
new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)),
|
||||
BodyColor,
|
||||
BomberPictureBox.getWidth(),
|
||||
BomberPictureBox.getHeight()
|
||||
);
|
||||
@ -96,17 +128,17 @@ public class BomberForm extends JFrame
|
||||
return;
|
||||
|
||||
_strategy.SetData(
|
||||
new ObjectEntityRenderer(_bomberRenderer),
|
||||
_bomberRenderer.MovableObject(),
|
||||
BomberPictureBox.getWidth(),
|
||||
BomberPictureBox.getHeight()
|
||||
);
|
||||
|
||||
MovementStrategyComboBox.setEnabled(false);
|
||||
}
|
||||
|
||||
if (_strategy == null)
|
||||
return;
|
||||
|
||||
MovementStrategyComboBox.setEnabled(false);
|
||||
|
||||
_strategy.MakeStep();
|
||||
Draw();
|
||||
|
||||
@ -157,6 +189,7 @@ public class BomberForm extends JFrame
|
||||
MovementStrategyComboBox = new JComboBox<String>();
|
||||
ButtonCreateBomber = new JButton();
|
||||
ButtonPerformMove = new JButton();
|
||||
SelectBomberButton = new JButton();
|
||||
//
|
||||
// BomberPictureBox
|
||||
//
|
||||
@ -238,14 +271,23 @@ public class BomberForm extends JFrame
|
||||
ButtonPerformMove.setFocusable(false);
|
||||
ButtonPerformMove.addActionListener(e -> ButtonPerformStep_Click(e));
|
||||
//
|
||||
// SelectBomberButton
|
||||
//
|
||||
SelectBomberButton.setBounds(354, 407, 159, 42);
|
||||
SelectBomberButton.setName("SelectBomberButton");
|
||||
SelectBomberButton.setText("Выбрать бомбардировщик");
|
||||
SelectBomberButton.setBackground(new Color(225, 225, 225));
|
||||
SelectBomberButton.setFont(new Font("Segoe UI", Font.PLAIN, 11));
|
||||
SelectBomberButton.setFocusable(false);
|
||||
//
|
||||
// BomberForm
|
||||
//
|
||||
setTitle("Бомбардировщик");
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
setSize(900, 500);
|
||||
setLayout(null);
|
||||
setLocationRelativeTo(null);
|
||||
setVisible(true);
|
||||
add(SelectBomberButton);
|
||||
add(ButtonPerformMove);
|
||||
add(ButtonCreateBomber);
|
||||
add(MovementStrategyComboBox);
|
||||
@ -266,4 +308,5 @@ public class BomberForm extends JFrame
|
||||
private JComboBox<String> MovementStrategyComboBox;
|
||||
private JButton ButtonCreateBomber;
|
||||
private JButton ButtonPerformMove;
|
||||
public JButton SelectBomberButton;
|
||||
}
|
||||
|
148
src/AirBomber/FormEntityCollection.java
Normal file
148
src/AirBomber/FormEntityCollection.java
Normal file
@ -0,0 +1,148 @@
|
||||
package AirBomber;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import AirBomber.Generics.EntitiesGenericCollection;
|
||||
import AirBomber.MovementStrategy.ObjectEntityRenderer;
|
||||
import AirBomber.Rendering.BomberRendererBase;
|
||||
|
||||
public class FormEntityCollection extends JFrame
|
||||
{
|
||||
private final EntitiesGenericCollection<BomberRendererBase, ObjectEntityRenderer> _entities;
|
||||
|
||||
public FormEntityCollection()
|
||||
{
|
||||
InitializeComponent();
|
||||
|
||||
_entities = new EntitiesGenericCollection<BomberRendererBase, ObjectEntityRenderer>(
|
||||
CollectionPictureBox.getWidth(), CollectionPictureBox.getHeight()
|
||||
);
|
||||
}
|
||||
|
||||
public void ButtonAddEntity_Click(ActionEvent e)
|
||||
{
|
||||
BomberForm Form = new BomberForm();
|
||||
|
||||
Form.SelectBomberButton.addActionListener(e2 -> {
|
||||
if (_entities.Add(Form.GetSelectedRenderer()) != -1)
|
||||
{
|
||||
JOptionPane.showMessageDialog(null, "Объект добавлен");
|
||||
CollectionPictureBox.setIcon(new ImageIcon(_entities.ShowEntities()));
|
||||
}
|
||||
else
|
||||
{
|
||||
JOptionPane.showMessageDialog(null, "Не удалось добавить объект");
|
||||
}
|
||||
|
||||
Form.dispose();
|
||||
});
|
||||
}
|
||||
|
||||
public void ButtonRemoveEntity_Click(ActionEvent e)
|
||||
{
|
||||
int Pos;
|
||||
try
|
||||
{
|
||||
Pos = Integer.parseInt(NumberMaskedTextBox.getText());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
JOptionPane.showMessageDialog(null, "Некорректный ввод");
|
||||
return;
|
||||
}
|
||||
|
||||
if (_entities.Remove(Pos) == true)
|
||||
{
|
||||
JOptionPane.showMessageDialog(null, "Объект удален");
|
||||
CollectionPictureBox.setIcon(new ImageIcon(_entities.ShowEntities()));
|
||||
}
|
||||
else
|
||||
{
|
||||
JOptionPane.showMessageDialog(null, "Не удалось удалить объект");
|
||||
}
|
||||
}
|
||||
|
||||
public void ButtonRefreshCollection_Click(ActionEvent e)
|
||||
{
|
||||
CollectionPictureBox.setIcon(new ImageIcon(_entities.ShowEntities()));
|
||||
}
|
||||
|
||||
private void InitializeComponent()
|
||||
{
|
||||
CollectionPictureBox = new JLabel();
|
||||
ToolGroupBox = new JPanel();
|
||||
RefreshCollectionButton = new JButton();
|
||||
RemoveBomberButton = new JButton();
|
||||
NumberMaskedTextBox = new JTextField();
|
||||
AddBomberButton = new JButton();
|
||||
//
|
||||
// CollectionPictureBox
|
||||
//
|
||||
CollectionPictureBox.setBounds(0, 0, 812, 603);
|
||||
//
|
||||
// ToolGroupBox
|
||||
//
|
||||
ToolGroupBox.add(RefreshCollectionButton);
|
||||
ToolGroupBox.add(RemoveBomberButton);
|
||||
ToolGroupBox.add(NumberMaskedTextBox);
|
||||
ToolGroupBox.add(AddBomberButton);
|
||||
ToolGroupBox.setBounds(818, 12, 210, 545);
|
||||
ToolGroupBox.setName("ToolGroupBox");
|
||||
ToolGroupBox.setBorder(BorderFactory.createTitledBorder("Инструменты"));
|
||||
ToolGroupBox.setLayout(null);
|
||||
//
|
||||
// RefreshCollectionButton
|
||||
//
|
||||
RefreshCollectionButton.setBounds(10, 190, 190, 30);
|
||||
RefreshCollectionButton.setName("RefreshCollectionButton");
|
||||
RefreshCollectionButton.setText("Обновить коллекцию");
|
||||
RefreshCollectionButton.setBackground(new Color(225, 225, 225));
|
||||
RefreshCollectionButton.setFont(new Font("Segoe UI", Font.PLAIN, 11));
|
||||
RefreshCollectionButton.setFocusable(false);
|
||||
RefreshCollectionButton.addActionListener(e -> ButtonRefreshCollection_Click(e));
|
||||
//
|
||||
// RemoveBomberButton
|
||||
//
|
||||
RemoveBomberButton.setBounds(10, 141, 190, 30);
|
||||
RemoveBomberButton.setName("RemoveBomberButton");
|
||||
RemoveBomberButton.setText("Удалить бомбардировщик");
|
||||
RemoveBomberButton.setBackground(new Color(225, 225, 225));
|
||||
RemoveBomberButton.setFont(new Font("Segoe UI", Font.PLAIN, 11));
|
||||
RemoveBomberButton.setFocusable(false);
|
||||
RemoveBomberButton.addActionListener(e -> ButtonRemoveEntity_Click(e));
|
||||
//
|
||||
// NumberMaskedTextBox
|
||||
//
|
||||
NumberMaskedTextBox.setBounds(10, 102, 190, 23);
|
||||
NumberMaskedTextBox.setName("NumberMaskedTextBox");
|
||||
//
|
||||
// AddBomberButton
|
||||
//
|
||||
AddBomberButton.setBounds(10, 32, 190, 43);
|
||||
AddBomberButton.setName("AddBomberButton");
|
||||
AddBomberButton.setText("Добавить бомбардировщик");
|
||||
AddBomberButton.setBackground(new Color(225, 225, 225));
|
||||
AddBomberButton.setFont(new Font("Segoe UI", Font.PLAIN, 11));
|
||||
AddBomberButton.setFocusable(false);
|
||||
AddBomberButton.addActionListener(e -> ButtonAddEntity_Click(e));
|
||||
//
|
||||
// FormEntityCollection
|
||||
//
|
||||
setTitle("Набор бомбардировщиков");
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
setSize(1050, 603);
|
||||
setLayout(null);
|
||||
setLocationRelativeTo(null);
|
||||
setVisible(true);
|
||||
add(ToolGroupBox);
|
||||
add(CollectionPictureBox);
|
||||
}
|
||||
|
||||
private JLabel CollectionPictureBox;
|
||||
private JPanel ToolGroupBox;
|
||||
private JButton RefreshCollectionButton;
|
||||
private JButton RemoveBomberButton;
|
||||
private JTextField NumberMaskedTextBox;
|
||||
private JButton AddBomberButton;
|
||||
}
|
105
src/AirBomber/Generics/EntitiesGenericCollection.java
Normal file
105
src/AirBomber/Generics/EntitiesGenericCollection.java
Normal file
@ -0,0 +1,105 @@
|
||||
package AirBomber.Generics;
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
import AirBomber.MovementStrategy.IMovableObject;
|
||||
import AirBomber.Rendering.BomberRendererBase;
|
||||
|
||||
public class EntitiesGenericCollection <T extends BomberRendererBase, U extends IMovableObject>
|
||||
{
|
||||
private final int _pictureWidth;
|
||||
private final int _pictureHeight;
|
||||
|
||||
private final int _placeSizeWidth = 200;
|
||||
private final int _placeSizeHeight = 200;
|
||||
|
||||
private final SetGeneric<T> _collection;
|
||||
|
||||
public EntitiesGenericCollection(int PictureWidth, int PictureHeight)
|
||||
{
|
||||
int width = PictureWidth / _placeSizeWidth;
|
||||
int height = PictureHeight / _placeSizeHeight;
|
||||
|
||||
_pictureWidth = PictureWidth;
|
||||
_pictureHeight = PictureHeight;
|
||||
|
||||
_collection = new SetGeneric<T>(width * height);
|
||||
}
|
||||
|
||||
public int Add(T obj)
|
||||
{
|
||||
if (obj == null)
|
||||
return -1;
|
||||
|
||||
return _collection.Insert(obj);
|
||||
}
|
||||
|
||||
public boolean Remove(int pos)
|
||||
{
|
||||
T obj = _collection.Get(pos);
|
||||
|
||||
if (obj != null)
|
||||
return _collection.Remove(pos);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public U GetU(int pos)
|
||||
{
|
||||
return (U)_collection.Get(pos).MovableObject();
|
||||
}
|
||||
|
||||
public BufferedImage ShowEntities()
|
||||
{
|
||||
BufferedImage bmp = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_ARGB);
|
||||
Graphics2D gr = bmp.createGraphics();
|
||||
|
||||
DrawBackground(gr);
|
||||
DrawObjects(gr);
|
||||
|
||||
return bmp;
|
||||
}
|
||||
|
||||
private void DrawBackground(Graphics2D g)
|
||||
{
|
||||
g.setColor(Color.BLACK);
|
||||
g.setStroke(new BasicStroke(3));
|
||||
|
||||
for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++)
|
||||
{
|
||||
for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j)
|
||||
g.drawLine(
|
||||
i * _placeSizeWidth, j * _placeSizeHeight,
|
||||
i * _placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight
|
||||
);
|
||||
|
||||
g.drawLine(
|
||||
i * _placeSizeWidth, 0,
|
||||
i * _placeSizeWidth, _pictureHeight / _placeSizeHeight * _placeSizeHeight
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private void DrawObjects(Graphics2D g)
|
||||
{
|
||||
g.setStroke(new BasicStroke(1));
|
||||
|
||||
for (int i = 0; i < _collection.Count(); i++)
|
||||
{
|
||||
T Entity = _collection.Get(i);
|
||||
|
||||
if (Entity == null)
|
||||
continue;
|
||||
|
||||
/** Установка позиции */
|
||||
int NumX = _pictureWidth / _placeSizeWidth;
|
||||
|
||||
int PosX = (NumX - 1 - i % NumX) * _placeSizeWidth;
|
||||
int PosY = (i / NumX) * _placeSizeHeight;
|
||||
|
||||
Entity.SetPosition(PosX, PosY);
|
||||
Entity.DrawEntity(g);
|
||||
}
|
||||
}
|
||||
}
|
64
src/AirBomber/Generics/SetGeneric.java
Normal file
64
src/AirBomber/Generics/SetGeneric.java
Normal file
@ -0,0 +1,64 @@
|
||||
package AirBomber.Generics;
|
||||
|
||||
public class SetGeneric <T extends Object>
|
||||
{
|
||||
private final T[] _objects;
|
||||
|
||||
public int Count() { return _objects.length; }
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public SetGeneric(int Count)
|
||||
{
|
||||
_objects = (T[]) new Object[Count];
|
||||
}
|
||||
|
||||
public int Insert(T Entity)
|
||||
{
|
||||
return Insert(Entity, 0);
|
||||
}
|
||||
|
||||
public int Insert(T Entity, int Pos)
|
||||
{
|
||||
if (Pos >= Count() || Pos < 0)
|
||||
return -1;
|
||||
|
||||
if (_objects[Pos] == null)
|
||||
{
|
||||
_objects[Pos] = Entity;
|
||||
return Pos;
|
||||
}
|
||||
|
||||
/** Сдвиг элементов вправо начиная с Pos до ближайшего пустого места */
|
||||
int EmptyPos = -1;
|
||||
|
||||
for (int i = Pos + 1; i < Count(); i++)
|
||||
if (_objects[i] == null)
|
||||
{
|
||||
EmptyPos = i;
|
||||
break;
|
||||
}
|
||||
|
||||
if (EmptyPos == -1)
|
||||
return -1;
|
||||
|
||||
_objects[EmptyPos] = Entity;
|
||||
return EmptyPos;
|
||||
}
|
||||
|
||||
public boolean Remove(int Pos)
|
||||
{
|
||||
if (Pos >= Count())
|
||||
return false;
|
||||
|
||||
_objects[Pos] = null;
|
||||
return true;
|
||||
}
|
||||
|
||||
public T Get(int Pos)
|
||||
{
|
||||
if (Pos >= Count())
|
||||
return null;
|
||||
|
||||
return _objects[Pos];
|
||||
}
|
||||
}
|
@ -3,6 +3,6 @@ package AirBomber;
|
||||
public class Program {
|
||||
public static void main(String[] args) throws Exception
|
||||
{
|
||||
new BomberForm();
|
||||
new FormEntityCollection();
|
||||
}
|
||||
}
|
||||
|
@ -26,6 +26,8 @@ public class BomberRendererBase
|
||||
public int EntityWidth() { return _bomberWidth; }
|
||||
public int EntityHeight() { return _bomberHeight; }
|
||||
|
||||
public IMovableObject MovableObject() { return new ObjectEntityRenderer(this); }
|
||||
|
||||
public BomberRendererBase(int Speed, double Weight, Color BodyColor, int Width, int Height)
|
||||
{
|
||||
if (Width < _bomberWidth || Height < _bomberHeight)
|
||||
|
Loading…
Reference in New Issue
Block a user